Skip to contentSkip to conversation

provide fan-in and -out traversal interface

PATCH In review Bert Wesarg 2 participants 7 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 provide fan-in and -out traversal interface

This adds general functions to get the list of all offending branches for a given one. Either which depends on the given branch (fan-in) or all dependencies (fan-out).

Two simple users are provided which just lists the names or generates dot input.

---
 tg.sh |  170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 170 insertions(+), 0 deletions(-)
tg.sh +170 -0
index 3718702..926b31b 100644 tg.sh
@@ -351,6 +351,176 @@ setup_pager()
 	trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
 }
 
+# traverse_fan_out(for_each_name, for_each_dep, name, head_deps)
+#
+# traverse the dependencies of @name in bfs order and call @for_each_name
+# on each dep (i.e. node) and @for_each_dep on all dependencies (i.e. edge)
+# with source and dest as arguments.
+#
+# @name' needs to be a TopGit controlled branch
+#
+# @head_deps specifies where to take the .topdeps from for the HEAD branch
+# empty - from the committed tree
+# '(i)' - from the index
+# '(w)' - from the working dir
+#
+traverse_fan_out()
+{
+	local for_each_name=$1
+	local for_each_dep=$2
+	local name=$3
+	local head_deps=$4 || :
+	local deps_src
+	local head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')" || :
+
+	branchq="$(mktemp -t tg-fan-out.XXXXXX)"
+	allbranches="$(mktemp -t -d tg-fan-out-all.XXXXXX)"
+	trap "rm -rf \"$branchq\" \"$allbranches\"" 0
+
+	# fill queue with root name
+	echo "$name" > "$branchq"
+
+	while [ -s "$branchq" ]; do
+		# dequeue
+		{
+			read name
+			cat > "$branchq.headless"
+		} < "$branchq"
+		mv -f "$branchq.headless" "$branchq"
+
+		# eval name
+		eval "$for_each_name \"$name\""
+
+		# don't travers non-tgish branches
+		ref_exists "refs/top-bases/$name" ||
+			continue
+
+		deps_src=$name
+		# select .topdeps source for HEAD branch
+		[ "x$name" = "x$head" -a -n "$head_deps" ] &&
+			deps_src=$head_deps
+
+		old_IFS="$IFS"
+		IFS=""
+		cat_file "$deps_src:.topdeps" |
+		while read dep; do
+
+			# eval dep
+			eval "$for_each_dep \"$name\" \"$dep\""
+
+			[ -d "$allbranches/$dep" ] || {
+				mkdir -p "$allbranches/$dep"
+				echo "$dep" >> "$branchq"
+			}
+		done
+		IFS="$old_IFS"
+
+	done
+}
+
+_graph_dep_edge()
+{
+	printf "\t\"%s\" -> \"%s\";\n" "$1" "$2"
+}
+
+# prints the fan-out as a dot graph with edges
+graph_fan_out()
+{
+	printf "digraph G {\n"
+
+	traverse_fan_out : _graph_dep_edge "$1" $2
+
+	printf "}\n"
+}
+
+# prints the fan-out as name per line
+list_fan_out()
+{
+	traverse_fan_out echo : "$1" $2
+}
+
+# traverse_fan_in(for_each_name, for_each_dep, name, head_deps)
+#
+# traverse all branches which depends on @name in bfs order and call
+# @for_each_name on each (i.e. node) and @for_each_dep on all dependencies
+# (i.e. edge) with source and dest as arguments.
+#
+# @name' needs not to be a TopGit controlled branch
+#
+# @head_deps specifies where to take the .topdeps from for the HEAD branch
+# empty - from the committed tree
+# '(i)' - from the index
+# '(w)' - from the working dir
+#
+traverse_fan_in()
+{
+	local for_each_name=$1
+	local for_each_dep=$2
+	local name=$3
+	local head_deps=$4 || :
+	local deps_src
+	local head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')" || :
+
+	branchq="$(mktemp -t tg-fan-in.XXXXXX)"
+	allbranches="$(mktemp -t -d tg-fan-in-all.XXXXXX)"
+	trap "rm -rf \"$branchq\" \"$allbranches\"" 0
+
+	echo "$name" > "$branchq"
+
+	while [ -s "$branchq" ]; do
+		# dequeue
+		{
+			read name
+			cat > "$branchq.headless"
+		} < "$branchq"
+		mv -f "$branchq.headless" "$branchq"
+
+		[ ! -d "$allbranches/$name" ] ||
+			continue;
+		mkdir -p "$allbranches/$name"
+
+		# eval branch
+		eval "$for_each_name \"$name\""
+
+		old_IFS="$IFS"
+		IFS=""
+		git for-each-ref --format='%(refname)' refs/top-bases |
+			while read ref; do
+				parent="${ref#refs/top-bases/}"
+
+				deps_src=$parent
+				# select branch/index/worktree for HEAD branch
+				[ "x$parent" = "x$head" -a -n "$head_deps" ] &&
+					deps_src=$head_deps
+				cat_file "$deps_src:.topdeps" | fgrep -qx "$name" ||
+					continue
+
+				# eval dep
+				eval "$for_each_dep \"$parent\" \"$name\""
+
+				echo "$parent" >> "$branchq"
+			done
+		IFS="$old_IFS"
+
+	done
+}
+
+# prints the fan-in as a dot graph with edges
+graph_fan_in()
+{
+	printf "digraph G {\n"
+
+	traverse_fan_in : _graph_dep_edge "$1" $2
+
+	printf "}\n"
+}
+
+# prints the fan-in as name per line
+list_fan_in()
+{
+	traverse_fan_in echo : "$1" $2
+}
+
 ## Startup
 
 [ -d "@cmddir@" ] ||
