@matheusbsbs / matheus-bagliano / commits / 9debc3241b
Fork 0

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>

Paolo Bonzini committed Mar 15, 2007 at 09:23 UTC 9debc3241b5d54891600c839b4df4f48b2daa60c
7 files changed +79 -22
Documentation/config.txt
+4
@@ -272,6 +272,10 @@ branch.<name>.merge::
272 `git fetch`) to lookup the default branch for merging. Without
273 this option, `git pull` defaults to merge the first refspec fetched.
274 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.
279
280 color.branch::
281 A boolean to enable/disable color in the output of
builtin-branch.c
+25 -14
@@ -372,9 +372,26 @@ static int get_remote_config(const char *key, const char *value)
372 return 0;
373 }
374
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)
377 {
378 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 +{
395 const char *slash = strrchr(real_ref, '/');
396
397 if (!slash)
@@ -384,21 +401,15 @@ static void set_branch_defaults(const char *name, const char *real_ref)
401 start_len = strlen(real_ref);
402 base_len = slash - real_ref;
403 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 + }
410
411 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);
413 printf("Branch %s set up to track remote branch %s.\n",
414 name, real_ref);
415 }
git-checkout.sh
+1 -1
@@ -89,7 +89,7 @@ while [ "$#" != "0" ]; do
89 esac
90 done
91
92 -case "$new_branch,$track" in
92 +case "$newbranch,$track" in
93 ,--*)
94 die "git checkout: --track and --no-track require -b"
95 esac
git-fetch.sh
+7 -5
@@ -157,7 +157,7 @@ then
157 fi
158 fi
159
160 -fetch_native () {
160 +fetch_all_at_once () {
161
162 eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
163 eval "$eval"
@@ -165,7 +165,9 @@ fetch_native () {
165 ( : subshell because we muck with IFS
166 IFS=" $LF"
167 (
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
171 test -n "$shallow_depth" &&
172 die "shallow clone with bundle is not supported"
173 git-bundle unbundle "$remote" $rref ||
@@ -188,7 +190,7 @@ fetch_native () {
190
191 }
192
191 -fetch_dumb () {
193 +fetch_per_ref () {
194 reflist="$1"
195 refs=
196 rref=
@@ -292,10 +294,10 @@ fetch_dumb () {
294 fetch_main () {
295 case "$remote" in
296 http://* | https://* | ftp://* | rsync://* )
295 - fetch_dumb "$@"
297 + fetch_per_ref "$@"
298 ;;
299 *)
298 - fetch_native "$@"
300 + fetch_all_at_once "$@"
301 ;;
302 esac
303 }
git-parse-remote.sh
+12 -2
@@ -9,6 +9,9 @@ get_data_source () {
9 */*)
10 echo ''
11 ;;
12 + .)
13 + echo self
14 + ;;
15 *)
16 if test "$(git-config --get "remote.$1.url")"
17 then
@@ -31,6 +34,9 @@ get_remote_url () {
34 '')
35 echo "$1"
36 ;;
37 + self)
38 + echo "$1"
39 + ;;
40 config)
41 git-config --get "remote.$1.url"
42 ;;
@@ -57,7 +63,7 @@ get_default_remote () {
63 get_remote_default_refs_for_push () {
64 data_source=$(get_data_source "$1")
65 case "$data_source" in
60 - '' | branches)
66 + '' | branches | self)
67 ;; # no default push mapping, just send matching refs.
68 config)
69 git-config --get-all "remote.$1.push" ;;
@@ -163,6 +169,10 @@ get_remote_default_refs_for_fetch () {
169 case "$data_source" in
170 '')
171 echo "HEAD:" ;;
172 + self)
173 + canon_refs_list_for_fetch -d "$1" \
174 + $(git-for-each-ref --format='%(refname):')
175 + ;;
176 config)
177 canon_refs_list_for_fetch -d "$1" \
178 $(git-config --get-all "remote.$1.fetch") ;;
@@ -177,7 +187,7 @@ get_remote_default_refs_for_fetch () {
187 }' "$GIT_DIR/remotes/$1")
188 ;;
189 *)
180 - die "internal error: get-remote-default-ref-for-push $1" ;;
190 + die "internal error: get-remote-default-ref-for-fetch $1" ;;
191 esac
192 }
193
t/t3200-branch.sh
+6
@@ -145,9 +145,15 @@ test_expect_success 'test overriding tracking setup via --no-track' \
145 git-config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
146 (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
147 git-branch --no-track my2 local/master &&
148 + git-config branch.autosetupmerge false &&
149 ! test $(git-config branch.my2.remote) = local &&
150 ! test $(git-config branch.my2.merge) = refs/heads/master'
151
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 +
157 # Keep this test last, as it changes the current branch
158 cat >expect <<EOF
159 0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
t/t5520-pull.sh
+24
@@ -29,5 +29,29 @@ test_expect_success 'checking the results' '
29 diff file cloned/file
30 '
31
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 +
56 test_done
57