-
Notifications
You must be signed in to change notification settings - Fork 11
/
git-origdate.py
87 lines (79 loc) · 2.68 KB
/
git-origdate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# file: git-origdate.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2015-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2015-01-03T16:31:09+01:00
# Last modified: 2024-05-12T12:24:12+0200
"""Report when files named on the command line were checked into git."""
import argparse
import logging
import os
import subprocess as sp
import sys
__version__ = "2024.05.12"
def main():
args = setup()
origdir = os.getcwd()
for path in args.files:
dirname, filename = os.path.split(path)
args = [
"git",
"--no-pager",
"log",
"--diff-filter=A",
"--format=%aI",
"--",
filename,
]
if dirname:
logging.info(f"changing to directory {dirname}")
os.chdir(dirname)
cp = sp.run(args, stdout=sp.PIPE, stderr=sp.DEVNULL, text=True)
logging.debug(f"file {path}, returncode = {cp.returncode}")
os.chdir(origdir)
if cp.returncode == 128:
logging.warning(f"“{path}” not in a git repository; skipping")
continue
elif cp.returncode != 0:
logging.warning(f"git returned {cp.returncode}; skipping “{path}”")
continue
dates = cp.stdout.strip().splitlines()
if len(dates) > 1:
logging.debug(f"multiple dates returned for {path}:")
for d in dates:
logging.debug(f"{d}")
# Sometimes this git command will return *multiple dates*!
# In that case, select the oldest.
print(f'{path}: {dates[-1]}')
def setup():
"""Parse command-line arguments. Check for required programs."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--log",
default="info",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'info')",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument(
"files", nargs="*", help="one or more files to process"
)
args = parser.parse_args(sys.argv[1:])
logging.basicConfig(
level=getattr(logging, args.log.upper(), None),
format="%(levelname)s: %(message)s",
)
logging.debug(f"Command line arguments = {sys.argv}")
logging.debug(f"Parsed arguments = {args}")
# Check for required programs.
try:
sp.run(["git"], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.debug("found “git”")
except FileNotFoundError:
logging.error("the program “git” cannot be found")
sys.exit(1)
return args
if __name__ == "__main__":
main()