我正在为 fuse (linux) 编写一个 git 包装器来像普通文件和目录一样访问 git 存储库。
为分支、标签和提交访问文件夹和文件工作得很好,但是当我提交文件时我得到一个奇怪的行为。
我做了以下 styff:
- 从流(磁盘上的临时文件)创建一个新的 Blob
- 创建一个新的 TreeDefinition
- 创建一棵新树
- 在 ObjectDatabase 中创建一个新提交
- 将分支引用更新为新的提交
之后我更新了分支引用,我只查看了更新后的文件,没有别的!
代码在这里
String referenceName = null;
IEnumerable<Commit> parentCommit = null;
// Riposiziona il puntatore dello stream all'inizio
openedHandle.Stream.Seek(0, SeekOrigin.Begin);
// Crea il blob
Blob blob = this.repository.ObjectDatabase.CreateBlob(openedHandle.Stream);
// Acquisisce la path rimuovendo le prime due parti della path
List<string> pathParts = new List<string>(openedHandle.Path.Split('/'));
pathParts.RemoveRange(0, 3);
// Inserisce il blob in un tree
TreeDefinition treeDefinition = new TreeDefinition();
treeDefinition.Add(String.Join("/", pathParts), blob, Mode.NonExecutableFile);
Tree tree = this.repository.ObjectDatabase.CreateTree(treeDefinition);
// Inizializza l'autore ed il commiter
Signature committer = new Signature("My Name", "abc@def.tld", DateTime.Now);
Signature author = committer;
// Acquisisce l'elenco dei commits
switch (openedHandle.PathType)
{
case PathType.Branches:
Branch branch = this.GetBranchByPath(openedHandle.Path);
referenceName = branch.CanonicalName;
parentCommit = branch.Commits;
break;
default:
throw new Exception("Can update only branches");
}
// Crea il commit
Commit commit = this.repository.ObjectDatabase.CreateCommit(
author,
committer,
(openedHandle.New ? String.Format("{0} created", openedHandle.Path) : String.Format("{0} updated", openedHandle.Path)) + "\r\n",
false,
tree,
parentCommit);
// Aggiorna il riferimento del target
this.repository.Refs.UpdateTarget(this.repository.Refs[referenceName], commit.Id);
最佳答案
TreeDefinition treeDefinition = new TreeDefinition()
将创建一个空的 TreeDefinition。因此,当您向其中添加一个 Blob 时,最终创建的 Tree
将只包含一个条目。
TreeDefinition.From()
静态辅助方法可能会在此处为您提供帮助。它将允许从现有 Commit
或 Tree
的实际内容创建 TreeDefinition
。
标准流程是从 Commit
A 构建一个 TreeDefinition
,更新 TreeDefinition
(通过添加/从中删除条目),从中创建一个 Tree
并最终创建一个新的 Commit
B 其父级将是 Commit
A。
你可以看看这个 test 展示了这个确切的用法(注意:测试实际上并没有更新 HEAD 引用以使其指向新创建的提交,但是您的代码确实已经解决了这个问题)。
关于linux - 如何提交到裸存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22147207/