GitPython:将重命名的文件视为已删除和已添加

huangapple go评论94阅读模式
英文:

GitPython: get rename files as deleted and added

问题

我需要使用GitPython获取特定提交后的git差异文件据我了解可以使用'R'类型获取重命名的文件在删除列表中是否可以获取rename_from而在添加列表中获取rename_to

我以以下方式成功实现

```python
commit = repo.commit(<commit_hash>)

# 删除的
for diff_del in commit.diff(commit.parents[0]).iter_change_type('D'):
    print(diff_del.a_path)

# 添加的
for diff_del in commit.diff(commit.parents[0]).iter_change_type('A'):
    print(diff_del.a_path)

# 重命名 - 不需要,文件将包含在'deleted'和'added'中
# for diff_mv in commit.diff(commit.parents[0]).iter_change_type('R'):
#     print(diff_mv.a_path)

# 修改的
for diff_mod in commit.diff(commit.parents[0]).iter_change_type('M'):
    print(diff_mod.a_path)

我基于这个问题:https://stackoverflow.com/questions/68422402/extract-paths-to-modified-files-except-for-deleted-or-renamed-files


<details>
<summary>英文:</summary>

I need to get git diff files since specific commit using GitPython. As I understood I get renamed files using &#39;R&#39; type . Is it possible to get the rename_from in the deleted lists and the rename_to in the added list?

I managed to do it this way:

commit = repo.commit(<commit_hash>)

Deleted

for diff_del in commit.diff(commit.parents[0]).iter_change_type('D'):
print(diff_del.a_path)

Added

for diff_del in commit.diff(commit.parents[0]).iter_change_type('A'):
print(diff_del.a_path)

Renamed - No need, Files will be included in 'deleted' and 'added'

#for diff_mv in commit.diff(commit.parents[0]).iter_change_type('R'):

print(diff_mv.a_path)

Modified

for diff_mod in commit.diff(commit.parents[0]).iter_change_type('M'):
print(diff_mod.a_path)


I based on this question: [https://stackoverflow.com/questions/68422402/extract-paths-to-modified-files-except-for-deleted-or-renamed-files](https://stackoverflow.com)

</details>


# 答案1
**得分**: 0

根据 @Tim Roberts 的说法,重命名的文件必须出现在 'R' 部分。因此,我进行了以下解决方案:

```python
added = [diff.b_path for diff in changes.iter_change_type('A')]
deleted = [diff.a_path for diff in changes.iter_change_type('D')]
updated = [diff.a_path for diff in changes.iter_change_type('M')]

for diff in changes.iter_change_type('R'):
    deleted.append(diff.a_path)
    added.append(diff.b_path)
英文:

According to @Tim Roberts, renamed files must appear in 'R' section. Therefore I did this workaround:

added = [diff.b_path for diff in changes.iter_change_type(&#39;A&#39;)]
deleted = [diff.a_path for diff in changes.iter_change_type(&#39;D&#39;)]
updated = [diff.a_path for diff in changes.iter_change_type(&#39;M&#39;)]

for diff in changes.iter_change_type(&#39;R&#39;):
    deleted.append(diff.a_path)
    added.append(diff.b_path)

huangapple
  • 本文由 发表于 2023年7月18日 15:09:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定