threads / patch / 32941

v2, 3 partscontrib/subtree: Store subtree sources in .gitsubtree

Subject: [PATCH v2 1/3] contrib/subtree: Store subtree sources in .gitsubtree

## tl;dr

One message between Feb 19, 2013 and Feb 19, 2013. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Paul Campbell· Feb 19, 2013, 12:29 UTC · lore

Add the prefix, repository and refspec in the file .gitsubtree when git subtree add is used. Then when a git subtree push or pull is needed the repository and refspec from .gitsubtree are used as the default values.

Having to remember what subtree came from what source is a waste of developer memory and doesn't transfer easily to other developers.

git subtree push/pull operations would typically be to/from the same source that the original subtree was cloned from with git subtree add.

The <repository> and <refspec>, or <commit>, used in the git subtree add operation are stored in .gitsubtree. One line each, delimited by a space: "<prefix> <repository> <refspec>" or "<prefix> . <commit>".

Subsequent git subtree push/pull operations now default to the values stored in .gitsubtree, unless overridden from the command line.

The .gitsubtree file should be tracked as part of the repo as it describes where parts of the tree came from and can be used to update to and from that source.

Signed-off-by: Paul Campbell <pcampbell@kemitix.net>
---
 contrib/subtree/git-subtree.sh | 75 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 64 insertions(+), 11 deletions(-)
Show changes to contrib/subtree/git-subtree.sh +64 −13
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 8a23f58..1d0bf35 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -11,8 +11,8 @@ OPTS_SPEC="\
 git subtree add   --prefix=<prefix> <commit>
 git subtree add   --prefix=<prefix> <repository> <commit>
 git subtree merge --prefix=<prefix> <commit>
-git subtree pull  --prefix=<prefix> <repository> <refspec...>
-git subtree push  --prefix=<prefix> <repository> <refspec...>
+git subtree pull  --prefix=<prefix> [<repository> <refspec...>]
+git subtree push  --prefix=<prefix> [<repository> <refspec...>]
 git subtree split --prefix=<prefix> <commit...>
 --
 h,help        show the help
@@ -489,6 +489,28 @@ ensure_clean()
 	fi
 }

+add_subtree () {
+	if ( grep "^$dir " .gitsubtree 2>/dev/null )
+	then
+		# remove $dir from .gitsubtree - there's probably a clever way to
do this with sed
+		grep -v "^$dir " .gitsubtree >.gitsubtree.temp
+		rm .gitsubtree
+		mv .gitsubtree.temp .gitsubtree
+	fi
+	if test $# -eq 1
+	then
+		# Only a commit provided, thus use the current repository
+		echo "$dir . $@" >>.gitsubtree
+	elif test $# -eq 2
+	then
+		echo "$dir $@" >>.gitsubtree
+	fi
+}
+
+get_subtree () {
+	grep "^$dir " .gitsubtree || die "Subtree not found in .gitsubtree: " "$dir"
+}
+
 cmd_add()
 {
 	if [ -e "$dir" ]; then
@@ -497,6 +519,8 @@ cmd_add()

 	ensure_clean
 	
+	add_subtree "$@"
+
 	if [ $# -eq 1 ]; then
 	    git rev-parse -q --verify "$1^{commit}" >/dev/null ||
 	    die "'$1' does not refer to a commit"
@@ -700,7 +724,23 @@ cmd_merge()
 cmd_pull()
 {
 	ensure_clean
-	git fetch "$@" || exit $?
+	if test $# -eq 0
+	then
+		subtree=($(get_subtree))
+		repository=${subtree[1]}
+		refspec=${subtree[2]}
+		if test "$repository" == "."
+		then
+			echo "Pulling into $dir from branch $refspec"
+		else
+			echo "Pulling into $dir from $repository $refspec"
+		fi
+		echo "git fetch using: " $repository $refspec
+		git fetch "$repository" "$refspec" || exit $?
+	else
+		echo "git fetch using: $@"
+		git fetch "$@" || exit $?
+	fi
 	revs=FETCH_HEAD
 	set -- $revs
 	cmd_merge "$@"
@@ -708,16 +748,29 @@ cmd_pull()

 cmd_push()
 {
-	if [ $# -ne 2 ]; then
-	    die "You must provide <repository> <refspec>"
+	repository=$1
+	refspec=$2
+	if test $# -eq 0
+	then
+		subtree=($(get_subtree))
+		repository=${subtree[1]}
+		refspec=${subtree[2]}
+		if test "$repository" == "."
+		then
+			echo "Pushing from $dir into branch $refspec"
+		else
+			echo "Pushing from $dir into $repository $refspec"
+		fi
+	elif test $# -ne 2
+	then
+		die "You must provide <repository> <refspec>, or a <prefix> listed
in .gitsubtree"
 	fi
-	if [ -e "$dir" ]; then
-	    repository=$1
-	    refspec=$2
-	    echo "git push using: " $repository $refspec
-	    git push $repository $(git subtree split
--prefix=$prefix):refs/heads/$refspec
+	if test -e "$dir"
+	then
+		echo "git push using: " $repository $refspec
+		git push $repository $(git subtree split
--prefix=$prefix):refs/heads/$refspec
 	else
-	    die "'$dir' must already exist. Try 'git subtree add'."
+		die "'$dir' must already exist. Try 'git subtree add'."
 	fi
 }
-- 
1.8.2.rc0.16.g20a599e

← back to recent threads