1

你好,

我正在克隆一个工作树作为一个裸仓库nodegit

Git.Clone('/work/localrepo', '/git/newbare', {bare: 1})

这会创建一个裸仓库,就像

# in /git/newbare
> git clone --bare /work/localrepo

注意:newbare对 中的所有localrepo分支都有引用/git/newbare/refs/remote/origin,但只有localrepo's活动分支被克隆到newbare's本地引用中

这意味着如果localrepo克隆时在master上,则newbare仅跟踪master

现在我可以git跟踪所有分支origin

#in bare

> git fetch origin '+refs/*;refs/*'

>>> /git/newbare
    From /work/localrepo
      * [new branch]      feature     -> feature

如果您想在此处获取有关获取的更多信息,请查看refspec 。

问题

我不知道如何在裸目录中设置跟踪分支,所有分支都remote/origin使用nodegit

4

1 回答 1

1

知道了

我写了这个并解决了我的问题

module.exports = {
    async trackRemoteBranch(repo, remote_name, branch_name) {

        let remote_path = path.join(remote_name, branch_name)

        let branch_commit = await repo.getBranchCommit(remote_path)
        let branch_ref = await repo.createBranch(branch_name, branch_commit)
        await Branch.setUpstream(branch_ref, remote_path)
        return true

    },
    /**
     * refs/remotes/origin/master =>
     * {
     *  remote_name: origin,
     *  branch_name: master,
     * }
     * very rough solution
     */
    async cleaveRef(remote_ref) {
        let parts = remote_ref.split('/')
        let [remote_name, branch_name] = parts.slice(parts.length - 2)
        return {
            remote_name,
            branch_name
        }
    },
    async getRemoteReferences(repo) {
        let refs = await repo.getReferences()
        let remote_refs = refs
            .filter((ref) => ref.isRemote())
            .map(ref => ref.name())
        return remote_refs
    },
    async trackAll(repo) {
        (await this.getRemoteReferences(repo))
            .map(ref => this.cleaveRef(ref))
            .forEach(async ({remote_name, branch_name}) => {
                try {
                    await this.trackRemoteBranch(config, remote_name, branch_name)
                } catch (err) {}
            })
        return true
    }
}

如果有人觉得这很有趣,我可以尽力解释。现在,来吧。有一个好人!

于 2020-05-08T00:02:20.713 回答