Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't commit if this would be an empty commit #1056

Closed
mhajder opened this issue Sep 6, 2020 · 2 comments
Closed

Don't commit if this would be an empty commit #1056

mhajder opened this issue Sep 6, 2020 · 2 comments
Labels
Q&A

Comments

@mhajder
Copy link

@mhajder mhajder commented Sep 6, 2020

Hi,
I have no idea how to write a functions without committing and sending code, if there are no changes and it's an empty commit.

from git import Repo, Actor

repo = Repo(full_path)
assert not repo.bare

origin = repo.remote('origin')
assert origin.exists()
assert origin == repo.remotes.origin == repo.remotes['origin']

origin.pull()
repo.git.add(f'{full_path}/file_list.txt')
repo.git.add(f'{full_path}/files/')

repo.index.commit(f'Time now: {time_now}')
origin.push()

Currently, after running such code, a commit is created with no changes to the files. I can't find any function that checks for any changes before commit after adding files. I don't mean changes to all code and to all files in the main .git folder, but changes to one file and one folder with many files.

I am also looking for a solution other than using untracked_files in a loop looking to see if my needed files are there.

@Byron Byron added the Q&A label Sep 7, 2020
@Byron
Copy link
Member

@Byron Byron commented Sep 7, 2020

A commit has a link to a tree, which identifies the state of all files contained with it.
Thus, if the tree hash does not change between commits, they refer to the same state.
I believe one can get a handle on a tree with commit.tree.

Even though I am closing the issue, please feel free to keep posting here for any follow ups.

@Byron Byron closed this Sep 7, 2020
@mhajder
Copy link
Author

@mhajder mhajder commented Sep 7, 2020

Thanks for the answer. I was able to find such a solution. I don't know if it's completely correct but it's important that it works.

from git import Repo, Actor

repo = Repo(full_path)
assert not repo.bare

origin = repo.remote('origin')
assert origin.exists()
assert origin == repo.remotes.origin == repo.remotes['origin']

origin.pull()
repo.git.add(f'{full_path}/file_list.txt')
repo.git.add(f'{full_path}/files/')

if len(repo.index.diff('HEAD')) > 0:
    repo.index.commit(f'Time now: {time_now}')
    origin.push()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.