How to use nodegit - 10 common examples

To help you get started, we’ve selected a few nodegit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github twosigma / git-meta / test / util / read_repo_ast_util.js View on Github external
let index = yield NodeGit.Merge.commits(repo,
                                                masterCommit,
                                                whamCommit,
                                                null);

        // Have to force set the submodule to the 'bar' commit.

        yield NodeGit.Reset.reset(subRepo,
                                  localBar,
                                  NodeGit.Reset.TYPE.HARD);

        yield index.conflictCleanup();
        yield index.writeTreeTo(repo);
        yield NodeGit.Checkout.index(repo, index, {
            checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE,
        });
        index = yield repo.index();
        yield index.addByPath("s");
        yield index.write();
        const id = yield index.writeTreeTo(repo);
        const mergeCommit = yield repo.createCommit(
                                                 "HEAD",
                                                 sig,
                                                 sig,
                                                 "message",
                                                 id,
                                                 [ masterCommit, whamCommit ]);
        const mergeSha = mergeCommit.tostrS();
        const Commit = RepoAST.Commit;
        const Submodule = RepoAST.Submodule;
github DefinitelyTyped / DefinitelyTyped / types / nodegit / nodegit-tests.ts View on Github external
let result = Git.Attr.addMacro(repo, "name", "values");

Git.Attr.cacheFlush(repo);

Git.Attr.get(repo, 1, "path", "name").then((string) => {
    // Use string
});

const array = Git.Attr.getMany(repo, 1, "path", 1, "names");

result = Git.Attr.value("attr");

const blameOptions = new Git.BlameOptions();

Git.Branch.lookup(repo, "branch_name", Git.Branch.BRANCH.LOCAL).then((reference) => {
    // Use reference
});

repo.getCommit("0123456789abcdef0123456789abcdef").then((commit) => {
  const sig = Git.Signature.now('John Doe', 'jd@example.com');
  const newCommit: Promise = commit.amend('ref', sig, sig, 'utf8', 'message', tree);
});

const signature = Git.Signature.now("name", "email");
signature.name();
signature.email();
signature.when();

repo.createBlobFromBuffer(Buffer.from("test")).then((oid: Git.Oid) => oid.cpy());
repo.commondir();
github twosigma / git-meta / test / util / close.js View on Github external
const subRepo = yield sub.open();
        const origin = yield subRepo.getRemote("origin");
        yield origin.connect(NodeGit.Enums.DIRECTION.FETCH,
                             new NodeGit.RemoteCallbacks(),
                             function () {});
                             yield subRepo.fetch("origin", {});
        subRepo.setHeadDetached(subHead.id().tostrS());
        yield sub.addFinalize();

        // Commit the submodule it.

        yield TestUtil.makeCommit(repo, ["x/y", ".gitmodules"]);

        // Verify that the status currently indicates a visible submodule.

        const addedStatus = yield NodeGit.Submodule.status(repo, "x/y", 0);
        const WD_UNINITIALIZED = (1 << 7);  // means "closed"
        assert(!(addedStatus & WD_UNINITIALIZED));

        // Then close it and recheck status.

        yield close.close(repo, "x/y");
        const closedStatus = yield NodeGit.Submodule.status(repo, "x/y", 0);
        assert(closedStatus & WD_UNINITIALIZED);

        // Make sure we can reopen it.

        yield sub.init(1);
        yield sub.repoInit(1);
        const reopenedStat = yield NodeGit.Submodule.status(repo, "x/y", 0);
        assert(!(reopenedStat & WD_UNINITIALIZED));
    }));
github twosigma / git-meta / test / util / read_repo_ast_util.js View on Github external
const whamCommit = yield TestUtil.makeCommit(repo, ["s"]);
        const whamSha = whamCommit.id().tostrS();

        // Go back to master and put a different submodule change on it.

        yield repo.checkoutBranch("master");
        const localFoo = yield subRepo.getCommit(fooSha);
        yield NodeGit.Reset.reset(subRepo,
                                  localFoo,
                                  NodeGit.Reset.TYPE.HARD);
        const masterCommit = yield TestUtil.makeCommit(repo, ["s"]);
        const masterSha = masterCommit.id().tostrS();

        // Now make the merge commit.

        let index = yield NodeGit.Merge.commits(repo,
                                                masterCommit,
                                                whamCommit,
                                                null);

        // We're going to ignore the 'bar' commit.

        yield index.conflictCleanup();
        yield index.writeTreeTo(repo);
        yield NodeGit.Checkout.index(repo, index, {
            checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE,
        });
        index = yield repo.index();
        yield index.write();
        const id = yield index.writeTreeTo(repo);
        const mergeCommit = yield repo.createCommit(
                                                 "HEAD",
github DefinitelyTyped / DefinitelyTyped / types / nodegit / nodegit-tests.ts View on Github external
import * as Git from 'nodegit';

Git.Repository.discover("startPath", 1, "ceilingDirs").then((string) => {
    // Use string
});

Git.Repository.init("path", 0).then((repository) => {
    // Use repository
});

const repo = new Git.Repository();
const id = new Git.Oid();
const ref = new Git.Reference();
const tree = new Git.Tree();

tree.walk().start();
tree.getEntry("/").then(entry => {
    // Use entry
});

// AnnotatedCommit Tests

Git.AnnotatedCommit.fromFetchhead(repo, "branch_name", "remote_url", id).then((annotatedCommit) => {
    // Use annotatedCommit
});

Git.AnnotatedCommit.fromRef(repo, ref).then((annotatedCommit) => {
github twosigma / git-meta / node / lib / util / test_util.js View on Github external
const sig = bare.defaultSignature();
    for (let i = 0; i < refs.length; ++i) {
        const ref = refs[i];
        const shorthand = ref.shorthand();
        if (ref.isBranch() && !(shorthand in existingBranches)) {
            yield bare.createBranch(ref.shorthand(),
                                    ref.target(),
                                    1,
                                    sig,
                                    "i made a branch");
        }
    }

    // And then remove the original remote.

    yield NodeGit.Remote.delete(bare, "origin");
    return bare;
});
github DefinitelyTyped / DefinitelyTyped / types / nodegit / nodegit-tests.ts View on Github external
const id = new Git.Oid();
const ref = new Git.Reference();
const tree = new Git.Tree();

tree.walk().start();
tree.getEntry("/").then(entry => {
    // Use entry
});

// AnnotatedCommit Tests

Git.AnnotatedCommit.fromFetchhead(repo, "branch_name", "remote_url", id).then((annotatedCommit) => {
    // Use annotatedCommit
});

Git.AnnotatedCommit.fromRef(repo, ref).then((annotatedCommit) => {
    // Use annotatedCommit
});

Git.AnnotatedCommit.fromRevspec(repo, "revspec").then((annotatedCommit) => {
    // Use annotatedCommit
});

Git.AnnotatedCommit.lookup(repo, id).then((annotatedCommit) => {
    // Use annotatedCommit
    annotatedCommit.free();
    annotatedCommit.id();
});

// Attr tests

let result = Git.Attr.addMacro(repo, "name", "values");
github DefinitelyTyped / DefinitelyTyped / types / nodegit / nodegit-tests.ts View on Github external
// Use repository
});

