t / t1503-rev-parse-verify.shon commit difftool --no-index: error out on --dir-diff (and don't crash) (f3a3a02)
   1#!/bin/sh
   2#
   3# Copyright (c) 2008 Christian Couder
   4#
   5test_description='test git rev-parse --verify'
   6
   7exec </dev/null
   8
   9. ./test-lib.sh
  10
  11add_line_into_file()
  12{
  13    _line=$1
  14    _file=$2
  15
  16    if [ -f "$_file" ]; then
  17        echo "$_line" >> $_file || return $?
  18        MSG="Add <$_line> into <$_file>."
  19    else
  20        echo "$_line" > $_file || return $?
  21        git add $_file || return $?
  22        MSG="Create file <$_file> with <$_line> inside."
  23    fi
  24
  25    test_tick
  26    git commit --quiet -m "$MSG" $_file
  27}
  28
  29HASH1=
  30HASH2=
  31HASH3=
  32HASH4=
  33
  34test_expect_success 'set up basic repo with 1 file (hello) and 4 commits' '
  35        add_line_into_file "1: Hello World" hello &&
  36        HASH1=$(git rev-parse --verify HEAD) &&
  37        add_line_into_file "2: A new day for git" hello &&
  38        HASH2=$(git rev-parse --verify HEAD) &&
  39        add_line_into_file "3: Another new day for git" hello &&
  40        HASH3=$(git rev-parse --verify HEAD) &&
  41        add_line_into_file "4: Ciao for now" hello &&
  42        HASH4=$(git rev-parse --verify HEAD)
  43'
  44
  45test_expect_success 'works with one good rev' '
  46        rev_hash1=$(git rev-parse --verify $HASH1) &&
  47        test "$rev_hash1" = "$HASH1" &&
  48        rev_hash2=$(git rev-parse --verify $HASH2) &&
  49        test "$rev_hash2" = "$HASH2" &&
  50        rev_hash3=$(git rev-parse --verify $HASH3) &&
  51        test "$rev_hash3" = "$HASH3" &&
  52        rev_hash4=$(git rev-parse --verify $HASH4) &&
  53        test "$rev_hash4" = "$HASH4" &&
  54        rev_master=$(git rev-parse --verify master) &&
  55        test "$rev_master" = "$HASH4" &&
  56        rev_head=$(git rev-parse --verify HEAD) &&
  57        test "$rev_head" = "$HASH4"
  58'
  59
  60test_expect_success 'fails with any bad rev or many good revs' '
  61        test_must_fail git rev-parse --verify 2>error &&
  62        grep "single revision" error &&
  63        test_must_fail git rev-parse --verify foo 2>error &&
  64        grep "single revision" error &&
  65        test_must_fail git rev-parse --verify HEAD bar 2>error &&
  66        grep "single revision" error &&
  67        test_must_fail git rev-parse --verify baz HEAD 2>error &&
  68        grep "single revision" error &&
  69        test_must_fail git rev-parse --verify $HASH2 HEAD 2>error &&
  70        grep "single revision" error
  71'
  72
  73test_expect_success 'fails silently when using -q' '
  74        test_must_fail git rev-parse --verify --quiet 2>error &&
  75        test_must_be_empty error &&
  76        test_must_fail git rev-parse -q --verify foo 2>error &&
  77        test_must_be_empty error &&
  78        test_must_fail git rev-parse --verify -q HEAD bar 2>error &&
  79        test_must_be_empty error &&
  80        test_must_fail git rev-parse --quiet --verify baz HEAD 2>error &&
  81        test_must_be_empty error &&
  82        test_must_fail git rev-parse -q --verify $HASH2 HEAD 2>error &&
  83        test_must_be_empty error
  84'
  85
  86test_expect_success 'fails silently when using -q with deleted reflogs' '
  87        ref=$(git rev-parse HEAD) &&
  88        git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
  89        git reflog delete --updateref --rewrite refs/test@{0} &&
  90        test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 &&
  91        test_must_be_empty error
  92'
  93
  94test_expect_success 'fails silently when using -q with not enough reflogs' '
  95        ref=$(git rev-parse HEAD) &&
  96        git update-ref --create-reflog -m "message for refs/test2" refs/test2 "$ref" &&
  97        test_must_fail git rev-parse -q --verify refs/test2@{999} >error 2>&1 &&
  98        test_must_be_empty error
  99'
 100
 101test_expect_success 'succeeds silently with -q and reflogs that do not go far back enough in time' '
 102        ref=$(git rev-parse HEAD) &&
 103        git update-ref --create-reflog -m "message for refs/test3" refs/test3 "$ref" &&
 104        git rev-parse -q --verify refs/test3@{1.year.ago} >actual 2>error &&
 105        test_must_be_empty error &&
 106        echo "$ref" >expect &&
 107        test_cmp expect actual
 108'
 109
 110test_expect_success 'no stdout output on error' '
 111        test -z "$(git rev-parse --verify)" &&
 112        test -z "$(git rev-parse --verify foo)" &&
 113        test -z "$(git rev-parse --verify baz HEAD)" &&
 114        test -z "$(git rev-parse --verify HEAD bar)" &&
 115        test -z "$(git rev-parse --verify $HASH2 HEAD)"
 116'
 117
 118test_expect_success 'use --default' '
 119        git rev-parse --verify --default master &&
 120        git rev-parse --verify --default master HEAD &&
 121        git rev-parse --default master --verify &&
 122        git rev-parse --default master --verify HEAD &&
 123        git rev-parse --verify HEAD --default master &&
 124        test_must_fail git rev-parse --verify foo --default master &&
 125        test_must_fail git rev-parse --default HEAD --verify bar &&
 126        test_must_fail git rev-parse --verify --default HEAD baz &&
 127        test_must_fail git rev-parse --default foo --verify &&
 128        test_must_fail git rev-parse --verify --default bar
 129'
 130
 131test_expect_success 'master@{n} for various n' '
 132        N=$(git reflog | wc -l) &&
 133        Nm1=$(($N-1)) &&
 134        Np1=$(($N+1)) &&
 135        git rev-parse --verify master@{0} &&
 136        git rev-parse --verify master@{1} &&
 137        git rev-parse --verify master@{$Nm1} &&
 138        test_must_fail git rev-parse --verify master@{$N} &&
 139        test_must_fail git rev-parse --verify master@{$Np1}
 140'
 141
 142test_expect_success SYMLINKS 'ref resolution not confused by broken symlinks' '
 143        ln -s does-not-exist .git/refs/heads/broken &&
 144        test_must_fail git rev-parse --verify broken
 145'
 146
 147test_done