我正在为 fuse (linux) 编写一个 git 包装器来访问 git 存储库,如普通文件和目录。
访问分支、标签和提交的文件夹和文件效果很好,但是当我提交文件时会出现奇怪的行为。
我做了以下工作:
- 从流(磁盘上的临时文件)创建一个新的 Blob
- 创建一个新的树定义
- 创建一棵新树
- 在 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);