From 141b78f42c7a3c1da1e5d605af3fc56aceb921ab Mon Sep 17 00:00:00 2001 From: avi Date: Fri, 17 Jul 2015 21:58:55 +0530 Subject: [PATCH 1/2] Added two extra paramaters for commit to take author date and commit date --- git/index/base.py | 5 +++-- git/objects/commit.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/git/index/base.py b/git/index/base.py index f86968007..78120da39 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -922,7 +922,7 @@ def move(self, items, skip_errors=False, **kwargs): return out - def commit(self, message, parent_commits=None, head=True, author=None, committer=None): + def commit(self, message, parent_commits=None, head=True, author=None, committer=None, author_date=None, commit_date=None): """Commit the current default index file, creating a commit object. For more information on the arguments, see tree.commit. @@ -932,7 +932,8 @@ def commit(self, message, parent_commits=None, head=True, author=None, committer run_commit_hook('pre-commit', self) tree = self.write_tree() rval = Commit.create_from_tree(self.repo, tree, message, parent_commits, - head, author=author, committer=committer) + head, author=author, committer=committer, + author_date=author_date, commit_date=commit_date) run_commit_hook('post-commit', self) return rval diff --git a/git/objects/commit.py b/git/objects/commit.py index d301e3014..376b451d9 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -266,7 +266,8 @@ def _iter_from_process_or_stream(cls, repo, proc_or_stream): finalize_process(proc_or_stream) @classmethod - def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False, author=None, committer=None): + def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False, author=None, committer=None, + author_date=None, commit_date=None): """Commit the given tree, creating a commit object. :param repo: Repo object the commit should be part of @@ -288,6 +289,8 @@ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False, configuration is used to obtain this value. :param committer: The name of the committer, optional. If unset, the repository configuration is used to obtain this value. + :param author_date: The timestamp for the author field + :param commit_date: The timestamp for the committer field :return: Commit object representing the new commit @@ -327,14 +330,18 @@ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False, offset = altzone author_date_str = env.get(cls.env_author_date, '') - if author_date_str: + if author_date: + author_time, author_offset = parse_date(author_date) + elif author_date_str: author_time, author_offset = parse_date(author_date_str) else: author_time, author_offset = unix_time, offset # END set author time committer_date_str = env.get(cls.env_committer_date, '') - if committer_date_str: + if commit_date: + committer_time, committer_offset = parse_date(commit_date) + elif committer_date_str: committer_time, committer_offset = parse_date(committer_date_str) else: committer_time, committer_offset = unix_time, offset From e3068025b64bee24efc1063aba5138708737c158 Mon Sep 17 00:00:00 2001 From: avi Date: Fri, 17 Jul 2015 22:18:46 +0530 Subject: [PATCH 2/2] added tests for commits with dates --- git/index/base.py | 3 ++- git/test/test_index.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/git/index/base.py b/git/index/base.py index 78120da39..b955dae4e 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -922,7 +922,8 @@ def move(self, items, skip_errors=False, **kwargs): return out - def commit(self, message, parent_commits=None, head=True, author=None, committer=None, author_date=None, commit_date=None): + def commit(self, message, parent_commits=None, head=True, author=None, + committer=None, author_date=None, commit_date=None): """Commit the current default index file, creating a commit object. For more information on the arguments, see tree.commit. diff --git a/git/test/test_index.py b/git/test/test_index.py index 8c3775d2b..2fd53f656 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -470,6 +470,17 @@ def mixed_iterator(): assert cur_head.commit == commit_actor assert cur_head.log()[-1].actor == my_committer + # commit with author_date and commit_date + cur_commit = cur_head.commit + commit_message = u"commit with dates by Avinash Sajjanshetty" + + new_commit = index.commit(commit_message, author_date="2006-04-07T22:13:13", commit_date="2005-04-07T22:13:13") + assert cur_commit != new_commit + print(new_commit.authored_date, new_commit.committed_date) + assert new_commit.message == commit_message + assert new_commit.authored_date == 1144447993 + assert new_commit.committed_date == 1112911993 + # same index, no parents commit_message = "index without parents" commit_no_parents = index.commit(commit_message, parent_commits=list(), head=True)