1#!/bin/sh
   2#
   3# Copyright (c) 2008 Johannes E. Schindelin
   4#
   5test_description='prune'
   7. ./test-lib.sh
   8day=$((60*60*24))
  10week=$(($day*7))
  11add_blob() {
  13        before=$(git count-objects | sed "s/ .*//") &&
  14        BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
  15        BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
  16        test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
  17        test -f $BLOB_FILE
  18}
  19test_expect_success setup '
  21        : > file &&
  23        git add file &&
  24        test_tick &&
  25        git commit -m initial &&
  26        git gc
  27'
  29test_expect_success 'prune stale packs' '
  31        orig_pack=$(echo .git/objects/pack/*.pack) &&
  33        : > .git/objects/tmp_1.pack &&
  34        : > .git/objects/tmp_2.pack &&
  35        test-chmtime =-86501 .git/objects/tmp_1.pack &&
  36        git prune --expire 1.day &&
  37        test -f $orig_pack &&
  38        test -f .git/objects/tmp_2.pack &&
  39        ! test -f .git/objects/tmp_1.pack
  40'
  42test_expect_success 'prune --expire' '
  44        add_blob &&
  46        git prune --expire=1.hour.ago &&
  47        test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
  48        test -f $BLOB_FILE &&
  49        test-chmtime =-86500 $BLOB_FILE &&
  50        git prune --expire 1.day &&
  51        test $before = $(git count-objects | sed "s/ .*//") &&
  52        ! test -f $BLOB_FILE
  53'
  55test_expect_success 'gc: implicit prune --expire' '
  57        add_blob &&
  59        test-chmtime =-$((2*$week-30)) $BLOB_FILE &&
  60        git gc &&
  61        test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
  62        test -f $BLOB_FILE &&
  63        test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
  64        git gc &&
  65        test $before = $(git count-objects | sed "s/ .*//") &&
  66        ! test -f $BLOB_FILE
  67'
  69test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
  71        git config gc.pruneExpire invalid &&
  73        test_must_fail git gc
  74'
  76test_expect_success 'gc: start with ok gc.pruneExpire' '
  78        git config gc.pruneExpire 2.days.ago &&
  80        git gc
  81'
  83test_expect_success 'prune: prune nonsense parameters' '
  85        test_must_fail git prune garbage &&
  87        test_must_fail git prune --- &&
  88        test_must_fail git prune --no-such-option
  89'
  91test_expect_success 'prune: prune unreachable heads' '
  93        git config core.logAllRefUpdates false &&
  95        mv .git/logs .git/logs.old &&
  96        : > file2 &&
  97        git add file2 &&
  98        git commit -m temporary &&
  99        tmp_head=$(git rev-list -1 HEAD) &&
 100        git reset HEAD^ &&
 101        git prune &&
 102        test_must_fail git reset $tmp_head --
 103'
 105test_expect_success 'prune: do not prune heads listed as an argument' '
 107        : > file2 &&
 109        git add file2 &&
 110        git commit -m temporary &&
 111        tmp_head=$(git rev-list -1 HEAD) &&
 112        git reset HEAD^ &&
 113        git prune -- $tmp_head &&
 114        git reset $tmp_head --
 115'
 117test_expect_success 'gc --no-prune' '
 119        add_blob &&
 121        test-chmtime =-$((5001*$day)) $BLOB_FILE &&
 122        git config gc.pruneExpire 2.days.ago &&
 123        git gc --no-prune &&
 124        test 1 = $(git count-objects | sed "s/ .*//") &&
 125        test -f $BLOB_FILE
 126'
 128test_expect_success 'gc respects gc.pruneExpire' '
 130        git config gc.pruneExpire 5002.days.ago &&
 132        git gc &&
 133        test -f $BLOB_FILE &&
 134        git config gc.pruneExpire 5000.days.ago &&
 135        git gc &&
 136        test ! -f $BLOB_FILE
 137'
 139test_expect_success 'gc --prune=<date>' '
 141        add_blob &&
 143        test-chmtime =-$((5001*$day)) $BLOB_FILE &&
 144        git gc --prune=5002.days.ago &&
 145        test -f $BLOB_FILE &&
 146        git gc --prune=5000.days.ago &&
 147        test ! -f $BLOB_FILE
 148'
 150test_expect_success 'gc: prune old objects after local clone' '
 152        add_blob &&
 153        test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
 154        git clone --no-hardlinks . aclone &&
 155        (
 156                cd aclone &&
 157                test 1 = $(git count-objects | sed "s/ .*//") &&
 158                test -f $BLOB_FILE &&
 159                git gc --prune &&
 160                test 0 = $(git count-objects | sed "s/ .*//") &&
 161                ! test -f $BLOB_FILE
 162        )
 163'
 164test_done