Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ ghq.<url>.vcs::
remote repository. The URL is matched against '<url>' using 'git config --get-urlmatch'. +
Accepted values are "git", "github" (an alias for "git"), "subversion",
"svn" (an alias for "subversion"), "git-svn", "mercurial", "hg" (an alias for "mercurial"),
"darcs", "fossil", "bazaar", and "bzr" (an alias for "bazaar"). +
"darcs", "fossil", "bazaar", "bzr" (an alias for "bazaar"), "p4", and "perforce"
(an alias for "p4"; see <<perforce,PERFORCE>>). +
To get this configuration variable effective, you will need Git 1.8.5 or higher.

ghq.<url>.root::
Expand Down Expand Up @@ -196,6 +197,28 @@ Local repositories are placed under 'ghq.root' with named github.com/_user_/_rep
....


== [[perforce]]PERFORCE

Perforce depot paths are checked out with `p4://` URLs. The port defaults to 1666; use
`p4+ssl://` for servers whose P4PORT is `ssl:host:port`.

$ ghq get p4://p4.example.com:1666/depot/proj/main
# Creates a client workspace that maps //depot/proj/main/... to
# ~/ghq/p4.example.com/depot/proj/main and runs `p4 sync` in it

The client is named `<user>_<hostname>_<depot path>_<hash>` and its root is the local
directory. ghq writes the server and the client to `.p4config` in the workspace, which
`ghq get -u` and `ghq list` use; set `P4CONFIG=.p4config` to let `p4` pick it up as well.
Log in with `p4 login` beforehand. A user in the URL (`p4://alice@p4.example.com/...`)
is passed to `p4` with `-u` and recorded in `.p4config`. If `p4 sync` fails, the workspace
is kept and `ghq get -u` resumes it; `ghq get -u` refuses to sync a client that no longer
has the workspace as its root.

`--branch`, `--bare` and `ghq create` are not supported for Perforce, and `--shallow`,
`--partial` and `--no-recursive` have no effect. `ghq migrate` updates the root of the
client to the new location. `ghq rm` removes only the local directory; run
`p4 client -d <client>` to delete the client on the server.

== [[installing]]INSTALLATION

=== macOS
Expand Down
4 changes: 4 additions & 0 deletions cmd_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func TestDoCreate(t *testing.T) {
name: "unsupported VCS",
input: []string{"create", "--vcs=svn", "motemen/ghq-svn"},
errStr: "unsupported VCS",
}, {
name: "Perforce",
input: []string{"create", "--vcs=p4", "motemen/ghq-p4"},
errStr: "unsupported VCS",
}, {
name: "not permitted",
input: []string{"create", "motemen/ghq-notpermitted"},
Expand Down
122 changes: 122 additions & 0 deletions cmd_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,125 @@ func TestDoGet_parallelLook(t *testing.T) {
})
}
}

func TestDoGet_p4(t *testing.T) {
root := newTempDir(t)
setRepositoryPathTestRoot(t, root)
const url = "p4://p4.example.com:1666/depot/proj/main"
dir := filepath.Join(root, "p4.example.com", "depot", "proj", "main")
client := p4ClientName("alice", "myhost", "p4.example.com:1666", "//depot/proj/main", dir)
calls := fakeP4(t, newP4FakeServer().respond)
run := func(args ...string) (string, error) {
var err error
out, _, _ := captureWithInput([]string{"y"}, func() {
err = newApp().Run(context.Background(), append([]string{"ghq"}, args...))
})
return out, err
}

if _, err := run("get", url); err != nil {
t.Fatal(err)
}
if got, expect := readFileString(t, filepath.Join(dir, p4ConfigName)), "P4PORT=p4.example.com:1666\nP4CLIENT="+client+"\n"; got != expect {
t.Errorf(".p4config:\ngot: %q\nexpect: %q", got, expect)
}
if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
t.Error(err)
}

