Convert Figma logo to code with AI

nodegit logonodegit

Native Node bindings to Git.

5,636
691
5,636
373

Top Related Projects

9,602

A cross-platform, linkable library implementation of Git that you can use in your application.

A pure JavaScript implementation of git for node and browsers!

4,907

Project has been moved to: https://github.com/go-git/go-git

2,040

Pure-Python Git implementation

1,606

Python bindings for libgit2

Quick Overview

NodeGit is a native Node.js binding to the libgit2 library, providing asynchronous Git operations in JavaScript. It allows developers to interact with Git repositories programmatically, enabling the creation of Git-based applications and tools within the Node.js ecosystem.

Pros

  • High performance due to native bindings to libgit2
  • Comprehensive Git functionality, including repository management, commit operations, and branch handling
  • Asynchronous API design, well-suited for Node.js applications
  • Active development and community support

Cons

  • Complex installation process due to native dependencies
  • Steep learning curve, especially for developers unfamiliar with Git internals
  • Large package size compared to pure JavaScript Git implementations
  • Potential compatibility issues across different operating systems and Node.js versions

Code Examples

  1. Cloning a repository:
const Git = require("nodegit");

Git.Clone("https://github.com/nodegit/nodegit", "nodegit")
  .then((repository) => {
    console.log("Repository cloned successfully");
  })
  .catch((error) => {
    console.error("Failed to clone repository:", error);
  });
  1. Creating a new commit:
const Git = require("nodegit");

Git.Repository.open("./my-repo")
  .then((repo) => {
    let index, oid;

    return repo.refreshIndex()
      .then((idx) => {
        index = idx;
        return index.addByPath("newfile.txt");
      })
      .then(() => index.write())
      .then(() => index.writeTree())
      .then((oidResult) => {
        oid = oidResult;
        return Git.Reference.nameToId(repo, "HEAD");
      })
      .then((head) => repo.getCommit(head))
      .then((parent) => {
        const author = Git.Signature.now("John Doe", "johndoe@example.com");
        return repo.createCommit("HEAD", author, author, "Commit message", oid, [parent]);
      });
  })
  .then((commitId) => {
    console.log("New commit created with ID:", commitId);
  })
  .catch((error) => {
    console.error("Failed to create commit:", error);
  });
  1. Listing branches:
const Git = require("nodegit");

Git.Repository.open("./my-repo")
  .then((repo) => repo.getReferenceNames(Git.Reference.TYPE.LISTALL))
  .then((branchNames) => {
    console.log("Branches:");
    branchNames.forEach((branchName) => {
      console.log(`- ${branchName}`);
    });
  })
  .catch((error) => {
    console.error("Failed to list branches:", error);
  });

Getting Started

  1. Install NodeGit:

    npm install nodegit
    
  2. Create a new JavaScript file (e.g., git-example.js) and add the following code:

    const Git = require("nodegit");
    
    Git.Repository.open(".")
      .then((repo) => repo.getStatus())
      .then((statuses) => {
        statuses.forEach((file) => {
          console.log(`File ${file.path()} has status: ${file.status()}`);
        });
      })
      .catch((error) => {
        console.error("An error occurred:", error);
      });
    
  3. Run the script:

    node git-example.js
    

This example opens the Git repository in the current directory and lists the status of all files.

Competitor Comparisons

9,602

A cross-platform, linkable library implementation of Git that you can use in your application.

Pros of libgit2

  • Written in C, offering better performance and lower-level access to Git operations
  • More versatile, can be used with multiple programming languages through bindings
  • Larger community and more frequent updates

Cons of libgit2

  • Requires more setup and configuration for use in Node.js projects
  • Steeper learning curve for developers primarily working with JavaScript
  • May require additional compilation steps depending on the target environment

Code Comparison

libgit2 (C):

git_repository *repo = NULL;
git_repository_open(&repo, "path/to/repository");
git_commit *commit;
git_commit_lookup(&commit, repo, &oid);
const char *message = git_commit_message(commit);

nodegit (JavaScript):

const Git = require('nodegit');
Git.Repository.open("path/to/repository")
  .then(repo => repo.getCommit("oid"))
  .then(commit => commit.message())
  .then(message => console.log(message));

Summary

