-
Notifications
You must be signed in to change notification settings - Fork 9
/
.contributors.sh
executable file
·139 lines (122 loc) · 2.71 KB
/
.contributors.sh
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
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
set -e
dryrun="--dry-run"
name="sa-sawp"
email="[email protected]"
mailmap=
mapfile=
ref="main"
function help() {
read -r -d '' HELP << EOM
Usage:
$0 [options] [--] <comma-seperated mailmap>
Validate and rewrite author history.
Options:
-m use mailmap file for replacements
-n default name to use in replacements
-e default email to use in replacements
-r git ref
--replace rewrite git history for the specified ref
-h help
EOM
echo "$HELP"
}
options=$(getopt -o hn:e:m:r: --long replace -- "$@")
[ $? -eq 0 ] || {
echo "Invalid arguments"
help
exit 1
}
eval set -- "$options"
while true; do
case "$1" in
-h)
help
exit 0
;;
-n)
shift;
name=$1
;;
-e)
shift;
email=$1
;;
-m)
shift;
mapfile=$1
;;
-r)
shift;
ref=$1
;;
--replace)
dryrun=
;;
--)
shift
# process and trim comma seperated values
mailmap=$(echo "$@" | tr ',' '\n' | awk '{$1=$1};1')
break
;;
esac
shift
done
# show all contributors
function contributors() {
git log --pretty="%an <%ae>%n%cn <%ce>" $ref | sort | uniq
}
# show contributors not in .contributors
function diff_contributors() {
# sorted order is different on centos and ubuntu
tmp="/tmp/.contributors.tmp"
cat .contributors | sort > $tmp
contributors | comm -23 - $tmp
rm $tmp
}
# fails if a contributor is not in the approved list
function validate_contributors() {
# return status
local ret=1
# show lines unique to the git log (not in .contributors)
local result=$(diff_contributors)
if test -z "$result"; then
echo "Contributors on $ref"
echo "=========================================="
contributors
echo
ret=0
else
echo "Not in contributors list on $ref"
echo "=========================================="
echo -e "$result"
echo
fi
return $ret
}
# uses default author for unapproved contributors
function mailmap() {
if [ -n "$mapfile" ]; then
cat "$mapfile"
fi
if [ -n "$mailmap" ]; then
echo -e "$mailmap"
fi
if [[ -z "$mapfile" && -z "$mailmap" ]]; then
diff_contributors | xargs -L1 echo "$name <$email>"
fi
}
# exit with the return value of this command
validate_contributors || :
echo "Changes"
echo "======="
mailmap $name $email > .mailmap
cat .mailmap
echo
echo "Git filter-repo"
echo "==============="
git filter-repo $dryrun --force --use-mailmap --refs $ref
rm .mailmap
echo
# show the results of running the filter-repo command
validate_contributors