t / t6500-gc.shon commit gc: add --keep-largest-pack option (ae4e89e)
   1#!/bin/sh
   2
   3test_description='basic git gc tests
   4'
   5
   6. ./test-lib.sh
   7
   8test_expect_success 'gc empty repository' '
   9        git gc
  10'
  11
  12test_expect_success 'gc does not leave behind pid file' '
  13        git gc &&
  14        test_path_is_missing .git/gc.pid
  15'
  16
  17test_expect_success 'gc --gobbledegook' '
  18        test_expect_code 129 git gc --nonsense 2>err &&
  19        test_i18ngrep "[Uu]sage: git gc" err
  20'
  21
  22test_expect_success 'gc -h with invalid configuration' '
  23        mkdir broken &&
  24        (
  25                cd broken &&
  26                git init &&
  27                echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
  28                test_expect_code 129 git gc -h >usage 2>&1
  29        ) &&
  30        test_i18ngrep "[Uu]sage" broken/usage
  31'
  32
  33test_expect_success 'gc is not aborted due to a stale symref' '
  34        git init remote &&
  35        (
  36                cd remote &&
  37                test_commit initial &&
  38                git clone . ../client &&
  39                git branch -m develop &&
  40                cd ../client &&
  41                git fetch --prune &&
  42                git gc
  43        )
  44'
  45
  46test_expect_success 'gc --keep-largest-pack' '
  47        test_create_repo keep-pack &&
  48        (
  49                cd keep-pack &&
  50                test_commit one &&
  51                test_commit two &&
  52                test_commit three &&
  53                git gc &&
  54                ( cd .git/objects/pack && ls *.pack ) >pack-list &&
  55                test_line_count = 1 pack-list &&
  56                BASE_PACK=.git/objects/pack/pack-*.pack &&
  57                test_commit four &&
  58                git repack -d &&
  59                test_commit five &&
  60                git repack -d &&
  61                ( cd .git/objects/pack && ls *.pack ) >pack-list &&
  62                test_line_count = 3 pack-list &&
  63                git gc --keep-largest-pack &&
  64                ( cd .git/objects/pack && ls *.pack ) >pack-list &&
  65                test_line_count = 2 pack-list &&
  66                test_path_is_file $BASE_PACK &&
  67                git fsck
  68        )
  69'
  70
  71test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' '
  72        test_config gc.auto 3 &&
  73        test_config gc.autodetach false &&
  74        test_config pack.writebitmaps true &&
  75        # We need to create two object whose sha1s start with 17
  76        # since this is what git gc counts.  As it happens, these
  77        # two blobs will do so.
  78        test_commit 263 &&
  79        test_commit 410 &&
  80        # Our first gc will create a pack; our second will create a second pack
  81        git gc --auto &&
  82        ls .git/objects/pack | sort >existing_packs &&
  83        test_commit 523 &&
  84        test_commit 790 &&
  85
  86        git gc --auto 2>err &&
  87        test_i18ngrep ! "^warning:" err &&
  88        ls .git/objects/pack/ | sort >post_packs &&
  89        comm -1 -3 existing_packs post_packs >new &&
  90        comm -2 -3 existing_packs post_packs >del &&
  91        test_line_count = 0 del && # No packs are deleted
  92        test_line_count = 2 new # There is one new pack and its .idx
  93'
  94
  95run_and_wait_for_auto_gc () {
  96        # We read stdout from gc for the side effect of waiting until the
  97        # background gc process exits, closing its fd 9.  Furthermore, the
  98        # variable assignment from a command substitution preserves the
  99        # exit status of the main gc process.
 100        # Note: this fd trickery doesn't work on Windows, but there is no
 101        # need to, because on Win the auto gc always runs in the foreground.
 102        doesnt_matter=$(git gc --auto 9>&1)
 103}
 104
 105test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
 106        test_commit foo &&
 107        test_commit bar &&
 108        git repack &&
 109        test_config gc.autopacklimit 1 &&
 110        test_config gc.autodetach true &&
 111        echo fleem >.git/gc.log &&
 112        test_must_fail git gc --auto 2>err &&
 113        test_i18ngrep "^error:" err &&
 114        test_config gc.logexpiry 5.days &&
 115        test-tool chmtime =-345600 .git/gc.log &&
 116        test_must_fail git gc --auto &&
 117        test_config gc.logexpiry 2.days &&
 118        run_and_wait_for_auto_gc &&
 119        ls .git/objects/pack/pack-*.pack >packs &&
 120        test_line_count = 1 packs
 121'
 122
 123test_expect_success 'background auto gc respects lock for all operations' '
 124        # make sure we run a background auto-gc
 125        test_commit make-pack &&
 126        git repack &&
 127        test_config gc.autopacklimit 1 &&
 128        test_config gc.autodetach true &&
 129
 130        # create a ref whose loose presence we can use to detect a pack-refs run
 131        git update-ref refs/heads/should-be-loose HEAD &&
 132        test_path_is_file .git/refs/heads/should-be-loose &&
 133
 134        # now fake a concurrent gc that holds the lock; we can use our
 135        # shell pid so that it looks valid.
 136        hostname=$(hostname || echo unknown) &&
 137        printf "$$ %s" "$hostname" >.git/gc.pid &&
 138
 139        # our gc should exit zero without doing anything
 140        run_and_wait_for_auto_gc &&
 141        test_path_is_file .git/refs/heads/should-be-loose
 142'
 143
 144# DO NOT leave a detached auto gc process running near the end of the
 145# test script: it can run long enough in the background to racily
 146# interfere with the cleanup in 'test_done'.
 147
 148test_done