Skip to content

Commit 21babae

Browse files
committed
fixup! feat: add fork remote command
Add integration test. Requires extending replaceForkUsername to support the local relative paths we use for remotes in integration tests.
1 parent 0400475 commit 21babae

File tree

5 files changed

+78
-6
lines changed

5 files changed

+78
-6
lines changed

pkg/gui/controllers/remotes_controller.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,19 @@ func (self *RemotesController) add() error {
216216
// 1. SCP-like SSH: git@host:owner[/subgroups]/repo(.git)
217217
// 2. SSH URL style: ssh://user@host[:port]/owner[/subgroups]/repo(.git)
218218
// 3. HTTPS: https://host/owner[/subgroups]/repo(.git)
219-
var urlRegex = regexp.MustCompile(`^(git@[^:]+:|ssh://[^/]+/|https?://[^/]+/)([^/]+(?:/[^/]+)*)/([^/]+?)(\.git)?$`)
219+
// 4. Only for integration tests: ../repo_name
220+
var (
221+
urlRegex = regexp.MustCompile(`^(git@[^:]+:|ssh://[^/]+/|https?://[^/]+/)([^/]+(?:/[^/]+)*)/([^/]+?)(\.git)?$`)
222+
integrationTestUrlRegex = regexp.MustCompile(`^\.\./.+$`)
223+
)
220224

221225
// Rewrites a Git remote URL to use the given fork username,
222226
// keeping the repo name and host intact. Supports SCP-like SSH, SSH URL style, and HTTPS.
223-
func replaceForkUsername(originUrl, forkUsername string) (string, error) {
227+
func replaceForkUsername(originUrl, forkUsername string, isIntegrationTest bool) (string, error) {
224228
if urlRegex.MatchString(originUrl) {
225229
return urlRegex.ReplaceAllString(originUrl, "${1}"+forkUsername+"/$3$4"), nil
230+
} else if isIntegrationTest && integrationTestUrlRegex.MatchString(originUrl) {
231+
return "../" + forkUsername, nil
226232
}
227233

228234
return "", fmt.Errorf("unsupported or invalid remote URL: %s", originUrl)
@@ -261,7 +267,7 @@ func (self *RemotesController) addFork() error {
261267
branchToCheckout = parts[1]
262268
}
263269
originUrl := origin.Urls[0]
264-
remoteUrl, err := replaceForkUsername(originUrl, forkUsername)
270+
remoteUrl, err := replaceForkUsername(originUrl, forkUsername, self.c.RunningIntegrationTest())
265271
if err != nil {
266272
return err
267273
}

pkg/gui/controllers/remotes_controller_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestReplaceForkUsername_SSH_OK(t *testing.T) {
5353

5454
for _, c := range cases {
5555
t.Run(c.name, func(t *testing.T) {
56-
got, err := replaceForkUsername(c.in, c.forkUser)
56+
got, err := replaceForkUsername(c.in, c.forkUser, false)
5757
assert.NoError(t, err)
5858
assert.Equal(t, c.expected, got)
5959
})
@@ -95,13 +95,19 @@ func TestReplaceForkUsername_HTTPS_OK(t *testing.T) {
9595

9696
for _, c := range cases {
9797
t.Run(c.name, func(t *testing.T) {
98-
got, err := replaceForkUsername(c.in, c.forkUser)
98+
got, err := replaceForkUsername(c.in, c.forkUser, false)
9999
assert.NoError(t, err)
100100
assert.Equal(t, c.expected, got)
101101
})
102102
}
103103
}
104104

105+
func TestReplaceForkUsername_IntegrationTest_OK(t *testing.T) {
106+
got, err := replaceForkUsername("../origin", "bob", true)
107+
assert.NoError(t, err)
108+
assert.Equal(t, "../bob", got)
109+
}
110+
105111
func TestReplaceForkUsername_Errors(t *testing.T) {
106112
cases := []struct {
107113
name string
@@ -143,11 +149,16 @@ func TestReplaceForkUsername_Errors(t *testing.T) {
143149
in: "",
144150
forkUser: "x",
145151
},
152+
{
153+
name: "integration test URL outside of integration test",
154+
in: "../origin",
155+
forkUser: "x",
156+
},
146157
}
147158

148159
for _, c := range cases {
149160
t.Run(c.name, func(t *testing.T) {
150-
_, err := replaceForkUsername(c.in, c.forkUser)
161+
_, err := replaceForkUsername(c.in, c.forkUser, false)
151162
assert.EqualError(t, err, "unsupported or invalid remote URL: "+c.in)
152163
})
153164
}

pkg/integration/components/shell.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,12 @@ func (self *Shell) SetBranchUpstream(branch string, upstream string) *Shell {
392392
return self
393393
}
394394

395+
func (self *Shell) RemoveBranch(branch string) *Shell {
396+
self.RunCommand([]string{"git", "branch", "-d", branch})
397+
398+
return self
399+
}
400+
395401
func (self *Shell) RemoveRemoteBranch(remoteName string, branch string) *Shell {
396402
self.RunCommand([]string{"git", "-C", "../" + remoteName, "branch", "-d", branch})
397403

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package remote
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var AddForkRemote = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Use the 'Add fork remote' command to add a fork remote and check out a branch from it",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.EmptyCommit("commit")
15+
shell.CloneIntoRemote("origin")
16+
shell.NewBranch("feature")
17+
shell.Clone("fork")
18+
shell.Checkout("master")
19+
shell.RemoveBranch("feature")
20+
},
21+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
22+
t.Views().Remotes().
23+
Focus().
24+
Lines(
25+
Contains("origin").IsSelected(),
26+
).
27+
Press(keys.Branches.AddForkRemote)
28+
29+
t.ExpectPopup().Prompt().
30+
Title(Equals("Fork owner (username/org). Use username:branch to check out a branch")).
31+
Type("fork:feature").
32+
Confirm()
33+
34+
t.Views().Remotes().
35+
Lines(
36+
Contains("origin"),
37+
Contains("fork").IsSelected(),
38+
)
39+
40+
t.Views().Branches().
41+
IsFocused().
42+
Lines(
43+
Contains("feature ✓"),
44+
Contains("master"),
45+
)
46+
},
47+
})

pkg/integration/tests/test_list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jesseduffield/lazygit/pkg/integration/tests/misc"
2222
"github.com/jesseduffield/lazygit/pkg/integration/tests/patch_building"
2323
"github.com/jesseduffield/lazygit/pkg/integration/tests/reflog"
24+
"github.com/jesseduffield/lazygit/pkg/integration/tests/remote"
2425
"github.com/jesseduffield/lazygit/pkg/integration/tests/shell_commands"
2526
"github.com/jesseduffield/lazygit/pkg/integration/tests/staging"
2627
"github.com/jesseduffield/lazygit/pkg/integration/tests/stash"
@@ -351,6 +352,7 @@ var tests = []*components.IntegrationTest{
351352
reflog.DoNotShowBranchMarkersInReflogSubcommits,
352353
reflog.Patch,
353354
reflog.Reset,
355+
remote.AddForkRemote,
354356
shell_commands.BasicShellCommand,
355357
shell_commands.ComplexShellCommand,
356358
shell_commands.DeleteFromHistory,

0 commit comments

Comments
 (0)