Don't write a log entry if there were no changes
authorKarl Hasselström <kha@treskal.com>
Sun, 21 Sep 2008 12:17:41 +0000 (14:17 +0200)
committerKarl Hasselström <kha@treskal.com>
Sun, 21 Sep 2008 12:19:07 +0000 (14:19 +0200)
Some commands end up calling log_entry() without verifying that they
did in fact change anything. (One example of this is a conflicting
push, which will log two entries, everything else and the conflicting
push, with the "everything else" part being empty if there was only
one patch to push.) So before appending to the log, make sure that the
entry we're appending isn't a no-op.

Signed-off-by: Karl Hasselström <kha@treskal.com>
stgit/commands/reset.py
stgit/lib/log.py

index 6db9559..593ed30 100644 (file)
@@ -119,7 +119,8 @@ def func(parser, options, args):
     stack = directory.repository.current_stack
     if len(args) >= 1:
         ref, patches = args[0], args[1:]
-        state = log.get_log_entry(stack.repository, ref)
+        state = log.get_log_entry(stack.repository, ref,
+                                  stack.repository.rev_parse(ref))
     else:
         raise common.CmdException('Wrong number of arguments')
     return reset_stack(stack, stack.repository.default_iw, state, patches,
index f4e079e..9568e8f 100644 (file)
@@ -286,24 +286,36 @@ class LogEntry(object):
                 tree = tree, message = self.message,
                 parents = [self.simplified] + parents))
 
-def get_log_entry(repo, ref):
+def get_log_entry(repo, ref, commit):
     try:
-        return LogEntry.from_commit(repo, repo.rev_parse(ref))
+        return LogEntry.from_commit(repo, commit)
     except LogException, e:
         raise LogException('While reading log from %s: %s' % (ref, e))
 
+def same_state(log1, log2):
+    """Check whether two log entries describe the same current state."""
+    s = [[lg.head, lg.applied, lg.unapplied, lg.hidden, lg.patches]
+         for lg in [log1, log2]]
+    return s[0] == s[1]
+
 def log_entry(stack, msg):
     """Write a new log entry for the stack."""
     ref = log_ref(stack.name)
     try:
-        last_log = stack.repository.refs.get(ref)
+        last_log_commit = stack.repository.refs.get(ref)
     except KeyError:
-        last_log = None
+        last_log_commit = None
     try:
+        if last_log_commit:
+            last_log = get_log_entry(stack.repository, ref, last_log_commit)
+        else:
+            last_log = None
         new_log = LogEntry.from_stack(last_log, stack, msg)
     except LogException, e:
         out.warn(str(e), 'No log entry written.')
         return
+    if last_log and same_state(last_log, new_log):
+        return
     new_log.write_commit()
     stack.repository.refs.set(ref, new_log.commit, msg)