libgit2 is a powerful, low-level Git library with broad language support, while nodegit provides a more JavaScript-friendly interface specifically for Node.js projects. The choice between them depends on the project requirements, developer expertise, and desired level of Git integration.

A pure JavaScript implementation of git for node and browsers!

Pros of isomorphic-git

  • Pure JavaScript implementation, allowing for easier integration in web browsers and Node.js environments
  • Supports a wider range of platforms, including browsers and Electron apps
  • Lighter weight and easier to install, with fewer dependencies

Cons of isomorphic-git

  • May have slower performance for certain operations compared to native implementations
  • Limited support for some advanced Git features and operations
  • Potentially less mature and battle-tested in production environments

Code Comparison

isomorphic-git:

import { clone } from 'isomorphic-git'
import http from 'isomorphic-git/http/web'

await clone({
  fs,
  http,
  dir: '/path/to/repo',
  url: 'https://github.com/user/repo'
})

nodegit:

const Git = require('nodegit')

Git.Clone('https://github.com/user/repo', '/path/to/repo')
  .then((repository) => {
    // Clone completed
  })

Both libraries provide Git functionality for JavaScript environments, but isomorphic-git focuses on cross-platform compatibility and ease of use, while nodegit offers native performance and more comprehensive Git feature support. The choice between them depends on specific project requirements and target platforms.

4,907

Project has been moved to: https://github.com/go-git/go-git

Pros of go-git

  • Written in Go, offering better performance and easier deployment
  • Pure Go implementation, no external dependencies required
  • Supports both client and server-side Git operations

Cons of go-git

  • Less mature and feature-complete compared to nodegit
  • Smaller community and ecosystem around the project
  • May lack some advanced Git features available in nodegit

Code Comparison

nodegit example:

const Git = require("nodegit");

Git.Clone("https://github.com/example/repo.git", "path/to/repo")
  .then((repository) => {
    console.log("Repository cloned successfully");
  })
  .catch((error) => console.error(error));

go-git example:

import "github.com/go-git/go-git/v5"

r, err := git.PlainClone("path/to/repo", false, &git.CloneOptions{
    URL: "https://github.com/example/repo.git",
})
if err != nil {
    panic(err)
}

Both libraries provide similar functionality for basic Git operations, but their syntax and usage differ due to the languages they're written in. nodegit leverages JavaScript's asynchronous nature with Promises, while go-git uses Go's error handling approach. go-git's implementation is more lightweight and portable, while nodegit offers a wider range of features and closer integration with the official Git implementation.

2,040

Pure-Python Git implementation

Pros of Dulwich

  • Pure Python implementation, making it easier to install and use across different platforms
  • Lightweight and has fewer dependencies compared to NodeGit
  • Provides both high-level and low-level APIs for Git operations

Cons of Dulwich

  • May have slower performance for certain operations compared to NodeGit's C bindings
  • Less extensive documentation and smaller community compared to NodeGit
  • Limited to Python ecosystem, while NodeGit can be used in Node.js applications

Code Comparison

Dulwich (Python):

from dulwich import porcelain
repo = porcelain.init("my_repo")
porcelain.add(repo.path, "file.txt")
porcelain.commit(repo.path, message="Initial commit")

NodeGit (JavaScript):

const Git = require("nodegit");
Git.Repository.init("my_repo")
  .then((repo) => repo.index())
  .then((index) => {
    index.addByPath("file.txt");
    return index.write();
  })
  .then(() => repo.createCommit("HEAD", author, committer, "Initial commit", oid, [parent]));

Both libraries provide similar functionality for basic Git operations, but with syntax and patterns specific to their respective languages. Dulwich offers a more straightforward API for simple operations, while NodeGit provides a more detailed, promise-based approach.

1,606

Python bindings for libgit2

Pros of pygit2

  • Written in Python, offering better integration with Python projects
  • Provides a more Pythonic API, making it easier for Python developers to work with
  • Generally faster performance due to its C implementation

Cons of pygit2

  • Limited to Python environments, reducing cross-language compatibility
  • May have a steeper learning curve for developers not familiar with Python

Code Comparison

pygit2:

from pygit2 import Repository
repo = Repository('path/to/repo')
commit = repo.revparse_single('HEAD')
print(commit.message)

nodegit:

const Git = require('nodegit');
Git.Repository.open('path/to/repo')
  .then(repo => repo.getHeadCommit())
  .then(commit => console.log(commit.message()));

