copy vs rename detection: avoid unnecessary O(n*m) loops
[git/git-p4.git] / git-repack.sh
blobe72adc4d91efb8eb6dc96c1f431c8863c408439b
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
6 USAGE='[-a|-A] [-d] [-f] [-l] [-n] [-q] [--max-pack-size=N] [--window=N] [--window-memory=N] [--depth=N]'
7 SUBDIRECTORY_OK='Yes'
8 . git-sh-setup
10 no_update_info= all_into_one= remove_redundant= keep_unreachable=
11 local= quiet= no_reuse= extra=
12 while test $# != 0
14 case "$1" in
15 -n) no_update_info=t ;;
16 -a) all_into_one=t ;;
17 -A) all_into_one=t
18 keep_unreachable=--keep-unreachable ;;
19 -d) remove_redundant=t ;;
20 -q) quiet=-q ;;
21 -f) no_reuse=--no-reuse-object ;;
22 -l) local=--local ;;
23 --max-pack-size=*) extra="$extra $1" ;;
24 --window=*) extra="$extra $1" ;;
25 --window-memory=*) extra="$extra $1" ;;
26 --depth=*) extra="$extra $1" ;;
27 *) usage ;;
28 esac
29 shift
30 done
32 # Later we will default repack.UseDeltaBaseOffset to true
33 default_dbo=false
35 case "`git config --bool repack.usedeltabaseoffset ||
36 echo $default_dbo`" in
37 true)
38 extra="$extra --delta-base-offset" ;;
39 esac
41 PACKDIR="$GIT_OBJECT_DIRECTORY/pack"
42 PACKTMP="$GIT_OBJECT_DIRECTORY/.tmp-$$-pack"
43 rm -f "$PACKTMP"-*
44 trap 'rm -f "$PACKTMP"-*' 0 1 2 3 15
46 # There will be more repacking strategies to come...
47 case ",$all_into_one," in
48 ,,)
49 args='--unpacked --incremental'
51 ,t,)
52 if [ -d "$PACKDIR" ]; then
53 for e in `cd "$PACKDIR" && find . -type f -name '*.pack' \
54 | sed -e 's/^\.\///' -e 's/\.pack$//'`
56 if [ -e "$PACKDIR/$e.keep" ]; then
57 : keep
58 else
59 args="$args --unpacked=$e.pack"
60 existing="$existing $e"
62 done
64 if test -z "$args"
65 then
66 args='--unpacked --incremental'
67 elif test -n "$keep_unreachable"
68 then
69 args="$args $keep_unreachable"
72 esac
74 args="$args $local $quiet $no_reuse$extra"
75 names=$(git pack-objects --non-empty --all --reflog $args </dev/null "$PACKTMP") ||
76 exit 1
77 if [ -z "$names" ]; then
78 if test -z "$quiet"; then
79 echo Nothing new to pack.
82 for name in $names ; do
83 fullbases="$fullbases pack-$name"
84 chmod a-w "$PACKTMP-$name.pack"
85 chmod a-w "$PACKTMP-$name.idx"
86 if test "$quiet" != '-q'; then
87 echo "Pack pack-$name created."
89 mkdir -p "$PACKDIR" || exit
91 for sfx in pack idx
93 if test -f "$PACKDIR/pack-$name.$sfx"
94 then
95 mv -f "$PACKDIR/pack-$name.$sfx" \
96 "$PACKDIR/old-pack-$name.$sfx"
98 done &&
99 mv -f "$PACKTMP-$name.pack" "$PACKDIR/pack-$name.pack" &&
100 mv -f "$PACKTMP-$name.idx" "$PACKDIR/pack-$name.idx" &&
101 test -f "$PACKDIR/pack-$name.pack" &&
102 test -f "$PACKDIR/pack-$name.idx" || {
103 echo >&2 "Couldn't replace the existing pack with updated one."
104 echo >&2 "The original set of packs have been saved as"
105 echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
106 exit 1
108 rm -f "$PACKDIR/old-pack-$name.pack" "$PACKDIR/old-pack-$name.idx"
109 done
111 if test "$remove_redundant" = t
112 then
113 # We know $existing are all redundant.
114 if [ -n "$existing" ]
115 then
116 sync
117 ( cd "$PACKDIR" &&
118 for e in $existing
120 case " $fullbases " in
121 *" $e "*) ;;
122 *) rm -f "$e.pack" "$e.idx" "$e.keep" ;;
123 esac
124 done
127 git prune-packed $quiet
130 case "$no_update_info" in
131 t) : ;;
132 *) git-update-server-info ;;
133 esac