-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlog.py
More file actions
76 lines (66 loc) · 2.28 KB
/
gitlog.py
File metadata and controls
76 lines (66 loc) · 2.28 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
# ------------------------------------------------------------------------------
# Python Scripts scriptarium/[gitlog.py]
# (c) balarabe@protonmail.com
# ------------------------------------------------------------------------------
import argparse
import os
import subprocess
# read named parameters from the command line:
pr = argparse.ArgumentParser()
pr.add_argument('--source', help='path to the source Git repository')
pr.add_argument('--author', help='name of author to select (exclude others)')
pr.add_argument('--target', help='target file into which to write changelog')
pr.add_argument('--prefix', help='optional prefix to append before repo name')
args = pr.parse_args()
#
if args.source == None:
print('--source not specified')
quit()
if args.target == None:
print('--target not specified')
quit()
if args.prefix == None:
args.prefix = ""
if args.author == None:
args.author = ""
# change current directory and build repo name
os.chdir(args.source)
cwd = os.getcwd()
parts = cwd.split('/')
repo_name = args.prefix + parts[len(parts)-1]
print('cwd:', cwd, "repo_name:", repo_name)
# execute 'git log' command and store its output in 'lines'
proc = subprocess.Popen(
['git', 'log', '-200', '--date=local', '--pretty=format:"%ai %an: %s"'],
stdout = subprocess.PIPE
)
text = proc.communicate()[0].decode('utf-8')
lines = text.split('\n')
# filter lines by author name, replace author with repo_name
text = '\n\n'
for s in lines:
if args.author != "" and not args.author in s:
continue
s = s[1:len(s)-1] # remove leading and trailing double quotes
s = s[0:20] + s[26:] # remove timezone e.g. +0100
if args.author != "":
s = s.replace(args.author, repo_name)
text += s + '\n'
# append text to target file
fl = open(args.target, 'a')
fl.write(text)
fl.close()
quit()
# sample zsh script to update multiple repos:
# (replace SOME_.. with actual paths and names)
#
# for repo in \
# "/SOME_PATH/REPO1" \
# "/SOME_PATH/REPO2"
# do
# python3 /SOME_PATH/scriptarium/gitlog.py \
# --source $repo \
# --prefix "SOME_PREFIX/" --author "SOME_AUTHOR_NAME" \
# --target /SOME_PATH/SOME_TARGET.txt
# done
# end