简体   繁体   English

GitPython:如何提交更新的子模块

[英]GitPython: how to commit updated submodule

I have been at this for hours now, and although I have a feeling I'm close I can't seem to figure this out. 我已经在这几个小时了,虽然我有一种感觉,我很接近我似乎无法解决这个问题。

I'm trying to make a script that takes a git repository, updates a submodule in that repository to a specified version, and commits that change. 我正在尝试创建一个带有git存储库的脚本,将该存储库中的子模块更新为指定版本,并提交更改。

What works: 什么有效:

I can find the repository, get the submodule and check out the commit I want. 我可以找到存储库,获取子模块并检查我想要的提交。

What doesn't work: 什么行不通:

I can't seem to add the updated submodule hash so I can commit it. 我似乎无法添加更新的子模块哈希,所以我可以提交它。

My Code: 我的代码:

repos = Repo('path/to/repos')
submodule = repos.submodule('submodule-name')
submodule.module().git.checkout('wanted commit')

diff = repos.index.diff(None)

At this point I can see the submodule-change. 此时我可以看到子模块的变化。 If I check sourcetree, I can see the changed submodule in the 'unstaged files'. 如果我检查sourcetree,我可以在'unstaged files'中看到更改的子模块。 The thing is, I have no clue how to stage the change so I can commit it. 问题是,我不知道如何进行更改以便我可以提交。

What I have tried: 我尝试过的:

  • If I commit using repos.index.commit('') , it creates an empty commit. 如果我使用repos.index.commit('')提交,则会创建一个空提交。
  • If I try to add the path of the submodule using repos.index.add([submodule.path]) , all files in the submodule are added to the repository, which is definately not what I want. 如果我尝试使用repos.index.add([submodule.path])添加子模块的路径,则子repos.index.add([submodule.path])所有文件都会添加到存储库中,这肯定不是我想要的。
  • If I try to add the submodule itself (which should be possible according to the docs) using repos.index.add([submodule]) , nothing seems to happen. 如果我尝试使用repos.index.add([submodule])添加子模块本身(根据文档应该是可能的repos.index.add([submodule]) ,似乎什么也没发生。

There are two ways to add new submodule commits to the parent repository. 有两种方法可以将新的子模块提交添加到父存储库。 One will use the git command directly, the other one will be implemented in pure-python. 一个将直接使用git命令,另一个将在pure-python中实现。

All examples are based on the code presented in the question. 所有示例都基于问题中提供的代码。

Simple 简单

repos.git.add(submodule.path)
repos.index.commit("updated submodule to 'wanted commit'")

The code above will invoke the git command, which is similar to doing a git add <submodule.path> in a shell. 上面的代码将调用git命令,这类似于在shell中执行git add <submodule.path>

Pythonic Python化

submodule.binsha = submodule.module().head.commit.binsha
repos.index.add([submodule])
repos.index.commit("updated submodule to 'wanted commit'")

The IndexFile.add(...) implementation adds whichever binsha it finds in the submodule object. binsha IndexFile.add(...)实现添加它在子模块对象中找到的任何binsha This one would be the one in the parent repository's current commit, and not the commit that was checked out in the submodule. 这个将是父存储库当前提交中的那个,而不是在子模块中检出的提交。 One can see the Submodule object as a singular snapshot of the submodule, which does not change, nor does not it know about changes to the submodule's repository. 可以看到Submodule对象是Submodule的单个快照,它不会改变,也不会知道子模块库的更改。

In this case it seems easiest to just overwrite the binsha field of the submodule object to the one that is actually checked out in its repository, so adding it to the index will have the desired effect. 在这种情况下,将子模块对象的binsha字段覆盖到其存储库中实际检出的字段似乎是最简单的,因此将其添加到索引将具有所需的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM