-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-cat
executable file
·46 lines (40 loc) · 1.03 KB
/
git-cat
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
#!/bin/bash
# For debugging
#set -x
prog="${0##*/}"
if [ $# -lt 2 ]; then
branch=HEAD
# echo "Usage: $prog branch file [file..]"
# exit 1
else
branch="$1"
fi
root="$(git rev-parse --show-toplevel)"
# check to see if we should stick "origin/" in front
if [[ ! -r "${root}/.git/refs/heads/${branch}" ]]; then
# grep for the branch name fromthe list of branches, pinning it to the end
branches=($(git branch -r | grep -i "${branch}$"))
if [[ ${#branches[@]} == 1 ]]; then
branch="${branches}"
echo "${prog}: using branch $branch"
elif [[ ${#branches[@]} == 0 ]]; then
if [[ $(git cat-file -t ${branch} 2>/dev/null) != "commit" ]]; then
echo "${prog}: Cannot find branch or commit locally or on any server: $branch"
exit 1
fi
else
echo "${prog}: branch name is ambiguous: ${branch}"
for br in "${branches[@]}"; do
echo " $br"
done
exit 1
fi
fi
shift
for file; do
if [[ "${file}" != /* && "${file}" != ./* ]]; then
file="./${file}"
fi
echo git show "${branch}":"${file}"
git show "${branch}":"${file}"
done