git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Nicola Paolucci <npaolucci@atlassian.com>
Date
Mar 10, 2016, 09:44 UTC
Message-ID
<1457603052-53963-3-git-send-email-npaolucci@atlassian.com>
In-Reply-To
<1457603052-53963-1-git-send-email-npaolucci@atlassian.com>
Example output:

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

Signed-off-by: Nicola Paolucci <npaolucci@atlassian.com>
---
 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
Previous: Nicola PaolucciNext: Nicola Paolucci
Message 3 of 5 in “subtree: add 'git-subtree-repo' and list command”
  1. 0/3 subtree: add 'git-subtree-repo' and list commandNicola Paolucci, Mar 10, 2016
  2. 1/3 contrib/subtree: 'add' stores 'git-subtree-repo'Nicola Paolucci, Mar 10, 2016
  3. 2/3 contrib/subtree: new list command to list subtreesNicola Paolucci, Mar 10, 2016
  4. 3/3 contrib/subtree: list --resolve gets symbolic refsNicola Paolucci, Mar 10, 2016
  5. David A. GreeneMay 21, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.