From 7ed52ffcb81510679083d05628fe716cd1fca40f Mon Sep 17 00:00:00 2001 From: t04dJ14n9 <61448324+t04dJ14n9@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:38:58 +0800 Subject: [PATCH 1/4] Support Perforce (p4) Add a Perforce backend for p4://host[:port]/depot/path and p4+ssl://host[:port]/depot/path URLs (default port 1666), also selectable with --vcs=p4 or ghq..vcs = p4. "ghq get" creates a client whose root is the local directory and whose view maps //depot/path/... into it, records P4PORT/P4CLIENT (and P4USER from the URL) in .p4config, and runs "p4 sync". "ghq get -u" runs "p4 sync" with the server and client from .p4config passed explicitly. .p4config marks the workspace for "ghq list". "ghq migrate" updates the root of the client. Client names are ___; an existing client is reused only when its root is the target directory. Co-Authored-By: Claude Opus 5.5 --- README.adoc | 23 +- cmd_create_test.go | 4 + cmd_get_test.go | 76 +++++ cmd_list_test.go | 17 + cmd_migrate.go | 10 + cmd_migrate_test.go | 56 ++++ local_repository.go | 2 + local_repository_test.go | 33 ++ logger/log.go | 1 + misc/bash/_ghq | 2 +- misc/fish/ghq.fish | 1 + misc/zsh/_ghq | 6 +- remote_repository.go | 3 +- remote_repository_test.go | 8 + url_test.go | 10 + vcs.go | 2 + vcs_p4.go | 508 +++++++++++++++++++++++++++++ vcs_test.go | 668 ++++++++++++++++++++++++++++++++++++++ 18 files changed, 1424 insertions(+), 6 deletions(-) create mode 100644 vcs_p4.go diff --git a/README.adoc b/README.adoc index 949d1ce4..54202385 100644 --- a/README.adoc +++ b/README.adoc @@ -127,7 +127,8 @@ ghq..vcs:: remote repository. The URL is matched against '' 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 <>). + To get this configuration variable effective, you will need Git 1.8.5 or higher. ghq..root:: @@ -196,6 +197,26 @@ 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 `___` 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`. + +`--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 ` to delete the client on the server. + == [[installing]]INSTALLATION === macOS diff --git a/cmd_create_test.go b/cmd_create_test.go index 06c179f5..ec9638e1 100644 --- a/cmd_create_test.go +++ b/cmd_create_test.go @@ -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"}, diff --git a/cmd_get_test.go b/cmd_get_test.go index 14ad3285..04dc0c5e 100644 --- a/cmd_get_test.go +++ b/cmd_get_test.go @@ -714,3 +714,79 @@ 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, func(sub string, cmd *exec.Cmd) error { + if sub == "sync" || sub == "sync -f" { + return os.WriteFile(filepath.Join(cmd.Dir, "README"), []byte("hello\n"), 0644) + } + return nil + }) + 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, "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) + } +} diff --git a/cmd_list_test.go b/cmd_list_test.go index aa8ef52c..1d8446b7 100644 --- a/cmd_list_test.go +++ b/cmd_list_test.go @@ -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 @@ -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"}, @@ -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\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...) diff --git a/cmd_migrate.go b/cmd_migrate.go index 31a68124..a0a5f845 100644 --- a/cmd_migrate.go +++ b/cmd_migrate.go @@ -106,6 +106,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 } @@ -155,6 +158,13 @@ func doMigrate(ctx context.Context, cmd *cli.Command) error { } } + // A p4 client spec records its workspace root on the server. + if vcsBackend == PerforceBackend { + if err := p4UpdateClientRoot(destPath); err != nil { + logger.Log("warning", fmt.Sprintf("failed to update the root of the p4 client to %s: %v", destPath, err)) + } + } + fmt.Fprintln(w, destPath) return nil } diff --git a/cmd_migrate_test.go b/cmd_migrate_test.go index 5f9ffb3c..5ad142ed 100644 --- a/cmd_migrate_test.go +++ b/cmd_migrate_test.go @@ -2,9 +2,12 @@ package main import ( "context" + "fmt" "os" "os/exec" "path/filepath" + "reflect" + "slices" "strings" "sync" "testing" @@ -457,3 +460,56 @@ func TestIsLinkedGitDir(t *testing.T) { } }) } + +func TestDoMigrate_p4(t *testing.T) { + tmpdir := newTempDir(t) + setRepositoryPathTestRoot(t, tmpdir) + calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { + if sub == "client -o ws" { + fmt.Fprint(cmd.Stdout, p4TestSpec("ws", "/old/root", true, "//depot/proj/main/... //ws/...")) + } + return nil + }) + newSrc := func(name string) string { + src := filepath.Join(tmpdir, "sources", name) + os.MkdirAll(src, 0755) + if err := writeP4Config(src, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { + t.Fatal(err) + } + return src + } + dest := filepath.Join(tmpdir, "p4.example.com", "depot", "proj", "main") + + t.Run("dry-run", func(t *testing.T) { + src := newSrc("dry") + out, _, _ := capture(func() { + if err := newApp().Run(context.Background(), []string{"ghq", "migrate", "--dry-run", src}); err != nil { + t.Fatal(err) + } + }) + if !strings.Contains(out, dest) || !strings.Contains(out, "p4 client") { + t.Errorf("unexpected output: %s", out) + } + for _, c := range *calls { + if slices.Contains(c.args, "-i") { + t.Errorf("dry-run should not modify the client: %v", c.args) + } + } + }) + + t.Run("migrate", func(t *testing.T) { + *calls = nil + src := newSrc("proj") + if err := newApp().Run(context.Background(), []string{"ghq", "migrate", "-y", src}); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(dest, p4ConfigName)); err != nil { + t.Fatal(err) + } + last := (*calls)[len(*calls)-1] + if !reflect.DeepEqual(last.args, []string{"p4", "-p", "p4.example.com:1666", "-c", "ws", "client", "-i"}) || + !strings.Contains(last.stdin, "\nRoot:\t"+dest+"\n") { + t.Errorf("client root should be updated, got %v:\n%s", last.args, last.stdin) + } + }) +} diff --git a/local_repository.go b/local_repository.go index 4224710c..b1b97355 100644 --- a/local_repository.go +++ b/local_repository.go @@ -275,6 +275,7 @@ var vcsContentsMap = map[string]*VCSBackend{ ".fslckout": FossilBackend, // file "_FOSSIL_": FossilBackend, // file "CVS/Repository": cvsDummyBackend, + ".p4config": PerforceBackend, // file } var vcsContents = [...]string{ @@ -287,6 +288,7 @@ var vcsContents = [...]string{ ".fslckout", "._FOSSIL_", "CVS/Repository", + ".p4config", } func findVCSBackend(fpath, vcs string) *VCSBackend { diff --git a/local_repository_test.go b/local_repository_test.go index ebb6459a..7c22aa70 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -116,6 +116,14 @@ func TestNewLocalRepository(t *testing.T) { name: "bitbucket host with port", url: "https://bitbucket.local:8888/motemen/ghq.git", expect: filepath.Join(tmproot, "bitbucket.local/motemen/ghq"), + }, { + name: "p4 with port", + url: "p4://p4.example.com:1666/depot/proj/main", + expect: filepath.Join(tmproot, "p4.example.com/depot/proj/main"), + }, { + name: "p4 ssl with user", + url: "p4+ssl://alice@p4.example.com/depot/proj/main/", + expect: filepath.Join(tmproot, "p4.example.com/depot/proj/main"), }} for _, tc := range testCases { @@ -360,6 +368,31 @@ func TestFindVCSBackend(t *testing.T) { return dir, "mercurial" }, expect: nil, + }, { + name: "p4", + setup: func(t *testing.T) (string, string) { + dir := newTempDir(t) + os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\n"), 0644) + return dir, "" + }, + expect: PerforceBackend, + }, { + name: "p4 with matched vcs", + setup: func(t *testing.T) (string, string) { + dir := newTempDir(t) + os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\n"), 0644) + return dir, "p4" + }, + expect: PerforceBackend, + }, { + name: "git with .p4config", + setup: func(t *testing.T) (string, string) { + dir := newTempDir(t) + os.MkdirAll(filepath.Join(dir, ".git"), 0755) + os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\n"), 0644) + return dir, "" + }, + expect: GitBackend, }} for _, tc := range testCases { diff --git a/logger/log.go b/logger/log.go index 8fed2795..dfe32ca0 100644 --- a/logger/log.go +++ b/logger/log.go @@ -28,6 +28,7 @@ var ( "pijul": VerboseColor, "bzr": VerboseColor, "fossil": VerboseColor, + "p4": VerboseColor, "skip": VerboseColor, "cd": VerboseColor, "resolved": VerboseColor, diff --git a/misc/bash/_ghq b/misc/bash/_ghq index 483b856f..34d4081a 100644 --- a/misc/bash/_ghq +++ b/misc/bash/_ghq @@ -10,7 +10,7 @@ _ghq() { return 0 fi - local vcs_backends="git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar" + local vcs_backends="git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar p4 perforce" case "${words[1]}" in get|clone) diff --git a/misc/fish/ghq.fish b/misc/fish/ghq.fish index 3ee5ca95..a97c5426 100644 --- a/misc/fish/ghq.fish +++ b/misc/fish/ghq.fish @@ -72,3 +72,4 @@ complete -c ghq -n '__fish_seen_subcommand_from get clone list create' -n '__fis complete -c ghq -n '__fish_seen_subcommand_from get clone list create' -n '__fish_seen_argument --vcs' -l vcs -x -a pijul -d pijul complete -c ghq -n '__fish_seen_subcommand_from get clone list create' -n '__fish_seen_argument --vcs' -l vcs -x -a fossil -d fossil complete -c ghq -n '__fish_seen_subcommand_from get clone list create' -n '__fish_seen_argument --vcs' -l vcs -x -a 'bzr bazaar' -d bazaar +complete -c ghq -n '__fish_seen_subcommand_from get clone list create' -n '__fish_seen_argument --vcs' -l vcs -x -a 'p4 perforce' -d perforce diff --git a/misc/zsh/_ghq b/misc/zsh/_ghq index dec37fd3..a0e9a644 100644 --- a/misc/zsh/_ghq +++ b/misc/zsh/_ghq @@ -21,7 +21,7 @@ function _ghq () { '-p[Clone with SSH]' \ '--shallow[Do a shallow clone]' \ '(-l --look)'{-l,--look}'[Look after get]' \ - '--vcs[Specify vcs backend for cloning]: :(git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar)' \ + '--vcs[Specify vcs backend for cloning]: :(git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar p4 perforce)' \ '(-s --silent)'{-s,--silent}'[Clone or update silently]' \ '--no-recursive[Prevent recursive fetching]' \ '--bare[Do a bare clone]' \ @@ -37,7 +37,7 @@ function _ghq () { (list) _arguments -C \ '(-e --exact)'{-e,--exact}'[Perform an exact match]' \ - '--vcs[Specify vcs backend for matching]: :(git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar)' \ + '--vcs[Specify vcs backend for matching]: :(git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar p4 perforce)' \ '(-p --full-path)'{-p,--full-path}'[Print full paths]' \ '--unique[Print unique subpaths]' \ '--bare[Query bare repositories]' \ @@ -52,7 +52,7 @@ function _ghq () { ;; (create) _arguments -C \ - '--vcs[Specify vcs backend explicitly]: :(git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar)' \ + '--vcs[Specify vcs backend explicitly]: :(git github codecommit svn subversion git-svn hg mercurial darcs pijul fossil bzr bazaar p4 perforce)' \ '--bare[Create a bare repository]' \ '(-)*:: :->null_state' \ && ret=0 diff --git a/remote_repository.go b/remote_repository.go index 70b8f8c0..1d0c8c4b 100644 --- a/remote_repository.go +++ b/remote_repository.go @@ -173,12 +173,13 @@ func (repo *OtherRepository) IsValid() bool { } var ( - vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr|codecommit)(?:\+|$)`) + vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr|codecommit|p4)(?:\+|$)`) scheme2vcs = map[string]*VCSBackend{ "git": GitBackend, "codecommit": GitBackend, "svn": SubversionBackend, "bzr": BazaarBackend, + "p4": PerforceBackend, } ) diff --git a/remote_repository_test.go b/remote_repository_test.go index 4bd7ddcd..e90a75c2 100644 --- a/remote_repository_test.go +++ b/remote_repository_test.go @@ -43,6 +43,14 @@ func TestNewRemoteRepository(t *testing.T) { url: "svn+ssh://example.com/proj/repo", valid: true, vcsBackend: SubversionBackend, + }, { + url: "p4://p4.example.com:1666/depot/proj/main", + valid: true, + vcsBackend: PerforceBackend, + }, { + url: "p4+ssl://p4.example.com/depot/proj/main", + valid: true, + vcsBackend: PerforceBackend, }} for _, tc := range testCases { diff --git a/url_test.go b/url_test.go index 65c81a3e..d4c28aa5 100644 --- a/url_test.go +++ b/url_test.go @@ -75,6 +75,16 @@ defaultHost = gitlab.com`)) url: "gnuwget/wget2", expect: "https://gitlab.com/gnuwget/wget2", host: "gitlab.com", + }, { + name: "p4", + url: "p4://p4.example.com:1666/depot/proj/main", + expect: "p4://p4.example.com:1666/depot/proj/main", + host: "p4.example.com:1666", + }, { + name: "p4+ssl", + url: "p4+ssl://alice@p4.example.com/depot/proj/main", + expect: "p4+ssl://alice@p4.example.com/depot/proj/main", + host: "p4.example.com", }} for _, tc := range testCases { diff --git a/vcs.go b/vcs.go index c932282d..8dbb9ae1 100644 --- a/vcs.go +++ b/vcs.go @@ -573,4 +573,6 @@ var vcsRegistry = map[string]*VCSBackend{ "fossil": FossilBackend, "bzr": BazaarBackend, "bazaar": BazaarBackend, + "p4": PerforceBackend, + "perforce": PerforceBackend, } diff --git a/vcs_p4.go b/vcs_p4.go new file mode 100644 index 00000000..3eebd016 --- /dev/null +++ b/vcs_p4.go @@ -0,0 +1,508 @@ +package main + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "net" + "net/url" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "unicode" + + "github.com/x-motemen/ghq/cmdutil" + "github.com/x-motemen/ghq/logger" +) + +/* +Perforce (p4) workspaces are identified by URLs of the form + + p4://[user@]host[:port]/depot/path (P4PORT host:port, default port 1666) + p4+ssl://[user@]host[:port]/depot/path (P4PORT ssl:host:port) + +"ghq get" creates a client workspace whose root is the local directory and +whose view maps //depot/path/... into it, writes the connection settings to +.p4config in the workspace root, and runs "p4 sync". +*/ + +const ( + p4ConfigName = ".p4config" + p4DefaultPort = "1666" +) + +// p4Config holds the connection settings recorded in a workspace's .p4config. +type p4Config struct { + port, user, client string +} + +// args returns the p4 global options selecting the server, user and client, +// so that p4 never falls back to P4PORT/P4CLIENT from the environment. +func (c *p4Config) args(args ...string) []string { + ret := []string{"-p", c.port} + if c.user != "" { + ret = append(ret, "-u", c.user) + } + if c.client != "" { + ret = append(ret, "-c", c.client) + } + return append(ret, args...) +} + +func hasControlChar(s string) bool { + return strings.IndexFunc(s, unicode.IsControl) >= 0 +} + +// p4ConfigFromURL returns the connection settings and the depot path of a +// p4:// or p4+ssl:// URL. Other schemes (for example https:// with +// "ghq..vcs = p4") are treated as plain TCP. +func p4ConfigFromURL(u *url.URL) (*p4Config, string, error) { + host := u.Hostname() + if host == "" { + return nil, "", fmt.Errorf("missing p4 server host in URL %q", u) + } + if hasControlChar(host) || strings.ContainsAny(host, "/@") { + return nil, "", fmt.Errorf("invalid p4 server host in URL %q", u) + } + port := u.Port() + if port == "" { + port = p4DefaultPort + } + proto := "" + if u.Scheme == "p4+ssl" { + proto = "ssl" + } + if strings.Contains(host, ":") { // IPv6 literal + if proto == "" { + proto = "tcp" + } + proto += "6" + } + p4port := net.JoinHostPort(host, port) + if proto != "" { + p4port = proto + ":" + p4port + } + + cfg := &p4Config{port: p4port} + if u.User != nil { + if _, ok := u.User.Password(); ok { + return nil, "", fmt.Errorf("passwords in p4 URLs are not supported, use 'p4 login' instead: %q", u.Redacted()) + } + cfg.user = u.User.Username() + if hasControlChar(cfg.user) || strings.ContainsAny(cfg.user, " @#") { + return nil, "", fmt.Errorf("invalid p4 user in URL %q", u.Redacted()) + } + } + + depotPath, err := p4DepotPathFromURL(u) + if err != nil { + return nil, "", err + } + return cfg, depotPath, nil +} + +// p4DepotPathFromURL converts the URL path "/depot/proj/main" to the depot +// path "//depot/proj/main", rejecting paths that p4 would interpret as +// wildcards or revision specifiers, or that could not be written into a +// client spec safely. +func p4DepotPathFromURL(u *url.URL) (string, error) { + p := strings.TrimRight(u.Path, "/") + p = strings.TrimPrefix(p, "/") + if p == "" { + return "", fmt.Errorf("missing depot path in URL %q", u.Redacted()) + } + for _, part := range strings.Split(p, "/") { + switch { + case part == "", part == ".", part == "..": + return "", fmt.Errorf("invalid depot path in URL %q: empty or dot path components are not allowed", u.Redacted()) + case strings.Contains(part, "..."), strings.ContainsAny(part, `*@#%"`): + return "", fmt.Errorf("invalid depot path in URL %q: p4 wildcards and revision specifiers are not allowed", u.Redacted()) + case hasControlChar(part): + return "", fmt.Errorf("invalid depot path in URL %q: control characters are not allowed", u.Redacted()) + } + } + return "//" + p, nil +} + +var ( + p4NameUnsafeReg = regexp.MustCompile(`[^A-Za-z0-9_-]+`) + p4Hostname = os.Hostname // replaced in tests +) + +func p4NamePart(s string, maxLen int) string { + s = strings.Trim(p4NameUnsafeReg.ReplaceAllString(s, "_"), "_-") + if len(s) > maxLen { + s = strings.TrimRight(s[:maxLen], "_-") + } + return s +} + +// p4ClientName returns a deterministic client name of the form +// ___. Sanitizing can map different +// inputs to the same readable part, so the hash of the server, depot path +// and local root keeps names unique. +func p4ClientName(user, hostname, port, depotPath, root string) string { + sum := sha256.Sum256([]byte(port + "\x00" + depotPath + "\x00" + root)) + var parts []string + for _, p := range []string{ + p4NamePart(user, 32), p4NamePart(hostname, 32), p4NamePart(depotPath, 64), + } { + if p != "" { + parts = append(parts, p) + } + } + if len(parts) == 0 { + parts = append(parts, "ghq") + } + return strings.Join(append(parts, hex.EncodeToString(sum[:4])), "_") +} + +func p4LocalUser(u *url.URL) string { + if u.User != nil && u.User.Username() != "" { + return u.User.Username() + } + for _, env := range []string{"P4USER", "USER", "USERNAME"} { + if v := os.Getenv(env); v != "" { + return v + } + } + return "" +} + +// p4SpecField returns the value lines of a field in a p4 form such as the +// output of "p4 client -o". It returns nil if the field does not exist. +func p4SpecField(spec, name string) []string { + var values []string + found := false + for _, line := range strings.Split(spec, "\n") { + line = strings.TrimRight(line, "\r") + if found { + if strings.HasPrefix(line, "\t") || strings.HasPrefix(line, " ") { + if v := strings.TrimSpace(line); v != "" { + values = append(values, v) + } + continue + } + if line == "" { + continue + } + break + } + if v, ok := strings.CutPrefix(line, name+":"); ok { + found = true + values = []string{} + if v = strings.TrimSpace(v); v != "" { + values = append(values, v) + } + } + } + if !found { + return nil + } + return values +} + +// p4SetSpecField replaces the value of a field in a p4 form, or appends the +// field if the form does not have it. With multiLine, values are written as +// indented lines below the field name. +func p4SetSpecField(spec, name string, multiLine bool, values ...string) string { + field := []string{name + ":\t" + strings.Join(values, " ")} + if multiLine { + field = []string{name + ":"} + for _, v := range values { + field = append(field, "\t"+v) + } + } + var out []string + found, skipping := false, false + for _, line := range strings.Split(spec, "\n") { + line = strings.TrimRight(line, "\r") + if skipping { + if strings.HasPrefix(line, "\t") || strings.HasPrefix(line, " ") { + continue + } + skipping = false + } + if strings.HasPrefix(line, name+":") && !found { + found, skipping = true, true + out = append(out, field...) + continue + } + out = append(out, line) + } + if !found { + out = append(out, "") + out = append(out, field...) + } + return strings.Join(out, "\n") +} + +// p4View maps depotPath into the workspace root, excluding a .p4config in the +// depot so that it can neither clobber nor be clobbered by ghq's own. +func p4View(depotPath, client string) []string { + return []string{ + fmt.Sprintf(`"%s/..." "//%s/..."`, depotPath, client), + fmt.Sprintf(`"-%s/%s" "//%s/%s"`, depotPath, p4ConfigName, client, p4ConfigName), + } +} + +// p4ViewDepotPath returns the depot path of the first inclusive mapping of +// a client view, e.g. "//depot/proj" for `//depot/proj/... //client/...`. +func p4ViewDepotPath(view []string) (string, error) { + for _, line := range view { + var lhs string + if rest, ok := strings.CutPrefix(line, `"`); ok { + lhs, _, _ = strings.Cut(rest, `"`) + } else { + lhs, _, _ = strings.Cut(line, " ") + } + if !strings.HasPrefix(lhs, "//") { + continue // exclusion (-), overlay (+) or ditto (&) mapping + } + if p, ok := strings.CutSuffix(lhs, "/..."); ok && len(p) > 2 { + return p, nil + } + } + return "", fmt.Errorf("no depot path found in client view %q", view) +} + +// p4URLFromConfig rebuilds the p4:// URL of a workspace. +func p4URLFromConfig(cfg *p4Config, depotPath string) (*url.URL, error) { + port := cfg.port + scheme := "p4" + if proto, rest, ok := strings.Cut(port, ":"); ok { + switch proto { + case "tcp", "tcp4", "tcp6", "tcp46", "tcp64": + port = rest + case "ssl", "ssl4", "ssl6", "ssl46", "ssl64": + port, scheme = rest, "p4+ssl" + } + } + host, portNum, err := net.SplitHostPort(port) + if err != nil { + host, portNum = "localhost", port // P4PORT=1666 + } + if portNum == "" || strings.Trim(portNum, "0123456789") != "" { + return nil, fmt.Errorf("unsupported P4PORT %q", cfg.port) + } + if host == "" { + host = "localhost" + } + u := &url.URL{ + Scheme: scheme, + Host: net.JoinHostPort(host, portNum), + Path: "/" + strings.TrimLeft(depotPath, "/"), + } + if cfg.user != "" { + u.User = url.User(cfg.user) + } + return u, nil +} + +func readP4Config(dir string) (*p4Config, error) { + b, err := os.ReadFile(filepath.Join(dir, p4ConfigName)) + if err != nil { + return nil, err + } + cfg := &p4Config{} + for _, line := range strings.Split(string(b), "\n") { + key, value, ok := strings.Cut(strings.TrimSpace(line), "=") + if !ok || strings.HasPrefix(key, "#") { + continue + } + value = strings.TrimSpace(value) + switch strings.TrimSpace(key) { + case "P4PORT": + cfg.port = value + case "P4USER": + cfg.user = value + case "P4CLIENT": + cfg.client = value + } + } + if cfg.port == "" || cfg.client == "" { + return nil, fmt.Errorf("%s does not set both P4PORT and P4CLIENT", filepath.Join(dir, p4ConfigName)) + } + return cfg, nil +} + +func writeP4Config(dir string, cfg *p4Config) error { + content := "P4PORT=" + cfg.port + "\n" + if cfg.user != "" { + content += "P4USER=" + cfg.user + "\n" + } + content += "P4CLIENT=" + cfg.client + "\n" + return os.WriteFile(filepath.Join(dir, p4ConfigName), []byte(content), 0644) +} + +func p4Error(err error, stderr *bytes.Buffer) error { + if msg := strings.TrimSpace(stderr.String()); msg != "" { + return fmt.Errorf("%w: %s", err, msg) + } + return err +} + +// p4Output runs p4 silently and returns its standard output. +func p4Output(cfg *p4Config, args ...string) (string, error) { + var stdout, stderr bytes.Buffer + cmd := exec.Command("p4", cfg.args(args...)...) + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if err := cmdutil.RunCommand(cmd, true); err != nil { + return "", p4Error(err, &stderr) + } + return stdout.String(), nil +} + +// p4SaveClient saves a client spec with "p4 client -i". +func p4SaveClient(cfg *p4Config, spec string, silent bool) error { + var stderr bytes.Buffer + cmd := exec.Command("p4", cfg.args("client", "-i")...) + cmd.Stdin = strings.NewReader(spec) + cmd.Stdout = os.Stderr + if silent { + cmd.Stdout = io.Discard + } + cmd.Stderr = &stderr + if err := cmdutil.RunCommand(cmd, silent); err != nil { + return p4Error(err, &stderr) + } + return nil +} + +// p4ExistingClient returns the spec of cfg.client and whether the client +// already exists on the server. "p4 client -o" prints a default spec for a +// nonexistent client; only existing ones have an Access date. +func p4ExistingClient(cfg *p4Config) (string, bool, error) { + spec, err := p4Output(cfg, "client", "-o", cfg.client) + if err != nil { + return "", false, err + } + return spec, p4SpecField(spec, "Access") != nil, nil +} + +// p4UpdateClientRoot points the root of the client recorded in dir/.p4config +// at dir, e.g. after the workspace has been moved by "ghq migrate". +func p4UpdateClientRoot(dir string) error { + cfg, err := readP4Config(dir) + if err != nil { + return err + } + spec, exists, err := p4ExistingClient(cfg) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("p4 client %q does not exist", cfg.client) + } + return p4SaveClient(cfg, p4SetSpecField(spec, "Root", false, dir), true) +} + +func p4Clone(vg *vcsGetOption) (err error) { + if vg.branch != "" { + return errors.New("--branch option (or @ in the URL) is unavailable for Perforce; specify the depot path of the branch in the URL") + } + if vg.bare { + return errors.New("bare checkouts are unavailable for Perforce") + } + cfg, depotPath, err := p4ConfigFromURL(vg.url) + if err != nil { + return err + } + if hasControlChar(vg.dir) { + return fmt.Errorf("invalid workspace root %q", vg.dir) + } + hostname, _ := p4Hostname() + cfg.client = p4ClientName(p4LocalUser(vg.url), hostname, cfg.port, depotPath, vg.dir) + + spec, exists, err := p4ExistingClient(cfg) + if err != nil { + return err + } + if exists { + // A leftover client of the same workspace, e.g. after "ghq rm". + // Anything else is not ours to modify. + root := "" + if v := p4SpecField(spec, "Root"); len(v) > 0 { + root = v[0] + } + if filepath.Clean(root) != filepath.Clean(vg.dir) { + return fmt.Errorf("p4 client %q already exists with root %q", cfg.client, root) + } + } + spec = p4SetSpecField(spec, "Root", false, vg.dir) + spec = p4SetSpecField(spec, "View", true, p4View(depotPath, cfg.client)...) + if err := p4SaveClient(cfg, spec, vg.silent); err != nil { + return err + } + + if _, statErr := os.Stat(vg.dir); os.IsNotExist(statErr) { + // Do not leave a half-made workspace that later "ghq get" would skip. + defer func() { + if err != nil { + os.RemoveAll(vg.dir) + } + }() + } + if err := os.MkdirAll(vg.dir, 0755); err != nil { + return err + } + if err := writeP4Config(vg.dir, cfg); err != nil { + return err + } + syncArgs := []string{"sync"} + if exists { + // The have list of a reused client describes files that are gone. + syncArgs = append(syncArgs, "-f") + } + if err := runInDir(vg.silent)(vg.dir, "p4", cfg.args(syncArgs...)...); err != nil { + return err + } + if entries, err := os.ReadDir(vg.dir); err == nil && len(entries) == 1 { + logger.Log("warning", fmt.Sprintf("no files found in %s/...; the workspace is empty", depotPath)) + } + return nil +} + +// PerforceBackend is the VCSBackend for Perforce (p4) +var PerforceBackend = &VCSBackend{ + // --shallow, --partial and --recursive are ignored: a workspace holds + // only the synced revisions anyway. + Clone: p4Clone, + Update: func(vg *vcsGetOption) error { + cfg, err := readP4Config(vg.dir) + if err != nil { + return err + } + return runInDir(vg.silent)(vg.dir, "p4", cfg.args("sync")...) + }, + // Init is unsupported: depot paths are created by submitting files. + Contents: []string{p4ConfigName}, + RemoteURL: func(dir string) (string, error) { + cfg, err := readP4Config(dir) + if err != nil { + return "", err + } + spec, exists, err := p4ExistingClient(cfg) + if err != nil { + return "", err + } + if !exists { + return "", fmt.Errorf("p4 client %q does not exist", cfg.client) + } + depotPath, err := p4ViewDepotPath(p4SpecField(spec, "View")) + if err != nil { + return "", err + } + u, err := p4URLFromConfig(cfg, depotPath) + if err != nil { + return "", err + } + return u.String(), nil + }, +} diff --git a/vcs_test.go b/vcs_test.go index 55024f93..182ad59e 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -1,11 +1,15 @@ package main import ( + "errors" "fmt" + "io" "os" "os/exec" "path/filepath" "reflect" + "regexp" + "slices" "strings" "testing" @@ -14,6 +18,7 @@ import ( var ( remoteDummyURL = mustParseURL("https://example.com/git/repo") + p4DummyURL = mustParseURL("p4://alice@p4.example.com:1666/depot/proj/main") dummySvnInfo = []byte(`Path: trunk URL: https://svn.apache.org/repos/asf/subversion/trunk Relative URL: ^/subversion/trunk @@ -42,6 +47,9 @@ func TestVCSBackend(t *testing.T) { } return nil } + defer func(orig func() (string, error)) { p4Hostname = orig }(p4Hostname) + p4Hostname = func() (string, error) { return "myhost", nil } + p4Client := p4ClientName("alice", "myhost", "p4.example.com:1666", "//depot/proj/main", localDir) testCases := []struct { name string @@ -435,6 +443,30 @@ func TestVCSBackend(t *testing.T) { }, expect: []string{"fossil", "update"}, dir: localDir, + }, { + name: "[p4] clone", + f: func() error { + defer os.Remove(filepath.Join(localDir, p4ConfigName)) + return PerforceBackend.Clone(&vcsGetOption{ + url: p4DummyURL, + dir: localDir, + }) + }, + expect: []string{"p4", "-p", "p4.example.com:1666", "-u", "alice", "-c", p4Client, "sync"}, + dir: localDir, + }, { + name: "[p4] update", + f: func() error { + if err := writeP4Config(localDir, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { + return err + } + defer os.Remove(filepath.Join(localDir, p4ConfigName)) + return PerforceBackend.Update(&vcsGetOption{ + dir: localDir, + }) + }, + expect: []string{"p4", "-p", "p4.example.com:1666", "-c", "ws", "sync"}, + dir: localDir, }} for _, tc := range testCases { @@ -607,3 +639,639 @@ func TestBranchOptionIgnoredErrors(t *testing.T) { t.Error("error should be occurred, but nil") } } + +type p4Call struct { + args []string + dir string + stdin string +} + +// fakeP4 replaces the command runner with one that records p4 invocations +// and lets respond write output or fail per subcommand ("client -o NAME", +// "client -i", "sync", ...). +func fakeP4(t *testing.T, respond func(sub string, cmd *exec.Cmd) error) *[]p4Call { + t.Helper() + origRunner, origHostname := cmdutil.CommandRunner, p4Hostname + t.Cleanup(func() { cmdutil.CommandRunner, p4Hostname = origRunner, origHostname }) + p4Hostname = func() (string, error) { return "myhost", nil } + t.Setenv("P4USER", "alice") + + calls := &[]p4Call{} + cmdutil.CommandRunner = func(cmd *exec.Cmd) error { + c := p4Call{args: cmd.Args, dir: cmd.Dir} + if cmd.Stdin != nil { + b, err := io.ReadAll(cmd.Stdin) + if err != nil { + return err + } + c.stdin = string(b) + } + *calls = append(*calls, c) + if cmd.Args[0] != "p4" { + t.Errorf("unexpected command: %v", cmd.Args) + return errors.New("unexpected command") + } + args := cmd.Args[1:] + for len(args) >= 2 && (args[0] == "-p" || args[0] == "-u" || args[0] == "-c") { + args = args[2:] + } + if respond == nil { + return nil + } + return respond(strings.Join(args, " "), cmd) + } + return calls +} + +// p4TestSpec returns a client spec as printed by "p4 client -o". +func p4TestSpec(client, root string, exists bool, view ...string) string { + b := &strings.Builder{} + fmt.Fprintf(b, "# A Perforce Client Specification.\n#\n# Root: The base directory of the client workspace.\n\nClient:\t%s\n\n", client) + if exists { + b.WriteString("Update:\t2026/01/02 03:04:05\n\nAccess:\t2026/01/02 03:04:05\n\n") + } + fmt.Fprintf(b, "Owner:\talice\n\nHost:\tmyhost\n\nDescription:\n\tCreated by alice.\n\nRoot:\t%s\n\n", root) + b.WriteString("Options:\tnoallwrite noclobber nocompress unlocked nomodtime normdir noaltsync\n\nView:\n") + for _, v := range view { + fmt.Fprintf(b, "\t%s\n", v) + } + return b.String() +} + +func readFileString(t *testing.T, name string) string { + t.Helper() + b, err := os.ReadFile(name) + if err != nil { + t.Fatal(err) + } + return string(b) +} + +func TestPerforceBackend_Clone(t *testing.T) { + const port = "p4.example.com:1666" + newDir := func(t *testing.T) string { + return filepath.Join(newTempDir(t), "p4.example.com", "depot", "proj", "main") + } + + t.Run("new client", func(t *testing.T) { + dir := newDir(t) + client := p4ClientName("alice", "myhost", port, "//depot/proj/main", dir) + calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { + if sub == "client -o "+client { + fmt.Fprint(cmd.Stdout, p4TestSpec(client, "/home/alice", false, "//depot/... //"+client+"/...")) + } + return nil + }) + err := PerforceBackend.Clone(&vcsGetOption{ + url: mustParseURL("p4://p4.example.com/depot/proj/main/"), + dir: dir, + // ignored by p4 + shallow: true, recursive: true, partial: "blobless", silent: true, + }) + if err != nil { + t.Fatal(err) + } + global := []string{"p4", "-p", port, "-c", client} + expect := []p4Call{ + {args: append(global[:5:5], "client", "-o", client)}, + {args: append(global[:5:5], "client", "-i")}, + {args: append(global[:5:5], "sync"), dir: dir}, + } + if len(*calls) != len(expect) { + t.Fatalf("got %d commands, expect %d: %+v", len(*calls), len(expect), *calls) + } + for i, c := range *calls { + if !reflect.DeepEqual(c.args, expect[i].args) || c.dir != expect[i].dir { + t.Errorf("command %d:\ngot: %v (dir %q)\nexpect: %v (dir %q)", i, c.args, c.dir, expect[i].args, expect[i].dir) + } + } + spec := (*calls)[1].stdin + for _, want := range []string{ + "\nOwner:\talice\n", + "\nRoot:\t" + dir + "\n", + "\nView:\n\t\"//depot/proj/main/...\" \"//" + client + "/...\"\n\t\"-//depot/proj/main/.p4config\" \"//" + client + "/.p4config\"\n", + } { + if !strings.Contains(spec, want) { + t.Errorf("client spec should contain %q, but:\n%s", want, spec) + } + } + if strings.Contains(spec, "/home/alice") || strings.Contains(spec, "//depot/... ") { + t.Errorf("client spec should not keep the default root and view:\n%s", spec) + } + if got, expect := readFileString(t, filepath.Join(dir, p4ConfigName)), "P4PORT="+port+"\nP4CLIENT="+client+"\n"; got != expect { + t.Errorf(".p4config:\ngot: %q\nexpect: %q", got, expect) + } + }) + + t.Run("ssl with user", func(t *testing.T) { + dir := newDir(t) + const sslPort = "ssl:p4.example.com:1667" + client := p4ClientName("bob", "myhost", sslPort, "//depot/proj/main", dir) + calls := fakeP4(t, nil) + err := PerforceBackend.Clone(&vcsGetOption{ + url: mustParseURL("p4+ssl://bob@p4.example.com:1667/depot/proj/main"), + dir: dir, + }) + if err != nil { + t.Fatal(err) + } + expect := []string{"p4", "-p", sslPort, "-u", "bob", "-c", client, "sync"} + if got := (*calls)[len(*calls)-1].args; !reflect.DeepEqual(got, expect) { + t.Errorf("\ngot: %v\nexpect: %v", got, expect) + } + if got, expect := readFileString(t, filepath.Join(dir, p4ConfigName)), "P4PORT="+sslPort+"\nP4USER=bob\nP4CLIENT="+client+"\n"; got != expect { + t.Errorf(".p4config:\ngot: %q\nexpect: %q", got, expect) + } + }) + + t.Run("reuse client left by ghq rm", func(t *testing.T) { + dir := newDir(t) + client := p4ClientName("alice", "myhost", port, "//depot/proj/main", dir) + calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { + if sub == "client -o "+client { + fmt.Fprint(cmd.Stdout, p4TestSpec(client, dir, true, "//depot/proj/main/... //"+client+"/...")) + } + return nil + }) + if err := PerforceBackend.Clone(&vcsGetOption{url: mustParseURL("p4://p4.example.com/depot/proj/main"), dir: dir}); err != nil { + t.Fatal(err) + } + expect := []string{"p4", "-p", port, "-c", client, "sync", "-f"} + if got := (*calls)[len(*calls)-1].args; !reflect.DeepEqual(got, expect) { + t.Errorf("\ngot: %v\nexpect: %v", got, expect) + } + }) + + failures := []struct { + name string + url string + branch string + bare bool + mkdir bool + respond func(client, dir string) func(sub string, cmd *exec.Cmd) error + errStr string + calls int + }{{ + name: "client of another workspace", + url: "p4://p4.example.com/depot/proj/main", + respond: func(client, dir string) func(string, *exec.Cmd) error { + return func(sub string, cmd *exec.Cmd) error { + if strings.HasPrefix(sub, "client -o") { + fmt.Fprint(cmd.Stdout, p4TestSpec(client, "/somewhere/else", true, "//depot/... //"+client+"/...")) + } + return nil + } + }, + errStr: "already exists with root", + calls: 1, + }, { + name: "not logged in", + url: "p4://p4.example.com/depot/proj/main", + respond: func(client, dir string) func(string, *exec.Cmd) error { + return func(sub string, cmd *exec.Cmd) error { + fmt.Fprintln(cmd.Stderr, "Perforce password (P4PASSWD) invalid or unset.") + return errors.New("exit status 1") + } + }, + errStr: "Perforce password (P4PASSWD) invalid or unset.", + calls: 1, + }, { + name: "client spec rejected", + url: "p4://p4.example.com/nodepot/proj", + respond: func(client, dir string) func(string, *exec.Cmd) error { + return func(sub string, cmd *exec.Cmd) error { + if sub == "client -i" { + fmt.Fprintln(cmd.Stderr, "Error in client specification.") + return errors.New("exit status 1") + } + return nil + } + }, + errStr: "Error in client specification.", + calls: 2, + }, { + name: "sync fails", + url: "p4://p4.example.com/depot/proj/main", + respond: func(client, dir string) func(string, *exec.Cmd) error { + return func(sub string, cmd *exec.Cmd) error { + if sub == "sync" { + return errors.New("exit status 1") + } + return nil + } + }, + errStr: "exit status 1", + calls: 3, + }, { + name: "sync fails in existing directory", + url: "p4://p4.example.com/depot/proj/main", + mkdir: true, + respond: func(client, dir string) func(string, *exec.Cmd) error { + return func(sub string, cmd *exec.Cmd) error { + if sub == "sync" { + return errors.New("exit status 1") + } + return nil + } + }, + errStr: "exit status 1", + calls: 3, + }, { + name: "branch", + url: "p4://p4.example.com/depot/proj/main", + branch: "dev", + errStr: "--branch option", + }, { + name: "bare", + url: "p4://p4.example.com/depot/proj/main", + bare: true, + errStr: "bare checkouts are unavailable", + }, { + name: "no host", + url: "p4:///depot/proj", + errStr: "missing p4 server host", + }, { + name: "no depot path", + url: "p4://p4.example.com/", + errStr: "missing depot path", + }, { + name: "password", + url: "p4://alice:secret@p4.example.com/depot/proj", + errStr: "passwords in p4 URLs are not supported", + }, { + name: "newline in user", + url: "p4://al%0Aice@p4.example.com/depot/proj", + errStr: "invalid p4 user", + }, { + name: "wildcard", + url: "p4://p4.example.com/depot/proj/...", + errStr: "wildcards and revision specifiers", + }, { + name: "asterisk", + url: "p4://p4.example.com/depot/*/main", + errStr: "wildcards and revision specifiers", + }, { + name: "revision", + url: "p4://p4.example.com/depot/proj%23head", + errStr: "wildcards and revision specifiers", + }, { + name: "label", + url: "p4://p4.example.com/depot/proj%40label", + errStr: "wildcards and revision specifiers", + }, { + name: "percent escape", + url: "p4://p4.example.com/depot/proj%2540", + errStr: "wildcards and revision specifiers", + }, { + name: "quote", + url: "p4://p4.example.com/depot/pr%22oj", + errStr: "wildcards and revision specifiers", + }, { + name: "spec injection", + url: "p4://p4.example.com/depot/proj%0AOptions:%09allwrite", + errStr: "control characters", + }, { + name: "empty component", + url: "p4://p4.example.com/depot//proj", + errStr: "empty or dot path components", + }, { + name: "parent component", + url: "p4://p4.example.com/depot/../proj", + errStr: "empty or dot path components", + }} + for _, tc := range failures { + t.Run(tc.name, func(t *testing.T) { + dir := newDir(t) + if tc.mkdir { + if err := os.MkdirAll(dir, 0755); err != nil { + t.Fatal(err) + } + } + u := mustParseURL(tc.url) + var respond func(string, *exec.Cmd) error + if tc.respond != nil { + cfg, depotPath, err := p4ConfigFromURL(u) + if err != nil { + t.Fatal(err) + } + respond = tc.respond(p4ClientName("alice", "myhost", cfg.port, depotPath, dir), dir) + } + calls := fakeP4(t, respond) + err := PerforceBackend.Clone(&vcsGetOption{url: u, dir: dir, branch: tc.branch, bare: tc.bare}) + if err == nil || !strings.Contains(err.Error(), tc.errStr) { + t.Errorf("error should contain %q, but: %v", tc.errStr, err) + } + if len(*calls) != tc.calls { + t.Errorf("got %d commands, expect %d: %+v", len(*calls), tc.calls, *calls) + } + _, statErr := os.Stat(dir) + if tc.mkdir { + if statErr != nil { + t.Errorf("existing directory should be kept: %v", statErr) + } + } else if !os.IsNotExist(statErr) { + t.Errorf("directory should not be left behind: %v", statErr) + } + }) + } +} + +func TestPerforceBackend_Update(t *testing.T) { + t.Run("with user", func(t *testing.T) { + dir := newTempDir(t) + config := "# comment=ignored\r\nP4PORT = ssl:p4.example.com:1666\r\nP4USER=bob\r\nP4CLIENT=bob_ws\r\nP4CHARSET=utf8\r\n" + if err := os.WriteFile(filepath.Join(dir, p4ConfigName), []byte(config), 0644); err != nil { + t.Fatal(err) + } + calls := fakeP4(t, nil) + if err := PerforceBackend.Update(&vcsGetOption{dir: dir, silent: true}); err != nil { + t.Fatal(err) + } + expect := []p4Call{{args: []string{"p4", "-p", "ssl:p4.example.com:1666", "-u", "bob", "-c", "bob_ws", "sync"}, dir: dir}} + if !reflect.DeepEqual(*calls, expect) { + t.Errorf("\ngot: %+v\nexpect: %+v", *calls, expect) + } + }) + + for name, config := range map[string]*string{ + "no .p4config": nil, + "no client": ptr("P4PORT=p4.example.com:1666\n"), + "no port": ptr("P4CLIENT=ws\n"), + } { + t.Run(name, func(t *testing.T) { + dir := newTempDir(t) + if config != nil { + if err := os.WriteFile(filepath.Join(dir, p4ConfigName), []byte(*config), 0644); err != nil { + t.Fatal(err) + } + } + calls := fakeP4(t, nil) + if err := PerforceBackend.Update(&vcsGetOption{dir: dir}); err == nil { + t.Error("error should be occurred, but nil") + } + if len(*calls) != 0 { + t.Errorf("p4 should not be run: %+v", *calls) + } + }) + } +} + +func ptr[T any](v T) *T { return &v } + +func TestPerforceBackend_RemoteURL(t *testing.T) { + testCases := []struct { + name string + config string + spec func(client string) string + expect string + errStr string + }{{ + name: "tcp", + config: "P4PORT=p4.example.com:1666\nP4CLIENT=ws\n", + spec: func(c string) string { + return p4TestSpec(c, "/r", true, "//depot/proj/main/... //ws/...", "-//depot/proj/main/.p4config //ws/.p4config") + }, + expect: "p4://p4.example.com:1666/depot/proj/main", + }, { + name: "ssl with user", + config: "P4PORT=ssl:p4.example.com:1667\nP4USER=bob\nP4CLIENT=ws\n", + spec: func(c string) string { + return p4TestSpec(c, "/r", true, "-//depot/x/... //ws/x/...", "//depot/proj/... //ws/...") + }, + expect: "p4+ssl://bob@p4.example.com:1667/depot/proj", + }, { + name: "quoted view", + config: "P4PORT=1666\nP4CLIENT=ws\n", + spec: func(c string) string { + return p4TestSpec(c, "/r", true, `"//depot/my proj/..." "//ws/..."`) + }, + expect: "p4://localhost:1666/depot/my%20proj", + }, { + name: "client deleted on server", + config: "P4PORT=p4.example.com:1666\nP4CLIENT=ws\n", + spec: func(c string) string { + return p4TestSpec(c, "/r", false, "//depot/... //ws/...") + }, + errStr: "does not exist", + }, { + name: "no depot path in view", + config: "P4PORT=p4.example.com:1666\nP4CLIENT=ws\n", + spec: func(c string) string { + return p4TestSpec(c, "/r", true, "//depot/file.txt //ws/file.txt") + }, + errStr: "no depot path found", + }} + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + dir := newTempDir(t) + if err := os.WriteFile(filepath.Join(dir, p4ConfigName), []byte(tc.config), 0644); err != nil { + t.Fatal(err) + } + calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { + if sub == "client -o ws" { + fmt.Fprint(cmd.Stdout, tc.spec("ws")) + } + return nil + }) + got, err := PerforceBackend.RemoteURL(dir) + if tc.errStr != "" { + if err == nil || !strings.Contains(err.Error(), tc.errStr) { + t.Errorf("error should contain %q, but: %v", tc.errStr, err) + } + return + } + if err != nil { + t.Fatal(err) + } + if got != tc.expect { + t.Errorf("got: %s, expect: %s", got, tc.expect) + } + if len(*calls) != 1 || !slices.Contains((*calls)[0].args, "-c") { + t.Errorf("unexpected commands: %+v", *calls) + } + }) + } +} + +func TestP4UpdateClientRoot(t *testing.T) { + dir := newTempDir(t) + if err := writeP4Config(dir, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { + t.Fatal(err) + } + calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { + if sub == "client -o ws" { + fmt.Fprint(cmd.Stdout, p4TestSpec("ws", "/old/root", true, "//depot/proj/... //ws/...")) + } + return nil + }) + if err := p4UpdateClientRoot(dir); err != nil { + t.Fatal(err) + } + if len(*calls) != 2 { + t.Fatalf("unexpected commands: %+v", *calls) + } + spec := (*calls)[1].stdin + if !strings.Contains(spec, "\nRoot:\t"+dir+"\n") || strings.Contains(spec, "/old/root") || + !strings.Contains(spec, "\n\t//depot/proj/... //ws/...\n") { + t.Errorf("unexpected client spec:\n%s", spec) + } +} + +func TestP4ConfigFromURL(t *testing.T) { + testCases := []struct { + url, port, user, depot string + }{ + {"p4://p4.example.com/depot/proj/main", "p4.example.com:1666", "", "//depot/proj/main"}, + {"p4://p4.example.com:1777/depot/proj/main/", "p4.example.com:1777", "", "//depot/proj/main"}, + {"p4://alice@p4.example.com/depot/my%20proj", "p4.example.com:1666", "alice", "//depot/my proj"}, + {"p4+ssl://p4.example.com/depot", "ssl:p4.example.com:1666", "", "//depot"}, + {"p4://[::1]:1666/depot/proj", "tcp6:[::1]:1666", "", "//depot/proj"}, + {"p4+ssl://[::1]/depot/proj", "ssl6:[::1]:1666", "", "//depot/proj"}, + // ghq..vcs = p4 or --vcs=p4 without a p4:// URL + {"https://p4.example.com/depot/proj", "p4.example.com:1666", "", "//depot/proj"}, + } + for _, tc := range testCases { + t.Run(tc.url, func(t *testing.T) { + cfg, depot, err := p4ConfigFromURL(mustParseURL(tc.url)) + if err != nil { + t.Fatal(err) + } + if cfg.port != tc.port || cfg.user != tc.user || depot != tc.depot { + t.Errorf("got: (%q, %q, %q), expect: (%q, %q, %q)", cfg.port, cfg.user, depot, tc.port, tc.user, tc.depot) + } + // RemoteURL must rebuild an equivalent URL + u, err := p4URLFromConfig(cfg, depot) + if err != nil { + t.Fatal(err) + } + cfg2, depot2, err := p4ConfigFromURL(u) + if err != nil { + t.Fatal(err) + } + if *cfg2 != *cfg || depot2 != depot { + t.Errorf("round trip via %s: got (%+v, %q), expect (%+v, %q)", u, cfg2, depot2, cfg, depot) + } + }) + } +} + +func TestP4URLFromConfig(t *testing.T) { + testCases := []struct { + port, expect string + }{ + {"1666", "p4://localhost:1666/depot/proj"}, + {"p4.example.com:1666", "p4://p4.example.com:1666/depot/proj"}, + {"tcp:p4.example.com:1666", "p4://p4.example.com:1666/depot/proj"}, + {"tcp4:p4.example.com:1666", "p4://p4.example.com:1666/depot/proj"}, + {"ssl:p4.example.com:1666", "p4+ssl://p4.example.com:1666/depot/proj"}, + {"ssl:1666", "p4+ssl://localhost:1666/depot/proj"}, + {"tcp6:[::1]:1666", "p4://[::1]:1666/depot/proj"}, + {"perforce:1666", "p4://perforce:1666/depot/proj"}, + {"rsh:p4d -r /p4root -i", ""}, + {"p4.example.com", ""}, + {"p4.example.com:", ""}, + } + for _, tc := range testCases { + t.Run(tc.port, func(t *testing.T) { + u, err := p4URLFromConfig(&p4Config{port: tc.port, client: "ws"}, "//depot/proj") + if tc.expect == "" { + if err == nil { + t.Errorf("error should be occurred, but got %s", u) + } + return + } + if err != nil { + t.Fatal(err) + } + if u.String() != tc.expect { + t.Errorf("got: %s, expect: %s", u, tc.expect) + } + }) + } +} + +func TestP4ClientName(t *testing.T) { + const port = "p4.example.com:1666" + name := p4ClientName("alice", "my.host", port, "//depot/proj/main", "/ghq/p4.example.com/depot/proj/main") + if !regexp.MustCompile(`^alice_my_host_depot_proj_main_[0-9a-f]{8}$`).MatchString(name) { + t.Errorf("unexpected client name: %s", name) + } + if again := p4ClientName("alice", "my.host", port, "//depot/proj/main", "/ghq/p4.example.com/depot/proj/main"); again != name { + t.Errorf("client name should be deterministic: %s != %s", again, name) + } + + // Names that sanitize to the same slug must still differ. + seen := map[string]bool{} + for _, in := range [][2]string{ + {"//depot/a_b", "/ghq/h/depot/a_b"}, + {"//depot/a/b", "/ghq/h/depot/a/b"}, + {"//depot/a b", "/ghq/h/depot/a b"}, + {"//depot/a/b", "/other-root/h/depot/a/b"}, + } { + n := p4ClientName("alice", "my.host", port, in[0], in[1]) + if seen[n] { + t.Errorf("client name collision: %s", n) + } + seen[n] = true + } + if p4ClientName("alice", "h", port, "//depot/a", "/r") == p4ClientName("alice", "h", "ssl:"+port, "//depot/a", "/r") { + t.Error("client names should differ between servers") + } + + valid := regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]*$`) + for _, in := range [][3]string{ + {"-u x", "host;rm -rf", "//depot/..."}, + {`DOMAIN\user`, "HOST", "//depot/@#%*"}, + {"ユーザー", "ホスト", "//デポ/パス"}, + {"", "", "//depot/" + strings.Repeat("long/", 100)}, + } { + n := p4ClientName(in[0], in[1], port, in[2], "/r") + if !valid.MatchString(n) || len(n) > 140 { + t.Errorf("invalid client name for %q: %q", in, n) + } + } + if n := p4ClientName("ユーザー", "ホスト", port, "//デポ", "/r"); !strings.HasPrefix(n, "ghq_") { + t.Errorf("client name without readable parts should start with ghq_: %s", n) + } +} + +func TestP4Spec(t *testing.T) { + spec := "# Root: comment\r\nClient:\tws\r\n\r\nRoot:\t/old root\r\n\r\nAltRoots:\r\n\t/alt\r\n\r\nView:\r\n\t//depot/a/... //ws/a/...\r\n\t\"//depot/b c/...\" \"//ws/b c/...\"\r\n\r\nLineEnd:\tlocal\r\n" + if got := p4SpecField(spec, "Root"); !reflect.DeepEqual(got, []string{"/old root"}) { + t.Errorf("Root: got %q", got) + } + if got := p4SpecField(spec, "View"); !reflect.DeepEqual(got, []string{"//depot/a/... //ws/a/...", `"//depot/b c/..." "//ws/b c/..."`}) { + t.Errorf("View: got %q", got) + } + if got := p4SpecField(spec, "Access"); got != nil { + t.Errorf("Access: got %q", got) + } + if got := p4SpecField("Description:\n\nRoot:\t/r\n", "Description"); got == nil || len(got) != 0 { + t.Errorf("empty Description: got %#v", got) + } + + edited := p4SetSpecField(spec, "Root", false, "/new root") + edited = p4SetSpecField(edited, "View", true, "//depot/x/... //ws/...") + edited = p4SetSpecField(edited, "Stream", false, "//stream/main") + expect := "# Root: comment\nClient:\tws\n\nRoot:\t/new root\n\nAltRoots:\n\t/alt\n\nView:\n\t//depot/x/... //ws/...\n\nLineEnd:\tlocal\n\n\nStream:\t//stream/main" + if edited != expect { + t.Errorf("\ngot: %q\nexpect: %q", edited, expect) + } + + for _, tc := range []struct { + view []string + expect string + }{ + {[]string{"//depot/a/... //ws/..."}, "//depot/a"}, + {[]string{`"-//depot/a/x" "//ws/x"`, `"//depot/b c/..." "//ws/..."`}, "//depot/b c"}, + {[]string{"+//depot/o/... //ws/...", "//depot/c/... //ws/c/..."}, "//depot/c"}, + {[]string{"//depot/... //ws/..."}, "//depot"}, + {[]string{"//... //ws/..."}, ""}, + {nil, ""}, + } { + got, err := p4ViewDepotPath(tc.view) + if got != tc.expect || (err == nil) != (tc.expect != "") { + t.Errorf("p4ViewDepotPath(%q) = %q, %v; expect %q", tc.view, got, err, tc.expect) + } + } +} From cc19aa7f4bebb7ae7a9f9fc1e2afe5c9b5b95d45 Mon Sep 17 00:00:00 2001 From: t04dJ14n9 <61448324+t04dJ14n9@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:00:49 +0800 Subject: [PATCH 2/4] p4: harden workspace handling after adversarial review - Accept only tcp/ssl P4PORT forms from .p4config and URLs; a P4PORT such as rsh: made p4 run a local command. - Treat .p4config as a workspace only if it names a valid client, so shared P4CONFIG files no longer hide repositories below them. - Check that the client exists and is rooted at the workspace before "get -u" and RemoteURL; p4 sync writes to the root on the server. - Refuse "get -u" with a p4:// URL of another server or user (ghq paths omit the port). - Claim the workspace directory exclusively before running p4, and remove only that directory on failure. - Reject URL fragments and queries (p4://host/path#rev used to sync head). - Hash the raw user and hostname into client names. - migrate: fail and move the workspace back if the client root cannot be updated. Co-Authored-By: Claude Opus 5.5 --- cmd_get_test.go | 33 ++++-- cmd_list_test.go | 19 ++- cmd_migrate.go | 10 +- cmd_migrate_test.go | 65 +++++++++-- local_repository.go | 12 +- local_repository_test.go | 14 ++- vcs_p4.go | 181 ++++++++++++++++++++-------- vcs_test.go | 246 +++++++++++++++++++++++++++++++++------ 8 files changed, 477 insertions(+), 103 deletions(-) diff --git a/cmd_get_test.go b/cmd_get_test.go index 04dc0c5e..43be1884 100644 --- a/cmd_get_test.go +++ b/cmd_get_test.go @@ -721,12 +721,7 @@ func TestDoGet_p4(t *testing.T) { 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, func(sub string, cmd *exec.Cmd) error { - if sub == "sync" || sub == "sync -f" { - return os.WriteFile(filepath.Join(cmd.Dir, "README"), []byte("hello\n"), 0644) - } - return nil - }) + calls := fakeP4(t, newP4FakeServer().respond) run := func(args ...string) (string, error) { var err error out, _, _ := captureWithInput([]string{"y"}, func() { @@ -750,7 +745,10 @@ func TestDoGet_p4(t *testing.T) { if _, err := run("get", "-u", url); err != nil { t.Fatal(err) } - expect := []p4Call{{args: []string{"p4", "-p", "p4.example.com:1666", "-c", client, "sync"}, dir: dir}} + 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) } @@ -790,3 +788,24 @@ func TestDoGet_p4(t *testing.T) { 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) + } + } +} diff --git a/cmd_list_test.go b/cmd_list_test.go index 1d8446b7..ec85a2e4 100644 --- a/cmd_list_test.go +++ b/cmd_list_test.go @@ -146,7 +146,7 @@ func TestDoList_query(t *testing.T) { } for _, r := range p4Repos { os.MkdirAll(filepath.Join(tmproot, r, "src"), 0755) - os.WriteFile(filepath.Join(tmproot, r, ".p4config"), []byte("P4PORT=1666\n"), 0644) + 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) } @@ -322,3 +322,20 @@ 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) + out, _, _ := capture(func() { + if err := newApp().Run(context.Background(), []string{"ghq", "list"}); err != nil { + t.Error(err) + } + }) + if out != "p4.example.com/team/app\n" { + t.Errorf("got %q", out) + } +} diff --git a/cmd_migrate.go b/cmd_migrate.go index a0a5f845..e73d6c54 100644 --- a/cmd_migrate.go +++ b/cmd_migrate.go @@ -158,10 +158,14 @@ func doMigrate(ctx context.Context, cmd *cli.Command) error { } } - // A p4 client spec records its workspace root on the server. + // A p4 client records its root on the server, and the workspace is + // unusable until it points at the new location. if vcsBackend == PerforceBackend { - if err := p4UpdateClientRoot(destPath); err != nil { - logger.Log("warning", fmt.Sprintf("failed to update the root of the p4 client to %s: %v", destPath, err)) + if err := p4UpdateClientRoot(absDir, destPath); err != nil { + 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) } } diff --git a/cmd_migrate_test.go b/cmd_migrate_test.go index 5ad142ed..60b0c6fa 100644 --- a/cmd_migrate_test.go +++ b/cmd_migrate_test.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "os" "os/exec" "path/filepath" @@ -464,18 +463,15 @@ func TestIsLinkedGitDir(t *testing.T) { func TestDoMigrate_p4(t *testing.T) { tmpdir := newTempDir(t) setRepositoryPathTestRoot(t, tmpdir) - calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { - if sub == "client -o ws" { - fmt.Fprint(cmd.Stdout, p4TestSpec("ws", "/old/root", true, "//depot/proj/main/... //ws/...")) - } - return nil - }) + srv := newP4FakeServer() + calls := fakeP4(t, srv.respond) newSrc := func(name string) string { src := filepath.Join(tmpdir, "sources", name) os.MkdirAll(src, 0755) if err := writeP4Config(src, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { t.Fatal(err) } + srv.clients["ws"] = p4TestSpec("ws", src, true, "//depot/proj/main/... //ws/...") return src } dest := filepath.Join(tmpdir, "p4.example.com", "depot", "proj", "main") @@ -513,3 +509,58 @@ func TestDoMigrate_p4(t *testing.T) { } }) } + +func TestDoMigrate_p4Review1(t *testing.T) { + tmpdir := newTempDir(t) + setRepositoryPathTestRoot(t, tmpdir) + dest := filepath.Join(tmpdir, "p4.example.com", "depot", "proj", "main") + newSrc := func(t *testing.T, name string) string { + src := filepath.Join(tmpdir, "sources", name) + os.MkdirAll(src, 0755) + if err := writeP4Config(src, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { + t.Fatal(err) + } + return src + } + saved := func(calls []p4Call) bool { + for _, c := range calls { + if slices.Contains(c.args, "-i") { + return true + } + } + return false + } + + t.Run("copy of a workspace rooted elsewhere", func(t *testing.T) { + src := newSrc(t, "copy") + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", "/active/workspace", true, "//depot/proj/main/... //ws/...") + calls := fakeP4(t, srv.respond) + if err := newApp().Run(context.Background(), []string{"ghq", "migrate", "-y", src}); err == nil { + t.Error("error should be occurred, but nil") + } + if _, err := os.Stat(filepath.Join(src, p4ConfigName)); err != nil { + t.Errorf("source should stay: %v", err) + } + if saved(*calls) { + t.Errorf("the other workspace's client must not be modified: %+v", *calls) + } + }) + + t.Run("client root update fails", func(t *testing.T) { + src := newSrc(t, "fail") + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", src, true, "//depot/proj/main/... //ws/...") + srv.fail["client -i"] = "Perforce password (P4PASSWD) invalid or unset." + fakeP4(t, srv.respond) + if err := newApp().Run(context.Background(), []string{"ghq", "migrate", "-y", src}); err == nil { + t.Error("error should be occurred, but nil") + } + if _, err := os.Stat(filepath.Join(src, p4ConfigName)); err != nil { + t.Errorf("workspace should be moved back: %v", err) + } + if _, err := os.Stat(dest); !os.IsNotExist(err) { + t.Errorf("destination should not be left behind: %v", err) + } + }) +} diff --git a/local_repository.go b/local_repository.go index b1b97355..cd187271 100644 --- a/local_repository.go +++ b/local_repository.go @@ -302,7 +302,7 @@ func findVCSBackend(fpath, vcs string) *VCSBackend { return vcsBackend } for _, d := range vcsBackend.Contents { - if _, err := os.Stat(filepath.Join(fpath, d)); err == nil { + if hasVCSContents(fpath, d, vcsBackend) { return vcsBackend } } @@ -312,13 +312,21 @@ func findVCSBackend(fpath, vcs string) *VCSBackend { return GitBackend } for _, d := range vcsContents { - if _, err := os.Stat(filepath.Join(fpath, d)); err == nil { + if hasVCSContents(fpath, d, vcsContentsMap[d]) { return vcsContentsMap[d] } } return nil } +func hasVCSContents(fpath, d string, backend *VCSBackend) bool { + if _, err := os.Stat(filepath.Join(fpath, d)); err != nil { + return false + } + // .p4config is also used for settings shared by several workspaces. + return backend != PerforceBackend || isP4Workspace(fpath) +} + func walkAllLocalRepositories(callback func(*LocalRepository)) error { return walkLocalRepositories("", callback) } diff --git a/local_repository_test.go b/local_repository_test.go index 7c22aa70..c30677c0 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -372,15 +372,23 @@ func TestFindVCSBackend(t *testing.T) { name: "p4", setup: func(t *testing.T) (string, string) { dir := newTempDir(t) - os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\n"), 0644) + os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\nP4CLIENT=ws\n"), 0644) return dir, "" }, expect: PerforceBackend, }, { - name: "p4 with matched vcs", + name: "shared .p4config without client", setup: func(t *testing.T) (string, string) { dir := newTempDir(t) os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\n"), 0644) + return dir, "" + }, + expect: nil, + }, { + name: "p4 with matched vcs", + setup: func(t *testing.T) (string, string) { + dir := newTempDir(t) + os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\nP4CLIENT=ws\n"), 0644) return dir, "p4" }, expect: PerforceBackend, @@ -389,7 +397,7 @@ func TestFindVCSBackend(t *testing.T) { setup: func(t *testing.T) (string, string) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git"), 0755) - os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\n"), 0644) + os.WriteFile(filepath.Join(dir, ".p4config"), []byte("P4PORT=1666\nP4CLIENT=ws\n"), 0644) return dir, "" }, expect: GitBackend, diff --git a/vcs_p4.go b/vcs_p4.go index 3eebd016..25cb2aff 100644 --- a/vcs_p4.go +++ b/vcs_p4.go @@ -13,6 +13,7 @@ import ( "os/exec" "path/filepath" "regexp" + "strconv" "strings" "unicode" @@ -62,6 +63,9 @@ func hasControlChar(s string) bool { // p4:// or p4+ssl:// URL. Other schemes (for example https:// with // "ghq..vcs = p4") are treated as plain TCP. func p4ConfigFromURL(u *url.URL) (*p4Config, string, error) { + if u.Opaque != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" { + return nil, "", fmt.Errorf("invalid p4 URL %q: queries, fragments and revision specifiers are not supported", u.Redacted()) + } host := u.Hostname() if host == "" { return nil, "", fmt.Errorf("missing p4 server host in URL %q", u) @@ -87,6 +91,9 @@ func p4ConfigFromURL(u *url.URL) (*p4Config, string, error) { if proto != "" { p4port = proto + ":" + p4port } + if _, _, err := p4ParsePort(p4port); err != nil { + return nil, "", fmt.Errorf("invalid p4 server in URL %q: %w", u.Redacted(), err) + } cfg := &p4Config{port: p4port} if u.User != nil { @@ -143,11 +150,11 @@ func p4NamePart(s string, maxLen int) string { } // p4ClientName returns a deterministic client name of the form -// ___. Sanitizing can map different -// inputs to the same readable part, so the hash of the server, depot path -// and local root keeps names unique. +// ___. Sanitizing and truncating can +// map different inputs to the same readable parts, so the hash of all inputs +// keeps names unique. func p4ClientName(user, hostname, port, depotPath, root string) string { - sum := sha256.Sum256([]byte(port + "\x00" + depotPath + "\x00" + root)) + sum := sha256.Sum256([]byte(strings.Join([]string{user, hostname, port, depotPath, root}, "\x00"))) var parts []string for _, p := range []string{ p4NamePart(user, 32), p4NamePart(hostname, 32), p4NamePart(depotPath, 64), @@ -271,31 +278,53 @@ func p4ViewDepotPath(view []string) (string, error) { return "", fmt.Errorf("no depot path found in client view %q", view) } -// p4URLFromConfig rebuilds the p4:// URL of a workspace. -func p4URLFromConfig(cfg *p4Config, depotPath string) (*url.URL, error) { - port := cfg.port - scheme := "p4" - if proto, rest, ok := strings.Cut(port, ":"); ok { +var p4HostReg = regexp.MustCompile(`^[A-Za-z0-9_]([A-Za-z0-9_.-]*[A-Za-z0-9_])?$`) + +// p4ParsePort parses the P4PORT forms ghq supports, [tcp*:|ssl*:][host:]port, +// and returns the URL scheme and host:port. Other transports are rejected: +// with "rsh:", for instance, p4 runs a local command instead of connecting. +func p4ParsePort(port string) (scheme, hostport string, err error) { + scheme, rest := "p4", port + if proto, r, ok := strings.Cut(port, ":"); ok { switch proto { case "tcp", "tcp4", "tcp6", "tcp46", "tcp64": - port = rest + rest = r case "ssl", "ssl4", "ssl6", "ssl46", "ssl64": - port, scheme = rest, "p4+ssl" + scheme, rest = "p4+ssl", r + case "rsh", "jsh": + return "", "", fmt.Errorf("unsupported P4PORT %q", port) } } - host, portNum, err := net.SplitHostPort(port) - if err != nil { - host, portNum = "localhost", port // P4PORT=1666 - } - if portNum == "" || strings.Trim(portNum, "0123456789") != "" { - return nil, fmt.Errorf("unsupported P4PORT %q", cfg.port) + host, num, splitErr := net.SplitHostPort(rest) + if splitErr != nil { + host, num = "localhost", rest // P4PORT=1666 } if host == "" { host = "localhost" } + if n, err := strconv.Atoi(num); err != nil || n < 1 || n > 65535 || strings.Trim(num, "0123456789") != "" || + (net.ParseIP(host) == nil && !p4HostReg.MatchString(host)) { + return "", "", fmt.Errorf("unsupported P4PORT %q", port) + } + return scheme, net.JoinHostPort(host, num), nil +} + +// p4SamePort reports whether two P4PORTs address the same server. +func p4SamePort(a, b string) bool { + schemeA, hostA, errA := p4ParsePort(a) + schemeB, hostB, errB := p4ParsePort(b) + return errA == nil && errB == nil && schemeA == schemeB && strings.EqualFold(hostA, hostB) +} + +// p4URLFromConfig rebuilds the p4:// URL of a workspace. +func p4URLFromConfig(cfg *p4Config, depotPath string) (*url.URL, error) { + scheme, hostport, err := p4ParsePort(cfg.port) + if err != nil { + return nil, err + } u := &url.URL{ Scheme: scheme, - Host: net.JoinHostPort(host, portNum), + Host: hostport, Path: "/" + strings.TrimLeft(depotPath, "/"), } if cfg.user != "" { @@ -325,12 +354,28 @@ func readP4Config(dir string) (*p4Config, error) { cfg.client = value } } + name := filepath.Join(dir, p4ConfigName) if cfg.port == "" || cfg.client == "" { - return nil, fmt.Errorf("%s does not set both P4PORT and P4CLIENT", filepath.Join(dir, p4ConfigName)) + return nil, fmt.Errorf("%s does not set both P4PORT and P4CLIENT", name) + } + if _, _, err := p4ParsePort(cfg.port); err != nil { + return nil, fmt.Errorf("%s: %w", name, err) + } + for _, v := range []string{cfg.client, cfg.user} { + if strings.HasPrefix(v, "-") || strings.IndexFunc(v, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }) >= 0 { + return nil, fmt.Errorf("%s: invalid P4CLIENT or P4USER %q", name, v) + } } return cfg, nil } +// isP4Workspace reports whether dir has a .p4config naming a client. A +// .p4config that only sets shared settings such as P4PORT is not a workspace. +func isP4Workspace(dir string) bool { + _, err := readP4Config(dir) + return err == nil +} + func writeP4Config(dir string, cfg *p4Config) error { content := "P4PORT=" + cfg.port + "\n" if cfg.user != "" { @@ -386,20 +431,50 @@ func p4ExistingClient(cfg *p4Config) (string, bool, error) { return spec, p4SpecField(spec, "Access") != nil, nil } -// p4UpdateClientRoot points the root of the client recorded in dir/.p4config -// at dir, e.g. after the workspace has been moved by "ghq migrate". -func p4UpdateClientRoot(dir string) error { +func p4SpecRoot(spec string) string { + if v := p4SpecField(spec, "Root"); len(v) > 0 { + return v[0] + } + return "" +} + +func p4SameRoot(a, b string) bool { + if filepath.Clean(a) == filepath.Clean(b) { + return true + } + fa, errA := os.Stat(a) + fb, errB := os.Stat(b) + return errA == nil && errB == nil && os.SameFile(fa, fb) +} + +// p4WorkspaceClient returns the spec of cfg.client after checking that the +// client exists and is rooted at root: "p4 sync" writes to the root recorded +// on the server, wherever it is run from. +func p4WorkspaceClient(cfg *p4Config, root string) (string, error) { + spec, exists, err := p4ExistingClient(cfg) + if err != nil { + return "", err + } + if !exists { + return "", fmt.Errorf("p4 client %q does not exist", cfg.client) + } + if r := p4SpecRoot(spec); !p4SameRoot(r, root) { + return "", fmt.Errorf("the root of p4 client %q is %q, not %q", cfg.client, r, root) + } + return spec, nil +} + +// p4UpdateClientRoot points the client recorded in dir/.p4config, which +// must be rooted at oldRoot, at dir after "ghq migrate" moved the workspace. +func p4UpdateClientRoot(oldRoot, dir string) error { cfg, err := readP4Config(dir) if err != nil { return err } - spec, exists, err := p4ExistingClient(cfg) + spec, err := p4WorkspaceClient(cfg, oldRoot) if err != nil { return err } - if !exists { - return fmt.Errorf("p4 client %q does not exist", cfg.client) - } return p4SaveClient(cfg, p4SetSpecField(spec, "Root", false, dir), true) } @@ -420,6 +495,23 @@ func p4Clone(vg *vcsGetOption) (err error) { hostname, _ := p4Hostname() cfg.client = p4ClientName(p4LocalUser(vg.url), hostname, cfg.port, depotPath, vg.dir) + // Claim the workspace directory: a concurrent "ghq get" of the same + // path fails here, and a failure below removes only what we created. + if err := os.MkdirAll(filepath.Dir(vg.dir), 0755); err != nil { + return err + } + if err := os.Mkdir(vg.dir, 0755); err != nil { + if os.IsExist(err) { + return fmt.Errorf("%s already exists", vg.dir) + } + return err + } + defer func() { + if err != nil { + os.RemoveAll(vg.dir) + } + }() + spec, exists, err := p4ExistingClient(cfg) if err != nil { return err @@ -427,11 +519,7 @@ func p4Clone(vg *vcsGetOption) (err error) { if exists { // A leftover client of the same workspace, e.g. after "ghq rm". // Anything else is not ours to modify. - root := "" - if v := p4SpecField(spec, "Root"); len(v) > 0 { - root = v[0] - } - if filepath.Clean(root) != filepath.Clean(vg.dir) { + if root := p4SpecRoot(spec); !p4SameRoot(root, vg.dir) { return fmt.Errorf("p4 client %q already exists with root %q", cfg.client, root) } } @@ -440,18 +528,6 @@ func p4Clone(vg *vcsGetOption) (err error) { if err := p4SaveClient(cfg, spec, vg.silent); err != nil { return err } - - if _, statErr := os.Stat(vg.dir); os.IsNotExist(statErr) { - // Do not leave a half-made workspace that later "ghq get" would skip. - defer func() { - if err != nil { - os.RemoveAll(vg.dir) - } - }() - } - if err := os.MkdirAll(vg.dir, 0755); err != nil { - return err - } if err := writeP4Config(vg.dir, cfg); err != nil { return err } @@ -479,6 +555,20 @@ var PerforceBackend = &VCSBackend{ if err != nil { return err } + // ghq paths omit the port, so p4:// URLs of different servers can + // resolve to this workspace. (Other schemes come from "ghq list".) + if vg.url != nil && (vg.url.Scheme == "p4" || vg.url.Scheme == "p4+ssl") { + want, _, err := p4ConfigFromURL(vg.url) + if err != nil { + return err + } + if !p4SamePort(want.port, cfg.port) || (want.user != "" && want.user != cfg.user) { + return fmt.Errorf("%s is a workspace of P4PORT=%s P4USER=%s, not of %s", vg.dir, cfg.port, cfg.user, vg.url.Redacted()) + } + } + if _, err := p4WorkspaceClient(cfg, vg.dir); err != nil { + return err + } return runInDir(vg.silent)(vg.dir, "p4", cfg.args("sync")...) }, // Init is unsupported: depot paths are created by submitting files. @@ -488,13 +578,10 @@ var PerforceBackend = &VCSBackend{ if err != nil { return "", err } - spec, exists, err := p4ExistingClient(cfg) + spec, err := p4WorkspaceClient(cfg, dir) if err != nil { return "", err } - if !exists { - return "", fmt.Errorf("p4 client %q does not exist", cfg.client) - } depotPath, err := p4ViewDepotPath(p4SpecField(spec, "View")) if err != nil { return "", err diff --git a/vcs_test.go b/vcs_test.go index 182ad59e..c2a744af 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -49,7 +49,8 @@ func TestVCSBackend(t *testing.T) { } defer func(orig func() (string, error)) { p4Hostname = orig }(p4Hostname) p4Hostname = func() (string, error) { return "myhost", nil } - p4Client := p4ClientName("alice", "myhost", "p4.example.com:1666", "//depot/proj/main", localDir) + p4Dir := filepath.Join(tempDir, "p4repo") + p4Client := p4ClientName("alice", "myhost", "p4.example.com:1666", "//depot/proj/main", p4Dir) testCases := []struct { name string @@ -446,17 +447,26 @@ func TestVCSBackend(t *testing.T) { }, { name: "[p4] clone", f: func() error { - defer os.Remove(filepath.Join(localDir, p4ConfigName)) return PerforceBackend.Clone(&vcsGetOption{ url: p4DummyURL, - dir: localDir, + dir: p4Dir, }) }, expect: []string{"p4", "-p", "p4.example.com:1666", "-u", "alice", "-c", p4Client, "sync"}, - dir: localDir, + dir: p4Dir, }, { name: "[p4] update", f: func() error { + defer func(orig func(cmd *exec.Cmd) error) { + cmdutil.CommandRunner = orig + }(cmdutil.CommandRunner) + cmdutil.CommandRunner = func(cmd *exec.Cmd) error { + _commands = append(_commands, cmd) + if slices.Contains(cmd.Args, "-o") { + fmt.Fprint(cmd.Stdout, p4TestSpec("ws", localDir, true, "//depot/proj/... //ws/...")) + } + return nil + } if err := writeP4Config(localDir, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { return err } @@ -665,6 +675,7 @@ func fakeP4(t *testing.T, respond func(sub string, cmd *exec.Cmd) error) *[]p4Ca return err } c.stdin = string(b) + cmd.Stdin = strings.NewReader(c.stdin) } *calls = append(*calls, c) if cmd.Args[0] != "p4" { @@ -863,19 +874,10 @@ func TestPerforceBackend_Clone(t *testing.T) { errStr: "exit status 1", calls: 3, }, { - name: "sync fails in existing directory", - url: "p4://p4.example.com/depot/proj/main", - mkdir: true, - respond: func(client, dir string) func(string, *exec.Cmd) error { - return func(sub string, cmd *exec.Cmd) error { - if sub == "sync" { - return errors.New("exit status 1") - } - return nil - } - }, - errStr: "exit status 1", - calls: 3, + name: "existing directory", + url: "p4://p4.example.com/depot/proj/main", + mkdir: true, + errStr: "already exists", }, { name: "branch", url: "p4://p4.example.com/depot/proj/main", @@ -983,11 +985,17 @@ func TestPerforceBackend_Update(t *testing.T) { if err := os.WriteFile(filepath.Join(dir, p4ConfigName), []byte(config), 0644); err != nil { t.Fatal(err) } - calls := fakeP4(t, nil) - if err := PerforceBackend.Update(&vcsGetOption{dir: dir, silent: true}); err != nil { + srv := newP4FakeServer() + srv.clients["bob_ws"] = p4TestSpec("bob_ws", dir, true, "//depot/proj/... //bob_ws/...") + calls := fakeP4(t, srv.respond) + if err := PerforceBackend.Update(&vcsGetOption{dir: dir, silent: true, url: mustParseURL("p4+ssl://bob@p4.example.com/depot/proj")}); err != nil { t.Fatal(err) } - expect := []p4Call{{args: []string{"p4", "-p", "ssl:p4.example.com:1666", "-u", "bob", "-c", "bob_ws", "sync"}, dir: dir}} + global := []string{"p4", "-p", "ssl:p4.example.com:1666", "-u", "bob", "-c", "bob_ws"} + expect := []p4Call{ + {args: append(global[:7:7], "client", "-o", "bob_ws")}, + {args: append(global[:7:7], "sync"), dir: dir}, + } if !reflect.DeepEqual(*calls, expect) { t.Errorf("\ngot: %+v\nexpect: %+v", *calls, expect) } @@ -1022,42 +1030,42 @@ func TestPerforceBackend_RemoteURL(t *testing.T) { testCases := []struct { name string config string - spec func(client string) string + spec func(client, root string) string expect string errStr string }{{ name: "tcp", config: "P4PORT=p4.example.com:1666\nP4CLIENT=ws\n", - spec: func(c string) string { - return p4TestSpec(c, "/r", true, "//depot/proj/main/... //ws/...", "-//depot/proj/main/.p4config //ws/.p4config") + spec: func(c, root string) string { + return p4TestSpec(c, root, true, "//depot/proj/main/... //ws/...", "-//depot/proj/main/.p4config //ws/.p4config") }, expect: "p4://p4.example.com:1666/depot/proj/main", }, { name: "ssl with user", config: "P4PORT=ssl:p4.example.com:1667\nP4USER=bob\nP4CLIENT=ws\n", - spec: func(c string) string { - return p4TestSpec(c, "/r", true, "-//depot/x/... //ws/x/...", "//depot/proj/... //ws/...") + spec: func(c, root string) string { + return p4TestSpec(c, root, true, "-//depot/x/... //ws/x/...", "//depot/proj/... //ws/...") }, expect: "p4+ssl://bob@p4.example.com:1667/depot/proj", }, { name: "quoted view", config: "P4PORT=1666\nP4CLIENT=ws\n", - spec: func(c string) string { - return p4TestSpec(c, "/r", true, `"//depot/my proj/..." "//ws/..."`) + spec: func(c, root string) string { + return p4TestSpec(c, root, true, `"//depot/my proj/..." "//ws/..."`) }, expect: "p4://localhost:1666/depot/my%20proj", }, { name: "client deleted on server", config: "P4PORT=p4.example.com:1666\nP4CLIENT=ws\n", - spec: func(c string) string { - return p4TestSpec(c, "/r", false, "//depot/... //ws/...") + spec: func(c, root string) string { + return p4TestSpec(c, root, false, "//depot/... //ws/...") }, errStr: "does not exist", }, { name: "no depot path in view", config: "P4PORT=p4.example.com:1666\nP4CLIENT=ws\n", - spec: func(c string) string { - return p4TestSpec(c, "/r", true, "//depot/file.txt //ws/file.txt") + spec: func(c, root string) string { + return p4TestSpec(c, root, true, "//depot/file.txt //ws/file.txt") }, errStr: "no depot path found", }} @@ -1069,7 +1077,7 @@ func TestPerforceBackend_RemoteURL(t *testing.T) { } calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { if sub == "client -o ws" { - fmt.Fprint(cmd.Stdout, tc.spec("ws")) + fmt.Fprint(cmd.Stdout, tc.spec("ws", dir)) } return nil }) @@ -1104,7 +1112,7 @@ func TestP4UpdateClientRoot(t *testing.T) { } return nil }) - if err := p4UpdateClientRoot(dir); err != nil { + if err := p4UpdateClientRoot("/old/root", dir); err != nil { t.Fatal(err) } if len(*calls) != 2 { @@ -1275,3 +1283,175 @@ func TestP4Spec(t *testing.T) { } } } + +// p4FakeServer is an in-memory stand-in for a p4 server's client specs. +type p4FakeServer struct { + clients map[string]string // name -> spec + fail map[string]string // subcommand -> error message +} + +func newP4FakeServer() *p4FakeServer { + return &p4FakeServer{clients: map[string]string{}, fail: map[string]string{}} +} + +func (s *p4FakeServer) respond(sub string, cmd *exec.Cmd) error { + if msg, ok := s.fail[sub]; ok { + fmt.Fprintln(cmd.Stderr, msg) + return errors.New("exit status 1") + } + switch { + case strings.HasPrefix(sub, "client -o "): + name := strings.TrimPrefix(sub, "client -o ") + if spec, ok := s.clients[name]; ok { + fmt.Fprint(cmd.Stdout, spec) + } else { + fmt.Fprint(cmd.Stdout, p4TestSpec(name, "/home/alice", false, "//depot/... //"+name+"/...")) + } + case sub == "client -i": + b, err := io.ReadAll(cmd.Stdin) + if err != nil { + return err + } + spec := string(b) + if p4SpecField(spec, "Access") == nil { + spec = strings.Replace(spec, "\nOwner:", "\nAccess:\t2026/01/02 03:04:05\n\nOwner:", 1) + } + s.clients[p4SpecField(spec, "Client")[0]] = spec + case sub == "sync" || sub == "sync -f": + return os.WriteFile(filepath.Join(cmd.Dir, "README"), []byte("hello\n"), 0644) + } + return nil +} + +// Regression tests for the first adversarial review round. +func TestPerforceBackend_Review1(t *testing.T) { + const port = "p4.example.com:1666" + writeConfig := func(t *testing.T, dir, content string) { + t.Helper() + if err := os.WriteFile(filepath.Join(dir, p4ConfigName), []byte(content), 0644); err != nil { + t.Fatal(err) + } + } + + t.Run("rsh P4PORT in .p4config is never passed to p4", func(t *testing.T) { + dir := newTempDir(t) + writeConfig(t, dir, "P4PORT=rsh:touch pwned\nP4CLIENT=ws\n") + calls := fakeP4(t, nil) + if err := PerforceBackend.Update(&vcsGetOption{dir: dir}); err == nil { + t.Error("Update: error should be occurred, but nil") + } + if _, err := PerforceBackend.RemoteURL(dir); err == nil { + t.Error("RemoteURL: error should be occurred, but nil") + } + if len(*calls) != 0 { + t.Errorf("p4 should not be run: %+v", *calls) + } + if b := findVCSBackend(dir, ""); b != nil { + t.Errorf("a .p4config with an unsupported P4PORT should not mark a workspace: %v", b) + } + }) + + t.Run("get -u from another server", func(t *testing.T) { + dir := newTempDir(t) + writeConfig(t, dir, "P4PORT="+port+"\nP4CLIENT=ws\n") + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", dir, true, "//depot/proj/... //ws/...") + calls := fakeP4(t, srv.respond) + for _, u := range []string{ + "p4+ssl://p4.example.com:1667/depot/proj", + "p4://p4.example.com:1667/depot/proj", + "p4://bob@p4.example.com:1666/depot/proj", + "p4://p4.example.com/depot/proj#123", + } { + if err := PerforceBackend.Update(&vcsGetOption{dir: dir, url: mustParseURL(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("should not sync: %v", c.args) + } + } + // equivalent spellings of the same server are accepted + for _, u := range []string{"p4://P4.example.com/depot/proj", "p4://p4.example.com:1666/depot/proj", "https://p4.example.com/depot/proj"} { + if err := PerforceBackend.Update(&vcsGetOption{dir: dir, url: mustParseURL(u)}); err != nil { + t.Errorf("%s: %v", u, err) + } + } + }) + + t.Run("get -u of a client rooted elsewhere or deleted", func(t *testing.T) { + dir := newTempDir(t) + writeConfig(t, dir, "P4PORT="+port+"\nP4CLIENT=ws\n") + for name, spec := range map[string]string{ + "elsewhere": p4TestSpec("ws", "/elsewhere", true, "//depot/proj/... //ws/..."), + "deleted": p4TestSpec("ws", "/home/alice", false, "//depot/... //ws/..."), + } { + t.Run(name, func(t *testing.T) { + srv := newP4FakeServer() + srv.clients["ws"] = spec + calls := fakeP4(t, srv.respond) + if err := PerforceBackend.Update(&vcsGetOption{dir: dir}); err == nil { + t.Error("error should be occurred, but nil") + } + if _, err := PerforceBackend.RemoteURL(dir); err == nil { + t.Error("RemoteURL: error should be occurred, but nil") + } + for _, c := range *calls { + if slices.Contains(c.args, "sync") { + t.Errorf("should not sync: %v", c.args) + } + } + }) + } + }) + + t.Run("clone into a directory created concurrently", func(t *testing.T) { + dir := filepath.Join(newTempDir(t), "p4.example.com", "depot", "proj") + if err := os.MkdirAll(dir, 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "theirs"), nil, 0644); err != nil { + t.Fatal(err) + } + srv := newP4FakeServer() + srv.fail["sync"] = "sync failed" + calls := fakeP4(t, srv.respond) + if err := PerforceBackend.Clone(&vcsGetOption{url: mustParseURL("p4://p4.example.com/depot/proj"), dir: dir}); err == nil { + t.Error("error should be occurred, but nil") + } + if _, err := os.Stat(filepath.Join(dir, "theirs")); err != nil { + t.Errorf("files of the other checkout should be kept: %v", err) + } + if len(*calls) != 0 { + t.Errorf("p4 should not be run: %+v", *calls) + } + }) + + t.Run("revision fragments and queries", func(t *testing.T) { + for _, u := range []string{"p4://p4.example.com/depot/proj#123", "p4://p4.example.com/depot/proj?rev=1"} { + dir := filepath.Join(newTempDir(t), "p4.example.com", "depot", "proj") + calls := fakeP4(t, newP4FakeServer().respond) + if err := PerforceBackend.Clone(&vcsGetOption{url: mustParseURL(u), dir: dir}); err == nil { + t.Errorf("%s: error should be occurred, but nil", u) + } + if len(*calls) != 0 { + t.Errorf("%s: p4 should not be run: %+v", u, *calls) + } + } + }) + + t.Run("client names of similar hosts and users differ", func(t *testing.T) { + for _, pair := range [][2][2]string{ + {{"alice", "build.runner"}, {"alice", "build_runner"}}, + {{"alice", "ci-runner-" + strings.Repeat("x", 40) + "-1"}, {"alice", "ci-runner-" + strings.Repeat("x", 40) + "-2"}}, + {{"a.b", "host"}, {"a_b", "host"}}, + } { + a := p4ClientName(pair[0][0], pair[0][1], port, "//depot/proj", "/ghq/p4.example.com/depot/proj") + b := p4ClientName(pair[1][0], pair[1][1], port, "//depot/proj", "/ghq/p4.example.com/depot/proj") + if a == b { + t.Errorf("client names should differ for %q: %s", pair, a) + } + } + }) +} From 8f1e8c15f84e080eb71897cf98a5904323c4839b Mon Sep 17 00:00:00 2001 From: t04dJ14n9 <61448324+t04dJ14n9@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:22:28 +0800 Subject: [PATCH 3/4] p4: address second adversarial review - Clone: until "p4 sync" starts, clean up only .p4config and the directory if still empty, so a nested checkout created meanwhile is never deleted; keep a workspace whose sync failed for "ghq get -u". - get -u: reject @revision (the getter now passes it to Update) and a p4:// URL whose depot path is not the client's (ghq strips ".git"). - migrate: capture the client root before moving and compare the server's value afterwards; after a failed save, re-read the client to tell an applied save (lost reply) from an unchanged one before moving back, and leave the workspace in place when the outcome is unknown. Co-Authored-By: Claude Opus 5.5 --- README.adoc | 4 +- cmd_get_test.go | 27 +++++++++ cmd_migrate.go | 18 ++++-- cmd_migrate_test.go | 69 ++++++++++++++++++++++ getter.go | 1 + vcs_p4.go | 88 +++++++++++++++++++++++----- vcs_test.go | 139 +++++++++++++++++++++++++++++++++++++++----- 7 files changed, 309 insertions(+), 37 deletions(-) diff --git a/README.adoc b/README.adoc index 54202385..58e5cf00 100644 --- a/README.adoc +++ b/README.adoc @@ -210,7 +210,9 @@ The client is named `___` and its root is the 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`. +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 diff --git a/cmd_get_test.go b/cmd_get_test.go index 43be1884..d6a4ce1a 100644 --- a/cmd_get_test.go +++ b/cmd_get_test.go @@ -809,3 +809,30 @@ func TestDoGet_p4Revision(t *testing.T) { } } } + +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) + } + } + } +} diff --git a/cmd_migrate.go b/cmd_migrate.go index e73d6c54..31f13d1b 100644 --- a/cmd_migrate.go +++ b/cmd_migrate.go @@ -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 { @@ -158,10 +166,12 @@ func doMigrate(ctx context.Context, cmd *cli.Command) error { } } - // A p4 client records its root on the server, and the workspace is - // unusable until it points at the new location. - if vcsBackend == PerforceBackend { - if err := p4UpdateClientRoot(absDir, destPath); err != nil { + // 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) } diff --git a/cmd_migrate_test.go b/cmd_migrate_test.go index 60b0c6fa..3b9059cc 100644 --- a/cmd_migrate_test.go +++ b/cmd_migrate_test.go @@ -564,3 +564,72 @@ func TestDoMigrate_p4Review1(t *testing.T) { } }) } + +func TestDoMigrate_p4Review2(t *testing.T) { + tmpdir := newTempDir(t) + setRepositoryPathTestRoot(t, tmpdir) + dest := filepath.Join(tmpdir, "p4.example.com", "depot", "proj", "main") + newSrc := func(t *testing.T, parent string) string { + src := filepath.Join(parent, "proj") + os.MkdirAll(src, 0755) + if err := writeP4Config(src, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.RemoveAll(dest); os.RemoveAll(src) }) + return src + } + migrate := func(src string) error { + var err error + capture(func() { err = newApp().Run(context.Background(), []string{"ghq", "migrate", "-y", src}) }) + return err + } + + t.Run("save applied but reply lost", func(t *testing.T) { + src := newSrc(t, filepath.Join(tmpdir, "lost")) + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", src, true, "//depot/proj/main/... //ws/...") + srv.lostReply = true + fakeP4(t, srv.respond) + if err := migrate(src); err != nil { + t.Errorf("migration should succeed when the new root was saved: %v", err) + } + if _, err := os.Stat(filepath.Join(dest, p4ConfigName)); err != nil { + t.Errorf("workspace should be at the destination recorded on the server: %v", err) + } + }) + + t.Run("outcome unknown", func(t *testing.T) { + src := newSrc(t, filepath.Join(tmpdir, "unknown")) + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", src, true, "//depot/proj/main/... //ws/...") + srv.offlineAfterSave = true + fakeP4(t, srv.respond) + err := migrate(src) + if err == nil || !strings.Contains(err.Error(), dest) { + t.Errorf("error should tell where the workspace is, but: %v", err) + } + if _, err := os.Stat(filepath.Join(dest, p4ConfigName)); err != nil { + t.Errorf("workspace should not be moved back blindly: %v", err) + } + }) + + t.Run("source reached through a symlink", func(t *testing.T) { + real := filepath.Join(tmpdir, "real") + os.MkdirAll(real, 0755) + link := filepath.Join(tmpdir, "link") + if err := os.Symlink(real, link); err != nil { + t.Skip(err) + } + newSrc(t, real) + src := filepath.Join(link, "proj") + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", filepath.Join(real, "proj"), true, "//depot/proj/main/... //ws/...") + fakeP4(t, srv.respond) + if err := migrate(src); err != nil { + t.Errorf("migration through an alias of the client root should succeed: %v", err) + } + if got := p4SpecRoot(srv.clients["ws"]); got != dest { + t.Errorf("client root: got %q, expect %q", got, dest) + } + }) +} diff --git a/getter.go b/getter.go index 540900f8..6c5a8020 100644 --- a/getter.go +++ b/getter.go @@ -152,6 +152,7 @@ func (g *getter) getRemoteRepository(ctx context.Context, remote RemoteRepositor return info, vcs.Update(&vcsGetOption{ url: repoURL, dir: localRepoRoot, + branch: branch, silent: g.silent, recursive: g.recursive, bare: g.bareMode != BareNone, diff --git a/vcs_p4.go b/vcs_p4.go index 25cb2aff..f35e8810 100644 --- a/vcs_p4.go +++ b/vcs_p4.go @@ -37,6 +37,11 @@ const ( p4DefaultPort = "1666" ) +var ( + errP4Branch = errors.New("--branch option (or @ in the URL) is unavailable for Perforce; specify the depot path of the branch in the URL") + errP4RootUnchanged = errors.New("the root of the p4 client was not changed") +) + // p4Config holds the connection settings recorded in a workspace's .p4config. type p4Config struct { port, user, client string @@ -464,23 +469,56 @@ func p4WorkspaceClient(cfg *p4Config, root string) (string, error) { return spec, nil } -// p4UpdateClientRoot points the client recorded in dir/.p4config, which -// must be rooted at oldRoot, at dir after "ghq migrate" moved the workspace. -func p4UpdateClientRoot(oldRoot, dir string) error { +// p4RootMove repoints the client of a workspace moved by "ghq migrate". +type p4RootMove struct { + cfg *p4Config + root string // as recorded on the server before the move +} + +// p4PrepareRootMove checks, before dir is moved, that dir is the root of +// the client named in its .p4config. +func p4PrepareRootMove(dir string) (*p4RootMove, error) { cfg, err := readP4Config(dir) if err != nil { - return err + return nil, err } - spec, err := p4WorkspaceClient(cfg, oldRoot) + spec, err := p4WorkspaceClient(cfg, dir) if err != nil { - return err + return nil, err + } + return &p4RootMove{cfg: cfg, root: p4SpecRoot(spec)}, nil +} + +// apply sets the root of the client to newRoot. Errors wrap +// errP4RootUnchanged when the server is known to keep the old root. +func (m *p4RootMove) apply(newRoot string) error { + spec, exists, err := p4ExistingClient(m.cfg) + if err != nil { + return fmt.Errorf("%w: %w", errP4RootUnchanged, err) + } + if !exists || p4SpecRoot(spec) != m.root { + return fmt.Errorf("%w: p4 client %q was changed or deleted meanwhile", errP4RootUnchanged, m.cfg.client) + } + saveErr := p4SaveClient(m.cfg, p4SetSpecField(spec, "Root", false, newRoot), true) + if saveErr == nil { + return nil + } + // p4 may fail after the server saved the spec, e.g. if its reply is lost. + spec, exists, err = p4ExistingClient(m.cfg) + switch { + case err != nil: + return fmt.Errorf("%w (checking the client afterwards failed: %v)", saveErr, err) + case exists && p4SpecRoot(spec) == newRoot: + return nil + case exists && p4SpecRoot(spec) == m.root: + return fmt.Errorf("%w: %w", errP4RootUnchanged, saveErr) } - return p4SaveClient(cfg, p4SetSpecField(spec, "Root", false, dir), true) + return saveErr } func p4Clone(vg *vcsGetOption) (err error) { if vg.branch != "" { - return errors.New("--branch option (or @ in the URL) is unavailable for Perforce; specify the depot path of the branch in the URL") + return errP4Branch } if vg.bare { return errors.New("bare checkouts are unavailable for Perforce") @@ -496,7 +534,9 @@ func p4Clone(vg *vcsGetOption) (err error) { cfg.client = p4ClientName(p4LocalUser(vg.url), hostname, cfg.port, depotPath, vg.dir) // Claim the workspace directory: a concurrent "ghq get" of the same - // path fails here, and a failure below removes only what we created. + // path fails here. Until the sync starts, the directory holds at most + // our .p4config, so a failure removes just that and the directory if it + // is still empty, keeping e.g. a nested checkout made meanwhile. if err := os.MkdirAll(filepath.Dir(vg.dir), 0755); err != nil { return err } @@ -506,9 +546,11 @@ func p4Clone(vg *vcsGetOption) (err error) { } return err } + syncing := false defer func() { - if err != nil { - os.RemoveAll(vg.dir) + if err != nil && !syncing { + os.Remove(filepath.Join(vg.dir, p4ConfigName)) + os.Remove(vg.dir) } }() @@ -536,8 +578,10 @@ func p4Clone(vg *vcsGetOption) (err error) { // The have list of a reused client describes files that are gone. syncArgs = append(syncArgs, "-f") } + syncing = true if err := runInDir(vg.silent)(vg.dir, "p4", cfg.args(syncArgs...)...); err != nil { - return err + // Keep what was synced; the client and .p4config are in place. + return fmt.Errorf("%w; run 'ghq get -u' to resume syncing %s", err, vg.dir) } if entries, err := os.ReadDir(vg.dir); err == nil && len(entries) == 1 { logger.Log("warning", fmt.Sprintf("no files found in %s/...; the workspace is empty", depotPath)) @@ -551,14 +595,20 @@ var PerforceBackend = &VCSBackend{ // only the synced revisions anyway. Clone: p4Clone, Update: func(vg *vcsGetOption) error { + if vg.branch != "" { + return errP4Branch + } cfg, err := readP4Config(vg.dir) if err != nil { return err } - // ghq paths omit the port, so p4:// URLs of different servers can - // resolve to this workspace. (Other schemes come from "ghq list".) + // ghq paths omit the port and a trailing ".git", so p4:// URLs of + // other servers or depot paths can resolve to this workspace. + // (Other schemes come from "ghq list" and carry no p4 identity.) + var depotPath string if vg.url != nil && (vg.url.Scheme == "p4" || vg.url.Scheme == "p4+ssl") { - want, _, err := p4ConfigFromURL(vg.url) + var want *p4Config + want, depotPath, err = p4ConfigFromURL(vg.url) if err != nil { return err } @@ -566,9 +616,15 @@ var PerforceBackend = &VCSBackend{ return fmt.Errorf("%s is a workspace of P4PORT=%s P4USER=%s, not of %s", vg.dir, cfg.port, cfg.user, vg.url.Redacted()) } } - if _, err := p4WorkspaceClient(cfg, vg.dir); err != nil { + spec, err := p4WorkspaceClient(cfg, vg.dir) + if err != nil { return err } + if depotPath != "" { + if got, _ := p4ViewDepotPath(p4SpecField(spec, "View")); got != depotPath { + return fmt.Errorf("p4 client %q of %s maps %q, not %q", cfg.client, vg.dir, got, depotPath) + } + } return runInDir(vg.silent)(vg.dir, "p4", cfg.args("sync")...) }, // Init is unsupported: depot paths are created by submitting files. diff --git a/vcs_test.go b/vcs_test.go index c2a744af..adaa33e5 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -819,6 +819,7 @@ func TestPerforceBackend_Clone(t *testing.T) { branch string bare bool mkdir bool + kept bool // a failed sync keeps the workspace respond func(client, dir string) func(sub string, cmd *exec.Cmd) error errStr string calls int @@ -871,8 +872,9 @@ func TestPerforceBackend_Clone(t *testing.T) { return nil } }, - errStr: "exit status 1", + errStr: "run 'ghq get -u' to resume", calls: 3, + kept: true, }, { name: "existing directory", url: "p4://p4.example.com/depot/proj/main", @@ -967,9 +969,9 @@ func TestPerforceBackend_Clone(t *testing.T) { t.Errorf("got %d commands, expect %d: %+v", len(*calls), tc.calls, *calls) } _, statErr := os.Stat(dir) - if tc.mkdir { + if tc.mkdir || tc.kept { if statErr != nil { - t.Errorf("existing directory should be kept: %v", statErr) + t.Errorf("directory should be kept: %v", statErr) } } else if !os.IsNotExist(statErr) { t.Errorf("directory should not be left behind: %v", statErr) @@ -1101,28 +1103,40 @@ func TestPerforceBackend_RemoteURL(t *testing.T) { } } -func TestP4UpdateClientRoot(t *testing.T) { +func TestP4RootMove(t *testing.T) { dir := newTempDir(t) if err := writeP4Config(dir, &p4Config{port: "p4.example.com:1666", client: "ws"}); err != nil { t.Fatal(err) } - calls := fakeP4(t, func(sub string, cmd *exec.Cmd) error { - if sub == "client -o ws" { - fmt.Fprint(cmd.Stdout, p4TestSpec("ws", "/old/root", true, "//depot/proj/... //ws/...")) - } - return nil - }) - if err := p4UpdateClientRoot("/old/root", dir); err != nil { + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", dir, true, "//depot/proj/... //ws/...") + fakeP4(t, srv.respond) + m, err := p4PrepareRootMove(dir) + if err != nil { t.Fatal(err) } - if len(*calls) != 2 { - t.Fatalf("unexpected commands: %+v", *calls) + if err := m.apply("/new/root"); err != nil { + t.Fatal(err) } - spec := (*calls)[1].stdin - if !strings.Contains(spec, "\nRoot:\t"+dir+"\n") || strings.Contains(spec, "/old/root") || - !strings.Contains(spec, "\n\t//depot/proj/... //ws/...\n") { + spec := srv.clients["ws"] + if p4SpecRoot(spec) != "/new/root" || !strings.Contains(spec, "\n\t//depot/proj/... //ws/...\n") { t.Errorf("unexpected client spec:\n%s", spec) } + + // changed on the server between prepare and apply + srv.clients["ws"] = p4TestSpec("ws", "/someone/else", true, "//depot/proj/... //ws/...") + if err := m.apply("/new/root"); !errors.Is(err, errP4RootUnchanged) { + t.Errorf("error should wrap errP4RootUnchanged, but: %v", err) + } + // rejected save + srv.clients["ws"] = p4TestSpec("ws", "/new/root", true, "//depot/proj/... //ws/...") + srv.fail["client -i"] = "Locked client" + if err := m.apply("/newer/root"); !errors.Is(err, errP4RootUnchanged) { + t.Errorf("error should wrap errP4RootUnchanged, but: %v", err) + } + if _, err := p4PrepareRootMove(newTempDir(t)); err == nil { + t.Error("error should be occurred for a directory without .p4config") + } } func TestP4ConfigFromURL(t *testing.T) { @@ -1288,6 +1302,11 @@ func TestP4Spec(t *testing.T) { type p4FakeServer struct { clients map[string]string // name -> spec fail map[string]string // subcommand -> error message + // lostReply makes "client -i" save the spec but fail as if its reply was + // lost; offlineAfterSave fails "client -i" and every command after it. + lostReply, offlineAfterSave, offline bool + // onClientO runs before "client -o" replies, e.g. to race with it. + onClientO func() } func newP4FakeServer() *p4FakeServer { @@ -1295,10 +1314,22 @@ func newP4FakeServer() *p4FakeServer { } func (s *p4FakeServer) respond(sub string, cmd *exec.Cmd) error { + if s.offline { + fmt.Fprintln(cmd.Stderr, "Connect to server failed; check $P4PORT.") + return errors.New("exit status 1") + } + if sub == "client -i" && s.offlineAfterSave { + s.offline = true // the connection drops during the save + fmt.Fprintln(cmd.Stderr, "TCP receive failed.") + return errors.New("exit status 1") + } if msg, ok := s.fail[sub]; ok { fmt.Fprintln(cmd.Stderr, msg) return errors.New("exit status 1") } + if strings.HasPrefix(sub, "client -o ") && s.onClientO != nil { + s.onClientO() + } switch { case strings.HasPrefix(sub, "client -o "): name := strings.TrimPrefix(sub, "client -o ") @@ -1317,6 +1348,10 @@ func (s *p4FakeServer) respond(sub string, cmd *exec.Cmd) error { spec = strings.Replace(spec, "\nOwner:", "\nAccess:\t2026/01/02 03:04:05\n\nOwner:", 1) } s.clients[p4SpecField(spec, "Client")[0]] = spec + if s.lostReply { + fmt.Fprintln(cmd.Stderr, "TCP receive failed.") + return errors.New("exit status 1") + } case sub == "sync" || sub == "sync -f": return os.WriteFile(filepath.Join(cmd.Dir, "README"), []byte("hello\n"), 0644) } @@ -1455,3 +1490,75 @@ func TestPerforceBackend_Review1(t *testing.T) { } }) } + +// Regression tests for the second adversarial review round. +func TestPerforceBackend_Review2(t *testing.T) { + const port = "p4.example.com:1666" + + t.Run("failed clone keeps a nested checkout created meanwhile", func(t *testing.T) { + dir := filepath.Join(newTempDir(t), "p4.example.com", "depot", "proj") + srv := newP4FakeServer() + srv.fail["client -i"] = "Error in client specification." + srv.onClientO = func() { // another ghq get of //depot/proj/child + os.MkdirAll(filepath.Join(dir, "child"), 0755) + writeP4Config(filepath.Join(dir, "child"), &p4Config{port: port, client: "child"}) + } + fakeP4(t, srv.respond) + if err := PerforceBackend.Clone(&vcsGetOption{url: mustParseURL("p4://p4.example.com/depot/proj"), dir: dir}); err == nil { + t.Error("error should be occurred, but nil") + } + if _, err := os.Stat(filepath.Join(dir, "child", p4ConfigName)); err != nil { + t.Errorf("the nested checkout should be kept: %v", err) + } + if _, err := os.Stat(filepath.Join(dir, p4ConfigName)); !os.IsNotExist(err) { + t.Errorf(".p4config of the failed clone should be removed: %v", err) + } + }) + + t.Run("failed sync keeps the workspace for get -u", func(t *testing.T) { + dir := filepath.Join(newTempDir(t), "p4.example.com", "depot", "proj") + srv := newP4FakeServer() + srv.fail["sync"] = "TCP receive failed." + fakeP4(t, srv.respond) + err := PerforceBackend.Clone(&vcsGetOption{url: mustParseURL("p4://p4.example.com/depot/proj"), dir: dir}) + if err == nil || !strings.Contains(err.Error(), "ghq get -u") { + t.Errorf("error should suggest ghq get -u, but: %v", err) + } + if !isP4Workspace(dir) { + t.Error("the workspace should be kept") + } + }) + + t.Run("get -u of another depot path mapped to the same directory", func(t *testing.T) { + dir := newTempDir(t) + writeP4Config(dir, &p4Config{port: port, client: "ws"}) + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", dir, true, `"//depot/app/..." "//ws/..."`, `"-//depot/app/.p4config" "//ws/.p4config"`) + calls := fakeP4(t, srv.respond) + if err := PerforceBackend.Update(&vcsGetOption{dir: dir, url: mustParseURL("p4://p4.example.com/depot/app.git")}); err == nil { + t.Error("error should be occurred, but nil") + } + for _, c := range *calls { + if slices.Contains(c.args, "sync") { + t.Errorf("should not sync: %v", c.args) + } + } + if err := PerforceBackend.Update(&vcsGetOption{dir: dir, url: mustParseURL("p4://p4.example.com/depot/app/")}); err != nil { + t.Error(err) + } + }) + + t.Run("get -u with a revision", func(t *testing.T) { + dir := newTempDir(t) + writeP4Config(dir, &p4Config{port: port, client: "ws"}) + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", dir, true, "//depot/app/... //ws/...") + calls := fakeP4(t, srv.respond) + if err := PerforceBackend.Update(&vcsGetOption{dir: dir, branch: "123"}); err == nil { + t.Error("error should be occurred, but nil") + } + if len(*calls) != 0 { + t.Errorf("p4 should not be run: %+v", *calls) + } + }) +} From d76234d30a0f5c1ba8e8875a091f0bf04711e352 Mon Sep 17 00:00:00 2001 From: t04dJ14n9 <61448324+t04dJ14n9@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:39:17 +0800 Subject: [PATCH 4/4] p4: address third adversarial review - Clone: clear a reused client's have list with "sync -k //client/...#none" and run a plain sync instead of "sync -f", so that an interrupted sync is resumed correctly by "ghq get -u". - Reject p4+ schemes other than p4+ssl instead of falling back to TCP. - get -u: check the user, an explicit port and the depot path of every URL, not only p4:// ones (e.g. --vcs=p4 with an https URL); allow URLs of directories inside the workspace. - migrate: treat a client already rooted at the destination as moved, and do not move the workspace back when its root was changed by others. - list: ignore .p4config in a root or directory, where ghq never creates p4 workspaces, so shared settings naming a client do not hide other repositories. Co-Authored-By: Claude Opus 5.5 --- cmd_list_test.go | 6 +- local_repository.go | 5 ++ vcs_p4.go | 35 ++++++++---- vcs_test.go | 134 +++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 159 insertions(+), 21 deletions(-) diff --git a/cmd_list_test.go b/cmd_list_test.go index ec85a2e4..e2150e53 100644 --- a/cmd_list_test.go +++ b/cmd_list_test.go @@ -330,12 +330,16 @@ func TestDoList_sharedP4config(t *testing.T) { 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 / + // 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\n" { + if out != "p4.example.com/team/app\np4.other.example.com/team/lib\n" { t.Errorf("got %q", out) } } diff --git a/local_repository.go b/local_repository.go index cd187271..3a2af099 100644 --- a/local_repository.go +++ b/local_repository.go @@ -362,6 +362,11 @@ func walkLocalRepositories(vcs string, callback func(*LocalRepository)) error { if err != nil || repo == nil { return nil } + // A .p4config in a root or directory configures several + // workspaces; ghq creates p4 workspaces at //... only. + if vcsBackend == PerforceBackend && (repo.RelPath == "." || len(repo.PathParts) < 2) { + return nil + } callback(repo) if isSymlink { diff --git a/vcs_p4.go b/vcs_p4.go index f35e8810..188e2e17 100644 --- a/vcs_p4.go +++ b/vcs_p4.go @@ -71,6 +71,9 @@ func p4ConfigFromURL(u *url.URL) (*p4Config, string, error) { if u.Opaque != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" { return nil, "", fmt.Errorf("invalid p4 URL %q: queries, fragments and revision specifiers are not supported", u.Redacted()) } + if strings.HasPrefix(u.Scheme, "p4+") && u.Scheme != "p4+ssl" { + return nil, "", fmt.Errorf("unsupported scheme %q: use p4:// or p4+ssl://", u.Scheme) + } host := u.Hostname() if host == "" { return nil, "", fmt.Errorf("missing p4 server host in URL %q", u) @@ -494,10 +497,14 @@ func p4PrepareRootMove(dir string) (*p4RootMove, error) { func (m *p4RootMove) apply(newRoot string) error { spec, exists, err := p4ExistingClient(m.cfg) if err != nil { + // Nothing was sent, and the root was m.root when checked before the move. return fmt.Errorf("%w: %w", errP4RootUnchanged, err) } - if !exists || p4SpecRoot(spec) != m.root { - return fmt.Errorf("%w: p4 client %q was changed or deleted meanwhile", errP4RootUnchanged, m.cfg.client) + switch root := p4SpecRoot(spec); { + case exists && root == newRoot: + return nil + case !exists || root != m.root: + return fmt.Errorf("p4 client %q was changed or deleted meanwhile (root %q)", m.cfg.client, root) } saveErr := p4SaveClient(m.cfg, p4SetSpecField(spec, "Root", false, newRoot), true) if saveErr == nil { @@ -573,13 +580,16 @@ func p4Clone(vg *vcsGetOption) (err error) { if err := writeP4Config(vg.dir, cfg); err != nil { return err } - syncArgs := []string{"sync"} if exists { // The have list of a reused client describes files that are gone. - syncArgs = append(syncArgs, "-f") + // Clear it (without touching files) rather than "sync -f", so that a + // sync interrupted below can be resumed by a plain "p4 sync". + if err := runInDir(vg.silent)(vg.dir, "p4", cfg.args("sync", "-k", "//"+cfg.client+"/...#none")...); err != nil { + return err + } } syncing = true - if err := runInDir(vg.silent)(vg.dir, "p4", cfg.args(syncArgs...)...); err != nil { + if err := runInDir(vg.silent)(vg.dir, "p4", cfg.args("sync")...); err != nil { // Keep what was synced; the client and .p4config are in place. return fmt.Errorf("%w; run 'ghq get -u' to resume syncing %s", err, vg.dir) } @@ -602,17 +612,19 @@ var PerforceBackend = &VCSBackend{ if err != nil { return err } - // ghq paths omit the port and a trailing ".git", so p4:// URLs of - // other servers or depot paths can resolve to this workspace. - // (Other schemes come from "ghq list" and carry no p4 identity.) + // ghq paths omit the port, the user and a trailing ".git", so URLs + // of other servers, users or depot paths can resolve to this + // workspace. The server is named by p4:// URLs and explicit ports; + // "ghq list | ghq get -u" passes https:///. var depotPath string - if vg.url != nil && (vg.url.Scheme == "p4" || vg.url.Scheme == "p4+ssl") { + if vg.url != nil { var want *p4Config want, depotPath, err = p4ConfigFromURL(vg.url) if err != nil { return err } - if !p4SamePort(want.port, cfg.port) || (want.user != "" && want.user != cfg.user) { + namesPort := vg.url.Scheme == "p4" || vg.url.Scheme == "p4+ssl" || vg.url.Port() != "" + if (namesPort && !p4SamePort(want.port, cfg.port)) || (want.user != "" && want.user != cfg.user) { return fmt.Errorf("%s is a workspace of P4PORT=%s P4USER=%s, not of %s", vg.dir, cfg.port, cfg.user, vg.url.Redacted()) } } @@ -621,7 +633,8 @@ var PerforceBackend = &VCSBackend{ return err } if depotPath != "" { - if got, _ := p4ViewDepotPath(p4SpecField(spec, "View")); got != depotPath { + // The URL may also name a directory inside the workspace. + if got, _ := p4ViewDepotPath(p4SpecField(spec, "View")); got == "" || (depotPath != got && !strings.HasPrefix(depotPath, got+"/")) { return fmt.Errorf("p4 client %q of %s maps %q, not %q", cfg.client, vg.dir, got, depotPath) } } diff --git a/vcs_test.go b/vcs_test.go index adaa33e5..6e1bf00f 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -807,9 +807,14 @@ func TestPerforceBackend_Clone(t *testing.T) { if err := PerforceBackend.Clone(&vcsGetOption{url: mustParseURL("p4://p4.example.com/depot/proj/main"), dir: dir}); err != nil { t.Fatal(err) } - expect := []string{"p4", "-p", port, "-c", client, "sync", "-f"} - if got := (*calls)[len(*calls)-1].args; !reflect.DeepEqual(got, expect) { - t.Errorf("\ngot: %v\nexpect: %v", got, expect) + n := len(*calls) + for i, expect := range [][]string{ + {"p4", "-p", port, "-c", client, "sync", "-k", "//" + client + "/...#none"}, + {"p4", "-p", port, "-c", client, "sync"}, + } { + if got := (*calls)[n-2+i]; !reflect.DeepEqual(got.args, expect) || got.dir != dir { + t.Errorf("\ngot: %v (dir %q)\nexpect: %v", got.args, got.dir, expect) + } } }) @@ -1123,13 +1128,18 @@ func TestP4RootMove(t *testing.T) { t.Errorf("unexpected client spec:\n%s", spec) } - // changed on the server between prepare and apply + // already moved on the server between prepare and apply + if err := m.apply("/new/root"); err != nil { + t.Errorf("a client already rooted at the destination should be accepted: %v", err) + } + // changed to something else between prepare and apply: moving back + // would be as wrong as staying, so it must not be reported as unchanged srv.clients["ws"] = p4TestSpec("ws", "/someone/else", true, "//depot/proj/... //ws/...") - if err := m.apply("/new/root"); !errors.Is(err, errP4RootUnchanged) { - t.Errorf("error should wrap errP4RootUnchanged, but: %v", err) + if err := m.apply("/new/root"); err == nil || errors.Is(err, errP4RootUnchanged) { + t.Errorf("error should not wrap errP4RootUnchanged, but: %v", err) } // rejected save - srv.clients["ws"] = p4TestSpec("ws", "/new/root", true, "//depot/proj/... //ws/...") + srv.clients["ws"] = p4TestSpec("ws", dir, true, "//depot/proj/... //ws/...") srv.fail["client -i"] = "Locked client" if err := m.apply("/newer/root"); !errors.Is(err, errP4RootUnchanged) { t.Errorf("error should wrap errP4RootUnchanged, but: %v", err) @@ -1307,10 +1317,42 @@ type p4FakeServer struct { lostReply, offlineAfterSave, offline bool // onClientO runs before "client -o" replies, e.g. to race with it. onClientO func() + // files are the depot files that "sync" writes and haves the have list + // per client; syncFailAfter makes the next sync fail after that many files. + files []string + haves map[string]map[string]bool + syncFailAfter int } func newP4FakeServer() *p4FakeServer { - return &p4FakeServer{clients: map[string]string{}, fail: map[string]string{}} + return &p4FakeServer{ + clients: map[string]string{}, fail: map[string]string{}, + files: []string{"README"}, haves: map[string]map[string]bool{}, + } +} + +func (s *p4FakeServer) sync(client, dir string, force bool) error { + have := s.haves[client] + if have == nil { + have = map[string]bool{} + s.haves[client] = have + } + written := 0 + for _, f := range s.files { + if have[f] && !force { + continue + } + if s.syncFailAfter > 0 && written == s.syncFailAfter { + s.syncFailAfter = 0 + return errors.New("exit status 1") + } + if err := os.WriteFile(filepath.Join(dir, f), []byte(f+"\n"), 0644); err != nil { + return err + } + have[f] = true + written++ + } + return nil } func (s *p4FakeServer) respond(sub string, cmd *exec.Cmd) error { @@ -1353,11 +1395,20 @@ func (s *p4FakeServer) respond(sub string, cmd *exec.Cmd) error { return errors.New("exit status 1") } case sub == "sync" || sub == "sync -f": - return os.WriteFile(filepath.Join(cmd.Dir, "README"), []byte("hello\n"), 0644) + return s.sync(p4ArgValue(cmd.Args, "-c"), cmd.Dir, sub == "sync -f") + case strings.HasPrefix(sub, "sync -k ") && strings.HasSuffix(sub, "/...#none"): + delete(s.haves, p4ArgValue(cmd.Args, "-c")) } return nil } +func p4ArgValue(args []string, opt string) string { + if i := slices.Index(args, opt); i >= 0 && i+1 < len(args) { + return args[i+1] + } + return "" +} + // Regression tests for the first adversarial review round. func TestPerforceBackend_Review1(t *testing.T) { const port = "p4.example.com:1666" @@ -1562,3 +1613,68 @@ func TestPerforceBackend_Review2(t *testing.T) { } }) } + +// Regression tests for the third adversarial review round. +func TestPerforceBackend_Review3(t *testing.T) { + const port = "p4.example.com:1666" + + t.Run("retrying a failed re-clone restores every file", func(t *testing.T) { + dir := filepath.Join(newTempDir(t), "p4.example.com", "depot", "proj") + client := p4ClientName("alice", "myhost", port, "//depot/proj", dir) + srv := newP4FakeServer() + srv.files = []string{"a", "b", "c"} + // left over by "ghq rm": the server thinks every file is synced + srv.clients[client] = p4TestSpec(client, dir, true, "//depot/proj/... //"+client+"/...") + srv.haves[client] = map[string]bool{"a": true, "b": true, "c": true} + srv.syncFailAfter = 1 + fakeP4(t, srv.respond) + u := mustParseURL("p4://p4.example.com/depot/proj") + if err := PerforceBackend.Clone(&vcsGetOption{url: u, dir: dir}); err == nil { + t.Fatal("the first sync should fail") + } + if err := PerforceBackend.Update(&vcsGetOption{url: u, dir: dir}); err != nil { + t.Fatal(err) + } + for _, f := range srv.files { + if _, err := os.Stat(filepath.Join(dir, f)); err != nil { + t.Errorf("%s should be synced after get -u: %v", f, err) + } + } + }) + + t.Run("unknown p4+ transports", func(t *testing.T) { + for _, u := range []string{"p4+ssl4://p4.example.com/depot/proj", "p4+tls://p4.example.com/depot/proj", "p4+sll://p4.example.com/depot/proj"} { + dir := filepath.Join(newTempDir(t), "p4.example.com", "depot", "proj") + calls := fakeP4(t, newP4FakeServer().respond) + if err := PerforceBackend.Clone(&vcsGetOption{url: mustParseURL(u), dir: dir}); err == nil { + t.Errorf("%s: error should be occurred, but nil", u) + } + if len(*calls) != 0 { + t.Errorf("%s: p4 should not be run: %+v", u, *calls) + } + } + }) + + t.Run("get -u with explicit https URLs", func(t *testing.T) { + dir := newTempDir(t) + writeP4Config(dir, &p4Config{port: "p4.example.com:1777", user: "alice", client: "ws"}) + srv := newP4FakeServer() + srv.clients["ws"] = p4TestSpec("ws", dir, true, "//depot/app/... //ws/...") + fakeP4(t, srv.respond) + for u, ok := range map[string]bool{ + "https://bob@p4.example.com:1666/depot/app.git": false, + "https://p4.example.com:1666/depot/app": false, + "https://bob@p4.example.com/depot/app": false, + "https://p4.example.com/depot/app.git": false, + "https://p4.example.com/depot/app?x=1": false, + "https://p4.example.com/depot/app": true, // ghq list | ghq get -u + "https://p4.example.com:1777/depot/app": true, + "https://alice@p4.example.com/depot/app/sub": true, // a directory inside + } { + err := PerforceBackend.Update(&vcsGetOption{dir: dir, url: mustParseURL(u)}) + if (err == nil) != ok { + t.Errorf("%s: got %v, expect success=%t", u, err, ok) + } + } + }) +}