I would recommend GitPython which is full featured, very well documented and easy to use IMO. For more info, check out this answer
Answer from Daniel Perez on Stack OverflowGitPython can stage changes for commit that git won't commit
python - Get changed files using gitpython - Stack Overflow
repo.index.diff( head ) show new file as deleted
Gitpython how to check if a package is up to date?
for item in repo.index.diff(None):
print(item.a_path)
or to get just the list:
changedFiles = [item.a_path for item in repo.index.diff(None)]
repo.index.diff() returns git.diff.Diffable described in http://gitpython.readthedocs.io/en/stable/reference.html#module-git.diff
So the function can look like this:
def get_status(repo, path):
changed = [item.a_path for item in repo.index.diff(None)]
if path in repo.untracked_files:
return "untracked"
elif path in changed:
return "modified"
else:
return "don't care"
just to catch up on @ciasto piekarz question: depending on what you want to show:
repo.index.diff(None)
does only list files that have not been staged
repo.index.diff('Head')
does only list files that have been staged
Hello there, for a project I make I need to check if a package is up to date before pulling it with the git package here is what I have in mind:
repo = git.Repo(myrepo)
origin = repo.remote()
if ?packageIsUpToDate()? == True:
origin.pull()
print('The package is updated')
else:
print('The package is already up to date')This is the ideal way of doing it, if it's not possible pulling and then telling if the package was up to date is another acceptable solution such as:
repo = git.Repo(myrepo)
origin = repo.remote()
origin.pull()
if ?packagePulledSuccsessfully()? == True:
print('The package is updated')
else:
print('The package is already up to date')See https://stackoverflow.com/a/15862203/197789
E.g.
commits_behind = repo.iter_commits('master..origin/master')
and
commits_ahead = repo.iter_commits('origin/master..master')
Then you can use something like the following to go from iterator to a count:
count = sum(1 for c in commits_ahead)
(You may want to fetch from the remotes before running iter_commits, eg: repo.remotes.origin.fetch())
This was last checked with GitPython 1.0.2.
The following worked better for me, which I got from this stackoverflow answer
commits_diff = repo.git.rev_list('--left-right', '--count', f'{branch}...{branch}@{{u}}')
num_ahead, num_behind = commits_diff.split('\t')
print(f'num_commits_ahead: {num_ahead}')
print(f'num_commits_behind: {num_behind}')
» pip install GitPython