forked from palday/automatic-winner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-diff-md.py
More file actions
executable file
·128 lines (105 loc) · 4.61 KB
/
git-diff-md.py
File metadata and controls
executable file
·128 lines (105 loc) · 4.61 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
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
#! /usr/bin/env python3
import sys
import argparse
from subprocess import call
parser = argparse.ArgumentParser(description='Change boldfance for Pandoc Markdown',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('revision',type=str,default="0",
help='Git revision to find differences to')
parser.add_argument('blame',type=argparse.FileType('r'),
nargs='?',default=sys.stdin,
help='Output from git blame -s')
parser.add_argument('out',type=argparse.FileType('w'),
nargs='?',default=sys.stdout,
help='Output file')
parser.add_argument('--skip-yaml-block',type=bool,default=True,
help="Don't circumfix a YAML (when located at top of file)")
parser.add_argument('--prefix',type=str,nargs='?',default='**',
help='Prefix changed lines with this symbol')
parser.add_argument('--suffix',type=str,nargs='?',default='**',
help='Suffix changed lines with this symbol')
# need to add text about nodiff
# need to add text about being able to work around basic latex formatting
# need to add note that this won't find differences resulting from non-related diffs
def is_ancestor(hashA, hashB):
'''test whether commit A is an ancestor of commit B'''
# this is trivial, and we don't allow us to be our own grandpa
# this won't work if there's a collision in the short hashes, but then again
# other things would break anyway, so don't be stupid, okay?
if hashA in hashB or hashB in hashA:
return False
# git merge-base returns 0 for ancestor, so we have to negate
return not call(['git', 'merge-base', '--is-ancestor', hashA, hashB])
yaml_delim = set(['---', '...'])
def main(argv=None):
args = parser.parse_args(argv)
lineno = 1
if args.skip_yaml_block and args.blame.readline().strip() in yaml_delim:
line = args.blame.readline()
line = line.split(maxsplit=2)
rev, gitlineno = line[:2]
line = line[2] if len(line) == 3 else ''
rev = rev[1:] if rev[0] == "^" else rev # strip leading caret
gitlineno = int(gitlineno[:-1]) # strip trailing parenthesis
print('{}'.format(line.rstrip()),file=args.out)
# this keeps us form spinning uselessly if we hit the end of a file
# without ending the header block
for line in args.blame:
line = line.split(maxsplit=2)
rev, gitlineno = line[:2]
line = line[2] if len(line) == 3 else ''
rev = rev[1:] if rev[0] == "^" else rev # strip leading caret
gitlineno = int(gitlineno[:-1]) # strip trailing parenthesis
lineno += 1
print('{}'.format(line.rstrip()),file=args.out)
if line.strip() in yaml_delim:
break
#print(lineno,"lines skipped in YAML block")
in_caption = False
skip = False
prefix = args.prefix
suffix = args.suffix
for line in args.blame:
close_env = False
line = line.split(maxsplit=2)
rev, gitlineno = line[:2]
line = line[2] if len(line) == 3 else ''
rev = rev[1:] if rev[0] == "^" else rev # strip leading caret
gitlineno = int(gitlineno[:-1]) # strip trailing parenthesis
line = line.rstrip()
if "<nodiff>" in line:
skip = True
elif "</nodiff>" in line:
skip = False
lineno += 1
#print(int(rev))
# pandoc assumes everything in a latex block is latex, so we have
# to do latex style formatting in captions
if r"\caption{" in line:
in_caption = True
if args.prefix in ['*','**']:
suffix = "}"
if args.prefix == '*':
prefix = r'\emph{'
else:
prefix = r'\textbf{'
if in_caption and line[-1] == '}':
in_caption = False
prefix = args.prefix
suffix = args.suffix
if line == ']':
close_env = True
begin_capt_lbl = r"\caption{" in line or r"\label{" in line
empty = len(line) == 0
header = "#" in line
modified = is_ancestor(args.revision, rev)
if modified and not any([header, empty, begin_capt_lbl, skip, close_env]):
ending = ""
if line[-2:] == '^[':
ending = line[-2:]
line = line[:-2]
print('{}{}{}{}'.format(prefix,line,suffix,ending),file=args.out)
else:
print('{}'.format(line),file=args.out)
if __name__ == '__main__':
sys.exit(main())