]> git.phdru.name Git - git-scripts.git/blobdiff - set-commit-date.py
Refactor: Use `$#` to check parameters
[git-scripts.git] / set-commit-date.py
index dc6a5fb732d93a0499323e9197305797a24172d8..a88c5dea43849ddba38a0272f6ce74a24ac9a995 100755 (executable)
@@ -8,20 +8,23 @@
 import os
 import subprocess
 
+git_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'],
+                                   universal_newlines=True).rstrip('\n')
+os.chdir(git_root)
+
 separator = '----- GIT LOG SEPARATOR -----'
 
 git_log = subprocess.Popen(['git', 'log', '-m', '--first-parent',
-                            '--name-only', '--no-color',
+                            '--name-status', '--no-color',
                             '--format=%s%%n%%ct' % separator],
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                            universal_newlines=True)
-filenames = set()
+changed_files = set()
+deleted_files = set()
+
 # stages: 1 - start of commit, 2 - timestamp, 3 - empty line, 4 - files
 stage = 1
-while True:
-    line = git_log.stdout.readline()
-    if not line:  # EOF
-        break
+for line in git_log.stdout:
     line = line.strip()
     if (stage in (1, 4)) and (line == separator):  # Start of a commit
         stage = 2
@@ -35,9 +38,16 @@ while True:
         stage = 4
         assert line == '', line
     elif stage == 4:
-        filename = line
-        if filename not in filenames:
-            filenames.add(filename)
+        if line.startswith('A') or line.startswith('M') \
+                or line.startswith('D'):
+            filename = line.split(None, 2)[1]
+        elif line.startswith('R'):
+            filename = line.split(None, 3)[2]  # renamed to
+        if line.startswith('D') and filename not in changed_files:
+            # The file was not readded
+            deleted_files.add(filename)
+        if filename not in deleted_files and filename not in changed_files:
+            changed_files.add(filename)
             if os.path.exists(filename):
                 os.utime(filename, (time, time))
     else: