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

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

GitPython: get rename files as deleted and added

问题

  1. 我需要使用GitPython获取特定提交后的git差异文件据我了解可以使用'R'类型获取重命名的文件在删除列表中是否可以获取rename_from而在添加列表中获取rename_to
  2. 我以以下方式成功实现
  3. ```python
  4. commit = repo.commit(<commit_hash>)
  5. # 删除的
  6. for diff_del in commit.diff(commit.parents[0]).iter_change_type('D'):
  7. print(diff_del.a_path)
  8. # 添加的
  9. for diff_del in commit.diff(commit.parents[0]).iter_change_type('A'):
  10. print(diff_del.a_path)
  11. # 重命名 - 不需要,文件将包含在'deleted'和'added'中
  12. # for diff_mv in commit.diff(commit.parents[0]).iter_change_type('R'):
  13. # print(diff_mv.a_path)
  14. # 修改的
  15. for diff_mod in commit.diff(commit.parents[0]).iter_change_type('M'):
  16. print(diff_mod.a_path)

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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?
  4. 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)

  1. I based on this question: [https://stackoverflow.com/questions/68422402/extract-paths-to-modified-files-except-for-deleted-or-renamed-files](https://stackoverflow.com)
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 根据 @Tim Roberts 的说法,重命名的文件必须出现在 'R' 部分。因此,我进行了以下解决方案:
  6. ```python
  7. added = [diff.b_path for diff in changes.iter_change_type('A')]
  8. deleted = [diff.a_path for diff in changes.iter_change_type('D')]
  9. updated = [diff.a_path for diff in changes.iter_change_type('M')]
  10. for diff in changes.iter_change_type('R'):
  11. deleted.append(diff.a_path)
  12. added.append(diff.b_path)
英文:

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

  1. added = [diff.b_path for diff in changes.iter_change_type(&#39;A&#39;)]
  2. deleted = [diff.a_path for diff in changes.iter_change_type(&#39;D&#39;)]
  3. updated = [diff.a_path for diff in changes.iter_change_type(&#39;M&#39;)]
  4. for diff in changes.iter_change_type(&#39;R&#39;):
  5. deleted.append(diff.a_path)
  6. 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:

确定