[PATCH 2/3] contrib/subtree: new list command to list subtrees

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

 



Example output:

$ git subtree list
.vim/bundle/fireplace https://github.com/tpope/vim-fireplace.git b999b0

Signed-off-by: Nicola Paolucci <npaolucci@xxxxxxxxxxxxx>
---
 contrib/subtree/git-subtree.sh     | 54 ++++++++++++++++++++++++++++++++++----
 contrib/subtree/t/t7900-subtree.sh | 18 +++++++++++++
 2 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 278699b..82f3fce 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -14,6 +14,7 @@ git subtree merge --prefix=<prefix> <commit>
 git subtree pull  --prefix=<prefix> <repository> <ref>
 git subtree push  --prefix=<prefix> <repository> <ref>
 git subtree split --prefix=<prefix> <commit...>
+git subtree list
 --
 h,help        show the help
 q             quiet
@@ -109,19 +110,22 @@ done
 command="$1"
 shift
 case "$command" in
-	add|merge|pull) default= ;;
+	add|merge|pull|list) default= ;;
 	split|push) default="--default HEAD" ;;
 	*) die "Unknown command '$command'" ;;
 esac
 
-if [ -z "$prefix" ]; then
-	die "You must provide the --prefix option."
+if [ "$command" != "list" ]; then
+	if [ -z "$prefix" ]; then
+		die "You must provide the --prefix option."
+	fi
 fi
 
 case "$command" in
-	add) [ -e "$prefix" ] && 
+	add) [ -e "$prefix" ] &&
 		die "prefix '$prefix' already exists." ;;
-	*)   [ -e "$prefix" ] || 
+	list) ;;
+	*)   [ -e "$prefix" ] ||
 		die "'$prefix' does not exist; use 'git subtree add'" ;;
 esac
 
@@ -230,6 +234,41 @@ try_remove_previous()
 	fi
 }
 
+find_subtree_repos()
+{
+	debug "Looking for subtree repos..."
+	sq=
+	main=
+	sub=
+	git log --grep="^git-subtree-dir:" \
+		--pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
+	while read a b c; do
+		debug "$a $b $c"
+		debug "{{$sq/$main/$sub}}"
+		case "$a" in
+			START) sq="$b" ;;
+			git-subtree-dir:) dir="$b $c" ;;
+			git-subtree-mainline:) main="$b" ;;
+			git-subtree-split:) sub="$b" ;;
+			git-subtree-repo:) repo="$b $c" ;;
+			END)
+				if [ -n "$sub" ]; then
+					if [ -n "$main" ]; then
+						# a rejoin commit?
+						# Pretend its sub was a squash.
+						sq="$sub"
+					fi
+					debug "Subtree found: $dir $repo $sub"
+					echo "$dir" "$repo" "$sub"
+				fi
+				sq=
+				main=
+				sub=
+				;;
+		esac
+	done
+}
+
 find_latest_squash()
 {
 	debug "Looking for latest squash ($dir)..."
@@ -536,6 +575,11 @@ get_repository_url()
 	echo $repo_url
 }
 
+cmd_list()
+{
+	find_subtree_repos "$@"
+}
+
 cmd_add()
 {
 	if [ -e "$dir" ]; then
diff --git a/contrib/subtree/t/t7900-subtree.sh b/contrib/subtree/t/t7900-subtree.sh
index ed40e73..ce97446 100755
--- a/contrib/subtree/t/t7900-subtree.sh
+++ b/contrib/subtree/t/t7900-subtree.sh
@@ -196,6 +196,24 @@ test_expect_success 'add --squash stores git-subtree-repo value' '
 '
 
 #
+# Tests for 'git subtree list'
+#
+
+next_test
+test_expect_success 'list outputs list of subtrees' '
+	subtree_test_create_repo "$subtree_test_count" &&
+	subtree_test_create_repo "$subtree_test_count/sub proj" &&
+	test_create_commit "$subtree_test_count" main1 &&
+	test_create_commit "$subtree_test_count/sub proj" sub1 &&
+	(
+		cd "$subtree_test_count" &&
+		git fetch ./"sub proj" master &&
+		git subtree add --prefix="sub dir" "./sub proj" HEAD --squash &&
+		check_equal "$(git subtree list | cut -c -19)" "sub dir ./sub proj "
+	)
+'
+
+#
 # Tests for 'git subtree merge'
 #
 
-- 
2.7.1

--
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]