]> git.phdru.name Git - git-scripts.git/blobdiff - set-commit-date.py
Feat(submodules/remove): Add option `-c`
[git-scripts.git] / set-commit-date.py
index 9588bd87aa26e991006637b3e7779c2df82ffa99..c0aaa8cf999bae085a6ab0c484a7cc23edc7f5bf 100755 (executable)
@@ -5,20 +5,45 @@
 
 # Adapted from https://git.wiki.kernel.org/index.php/ExampleScripts#Setting_the_timestamps_of_the_files_to_the_commit_timestamp_of_the_commit_which_last_touched_them  # noqa
 
+import argparse
 import os
+import sys
 import subprocess
 
+git_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'],
+                                   universal_newlines=True).rstrip('\n')
+os.chdir(git_root)
+
+parser = argparse.ArgumentParser(description='Set commit date')
+parser.add_argument('-a', '--author', '--author-date',
+                    action='store_true')
+parser.add_argument('-c', '--committer', '--committer-date',
+                    action='store_true')
+args = parser.parse_args()
+
+if args.author and args.committer:
+    parser.print_help()
+    sys.exit("Use only one of `-a' or `-c' but not both")
+
+if args.author:
+    date_format = 'a'
+else:
+    date_format = 'c'  # This is the default
+
 separator = '----- GIT LOG SEPARATOR -----'
 
 git_log = subprocess.Popen(['git', 'log', '-m', '--first-parent',
-                            '--name-only', '--no-color',
-                            '--format=%s%%n%%ct' % separator],
-                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-filenames = set()
+                            '--name-status', '--no-color',
+                            '--format=%s%%n%%%st' % (separator, date_format)],
+                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+                           universal_newlines=True)
+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().strip()
+for line in git_log.stdout:
+    line = line.strip()
     if (stage in (1, 4)) and (line == separator):  # Start of a commit
         stage = 2
     elif stage == 2:
@@ -30,13 +55,18 @@ while True:
             continue
         stage = 4
         assert line == '', line
-    elif not line:  # EOF
-        break
     elif stage == 4:
-        filename = line
-        if filename not in filenames:
-            filenames.add(filename)
-            if os.path.exists(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) and not os.path.islink(filename):
                 os.utime(filename, (time, time))
     else:
         raise ValueError("stage: %d, line: %s" % (stage, line))