Skip to content

Commit a366bde

Browse files
KarthikNayakpeff
authored andcommitted
update-ref: utilize rejected error details if available
When git-update-ref(1) received the '--update-ref' flag, the error details generated in the refs namespace wasn't propagated with failed updates. Instead only an error code pertaining to the type of rejection was noted. This missed detailed error message which the user can act upon. The previous commits added the required code to propagate these detailed error messages from the refs namespace. Now that additional details are available, let's output this additional details to stderr. This allows users to have additional information over the already present machine parsable output. While we're here, improve the existing tests for the machine parsable output by checking for the entire output string and not just the rejection reason. Reported-by: Elijah Newren <newren@gmail.com> Co-authored-by: Jeff King <peff@peff.net> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent be54b10 commit a366bde

2 files changed

Lines changed: 47 additions & 32 deletions

File tree

builtin/update-ref.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,16 +573,18 @@ static void print_rejected_refs(const char *refname,
573573
const char *old_target,
574574
const char *new_target,
575575
enum ref_transaction_error err,
576-
const char *details UNUSED,
576+
const char *details,
577577
void *cb_data UNUSED)
578578
{
579579
struct strbuf sb = STRBUF_INIT;
580-
const char *reason = ref_transaction_error_msg(err);
580+
581+
if (details && *details)
582+
error("%s", details);
581583

582584
strbuf_addf(&sb, "rejected %s %s %s %s\n", refname,
583585
new_oid ? oid_to_hex(new_oid) : new_target,
584586
old_oid ? oid_to_hex(old_oid) : old_target,
585-
reason);
587+
ref_transaction_error_msg(err));
586588

587589
fwrite(sb.buf, sb.len, 1, stdout);
588590
strbuf_release(&sb);

t/t1400-update-ref.sh

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,14 +2093,15 @@ do
20932093
20942094
format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
20952095
format_command $type "update refs/heads/ref2" "$(test_oid 001)" "$head" >>stdin &&
2096-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2096+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
20972097
echo $old_head >expect &&
20982098
git rev-parse refs/heads/ref1 >actual &&
20992099
test_cmp expect actual &&
21002100
echo $head >expect &&
21012101
git rev-parse refs/heads/ref2 >actual &&
21022102
test_cmp expect actual &&
2103-
test_grep -q "invalid new value provided" stdout
2103+
test_grep "rejected refs/heads/ref2 $(test_oid 001) $head invalid new value provided" stdout &&
2104+
test_grep "trying to write ref ${SQ}refs/heads/ref2${SQ} with nonexistent object" err
21042105
)
21052106
'
21062107

@@ -2119,14 +2120,15 @@ do
21192120
21202121
format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
21212122
format_command $type "update refs/heads/ref2" "$head_tree" "$head" >>stdin &&
2122-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2123+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
21232124
echo $old_head >expect &&
21242125
git rev-parse refs/heads/ref1 >actual &&
21252126
test_cmp expect actual &&
21262127
echo $head >expect &&
21272128
git rev-parse refs/heads/ref2 >actual &&
21282129
test_cmp expect actual &&
2129-
test_grep -q "invalid new value provided" stdout
2130+
test_grep "rejected refs/heads/ref2 $head_tree $head invalid new value provided" stdout &&
2131+
test_grep "trying to write non-commit object $head_tree to branch ${SQ}refs/heads/ref2${SQ}" err
21302132
)
21312133
'
21322134

@@ -2143,12 +2145,13 @@ do
21432145
21442146
format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
21452147
format_command $type "update refs/heads/ref2" "$old_head" "$head" >>stdin &&
2146-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2148+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
21472149
echo $old_head >expect &&
21482150
git rev-parse refs/heads/ref1 >actual &&
21492151
test_cmp expect actual &&
21502152
test_must_fail git rev-parse refs/heads/ref2 &&
2151-
test_grep -q "reference does not exist" stdout
2153+
test_grep "rejected refs/heads/ref2 $old_head $head reference does not exist" stdout &&
2154+
test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: unable to resolve reference ${SQ}refs/heads/ref2${SQ}" err
21522155
)
21532156
'
21542157

