-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-index
executable file
·122 lines (110 loc) · 2.29 KB
/
update-index
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh -eu
SHIT_PATH=$(dirname "$0")
. $SHIT_PATH/common.sh
write_index_header() (
nentries="$1"
printf 'DIRC'
write_int32 $INDEX_VERSION
write_int32 $nentries
# TODO: Extensions would go here if we cared
)
write_index_file() (
path="$(normalize_path "$1")"
stat=$(stat -c "%W %Y %d %i %u %g %s" "$path") # XXX: GNUism
ctime=$(printf "%s" "$stat" | cut -d' ' -f1)
mtime=$(printf "%s" "$stat" | cut -d' ' -f2)
dev=$(printf "%s" "$stat" | cut -d' ' -f3)
inode=$(printf "%s" "$stat" | cut -d' ' -f4)
uid=$(printf "%s" "$stat" | cut -d' ' -f5)
gid=$(printf "%s" "$stat" | cut -d' ' -f6)
size=$(printf "%s" "$stat" | cut -d' ' -f7)
write_int32 "$ctime"
write_int32 0 # nanoseconds
write_int32 "$mtime"
write_int32 0 # nanoseconds
write_int32 "$dev"
write_int32 "$inode"
# object type & mode
if [ -x "$path" ]
then
write_int32 $((0x8000 | 0755))
else
write_int32 $((0x8000 | 0644))
fi
write_int32 "$uid"
write_int32 "$gid"
write_int32 "$size"
sha=$("$SHIT_PATH"/hash-object -w "$path")
write_hex "$sha"
# XXX: If file name length is >0xFFF this is wrong
write_int16 "${#path}"
printf '%s\0' "$path"
padding=$((${#path} + 1 + 62))
padding=$((padding % 8))
padding=$(((8 - padding) % 8))
while [ $padding -gt 0 ]
do
printf '\0'
padding=$((padding-1))
done
)
write_index_link() (
printf '%s' "Symlinks are not implemented\n" >&2
exit 1
)
do_add=0
do_remove=0
force_remove=0
while [ $# -ne 0 ]
do
case "$1" in
--add)
do_add=1
;;
--remove)
do_remove=1
;;
--force-remove)
do_remove=1
force_remove=1
;;
*)
break
;;
esac
shift
done
# TODO: Update existing index
cleanup_old_index() {
if [ $? -eq 0 ]
then
rm "$GIT_DIR"/index.old
else
# Restore old index on error
mv "$GIT_DIR"/index.old "$GIT_DIR"/index
fi
}
if [ -f "$GIT_DIR"/index ]
then
mv "$GIT_DIR"/index "$GIT_DIR"/index.old
trap cleanup_old_index EXIT
fi
write_index_header "$#" >>"$GIT_DIR"/index
for path in "$@"
do
printf "%s\n" "$path"
done | gitsort | while read -r path
do
if [ -f "$path" ]
then
write_index_file "$path" >>"$GIT_DIR"/index
elif [ -L "$path" ]
then
write_index_link "$path" >>"$GIT_DIR"/index
else
printf "Invalid path for indexing: %s\n" "$path" >&2
exit 1
fi
done
sha=$(sha1sum "$GIT_DIR"/index | cut -d' ' -f1)
write_hex "$sha" >>"$GIT_DIR"/index