添加并提交一个新文件使用git2go。

huangapple go评论120阅读模式
英文:

Adding and committing a new file with git2go

问题

我目前正在努力将文件暂存和提交到内存中的git仓库(即不将文件写入磁盘,然后添加和提交它)。

这是我代码的简化版本;它几乎可以工作

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/libgit2/git2go.v25"
  5. "time"
  6. )
  7. func main() {
  8. sig := &git.Signature{
  9. Name: "Some Person",
  10. Email: "somebody@something.org",
  11. When: time.Now(),
  12. }
  13. repo, err := git.OpenRepository("./repo")
  14. check(err)
  15. defer repo.Free()
  16. content := []byte("hello world")
  17. index, err := repo.Index()
  18. check(err)
  19. oid, err := repo.CreateBlobFromBuffer(content)
  20. check(err)
  21. ie := git.IndexEntry{
  22. Mode: git.FilemodeBlob,
  23. Id: oid,
  24. Path: "documents/document_9.md",
  25. }
  26. err = index.Add(&ie)
  27. check(err)
  28. treeId, err := index.WriteTree()
  29. check(err)
  30. tree, err := repo.LookupTree(treeId)
  31. check(err)
  32. index.Write()
  33. currentBranch, err := repo.Head()
  34. check(err)
  35. currentTip, err := repo.LookupCommit(currentBranch.Target())
  36. check(err)
  37. commitId, err := repo.CreateCommit("HEAD", sig, sig, "A new commit", tree, currentTip)
  38. check(err)
  39. fmt.Println(commitId)
  40. }
  41. func check(err error) {
  42. if err != nil {
  43. panic(err)
  44. }
  45. }

然而,当我运行它并查看./repo时,有些地方不太对劲。正如你从git status中可以看到的那样,新的document_9.md文件被标记为已删除(即git知道它存在,但实际上不存在)。

  1. git status
  2. On branch master
  3. Changes not staged for commit:
  4. (use "git add/rm <file>..." to update what will be committed)
  5. (use "git checkout -- <file>..." to discard changes in working directory)
  6. deleted: documents/document_9.md

查看日志后,可以确认提交实际上是成功的:

  1. git log --patch
  2. commit a609b43e6a3f7da7d0589971ab0f64f3f432e76c
  3. Author: Some Person <somebody@something.org>
  4. Date: Thu Mar 23 19:43:56 2017 +0000
  5. a new commit
  6. diff --git a/documents/document_9.md b/documents/document_9.md
  7. new file mode 100644
  8. index 0000000..95d09f2
  9. --- /dev/null
  10. +++ b/documents/document_9.md
  11. @@ -0,0 +1 @@
  12. +hello world
  13. \ No newline at end of file
  14. commit 668762279b51483c05dbdedcf3c599a1b41c203b
  15. Author: Original Committer <someguy@something.com>
  16. Date: Thu Mar 23 19:42:52 2017 +0000
  17. First commit
  18. diff --git a/documents/docment_1.md b/documents/docment_1.md
  19. new file mode 100644
  20. index 0000000..2965834
  21. --- /dev/null
  22. +++ b/documents/docment_1.md
  23. @@ -0,0 +1 @@
  24. +# Hello World

我相信我漏掉了一步。不知何故,我需要确保我的提交与文件系统“同步”。我认为(根据文档),这可以通过index.Write()index.WriteTree()来实现,但我无法让它工作。

任何对正确方向的指引将不胜感激。

英文:

I am currently struggling to stage and commit a file to a git repository in memory (i.e. without writing a file to disk, adding and committing it)

Here's a shortened version of my code; it almost works

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;gopkg.in/libgit2/git2go.v25&quot;
  5. &quot;time&quot;
  6. )
  7. func main() {
  8. sig := &amp;git.Signature{
  9. Name: &quot;Some Person&quot;,
  10. Email: &quot;somebody@something.org&quot;,
  11. When: time.Now(),
  12. }
  13. repo, err := git.OpenRepository(&quot;./repo&quot;)
  14. check(err)
  15. defer repo.Free()
  16. content := []byte(&quot;hello world&quot;)
  17. index, err := repo.Index()
  18. check(err)
  19. oid, err := repo.CreateBlobFromBuffer(content)
  20. check(err)
  21. ie := git.IndexEntry{
  22. Mode: git.FilemodeBlob,
  23. Id: oid,
  24. Path: &quot;documents/document_9.md&quot;,
  25. }
  26. err = index.Add(&amp;ie)
  27. check(err)
  28. treeId, err := index.WriteTree()
  29. check(err)
  30. tree, err := repo.LookupTree(treeId)
  31. check(err)
  32. index.Write()
  33. currentBranch, err := repo.Head()
  34. check(err)
  35. currentTip, err := repo.LookupCommit(currentBranch.Target())
  36. check(err)
  37. commitId, err := repo.CreateCommit(&quot;HEAD&quot;, sig, sig, &quot;A new commit&quot;, tree, currentTip)
  38. check(err)
  39. fmt.Println(commitId)
  40. }
  41. func check(err error) {
  42. if err != nil {
  43. panic(err)
  44. }
  45. }

However, when I run it and look in ./repo, something's not quite right. As you can see from git status, the new document_9.md file is marked as being deleted (i.e. git knows about it but it's not present)

  1. git status
  2. On branch master
  3. Changes not staged for commit:
  4. (use &quot;git add/rm &lt;file&gt;...&quot; to update what will be committed)
  5. (use &quot;git checkout -- &lt;file&gt;...&quot; to discard changes in working directory)
  6. deleted: documents/document_9.md

Looking at the log, it's confirmed that the commit actually worked:

  1. git log --patch
  2. commit a609b43e6a3f7da7d0589971ab0f64f3f432e76c
  3. Author: Some Person &lt;somebody@something.org&gt;
  4. Date: Thu Mar 23 19:43:56 2017 +0000
  5. a new commit
  6. diff --git a/documents/document_9.md b/documents/document_9.md
  7. new file mode 100644
  8. index 0000000..95d09f2
  9. --- /dev/null
  10. +++ b/documents/document_9.md
  11. @@ -0,0 +1 @@
  12. +hello world
  13. \ No newline at end of file
  14. commit 668762279b51483c05dbdedcf3c599a1b41c203b
  15. Author: Original Committer &lt;someguy@something.com&gt;
  16. Date: Thu Mar 23 19:42:52 2017 +0000
  17. First commit
  18. diff --git a/documents/docment_1.md b/documents/docment_1.md
  19. new file mode 100644
  20. index 0000000..2965834
  21. --- /dev/null
  22. +++ b/documents/docment_1.md
  23. @@ -0,0 +1 @@
  24. +# Hello World

I believe that I'm missing a step. Somehow I need to ensure that my commit is 'synced' with the filesystem. I thought that (based on the documentation), this was achieved by index.Write() and index.WriteTree(), but I can't get it to work.

Any nudge in the right direction would be much appreciated.

答案1

得分: 1

如果你想让工作目录与新的 HEAD 提交的状态匹配,那么你需要检出它(例如,使用类似于 git_checkout_head 的 go 命令)。

git_index_write_tree 将一个树对象写入对象数据库,而 git_index_write 将索引的内存表示写入磁盘。两者都不会影响工作目录。

英文:

If you want the working directory to match the state of the new HEAD commit then you need to check it out (e.g. with the go equivalent of git_checkout_head).

git_index_write_tree writes a tree object to the object database and git_index_write writes the in-memory representation of the index out to disk. Neither affect the working directory.

huangapple
  • 本文由 发表于 2017年3月24日 03:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/42985879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定