Signature · 1 line
tg: (ff59ac7..) bw/fan-in-out (depends on: master)
#1 lore raw

PATCH 2/4 tg-depend: reduce: apply transitive reduction to the dependecies

This uses the tred(1) and gvpr(1) programs from the graphviz package to reduce the dependencies of the given TopGit branch.

It provides a also a new subcommand (tg tred) to play with it.

---

 .gitignore                 |    2 +
 README                     |   42 +++++++++++++++++-
 contrib/tg-completion.bash |   36 ++++++++++++++--
 tg-depend.sh               |  103 +++++++++++++++++++++++++++++--------------
 tg-tred.sh                 |   53 ++++++++++++++++++++++
 5 files changed, 196 insertions(+), 40 deletions(-)
.gitignore +2 -0
index 3298889..290cfdc 100644 .gitignore
@@ -38,5 +38,7 @@
 /tg-remote.txt
 /tg-summary
 /tg-summary.txt
+/tg-tred
+/tg-tred.txt
 /tg-update
 /tg-update.txt
README +40 -2

60 lines not on this page · raw message

contrib/tg-completion.bash +32 -4
index ccf1a32..026b9fd 100755 contrib/tg-completion.bash
@@ -267,16 +267,27 @@ _tg_delete ()
 
 _tg_depend ()
 {
-	local subcommands="add"
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+
+	local subcommands="add reduce"
 	local subcommand="$(__git_find_subcommand "$subcommands")"
 	if [ -z "$subcommand" ]; then
 		__tgcomp "$subcommands"
 		return
 	fi
 
-	case "$subcommand" in
-	add)
-		__tgcomp "$(__tg_refs)"
+	case "$subcommand:$cur" in
+	add:-*)
+		__tgcomp "
+			--stage-only
+		";;
+	add:*)
+		__tgcomp "$(__tg_refs)";;
+	reduce:-*)
+		__tgcomp "
+			--stage-only
+			--staged
+		";;
 	esac
 }
 
@@ -431,6 +442,22 @@ _tg_summary ()
 	esac
 }
 
+_tg_tred ()
+{
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+
+	case "$cur" in
+	-*)
+		__tgcomp "
+			-i
+			-w
+		"
+		;;
+	*)
+		__tgcomp "$(__tg_topics)"
+	esac
+}
+
 _tg_update ()
 {
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -491,6 +518,7 @@ _tg ()
 	push)        _tg_push ;;
 	remote)      _tg_remote ;;
 	summary)     _tg_summary ;;
+	tred)        _tg_tred ;;
 	update)      _tg_update ;;
 	*)           COMPREPLY=() ;;
 	esac
tg-depend.sh +69 -34

132 lines not on this page · raw message

tg-tred.sh new file +53 -0

53 lines not on this page · raw message

Signature · 1 line
tg: (06b6e18..) bw/tred (depends on: bw/fan-in-out)
#2 lore raw