*calls = nil
seen.Delete(dir) // each path is fetched once per process
if _, err := run("get", "-u", url); err != nil {
t.Fatal(err)
}
expect := []p4Call{
{args: []string{"p4", "-p", "p4.example.com:1666", "-c", client, "client", "-o", client}},
{args: []string{"p4", "-p", "p4.example.com:1666", "-c", client, "sync"}, dir: dir},
}
if !reflect.DeepEqual(*calls, expect) {
t.Errorf("update:\ngot: %+v\nexpect: %+v", *calls, expect)
}

for _, args := range [][]string{{"list"}, {"list", "--vcs", "p4"}, {"list", "-p"}} {
out, err := run(args...)
if err != nil {
t.Fatal(err)
}
want := "p4.example.com/depot/proj/main"
if slices.Contains(args, "-p") {
want = dir
}
if strings.TrimSpace(out) != filepath.FromSlash(want) && strings.TrimSpace(out) != want {
t.Errorf("%v: got %q, expect %q", args, out, want)
}
}

*calls = nil
for _, args := range [][]string{
{"get", "p4://p4.example.com:1666/depot/proj/label@label"},
{"get", "--branch", "dev", "p4://p4.example.com/depot/proj/dev"},
{"get", "--bare", "p4://p4.example.com/depot/proj/bare"},
} {
if _, err := run(args...); err == nil {
t.Errorf("%v: error should be occurred, but nil", args)
}
}
if len(*calls) != 0 {
t.Errorf("p4 should not be run for rejected options: %+v", *calls)
}

if _, err := run("rm", url); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Errorf("%s should be removed: %v", dir, err)
}
}

func TestDoGet_p4Revision(t *testing.T) {
root := newTempDir(t)
setRepositoryPathTestRoot(t, root)
calls := fakeP4(t, newP4FakeServer().respond)
for _, u := range []string{"p4://p4.example.com/depot/rev#123", "p4://p4.example.com/depot/rev2%23123", "p4://p4.example.com/depot/rev3@123"} {
var err error
capture(func() { err = newApp().Run(context.Background(), []string{"ghq", "get", u}) })
if err == nil {
t.Errorf("%s: error should be occurred, but nil", u)
}
}
if len(*calls) != 0 {
t.Errorf("p4 should not be run: %+v", *calls)
}
for _, name := range []string{"rev", "rev2#123", "rev3"} {
if _, err := os.Stat(filepath.Join(root, "p4.example.com", "depot", name)); !os.IsNotExist(err) {
t.Errorf("no workspace should be created: %v", err)
}
}
}

