git-repack.shon commit Fix and improve t7004 (git-tag tests) (eb9d2b9)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Linus Torvalds
   4#
   5
   6USAGE='[-a|-A] [-d] [-f] [-l] [-n] [-q] [--max-pack-size=N] [--window=N] [--window-memory=N] [--depth=N]'
   7SUBDIRECTORY_OK='Yes'
   8. git-sh-setup
   9
  10no_update_info= all_into_one= remove_redundant= keep_unreachable=
  11local= quiet= no_reuse= extra=
  12while test $# != 0
  13do
  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
  30done
  31
  32# Later we will default repack.UseDeltaBaseOffset to true
  33default_dbo=false
  34
  35case "`git config --bool repack.usedeltabaseoffset ||
  36       echo $default_dbo`" in
  37true)
  38        extra="$extra --delta-base-offset" ;;
  39esac
  40
  41PACKDIR="$GIT_OBJECT_DIRECTORY/pack"
  42PACKTMP="$GIT_OBJECT_DIRECTORY/.tmp-$$-pack"
  43rm -f "$PACKTMP"-*
  44trap 'rm -f "$PACKTMP"-*' 0 1 2 3 15
  45
  46# There will be more repacking strategies to come...
  47case ",$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        ;;
  72esac
  73
  74args="$args $local $quiet $no_reuse$extra"
  75names=$(git pack-objects --non-empty --all --reflog $args </dev/null "$PACKTMP") ||
  76        exit 1
  77if [ -z "$names" ]; then
  78        if test -z "$quiet"; then
  79                echo Nothing new to pack.
  80        fi
  81fi
  82for 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
  87
  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"
 106done
 107
 108if test "$remove_redundant" = t
 109then
 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
 125fi
 126
 127case "$no_update_info" in
 128t) : ;;
 129*) git-update-server-info ;;
 130esac