Skip to content

Commit 9debc32

Browse files
bonziniJunio C Hamano
authored andcommitted
git-fetch, git-branch: Support local --track via a special remote '.'
This patch adds support for a dummy remote '.' to avoid having to declare a fake remote like [remote "local"] url = . fetch = refs/heads/*:refs/heads/* Such a builtin remote simplifies the operation of "git-fetch", which will populate FETCH_HEAD but will not pretend that two repositories are in use, will not create a thin pack, and will not perform any useless remapping of names. The speed improvement is around 20%, and it should improve more if "git-fetch" is converted to a builtin. To this end, git-parse-remote is grown with a new kind of remote, 'builtin'. In git-fetch.sh, we treat the builtin remote specially in that it needs no pack/store operations. In fact, doing git-fetch on a builtin remote will simply populate FETCH_HEAD appropriately. The patch also improves of the --track/--no-track support, extending it so that branch.<name>.remote items referring '.' can be created. Finally, it fixes a typo in git-checkout.sh. Signed-off-by: Paolo Bonzini <bonzini@gnu.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 803527f commit 9debc32

File tree

7 files changed

+79
-22
lines changed

7 files changed

+79
-22
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ branch.<name>.merge::
272272
`git fetch`) to lookup the default branch for merging. Without
273273
this option, `git pull` defaults to merge the first refspec fetched.
274274
Specify multiple values to get an octopus merge.
275+
If you wish to setup `git pull` so that it merges into <name> from
276+
another branch in the local repository, you can point
277+
branch.<name>.merge to the desired branch, and use the special setting
278+
`.` (a period) for branch.<name>.remote.
275279

276280
color.branch::
277281
A boolean to enable/disable color in the output of

builtin-branch.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,26 @@ static int get_remote_config(const char *key, const char *value)
372372
return 0;
373373
}
374374

375-
static void set_branch_defaults(const char *name, const char *real_ref)
375+
static void set_branch_merge(const char *name, const char *config_remote,
376+
const char *config_repo)
376377
{
377378
char key[1024];
379+
if (sizeof(key) <=
380+
snprintf(key, sizeof(key), "branch.%s.remote", name))
381+
die("what a long branch name you have!");
382+
git_config_set(key, config_remote);
383+
384+
/*
385+
* We do not have to check if we have enough space for
386+
* the 'merge' key, since it's shorter than the
387+
* previous 'remote' key, which we already checked.
388+
*/
389+
snprintf(key, sizeof(key), "branch.%s.merge", name);
390+
git_config_set(key, config_repo);
391+
}
392+
393+
static void set_branch_defaults(const char *name, const char *real_ref)
394+
{
378395
const char *slash = strrchr(real_ref, '/');
379396

380397
if (!slash)
@@ -384,21 +401,15 @@ static void set_branch_defaults(const char *name, const char *real_ref)
384401
start_len = strlen(real_ref);
385402
base_len = slash - real_ref;
386403
git_config(get_remote_config);
404+
if (!config_repo && !config_remote &&
405+
!prefixcmp(real_ref, "refs/heads/")) {
406+
set_branch_merge(name, ".", real_ref);
407+
printf("Branch %s set up to track local branch %s.\n",
408+
name, real_ref);
409+
}
387410

388411
if (config_repo && config_remote) {
389-
if (sizeof(key) <=
390-
snprintf(key, sizeof(key), "branch.%s.remote", name))
391-
die("what a long branch name you have!");
392-
git_config_set(key, config_remote);
393-
394-
/*
395-
* We do not have to check if we have enough space for
396-
* the 'merge' key, since it's shorter than the
397-
* previous 'remote' key, which we already checked.
398-
*/
399-
snprintf(key, sizeof(key), "branch.%s.merge", name);
400-
git_config_set(key, config_repo);
401-
412+
set_branch_merge(name, config_remote, config_repo);
402413
printf("Branch %s set up to track remote branch %s.\n",
403414
name, real_ref);
404415
}

git-checkout.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ while [ "$#" != "0" ]; do
8989
esac
9090
done
9191

92-
case "$new_branch,$track" in
92+
case "$newbranch,$track" in
9393
,--*)
9494
die "git checkout: --track and --no-track require -b"
9595
esac

