Merge branch 'sg/t3020-typofix' into maint
[git] / contrib / examples / git-repack.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Linus Torvalds
4 #
5
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git repack [options]
9 --
10 a               pack everything in a single pack
11 A               same as -a, and turn unreachable objects loose
12 d               remove redundant packs, and run git-prune-packed
13 f               pass --no-reuse-delta to git-pack-objects
14 F               pass --no-reuse-object to git-pack-objects
15 n               do not run git-update-server-info
16 q,quiet         be quiet
17 l               pass --local to git-pack-objects
18 unpack-unreachable=  with -A, do not loosen objects older than this
19  Packing constraints
20 window=         size of the window used for delta compression
21 window-memory=  same as the above, but limit memory size instead of entries count
22 depth=          limits the maximum delta depth
23 max-pack-size=  maximum size of each packfile
24 "
25 SUBDIRECTORY_OK='Yes'
26 . git-sh-setup
27
28 no_update_info= all_into_one= remove_redundant= unpack_unreachable=
29 local= no_reuse= extra=
30 while test $# != 0
31 do
32         case "$1" in
33         -n)     no_update_info=t ;;
34         -a)     all_into_one=t ;;
35         -A)     all_into_one=t
36                 unpack_unreachable=--unpack-unreachable ;;
37         --unpack-unreachable)
38                 unpack_unreachable="--unpack-unreachable=$2"; shift ;;
39         -d)     remove_redundant=t ;;
40         -q)     GIT_QUIET=t ;;
41         -f)     no_reuse=--no-reuse-delta ;;
42         -F)     no_reuse=--no-reuse-object ;;
43         -l)     local=--local ;;
44         --max-pack-size|--window|--window-memory|--depth)
45                 extra="$extra $1=$2"; shift ;;
46         --) shift; break;;
47         *)      usage ;;
48         esac
49         shift
50 done
51
52 case "$(git config --bool repack.usedeltabaseoffset || echo true)" in
53 true)
54         extra="$extra --delta-base-offset" ;;
55 esac
56
57 PACKDIR="$GIT_OBJECT_DIRECTORY/pack"
58 PACKTMP="$PACKDIR/.tmp-$$-pack"
59 rm -f "$PACKTMP"-*
60 trap 'rm -f "$PACKTMP"-*' 0 1 2 3 15
61
62 # There will be more repacking strategies to come...
63 case ",$all_into_one," in
64 ,,)
65         args='--unpacked --incremental'
66         ;;
67 ,t,)
68         args= existing=
69         if [ -d "$PACKDIR" ]; then
70                 for e in `cd "$PACKDIR" && find . -type f -name '*.pack' \
71                         | sed -e 's/^\.\///' -e 's/\.pack$//'`
72                 do
73                         if [ -e "$PACKDIR/$e.keep" ]; then
74                                 : keep
75                         else
76                                 existing="$existing $e"
77                         fi
78                 done
79                 if test -n "$existing" && test -n "$unpack_unreachable" && \
80                         test -n "$remove_redundant"
81                 then
82                         # This may have arbitrary user arguments, so we
83                         # have to protect it against whitespace splitting
84                         # when it gets run as "pack-objects $args" later.
85                         # Fortunately, we know it's an approxidate, so we
86                         # can just use dots instead.
87                         args="$args $(echo "$unpack_unreachable" | tr ' ' .)"
88                 fi
89         fi
90         ;;
91 esac
92
93 mkdir -p "$PACKDIR" || exit
94
95 args="$args $local ${GIT_QUIET:+-q} $no_reuse$extra"
96 names=$(git pack-objects --keep-true-parents --honor-pack-keep --non-empty --all --reflog $args </dev/null "$PACKTMP") ||
97         exit 1
98 if [ -z "$names" ]; then
99         say Nothing new to pack.
100 fi
101
102 # Ok we have prepared all new packfiles.
103
104 # First see if there are packs of the same name and if so
105 # if we can move them out of the way (this can happen if we
106 # repacked immediately after packing fully.
107 rollback=
108 failed=
109 for name in $names
110 do
111         for sfx in pack idx
112         do
113                 file=pack-$name.$sfx
114                 test -f "$PACKDIR/$file" || continue
115                 rm -f "$PACKDIR/old-$file" &&
116                 mv "$PACKDIR/$file" "$PACKDIR/old-$file" || {
117                         failed=t
118                         break
119                 }
120                 rollback="$rollback $file"
121         done
122         test -z "$failed" || break
123 done
124
125 # If renaming failed for any of them, roll the ones we have
126 # already renamed back to their original names.
127 if test -n "$failed"
128 then
129         rollback_failure=
130         for file in $rollback
131         do
132                 mv "$PACKDIR/old-$file" "$PACKDIR/$file" ||
133                 rollback_failure="$rollback_failure $file"
134         done
135         if test -n "$rollback_failure"
136         then
137                 echo >&2 "WARNING: Some packs in use have been renamed by"
138                 echo >&2 "WARNING: prefixing old- to their name, in order to"
139                 echo >&2 "WARNING: replace them with the new version of the"
140                 echo >&2 "WARNING: file.  But the operation failed, and"
141                 echo >&2 "WARNING: attempt to rename them back to their"
142                 echo >&2 "WARNING: original names also failed."
143                 echo >&2 "WARNING: Please rename them in $PACKDIR manually:"
144                 for file in $rollback_failure
145                 do
146                         echo >&2 "WARNING:   old-$file -> $file"
147                 done
148         fi
149         exit 1
150 fi
151
152 # Now the ones with the same name are out of the way...
153 fullbases=
154 for name in $names
155 do
156         fullbases="$fullbases pack-$name"
157         chmod a-w "$PACKTMP-$name.pack"
158         chmod a-w "$PACKTMP-$name.idx"
159         mv -f "$PACKTMP-$name.pack" "$PACKDIR/pack-$name.pack" &&
160         mv -f "$PACKTMP-$name.idx"  "$PACKDIR/pack-$name.idx" ||
161         exit
162 done
163
164 # Remove the "old-" files
165 for name in $names
166 do
167         rm -f "$PACKDIR/old-pack-$name.idx"
168         rm -f "$PACKDIR/old-pack-$name.pack"
169 done
170
171 # End of pack replacement.
172
173 if test "$remove_redundant" = t
174 then
175         # We know $existing are all redundant.
176         if [ -n "$existing" ]
177         then
178                 ( cd "$PACKDIR" &&
179                   for e in $existing
180                   do
181                         case " $fullbases " in
182                         *" $e "*) ;;
183                         *)      rm -f "$e.pack" "$e.idx" "$e.keep" ;;
184                         esac
185                   done
186                 )
187         fi
188         git prune-packed ${GIT_QUIET:+-q}
189 fi
190
191 case "$no_update_info" in
192 t) : ;;
193 *) git update-server-info ;;
194 esac