| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Manually write reflog entries' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | SIGNATURE="C O Mitter <committer@example.com> 1112911993 -0700" |
| 8 | |
| 9 | test_reflog_matches () { |
| 10 | repo="$1" && |
| 11 | refname="$2" && |
| 12 | cat >actual && |
| 13 | test-tool -C "$repo" ref-store main for-each-reflog-ent "$refname" >expected && |
| 14 | test_cmp expected actual |
| 15 | } |
| 16 | |
| 17 | test_expect_success 'invalid number of arguments' ' |
| 18 | test_when_finished "rm -rf repo" && |
| 19 | git init repo && |
| 20 | ( |
| 21 | cd repo && |
| 22 | for args in "" "1" "1 2" "1 2 3" "1 2 3 4 5" |
| 23 | do |
| 24 | test_must_fail git reflog write $args 2>err && |
| 25 | test_grep "usage: git reflog write" err || return 1 |
| 26 | done |
| 27 | ) |
| 28 | ' |
| 29 | |
| 30 | test_expect_success 'invalid refname' ' |
| 31 | test_when_finished "rm -rf repo" && |
| 32 | git init repo && |
| 33 | ( |
| 34 | cd repo && |
| 35 | test_must_fail git reflog write "refs/heads/ invalid" $ZERO_OID $ZERO_OID first 2>err && |
| 36 | test_grep "invalid reference name: " err |
| 37 | ) |
| 38 | ' |
| 39 | |
| 40 | test_expect_success 'unqualified refname is rejected' ' |
| 41 | test_when_finished "rm -rf repo" && |
| 42 | git init repo && |
| 43 | ( |
| 44 | cd repo && |
| 45 | test_must_fail git reflog write unqualified $ZERO_OID $ZERO_OID first 2>err && |
| 46 | test_grep "invalid reference name: " err |
| 47 | ) |
| 48 | ' |
| 49 | |
| 50 | test_expect_success 'nonexistent object IDs' ' |
| 51 | test_when_finished "rm -rf repo" && |
| 52 | git init repo && |
| 53 | ( |
| 54 | cd repo && |
| 55 | test_must_fail git reflog write refs/heads/something $(test_oid deadbeef) $ZERO_OID old-object-id 2>err && |
| 56 | test_grep "old object .* does not exist" err && |
| 57 | test_must_fail git reflog write refs/heads/something $ZERO_OID $(test_oid deadbeef) new-object-id 2>err && |
| 58 | test_grep "new object .* does not exist" err |
| 59 | ) |
| 60 | ' |
| 61 | |
| 62 | test_expect_success 'abbreviated object IDs' ' |
| 63 | test_when_finished "rm -rf repo" && |
| 64 | git init repo && |
| 65 | ( |
| 66 | cd repo && |
| 67 | test_commit initial && |
| 68 | abbreviated_oid=$(git rev-parse HEAD | test_copy_bytes 8) && |
| 69 | test_must_fail git reflog write refs/heads/something $abbreviated_oid $ZERO_OID old-object-id 2>err && |
| 70 | test_grep "invalid old object ID" err && |
| 71 | test_must_fail git reflog write refs/heads/something $ZERO_OID $abbreviated_oid new-object-id 2>err && |
| 72 | test_grep "invalid new object ID" err |
| 73 | ) |
| 74 | ' |
| 75 | |
| 76 | test_expect_success 'reflog message gets normalized' ' |
| 77 | test_when_finished "rm -rf repo" && |
| 78 | git init repo && |
| 79 | ( |
| 80 | cd repo && |
| 81 | test_commit initial && |
| 82 | COMMIT_OID=$(git rev-parse HEAD) && |
| 83 | git reflog write HEAD $COMMIT_OID $COMMIT_OID "$(printf "message\nwith\nnewlines")" && |
| 84 | git reflog show -1 --format=%gs HEAD >actual && |
| 85 | echo "message with newlines" >expected && |
| 86 | test_cmp expected actual |
| 87 | ) |
| 88 | ' |
| 89 | |
| 90 | test_expect_success 'simple writes' ' |
| 91 | test_when_finished "rm -rf repo" && |
| 92 | git init repo && |
| 93 | ( |
| 94 | cd repo && |
| 95 | test_commit initial && |
| 96 | COMMIT_OID=$(git rev-parse HEAD) && |
| 97 | |
| 98 | git reflog write refs/heads/something $ZERO_OID $COMMIT_OID first && |
| 99 | test_reflog_matches . refs/heads/something <<-EOF && |
| 100 | $ZERO_OID $COMMIT_OID $SIGNATURE first |
| 101 | EOF |
| 102 | |
| 103 | git reflog write refs/heads/something $COMMIT_OID $COMMIT_OID second && |
| 104 | test_reflog_matches . refs/heads/something <<-EOF |
| 105 | $ZERO_OID $COMMIT_OID $SIGNATURE first |
| 106 | $COMMIT_OID $COMMIT_OID $SIGNATURE second |
| 107 | EOF |
| 108 | ) |
| 109 | ' |
| 110 | |
| 111 | test_expect_success 'uses user.name and user.email config' ' |
| 112 | test_when_finished "rm -rf repo" && |
| 113 | git init repo && |
| 114 | ( |
| 115 | cd repo && |
| 116 | test_commit initial && |
| 117 | COMMIT_OID=$(git rev-parse HEAD) && |
| 118 | |
| 119 | sane_unset GIT_COMMITTER_NAME && |
| 120 | sane_unset GIT_COMMITTER_EMAIL && |
| 121 | git config --local user.name "Author" && |
| 122 | git config --local user.email "a@uth.or" && |
| 123 | git reflog write refs/heads/something $ZERO_OID $COMMIT_OID first && |
| 124 | test_reflog_matches . refs/heads/something <<-EOF |
| 125 | $ZERO_OID $COMMIT_OID Author <a@uth.or> $GIT_COMMITTER_DATE first |
| 126 | EOF |
| 127 | ) |
| 128 | ' |
| 129 | |
| 130 | test_expect_success 'environment variables take precedence over config' ' |
| 131 | test_when_finished "rm -rf repo" && |
| 132 | git init repo && |
| 133 | ( |
| 134 | cd repo && |
| 135 | test_commit initial && |
| 136 | COMMIT_OID=$(git rev-parse HEAD) && |
| 137 | |
| 138 | git config --local user.name "Author" && |
| 139 | git config --local user.email "a@uth.or" && |
| 140 | git reflog write refs/heads/something $ZERO_OID $COMMIT_OID first && |
| 141 | test_reflog_matches . refs/heads/something <<-EOF |
| 142 | $ZERO_OID $COMMIT_OID $SIGNATURE first |
| 143 | EOF |
| 144 | ) |
| 145 | ' |
| 146 | |
| 147 | test_expect_success 'can write to root ref' ' |
| 148 | test_when_finished "rm -rf repo" && |
| 149 | git init repo && |
| 150 | ( |
| 151 | cd repo && |
| 152 | test_commit initial && |
| 153 | COMMIT_OID=$(git rev-parse HEAD) && |
| 154 | |
| 155 | git reflog write ROOT_REF_HEAD $ZERO_OID $COMMIT_OID first && |
| 156 | test_reflog_matches . ROOT_REF_HEAD <<-EOF |
| 157 | $ZERO_OID $COMMIT_OID $SIGNATURE first |
| 158 | EOF |
| 159 | ) |
| 160 | ' |
| 161 | |
| 162 | test_done |