简体   繁体   中英

git-clone a bare repo with nodegit

Hi,

I am cloning a worktree as a bare repo in nodegit

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

This creates a bare repo just like

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

Note: newbare has refs to all localrepo branches in /git/newbare/refs/remote/origin , but only localrepo's active branch is cloned in the newbare's local refs

That means that if localrepo was on master when it was cloned, then newbare is only tracking master

Now in git I can track all branches on origin with

#in bare

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

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

Look at refspec if you want more info on the fetch here.

Problem

I can't figure out how to setup tracking branches in a bare directory, for all branches on remote/origin with nodegit

Got it

I wrote this and solved my problem

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
    }
}

If anyone finds this interesting, I can do my best to explain. For now, here ya go. Have a good one guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM