Skip to content

Commit 1d44d01

Browse files
authored
test(repo sync): verify squash-merge behavior (#535)
Adds a test that squash-merges a shamhub PR and verifies that repo sync behaves as expected.
1 parent b33a07a commit 1d44d01

File tree

3 files changed

+121
-9
lines changed

3 files changed

+121
-9
lines changed

internal/forge/shamhub/cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func (c *Cmd) Run(ts *testscript.TestScript, neg bool, args []string) {
136136
}
137137

138138
prune := flag.Bool("prune", false, "prune the branch after merging")
139+
squash := flag.Bool("squash", false, "squash-merge the commit")
139140
ts.Check(flag.Parse(args))
140141
args = flag.Args()
141142
if len(args) != 2 {
@@ -158,6 +159,7 @@ func (c *Cmd) Run(ts *testscript.TestScript, neg bool, args []string) {
158159
Repo: repo,
159160
Number: pr,
160161
DeleteBranch: *prune,
162+
Squash: *squash,
161163
}
162164
if at := ts.Getenv("GIT_COMMITTER_DATE"); at != "" {
163165
t, err := time.Parse(time.RFC3339, at)

internal/forge/shamhub/merge.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ type MergeChangeRequest struct {
105105
// should be deleted after the merge.
106106
DeleteBranch bool
107107

108-
// TODO: option to use squash commit
108+
// Squash requests that the CR be merged
109+
// as a single squashed commit with the PR subject/body
110+
// instead of a merge commit.
111+
Squash bool
109112
}
110113

111114
// MergeChange merges an open change against this forge.
@@ -180,14 +183,22 @@ func (sh *ShamHub) MergeChange(req MergeChangeRequest) error {
180183
logw, flush := ioutil.LogWriter(sh.log, log.DebugLevel)
181184
defer flush()
182185

183-
msg := fmt.Sprintf("Merge change #%d", req.Number)
184-
cmd := exec.Command(sh.gitExe,
185-
"commit-tree",
186-
"-p", sh.changes[changeIdx].Base,
187-
"-p", sh.changes[changeIdx].Head,
188-
"-m", msg,
189-
tree,
190-
)
186+
change := sh.changes[changeIdx]
187+
188+
var msg string
189+
args := []string{"commit-tree", "-p", change.Base}
190+
if req.Squash {
191+
msg = fmt.Sprintf("%s (#%d)\n\n%s",
192+
change.Subject,
193+
req.Number,
194+
change.Body)
195+
} else {
196+
msg = fmt.Sprintf("Merge change #%d", req.Number)
197+
args = append(args, "-p", change.Head)
198+
}
199+
args = append(args, "-m", msg, tree)
200+
201+
cmd := exec.Command(sh.gitExe, args...)
191202
cmd.Dir = sh.repoDir(req.Owner, req.Repo)
192203
cmd.Stderr = logw
193204
cmd.Env = append(os.Environ(), commitEnv...)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# 'repo sync' handles squash-merged CRs correctly.
2+
3+
as 'Test <[email protected]>'
4+
at '2024-12-28T18:14:15Z'
5+
6+
# setup
7+
cd repo
8+
git init
9+
git commit --allow-empty -m 'Initial commit'
10+
11+
# set up a fake GitHub remote
12+
shamhub init
13+
shamhub new origin alice/example.git
14+
shamhub register alice
15+
git push origin main
16+
17+
env SHAMHUB_USERNAME=alice
18+
gs auth login
19+
20+
# put all three changes in one commit
21+
git add feat1.txt
22+
gs bc -m 'Add feature 1'
23+
git add feat2.txt
24+
gs cc -m 'Add feature 2'
25+
git add feat3.txt
26+
gs cc -m 'Add feature 3'
27+
28+
# Submit
29+
env ROBOT_INPUT=$WORK/robot.golden ROBOT_OUTPUT=$WORK/robot.actual
30+
gs branch submit
31+
cmp $WORK/robot.actual $WORK/robot.golden
32+
stderr 'Created #'
33+
34+
shamhub dump change 1
35+
cmpenvJSON stdout $WORK/golden/pull.txt
36+
37+
gs ll
38+
cmp stderr $WORK/golden/ll-before.txt
39+
40+
shamhub merge --squash --prune alice/example 1
41+
gs repo sync
42+
stderr '#1 was merged'
43+
44+
git log -n1 --pretty=%B
45+
cmp stdout $WORK/golden/final-commit-msg
46+
47+
gs ll
48+
cmp stderr $WORK/golden/ll-after.txt
49+
50+
-- repo/feat1.txt --
51+
feature 1
52+
-- repo/feat2.txt --
53+
feature 2
54+
-- repo/feat3.txt --
55+
feature 3
56+
57+
-- robot.golden --
58+
===
59+
> Title: Add feature 1
60+
> Short summary of the change
61+
"Add multiple features"
62+
===
63+
> Body: Press [e] to open mockedit or [enter/tab] to skip
64+
> Open your editor to write a detailed description of the change
65+
{"give": "Adds features 1, 2, and 3 in one PR."}
66+
===
67+
> Draft: [y/N]
68+
> Mark the change as a draft?
69+
false
70+
-- golden/pull.txt --
71+
{
72+
"number": 1,
73+
"html_url": "$SHAMHUB_URL/alice/example/change/1",
74+
"state": "open",
75+
"title": "Add multiple features",
76+
"body": "Adds features 1, 2, and 3 in one PR.",
77+
"base": {
78+
"ref": "main",
79+
"sha": "e41b284d1e80e3acba627274a80d94129ebdf8fd"
80+
},
81+
"head": {
82+
"ref": "add-feature-1",
83+
"sha": "a9548da1d2feba06f2bba402eb77adf27a9266e7"
84+
}
85+
}
86+
87+
-- golden/ll-before.txt --
88+
┏━■ add-feature-1 (#1) ◀
89+
┃ a9548da Add feature 3 (now)
90+
┃ bcb3a72 Add feature 2 (now)
91+
┃ 4be7a61 Add feature 1 (now)
92+
main
93+
-- golden/ll-after.txt --
94+
main ◀
95+
-- golden/final-commit-msg --
96+
Add multiple features (#1)
97+
98+
Adds features 1, 2, and 3 in one PR.
99+

0 commit comments

Comments
 (0)