GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — How can i set create_patch=true, and meanwhile get change_type in Diff · gitpython-developers/GitPython · Discussion #1898

How can i set create_patch=true, and meanwhile get change_type in Diff #1898

Answered by topp
SongXueZhi asked this question in Q&A
How can i set create_patch=true, and meanwhile get change_type in Diff #1898
@SongXueZhi SongXueZhi
Apr 2, 2024 · 2 comments · 1 reply
Answered by topp Return to top
Discussion options
  • Uh oh!

    There was an error while loading. Please reload this page.

SongXueZhi
Apr 2, 2024

I find that when i use commit1.diff(commit2,create_patch = true), chang_type of Diff is aways none. if create_patch = False, i kown that i can get chang_type of Diff , but I can get hunk text.

Can i get Diff text ,and meanWhile get chang_type of Diff?

You must be logged in to vote
All reactions
Answered by topp Apr 15, 2024

+1 For getting change_type also set on diff item when create_patch=True.

@SongXueZhi Approach depends on your intention for further processing, but you can certainly retrieve the change type info even from a Diff generated with create_patch=True:

def get_change_type_of_diff_item(diff_item):
    if diff_item.new_file:
        change_type = 'Added'
    elif diff_item.deleted_file:
        change_type = 'Deleted'
    elif diff_item.renamed_file:
        change_type = 'Renamed'
    elif diff_item.copied_file:
        change_type = 'Copied'
    else:
        change_type = 'Modified'
    return change_type

Usage Example:

repo = git.Repo('/path/to/repository')
diff_index = repo.head.commit.diff…
View full answer

Replies: 2 comments · 1 reply

Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

topp
Apr 15, 2024

+1 For getting change_type also set on diff item when create_patch=True.

@SongXueZhi Approach depends on your intention for further processing, but you can certainly retrieve the change type info even from a Diff generated with create_patch=True:

def get_change_type_of_diff_item(diff_item):
    if diff_item.new_file:
        change_type = 'Added'
    elif diff_item.deleted_file:
        change_type = 'Deleted'
    elif diff_item.renamed_file:
        change_type = 'Renamed'
    elif diff_item.copied_file:
        change_type = 'Copied'
    else:
        change_type = 'Modified'
    return change_type

Usage Example:

repo = git.Repo('/path/to/repository')
diff_index = repo.head.commit.diff(None, create_patch=True)
for diff in diff_index:
    print(f"{get_change_type_of_diff_item(diff)}: {diff.a_path if diff.a_path else diff.b_path}")
```
You must be logged in to vote
All reactions
1 reply
@SongXueZhi
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

SongXueZhi Apr 18, 2024
Author

You're welcome! I ultimately chose to use pygit2, as it seems more flexible and better suited to my needs.

All reactions
Answer selected by SongXueZhi
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

deepdive101
Mar 4, 2025

If you want to do it in a single line, although it's just slightly inefficient this way:

def get_file_diff_type(d: git.Diff) -> str:
    # Note: `diff.change_type` is not used because it was observed to not be populated when using `create_patch=True`.
    return {d.copied_file: 'copied', d.deleted_file: 'deleted', d.new_file: 'new', d.renamed_file: 'renamed'}.get(True, 'modified')
You must be logged in to vote
All reactions
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
@SongXueZhi @topp @deepdive101
Converted from issue

This discussion was converted from issue #1897 on April 02, 2024 09:08.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.