代码之家  ›  专栏  ›  技术社区  ›  Thomas

裸存储库可以有索引吗?这是虫子吗?

  •  0
  • Thomas  · 技术社区  · 15 年前

    GitPython 0.3 将文件提交到存储库。大致来说,我是这样做的:

    data = ...
    istream = repo.odb.store(gitdb.IStream(git.Blob.type, len(data), StringIO(data)))
    entry = git.BaseIndexEntry((stat.S_IFREG | 0644, istream.binsha, 0, path))
    index = git.IndexFile.from_tree(repo, repo.heads['master'])
    index.add([entry])
    index.commit(commit_message)
    

    对于非裸存储库,这一点与预期一样有效。请注意,我从不显式地接触文件系统,只接触Git的对象数据库。

    IndexFile.add 函数用 git_working_dir 装饰师:

    @git_working_dir
    def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=None, 
                write=True):
        """Add files from the working tree, specific blobs or BaseIndexEntries
        to the index. 
    

    这个装潢师想把房子租给回购人 working_tree_dir ,以便正确解析路径引用。然而, 对于空存储库无效,引发 AssertionError

    有人知道这个装修工为什么在这里吗?它是仅仅用于路径解析,还是不可能在空存储库中创建索引?这是GitPython中的bug,还是我对Git的理解?


    编辑:类似地 IndexFile.remove 函数断言(通过 default_index decorator)我们是默认索引。裸存储库当然没有默认索引,但它们是否可以根本没有索引对象?

    @post_clear_cache
    @default_index
    def remove(self, items, working_tree=False, **kwargs):
        """Remove the given items from the index and optionally from
        the working tree as well.
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   mipadi    15 年前

    API reference :

    Decorator,它将当前工作目录更改为git存储库中的一个目录,以确保正确处理相对路径

    裸Git存储库没有工作目录,因此 add 函数被挂断了。

    然而,裸Git回购也没有索引 [1] .

        2
  •  0
  •   Thomas    15 年前

    在仔细检查 IndexFile.add 功能,我意识到我只需要很少的功能。事实上,只要更换 add 用这两条线打电话有个窍门:

    index.entries[index.entry_key(entry)] = git.IndexEntry.from_base(entry)
    index.write()
    

    我还在想这是不是个好主意。。。

    推荐文章