Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitpython to check for if there are any changes in files using PYTHON

I am experimenting with gitpython and I am new to it. I am trying to detect if there are any changes staged for commit.

Currently, I have a function that looks like this:

def commit(dir):
r = Repo(dir)
r.git.add(A=True)
r.git.commit(m='commit all')

But this is just the code to commit the directory. I wanna do something like, if there are changes, then display some message, else, display another message.

Anybody have any idea how do I do it in python ?

like image 836
decemberrobot Avatar asked Nov 14 '25 15:11

decemberrobot


1 Answers

You can check for all unstaged changes like this:

for x in r.index.diff("HEAD"):
    # Just print
    print(x)

    # Or for each entry you can find out information about it, e.g.
    print(x.new_file)
    print(x.b_path)

Basically you are comparing the staging area (i.e. index) to the active branch.

like image 141
dpwr Avatar answered Nov 17 '25 03:11

dpwr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!