Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git add and commit #17

Open
SaidTayebi opened this issue Sep 9, 2014 · 1 comment
Open

git add and commit #17

SaidTayebi opened this issue Sep 9, 2014 · 1 comment

Comments

@SaidTayebi
Copy link

Hi,

how do you perform an add and commit ?

thanks

@creationix
Copy link
Owner

There is no git add since there is no staging area. But you can save new objects to the database using repo.saveAs. So to add a new file you'll need to repo.saveAs("blob", contentAsStringOrBuffer, callback). Then then will give you the hash. To store it in a tree somewhere you update or create the tree entry where it goes and repo.saveAs("tree", treeData, callback) to get the new hash for the tree, then if it's a subfolder update that tree's entry in it's parent recursivly up to the root tree. Then once you have a new root tree you create a new callback that points to the tree and points to the old commit as it's parent. Then if you want to preserve this new commit, update the branch ref to point to it.

I highly recommend reading http://git-scm.com/book/en/Git-Internals to learn how js-git works. I will mention that js-git has a higher-level helper in https://github.com/creationix/js-git/blob/master/mixins/create-tree.js that lets you skip a few of the steps.

var entries = [
  {path: "foo/bar.js", mode: modes.blob, content: "console.log('Hello world');\n"},
  // Other updates can go in this array as well.
];
entries.hash = oldRootTreeHash; // This is the hash of the tree you wish to base on, inherit from.
repo.createTree(entries, function (err, hash) {
  if (err) return handleError(err);
  // You now have the new root tree hash, store it somewhere, create a new commit or whatever else you wanted to do.
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants