| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git refs create' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | setup_repo () { |
| 8 | git init "$1" && |
| 9 | test_commit -C "$1" A && |
| 10 | test_commit -C "$1" B |
| 11 | } |
| 12 | |
| 13 | test_ref_matches () { |
| 14 | git rev-parse "$1" >expect && |
| 15 | echo "$2" >actual && |
| 16 | test_cmp expect actual |
| 17 | } |
| 18 | |
| 19 | test_expect_success 'create a new reference' ' |
| 20 | test_when_finished "rm -rf repo" && |
| 21 | setup_repo repo && |
| 22 | ( |
| 23 | cd repo && |
| 24 | A=$(git rev-parse A) && |
| 25 | git refs create refs/heads/foo $A && |
| 26 | test_ref_matches refs/heads/foo "$A" |
| 27 | ) |
| 28 | ' |
| 29 | |
| 30 | test_expect_success 'create fails when the reference already exists' ' |
| 31 | test_when_finished "rm -rf repo" && |
| 32 | setup_repo repo && |
| 33 | ( |
| 34 | cd repo && |
| 35 | A=$(git rev-parse A) && |
| 36 | B=$(git rev-parse B) && |
| 37 | git refs create refs/heads/foo $A && |
| 38 | test_must_fail git refs create refs/heads/foo $B 2>err && |
| 39 | test_grep "reference already exists" err && |
| 40 | test_ref_matches refs/heads/foo "$A" |
| 41 | ) |
| 42 | ' |
| 43 | |
| 44 | test_expect_success 'create with null new value fails' ' |
| 45 | test_when_finished "rm -rf repo" && |
| 46 | setup_repo repo && |
| 47 | ( |
| 48 | cd repo && |
| 49 | test_must_fail git refs create refs/heads/foo $ZERO_OID 2>err && |
| 50 | test_grep "null new object ID" err && |
| 51 | test_must_fail git refs exists refs/heads/foo |
| 52 | ) |
| 53 | ' |
| 54 | |
| 55 | test_expect_success 'create with invalid new value fails' ' |
| 56 | test_when_finished "rm -rf repo" && |
| 57 | setup_repo repo && |
| 58 | ( |
| 59 | cd repo && |
| 60 | test_must_fail git refs create refs/heads/foo invalid-oid 2>err && |
| 61 | test_grep "invalid object ID" err && |
| 62 | test_must_fail git refs exists refs/heads/foo |
| 63 | ) |
| 64 | ' |
| 65 | |
| 66 | test_expect_success 'create does not create a reflog by default' ' |
| 67 | test_when_finished "rm -rf repo" && |
| 68 | setup_repo repo && |
| 69 | ( |
| 70 | cd repo && |
| 71 | A=$(git rev-parse A) && |
| 72 | git refs create refs/foo $A && |
| 73 | test_must_fail git reflog exists refs/foo |
| 74 | ) |
| 75 | ' |
| 76 | |
| 77 | test_expect_success 'create creates a reflog with --create-reflog' ' |
| 78 | test_when_finished "rm -rf repo" && |
| 79 | setup_repo repo && |
| 80 | ( |
| 81 | cd repo && |
| 82 | A=$(git rev-parse A) && |
| 83 | git refs create --create-reflog refs/foo $A && |
| 84 | git reflog exists refs/foo |
| 85 | ) |
| 86 | ' |
| 87 | |
| 88 | test_expect_success 'create with message records reason in reflog' ' |
| 89 | test_when_finished "rm -rf repo" && |
| 90 | setup_repo repo && |
| 91 | ( |
| 92 | cd repo && |
| 93 | A=$(git rev-parse A) && |
| 94 | git refs create --message="create reason" refs/heads/foo $A && |
| 95 | git reflog show refs/heads/foo >actual && |
| 96 | test_grep "create reason$" actual |
| 97 | ) |
| 98 | ' |
| 99 | |
| 100 | test_expect_success 'create with symref target creates target reference' ' |
| 101 | test_when_finished "rm -rf repo" && |
| 102 | setup_repo repo && |
| 103 | ( |
| 104 | cd repo && |
| 105 | A=$(git rev-parse A) && |
| 106 | git symbolic-ref refs/heads/symref refs/heads/target && |
| 107 | git refs create refs/heads/symref $A && |
| 108 | git reflog exists refs/heads/target |
| 109 | ) |
| 110 | ' |
| 111 | |
| 112 | test_expect_success 'create with symref target and --no-deref refuses to create reference' ' |
| 113 | test_when_finished "rm -rf repo" && |
| 114 | setup_repo repo && |
| 115 | ( |
| 116 | cd repo && |
| 117 | A=$(git rev-parse A) && |
| 118 | git symbolic-ref refs/heads/symref refs/heads/target && |
| 119 | test_must_fail git refs create --no-deref refs/heads/symref $A 2>err && |
| 120 | test_grep "dangling symref already exists" err && |
| 121 | test_must_fail git reflog exists refs/heads/target |
| 122 | ) |
| 123 | ' |
| 124 | |
| 125 | test_expect_success 'create with empty message fails' ' |
| 126 | test_when_finished "rm -rf repo" && |
| 127 | setup_repo repo && |
| 128 | ( |
| 129 | cd repo && |
| 130 | A=$(git rev-parse A) && |
| 131 | test_must_fail git refs create --message= refs/heads/foo $A 2>err && |
| 132 | test_grep "empty message" err && |
| 133 | test_must_fail git refs exists refs/heads/foo |
| 134 | ) |
| 135 | ' |
| 136 | |
| 137 | test_expect_success 'create without arguments fails' ' |
| 138 | test_when_finished "rm -rf repo" && |
| 139 | setup_repo repo && |
| 140 | test_must_fail git -C repo refs create 2>err && |
| 141 | test_grep "requires reference name" err |
| 142 | ' |
| 143 | |
| 144 | test_expect_success 'create with too many arguments fails' ' |
| 145 | test_when_finished "rm -rf repo" && |
| 146 | setup_repo repo && |
| 147 | test_must_fail git -C repo refs create refs/heads/foo a b 2>err && |
| 148 | test_grep "requires reference name" err |
| 149 | ' |
| 150 | |
| 151 | test_done |