git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH, fixed] git-fetch, git-branch: Support local --track via a special remote `.'
@ 2007-03-15  8:23 Paolo Bonzini
  2007-03-16  0:21 ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Bonzini @ 2007-03-15  8:23 UTC (permalink / raw)
  To: git

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>
---
 Documentation/config.txt |    4 ++++
 builtin-branch.c         |   39 +++++++++++++++++++++++++--------------
 git-checkout.sh          |    2 +-
 git-fetch.sh             |   29 +++++++++++++++++++----------
 git-parse-remote.sh      |   13 +++++++++++--
 t/t3200-branch.sh        |    6 ++++++
 t/t5520-pull.sh          |   24 ++++++++++++++++++++++++
 7 files changed, 90 insertions(+), 27 deletions(-)

	This patch includes the testcase from Junio -- which now
	passes.  Fixing it is easy but required a curious implementation:
	since all we have to do is append fetched heads to FETCH_HEADS,
	it is simpler to put the handling of builtin `.' into fetch_dumb.

	To make the name of fetch_dumb more consistent with the
	functionality, I renamed it to fetch_heads, as well as
	renaming fetch_native to fetch_packs.

	The patch was regression tested and shows no regression.  I added
	to t5520-pull.sh the testcase that was failing in the first respin.


diff --git a/Documentation/config.txt b/Documentation/config.txt
index aaae9ac..953acae 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -272,6 +272,10 @@ branch.<name>.merge::
 	`git fetch`) to lookup the default branch for merging. Without
 	this option, `git pull` defaults to merge the first refspec fetched.
 	Specify multiple values to get an octopus merge.
+	If you wish to setup `git pull` so that it merges into <name> from
+	another branch in the local repository, you can point
+	branch.<name>.merge to the desired branch, and use the special setting
+	`.` (a period) for branch.<name>.remote.
 
 color.branch::
 	A boolean to enable/disable color in the output of
diff --git a/builtin-branch.c b/builtin-branch.c
index 42b1ff1..a4494ee 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -372,9 +372,26 @@ static int get_remote_config(const char *key, const char *value)
 	return 0;
 }
 
-static void set_branch_defaults(const char *name, const char *real_ref)
+static void set_branch_merge(const char *name, const char *config_remote,
+			     const char *config_repo)
 {
 	char key[1024];
+	if (sizeof(key) <=
+	    snprintf(key, sizeof(key), "branch.%s.remote", name))
+		die("what a long branch name you have!");
+	git_config_set(key, config_remote);
+
+	/*
+	 * We do not have to check if we have enough space for
+	 * the 'merge' key, since it's shorter than the
+	 * previous 'remote' key, which we already checked.
+	 */
+	snprintf(key, sizeof(key), "branch.%s.merge", name);
+	git_config_set(key, config_repo);
+}
+
+static void set_branch_defaults(const char *name, const char *real_ref)
+{
 	const char *slash = strrchr(real_ref, '/');
 
 	if (!slash)
@@ -384,21 +401,15 @@ static void set_branch_defaults(const char *name, const char *real_ref)
 	start_len = strlen(real_ref);
 	base_len = slash - real_ref;
 	git_config(get_remote_config);
+	if (!config_repo && !config_remote &&
+	    !prefixcmp(real_ref, "refs/heads/")) {
+		set_branch_merge(name, ".", real_ref);
+		printf("Branch %s set up to track local branch %s.\n",
+		       name, real_ref);
+	}
 
 	if (config_repo && config_remote) {
-		if (sizeof(key) <=
-		    snprintf(key, sizeof(key), "branch.%s.remote", name))
-			die("what a long branch name you have!");
-		git_config_set(key, config_remote);
-
-		/*
-		 * We do not have to check if we have enough space for
-		 * the 'merge' key, since it's shorter than the
-		 * previous 'remote' key, which we already checked.
-		 */
-		snprintf(key, sizeof(key), "branch.%s.merge", name);
-		git_config_set(key, config_repo);
-
+		set_branch_merge(name, config_remote, config_repo);
 		printf("Branch %s set up to track remote branch %s.\n",
 		       name, real_ref);
 	}
diff --git a/git-checkout.sh b/git-checkout.sh
index 6caa9fd..b292ff0 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -89,7 +89,7 @@ while [ "$#" != "0" ]; do
     esac
 done
 
-case "$new_branch,$track" in
+case "$newbranch,$track" in
 ,--*)
 	die "git checkout: --track and --no-track require -b"
 esac
