-
Notifications
You must be signed in to change notification settings - Fork 34
/
git-meld
executable file
·80 lines (66 loc) · 1.79 KB
/
git-meld
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
tmpdir=/tmp/.git-meld
lock="$tmpdir"/.lock
lock_timeout=30
link=meld-git
umask 077
if [[ "$(basename "$0")" = git-meld ]]
then
mkdir -p "$tmpdir"
exec 4>>"$lock"
flock -w "$lock_timeout" -E 43 4
[[ $? -eq 43 ]] && {
echo >&2 "Failed to acquire lock for git-meld: $lock"
# notify-send -i meld\
# 'Failed to acquire lock for git-meld'\
# "Lock path: ${lock}, timeout: ${lock_timeout}s"
exit 1
}
:>"$lock" # flush old contents
GIT_EXTERNAL_DIFF=$link GIT_PAGER= git diff "$@" || exit
lock_meld="$lock".$$
readarray -t files <"$lock"
cp "$lock" "$lock_meld"
exec 5>>"$lock_meld"
flock -n 5 || exit 2
src= meld_args=()
for p in "${files[@]}"; do
[[ -z "$src" ]] && { src=$p; continue; }
meld_args+=( --diff "$src" "$p" ) src=
done
[[ -n "$src" ]] && {
echo >&2 "Uneven number of diff-files (leftover: $src)"
exit 1
}
# Do cleanup for old diffs
for p in "$lock".*; do
pid=${p##*.}
exec 6>>"$p"
flock -n 6 || continue # meld is still alive
readarray -t files <"$p"
rm -f "$p"
for p in "${files[@]}"; do
[[ "$p" =~ ^"$tmpdir"/ ]] && { rm -f "$p"; continue; }
echo >&2 "BUG: tried to remove non-tmpdir ($tmpdir) file - $p (left from pid: $pid)"
exit 3
done
done
[[ "${#meld_args[@]}" -eq 0 ]] && exit 0
exec 1>/dev/null 2>/dev/null # meld is very noisy ;(
exec meld "${meld_args[@]}"
else
# Gather files into tmpdir, pushing names to lockfile
# Expected to be called multiple times by git-diff
[[ ! ( -f "$2" || "$2" = /dev/null ) || ! ( -f "$5" || "$5" = /dev/null ) || "$3" = "$6" ]] && {
echo >&2 "Skipping diff for: $2 ($3) $5 ($6)"
exit 0
}
# old
dst="${tmpdir}/$$.$3.$(basename "$1")"
cp "$2" "$dst" || exit
echo "$dst" >>"$lock"
# new
dst="${tmpdir}/$$.$6.$(basename "$1")"
cp "$5" "$dst" || exit
echo "$dst" >>"$lock"
fi