-
Notifications
You must be signed in to change notification settings - Fork 2
/
github-diff
executable file
·61 lines (44 loc) · 1.55 KB
/
github-diff
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
#!/usr/bin/env python3
################################################################################
#
# Description:
# ------------
# Converts the git-diff output into an expandable GitHub comment form, and
# puts into the system clipboard to allow for easy pasting into the GitHub
# comment.
#
# Usage:
# ------
# (first place this script in one of the directories in $PATH)
# $> cd path/to/repo/where/you/want/to/diff
# $> PAGER= git diff [commits range] | github-diff
# now go to GitHub and paste into the comment box
#
################################################################################
import os
import sys
import tempfile
def die(msg):
print(msg)
sys.exit(1)
tmp_file = tempfile.NamedTemporaryFile(mode='w')
tmp_file.write('<details>\n')
tmp_file.write('<summary>Diff</summary>\n')
# A blank line is necessary after the summary to make sure that the details
# section gets properly formatted
tmp_file.write('\n')
tmp_file.write('```diff\n')
diff_has_something = False
for line in sys.stdin:
diff_has_something = True
tmp_file.write(line)
if not diff_has_something:
die('Empty diff, nothing to do')
tmp_file.write('```')
# TODO: find out why seeking to the beginning is necessary. If I don't do this,
# I see that only a part of the file (starting from the beginning however) gets
# copied to the clipboard
# Seek to the beginning
tmp_file.seek(0)
os.system("cat '{}' | xclip -selection clipboard".format(tmp_file.name))
print("GitHub comment-friendly diff copied to clipboard")