forked from netj/markdown-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-git-changes
executable file
·108 lines (95 loc) · 2.66 KB
/
markdown-git-changes
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
#!/usr/bin/env bash
# Summarize commit history and diff of Markdown files in the current Git repo
# Usage:
# markdown-git-changes [COMMIT [MARKDOWN_FILE]...]
#
# You can specify COMMIT as `--' to automatically see changes of given files
# against the upstream branch, HEAD^, or HEAD.
#
# Author: Jaeho Shin <netj@cs.stanford.edu>
# Created: 2013-11-18
set -eu
Here=$(dirname "$0")
PATH="$Here:$PATH" # add this directory to PATH for markdown-format-wdiff
# TODO passthru any diff options
# how to find markdown files in the current git repo
files=()
find_markdown_files() {
local IFS=$'\n'
set -- $(git ls-files | grep '\.\(md\|mkd\|markdn\|markdown\)$')
files=("$@")
}
# diff against given commit, or upstream branch, or HEAD^ if no local changes, or HEAD
since=--; [ $# -eq 0 ] || { since=$1; shift; }
if [ x"$since" = x"--" ]; then
find_markdown_files
since=`
git rev-parse --abbrev-ref HEAD@{upstream} 2>/dev/null ||
case $(git status --porcelain --untracked-files=no "${files[@]}" | wc -l) in
0) echo 'HEAD^' ;;
*) echo 'HEAD'
esac
`
fi
# unless list of files are given, find Markdown documents in the Git repo
if [ $# -gt 0 ]; then
files=("$@")
elif [ ${#files[@]} -eq 0 ]; then
find_markdown_files
fi
# make sure ${files[@]} isn't empty
[ ${#files[@]} -gt 0 ] || files+=(.)
# summarize commit history
{
echo "\$ git log --oneline "$since"..HEAD $*"
git log --oneline "$since"..HEAD "${files[@]}"
} |
sed 's/^/ /'
echo
# and embed the overall word diff
git diff ${GIT_DIFF_OPTS:-} --word-diff --patch-with-stat \
--minimal --patience \
"$since" -- "${files[@]}" |
sed '
# format prologue
1,/^diff /{
/^diff/! s/^/ /
}
# format file headers
/^diff /,/^+++ /{
/^diff /{
s|^diff .* \([^/]/\)\(.*\)|<div class="file-start"><code>\2</code></div>|
a\
\
}
/^<div class="file-start">/! s/^/ /
}
# format hunks
/^@@ -.* +.* @@/{
s| @@.*| @@|
s|^|<div class="hunk-start"><code>|
s|$|</code></div>|
}
' |
markdown-format-wdiff
echo '
<style>
pre:first-of-type { width: 78%; margin-left: auto; margin-right: auto; }
.file-start + p + pre,
.file-start + pre { margin-left: 61.8%; }
.file-start,
.hunk-start{ text-align: right; }
.file-start code{ font-size: inherit; }
.file-start/*:not(:first-of-type)*/{
font-size: 150%;
margin-top: 23.6%;
border-bottom: 1ex solid #ccc;
padding-bottom: 1ex;
}
.hunk-start{
margin-top: 2ex;
border-bottom: 1ex dashed #ccc;
padding-bottom: 1ex;
}
</style>
'