PATCH 3/4 tg-prev/tg-next: --all option

This adds an --all option to tg-prev and tg-next, to show all offenders.

---

Note the dependency on bw/tred is only historical, it depends only bw/fan-in-out.

 README                     |    2 ++
 contrib/tg-completion.bash |    2 ++
 tg-next.sh                 |   11 ++++++++++-
 tg-prev.sh                 |   11 ++++++++++-
 4 files changed, 24 insertions(+), 2 deletions(-)
README +2 -0
index 49f7b10..b1dc45a 100644 README
@@ -573,6 +573,7 @@ tg prev
 	Options:
 	  -i		show dependencies based on index instead of branch
 	  -w		show dependencies based on working tree instead of branch
+	 --all		show not only the direct but all dependencies
 
 tg next
 ~~~~~~~
@@ -582,6 +583,7 @@ tg next
 	Options:
 	  -i		show dependencies based on index instead of branch
 	  -w		show dependencies based on working tree instead of branch
+	 --all		show not only the direct but all dependencies
 
 TODO: tg rename
 
contrib/tg-completion.bash +2 -0
index d319828..d18f564 100755 contrib/tg-completion.bash
@@ -475,6 +475,7 @@ _tg_next ()
 	case "$cur" in
 	-*)
 		__tgcomp "
+			--all
 			-i
 			-w
 		"
@@ -491,6 +492,7 @@ _tg_prev ()
 	case "$cur" in
 	-*)
 		__tgcomp "
+			--all
 			-i
 			-w
 		"
tg-next.sh +10 -1
index 9b352b3..abb9a53 100644 tg-next.sh
@@ -5,6 +5,7 @@
 # GPLv2
 
 name=
+all=false
 head_deps=
 
 
@@ -17,8 +18,10 @@ while [ -n "$1" ]; do
 		head_deps='(i)';;
 	-w)
 		head_deps='(w)';;
+	--all)
+		all=true;;
 	-*)
-		echo "Usage: tg next [-i | -w] [NAME]" >&2
+		echo "Usage: tg next [--all] [-i | -w] [NAME]" >&2
 		exit 1;;
 	*)
 		[ -z "$name" ] || die "name already specified ($name)"
@@ -29,6 +32,12 @@ done
 head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
 [ -n "$name" ] || name=$head
 
+if $all; then
+	# ignore first line, which is $name
+	list_fan_in "$name" $head_deps | tail --lines=+2
+	exit $?
+fi
+
 git for-each-ref --format='%(refname)' refs/top-bases |
 	while read ref; do
 		parent="${ref#refs/top-bases/}"
tg-prev.sh +10 -1
index efef410..63f71bd 100644 tg-prev.sh
@@ -5,6 +5,7 @@
 # GPLv2
 
 name=
+all=false
 head_deps=
 
 
@@ -17,8 +18,10 @@ while [ -n "$1" ]; do
 		head_deps='(i)';;
 	-w)
 		head_deps='(w)';;
+	--all)
+		all=true;;
 	-*)
-		echo "Usage: tg next [-i | -w] [NAME]" >&2
+		echo "Usage: tg next [--all] [-i | -w] [NAME]" >&2
 		exit 1;;
 	*)
 		[ -z "$name" ] || die "name already specified ($name)"
@@ -31,6 +34,12 @@ head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
 	die "not a TopGit-controlled branch"
 
+if $all; then
+	# ignore first line, which is $name
+	list_fan_out "$name" $head_deps | tail --lines=+2
+	exit $?
+fi
+
 deps_src=$name
 # select .topdeps source for HEAD branch
 [ "x$name" = "x$head" -a -n "$head_deps" ] &&
Signature · 1 line
tg: (bf1dfaa..) bw/next-prev (depends on: bw/tred bw/next-prev-base)
#3 lore raw

PATCH v4 4/4 tg-graph: print dependency graph like git log --graph

Just want to send it out again. I'm not pushing this for 0.9.

The graph engine comes from the git projects graph.c file, which was written by Adam Simpkins.

Thanks,
Bert

---

Note the dependency on bw/tred is only historical, it depends only bw/fan-in-out.

ChangeLog:
v4:
* --fan-in and --fan-out option
* --subject option
* color tips dep chain on other color

v3:
* option to ignore tip-topics

