-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgit-smartlog.py
More file actions
executable file
·99 lines (78 loc) · 3.05 KB
/
git-smartlog.py
File metadata and controls
executable file
·99 lines (78 loc) · 3.05 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
import argparse
import configparser
import git
import logging
import os
import sys
from smartlog.builder import TreeBuilder
from smartlog.printer import TreePrinter, TreeNodePrinter, RefMap
from time import time
CONFIG_FNAME = "smartlog"
logging.basicConfig()
logger = logging.getLogger("smartlog")
logger.setLevel(logging.ERROR)
def parse_args():
parser = argparse.ArgumentParser(description="Git Smartlog")
parser.add_argument("-a", "--all", action="store_true", help="Force display all commits, regardless of time")
return parser.parse_args()
def main():
start_time = time()
args = parse_args()
# Compute minimum commit time for displayed commits
if args.all:
date_limit = None
else:
date_limit = time() - (14 * 24 * 3600) # 14 days
# Attempt to open the git repo in the current working directory
cwd = os.getcwd()
try:
repo = git.Repo(cwd, search_parent_directories=True)
except git.exc.InvalidGitRepositoryError:
print("Could not find a git repository at {}".format(cwd))
exit(1)
# Load the smartlog config file
config = configparser.ConfigParser(allow_no_value = True)
config.read(os.path.join(repo.git_dir, CONFIG_FNAME))
refmap = RefMap(repo.head)
head_refname = config.get("remote", "head", fallback="origin/HEAD")
try:
head_ref = repo.refs[head_refname]
refmap.add(head_ref)
except IndexError:
print(f"Unable to find {head_refname} branch")
exit(1)
tree_builder = TreeBuilder(repo, head_ref.commit, date_limit = date_limit)
# Add current head commit
tree_builder.add(repo.head.commit, ignore_date_limit = True)
# Add all local branches (and remote tracking too)
for ref in repo.heads:
logger.debug("Adding local branch {}".format(ref.name))
tree_builder.add(ref.commit)
refmap.add(ref)
try:
remote_ref = ref.tracking_branch()
if remote_ref is not None:
logger.debug("Adding remote tracking branch {}".format(remote_ref.name))
if remote_ref.commit != ref.commit:
tree_builder.add(remote_ref.commit)
refmap.add(remote_ref)
except ValueError:
pass
# Add any extra remote branches from the config file
if config.has_section("extra_refs"):
for key in config["extra_refs"]:
try:
ref = repo.refs[key]
refmap.add(ref)
tree_builder.add(ref.commit)
except IndexError:
print(f"Unable to find {key} ref. Check configuration in .git/{CONFIG_FNAME} file")
node_printer = TreeNodePrinter(repo, refmap)
tree_printer = TreePrinter(repo, node_printer)
tree_printer.print_tree(tree_builder.root_node)
if tree_builder.skip_count > 0:
print("Skipped {} old commits. Use `-a` argument to display them.".format(tree_builder.skip_count))
print("Finished in {:.2f} s.".format(time() - start_time))
if __name__ == "__main__":
main()