-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-diff-branch
executable file
·83 lines (74 loc) · 1.98 KB
/
git-diff-branch
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
#!/usr/bin/env bash
set -euo pipefail
if [[ $# == 0 ]]; then
echo "Usage:" >&2
echo " git diff-branch [-m] BRANCH [GIT_DIFF_ARGUMENTS]" >&2
exit 1
fi
MATCH=$1
shift
MAJOR=N
if [[ "$MATCH" == "-m" ]]; then
MAJOR=Y
MATCH=$1
shift
fi;
: "${REMOTE:=origin}"
REMOTE_URL=$(git remote get-url "$REMOTE")
if [[ "${REMOTE_URL%/*}" != "[email protected]:odoo" ]]; then
echo "🚫 This tool only works on github.com/odoo repositories. URL of remote ${REMOTE} is '${REMOTE_URL}'." >&2
exit 1
fi
AWK="awk"
if command -v gawk >/dev/null; then
AWK="gawk"
fi
PREV=$(
# shellcheck disable=SC2016
git branch --remotes --list "${REMOTE}"'/*' | $AWK -F/ '!/tmp|staging|nightly|HEAD/ {
branch=$2;
len = split(gensub("saas-", "", 1, branch), a, ".");
if(len == 1) {
a[2] = a[1];
switch(a[2]) {
case "master":
a[1] = a[2] = 9999;
break;
case /^[1-5]$/:
a[1] = 7;
break;
case 6:
a[1] = 8;
break;
case /^[789]$|^1[0123]$/:
a[1] = 9;
break;
case /^1[45678]$/:
a[1] = 10;
break;
default:
a[1] = -1;
};
}
else if (len != 2 || int(a[1]) < 1 || int(a[2]) != a[2])
a[1] = -1;
if (a[1] != -1)
printf("%d.%02d:%s\n", a[1], a[2], branch);
}' | sort -n | $AWK -F: --assign "MATCH=$MATCH" --assign "MAJOR=$MAJOR" '{
branch = $2;
if (branch == MATCH) {
print prev
}
}
{
if (MAJOR == "N" || index($1, ".00") != 0) {
prev = branch;
};
}'
)
if [[ -z "$PREV" ]]; then
echo "🙁 Cannot determine previous branch of $MATCH" >&2
exit 1
fi
set -x
git diff --no-prefix "${REMOTE}/${PREV}..${REMOTE}/${MATCH}" "$@"