v2:
* fix rename bug which caused random segfaults in gvpr
* add --decorate option, to print horizontal line from graph node to patch name (like gitk do it for commits with tags or branch decorations)
* in dfs mode we should maintain the order given by the input
* make dfs mode the default

 .gitignore                 |    2 +
 Makefile                   |    1 +
 README                     |    7 +
 contrib/tg-completion.bash |   37 ++
 share/graph.gvpr           | 1100 ++++++++++++++++++++++++++++++++++++++++++++
 tg-graph.sh                |  108 +++++
 tg-summary.sh              |   37 ++-
 tg.sh                      |   64 +++-
 8 files changed, 1344 insertions(+), 12 deletions(-)
.gitignore +2 -0
index 290cfdc..b79ab0e 100644 .gitignore
@@ -22,6 +22,8 @@
 /tg-depend.txt
 /tg-export
 /tg-export.txt
+/tg-graph
+/tg-graph.txt
 /tg-import
 /tg-import.txt
 /tg-info
Makefile +1 -0
index 3ce39a5..0de1277 100644 Makefile
@@ -38,6 +38,7 @@ install:: all
 	install $(hooks_out) "$(DESTDIR)$(hooksdir)"
 	install -d -m 755 "$(DESTDIR)$(sharedir)"
 	install -m 644 $(help_out) "$(DESTDIR)$(sharedir)"
+	install -m 644 share/graph.gvpr "$(DESTDIR)$(sharedir)"
 
 clean::
 	rm -f tg $(commands_out) $(hooks_out) $(help_out)
README +7 -0
index 4f49015..3ba8c35 100644 README
@@ -566,6 +566,13 @@ tg tred
 	TODO: tg tred -r for reducing recursive all from the current/named
 	      branch
 
+tg graph
+~~~~~~~~
+	Prints the dependency graph like git log --graph to the console.
+
+	TODO: document options
+	TODO: -i/-w options
+
 TODO: tg rename
 
 
contrib/tg-completion.bash +37 -0
index 026b9fd..d8e3e02 100755 contrib/tg-completion.bash
@@ -313,6 +313,38 @@ _tg_export ()
 	esac
 }
 
+_tg_graph ()
+{
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+
+	case "$cur" in
+	-*)
+		__tgcomp "
+			--fan-out
+			--fan-in
+			--color
+			--no-color
+			--bfs
+			--dfs
+			--reverse
+			--no-reverse
+			--subject
+			--no-subject
+			--header
+			--no-header
+			--body
+			--no-body
+			--decorate
+			--no-decorate
+			--ignore-tips
+			--no-ignore-tips
+		"
+		;;
+	*)
+		__tgcomp "$(__tg_refs)"
+	esac
+}
+
 _tg_help ()
 {
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -434,9 +466,13 @@ _tg_summary ()
 	local cur="${COMP_WORDS[COMP_CWORD]}"
 
 	case "$cur" in
+	--graphviz=*)
+		__tgcomp "verbose" "" "${cur##--graphviz=}"
+		;;
 	*)
 		__tgcomp "
 			--graphviz
+			--graphviz=
 			-t
 		"
 	esac
@@ -509,6 +545,7 @@ _tg ()
 	delete)      _tg_delete ;;
 	depend)      _tg_depend ;;
 	export)      _tg_export ;;
+	graph)       _tg_graph ;;
 	help)        _tg_help ;;
 	import)      _tg_import ;;
 	info)        _tg_info ;;
share/graph.gvpr new file +1100 -0

1,100 lines not on this page · raw message

tg-graph.sh new file +108 -0

108 lines not on this page · raw message

tg-summary.sh +27 -10
index af16888..9650c8a 100644 tg-summary.sh
@@ -7,7 +7,7 @@ terse=
 graphviz=
 sort=
 deps=
-
+graphviz_verbose=
 
 ## Parse options
 
@@ -18,6 +18,9 @@ while [ -n "$1" ]; do
 		terse=1;;
 	--graphviz)
 		graphviz=1;;
+	--graphviz=verbose)
+		graphviz=1
+		graphviz_verbose=1;;
 	--sort)
 		sort=1;;
 	--deps)
