linux - 如何提交到裸存储库?

标签 linux git mono libgit2 libgit2sharp

我正在为 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() 静态辅助方法可能会在此处为您提供帮助。它将允许从现有 CommitTree 的实际内容创建 TreeDefinition

标准流程是从 Commit A 构建一个 TreeDefinition,更新 TreeDefinition(通过添加/从中删除条目),从中创建一个 Tree 并最终创建一个新的 Commit B 其父级将是 Commit A

你可以看看这个 test 展示了这个确切的用法(注意:测试实际上并没有更新 HEAD 引用以使其指向新创建的提交,但是您的代码确实已经解决了这个问题)。

关于linux - 如何提交到裸存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22147207/

相关文章:

linux - tar exclude 在 bash 脚本中不起作用

linux - 期望 - telnet 连接

linux - 不能硬链接(hard link) gitconfig 文件

git - 如何从两周前或从特定日期开始 git checkout -b ?

linux - 将 ubuntu 虚拟机升级到 OpenGL 4

java - 从 linux : No JDK found 上的图标启动 Intellij IDEA

git - .gitignore 不忽略 `git status` 上带有空格的文件名

asp.net-mvc-4 - 访问路径 "/etc/mono/registry"被拒绝

mono - 单声道上的 NUnit?

c# - Mono PrivateFontCollection.AddFontFile 错误的解决方法