|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/github/gh-stack/internal/git" |
| 8 | + "github.com/github/gh-stack/internal/stack" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +// ancestryMock builds an IsAncestor func from an explicit linear history per |
| 13 | +// branch: history[branch] lists the SHAs that branch contains, oldest first. |
| 14 | +func ancestryMock(history map[string][]string) func(string, string) (bool, error) { |
| 15 | + index := func(list []string, sha string) int { |
| 16 | + for i, s := range list { |
| 17 | + if s == sha { |
| 18 | + return i |
| 19 | + } |
| 20 | + } |
| 21 | + return -1 |
| 22 | + } |
| 23 | + return func(ancestor, descendant string) (bool, error) { |
| 24 | + // descendant may be a branch name or a SHA within some branch. |
| 25 | + for branch, list := range history { |
| 26 | + if branch != descendant && index(list, descendant) < 0 { |
| 27 | + continue |
| 28 | + } |
| 29 | + end := len(list) |
| 30 | + if i := index(list, descendant); i >= 0 { |
| 31 | + end = i + 1 |
| 32 | + } |
| 33 | + if index(list[:end], ancestor) >= 0 { |
| 34 | + return true, nil |
| 35 | + } |
| 36 | + } |
| 37 | + return false, nil |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestResolveOntoOldBase(t *testing.T) { |
| 42 | + // b2 contains: trunk -> a-old -> b2-own |
| 43 | + // The parent (b1) has since been amended to a-new, which b2 does NOT have. |
| 44 | + history := map[string][]string{ |
| 45 | + "b2": {"trunk", "a-old", "b2-own"}, |
| 46 | + "b1": {"trunk", "a-new"}, |
| 47 | + } |
| 48 | + |
| 49 | + t.Run("recorded upstream is used when the branch contains it", func(t *testing.T) { |
| 50 | + restore := git.SetOps(&git.MockOps{IsAncestorFn: ancestryMock(history)}) |
| 51 | + defer restore() |
| 52 | + |
| 53 | + got := resolveOntoOldBase("a-old", "a-old", "b1", "b2") |
| 54 | + assert.Equal(t, "a-old", got) |
| 55 | + }) |
| 56 | + |
| 57 | + // The #250 case: the parent was amended, so its current tip is not in the |
| 58 | + // branch's history. Falling back to a merge base would replay the parent's |
| 59 | + // superseded commit; the recorded metadata base is the correct boundary. |
| 60 | + t.Run("falls back to the metadata base when the parent was amended", func(t *testing.T) { |
| 61 | + restore := git.SetOps(&git.MockOps{ |
| 62 | + IsAncestorFn: ancestryMock(history), |
| 63 | + MergeBaseFn: func(a, b string) (string, error) { return "trunk", nil }, |
| 64 | + }) |
| 65 | + defer restore() |
| 66 | + |
| 67 | + got := resolveOntoOldBase("a-new", "a-old", "b1", "b2") |
| 68 | + assert.Equal(t, "a-old", got, |
| 69 | + "should replay only b2's own commits, not the parent's superseded one") |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("prefers the latest usable boundary", func(t *testing.T) { |
| 73 | + restore := git.SetOps(&git.MockOps{ |
| 74 | + IsAncestorFn: ancestryMock(history), |
| 75 | + MergeBaseFn: func(a, b string) (string, error) { return "trunk", nil }, |
| 76 | + }) |
| 77 | + defer restore() |
| 78 | + |
| 79 | + // Both "trunk" and "a-old" are ancestors of b2; a-old replays fewer commits. |
| 80 | + got := resolveOntoOldBase("a-new", "a-old", "b1", "b2") |
| 81 | + assert.Equal(t, "a-old", got) |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("falls back to a merge base when no metadata base is recorded", func(t *testing.T) { |
| 85 | + restore := git.SetOps(&git.MockOps{ |
| 86 | + IsAncestorFn: ancestryMock(history), |
| 87 | + MergeBaseFn: func(a, b string) (string, error) { return "trunk", nil }, |
| 88 | + }) |
| 89 | + defer restore() |
| 90 | + |
| 91 | + got := resolveOntoOldBase("a-new", "", "b1", "b2") |
| 92 | + assert.Equal(t, "trunk", got) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("returns the recorded value when nothing is usable", func(t *testing.T) { |
| 96 | + restore := git.SetOps(&git.MockOps{ |
| 97 | + IsAncestorFn: func(string, string) (bool, error) { return false, nil }, |
| 98 | + MergeBaseFn: func(string, string) (string, error) { return "", errors.New("no merge base") }, |
| 99 | + }) |
| 100 | + defer restore() |
| 101 | + |
| 102 | + got := resolveOntoOldBase("a-new", "unrelated", "b1", "b2") |
| 103 | + assert.Equal(t, "a-new", got) |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("ignores an unusable metadata base", func(t *testing.T) { |
| 107 | + restore := git.SetOps(&git.MockOps{ |
| 108 | + IsAncestorFn: ancestryMock(history), |
| 109 | + MergeBaseFn: func(a, b string) (string, error) { return "trunk", nil }, |
| 110 | + }) |
| 111 | + defer restore() |
| 112 | + |
| 113 | + // A metadata base from an unrelated history must not be trusted. |
| 114 | + got := resolveOntoOldBase("a-new", "bogus", "b1", "b2") |
| 115 | + assert.Equal(t, "trunk", got) |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +func TestUpdateBaseSHAs(t *testing.T) { |
| 120 | + newStack := func(b2Base string) *stack.Stack { |
| 121 | + return &stack.Stack{ |
| 122 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 123 | + Branches: []stack.BranchRef{ |
| 124 | + {Branch: "b1"}, |
| 125 | + {Branch: "b2", Base: b2Base}, |
| 126 | + }, |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + t.Run("advances the base when the branch really is stacked on the parent", func(t *testing.T) { |
| 131 | + restore := git.SetOps(&git.MockOps{ |
| 132 | + RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil }, |
| 133 | + IsAncestorFn: func(string, string) (bool, error) { return true, nil }, |
| 134 | + }) |
| 135 | + defer restore() |
| 136 | + |
| 137 | + s := newStack("stale-base") |
| 138 | + updateBaseSHAs(s) |
| 139 | + assert.Equal(t, "sha-b1", s.Branches[1].Base) |
| 140 | + assert.Equal(t, "sha-b2", s.Branches[1].Head) |
| 141 | + }) |
| 142 | + |
| 143 | + // The root cause of #250: `gh stack push` recorded the parent's amended tip |
| 144 | + // as the child's base even though the child had not been rebased onto it, |
| 145 | + // which made the next cascade replay the parent's superseded commits. |
| 146 | + t.Run("keeps the recorded base when the parent moved out of band", func(t *testing.T) { |
| 147 | + restore := git.SetOps(&git.MockOps{ |
| 148 | + RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil }, |
| 149 | + IsAncestorFn: func(ancestor, descendant string) (bool, error) { |
| 150 | + // b2 does not contain b1's amended tip. |
| 151 | + if ancestor == "sha-b1" && descendant == "b2" { |
| 152 | + return false, nil |
| 153 | + } |
| 154 | + return true, nil |
| 155 | + }, |
| 156 | + }) |
| 157 | + defer restore() |
| 158 | + |
| 159 | + s := newStack("b1-original-tip") |
| 160 | + updateBaseSHAs(s) |
| 161 | + assert.Equal(t, "b1-original-tip", s.Branches[1].Base, |
| 162 | + "the base must keep describing where b2 actually sits") |
| 163 | + assert.Equal(t, "sha-b2", s.Branches[1].Head, "head is always the branch's own tip") |
| 164 | + }) |
| 165 | + |
| 166 | + t.Run("records a base when none was known yet", func(t *testing.T) { |
| 167 | + restore := git.SetOps(&git.MockOps{ |
| 168 | + RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil }, |
| 169 | + IsAncestorFn: func(string, string) (bool, error) { |
| 170 | + return false, nil |
| 171 | + }, |
| 172 | + }) |
| 173 | + defer restore() |
| 174 | + |
| 175 | + s := newStack("") |
| 176 | + updateBaseSHAs(s) |
| 177 | + assert.Equal(t, "sha-b1", s.Branches[1].Base, |
| 178 | + "with nothing recorded there is no truth to preserve") |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("keeps the recorded base when ancestry cannot be determined", func(t *testing.T) { |
| 182 | + restore := git.SetOps(&git.MockOps{ |
| 183 | + RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil }, |
| 184 | + IsAncestorFn: func(string, string) (bool, error) { return false, errors.New("boom") }, |
| 185 | + }) |
| 186 | + defer restore() |
| 187 | + |
| 188 | + s := newStack("known-base") |
| 189 | + updateBaseSHAs(s) |
| 190 | + assert.Equal(t, "known-base", s.Branches[1].Base) |
| 191 | + }) |
| 192 | + |
| 193 | + t.Run("skips merged branches", func(t *testing.T) { |
| 194 | + restore := git.SetOps(&git.MockOps{ |
| 195 | + RevParseFn: func(ref string) (string, error) { return "sha-" + ref, nil }, |
| 196 | + IsAncestorFn: func(string, string) (bool, error) { return true, nil }, |
| 197 | + }) |
| 198 | + defer restore() |
| 199 | + |
| 200 | + s := &stack.Stack{ |
| 201 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 202 | + Branches: []stack.BranchRef{ |
| 203 | + {Branch: "b1", Base: "frozen", PullRequest: &stack.PullRequestRef{Number: 1, Merged: true}}, |
| 204 | + {Branch: "b2"}, |
| 205 | + }, |
| 206 | + } |
| 207 | + updateBaseSHAs(s) |
| 208 | + assert.Equal(t, "frozen", s.Branches[0].Base, "a merged branch's base is left alone") |
| 209 | + assert.Equal(t, "sha-main", s.Branches[1].Base, "b2 is measured against trunk") |
| 210 | + }) |
| 211 | +} |
0 commit comments