-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblamehim
executable file
·80 lines (64 loc) · 1.58 KB
/
blamehim
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
#!/bin/bash
# parse options
usage (){
echo 'usage:' `basename $0` '[ -a <author> ] [ -e <email> ] range file'
exit
}
if [[ ! -d .git ]]; then
echo "error: Not a git repository."
exit
fi
while getopts "hfa:e:" flag; do
case $flag in
a) author=$OPTARG;;
e) email=$OPTARG;;
f) force=1;;
h) usage;;
\?) usage;;
esac
done
shift $(($OPTIND - 1))
range=$1
file=$2
if [[ -z $file ]]; then
echo "error: No file is specified."
exit
fi
# parse range pattern
range_arr=$(echo $range|tr ",-" "\n,")
# get real user and email
real_user=`git config user.name`
real_addr=`git config user.email`
# set fake user as current user
fake_user=${author:-$real_user}
fake_addr=${email:-$real_addr}
# hard_mode use git filter-branch to rewrite history
hard_mode (){
tmp_file="${TMPDIR%/}/blamehe-$$.$RANDOM"
for r in $range_arr; do
git blame --abbrev=40 $file|sed -n "$r p"|cut -d " " -f 1|sed 's/^\^//' >> $tmp_file
done
git filter-branch -f --env-filter "
if grep \$GIT_COMMIT $tmp_file >/dev/null 2>&1; then
GIT_AUTHOR_NAME=$fake_user
GIT_AUTHOR_EMAIL=$fake_addr
fi
" -- HEAD
rm $tmp_file
}
# soft_mode appends an extra commit with fake user's name and email address
soft_mode (){
# append *one* space to target lines
for r in $range_arr; do
sed -i '' -e "$r s/$/ /" $file
done
# do commit as fake_user
git add $file
git commit -m 'make some minor changes :P' --author="$fake_user <$fake_addr>"
}
if [[ $force ]]; then
hard_mode
else
soft_mode
fi
echo "Done yean!"