-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-updates.py
executable file
·142 lines (126 loc) · 4.41 KB
/
git-updates.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3 -u
"""
Description: Track updates from git repo
Author: thnikk
"""
import os
from subprocess import run, check_output, CalledProcessError
import argparse
import json
import re
from datetime import datetime, timezone
import sys
from common import print_debug
import tooltip as tt
class Git:
""" Git class """
def __init__(self, path):
self.path = path
self.name = self.get_name()
def get_name(self) -> str:
""" Get name of repo """
name = check_output(
['git', '-C', os.path.expanduser(self.path),
'config', '--get', 'remote.origin.url']
).decode().strip().split('/')[-1].split('.')[0].replace('-', ' ')
return name.capitalize()
def fetch(self) -> None:
""" Fetch """
try:
run(
['git', '-C', self.path, 'fetch'],
check=True, capture_output=True
)
except CalledProcessError as e:
print(e.output)
def commits(self):
""" Get commits """
def plural(num) -> str:
""" Pluralize word """
if num > 1:
return 's'
return ''
def get_time(input_list) -> str:
""" Get string of x days/minutes/hours ago """
for value, word in enumerate(['day', 'hour', 'minute', 'second']):
if input_list[value]:
return (
f"{input_list[value]} "
f"{word}{plural(input_list[value])} ago")
return None
try:
command_output = run(
[
'git', '-C', os.path.expanduser(self.path), 'log',
'--name-only', 'main..origin'
],
check=True, capture_output=True
).stdout.decode('utf-8')
except CalledProcessError:
try:
command_output = run(
[
'git', '-C', os.path.expanduser(self.path), 'log',
'--name-only', 'master..origin'
],
check=True, capture_output=True
).stdout.decode('utf-8')
except CalledProcessError as e:
lines = e.stderr.decode('utf-8').splitlines()
for line in lines:
print_debug(line)
sys.exit(1)
output = {}
for line in command_output.splitlines():
if re.match('^commit', line):
chash = line.split()[1][:7]
output[chash] = {
"author": None, "date": None, "msg": None, "files": []}
elif re.match('^Author:', line):
output[chash]['author'] = \
line.split(":")[1].split("<")[0].strip()
elif re.match('^Date:', line):
date = ':'.join(line.split(":")[1:]).strip()
delta = datetime.now(timezone.utc) - datetime.strptime(
date, '%a %b %d %H:%M:%S %Y %z')
dhms = [
delta.days, delta.seconds // 60 // 60,
delta.seconds // 60, delta.seconds]
output[chash]['date'] = get_time(dhms)
elif re.match('^ ', line):
output[chash]['msg'] = line.strip().replace('&', 'and')
else:
if line:
output[chash]['files'].append(line)
return output
def parse_args():
""" Parse arguments """
parser = argparse.ArgumentParser()
parser.add_argument('path', type=str, help='Path to git repo')
parser.add_argument('-i', '--icon', type=str, default='')
return parser.parse_args()
def main():
""" Main function """
args = parse_args()
git = Git(os.path.expanduser(args.path))
git.fetch()
commits = git.commits()
tooltip = []
for commit, info in commits.items():
tooltip.append(
f"{tt.span(commit, 'blue')} {info['msg']} "
f"({tt.span(info['date'], 'green')})"
)
for file in info['files']:
tooltip.append(f' {file}')
tooltip.append('')
if commits:
print(json.dumps({
"text": f"{args.icon} {len(commits)}",
"tooltip": "\n".join(tooltip).strip(),
"widget": {"name": git.name, "commits": commits}
}))
else:
print(json.dumps({"text": ""}))
if __name__ == "__main__":
main()