@@ -2166,13 +2169,14 @@ do
21662169
21672170
format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
21682171
format_command $type "update refs/heads/ref2" "$old_head" "$head" >>stdin &&
2169-
git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&
2172+
git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout 2>err &&
21702173
echo $old_head >expect &&
21712174
git rev-parse refs/heads/ref1 >actual &&
21722175
test_cmp expect actual &&
21732176
echo $head >expect &&
21742177
test_must_fail git rev-parse refs/heads/ref2 &&
2175-
test_grep -q "reference does not exist" stdout
2178+
test_grep "rejected refs/heads/ref2 $old_head $head reference does not exist" stdout &&
2179+
test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: reference is missing but expected $head" err
21762180
)
21772181
'
21782182

@@ -2190,15 +2194,16 @@ do
21902194
21912195
format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
21922196
format_command $type "symref-update refs/heads/ref2" "$old_head" "ref" "refs/heads/nonexistent" >>stdin &&
2193-
git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&
2197+
git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout 2>err &&
21942198
echo $old_head >expect &&
21952199
git rev-parse refs/heads/ref1 >actual &&
21962200
test_cmp expect actual &&
21972201
echo $head >expect &&
21982202
echo $head >expect &&
21992203
git rev-parse refs/heads/ref2 >actual &&
22002204
test_cmp expect actual &&
2201-
test_grep -q "expected symref but found regular ref" stdout
2205+
test_grep "rejected refs/heads/ref2 $ZERO_OID $ZERO_OID expected symref but found regular ref" stdout &&
2206+
test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: expected symref with target ${SQ}refs/heads/nonexistent${SQ}: but is a regular ref" err
22022207
)
22032208
'
22042209

@@ -2216,14 +2221,15 @@ do
22162221
22172222
format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
22182223
format_command $type "update refs/heads/ref2" "$old_head" "$Z" >>stdin &&
2219-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2224+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
22202225
echo $old_head >expect &&
22212226
git rev-parse refs/heads/ref1 >actual &&
22222227
test_cmp expect actual &&
22232228
echo $head >expect &&
22242229
git rev-parse refs/heads/ref2 >actual &&
22252230
test_cmp expect actual &&
2226-
test_grep -q "reference already exists" stdout
2231+
test_grep "rejected refs/heads/ref2 $old_head $ZERO_OID reference already exists" stdout &&
2232+
test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: reference already exists" err
22272233
)
22282234
'
22292235

@@ -2241,14 +2247,15 @@ do
22412247
22422248
format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
22432249
format_command $type "update refs/heads/ref2" "$head" "$old_head" >>stdin &&
2244-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2250+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
22452251
echo $old_head >expect &&
22462252
git rev-parse refs/heads/ref1 >actual &&
22472253
test_cmp expect actual &&
22482254
echo $head >expect &&
22492255
git rev-parse refs/heads/ref2 >actual &&
22502256
test_cmp expect actual &&
2251-
test_grep -q "incorrect old value provided" stdout
2257+
test_grep "rejected refs/heads/ref2 $head $old_head incorrect old value provided" stdout &&
2258+
test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: is at $head but expected $old_head" err
22522259
)
22532260
'
22542261

@@ -2264,12 +2271,13 @@ do
22642271
git update-ref refs/heads/ref/foo $head &&
22652272
22662273
format_command $type "update refs/heads/ref/foo" "$old_head" "$head" >stdin &&
2267-
format_command $type "update refs/heads/ref" "$old_head" "" >>stdin &&
2268-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2274+
format_command $type "update refs/heads/ref" "$old_head" "$ZERO_OID" >>stdin &&
2275+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
22692276
echo $old_head >expect &&
22702277
git rev-parse refs/heads/ref/foo >actual &&
22712278
test_cmp expect actual &&
2272-
test_grep -q "refname conflict" stdout
2279+
test_grep "rejected refs/heads/ref $old_head $ZERO_OID refname conflict" stdout &&
2280+
test_grep "${SQ}refs/heads/ref/foo${SQ} exists; cannot create ${SQ}refs/heads/ref${SQ}" err
22732281
)
22742282
'
22752283