func TestDoGet_p4UpdateRevision(t *testing.T) {
root := newTempDir(t)
setRepositoryPathTestRoot(t, root)
calls := fakeP4(t, newP4FakeServer().respond)
dir := filepath.Join(root, "p4.example.com", "depot", "proj")
run := func(args ...string) error {
var err error
capture(func() { err = newApp().Run(context.Background(), append([]string{"ghq"}, args...)) })
return err
}
if err := run("get", "p4://p4.example.com/depot/proj"); err != nil {
t.Fatal(err)
}
for _, u := range []string{"p4://p4.example.com/depot/proj@123", "p4://p4.example.com/depot/proj%40123"} {
*calls = nil
seen.Delete(dir)
if err := run("get", "-u", u); err == nil {
t.Errorf("%s: error should be occurred, but nil", u)
}
for _, c := range *calls {
if slices.Contains(c.args, "sync") {
t.Errorf("%s: should not sync: %v", u, c.args)
}
}
}
}
38 changes: 38 additions & 0 deletions cmd_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func TestDoList_query(t *testing.T) {
svnRepos := []string{
"github.com/msh5/svntest",
}
p4Repos := []string{
"p4.example.com/depot/proj/main",
}
testCases := []struct {
name string
args []string
Expand Down Expand Up @@ -112,6 +115,14 @@ func TestDoList_query(t *testing.T) {
name: "vcs",
args: []string{"--vcs", "svn"},
expect: "github.com/msh5/svntest\n",
}, {
name: "vcs p4",
args: []string{"--vcs", "p4"},
expect: "p4.example.com/depot/proj/main\n",
}, {
name: "p4 URL",
args: []string{"p4://p4.example.com:1666/depot/proj/main"},
expect: "p4.example.com/depot/proj/main\n",
}, {
name: "smartcasing fuzzy",
args: []string{"awesome"},
Expand All @@ -133,6 +144,12 @@ func TestDoList_query(t *testing.T) {
for _, r := range svnRepos {
os.MkdirAll(filepath.Join(tmproot, r, ".svn"), 0755)
}
for _, r := range p4Repos {
os.MkdirAll(filepath.Join(tmproot, r, "src"), 0755)
os.WriteFile(filepath.Join(tmproot, r, ".p4config"), []byte("P4PORT=1666\nP4CLIENT=ws\n"), 0644)
// a nested .p4config in the workspace is not a separate repository
os.WriteFile(filepath.Join(tmproot, r, "src", ".p4config"), []byte("P4PORT=1666\n"), 0644)
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := append([]string{"ghq", "list"}, tc.args...)
Expand Down Expand Up @@ -305,3 +322,24 @@ func TestDoList_withSystemHiddenDir(t *testing.T) {
t.Errorf("error should be nil, but: %v", err)
}
}

func TestDoList_sharedP4config(t *testing.T) {
root := newTempDir(t)
setRepositoryPathTestRoot(t, root)
// A shared .p4config (no client) above repositories is not a workspace.
os.MkdirAll(filepath.Join(root, "p4.example.com", "team", "app", ".git"), 0755)
os.WriteFile(filepath.Join(root, "p4.example.com", ".p4config"), []byte("P4PORT=p4.example.com:1666\n"), 0644)
os.WriteFile(filepath.Join(root, ".p4config"), []byte("P4PORT=1666\n"), 0644)
// Neither are .p4config files naming a client above the <host>/<depot>
// level, where ghq never creates workspaces.
os.MkdirAll(filepath.Join(root, "p4.other.example.com", "team", "lib", ".git"), 0755)
os.WriteFile(filepath.Join(root, "p4.other.example.com", ".p4config"), []byte("P4PORT=p4.other.example.com:1666\nP4CLIENT=ws\n"), 0644)
out, _, _ := capture(func() {
if err := newApp().Run(context.Background(), []string{"ghq", "list"}); err != nil {
t.Error(err)
}
})
if out != "p4.example.com/team/app\np4.other.example.com/team/lib\n" {
t.Errorf("got %q", out)
}
}
24 changes: 24 additions & 0 deletions cmd_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ func doMigrate(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("failed to check destination directory: %w", err)
}

// A p4 client records its root on the server; check that it is absDir.
var p4Move *p4RootMove
if vcsBackend == PerforceBackend {
if p4Move, err = p4PrepareRootMove(absDir); err != nil {
return err
}
}

// Check for linked worktrees before dry-run return so we can report them
var hasWorktrees bool
if vcsBackend == GitBackend {
Expand All @@ -106,6 +114,9 @@ func doMigrate(ctx context.Context, cmd *cli.Command) error {
if hasWorktrees {
fmt.Fprintf(w, "Would run 'git worktree repair' to update linked worktrees\n")
}
if vcsBackend == PerforceBackend {
fmt.Fprintf(w, "Would update the root of the p4 client\n")
}
return nil
}

Expand Down Expand Up @@ -155,6 +166,19 @@ func doMigrate(ctx context.Context, cmd *cli.Command) error {
}
}

// The workspace is unusable until its client points at the new location.
if p4Move != nil {
if err := p4Move.apply(destPath); err != nil {
if !errors.Is(err, errP4RootUnchanged) {
return fmt.Errorf("could not confirm that the root of the p4 client was updated, the workspace is at %s: %w", destPath, err)
}
if mvErr := moveDir(destPath, absDir); mvErr != nil {
return fmt.Errorf("failed to update the root of the p4 client: %w; the workspace is at %s (moving it back failed: %v)", err, destPath, mvErr)
}
return fmt.Errorf("failed to update the root of the p4 client, moved the workspace back: %w", err)
}
}

fmt.Fprintln(w, destPath)
return nil
}
Expand Down
Loading