diff --git a/git-fetch.sh b/git-fetch.sh
index ebe6c33..3b01f06 100755
--- a/git-fetch.sh
+++ b/git-fetch.sh
@@ -161,7 +161,7 @@ then
 	fi
 fi
 
-fetch_native () {
+fetch_packs () {
 
   eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
   eval "$eval"
@@ -192,7 +192,7 @@ fetch_native () {
 
 }
 
-fetch_dumb () {
+fetch_heads () {
   reflist="$1"
   refs=
   rref=
@@ -286,6 +286,10 @@ fetch_dumb () {
 	      rsync_slurped_objects=t
 	  }
 	  ;;
+      .)
+	  local_name=$remote_name
+	  head=$(git-rev-parse --verify "$local_name")
+	  ;;
       esac
 
       append_fetch_head "$head" "$remote" \
@@ -296,14 +300,19 @@ fetch_dumb () {
 }
 
 fetch_main () {
-	case "$remote" in
-	http://* | https://* | ftp://* | rsync://* )
-		fetch_dumb "$@"
-		;;
-	*)
-		fetch_native "$@"
-		;;
-	esac
+	data_source=$(get_data_source "$remote_nick")
+	if test "$data_source" = builtin; then
+		fetch_heads "$@"
+	else
+		case "$remote" in
+		http://* | https://* | ftp://* | rsync://* )
+			fetch_heads "$@"
+			;;
+		*)
+			fetch_packs "$@"
+			;;
+		esac
+	fi
 }
 
 fetch_main "$reflist" || exit
diff --git a/git-parse-remote.sh b/git-parse-remote.sh
index c46131f..a94215d 100755
--- a/git-parse-remote.sh
+++ b/git-parse-remote.sh
@@ -9,6 +9,9 @@ get_data_source () {
 	*/*)
 		echo ''
 		;;
+	.)
+		echo builtin
+		;;
 	*)
 		if test "$(git-config --get "remote.$1.url")"
 		then
@@ -31,6 +34,9 @@ get_remote_url () {
 	'')
 		echo "$1"
 		;;
+	builtin)
+		echo "$1"
+		;;
 	config)
 		git-config --get "remote.$1.url"
 		;;
@@ -57,7 +63,7 @@ get_default_remote () {
 get_remote_default_refs_for_push () {
 	data_source=$(get_data_source "$1")
 	case "$data_source" in
-	'' | branches)
+	'' | branches | builtin)
 		;; # no default push mapping, just send matching refs.
 	config)
 		git-config --get-all "remote.$1.push" ;;
@@ -163,6 +169,9 @@ get_remote_default_refs_for_fetch () {
 	case "$data_source" in
 	'')
 		echo "HEAD:" ;;
+	builtin)
+	        canon_refs_list_for_fetch -d "$1" \
+			$(git-show-ref | sed -n 's,.*[      ]\(refs/.*\),\1:,p') ;;
 	config)
 		canon_refs_list_for_fetch -d "$1" \
 			$(git-config --get-all "remote.$1.fetch") ;;
@@ -177,7 +186,7 @@ get_remote_default_refs_for_fetch () {
 					}' "$GIT_DIR/remotes/$1")
 		;;
 	*)
-		die "internal error: get-remote-default-ref-for-push $1" ;;
+		die "internal error: get-remote-default-ref-for-fetch $1" ;;
 	esac
 }
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 75c000a..9558bdb 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -145,9 +145,15 @@ test_expect_success 'test overriding tracking setup via --no-track' \
      git-config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
      (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
      git-branch --no-track my2 local/master &&
+     git-config branch.autosetupmerge false &&
      ! test $(git-config branch.my2.remote) = local &&
      ! test $(git-config branch.my2.merge) = refs/heads/master'
 
+test_expect_success 'test local tracking setup' \
+    'git branch --track my6 s &&
+     test $(git-config branch.my6.remote) = . &&
+     test $(git-config branch.my6.merge) = refs/heads/s'
+
 # Keep this test last, as it changes the current branch
 cat >expect <<EOF
 0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000	branch: Created from master
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index 7eb3783..243212d 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -29,5 +29,29 @@ test_expect_success 'checking the results' '
 	diff file cloned/file
 '
 
+test_expect_success 'test . as a remote' '
+
+	git branch copy master &&
+	git config branch.copy.remote . &&
+	git config branch.copy.merge refs/heads/master &&
+	echo updated >file &&
+	git commit -a -m updated &&
+	git checkout copy &&
+	test `cat file` = file &&
+	git pull &&
+	test `cat file` = updated
+'
+
+test_expect_success 'the default remote . should not break explicit pull' '
+	git checkout -b second master^ &&
+	echo modified >file &&
+	git commit -a -m modified &&
+	git checkout copy &&
+	git reset --hard HEAD^ &&
+	test `cat file` = file &&
+	git pull . second &&
+	test `cat file` = modified
+'
+
 test_done
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH, fixed] git-fetch, git-branch: Support local --track via a special remote `.'
  2007-03-15  8:23 [PATCH, fixed] git-fetch, git-branch: Support local --track via a special remote `.' Paolo Bonzini
@ 2007-03-16  0:21 ` Junio C Hamano
  2007-03-16  8:38   ` [PATCH] git-fetch, git-parse-remote: Cleanup implementation of '.' Paolo Bonzini
  0 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2007-03-16  0:21 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: git

Paolo Bonzini <bonzini@gnu.org> writes:

> 	To make the name of fetch_dumb more consistent with the
> 	functionality, I renamed it to fetch_heads, as well as
> 	renaming fetch_native to fetch_packs.

I agree renaming fetch_dumb vs fetch_native to names that
reflect their nature better might be a good idea.  The former is
to perform fetching objects and updating tracking ref for one
ref at a time, while the latter is to do the fetching and
updating all relevant tracking refs in one go.  The 'per-ref'
behaviour is exactly why the former is called "dumb".

If you really want to rename, perhaps making the former
fetch_per_ref and the latter fetch_all_at_once would reflect
their nature better.  While I do not have strong preference
either way, I think "fetch_head" is a slight improvement over
"fetch_dumb", because it tells that "per-ref" nature a bit
better, and "fetch_pack" is not any better than "fetch_native".

While talking about names, I think calling data_source 'builtin'
feels very wrong, as we _might_ want to later add other kinds of
repository the system knows about.  I would have picked 'self',
as this change is about referring the repository itself using a
fake name '.' for it.

I think it would be a lot more efficient if you made the 'self'
fetch piggyback on fetch_all_at_once side, like the attached
patch.

This is on top of your patch to illustrate the difference, and
does not have the renaming I suggested (i.e. dumb->per_ref,
packs->all_at_once, and builtin->self).

diff --git a/git-fetch.sh b/git-fetch.sh
index cbbb5ca..ffe6847 100755
--- a/git-fetch.sh
+++ b/git-fetch.sh
@@ -170,6 +170,10 @@ fetch_packs () {
 		die "shallow clone with bundle is not supported"
 	    git-bundle unbundle "$remote" $rref ||
 	    echo failed "$remote"
+	elif test "." = "$remote"
+	then
+		git-show-ref $rref ||
+		echo failed .
 	else
 	  git-fetch-pack --thin $exec $keep $shallow_depth $no_progress \
 		"$remote" $rref ||
@@ -280,10 +284,6 @@ fetch_heads () {
 	      rsync_slurped_objects=t
 	  }
 	  ;;
-      .)
-	  local_name=$remote_name
-	  head=$(git-rev-parse --verify "$local_name")
-	  ;;
       esac
 
       append_fetch_head "$head" "$remote" \
@@ -296,7 +296,7 @@ fetch_heads () {
 fetch_main () {
 	data_source=$(get_data_source "$remote_nick")
 	if test "$data_source" = builtin; then
-		fetch_heads "$@"
+		fetch_packs "$@"
 	else
 		case "$remote" in
 		http://* | https://* | ftp://* | rsync://* )

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] git-fetch, git-parse-remote: Cleanup implementation of '.'
  2007-03-16  0:21 ` Junio C Hamano
@ 2007-03-16  8:38   ` Paolo Bonzini
  2007-03-16  9:32     ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Bonzini @ 2007-03-16  8:38 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

As per the mailing list exchanges, this applies the following changes:

- renames the data source from 'builtin' to 'self'.

- renames fetch_packs/fetch_heads to fetch_per_ref and fetch_all_at_once

- processes any remote whose URL is '.' (not only the builtin one) using
native-store (this is tested by t3200-branch.sh).

Signed-Off-By: Paolo Bonzini  <bonzini@gnu.org>
---
 git-fetch.sh        |   33 +++++++++++++--------------------
 git-parse-remote.sh |    8 ++++----
 2 files changed, 17 insertions(+), 24 deletions(-)

	The patch is a cleanup of the one you posted, and it was
	appropriately tested with no regression.

	I can see now what you meant by the split between fetch and
	merge logic making my patch more complicated than necessary.

diff --git a/git-fetch.sh b/git-fetch.sh
index 3b01f06..a650116 100755
--- a/git-fetch.sh
+++ b/git-fetch.sh
@@ -161,7 +161,7 @@ then
 	fi
 fi
 
-fetch_packs () {
+fetch_all_at_once () {
 
   eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
   eval "$eval"
@@ -169,7 +169,9 @@ fetch_packs () {
     ( : subshell because we muck with IFS
       IFS=" 	$LF"
       (
-	if test -f "$remote" ; then
+	if test "$remote" = . ; then
+	    git-show-ref $rref || echo failed "$remote"
+	elif test -f "$remote" ; then
 	    test -n "$shallow_depth" &&
 		die "shallow clone with bundle is not supported"
 	    git-bundle unbundle "$remote" $rref ||
@@ -192,7 +194,7 @@ fetch_packs () {
 
 }
 
-fetch_heads () {
+fetch_per_ref () {
   reflist="$1"
   refs=
   rref=
@@ -286,10 +288,6 @@ fetch_heads () {
 	      rsync_slurped_objects=t
 	  }
 	  ;;
-      .)
-	  local_name=$remote_name
-	  head=$(git-rev-parse --verify "$local_name")
-	  ;;
       esac
 
       append_fetch_head "$head" "$remote" \
@@ -300,19 +298,14 @@ fetch_heads () {
 }
 
 fetch_main () {
-	data_source=$(get_data_source "$remote_nick")
-	if test "$data_source" = builtin; then
-		fetch_heads "$@"
-	else
-		case "$remote" in
-		http://* | https://* | ftp://* | rsync://* )
-			fetch_heads "$@"
-			;;
-		*)
-			fetch_packs "$@"
-			;;
-		esac
-	fi
+	case "$remote" in
+	http://* | https://* | ftp://* | rsync://* )
+		fetch_per_ref "$@"
+		;;
+	*)
+		fetch_all_at_once "$@"
+		;;
+	esac
 }
 
 fetch_main "$reflist" || exit
diff --git a/git-parse-remote.sh b/git-parse-remote.sh
index a94215d..f25f9c1 100755
--- a/git-parse-remote.sh
+++ b/git-parse-remote.sh
@@ -10,7 +10,7 @@ get_data_source () {
 		echo ''
 		;;
 	.)
-		echo builtin
+		echo self
 		;;
 	*)
 		if test "$(git-config --get "remote.$1.url")"
@@ -34,7 +34,7 @@ get_remote_url () {
 	'')
 		echo "$1"
 		;;
-	builtin)
+	self)
 		echo "$1"
 		;;
 	config)
@@ -63,7 +63,7 @@ get_default_remote () {
 get_remote_default_refs_for_push () {
 	data_source=$(get_data_source "$1")
 	case "$data_source" in
-	'' | branches | builtin)
+	'' | branches | self)
 		;; # no default push mapping, just send matching refs.
 	config)
 		git-config --get-all "remote.$1.push" ;;
@@ -169,7 +169,7 @@ get_remote_default_refs_for_fetch () {
 	case "$data_source" in
 	'')
 		echo "HEAD:" ;;
-	builtin)
+	self)
 	        canon_refs_list_for_fetch -d "$1" \
 			$(git-show-ref | sed -n 's,.*[      ]\(refs/.*\),\1:,p') ;;
 	config)

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] git-fetch, git-parse-remote: Cleanup implementation of '.'
  2007-03-16  8:38   ` [PATCH] git-fetch, git-parse-remote: Cleanup implementation of '.' Paolo Bonzini
@ 2007-03-16  9:32     ` Junio C Hamano
  2007-03-16  9:38       ` Paolo Bonzini
  0 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2007-03-16  9:32 UTC (permalink / raw)
  To: bonzini; +Cc: git

Paolo Bonzini <paolo.bonzini@lu.unisi.ch> writes:

> 	The patch is a cleanup of the one you posted, and it was
> 	appropriately tested with no regression.
>
> 	I can see now what you meant by the split between fetch and
> 	merge logic making my patch more complicated than necessary.

Thanks.  I'll squash the two and apply.

> +	builtin)
> +	        canon_refs_list_for_fetch -d "$1" \
> +			$(git-show-ref | sed -n 's,.*[      ]\(refs/.*\),\1:,p') ;;

I may not be really thinking straight tonight (no I am not
drunk, but just a tad sick), but I wonder if this is sufficient?

	$(git-for-each-ref --format='%(refname):')

Shorter and one less process and pipe.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] git-fetch, git-parse-remote: Cleanup implementation of '.'
  2007-03-16  9:32     ` Junio C Hamano
@ 2007-03-16  9:38       ` Paolo Bonzini
  2007-03-16 10:00         ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Bonzini @ 2007-03-16  9:38 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: bonzini, git


> I may not be really thinking straight tonight (no I am not
> drunk, but just a tad sick), but I wonder if this is sufficient?
> 
> 	$(git-for-each-ref --format='%(refname):')
> 
> Shorter and one less process and pipe.

It is.  Would you take care of that?

Paolo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] git-fetch, git-parse-remote: Cleanup implementation of '.'
  2007-03-16  9:38       ` Paolo Bonzini
@ 2007-03-16 10:00         ` Junio C Hamano
  0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2007-03-16 10:00 UTC (permalink / raw)
  To: bonzini; +Cc: git

Paolo Bonzini <paolo.bonzini@lu.unisi.ch> writes:

>> I may not be really thinking straight tonight (no I am not
>> drunk, but just a tad sick), but I wonder if this is sufficient?
>> 
>> 	$(git-for-each-ref --format='%(refname):')
>> 
>> Shorter and one less process and pipe.
>
> It is.  Would you take care of that?

Surely, and thanks for sanity checking.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-03-16 10:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-15  8:23 [PATCH, fixed] git-fetch, git-branch: Support local --track via a special remote `.' Paolo Bonzini
2007-03-16  0:21 ` Junio C Hamano
2007-03-16  8:38   ` [PATCH] git-fetch, git-parse-remote: Cleanup implementation of '.' Paolo Bonzini
2007-03-16  9:32     ` Junio C Hamano
2007-03-16  9:38       ` Paolo Bonzini
2007-03-16 10:00         ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).