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

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

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 (
&quot;fmt&quot;
&quot;gopkg.in/libgit2/git2go.v25&quot;
&quot;time&quot;
)
func main() {
sig := &amp;git.Signature{
Name:  &quot;Some Person&quot;,
Email: &quot;somebody@something.org&quot;,
When:  time.Now(),
}
repo, err := git.OpenRepository(&quot;./repo&quot;)
check(err)
defer repo.Free()
content := []byte(&quot;hello world&quot;)
index, err := repo.Index()
check(err)
oid, err := repo.CreateBlobFromBuffer(content)
check(err)
ie := git.IndexEntry{
Mode: git.FilemodeBlob,
Id:   oid,
Path: &quot;documents/document_9.md&quot;,
}
err = index.Add(&amp;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(&quot;HEAD&quot;, sig, sig, &quot;A new commit&quot;, 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 &quot;git add/rm &lt;file&gt;...&quot; to update what will be committed)
(use &quot;git checkout -- &lt;file&gt;...&quot; 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 &lt;somebody@something.org&gt;
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 &lt;someguy@something.com&gt;
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.

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:

确定