Code

nicer display of thin pack completion
[git.git] / git-repack.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Linus Torvalds
4 #
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
13 do
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'
50         ;;
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$//'`
55                 do
56                         if [ -e "$PACKDIR/$e.keep" ]; then
57                                 : keep
58                         else
59                                 args="$args --unpacked=$e.pack"
60                                 existing="$existing $e"
61                         fi
62                 done
63         fi
64         if test -z "$args"
65         then
66                 args='--unpacked --incremental'
67         elif test -n "$keep_unreachable"
68         then
69                 args="$args $keep_unreachable"
70         fi
71         ;;
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.
80         fi
81 fi
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         mkdir -p "$PACKDIR" || exit
88         for sfx in pack idx
89         do
90                 if test -f "$PACKDIR/pack-$name.$sfx"
91                 then
92                         mv -f "$PACKDIR/pack-$name.$sfx" \
93                                 "$PACKDIR/old-pack-$name.$sfx"
94                 fi
95         done &&
96         mv -f "$PACKTMP-$name.pack" "$PACKDIR/pack-$name.pack" &&
97         mv -f "$PACKTMP-$name.idx"  "$PACKDIR/pack-$name.idx" &&
98         test -f "$PACKDIR/pack-$name.pack" &&
99         test -f "$PACKDIR/pack-$name.idx" || {
100                 echo >&2 "Couldn't replace the existing pack with updated one."
101                 echo >&2 "The original set of packs have been saved as"
102                 echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
103                 exit 1
104         }
105         rm -f "$PACKDIR/old-pack-$name.pack" "$PACKDIR/old-pack-$name.idx"
106 done
108 if test "$remove_redundant" = t
109 then
110         # We know $existing are all redundant.
111         if [ -n "$existing" ]
112         then
113                 sync
114                 ( cd "$PACKDIR" &&
115                   for e in $existing
116                   do
117                         case " $fullbases " in
118                         *" $e "*) ;;
119                         *)      rm -f "$e.pack" "$e.idx" "$e.keep" ;;
120                         esac
121                   done
122                 )
123         fi
124         git prune-packed $quiet
125 fi
127 case "$no_update_info" in
128 t) : ;;
129 *) git-update-server-info ;;
130 esac