我正在为fuse(linux)编写一个git包装器来访问git存储库,比如普通文件和目录.
访问分支,标签和提交的文件夹和文件非常有效,但是当我提交文件时,我会遇到一种奇怪的行为.
我做以下的手风琴:
>从流中创建一个新的Blob(磁盘上的temporaney文件)
>创建一个新的TreeDefinition
>创建一个新树
>在ObjectDatabase中创建一个新提交
> update branch refs to new commit
之后,我更新分支引用,我只查看更新的文件,没有别的!
这里有代码
String referenceName = null; IEnumerable 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 pathParts = new List(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时,最终创建的树将只包含一个条目.
TreeDefinition.From()静态帮助器方法可以在这里帮助您.它将允许从现有Commit或Tree的实际内容创建TreeDefinition.
标准流程是从Commit A构建TreeDefinition,更新TreeDefinition(通过添加/删除条目),从中创建一个Tree,最终生成一个新的Commit B,其父级将是Commit A.
你可以看看这个test,它显示了这种确切的用法(注意:测试实际上不会更新HEAD引用,使其指向新创建的提交,但是你的代码已经解决了这个问题).来源:https://www.icode9.com/content-3-427301.html