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: 2 comments 1 reply

Comment options

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

Answer selected by SongXueZhi
Comment options

You must be logged in to vote
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
Converted from issue

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy