diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2015-01-07 15:35:55 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2015-01-07 15:35:55 +0100 |
commit | f4a49ff2dddc66bbe25af554caba2351fbf21702 (patch) | |
tree | 54ba82b7490038b911fc46bc1407cd7955bd7edf | |
parent | 45eb728554953fafcee2aab0f76ca65e005326b0 (diff) | |
parent | c6ee00d0dadcd7b10d60a2985db4fe137ca7cfed (diff) | |
download | gitpython-f4a49ff2dddc66bbe25af554caba2351fbf21702.tar.gz |
Merge branch 'firm1-commit_by_actor'
-rw-r--r-- | doc/source/tutorial.rst | 5 | ||||
-rw-r--r-- | git/objects/commit.py | 2 | ||||
-rw-r--r-- | git/refs/log.py | 26 | ||||
-rw-r--r-- | git/test/test_index.py | 21 | ||||
-rw-r--r-- | git/util.py | 2 |
5 files changed, 46 insertions, 10 deletions
diff --git a/doc/source/tutorial.rst b/doc/source/tutorial.rst index d9b35fda..3f45b70d 100644 --- a/doc/source/tutorial.rst +++ b/doc/source/tutorial.rst @@ -297,7 +297,10 @@ Access objects and add/remove entries. Commit the changes:: # Access the entries directly index.add(['my_new_file']) # add a new file to the index index.remove(['dir/existing_file']) - new_commit = index.commit("my commit message") + new_commit = index.commit("my commit message") # commit by commit message first + my_author = Actor("An author", "author@example.com") + my_committer = Actor("A committer", "committer@example.com") + next_commit = index.commit("my commit message", author=my_author, commiter=my_committer) # commit by commit message and author and committer Create new indices from other trees or as result of a merge. Write that result to a new index file:: diff --git a/git/objects/commit.py b/git/objects/commit.py index 8f93d1b9..f2ce91ca 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -358,7 +358,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): # as well ... import git.refs try: - repo.head.set_commit(new_commit, logmsg="commit: %s" % message) + repo.head.set_commit(new_commit, logmsg=message) except ValueError: # head is not yet set to the ref our HEAD points to # Happens on first commit diff --git a/git/refs/log.py b/git/refs/log.py index 8ce98d30..f8dc88da 100644 --- a/git/refs/log.py +++ b/git/refs/log.py @@ -18,6 +18,7 @@ from git.objects.util import ( altz_to_utctz_str, ) from git.compat import ( + PY3, xrange, string_types, defenc @@ -32,16 +33,30 @@ __all__ = ["RefLog", "RefLogEntry"] class RefLogEntry(tuple): """Named tuple allowing easy access to the revlog data fields""" - _fmt = "%s %s %s <%s> %i %s\t%s\n" _re_hexsha_only = re.compile('^[0-9A-Fa-f]{40}$') __slots__ = tuple() def __repr__(self): """Representation of ourselves in git reflog format""" + res = self.format() + if PY3: + return res + else: + # repr must return a string, which it will auto-encode from unicode using the default encoding. + # This usually fails, so we encode ourselves + return res.encode(defenc) + + def format(self): + """:return: a string suitable to be placed in a reflog file""" act = self.actor time = self.time - return self._fmt % (self.oldhexsha, self.newhexsha, act.name, act.email, - time[0], altz_to_utctz_str(time[1]), self.message) + return u"{0} {1} {2} <{3}> {4!s} {5}\t{6}\n".format(self.oldhexsha, + self.newhexsha, + act.name, + act.email, + time[0], + altz_to_utctz_str(time[1]), + self.message) @property def oldhexsha(self): @@ -267,10 +282,9 @@ class RefLog(list, Serializable): lf = LockFile(filepath) lf._obtain_lock_or_raise() - fd = open(filepath, 'ab') try: - fd.write(repr(entry).encode(defenc)) + fd.write(entry.format().encode(defenc)) finally: fd.close() lf._release_lock() @@ -295,7 +309,7 @@ class RefLog(list, Serializable): # write all entries for e in self: - write(repr(e).encode(defenc)) + write(e.format().encode(defenc)) # END for each entry def _deserialize(self, stream): diff --git a/git/test/test_index.py b/git/test/test_index.py index f7504b32..4fdd3d1b 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -1,3 +1,4 @@ +#-*-coding:utf-8-*- # test_index.py # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # @@ -10,6 +11,7 @@ from git.test.lib import ( fixture, with_rw_repo ) +from git.util import Actor from git import ( IndexFile, BlobFilter, @@ -432,7 +434,7 @@ class TestIndex(TestBase): # TEST COMMITTING # commit changed index cur_commit = cur_head.commit - commit_message = "commit default head" + commit_message = u"commit default head by Frèderic Çaufl€" new_commit = index.commit(commit_message, head=False) assert cur_commit != new_commit @@ -445,6 +447,23 @@ class TestIndex(TestBase): assert len(new_commit.parents) == 1 assert cur_head.commit == cur_commit + # commit with other actor + cur_commit = cur_head.commit + + my_author = Actor(u"Frèderic Çaufl€", "author@example.com") + my_committer = Actor(u"Committing Frèderic Çaufl€", "committer@example.com") + commit_actor = index.commit(commit_message, author=my_author, committer=my_committer) + assert cur_commit != commit_actor + assert commit_actor.author.name == u"Frèderic Çaufl€" + assert commit_actor.author.email == "author@example.com" + assert commit_actor.committer.name == u"Committing Frèderic Çaufl€" + assert commit_actor.committer.email == "committer@example.com" + assert commit_actor.message == commit_message + assert commit_actor.parents[0] == cur_commit + assert len(new_commit.parents) == 1 + assert cur_head.commit == commit_actor + assert cur_head.log()[-1].actor == my_committer + # same index, no parents commit_message = "index without parents" commit_no_parents = index.commit(commit_message, parent_commits=list(), head=True) diff --git a/git/util.py b/git/util.py index 4d1ea8d6..b1f3b38d 100644 --- a/git/util.py +++ b/git/util.py @@ -328,7 +328,7 @@ class Actor(object): return self.name def __repr__(self): - return '<git.Actor "%s <%s>">' % (self.name, self.email) + return u'<git.Actor "%s <%s>">' % (self.name, self.email) @classmethod def _from_string(cls, string): |