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 <pcampb...@kemitix.net>
---

Reworked my previous patch paying closer attention to the coding style
documentation and renamed my new functions to make more sense.

 contrib/subtree/git-subtree.sh | 75 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 64 insertions(+), 11 deletions(-)

diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 51146bd..6dc8999 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 )
+       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.1.3.605.g02339dd
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to