Nodegit: How to list all remote branches?

Created on 6 Nov 2017  路  5Comments  路  Source: nodegit/nodegit

There is this Git command where you can get a list of all remote branches

git ls-remote [url]

Is this possible with NodeGit?

Most helpful comment

@Mklueh, I did it this way

  1. First, you need to create Repository object

_for simplify I skip promises (e.g. then())_

// /tmp/remote - is a repository directory and next specifies is_bar option
// this is like `git init --bare /tmp/remote`
const localRepository = NodeGit.Repository.init('/tmp/remote', 1);
  1. Then create Remote object of this repository
// this is like `git remote add origin [email protected]:nodegit/nodegit.git`
const remoteRepository = NodeGit.Remote.create(localRepository, 'origin', '[email protected]:nodegit/nodegit.git');
  1. Perform connect to remote repository
remoteRepository.connect(NodeGit.Enums.DIRECTION.FETCH, {
  certificateCheck() { return 1; },
  credentials(url, username) {
    return new NodeGit.Cred.sshKeyNew(username, '~/.ssh/github_rsa.pub', '~/.ssh/github_rsa', '');
  }
});
  1. Next, request remote reference list
const references = remoteRepository.referenceList();
  1. Finally output useful information
references.forEach(reference => {
  console.log(`${reference.name()}: ${reference.oid().toString()}`);
});

Result something like

HEAD: 6f7d38184c05acbfd616b550246420c7c5e7b9a9
refs/heads/master: 6f7d38184c05acbfd616b550246420c7c5e7b9a9
refs/heads/refactoring: 60404998ac705d662211266c4c3f49479c62c90b

All 5 comments

@Mklueh I think you are referring to #1218?

Seems to go in the right direction, but I can麓t find anything in the documentation of NodeGit covering it.
I麓m looking for getting a list of all branches without checking the repository out locally

@Mklueh, I did it this way

  1. First, you need to create Repository object

_for simplify I skip promises (e.g. then())_

// /tmp/remote - is a repository directory and next specifies is_bar option
// this is like `git init --bare /tmp/remote`
const localRepository = NodeGit.Repository.init('/tmp/remote', 1);
  1. Then create Remote object of this repository
// this is like `git remote add origin [email protected]:nodegit/nodegit.git`
const remoteRepository = NodeGit.Remote.create(localRepository, 'origin', '[email protected]:nodegit/nodegit.git');
  1. Perform connect to remote repository
remoteRepository.connect(NodeGit.Enums.DIRECTION.FETCH, {
  certificateCheck() { return 1; },
  credentials(url, username) {
    return new NodeGit.Cred.sshKeyNew(username, '~/.ssh/github_rsa.pub', '~/.ssh/github_rsa', '');
  }
});
  1. Next, request remote reference list
const references = remoteRepository.referenceList();
  1. Finally output useful information
references.forEach(reference => {
  console.log(`${reference.name()}: ${reference.oid().toString()}`);
});

Result something like

HEAD: 6f7d38184c05acbfd616b550246420c7c5e7b9a9
refs/heads/master: 6f7d38184c05acbfd616b550246420c7c5e7b9a9
refs/heads/refactoring: 60404998ac705d662211266c4c3f49479c62c90b

Do you know how to get remote branch by name and delete it if exists?
I've tried
.then(() => nodegit.Remote.lookup(repo, 'origin'))
.then((remoteResult) => {
console.log('remote Configured');
remote = remoteResult;
return remote.connect(nodegit.Enums.DIRECTION.PUSH, callbacks);
})
.then(() => {
console.log('remote Connected?', remote.connected());

        return remote.push(
            'refs/heads/' + branchName
        );
    })

But does not work. please helper me out. thanks

If you want find out all the remote branch based on local repo

const listRemoteBranch = async (pathToRepo) => {
    const repo = await NodeGit.Repository.open(pathToRepo);
    const refs = await repo.getReferences();
    const remoteRefs = refs.filter(r => r.isRemote() === 1);

    return remoteRefs.map(r => r.name());
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

calvinhmw picture calvinhmw  路  5Comments

Sergeeeek picture Sergeeeek  路  9Comments

xiemms picture xiemms  路  5Comments

DawsonG picture DawsonG  路  8Comments

Yezior picture Yezior  路  8Comments