@@ -43,13 +46,13 @@ if [ -n "$graphviz" ]; then
 
 digraph G {
 
-graph [
-  rankdir = "TB"
-  label="TopGit Layout\n\n\n"
-  fontsize = 14
-  labelloc=top
-  pad = "0.5,0.5"
-];
+	graph [
+		rankdir = "TB"
+		label="TopGit Layout\n\n\n"
+		fontsize = 14
+		labelloc=top
+		pad = "0.5,0.5"
+	];
 
 EOT
 fi
@@ -121,8 +124,10 @@ git for-each-ref refs/top-bases |
 				if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
 					if [ -n "$graphviz" ]; then
 						echo "\"$name\" -> \"$dep\";"
-						if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
-							echo "\"$curname\" [style=filled,fillcolor=yellow];"
+						if [ -z "$graphviz_verbose" ]; then
+							if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
+								echo "\"$curname\" [style=filled,fillcolor=yellow];"
+							fi
 						fi
 					else
 						echo "$name $dep" >&4
@@ -135,6 +140,18 @@ git for-each-ref refs/top-bases |
 	done
 
 if [ -n "$graphviz" ]; then
+	# for-each-ref gives us a sorting order
+	# thats why we put the node attributes after the edges
+	# to somewhat keep the implied order stabel
+	[ -n "$graphviz_verbose" ] && {
+		_graph_topmsg="$(mktemp -t tg-summary.XXXXXX)"
+		trap 'rm -f "$_graph_topmsg"' 0
+		git for-each-ref refs/top-bases |
+			while read rev type ref; do
+				name="${ref#refs/top-bases/}"
+				_graph_verbose=1 _graph_dep_branch "$name"
+			done
+	}
 	echo '}'
 fi
 
tg.sh +62 -2

90 lines not on this page · raw message

Signature · 1 line
tg: (73b09ef..) bw/graph (depends on: bw/fix-echo_-e-in-dash bw/tred)
#4 lore raw
In reply to Bert Wesarg
Quoted 1 line · Bert Wesarg

This adds general functions to get the list of all offending branches for

Is offending the right word here? I don't understand that.

Quoted 5 lines

a given one. Either which depends on the given branch (fan-in) or all dependencies (fan-out).

Two simple users are provided which just lists the names or generates dot input.

This should then be used for tg summary --graphviz, too, doesn't it?

Quoted 12 lines
---
 tg.sh |  170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 170 insertions(+), 0 deletions(-)
tg.sh +0 -0
index 3718702..926b31b 100644 tg.sh

tg.sh setup_pager() · line 354

Quoted 4 lines
tg.sh +1 -0
@@ -351,6 +351,176 @@ setup_pager()
 	trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
 }
 
+# traverse_fan_out(for_each_name, for_each_dep, name, head_deps)

This uses an unusual format.

please make it

# traverse_fan_out FOR_EACH_NAME FOR_EACH_DEP NAME HEAD_DEPS
Quoted 10 lines
tg.sh +10 -0
+#
+# traverse the dependencies of @name in bfs order and call @for_each_name
+# on each dep (i.e. node) and @for_each_dep on all dependencies (i.e. edge)
+# with source and dest as arguments.
+#
+# @name' needs to be a TopGit controlled branch
+#
+# @head_deps specifies where to take the .topdeps from for the HEAD branch
+# empty - from the committed tree
+# '(i)' - from the index

tg.sh setup_pager() · line 374

