Link Search Menu Expand Document

Clone a repo

const git = require('nodegit')
const fs = require('fs-extra')
const { URL } = require('url')
const REPO_URL = 'git@github.com:org/path.git'
const CLONE_DIR = '/tmp/private-repo-clone-test'

;(async () => {
  await fs.emptyDir(CLONE_DIR)
  let authAttempted = false
  await git.Clone.clone(REPO_URL, CLONE_DIR, {
    fetchOpts: {
      callbacks: {
        certificateCheck: () => 1,
        credentials: (url, username) => {
          if (authAttempted) return git.Cred.defaultNew()
          authAttempted = true
          if (url.startsWith('https://') && url.includes('@')) {
            url = new URL(url)
            return git.Cred.userpassPlaintextNew(url.username, url.password)
          } else {
            return git.Cred.sshKeyFromAgent(username)
          }
        }
      },
    },
  })
})()

Add initial commit

var nodegit = require("../");
var path = require("path");
var fse = require("fs-extra");
var fileName = "newfile.txt";
var fileContent = "hello world";
var repoDir = "../../newRepo";

var repository;
var index;

fse.ensureDir(path.resolve(__dirname, repoDir))
.then(function() {
  return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
})
.then(function(repo) {
  repository = repo;
  return fse.writeFile(path.join(repository.workdir(), fileName), fileContent);
})
.then(function(){
  return repository.refreshIndex();
})
.then(function(idx) {
  index = idx;
})
.then(function() {
  return index.addByPath(fileName);
})
.then(function() {
  return index.write();
})
.then(function() {
  return index.writeTree();
})
.then(function(oid) {
  var author = nodegit.Signature.now("Scott Chacon",
    "schacon@gmail.com");
  var committer = nodegit.Signature.now("Scott A Chacon",
    "scott@github.com");

  // Since we're creating an inital commit, it has no parents. Note that unlike
  // normal we don't get the head either, because there isn't one yet.
  return repository.createCommit("HEAD", author, committer, "message", oid, []);
})
.done(function(commitId) {
  console.log("New Commit: ", commitId);
});

Create remote

const remoteRepository = NodeGit.Remote.create(localRepository, 'origin', 'git@github.com:nodegit/nodegit.git');

Check remotes

const refs = await repo.getReferences();
const remoteRefs = refs.filter(r => r.isRemote() === 1);
return remoteRefs.map(r => r.name());

Copyright © 2020 Thence LLC