[PATCH/RFC] Convenient support of remote branches in git-checkout

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



- When requested to checkout read-only remote branches,
  we try to create a new local development branch with same name.
- When we branch off a remote branch, set up default merge source.
---

This patch does the sensible thing when the user requests to
check-out a remote read-only branch (eg. origin/master).
It also automatically sets up the default merge source (with remote
entry) for a new branch.

Example to hack on git's next branch:

 git-clone --use-separate-remote http://www.kernel.org/pub/scm/git/git.git
 cd git
 git-checkout origin/next
 <hack on next>
 git pull (to merge patches from remote 'next')

The checkout creates local branch 'master' to checkout read-only remote branch
'remotes/origin/master'. Additionally, it sets up 'remotes/origin/master' from
remote repository 'origin' as default merge source for the new development branch.

Missing:
- "git branch -D <branch>" should remove remote branch attributes like
  "remote" and "merge"
- As "git-clone" already sets up a local development branch "master", it also
  should set up a default merge source for it

Josef

 git-checkout.sh |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/git-checkout.sh b/git-checkout.sh
index 119bca1..63b622e 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -12,6 +12,8 @@ force=
 branch=
 newbranch=
 newbranch_log=
+remote=
+remotebranch=
 merge=
 while [ "$#" != "0" ]; do
     arg="$1"
@@ -55,6 +57,11 @@ while [ "$#" != "0" ]; do
 			then
 				branch="$arg"
 			fi
+			if git-show-ref --verify --quiet -- "refs/remotes/$arg"
+			then
+				remote=$(echo $arg | sed -ne 's!/.*$!!p')
+				remotebranch=$(echo $arg | sed -e 's!^.*/!!')
+			fi
 		elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
 		then
 			# checking out selected paths from a tree-ish.
@@ -77,6 +84,20 @@ while [ "$#" != "0" ]; do
     esac
 done
 
+# Create a new local branch when checking out remote read-only branch
+if test -z "$newbranch" -a ! -z "$remotebranch"
+then
+	newbranch=$remotebranch
+	if git-show-ref --verify --quiet -- "refs/heads/$newbranch"
+	then
+		echo "Proposed new branch '$newbranch' to checkout read-only remote branch 'remotes/$remote/$remotebranch' exists!"
+		echo "To checkout, specify a new branch name with -b"
+		exit 1
+	fi
+	echo "Creating local branch '$newbranch' to checkout read-only remote branch 'remotes/$remote/$remotebranch'."
+fi
+
+
 # The behaviour of the command with and without explicit path
 # parameters is quite different.
 #
@@ -211,6 +232,14 @@ if [ "$?" -eq 0 ]; then
 			touch "$GIT_DIR/logs/refs/heads/$newbranch"
 		fi
 		git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
+
+		if test '' != "$remote"
+		then
+			echo "Using 'remotes/$remote/$remotebranch' from remote repository '$remote' as default merge source."
+			git-repo-config branch."$newbranch".remote "$remote"
+			git-repo-config branch."$newbranch".merge "remotes/$remote/$remotebranch"
+		fi
+
 		branch="$newbranch"
 	fi
 	[ "$branch" ] &&
-- 
1.4.3.rc2.gf8ffb

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]