isomorphic-git
A pure JavaScript implementation of git for node and browsers!
isomorphic-git
is a pure JavaScript implementation of git that works in node and browser environments (including WebWorkers and ServiceWorkers). This means it can be used to read and write to to git repositories, as well as fetch from and push to git remotes like Github.
Isomorphic-git aims for 100% interoperability with the canonical git implementation. This means it does all its operations by modifying files in a ".git" directory just like the git you are used to. The included isogit
CLI can operate on git repositories on your desktop or server.
Unlike earlier git-in-js solutions that were hypermodular, isomorphic-git
aims to be a complete solution with no assembly required.
The high-level API is a fluent interface modeled after the git CLI and should feel natural to read and write.
However, one size does not always fit. That's why isomorphic-git
also has a layered API that frees you to build a solution using only the exact features you need.
Table of Contents generated with DocToc
Getting Started
Set up your filesystem
If you're only using isomorphic-git
in Node, you already have a fs
module, so you can skip this step. If you're writing code for the browser though, you'll need something that emulates the fs
API. isomorphic-git
will look for a global "fs" variable. At the time of writing, the most complete option is BrowserFS.
Here's a quick config that works well in most cases:
<script src="https://unpkg.com/browserfs"></script>
<script>
BrowserFS.configure({ fs: "IndexedDB", options: {} }, err => {
if (err) {
alert(err);
} else {
window.fs = BrowserFS.BFSRequire("fs");
}
});
</script>
Besides IndexedDB, BrowserFS supports many different backends with different performance characteristics, as well as advanced configurations such as: multiple mounting points, and overlaying a writeable filesystems on top of a read-only filesystem. You don't need to know about all these features, but familiarizing yourself with the different options may be necessary if you hit a storage limit or performance bottleneck in the IndexedDB backend I suggested above.
Using a CDN script tag
If you want, you can just throw in a script tag with the UMD build directly from unpkg
. This will result in three global variables: BrowserFS
, fs
, and git
.
<script src="https://unpkg.com/browserfs"></script>
<script>
BrowserFS.configure({ fs: "IndexedDB", options: {} }, function (err) {
if (err) return console.log(err);
window.fs = BrowserFS.BFSRequire("fs");
});
</script>
<script src="https://unpkg.com/isomorphic-git"></script>
Using as an npm module
You can install it from npm.
npm install --save isomorphic-git
In the package.json you'll see there are actually 4 different versions:
"main": "dist/for-node/",
"browser": "dist/for-browserify/",
"module": "dist/for-future/",
"unpkg": "dist/bundle.umd.min.js",
This probably deserves a brief explanation.
- the "main" version is for node.
- the "browser" version is for browserify.
- the "module" version is for native ES6 module loaders when they arrive.
- the "unpkg" version is the UMD build.
For more details about each build see ./dist/README.md
isogit
CLI
Isomorphic-git comes with a simple CLI tool, named isogit
because isomorphic-git
is a lot to type. It is really just a thin shell that translates command line arguments into the equivalent JS API commands. So you should be able to run any current or future isomorphic-git commands using the CLI.
It always starts with an implicit git('.')
so it defaults to working in the
current working directory. (Note I may change that soon, now that I have a findRoot
function. I may change the default to git(git().findRoot(process.cwd()))
.)
High-level git()
API
I may continue to make small changes to the API until the 1.0 release, after which I promise not to make any breaking changes.
git(dir) vs .gitdir(dir) and .workdir(dir)
Setting the working directory and git directory
For regular repositories (with a .git
directory inside them) you simply pass the directory as the initial argument to git()
.
In this case, the git directory is set implicitly to path.join(workdir, '.git')
.
However, if you are working with bare repositories, that assumption is wrong. In this case, you can use the second version to specify the directories explicitly.
import git from 'isomorphic-git'
git('./path/to/repo')
git()
.gitdir('my-bare-repo')
.workdir('/var/www/website')
cd ./path/to/repo
isogit
isogit --gitdir=my-bare-repo --workdir=/var/www/website
git(workdir)
git()
.gitdir(gitdir)
.workdir(workdir)
- @param {string}
workdir
- The path to the working directory.
The working directory is where your files are checked out.
Usually this is the parent directory of ".git" but it doesn't have to be.
- @param {string}
gitdir
- The path to the git directory.
The git directory is where your git repository history is stored.
Usually this is a directory called ".git" inside your working directory.
.init()
Initialize a new repository
import git from 'isomorphic-git'
git('.').init()
isogit init
git()
.gitdir(gitdir)
.init()
- @param {string}
gitdir
- The path to the git directory. - @returns
Promise<void>
.clone(url)
Clone a repository
import git from 'isomorphic-git'
git('.')
.depth(1)
.clone('https://cors-buster-jfpactjnem.now.sh/github.com/wmhilton/isomorphic-git')
isogit --depth=1 clone https://github.com/wmhilton/isomorphic-git
git()
.workdir(workdir)
.gitdir(gitdir)
.branch(ref)
.auth(authUsername, authPassword)
.remote(remote)
.depth(depth)
.since(since)
.exclude(exclude)
.relative(relative)
.onprogress(progressHandler)
.clone(url)
- @param {string}
workdir
- The path to the working directory. - @param {string}
gitdir
- The path to the git directory. - @param {string} [
ref=undefined
] - Which branch to clone. By default this is the designated "main branch" of the repository. - @param {string} [
authUsername=undefined
] - The username to use with Basic Auth - @param {string} [
authPassword=undefined
] - The password to use with Basic Auth - @param {string} [
remote='origin'
] - What to name the remote that is created. The default is 'origin'. - @param {string}
url
- The URL of the remote repository. - @param {integer} [
depth=undefined
] - Determines how much of the git repository's history to retrieve. - @param {Date} [
since=undefined
] - Only fetch commits created after the given date. Mutually exclusive with depth
. - @param {string[]} [
exclude=[]
] - A list of branches or tags. Instructs the remote server not to send us any commits reachable from these refs. - @param {boolean} [
relative=false
] - Changes the meaning of depth
to be measured from the current shallow depth rather than from the branch tip. - @param {Function} [
progressHandler=undefined
] - Callback to receive ProgressEvents for the operation. - @returns
Promise<void>
.fetch(branch)
Fetch commits
import git from 'isomorphic-git'
git('.')
.remote('origin')
.depth(1)
.fetch('master')
isogit --remote=origin --depth=1 fetch master
git()
.gitdir(gitdir)
.auth(authUsername, authPassword)
.url(url)
.remote(remote)
.depth(depth)
.since(since)
.exclude(exclude)
.relative(relative)
.onprogress(progressHandler)
.fetch(ref)
- @param {string}
gitdir
- The path to the git directory. - @param {string} [
ref=undefined
] - Which branch to fetch from. By default this is the currently checked out branch. - @param {string} [
authUsername=undefined
] - The username to use with Basic Auth - @param {string} [
authPassword=undefined
] - The password to use with Basic Auth - @param {string} [
url=undefined
] - The URL of the remote git server. The default is the value set in the git config for that remote. - @param {string} [
remote='origin'
] - If URL is not specified, determines which remote to use. - @param {integer} [
depth=undefined
] - Determines how much of the git repository's history to retrieve. - @param {Date} [
since=undefined
] - Only fetch commits created after the given date. Mutually exclusive with depth
. - @param {string[]} [
exclude=[]
] - A list of branches or tags. Instructs the remote server not to send us any commits reachable from these refs. - @param {boolean} [
relative=false
] - Changes the meaning of depth
to be measured from the current shallow depth rather than from the branch tip. - @param {Function} [
progressHandler=undefined
] - Callback to receive ProgressEvents for the operation. - @returns
Promise<void>
.checkout(branch)
Checkout a branch
import git from 'isomorphic-git'
git('.')
.checkout('master')
isogit checkout master
git()
.workdir(workdir)
.gitdir(gitdir)
.remote(remote)
.checkout(ref)
- @param {string}
workdir
- The path to the working directory. - @param {string}
gitdir
- The path to the git directory. - @param {string} [
ref=undefined
] - Which branch to clone. By default this is the designated "main branch" of the repository. - @param {string} [
remote='origin'
] - What to name the remote that is created. The default is 'origin'. - @returns
Promise<void>
.list()
List all the tracked files in a repo
import git from 'isomorphic-git'
git('.')
.list()
isogit list
git()
.gitdir(gitdir)
.list()
- @param {string}
gitdir
- The path to the git directory. - @returns
Promise<string[]>
- A list of file paths.
.log(ref)
Get commit descriptions from the git history
import git from 'isomorphic-git'
let commits = await git('.')
.depth(5)
.log('master')
commits.map(c => console.log(JSON.stringify(c))
isogit --depth=5 log master
git()
.gitdir(gitdir)
.depth(depth)
.since(since)
.log(ref)
- @param {string}
gitdir
- The path to the git directory. - @param {integer} [
depth=undefined
] - Return at most this many commits. - @param {Date} [
since=undefined
] - Return history newer than the given date. Can be combined with depth
to get whichever is shorter. - @param {string} [
ref=HEAD
] - The commit to begin walking backwards through the history from. - @returns
Promise<CommitDescription[]>
type CommitDescription = {
oid: string,
message: string,
tree: string,
parent: string[],
author: {
name: string,
email: string,
timestamp: number,
timezoneOffset: number
},
committer: {
name: string,
email: string,
timestamp: number,
timezoneOffset: number
},
gpgsig: ?string
}
.add(file)
Add files to the git index (aka staging area)
import git from 'isomorphic-git'
git('.')
.add('README.md')
isogit add README.md
git()
.workdir(workdir)
.gitdir(gitdir)
.add(filepath)
- @param {string}
workdir
- The path to the working directory. - @param {string}
gitdir
- The path to the git directory. - @param {string}
filepath
- The path to the file to add to the index. - @returns
Promise<void>
.remove(file)
Remove files from the git index (aka staging area)
import git from 'isomorphic-git'
git('.')
.remove('README.md')
isogit remove README.md
git()
.gitdir(gitdir)
.remove(filepath)
- @param {string}
gitdir
- The path to the git directory. - @param {string}
filepath
- The path to the file to add to the index. - @returns
Promise<void>
.status(file)
Tell whether a file has been changed
import git from 'isomorphic-git'
git('.')
.status('README.md')
isogit status README.md
git()
.workdir(workdir)
.gitdir(gitdir)
.status(filepath)
- @param {string}
workdir
- The path to the working directory. - @param {string}
gitdir
- The path to the git directory. - @param {string}
filepath
- The path to the file to query. - @returns
Promise<String>
The possible return values are:
"ignored"
file ignored by a .gitignore rule"unmodified"
file unchanged from HEAD commit"*modified"
file has modifications, not yet staged"*deleted"
file has been removed, but the removal is not yet staged"*added"
file is untracked, not yet staged"absent"
file not present in HEAD commit, staging area, or working dir"modified"
file has modifications, staged"deleted"
file has been removed, staged"added"
previously untracked file, staged"*unmodified"
working dir and HEAD commit match, but index differs"*absent"
file not present in working dir or HEAD commit, but present in the index
.commit(msg)
Create a new commit
import git from 'isomorphic-git'
git('.')
.author('Mr. Test')
.email('mrtest@example.com')
.signingKey('-----BEGIN PGP PRIVATE KEY BLOCK-----...')
.commit('Added the a.txt file')
isogit --author='Mr. Test' \
--email=mrtest@example.com \
--signingKey="$(cat private.key)" \
commit 'Added the a.txt file'
git()
.gitdir(gitdir)
.author(author.name)
.email(author.email)
.timestamp(author.timestamp)
.datetime(author.date)
.signingKey(privateKeys)
.commit(message)
- @param {string}
gitdir
- The path to the git directory. - @param {Object}
author
- The details about the commit author. - @param {string} [
author.name=undefined
] - Default is user.name
config. - @param {string} [
author.email=undefined
] - Default is user.email
config. - @param {Date} [
author.date=new Date()
] - Set the author timestamp field. Default is the current date. - @param {integer} [
author.timestamp=undefined
] - Set the author timestamp field. This is an alternative to using date
using an integer number of seconds since the Unix epoch instead of a JavaScript date object. - @param {Object} [
committer=author
] - The details about the commit author. If not specified, the author details are used. - @param {string}
message
- The commit message to use. - @param {string} [
privateKeys=undefined
] - A PGP private key in ASCII armor format. - @returns
Promise<void>
.push(branch)
Push a branch
import git from 'isomorphic-git'
git('.')
.auth(process.env.GITHUB_TOKEN)
.remote('origin')
.push('master')
isogit --auth="$GITHUB_TOKEN" --remote=origin push master
git()
.gitdir(gitdir)
.depth(depth)
.remote(remote)
.url(url)
.auth(authUsername, authPassword)
.push(ref)
- @param {string}
gitdir
- The path to the git directory. - @param {integer} [
depth=0
] - Determines how much of the git repository's history to retrieve. If not specified it defaults to 0 which means the entire repo history. - @param {string} [
ref=undefined
] - Which branch to push. By default this is the currently checked out branch of the repository. - @param {string} [
authUsername=undefined
] - The username to use with Basic Auth - @param {string} [
authPassword=undefined
] - The password to use with Basic Auth - @param {string} [
url=undefined
] - The URL of the remote git server. The default is the value set in the git config for that remote. - @param {string} [
remote='origin'
] - If URL is not specified, determines which remote to use. - @returns
Promise<void>
.findRoot(dir)
Find the root git directory
import git from 'isomorphic-git'
git()
.findRoot('/path/to/some/gitrepo/path/to/some/file.txt')
isogit findRoot /path/to/some/gitrepo/path/to/some/file.txt
git()
.findRoot(dir)
- @param {string}
dir
- Starting at directory {dir}, walk upwards until you find a directory that contains a '.git' directory. - @returns
Promise<rootdir>
that directory, which is presumably the root directory of the git repository containing {dir}.
.listBranches()
List all local branches
import git from 'isomorphic-git'
git('.').listBranches()
isogit listBranches
git()
.gitdir(gitdir)
.listBranches()
- @param {string}
gitdir
- The path to the git directory. - @returns
Promise<branches[]>
an array of branch names.
.config(path)
Reading from git config
import git from 'isomorphic-git'
git('.').config('user.name')
isogit config user.name
git()
.gitdir(gitdir)
.config(path)
- @param {string}
gitdir
- The path to the git directory. - @param {string}
path
- The key of the git config entry. - @returns
Promise<value>
- the config value
.config(path, value)
Writing to git config
import git from 'isomorphic-git'
git('.').config('user.name', 'Mr. Test')
isogit config user.name 'Mr. Test'
git()
.gitdir(gitdir)
.config(path, value)
- @param {string}
gitdir
- The path to the git directory. - @param {string}
path
- The key of the git config entry. - @param {string}
value
- A value to store at that path. - @returns
Promise<void>
.auth(username, password_or_token)
Authentication is normally required for pushing to a git repository.
It may also be required to clone or fetch from a private repository.
Git does all its authentication using HTTPS Basic Authentication.
Usually this is straightforward, but there are some things to watch out for.
If you have two-factor authentication (2FA) enabled on your account, you
probably cannot push or pull using your regular username and password.
Instead, you may have to create a Personal Access Token (or an App
Password in Bitbucket lingo) and use that to authenticate.
git('.').auth('username', 'password')
git('.').auth('username:password')
git('.').auth('username', 'personal access token')
git('.').auth('username', 'app password')
git('.').auth('personal access token')
.oauth2(company, token)
If you are using OAuth2 for token-based authentication, then the form
that the Basic Auth headers take is slightly different. To help with
those cases, there is an oauth2()
method that is available as an
alternative to the auth()
method.
git('.').oauth2('github', 'token')
git('.').oauth2('gitlab', 'token')
git('.').oauth2('bitbucket', 'token')
.version()
import git from 'isomorphic-git'
console.log(git().version())
- @returns {string}
version
- the version string from package.json
Lower-level API
The high-level makes some assumptions (like you have a file-system and network access) that might not be well suited
to your embedded git-based concept thingy. Fear not! I have written this library
as a series of layers that should tree-shake very well:
- index.js (~5kb uncompressed)
- commands.js (~19kb uncompressed)
- managers.js (~11kb uncompressed)
- models.js (~19kb uncompressed)
- utils.js (~11kb uncompressed)
Commands
import {
add,
clone,
checkout,
commit,
fetch,
init,
list,
listCommits,
listObjects,
log,
pack,
push,
remove,
resolveRef,
config,
unpack,
verify,
status,
findRoot,
listBranches,
version
} from 'isomorphic-git/dist/for-node/commands'
Each command is available as its own file, so you are able to import individual commands
if you only need a few and are willing to sacrifice the fluent API
in order to optimize your bundle size.
Managers
import {
GitConfigManager,
GitShallowManager,
GitIndexManager,
GitObjectManager,
GitRefsManager,
GitRemoteHTTP
} from 'isomorphic-git/dist/for-node/managers'
Managers are a level above models. They take care of implementation performance details like
- batching reads to and from the file system
- in-process concurrency locks
- lockfiles
- caching files and invalidating cached results
- reusing objects
- object memory pools
Models and Utils
import {
GitCommit,
GitConfig,
GitPktLine,
GitIndex,
GitTree
} from 'isomorphic-git/dist/for-node/models'
Models and utils are the lowest level building blocks.
Models generally have very few or no dependencies except for 'buffer'
.
This makes them portable to many different environments so they can be a useful lowest common denominator.
They do not rely on Utils.
import {
rm,
flatFileListToDirectoryStructure,
default,
lock,
mkdirs,
read,
sleep,
write,
pkg
} from 'isomorphic-git/dist/for-node/utils'
Utils are basically miscellaneous functions.
Some are convenience wrappers for common filesystem operations.
Who is using isomorphic-git
?
- nde - a futuristic next-generation web IDE
- git-app-manager - install "unhosted" websites locally by git cloning them
Similar projects
Acknowledgments
Isomorphic-git would not have been possible without the pioneering work by
@creationix and @chrisdickinson. Git is a tricky binary mess, and without
their examples (and their modules!) I would not have been able to come even
close to finishing this. They are geniuses ahead of their time.
License
This work is released under The Unlicense