Skip to content

Commit 270c354

Browse files
committed
Merge branch 'mk/maint-cg-push' into maint
* mk/maint-cg-push: git push: Interpret $GIT_DIR/branches in a Cogito compatible way
2 parents 16d2583 + 18afe10 commit 270c354

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

Documentation/urls-remotes.txt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,22 @@ This file should have the following format:
6868
------------
6969

7070
`<url>` is required; `#<head>` is optional.
71-
When you do not provide a refspec on the command line,
72-
git will use the following refspec, where `<head>` defaults to `master`,
73-
and `<repository>` is the name of this file
74-
you provided in the command line.
71+
72+
Depending on the operation, git will use one of the following
73+
refspecs, if you don't provide one on the command line.
74+
`<branch>` is the name of this file in `$GIT_DIR/branches` and
75+
`<head>` defaults to `master`.
76+
77+
git fetch uses:
78+
79+
------------
80+
refs/heads/<head>:refs/heads/<branch>
81+
------------
82+
83+
git push uses:
7584

7685
------------
77-
refs/heads/<head>:<repository>
86+
HEAD:refs/heads/<head>
7887
------------
7988

8089

remote.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ static void read_branches_file(struct remote *remote)
298298
}
299299
add_url_alias(remote, p);
300300
add_fetch_refspec(remote, strbuf_detach(&branch, 0));
301+
/*
302+
* Cogito compatible push: push current HEAD to remote #branch
303+
* (master if missing)
304+
*/
305+
strbuf_init(&branch, 0);
306+
strbuf_addstr(&branch, "HEAD");
307+
if (frag)
308+
strbuf_addf(&branch, ":refs/heads/%s", frag);
309+
else
310+
strbuf_addstr(&branch, ":refs/heads/master");
311+
add_push_refspec(remote, strbuf_detach(&branch, 0));
301312
remote->fetch_tags = 1; /* always auto-follow */
302313
}
303314

t/t5516-fetch-push.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,54 @@ test_expect_success 'allow deleting an invalid remote ref' '
494494
495495
'
496496

497+
test_expect_success 'fetch with branches' '
498+
mk_empty &&
499+
git branch second $the_first_commit &&
500+
git checkout second &&
501+
echo ".." > testrepo/.git/branches/branch1 &&
502+
(cd testrepo &&
503+
git fetch branch1 &&
504+
r=$(git show-ref -s --verify refs/heads/branch1) &&
505+
test "z$r" = "z$the_commit" &&
506+
test 1 = $(git for-each-ref refs/heads | wc -l)
507+
) &&
508+
git checkout master
509+
'
510+
511+
test_expect_success 'fetch with branches containing #' '
512+
mk_empty &&
513+
echo "..#second" > testrepo/.git/branches/branch2 &&
514+
(cd testrepo &&
515+
git fetch branch2 &&
516+
r=$(git show-ref -s --verify refs/heads/branch2) &&
517+
test "z$r" = "z$the_first_commit" &&
518+
test 1 = $(git for-each-ref refs/heads | wc -l)
519+
) &&
520+
git checkout master
521+
'
522+
523+
test_expect_success 'push with branches' '
524+
mk_empty &&
525+
git checkout second &&
526+
echo "testrepo" > .git/branches/branch1 &&
527+
git push branch1 &&
528+
(cd testrepo &&
529+
r=$(git show-ref -s --verify refs/heads/master) &&
530+
test "z$r" = "z$the_first_commit" &&
531+
test 1 = $(git for-each-ref refs/heads | wc -l)
532+
)
533+
'
534+
535+
test_expect_success 'push with branches containing #' '
536+
mk_empty &&
537+
echo "testrepo#branch3" > .git/branches/branch2 &&
538+
git push branch2 &&
539+
(cd testrepo &&
540+
r=$(git show-ref -s --verify refs/heads/branch3) &&
541+
test "z$r" = "z$the_first_commit" &&
542+
test 1 = $(git for-each-ref refs/heads | wc -l)
543+
) &&
544+
git checkout master
545+
'
546+
497547
test_done

0 commit comments

Comments
 (0)