英文:
Adding and committing a new file with git2go
问题
我目前正在努力将文件暂存和提交到内存中的git仓库(即不将文件写入磁盘,然后添加和提交它)。
这是我代码的简化版本;它几乎可以工作
package main
import (
"fmt"
"gopkg.in/libgit2/git2go.v25"
"time"
)
func main() {
sig := &git.Signature{
Name: "Some Person",
Email: "somebody@something.org",
When: time.Now(),
}
repo, err := git.OpenRepository("./repo")
check(err)
defer repo.Free()
content := []byte("hello world")
index, err := repo.Index()
check(err)
oid, err := repo.CreateBlobFromBuffer(content)
check(err)
ie := git.IndexEntry{
Mode: git.FilemodeBlob,
Id: oid,
Path: "documents/document_9.md",
}
err = index.Add(&ie)
check(err)
treeId, err := index.WriteTree()
check(err)
tree, err := repo.LookupTree(treeId)
check(err)
index.Write()
currentBranch, err := repo.Head()
check(err)
currentTip, err := repo.LookupCommit(currentBranch.Target())
check(err)
commitId, err := repo.CreateCommit("HEAD", sig, sig, "A new commit", tree, currentTip)
check(err)
fmt.Println(commitId)
}
func check(err error) {
if err != nil {
panic(err)
}
}
然而,当我运行它并查看./repo
时,有些地方不太对劲。正如你从git status
中可以看到的那样,新的document_9.md
文件被标记为已删除(即git知道它存在,但实际上不存在)。
❯ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: documents/document_9.md
查看日志后,可以确认提交实际上是成功的:
❯ git log --patch
commit a609b43e6a3f7da7d0589971ab0f64f3f432e76c
Author: Some Person <somebody@something.org>
Date: Thu Mar 23 19:43:56 2017 +0000
a new commit
diff --git a/documents/document_9.md b/documents/document_9.md
new file mode 100644
index 0000000..95d09f2
--- /dev/null
+++ b/documents/document_9.md
@@ -0,0 +1 @@
+hello world
\ No newline at end of file
commit 668762279b51483c05dbdedcf3c599a1b41c203b
Author: Original Committer <someguy@something.com>
Date: Thu Mar 23 19:42:52 2017 +0000
First commit
diff --git a/documents/docment_1.md b/documents/docment_1.md
new file mode 100644
index 0000000..2965834
--- /dev/null
+++ b/documents/docment_1.md
@@ -0,0 +1 @@
+# 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
package main
import (
"fmt"
"gopkg.in/libgit2/git2go.v25"
"time"
)
func main() {
sig := &git.Signature{
Name: "Some Person",
Email: "somebody@something.org",
When: time.Now(),
}
repo, err := git.OpenRepository("./repo")
check(err)
defer repo.Free()
content := []byte("hello world")
index, err := repo.Index()
check(err)
oid, err := repo.CreateBlobFromBuffer(content)
check(err)
ie := git.IndexEntry{
Mode: git.FilemodeBlob,
Id: oid,
Path: "documents/document_9.md",
}
err = index.Add(&ie)
check(err)
treeId, err := index.WriteTree()
check(err)
tree, err := repo.LookupTree(treeId)
check(err)
index.Write()
currentBranch, err := repo.Head()
check(err)
currentTip, err := repo.LookupCommit(currentBranch.Target())
check(err)
commitId, err := repo.CreateCommit("HEAD", sig, sig, "A new commit", tree, currentTip)
check(err)
fmt.Println(commitId)
}
func check(err error) {
if err != nil {
panic(err)
}
}
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)
❯ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: documents/document_9.md
Looking at the log, it's confirmed that the commit actually worked:
❯ git log --patch
commit a609b43e6a3f7da7d0589971ab0f64f3f432e76c
Author: Some Person <somebody@something.org>
Date: Thu Mar 23 19:43:56 2017 +0000
a new commit
diff --git a/documents/document_9.md b/documents/document_9.md
new file mode 100644
index 0000000..95d09f2
--- /dev/null
+++ b/documents/document_9.md
@@ -0,0 +1 @@
+hello world
\ No newline at end of file
commit 668762279b51483c05dbdedcf3c599a1b41c203b
Author: Original Committer <someguy@something.com>
Date: Thu Mar 23 19:42:52 2017 +0000
First commit
diff --git a/documents/docment_1.md b/documents/docment_1.md
new file mode 100644
index 0000000..2965834
--- /dev/null
+++ b/documents/docment_1.md
@@ -0,0 +1 @@
+# 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论