const repo = new Git.Repository();
const id = new Git.Oid();
const ref = new Git.Reference();
const tree = new Git.Tree();

tree.walk().start();
tree.getEntry("/").then(entry => {
    // Use entry
});

// AnnotatedCommit Tests

Git.AnnotatedCommit.fromFetchhead(repo, "branch_name", "remote_url", id).then((annotatedCommit) => {
    // Use annotatedCommit
});

Git.AnnotatedCommit.fromRef(repo, ref).then((annotatedCommit) => {
    // Use annotatedCommit
});

Git.AnnotatedCommit.fromRevspec(repo, "revspec").then((annotatedCommit) => {
    // Use annotatedCommit
});

Git.AnnotatedCommit.lookup(repo, id).then((annotatedCommit) => {
    // Use annotatedCommit
    annotatedCommit.free();
    annotatedCommit.id();
});
github ksylor / git-live-view / src / rebase.js View on Github external
const ontoFileContents = await readFileAsync(repoPath + FILE_REBASE_ONTO, {encoding: 'utf8'});
    // get the rebase command text file which will give us a list of
    // the effected commits
    const todoFileContents = await readFileAsync(repoPath + FILE_REBASE_TODO, {encoding: 'utf8'});

    if (rebaseHeadFileContents.trim() === ontoFileContents.trim()) {
        // not sure if you'd get into this state?
        console.log('wtf are you even rebasing bro??');
    }

    // try to figure out what branch the onto commit is part of.
    // is it the head of another branch or is the rebase branch
    // getting rebased against itself?
    const rebaseHeadCommit = await nodegit.Commit.lookup(repo, rebaseHeadFileContents.trim());
    const rebaseBranch = await nodegit.Reference.lookup(repo, rebaseHeadNameFileContents.trim());
    const ontoCommit = await nodegit.Commit.lookup(repo, ontoFileContents);

    // check and see if the commit is in the current branch
    const foundIndex = await utils.searchHistoryForCommit(repo, rebaseHeadCommit, ontoCommit.id());
    if (foundIndex >= 0) {
        let branchHistory = await branches.getNormalizedSingleBranchHistory(repo, rebaseHeadCommit, rebaseBranch, foundIndex + 3, true, settings);

        // tag the affected commits
        branchHistory.local.history = await setAffectedCommits(repo, todoFileContents, branchHistory.local.history);

        // finally, tag the onto commit
        branchHistory.local.history[foundIndex].isRebaseOnto = true;

        return { isRebase: true, isMultiBranch: false, ...branchHistory };
    }

    // if the commit isn't in the current branch already,
github twosigma / git-meta / node / lib / util / destitch_util.js View on Github external
assert.isString(commitish);
    assert.isString(metaRemoteName);
    if (null !== targetRefName) {
        assert.isString(targetRefName);
    }

    const annotated = yield GitUtil.resolveCommitish(repo, commitish);
    if (null === annotated) {
        throw new UserError(`\
Could not resolve '${commitish}' to a commit.`);
    }
    const commit = yield repo.getCommit(annotated.id());
    if (!(yield GitUtil.isValidRemoteName(repo, metaRemoteName))) {
        throw new UserError(`Invalid remote name: '${metaRemoteName}'.`);
    }
    const remote = yield NodeGit.Remote.lookup(repo, metaRemoteName);
    const baseUrl = remote.url();

    const newlyStitched = {};

    console.log("Destitching");

    const result = yield exports.destitchChain(repo,
                                               commit,
                                               baseUrl,
                                               newlyStitched);
    const resultCommit = yield repo.getCommit(result);

    // Push synthetic-refs

    console.log("Pushing synthetic refs");
    yield exports.pushSyntheticRefs(repo, baseUrl, resultCommit, newlyStitched);