@@ -15,6 +15,9 @@ const StackCommentMarker = "<!-- gh-stack:nav -->"
1515// It includes a warning if the PR targets a non-trunk branch.
1616// The repoURL should be the base repository URL (e.g., "https://github.com/owner/repo").
1717// The prInfo map provides titles for PRs (keyed by PR number).
18+ //
19+ // Only the current stack is rendered: the path from root to the current branch,
20+ // plus all descendants of the current branch. Sibling stacks are excluded.
1821func GenerateStackComment (root * tree.Node , currentBranch , trunk , repoURL string , prInfo map [int ]PRInfo ) string {
1922 var sb strings.Builder
2023
@@ -24,6 +27,15 @@ func GenerateStackComment(root *tree.Node, currentBranch, trunk, repoURL string,
2427 return ""
2528 }
2629
30+ // Build ancestor path: the set of branch names from root down to (and
31+ // including) the current branch. When rendering, only children on this
32+ // path are shown for ancestor nodes; descendants of the current branch
33+ // are always shown in full.
34+ ancestorPath := make (map [string ]bool )
35+ for n := currentNode ; n != nil ; n = n .Parent {
36+ ancestorPath [n .Name ] = true
37+ }
38+
2739 // Start with marker
2840 sb .WriteString (StackCommentMarker )
2941 sb .WriteString ("\n " )
@@ -50,8 +62,8 @@ func GenerateStackComment(root *tree.Node, currentBranch, trunk, repoURL string,
5062 // Stack header
5163 sb .WriteString ("### :books: Pull Request Stack\n \n " )
5264
53- // Render tree from root as nested markdown list
54- renderTree (& sb , root , currentBranch , repoURL , prInfo , 0 )
65+ // Render tree from root as nested markdown list, filtered to current stack
66+ renderTree (& sb , root , currentBranch , repoURL , prInfo , 0 , ancestorPath )
5567
5668 sb .WriteString ("\n ---\n " )
5769 sb .WriteString ("*Managed by [gh-stack](https://github.com/boneskull/gh-stack)*\n " )
@@ -76,13 +88,20 @@ func collectPRNumbersRecursive(node *tree.Node, numbers *[]int) {
7688}
7789
7890// renderTree recursively renders the tree structure as nested markdown lists.
79- func renderTree (sb * strings.Builder , node * tree.Node , currentBranch , repoURL string , prInfo map [int ]PRInfo , depth int ) {
91+ //
92+ // The ancestorPath set controls which children are rendered at each level:
93+ // - For nodes that are ancestors of the current branch (on the path but not
94+ // the current branch itself), only the child on the ancestor path is shown.
95+ // - For the current branch and all its descendants, all children are shown.
96+ //
97+ // This ensures only the current stack is rendered, not sibling stacks.
98+ func renderTree (sb * strings.Builder , node * tree.Node , currentBranch , repoURL string , prInfo map [int ]PRInfo , depth int , ancestorPath map [string ]bool ) {
8099 // Build prefix based on depth (2 spaces per level for markdown nested lists)
81100 prefix := strings .Repeat (" " , depth ) + "- "
82101
83102 isCurrent := node .Name == currentBranch
84103
85- // Format: "[Title #N](url) - branch: `name`" or just branch name if no PR
104+ // Format: "[Title #N](url) - branch: `name`" or " branch: ` name`" if no PR
86105 if node .PR > 0 {
87106 prURL := fmt .Sprintf ("%s/pull/%d" , repoURL , node .PR )
88107 linkText := fmt .Sprintf ("#%d" , node .PR )
@@ -97,19 +116,25 @@ func renderTree(sb *strings.Builder, node *tree.Node, currentBranch, repoURL str
97116 fmt .Fprintf (sb , "%s[%s](%s) - branch: `%s`" , prefix , linkText , prURL , node .Name )
98117 }
99118 } else {
100- // No PR - just show branch name (e.g., trunk)
119+ // No PR - show " branch: ` name`"
101120 if isCurrent {
102- fmt .Fprintf (sb , "%s**`%s`**" , prefix , node .Name )
121+ fmt .Fprintf (sb , "%s**branch: `%s`** *(this PR) *" , prefix , node .Name )
103122 } else {
104- fmt .Fprintf (sb , "%s `%s`" , prefix , node .Name )
123+ fmt .Fprintf (sb , "%sbranch: `%s`" , prefix , node .Name )
105124 }
106125 }
107126
108127 sb .WriteString ("\n " )
109128
110- // Render children
129+ // For ancestor nodes (above the current branch), only render the child
130+ // that leads to the current branch. For the current branch and its
131+ // descendants, render all children.
132+ isAncestorAboveCurrent := ancestorPath [node .Name ] && ! isCurrent
111133 for _ , child := range node .Children {
112- renderTree (sb , child , currentBranch , repoURL , prInfo , depth + 1 )
134+ if isAncestorAboveCurrent && ! ancestorPath [child .Name ] {
135+ continue
136+ }
137+ renderTree (sb , child , currentBranch , repoURL , prInfo , depth + 1 , ancestorPath )
113138 }
114139}
115140
0 commit comments