@@ -2284,13 +2292,14 @@ do
22842292
head=$(git rev-parse HEAD) &&
22852293
git update-ref refs/heads/ref/foo $head &&
22862294
2287-
format_command $type "update refs/heads/foo" "$old_head" "" >stdin &&
2288-
format_command $type "update refs/heads/ref" "$old_head" "" >>stdin &&
2289-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2295+
format_command $type "update refs/heads/foo" "$old_head" "$ZERO_OID" >stdin &&
2296+
format_command $type "update refs/heads/ref" "$old_head" "$ZERO_OID" >>stdin &&
2297+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
22902298
echo $old_head >expect &&
22912299
git rev-parse refs/heads/foo >actual &&
22922300
test_cmp expect actual &&
2293-
test_grep -q "refname conflict" stdout
2301+
test_grep "rejected refs/heads/ref $old_head $ZERO_OID refname conflict" stdout &&
2302+
test_grep "${SQ}refs/heads/ref/foo${SQ} exists; cannot create ${SQ}refs/heads/ref${SQ}" err
22942303
)
22952304
'
22962305

@@ -2309,14 +2318,15 @@ do
23092318
format_command $type "create refs/heads/ref" "$old_head" &&
23102319
format_command $type "create refs/heads/Foo" "$old_head"
23112320
} >stdin &&
2312-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2321+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
23132322
23142323
echo $head >expect &&
23152324
git rev-parse refs/heads/foo >actual &&
23162325
echo $old_head >expect &&
23172326
git rev-parse refs/heads/ref >actual &&
23182327
test_cmp expect actual &&
2319-
test_grep -q "reference conflict due to case-insensitive filesystem" stdout
2328+
test_grep "rejected refs/heads/Foo $old_head $ZERO_OID reference conflict due to case-insensitive filesystem" stdout &&
2329+
test_grep -e "cannot lock ref ${SQ}refs/heads/Foo${SQ}: Unable to create" -e "Foo.lock" err
23202330
)
23212331
'
23222332

@@ -2357,8 +2367,9 @@ do
23572367
git symbolic-ref refs/heads/symbolic refs/heads/non-existent &&
23582368
23592369
format_command $type "delete refs/heads/symbolic" "$head" >stdin &&
2360-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2361-
test_grep "reference does not exist" stdout
2370+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2371+
test_grep "rejected refs/heads/non-existent $ZERO_OID $head reference does not exist" stdout &&
2372+
test_grep "cannot lock ref ${SQ}refs/heads/symbolic${SQ}: unable to resolve reference ${SQ}refs/heads/non-existent${SQ}" err
23622373
)
23632374
'
23642375

@@ -2373,8 +2384,9 @@ do
23732384
head=$(git rev-parse HEAD) &&
23742385
23752386
format_command $type "delete refs/heads/new-branch" "$head" >stdin &&
2376-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2377-
test_grep "incorrect old value provided" stdout
2387+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2388+
test_grep "rejected refs/heads/new-branch $ZERO_OID $head incorrect old value provided" stdout &&
2389+
test_grep "cannot lock ref ${SQ}refs/heads/new-branch${SQ}: is at $(git rev-parse new-branch) but expected $head" err
23782390
)
23792391
'
23802392

@@ -2387,8 +2399,9 @@ do
23872399
head=$(git rev-parse HEAD) &&
23882400
23892401
format_command $type "delete refs/heads/non-existent" "$head" >stdin &&
2390-
git update-ref $type --stdin --batch-updates <stdin >stdout &&
2391-
test_grep "reference does not exist" stdout
2402+
git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2403+
test_grep "rejected refs/heads/non-existent $ZERO_OID $head reference does not exist" stdout &&
2404+
test_grep "cannot lock ref ${SQ}refs/heads/non-existent${SQ}: unable to resolve reference ${SQ}refs/heads/non-existent${SQ}" err
23922405
)
23932406
'
23942407
done

0 commit comments

Comments
 (0)