-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
shit
executable file
·55 lines (46 loc) · 1.32 KB
/
shit
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
#!/bin/bash
#
# shit – Short Git.
#
# Makes commit indexes from git-linearize ("Extremely Linear Git History") usable with git.
#
# "shit show 14" -> "git show 00000140"
# "shit log 10..14" -> "git log 00000100..00000140"
# Final program to execute, arguments will be added to this array
X=("git")
# Convert arguments matching this format
SINGLE='^[0-9]{1,7}$'
# Range formats matching: "shit log 10..14"
RANGE='^([0-9]{1,7})(\.{2,3})([0-9]{1,7})$'
# Format to convert to
# Set EL_FORMAT externally to override
EL_FORMAT="${EL_FORMAT:-%07d0}"
function expand() {
# shellcheck disable=SC2059 # Disabled because EL_FORMAT contains the format
padded=$(printf "$EL_FORMAT" "$1")
# Find commit with the given prefix reachable from HEAD
reachable=$(git rev-list HEAD | grep --max-count 1 "$padded" | head -n1)
if [[ $reachable ]]; then
echo "$reachable"
return 0
fi
# Fallback to using the prefix
echo "$padded"
}
for arg in "$@"; do
if [[ $arg =~ $SINGLE ]]; then
# Looks like a number, add padding
out=$(expand "$arg")
X+=("$out")
elif [[ $arg =~ $RANGE ]]; then
# Convert ranges like "10..14" to "00000100..00000140"
first=$(expand "${BASH_REMATCH[1]}")
dots=${BASH_REMATCH[2]}
second=$(expand "${BASH_REMATCH[3]}")
X+=("${first}${dots}${second}")
else
# Not a number, keep as-is
X+=("$arg")
fi
done
"${X[@]}"