Merge branch 'maint'
[git] / git-repack.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Linus Torvalds
4 #
5
6 USAGE='[-a] [-d] [-f] [-l] [-n] [-q] [--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_delta= 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_delta=--no-reuse-delta ;;
20         -l)     local=--local ;;
21         --window=*) extra="$extra $1" ;;
22         --depth=*) extra="$extra $1" ;;
23         *)      usage ;;
24         esac
25         shift
26 done
27
28 # Later we will default repack.UseDeltaBaseOffset to true
29 default_dbo=false
30
31 case "`git repo-config --bool repack.usedeltabaseoffset ||
32        echo $default_dbo`" in
33 true)
34         extra="$extra --delta-base-offset" ;;
35 esac
36
37 PACKDIR="$GIT_OBJECT_DIRECTORY/pack"
38 PACKTMP="$GIT_DIR/.tmp-$$-pack"
39 rm -f "$PACKTMP"-*
40 trap 'rm -f "$PACKTMP"-*' 0 1 2 3 15
41
42 # There will be more repacking strategies to come...
43 case ",$all_into_one," in
44 ,,)
45         args='--unpacked --incremental'
46         ;;
47 ,t,)
48         args=
49
50         # Redundancy check in all-into-one case is trivial.
51         existing=`test -d "$PACKDIR" && cd "$PACKDIR" && \
52             find . -type f \( -name '*.pack' -o -name '*.idx' \) -print`
53         ;;
54 esac
55
56 args="$args $local $quiet $no_reuse_delta$extra"
57 name=$(git-pack-objects --non-empty --all $args </dev/null "$PACKTMP") ||
58         exit 1
59 if [ -z "$name" ]; then
60         echo Nothing new to pack.
61 else
62         if test "$quiet" != '-q'; then
63             echo "Pack pack-$name created."
64         fi
65         mkdir -p "$PACKDIR" || exit
66
67         for sfx in pack idx
68         do
69                 if test -f "$PACKDIR/pack-$name.$sfx"
70                 then
71                         mv -f "$PACKDIR/pack-$name.$sfx" \
72                                 "$PACKDIR/old-pack-$name.$sfx"
73                 fi
74         done &&
75         mv -f "$PACKTMP-$name.pack" "$PACKDIR/pack-$name.pack" &&
76         mv -f "$PACKTMP-$name.idx"  "$PACKDIR/pack-$name.idx" &&
77         test -f "$PACKDIR/pack-$name.pack" &&
78         test -f "$PACKDIR/pack-$name.idx" || {
79                 echo >&2 "Couldn't replace the existing pack with updated one."
80                 echo >&2 "The original set of packs have been saved as"
81                 echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
82                 exit 1
83         }
84         rm -f "$PACKDIR/old-pack-$name.pack" "$PACKDIR/old-pack-$name.idx"
85 fi
86
87 if test "$remove_redundant" = t
88 then
89         # We know $existing are all redundant only when
90         # all-into-one is used.
91         if test "$all_into_one" != '' && test "$existing" != ''
92         then
93                 sync
94                 ( cd "$PACKDIR" &&
95                   for e in $existing
96                   do
97                         case "$e" in
98                         ./pack-$name.pack | ./pack-$name.idx) ;;
99                         *)      rm -f $e ;;
100                         esac
101                   done
102                 )
103         fi
104         git-prune-packed
105 fi
106
107 case "$no_update_info" in
108 t) : ;;
109 *) git-update-server-info ;;
110 esac