Skip to content

Commit 6d7ebbc

Browse files
committed
Merge branch 'dl/push-missing-object-error' into jch
"git push" had a code path that led to BUG() but it should have been a die(), as it is a response to a usual but invalid end-user action to attempt pushing an object that does not exist. * dl/push-missing-object-error: remote.c: convert if-else ladder to switch remote.c: remove BUG in show_push_unqualified_ref_name_error() t5516: remove surrounding empty lines in test bodies
2 parents f690e97 + dfbfc22 commit 6d7ebbc

File tree

2 files changed

+19
-59
lines changed

2 files changed

+19
-59
lines changed

remote.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,6 @@ static void show_push_unqualified_ref_name_error(const char *dst_value,
11711171
const char *matched_src_name)
11721172
{
11731173
struct object_id oid;
1174-
enum object_type type;
11751174

11761175
/*
11771176
* TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
@@ -1196,30 +1195,37 @@ static void show_push_unqualified_ref_name_error(const char *dst_value,
11961195
BUG("'%s' is not a valid object, "
11971196
"match_explicit_lhs() should catch this!",
11981197
matched_src_name);
1199-
type = odb_read_object_info(the_repository->objects, &oid, NULL);
1200-
if (type == OBJ_COMMIT) {
1198+
1199+
switch (odb_read_object_info(the_repository->objects, &oid, NULL)) {
1200+
case OBJ_COMMIT:
12011201
advise(_("The <src> part of the refspec is a commit object.\n"
12021202
"Did you mean to create a new branch by pushing to\n"
12031203
"'%s:refs/heads/%s'?"),
12041204
matched_src_name, dst_value);
1205-
} else if (type == OBJ_TAG) {
1205+
break;
1206+
case OBJ_TAG:
12061207
advise(_("The <src> part of the refspec is a tag object.\n"
12071208
"Did you mean to create a new tag by pushing to\n"
12081209
"'%s:refs/tags/%s'?"),
12091210
matched_src_name, dst_value);
1210-
} else if (type == OBJ_TREE) {
1211+
break;
1212+
case OBJ_TREE:
12111213
advise(_("The <src> part of the refspec is a tree object.\n"
12121214
"Did you mean to tag a new tree by pushing to\n"
12131215
"'%s:refs/tags/%s'?"),
12141216
matched_src_name, dst_value);
1215-
} else if (type == OBJ_BLOB) {
1217+
break;
1218+
case OBJ_BLOB:
12161219
advise(_("The <src> part of the refspec is a blob object.\n"
12171220
"Did you mean to tag a new blob by pushing to\n"
12181221
"'%s:refs/tags/%s'?"),
12191222
matched_src_name, dst_value);
1220-
} else {
1221-
BUG("'%s' should be commit/tag/tree/blob, is '%d'",
1222-
matched_src_name, type);
1223+
break;
1224+
default:
1225+
advise(_("The <src> part of the refspec ('%s') "
1226+
"is an object ID that doesn't exist.\n"),
1227+
matched_src_name);
1228+
break;
12231229
}
12241230
}
12251231

t/t5516-fetch-push.sh

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ check_push_result () {
105105
}
106106

107107
test_expect_success setup '
108-
109108
>path1 &&
110109
git add path1 &&
111110
test_tick &&
@@ -117,7 +116,6 @@ test_expect_success setup '
117116
test_tick &&
118117
git commit -a -m second &&
119118
the_commit=$(git show-ref -s --verify refs/heads/main)
120-
121119
'
122120

123121
for cmd in push fetch
@@ -322,104 +320,82 @@ test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf
322320
'
323321

324322
test_expect_success 'push with matching heads' '
325-
326323
mk_test testrepo heads/main &&
327324
git push testrepo : &&
328325
check_push_result testrepo $the_commit heads/main
329-
330326
'
331327

332328
test_expect_success 'push with matching heads on the command line' '
333-
334329
mk_test testrepo heads/main &&
335330
git push testrepo : &&
336331
check_push_result testrepo $the_commit heads/main
337-
338332
'
339333

340334
test_expect_success 'failed (non-fast-forward) push with matching heads' '
341-
342335
mk_test testrepo heads/main &&
343336
git push testrepo : &&
344337
git commit --amend -massaged &&
345338
test_must_fail git push testrepo &&
346339
check_push_result testrepo $the_commit heads/main &&
347340
git reset --hard $the_commit
348-
349341
'
350342

351343
test_expect_success 'push --force with matching heads' '
352-
353344
mk_test testrepo heads/main &&
354345
git push testrepo : &&
355346
git commit --amend -massaged &&
356347
git push --force testrepo : &&
357348
! check_push_result testrepo $the_commit heads/main &&
358349
git reset --hard $the_commit
359-
360350
'
361351

362352
test_expect_success 'push with matching heads and forced update' '
363-
364353
mk_test testrepo heads/main &&
365354
git push testrepo : &&
366355
git commit --amend -massaged &&
367356
git push testrepo +: &&
368357
! check_push_result testrepo $the_commit heads/main &&
369358
git reset --hard $the_commit
370-
371359
'
372360

373361
test_expect_success 'push with no ambiguity (1)' '
374-
375362
mk_test testrepo heads/main &&
376363
git push testrepo main:main &&
377364
check_push_result testrepo $the_commit heads/main
378-
379365
'
380366

381367
test_expect_success 'push with no ambiguity (2)' '
382-
383368
mk_test testrepo remotes/origin/main &&
384369
git push testrepo main:origin/main &&
385370
check_push_result testrepo $the_commit remotes/origin/main
386-
387371
'
388372

389373
test_expect_success 'push with colon-less refspec, no ambiguity' '
390-
391374
mk_test testrepo heads/main heads/t/main &&
392375
git branch -f t/main main &&
393376
git push testrepo main &&
394377
check_push_result testrepo $the_commit heads/main &&
395378
check_push_result testrepo $the_first_commit heads/t/main
396-
397379
'
398380

399381
test_expect_success 'push with weak ambiguity (1)' '
400-
401382
mk_test testrepo heads/main remotes/origin/main &&
402383
git push testrepo main:main &&
403384
check_push_result testrepo $the_commit heads/main &&
404385
check_push_result testrepo $the_first_commit remotes/origin/main
405-
406386
'
407387

408388
test_expect_success 'push with weak ambiguity (2)' '
409-
410389
mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
411390
git push testrepo main:main &&
412391
check_push_result testrepo $the_commit heads/main &&
413392
check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
414-
415393
'
416394

417395
test_expect_success 'push with ambiguity' '
418-
419396
mk_test testrepo heads/frotz tags/frotz &&
420397
test_must_fail git push testrepo main:frotz &&
421398
check_push_result testrepo $the_first_commit heads/frotz tags/frotz
422-
423399
'
424400

425401
test_expect_success 'push with onelevel ref' '
@@ -428,17 +404,14 @@ test_expect_success 'push with onelevel ref' '
428404
'
429405

430406
test_expect_success 'push with colon-less refspec (1)' '
431-
432407
mk_test testrepo heads/frotz tags/frotz &&
433408
git branch -f frotz main &&
434409
git push testrepo frotz &&
435410
check_push_result testrepo $the_commit heads/frotz &&
436411
check_push_result testrepo $the_first_commit tags/frotz
437-
438412
'
439413

440414
test_expect_success 'push with colon-less refspec (2)' '
441-
442415
mk_test testrepo heads/frotz tags/frotz &&
443416
if git show-ref --verify -q refs/heads/frotz
444417
then
@@ -448,7 +421,6 @@ test_expect_success 'push with colon-less refspec (2)' '
448421
git push -f testrepo frotz &&
449422
check_push_result testrepo $the_commit tags/frotz &&
450423
check_push_result testrepo $the_first_commit heads/frotz
451-
452424
'
453425

454426
test_expect_success 'push with colon-less refspec (3)' '
@@ -465,7 +437,6 @@ test_expect_success 'push with colon-less refspec (3)' '
465437
'
466438

467439
test_expect_success 'push with colon-less refspec (4)' '
468-
469440
mk_test testrepo &&
470441
if git show-ref --verify -q refs/heads/frotz
471442
then
@@ -475,38 +446,34 @@ test_expect_success 'push with colon-less refspec (4)' '
475446
git push testrepo frotz &&
476447
check_push_result testrepo $the_commit tags/frotz &&
477448
test 1 = $( cd testrepo && git show-ref | wc -l )
478-
479449
'
480450

481451
test_expect_success 'push head with non-existent, incomplete dest' '
482-
483452
mk_test testrepo &&
484453
git push testrepo main:branch &&
485454
check_push_result testrepo $the_commit heads/branch
486-
487455
'
488456

489457
test_expect_success 'push tag with non-existent, incomplete dest' '
490-
491458
mk_test testrepo &&
492459
git tag -f v1.0 &&
493460
git push testrepo v1.0:tag &&
494461
check_push_result testrepo $the_commit tags/tag
495-
496462
'
497463

498464
test_expect_success 'push oid with non-existent, incomplete dest' '
499-
500465
mk_test testrepo &&
501466
test_must_fail git push testrepo $(git rev-parse main):foo
502-
503467
'
504468

505469
test_expect_success 'push ref expression with non-existent, incomplete dest' '
506-
507470
mk_test testrepo &&
508471
test_must_fail git push testrepo main^:branch
472+
'
509473

474+
test_expect_success 'push ref expression with non-existent oid src' '
475+
mk_test testrepo &&
476+
test_must_fail git push testrepo $(test_oid 001):branch
510477
'
511478

512479
for head in HEAD @
@@ -550,7 +517,6 @@ do
550517
git checkout main &&
551518
git push testrepo $head:branch &&
552519
check_push_result testrepo $the_commit heads/branch
553-
554520
'
555521

556522
test_expect_success "push with config remote.*.push = $head" '
@@ -596,7 +562,6 @@ test_expect_success 'push with remote.pushdefault' '
596562
'
597563

598564
test_expect_success 'push with config remote.*.pushurl' '
599-
600565
mk_test testrepo heads/main &&
601566
git checkout main &&
602567
test_config remote.there.url test2repo &&
@@ -655,15 +620,13 @@ test_expect_success 'push ignores "branch." config without subsection' '
655620
'
656621

657622
test_expect_success 'push with dry-run' '
658-
659623
mk_test testrepo heads/main &&
660624
old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
661625
git push --dry-run testrepo : &&
662626
check_push_result testrepo $old_commit heads/main
663627
'
664628

665629
test_expect_success 'push updates local refs' '
666-
667630
mk_test testrepo heads/main &&
668631
mk_child testrepo child &&
669632
(
@@ -673,11 +636,9 @@ test_expect_success 'push updates local refs' '
673636
test $(git rev-parse main) = \
674637
$(git rev-parse remotes/origin/main)
675638
)
676-
677639
'
678640

679641
test_expect_success 'push updates up-to-date local refs' '
680-
681642
mk_test testrepo heads/main &&
682643
mk_child testrepo child1 &&
683644
mk_child testrepo child2 &&
@@ -689,23 +650,19 @@ test_expect_success 'push updates up-to-date local refs' '
689650
test $(git rev-parse main) = \
690651
$(git rev-parse remotes/origin/main)
691652
)
692-
693653
'
694654

695655
test_expect_success 'push preserves up-to-date packed refs' '
696-
697656
mk_test testrepo heads/main &&
698657
mk_child testrepo child &&
699658
(
700659
cd child &&
701660
git push &&
702661
! test -f .git/refs/remotes/origin/main
703662
)
704-
705663
'
706664

707665
test_expect_success 'push does not update local refs on failure' '
708-
709666
mk_test testrepo heads/main &&
710667
mk_child testrepo child &&
711668
echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
@@ -717,16 +674,13 @@ test_expect_success 'push does not update local refs on failure' '
717674
test $(git rev-parse main) != \
718675
$(git rev-parse remotes/origin/main)
719676
)
720-
721677
'
722678

723679
test_expect_success 'allow deleting an invalid remote ref' '
724-
725680
mk_test testrepo heads/branch &&
726681
rm -f testrepo/.git/objects/??/* &&
727682
git push testrepo :refs/heads/branch &&
728683
(cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
729-
730684
'
731685

732686
test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '

0 commit comments

Comments
 (0)