Quoted 10 lines
tg.sh +10 -0
+# '(w)' - from the working dir
+#
+traverse_fan_out()
+{
+	local for_each_name=$1
+	local for_each_dep=$2
+	local name=$3
+	local head_deps=$4 || :
+	local deps_src
+	local head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')" || :

I don't remember the exact problems, but some shells get using local and assignment in a single command wrong. Can you please fix that up? (I fixed that up for one of your commits.)

Quoted 155 lines
tg.sh +149 -0
+
+	branchq="$(mktemp -t tg-fan-out.XXXXXX)"
+	allbranches="$(mktemp -t -d tg-fan-out-all.XXXXXX)"
+	trap "rm -rf \"$branchq\" \"$allbranches\"" 0
+
+	# fill queue with root name
+	echo "$name" > "$branchq"
+
+	while [ -s "$branchq" ]; do
+		# dequeue
+		{
+			read name
+			cat > "$branchq.headless"
+		} < "$branchq"
+		mv -f "$branchq.headless" "$branchq"
+
+		# eval name
+		eval "$for_each_name \"$name\""
+
+		# don't travers non-tgish branches
+		ref_exists "refs/top-bases/$name" ||
+			continue
+
+		deps_src=$name
+		# select .topdeps source for HEAD branch
+		[ "x$name" = "x$head" -a -n "$head_deps" ] &&
+			deps_src=$head_deps
+
+		old_IFS="$IFS"
+		IFS=""
+		cat_file "$deps_src:.topdeps" |
+		while read dep; do
+
+			# eval dep
+			eval "$for_each_dep \"$name\" \"$dep\""
+
+			[ -d "$allbranches/$dep" ] || {
+				mkdir -p "$allbranches/$dep"
+				echo "$dep" >> "$branchq"
+			}
+		done
+		IFS="$old_IFS"
+
+	done
+}
+
+_graph_dep_edge()
+{
+	printf "\t\"%s\" -> \"%s\";\n" "$1" "$2"
+}
+
+# prints the fan-out as a dot graph with edges
+graph_fan_out()
+{
+	printf "digraph G {\n"
+
+	traverse_fan_out : _graph_dep_edge "$1" $2
+
+	printf "}\n"
+}
+
+# prints the fan-out as name per line
+list_fan_out()
+{
+	traverse_fan_out echo : "$1" $2
+}
+
+# traverse_fan_in(for_each_name, for_each_dep, name, head_deps)
+#
+# traverse all branches which depends on @name in bfs order and call
+# @for_each_name on each (i.e. node) and @for_each_dep on all dependencies
+# (i.e. edge) with source and dest as arguments.
+#
+# @name' needs not to be a TopGit controlled branch
+#
+# @head_deps specifies where to take the .topdeps from for the HEAD branch
+# empty - from the committed tree
+# '(i)' - from the index
+# '(w)' - from the working dir
+#
+traverse_fan_in()
+{
+	local for_each_name=$1
+	local for_each_dep=$2
+	local name=$3
+	local head_deps=$4 || :
+	local deps_src
+	local head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')" || :
+
+	branchq="$(mktemp -t tg-fan-in.XXXXXX)"
+	allbranches="$(mktemp -t -d tg-fan-in-all.XXXXXX)"
+	trap "rm -rf \"$branchq\" \"$allbranches\"" 0
+
+	echo "$name" > "$branchq"
+
+	while [ -s "$branchq" ]; do
+		# dequeue
+		{
+			read name
+			cat > "$branchq.headless"
+		} < "$branchq"
+		mv -f "$branchq.headless" "$branchq"
+
+		[ ! -d "$allbranches/$name" ] ||
+			continue;
+		mkdir -p "$allbranches/$name"
+
+		# eval branch
+		eval "$for_each_name \"$name\""
+
+		old_IFS="$IFS"
+		IFS=""
+		git for-each-ref --format='%(refname)' refs/top-bases |
+			while read ref; do
+				parent="${ref#refs/top-bases/}"
+
+				deps_src=$parent
+				# select branch/index/worktree for HEAD branch
+				[ "x$parent" = "x$head" -a -n "$head_deps" ] &&
+					deps_src=$head_deps
+				cat_file "$deps_src:.topdeps" | fgrep -qx "$name" ||
+					continue
+
+				# eval dep
+				eval "$for_each_dep \"$parent\" \"$name\""
+
+				echo "$parent" >> "$branchq"
+			done
+		IFS="$old_IFS"
+
+	done
+}
+
+# prints the fan-in as a dot graph with edges
+graph_fan_in()
+{
+	printf "digraph G {\n"
+
+	traverse_fan_in : _graph_dep_edge "$1" $2
+
+	printf "}\n"
+}
+
+# prints the fan-in as name per line
+list_fan_in()
+{
+	traverse_fan_in echo : "$1" $2
+}
+
 ## Startup
 
 [ -d "@cmddir@" ] ||
Signature · 1 line
tg: (ff59ac7..) bw/fan-in-out (depends on: master)
Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#5 lore raw

2010/10/5 Uwe Kleine-König <[email protected]>:

Quoted 3 lines · Bert Wesarg
Quoted 1 line

This adds general functions to get the list of all offending branches for

Is offending the right word here?  I don't understand that.

I'm unsure about the right wording here too. 'offending' is definitive the wrong word here. Somethink like this may be better:

