Skip to content

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
Discussion options

You must be logged in to vote

+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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@SongXueZhi
Comment options

Answer selected by SongXueZhi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

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