0

how to use libgit2 to get a SHA value when i want to commit a new file to git The command in shell is:

git add
git commit
git pull
git push
alexqinbj
  • 1,091
  • 3
  • 13
  • 27

1 Answers1

1

The following libgit2 tests should get you started:

  • commitstagedfile.c: how to git add a file to the staging area, then perform a git commit and retrieve the sha of the created commit.

  • fetch.c: how to git fetch and update the content of the local repository with upstream changes

  • push.c: how to submit the local changes to the upstream repository, similarly to git push

Note: git pull is a combination of git fetch and git merge. The merging capabilities are not available yet in libgit2.

nulltoken
  • 64,429
  • 20
  • 138
  • 130
  • In commitstagedfile.c, it get oid from str.(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d")). I have already have a git dir, but I dont know how to get (const git_tree *tree) when I use git_commit_create instead of test hex value. Can you explain the process of this, thx – alexqinbj Jan 11 '13 at 12:43
  • 1
    The tree is being **[created in the object database](https://github.com/libgit2/libgit2/blob/development/tests-clar/object/commit/commitstagedfile.c#L101-104)** from the index. In order to pass it to the git_commit_creat_v() method, it's first **[retrieved from the object database](https://github.com/libgit2/libgit2/blob/development/tests-clar/object/commit/commitstagedfile.c#L112)**. – nulltoken Jan 11 '13 at 13:21
  • Thx :) I got it. I am a beginner using libgit2. Here is my thought at beginning, can you tell me what I have thought wrong? First, build a blob using git_blob_create_fromworkdir, secondly, git_treebuilder_create and git_tree_entry to create a tree builder, then use git_treebuilder_write to write the content, and use git_tree_lookup to got the git_tree? – alexqinbj Jan 12 '13 at 06:08