This adds general functions to get the list of all branches which either depends on a given one (fan-in), or all dependencies of a given one (fan-out).

Quoted 7 lines
Quoted 6 lines

a given one. Either which depends on the given branch (fan-in) or all dependencies (fan-out).

Two simple users are provided which just lists the names or generates dot input.

This should then be used for tg summary --graphviz, too, doesn't it?

No. tg summary --graphviz outputs the whole graph. These will generate only a subgraph based on a given topic. tg summary does not take a topic as argument.

Quoted 16 lines
Quoted 16 lines

---
 tg.sh |  170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 1 files changed, 170 insertions(+), 0 deletions(-)
tg.sh +0 -0
index 3718702..926b31b 100644 tg.sh
@@ -351,6 +351,176 @@ setup_pager()

      trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT }

Quoted 7 lines
Quoted 1 line

+# traverse_fan_out(for_each_name, for_each_dep, name, head_deps)

This uses an unusual format.

please make it

# traverse_fan_out FOR_EACH_NAME FOR_EACH_DEP NAME HEAD_DEPS

Yes. this makes it more consistent.

Quoted 15 lines
Quoted 15 lines
tg.sh +15 -0
+#
+# traverse the dependencies of @name in bfs order and call @for_each_name
+# on each dep (i.e. node) and @for_each_dep on all dependencies (i.e. edge)
+# with source and dest as arguments.
+#
+# @name' needs to be a TopGit controlled branch
+#
+# @head_deps specifies where to take the .topdeps from for the HEAD branch
+# empty - from the committed tree
+# '(i)' - from the index
+# '(w)' - from the working dir
+#
+traverse_fan_out()
+{
+     local for_each_name=$1
Quoted 8 lines
Quoted 5 lines

+     local for_each_dep=$2
+     local name=$3
+     local head_deps=$4 || :
+     local deps_src
+     local head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')" || :

I don't remember the exact problems, but some shells get using local and assignment in a single command wrong.  Can you please fix that up?  (I fixed that up for one of your commits.)

Yes, that should be the right thing to do.

Bert

#6 lore raw

PATCH v2 1/1 provide fan-in and -out traversal interface

This adds general functions to get the list of all branches which either depends on a given one (fan-in), or all dependencies of a given one (fan-out).

Two simple users are provided which just lists the names of generates dot input.

---

Note for v3: I should probably use the list_deps function, but that needs to honor -i/-w first.

Changes:
v2:
* make doc consistent
* provide $_name to FOR_EACH-commands
* don't assign values to local variables at declaration time

 tg.sh |  184 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 184 insertions(+), 0 deletions(-)
tg.sh +184 -0
index 3718702..8fa06d3 100644 tg.sh
@@ -351,6 +351,190 @@ setup_pager()
 	trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
 }
 
