Subject: [PATCHv2] git-branch: Set branch properties

From: Santi Béjar <[email-masked]>
Date: Tue, 17 Oct 2006 16:47:07 +0200

Added a flag to git-branch (-c) that makes git-branch to write the
config (branch properties) for the new branch so it will pull the same
branch it branched from.

If you want to work in the 'next' branch of git.git:

$ git clone git://git.kernel.org/pub/scm/git/git.git
$ cd git
$ git branch -c mynext origin next
$ tail -n 3 .git/config
[branch "mynext"]
        remote = origin
        merge = refs/heads/next
$ git branch -c secondnext next
$ tail -n 3 .git/config
[branch "secondnext"]
        remote = .
        merge = next

Signed-off-by: Santi Béjar <[email-masked]>
---

 Hi,

   it needs the get_ref_for_remote_branch function in
   git-parse-remote.sh that is with the patch:

   [RFC/PATCH] git-fetch: Use already fetched branch with the --local
   flag

   Also, this does not apply cleanly in next, the change in next is and
   the resolution trivial:

 <	rm -f "$GIT_DIR/logs/refs/heads/$branch_name"
 <	rm -f "$GIT_DIR/refs/heads/$branch_name"
 >	git update-ref -d "refs/heads/$branch_name" "$branch"

 What I have to do? Send the postimage or what?

 Another questions is about rerere: Can it be done at the hunk level? We
 could record the git-patch-id of the conflict and the postimage.

 Documentation/git-branch.txt |   12 ++++++++++--
 git-branch.sh                |   30 +++++++++++++++++++++++++++---
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index d43ef1d..77bbf81 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git-branch' [-r]
-'git-branch' [-l] [-f] <branchname> [<start-point>]
+'git-branch' [-l] [-f] [-c] <branchname> [<start-point> | <remote> <remotebranch>]
 'git-branch' (-d | -D) <branchname>...
 
 DESCRIPTION
@@ -18,7 +18,8 @@ With no arguments given (or just `-r`) a
 will be shown, the current branch will be highlighted with an asterisk.
 
 In its second form, a new branch named <branchname> will be created.
-It will start out with a head equal to the one given as <start-point>.
+It will start out with a head equal to the one given as <start-point>,
+or from branch <remotebranch> of the repository <remote>.
 If no <start-point> is given, the branch will be created with a head
 equal to that of the currently checked out branch.
 
@@ -26,9 +27,16 @@ With a `-d` or `-D` option, `<branchname
 specify more than one branch for deletion.  If the branch currently
 has a ref log then the ref log will also be deleted.
 
+With a `-c` option, the branch properties `branch.<branchname>.remote`
+and `branch.<branchname>.merge` will be set.
+
 
 OPTIONS
 -------
+-c::
+	Write the branch properties `branch.<branchname>.remote`
+	and `branch.<branchname>.merge`.
+
 -d::
 	Delete a branch. The branch must be fully merged.
 
diff --git a/git-branch.sh b/git-branch.sh
index 4f31903..afd768a 100755
--- a/git-branch.sh
+++ b/git-branch.sh
@@ -1,9 +1,10 @@
 #!/bin/sh
 
-USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
+USAGE='[-l] [(-d | -D) <branchname>] | [[-f] [-c] <branchname> [<start-point> | <remote> <remotebranch>]] | -r'
 LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
 If one argument, create a new branch <branchname> based off of current HEAD.
-If two arguments, create a new branch <branchname> based off of <start-point>.'
+If two arguments, create a new branch <branchname> based off of <start-point>.
+If three arguments, create a new branch <branchname> based off the branch <remotebranch> of the repository <remote>.'
 
 SUBDIRECTORY_OK='Yes'
 . git-sh-setup
@@ -44,6 +45,8 @@ If you are sure you want to delete it, r
 	esac
 	rm -f "$GIT_DIR/logs/refs/heads/$branch_name"
 	rm -f "$GIT_DIR/refs/heads/$branch_name"
+	git repo-config --unset branch."$branchname".remote
+	git repo-config --unset-all branch."$branchname".merge
 	echo "Deleted branch $branch_name."
     done
     exit 0
@@ -71,6 +74,9 @@ do
 	-f)
 		force="$1"
 		;;
+	-c)
+		config="yes"
+		;;
 	-l)
 		create_log="yes"
 		;;
@@ -101,9 +107,21 @@ case "$#" in
 	done
 	exit 0 ;;
 1)
+	remote="."
+	remote_branch="$headref"
 	head=HEAD ;;
 2)
+	remote="."
+	remote_branch="$2"
 	head="$2^0" ;;
+3)
+	remote="$2"
+	remote_branch="$3"
+	. git-parse-remote
+	ref=$(get_ref_for_remote_branch "$remote" "$remote_branch")
+	[ -z "$ref" ] && remote_branch="refs/heads/$remote_branch" && \
+		ref=$(get_ref_for_remote_branch "$remote" "$remote_branch")
+	head="$ref^0";;
 esac
 branchname="$1"
 
@@ -137,4 +155,10 @@ then
 	mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname")
 	touch "$GIT_DIR/logs/refs/heads/$branchname"
 fi
-git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev
+git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev || exit $?
+
+if test "$config" = "yes"
+then
+	git repo-config branch."$branchname".remote "$remote"
+	git repo-config branch."$branchname".merge "$remote_branch"
+fi
-- 
1.4.2.4.g687a-dirty