-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitXnormid.sh
executable file
·58 lines (48 loc) · 1.2 KB
/
gitXnormid.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
#!/bin/sh
#
# Internal: Normalize the given ID to a tree ID.
# Copyright (c) Petr Baudis, 2005
#
# Takes an arbitrary ID as a parameter. -c tells it to give
# a commit id rather than tree id.
SHA1="[A-Za-z0-9]{40}"
SHA1ONLY="^$SHA1$"
TREE="^tree $SHA1$"
if [ "$1" = "-c" ]; then
type="commit"
shift
else
type="tree"
fi
id=$1
if [ ! "$id" ]; then
id=$(cat .git/HEAD)
fi
ref=$(echo $id | cut -d\ -f 1)
if [ "$ref" = "ref:" ]; then
id=$(echo $id | cut -d\ -f 2)
id=$(cat .git/$id)
fi
if (echo $id | egrep -vq "$SHA1ONLY") && [ -r ".git/tags/$id" ]; then
id=$(cat ".git/tags/$id")
fi
if (echo $id | egrep -vq "$SHA1ONLY") && [ -r ".git/HEAD.$id" ]; then
id=$(cat ".git/HEAD.$id")
fi
idpref=$(echo "$id" | cut -c -2)
idpost=$(echo "$id" | cut -c 3-)
if [ $(find ".git/objects/$idpref" -name "$idpost*" 2>/dev/null | wc -l) -eq 1 ]; then
id=$idpref$(basename $(echo .git/objects/$idpref/$idpost*))
fi
if echo $id | egrep -vq "$SHA1ONLY"; then
echo "Invalid id: $id" >&2
exit 1
fi
if [ "$type" = "tree" ] && [ $(cat-f -t "$id") = "commit" ]; then
id=$(cat-f commit $id | egrep "$TREE" | cut -d ' ' -f 2)
fi
if [ $(cat-f -t "$id") != "$type" ]; then
echo "Invalid id: $id" >&2
exit 1
fi
echo $id