Coverage for pyguymer3/git_commits.py: 7%
14 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def git_commits(
5 cwd,
6 /,
7 *,
8 fname = None,
9 gitPath = None,
10 timeout = 60.0,
11):
12 # Import standard modules ...
13 import datetime
14 import shutil
15 import subprocess
17 # **************************************************************************
19 # Try to find the paths if the user did not provide them ...
20 if gitPath is None:
21 gitPath = shutil.which("git")
22 assert gitPath is not None, "\"git\" is not installed"
24 # Check if the user wants the commit for the whole repository or just a
25 # single file ...
26 if fname is None:
27 # Find the UNIX timestamps of all of the commits in the repository ...
28 resp = subprocess.run(
29 [
30 gitPath,
31 "log",
32 "--format=format:%ct",
33 ],
34 check = True,
35 cwd = cwd,
36 encoding = "utf-8",
37 stderr = subprocess.STDOUT,
38 stdout = subprocess.PIPE,
39 timeout = timeout,
40 )
41 else:
42 # Find the UNIX timestamps of all of the commits for the file ...
43 resp = subprocess.run(
44 [
45 gitPath,
46 "log",
47 "--follow",
48 "--format=format:%ct",
49 fname,
50 ],
51 check = True,
52 cwd = cwd,
53 encoding = "utf-8",
54 stderr = subprocess.STDOUT,
55 stdout = subprocess.PIPE,
56 timeout = timeout,
57 )
59 # Convert list of UNIX timestamps (as strings) to list of aware datetime
60 # objects ...
61 commits = []
62 for line in resp.stdout.strip().splitlines():
63 commits.append(datetime.datetime.fromtimestamp(int(line), tz = datetime.UTC))
65 # Return answer ...
66 return commits