我正在为fuse(linux)编写git包装器,以便像普通文件和目录一样访问git存储库。
访问分支、标签和提交的文件夹和文件功能良好,但当我提交文件时,会出现奇怪的行为。
我执行以下步骤:
- 从流中创建新的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);