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