首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >git.push()工作,但github上的提交为空

git.push()工作,但github上的提交为空
EN

Stack Overflow用户
提问于 2020-08-04 05:50:11
回答 1查看 527关注 0票数 2

我在浏览器中使用同构的git(现在使用Chrome)来尝试在github上保存代码。

在我克隆了一个私有github存储库之后,我尝试向它添加新的文件,进行提交,并将其推回同一个私有的Github存储库。

但是,当我检查Github时,提交会被记录下来,但是完全是空的!

它就像我添加的文件,由于某种原因没有链接到提交,而且提交不知道它们.

不过,我从推送中得到了一个肯定的答复:

代码语言:javascript
复制
{
    headers: {
        cache-control: "no-cache, max-age=0, must-revalidate"
        content-type: "application/x-git-receive-pack-result"
        date: "Tue, 04 Aug 2020 04:58:59 GMT"
        expires: "Fri, 01 Jan 1980 00:00:00 GMT"
        pragma: "no-cache"
        server: "cloudflare"
        vary: "Accept-Encoding"
        x-github-request-id: "CE24:0E4C:16E8D5:2B275C:5F28EB11"
    ok: true
    refs: {
        refs/heads/master: {
            error: ""
            ok: true
        }
    }
}

我觉得很奇怪..。但是提交和它的消息显示在github上。

我做错了什么?我想我做了所有必要的事情但也许我错过了什么..。

这是我的密码:

代码语言:javascript
复制
    const stagingRoot = '/staging'
    ...
    import('https://unpkg.com/isomorphic-git@1.7.4/http/web/index.js').then(http => {
      window.http = http
      return true
    }).then(response => {
      // Initialize isomorphic-git with a file system
      window.fs = new LightningFS('fs')
      // Using the Promisified version
      window.pfs = window.fs.promises
      return true
    }).then(response => {
      const buttonSpace = document.getElementById('git_button_space')
      const repositoryURL = appendText({value:'https://github.com/<my private repository path>.git'},document.getElementById('git_repository_url_space'))
      let branch = appendDropdown(
        {items:[{label:'master',value:'master'}],value:'master'},
        document.getElementById('git_repository_branch_space'),
        function(event) {}
      )
      const username = appendText({value:'<my user name>'},document.getElementById('user-js-git_username_space'))
      const password = appendText({value:'<my app key>'},document.getElementById('user-js-git_password_space'))
      const path = appendText({value:'/git/git_app2'},document.getElementById('user-js-git_repository_path_space'))
      const pushButton = appendButton('プッシュ',buttonSpace, function(event) {
        return pfs.du(stagingRoot)
        .then(diskUsage => {
          return pfs.rmdir(stagingRoot,{recursive: true})
        })
        .catch(error => {
          return true
        }).then(response => {
          return pfs.mkdir(stagingRoot)
          .catch(error => {
            return true
          })
        }).then(response => {
          return git.clone({
            fs,
            http,
            dir:stagingRoot,
            corsProxy: 'https://cors.isomorphic-git.org',
            url: repositoryURL.getValue(),
            ref: branch.getValue(),
            onAuth: url => {
              const auth = {
                username: username.getValue(),
                password: password.getValue(),
              }
              return auth
            },
            singleBranch: true,
            depth: 100
          })
        }).then(response => {
          return pfs.du(stagingRoot+path.getValue())
            .then(diskUsage => {
              return true
            })
            .catch(error => {
              return pfs.mkdir(stagingRoot+path.getValue(),{recursive:true})
              .catch(error => {
                return true
              })
            })
          }).then(response => {
            return git.add({ fs, dir: stagingRoot+'/git', filepath: 'git_app2' })
          }).then(response => {
            return pfs.writeFile(stagingRoot+path.getValue()+'/package.json', JSON.stringify({key1:val1,key2:val2}))
          }).then(response => {
            return git.add({ fs, dir: stagingRoot+path.getValue(), filepath: 'package.json' })
          }).then(response => {
            const user = kintone.getLoginUser()
            return swal.fire({
              input: 'textarea',
              inputPlaceholder: 'commit message',
              inputAttributes: {
                'aria-label': 'commit message'
              },
              showCancelButton: true
            }).then(commitMessageResponse =>{
              if(commitMessageResponse.isConfirmed) {
                return git.commit({
                  fs,
                  dir: stagingRoot,
                  author: {
                    name: user.name,
                    email: user.email,
                  },
                  message: commitMessageResponse.value,
                  ref: branch.getValue(),
                })
              } else {
                return false
              }
            })
          }).then(response => {
            return git.push({
              fs,
              http,
              dir: stagingRoot,
              corsProxy: 'https://cors.isomorphic-git.org',
              remote: 'origin',
              ref: branch.getValue(),
              onAuth: url => {
                const auth = {
                  username: username.getValue(),
                  password: password.getValue(),
                }
                return auth
              },
            })
          }).then(response => {
            console.log(response)
          })
      })

我使用isomorphic-git和它的FS库--fs,脚本标记中包括:

代码语言:javascript
复制
<script src="https://unpkg.com/@isomorphic-git/lightning-fs"></script>
<script src="https://unpkg.com/isomorphic-git@1.7.4"></script>

我还像下面这样动态导入isomorphic-git's http库,因为我的环境(Kintone)不喜欢ES6:

代码语言:javascript
复制
import('https://unpkg.com/isomorphic-git@1.7.4/http/web/index.js').then(http => {
    window.http = http
    return true
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-05 02:04:31

我解决了问题,因为有三个问题:

  1. 显然,闪电-fs没有为rmdir实现递归,所以我编写了自己的:

代码语言:javascript
复制
function recursive_rmdir(path) {
  return pfs.readdir(path).then(fileList=> {
    const allPromises = []
    fileList.forEach(file => {
      allPromises.push(pfs.lstat(path+'/'+file).then(fileInfo => {
        if(fileInfo.isDirectory()) {
          return recursive_rmdir(path+'/'+file)
        } else {
          return pfs.unlink(path+'/'+file)
        }
      }))
    })
    return Promise.all(allPromises).then(response => {
      return pfs.rmdir(path)
    })
  })
}

  1. 显然,lightning-fs没有为mkdir实现递归,所以我编写了自己的:

代码语言:javascript
复制
function recursive_mkdir(path) {
  function mkdir(existing_path,recursive_path) {
    const new_path = existing_path+recursive_path[0]+'/'
    return pfs.du(new_path)
      .then(diskUsage => {
        return true
      })
      .catch(error => {
        return pfs.mkdir(new_path)
          .catch(error => {
            console.log('error',error)
            return false
          })
      }).then(response => {
        if (recursive_path.length > 1) {
          return mkdir(new_path,recursive_path.slice(1))
        }
        return true
      })
  }
  return mkdir('',path.split('/'))
}

  1. 我误解了应该传递什么作为add()命令的参数,回想起来,这应该是显而易见的,但这就是为什么在文档中只放一些琐碎的例子从来都不是一个好主意,也不要这么简单的例子来澄清用例!那我犯了什么错?让我们看看参数:
代码语言:javascript
复制
git.add({
    fs: <a file system implementation client>,
    dir: '<The working tree directory path>',
    filepath: '<The path to the file to add to the index>'
})

API文档中提供的示例代码只有filepath的文件名,因为它位于工作树目录的。当我编写代码时,我无意中做了同样的事情:

代码语言:javascript
复制
...
}).then(response => {
    return pfs.writeFile(stagingRoot+path.getValue()+'/package.json', JSON.stringify({key1:val1,key2:val2}))
}).then(response => {
    return git.add({ fs, dir: stagingRoot+path.getValue(), filepath: 'package.json' })
}).then(response => {
...

但是工作目录dir只应该是stagingRoot!路径值应该插入到filepath中,如下所示:

代码语言:javascript
复制
...
}).then(response => {
    return pfs.writeFile(stagingRoot+path.getValue()+'/package.json', JSON.stringify({key1:val1,key2:val2}}))
}).then(response => {
    return git.add({ fs, dir: stagingRoot, filepath: path.getValue().slice(1)+'/'+'package.json' })
}).then(response => {
...

.slice(1)是要起飞领先的/,因为filepath需要一个相对的路径!

我终于发现了我的错误,因为当我使用.git列出目录的内容时,package.json旁边出现了一个readdir() .

我希望这将对某人有所帮助,所以我也发布了我的最后代码的摘录:

代码语言:javascript
复制
      const pushButton = appendButton('プッシュ',buttonSpace, function(event) {
        return pfs.du(stagingRoot)
        .then(diskUsage => {
          return recursive_rmdir(stagingRoot)
        })
        .catch(error => {
          return true
        }).then(response => {
          return pfs.mkdir(stagingRoot)
          .catch(error => {
            console.log(error)
            return true
          })
        }).then(response => {
          return git.clone({
            fs,
            http,
            dir:stagingRoot,
            corsProxy: 'https://cors.isomorphic-git.org',
            url: repositoryURL.getValue(),
            ref: branch.getValue(),
            onAuth: url => {
              const auth = {
                username: username.getValue(),
                password: password.getValue(),
              }
              return auth
            },
            singleBranch: true,
            depth: 100
          })
        }).then(response => {
          return recursive_mkdir(stagingRoot+path.getValue())
            .catch(error => {
              console.log(error)
              return true
            })
          }).then(response => {
            return pfs.writeFile(stagingRoot+path.getValue()+'/package.json', JSON.stringify({key1:val1,key2:val2}))
          }).then(response => {
            return git.add({ fs, dir: stagingRoot, filepath: path.getValue().slice(1)+'/'+'package.json' })

          }).then(response => {
            const user = kintone.getLoginUser()
            return swal.fire({
              input: 'textarea',
              inputPlaceholder: 'コミットのメッセージここに入力してください',
              inputAttributes: {
                'aria-label': 'コミットのメッセージここに入力してください'
              },
              showCancelButton: true
            }).then(commitMessageResponse =>{
              if(commitMessageResponse.isConfirmed) {
                return git.commit({
                  fs,
                  dir: stagingRoot,
                  author: {
                    name: user.name,
                    email: user.email,
                  },
                  message: commitMessageResponse.value,
                  ref: branch.getValue(),
                })
              } else {
                return false
              }
            })
          }).then(response => {
            return git.push({
              fs,
              http,
              dir: stagingRoot,
              corsProxy: 'https://cors.isomorphic-git.org',
              remote: 'origin',
              ref: branch.getValue(),
              onAuth: url => {
                const auth = {
                  username: username.getValue(),
                  password: password.getValue(),
                }
                return auth
              },
            })
          })
      })

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63240930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档