git-fetch.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,17 @@ then
157157
fi
158158
fi
159159
160-
fetch_native () {
160+
fetch_all_at_once () {
161161
162162
eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
163163
eval "$eval"
164164
165165
( : subshell because we muck with IFS
166166
IFS=" $LF"
167167
(
168-
if test -f "$remote" ; then
168+
if test "$remote" = . ; then
169+
git-show-ref $rref || echo failed "$remote"
170+
elif test -f "$remote" ; then
169171
test -n "$shallow_depth" &&
170172
die "shallow clone with bundle is not supported"
171173
git-bundle unbundle "$remote" $rref ||
@@ -188,7 +190,7 @@ fetch_native () {
188190
189191
}
190192
191-
fetch_dumb () {
193+
fetch_per_ref () {
192194
reflist="$1"
193195
refs=
194196
rref=
@@ -292,10 +294,10 @@ fetch_dumb () {
292294
fetch_main () {
293295
case "$remote" in
294296
http://* | https://* | ftp://* | rsync://* )
295-
fetch_dumb "$@"
297+
fetch_per_ref "$@"
296298
;;
297299
*)
298-
fetch_native "$@"
300+
fetch_all_at_once "$@"
299301
;;
300302
esac
301303
}

git-parse-remote.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ get_data_source () {
99
*/*)
1010
echo ''
1111
;;
12+
.)
13+
echo self
14+
;;
1215
*)
1316
if test "$(git-config --get "remote.$1.url")"
1417
then
@@ -31,6 +34,9 @@ get_remote_url () {
3134
'')
3235
echo "$1"
3336
;;
37+
self)
38+
echo "$1"
39+
;;
3440
config)
3541
git-config --get "remote.$1.url"
3642
;;
@@ -57,7 +63,7 @@ get_default_remote () {
5763
get_remote_default_refs_for_push () {
5864
data_source=$(get_data_source "$1")
5965
case "$data_source" in
60-
'' | branches)
66+
'' | branches | self)
6167
;; # no default push mapping, just send matching refs.
6268
config)
6369
git-config --get-all "remote.$1.push" ;;
@@ -163,6 +169,10 @@ get_remote_default_refs_for_fetch () {
163169
case "$data_source" in
164170
'')
165171
echo "HEAD:" ;;
172+
self)
173+
canon_refs_list_for_fetch -d "$1" \
174+
$(git-for-each-ref --format='%(refname):')
175+
;;
166176
config)
167177
canon_refs_list_for_fetch -d "$1" \
168178
$(git-config --get-all "remote.$1.fetch") ;;
@@ -177,7 +187,7 @@ get_remote_default_refs_for_fetch () {
177187
}' "$GIT_DIR/remotes/$1")
178188
;;
179189
*)
180-
die "internal error: get-remote-default-ref-for-push $1" ;;
190+
die "internal error: get-remote-default-ref-for-fetch $1" ;;
181191
esac
182192
}
183193

t/t3200-branch.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,15 @@ test_expect_success 'test overriding tracking setup via --no-track' \
145145
git-config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
146146
(git-show-ref -q refs/remotes/local/master || git-fetch local) &&
147147
git-branch --no-track my2 local/master &&
148+
git-config branch.autosetupmerge false &&
148149
! test $(git-config branch.my2.remote) = local &&
149150
! test $(git-config branch.my2.merge) = refs/heads/master'
150151

152+
test_expect_success 'test local tracking setup' \
153+
'git branch --track my6 s &&
154+
test $(git-config branch.my6.remote) = . &&
155+
test $(git-config branch.my6.merge) = refs/heads/s'
156+
151157
# Keep this test last, as it changes the current branch
152158
cat >expect <<EOF
153159
0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master

t/t5520-pull.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,29 @@ test_expect_success 'checking the results' '
2929
diff file cloned/file
3030
'
3131

32+
test_expect_success 'test . as a remote' '
33+
34+
git branch copy master &&
35+
git config branch.copy.remote . &&
36+
git config branch.copy.merge refs/heads/master &&
37+
echo updated >file &&
38+
git commit -a -m updated &&
39+
git checkout copy &&
40+
test `cat file` = file &&
41+
git pull &&
42+
test `cat file` = updated
43+
'
44+
45+
test_expect_success 'the default remote . should not break explicit pull' '
46+
git checkout -b second master^ &&
47+
echo modified >file &&
48+
git commit -a -m modified &&
49+
git checkout copy &&
50+
git reset --hard HEAD^ &&
51+
test `cat file` = file &&
52+
git pull . second &&
53+
test `cat file` = modified
54+
'
55+
3256
test_done
3357

0 commit comments

Comments
 (0)