英文:
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)
<details>
<summary>英文:</summary>
I need to get git diff files since specific commit using GitPython. As I understood I get renamed files using 'R' 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('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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论