+# traverse_fan_out FOR_EACH_NAME FOR_EACH_DEP NAME HEAD_DEPS
+#
+# traverse the dependencies of NAME in bfs order and call FOR_EACH_NAME
+# on each dep (i.e. node) and FOR_EACH_DEP on all dependencies (i.e. edge)
+# with source and dest as arguments.
+#
+# FOR_EACH_NAME and FOR_EACH_DEP can refer to $_name for queried branch NAME
+#
+# NAME needs to be a TopGit controlled branch
+#
+# HEAD_DEPS specifies where to take the .topdeps from for the HEAD branch
+# empty - from the committed tree
+# '(i)' - from the index
+# '(w)' - from the working dir
+#
+traverse_fan_out()
+{
+	local for_each_name for_each_dep name head_deps deps_src head
+
+	_name="$3"
+	for_each_name="$1"
+	for_each_dep="$2"
+	head_deps=$4 || :
+	head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')" || :
+
+	branchq="$(mktemp -t tg-fan-out.XXXXXX)"
+	allbranches="$(mktemp -t -d tg-fan-out-all.XXXXXX)"
+	trap "rm -rf \"$branchq\" \"$allbranches\"" 0
+
+	# fill queue with root name
+	echo "$_name" > "$branchq"
+
+	while [ -s "$branchq" ]; do
+		# dequeue
+		{
+			read name
+			cat > "$branchq.headless"
+		} < "$branchq"
+		mv -f "$branchq.headless" "$branchq"
+
+		# eval name
+		eval "$for_each_name \"$name\""
+
+		# don't travers non-tgish branches
+		ref_exists "refs/top-bases/$name" ||
+			continue
+
+		deps_src=$name
+		# select .topdeps source for HEAD branch
+		[ "x$name" = "x$head" -a -n "$head_deps" ] &&
+			deps_src=$head_deps
+
+		old_IFS="$IFS"
+		IFS=""
+		cat_file "$deps_src:.topdeps" |
+		while read dep; do
+
+			# eval dep
+			eval "$for_each_dep \"$name\" \"$dep\""
+
+			[ -d "$allbranches/$dep" ] || {
+				mkdir -p "$allbranches/$dep"
+				echo "$dep" >> "$branchq"
+			}
+		done
+		IFS="$old_IFS"
+
+	done
+}
+
+_graph_dep_edge()
+{
+	printf "\t\"%s\" -> \"%s\";\n" "$1" "$2"
+}
+
+# prints the fan-out as a dot graph with edges
+graph_fan_out()
+{
+	printf "digraph G {\n"
+
+	traverse_fan_out : _graph_dep_edge "$1" $2
+
+	printf "}\n"
+}
+
+_list_fan()
+{
+	# only print the deps
+	[ "$_name" = "$1 "] ||
+		echo "$1"
+}
+
+# prints the fan-out as name per line
+list_fan_out()
+{
+	traverse_fan_out _list_fan : "$1" $2
+}
+
+# traverse_fan_in FOR_EACH_NAME FOR_EACH_DEP NAME HEAD_DEPS
+#
+# traverse all branches which depends on NAME in bfs order and call
+# FOR_EACH_NAME on each (i.e. node) and FOR_EACH_DEP on all dependencies
+# (i.e. edge) with source and dest as arguments.
+#
+# FOR_EACH_NAME and FOR_EACH_DEP can refer to $_name for queried branch NAME
+#
+# NAME needs not to be a TopGit controlled branch
+#
+# HEAD_DEPS specifies where to take the .topdeps from for the HEAD branch
+# empty - from the committed tree
+# '(i)' - from the index
+# '(w)' - from the working dir
+#
+traverse_fan_in()
+{
+	local for_each_name for_each_dep name head_deps deps_src head
+
+	_name="$3"
+	for_each_name="$1"
+	for_each_dep="$2"
+	head_deps="$4" || :
+	head="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')" || :
+
+	branchq="$(mktemp -t tg-fan-in.XXXXXX)"
+	allbranches="$(mktemp -t -d tg-fan-in-all.XXXXXX)"
+	trap "rm -rf \"$branchq\" \"$allbranches\"" 0
+
+	# fill queue with root name
+	echo "$_name" > "$branchq"
+
+	while [ -s "$branchq" ]; do
+		# dequeue
+		{
+			read name
+			cat > "$branchq.headless"
+		} < "$branchq"
+		mv -f "$branchq.headless" "$branchq"
+
+		[ ! -d "$allbranches/$name" ] ||
+			continue;
+		mkdir -p "$allbranches/$name"
+
+		# eval branch
+		eval "$for_each_name \"$name\""
+
+		old_IFS="$IFS"
+		IFS=""
+		git for-each-ref --format='%(refname)' refs/top-bases |
+			while read ref; do
+				parent="${ref#refs/top-bases/}"
+
+				deps_src=$parent
+				# select branch/index/worktree for HEAD branch
+				[ "x$parent" = "x$head" -a -n "$head_deps" ] &&
+					deps_src=$head_deps
+				cat_file "$deps_src:.topdeps" | fgrep -qx "$name" ||
+					continue
+
+				# eval dep
+				eval "$for_each_dep \"$parent\" \"$name\""
+
+				echo "$parent" >> "$branchq"
+			done
+		IFS="$old_IFS"
+
+	done
+}
+
+# prints the fan-in as a dot graph with edges
+graph_fan_in()
+{
+	printf "digraph G {\n"
+
+	traverse_fan_in : _graph_dep_edge "$1" $2
+
+	printf "}\n"
+}
+
+# prints the fan-in as name per line
+list_fan_in()
+{
+	traverse_fan_in _list_fan : "$1" $2
+}
+
 ## Startup
 
 [ -d "@cmddir@" ] ||
Signature · 1 line
tg: (ff59ac7..) bw/fan-in-out (depends on: master)