Both libraries provide similar functionality for working with Git repositories, but their syntax and approach differ based on their respective languages. pygit2 offers a more straightforward, synchronous API typical of Python, while nodegit uses JavaScript's asynchronous patterns with Promises.

The choice between these libraries often depends on the primary language of the project and the developer's familiarity with Python or JavaScript. pygit2 is generally preferred for Python-centric projects, while nodegit is better suited for Node.js environments.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

NodeGit

Node bindings to the libgit2 project.

Actions Status

Stable (libgit2@v0.28.3): 0.28.3

Have a problem? Come chat with us!

Visit slack.libgit2.org to sign up, then join us in #nodegit.

Maintained by

Tyler Ang-Wanek @twwanek with help from tons of awesome contributors!

Alumni Maintainers

Tim Branyen @tbranyen, John Haley @johnhaley81, Max Korp @maxkorp, Steve Smith @orderedlist, Michael Robinson @codeofinterest, and Nick Kallen @nk

API Documentation.

http://www.nodegit.org/

Getting started.

NodeGit will work on most systems out-of-the-box without any native dependencies.

npm install nodegit

If you receive errors about libstdc++, which are commonly experienced when building on Travis-CI, you can fix this by upgrading to the latest libstdc++-4.9.

In Ubuntu:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install libstdc++-4.9-dev

In Travis:

addons:
  apt:
    sources:
      - ubuntu-toolchain-r-test
    packages:
      - libstdc++-4.9-dev

In CircleCI:

  dependencies:
    pre:
      - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
      - sudo apt-get update
      - sudo apt-get install -y libstdc++-4.9-dev

If you receive errors about lifecycleScripts preinstall/install you probably miss libssl-dev In Ubuntu:

sudo apt-get install libssl-dev

You will need the following libraries installed on your linux machine:

  • libpcre
  • libpcreposix
  • libkrb5
  • libk5crypto
  • libcom_err

When building locally, you will also need development packages for kerberos and pcre, so both of these utilities must be present on your machine:

  • pcre-config
  • krb5-config

If you are still encountering problems while installing, you should try the Building from source instructions.

API examples.

Cloning a repository and reading a file:

var Git = require("nodegit");

// Clone a given repository into the `./tmp` folder.
Git.Clone("https://github.com/nodegit/nodegit", "./tmp")
  // Look up this known commit.
  .then(function(repo) {
    // Use a known commit sha from this repository.
    return repo.getCommit("59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5");
  })
  // Look up a specific file within that commit.
  .then(function(commit) {
    return commit.getEntry("README.md");
  })
  // Get the blob contents from the file.
  .then(function(entry) {
    // Patch the blob to contain a reference to the entry.
    return entry.getBlob().then(function(blob) {
      blob.entry = entry;
      return blob;
    });
  })
  // Display information about the blob.
  .then(function(blob) {
    // Show the path, sha, and filesize in bytes.
    console.log(blob.entry.path() + blob.entry.sha() + blob.rawsize() + "b");

    // Show a spacer.
    console.log(Array(72).join("=") + "\n\n");

    // Show the entire file.
    console.log(String(blob));
  })
  .catch(function(err) { console.log(err); });

Emulating git log:

var Git = require("nodegit");

// Open the repository directory.
Git.Repository.open("tmp")
  // Open the master branch.
  .then(function(repo) {
    return repo.getMasterCommit();
  })
  // Display information about commits on master.
  .then(function(firstCommitOnMaster) {
    // Create a new history event emitter.
    var history = firstCommitOnMaster.history();

    // Create a counter to only show up to 9 entries.
    var count = 0;

    // Listen for commit events from the history.
    history.on("commit", function(commit) {
      // Disregard commits past 9.
      if (++count >= 9) {
        return;
      }

      // Show the commit sha.
      console.log("commit " + commit.sha());

      // Store the author object.
      var author = commit.author();

      // Display author information.
      console.log("Author:\t" + author.name() + " <" + author.email() + ">");

      // Show the commit date.
      console.log("Date:\t" + commit.date());

      // Give some space and show the message.
      console.log("\n    " + commit.message());
    });

    // Start emitting events.
    history.start();
  });

For more examples, check the examples/ folder.

Unit tests.

You will need to build locally before running the tests. See above.

npm test

NPM DownloadsLast 30 Days