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