Skip to content

How do I make commits with a specified date? #315

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

Closed
avinassh opened this issue Jul 16, 2015 · 2 comments
Closed

How do I make commits with a specified date? #315

avinassh opened this issue Jul 16, 2015 · 2 comments

Comments

@avinassh
Copy link
Contributor

I can't seem to find out how to make commit with specifying author_date and commit_date, but it did not really work:

import git
...
repo = git.Repo.init(repo_path)
repo.index.add(['some_file'])
repo.index.commit(message, authored_date=commit_date, committed_date=commit_date)

throws:

TypeError: commit() got an unexpected keyword argument 'authored_date'

Then I looked at the source and found out it does not really have parameters for commit datetime. (or am I missing something? Would you be interested in a PR which adds these parameters? I am interested to learn and make contribution 😄 )

So, I either I have to create a commit then change dates (using Commit object) or use git directly.

or set OS envs before commit call:

...
os.environ['GIT_AUTHOR_DATE'] = date_in_iso
os.environ['GIT_COMMITTER_DATE'] = date_in_iso
repo.index.commit(message)
@Byron Byron assigned Byron and unassigned Byron Jul 17, 2015
@Byron
Copy link
Member

Byron commented Jul 17, 2015

As you noted, there is currently no way to specify these arguments within the call. As a workaround, using environment variables seems to be working. Even though it appears slightly awkward as part of an API, I believe GitPython attempts to support standard git environment variables wherever possible.

To me it seems like a very viable amendment to support these parameters as part of the method signature. These could be added without breaking the API, and thus I would be very happy if you would consider to make a contribution. The difficulty of this task also makes it a good learning experience.

The only real advice I can make is to assure you can execute the tests locally without issues. You might want to look at the .travis.yml file to see what kind of preparations fresh checkouts need to be viable for this and how to run the tests - unfortunately the tests are not properly sandboxed and will use the GitPython checkout for read-only operations and tests.

If you have any questions coming up while working on the PR, please don't hesitate to post them here.

@Byron Byron added this to the v1.0.2 - Fixes milestone Jul 17, 2015
@avinassh
Copy link
Contributor Author

Great 😄

So, I was looking at the code again and I think adding this feature is fairly easy. Since the API already supports environment variables, most of the heavy holding is already done. here. Here's what I am gonna do:

  1. Add parameters to take the dates author_date, commit_date (same as Commit object takes them) here
  2. Add a new if block and move rest in elif. Since user is specifying the dates, it takes the precedence. (explicit dates > os env > default date)
  3. No need to do the parsing the date, parse_date already does that.

Currently reading tests code. I think I should write a test similar to this but a smaller version, with just checking the dates. Will send a PR soon!

@Byron Byron closed this as completed in 58c78e6 Jul 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants