Skip to content

Add replace command for dependency redirects - #236

Merged
andrew merged 2 commits into
git-pkgs:mainfrom
abhinavgautam01:feature/replace-command-181
Jun 30, 2026
Merged

andrew merged 2 commits into
git-pkgs:mainfrom
abhinavgautam01:feature/replace-command-181

Conversation

@abhinavgautam01

@abhinavgautam01 abhinavgautam01 commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

closes #181

Summary

Adds git pkgs replace for redirecting a dependency to a local checkout, git ref or explicit registry version for downstream testing workflows.

Changes

  • Add git pkgs replace <package> [version]
  • Support --path, --git, --ref, --drop, --manager, --ecosystem, --dry-run and timeout/extra args
  • Route version replacements through the existing package-manager add operation
  • Support Go replacements via go mod edit
  • Support npm-family and Composer replacement commands
  • Support manifest edits for Cargo [patch.crates-io], uv [tool.uv.sources] and Bundler Gemfile
  • Document usage in the README
  • Add tests for validation, dry-run output and manifest edits

Notes

npm-family --drop is intentionally rejected because the original registry version constraint cannot be recovered safely. Users can restore a registry dependency with:

git pkgs replace <package> <version>

Testing

- go test ./cmd -run Replace
- go test ./cmd -run 'Replace|PagerFlagAccepted'
- go test ./...
- go tool golangci-lint run ./...
- git diff --check
- go build ./...

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new git pkgs replace command to support redirecting a dependency to a local checkout, git ref, or explicit version (via the existing add flow) to enable downstream testing workflows across multiple ecosystems.

Changes:

  • Adds a new replace CLI subcommand wired into the root command.
  • Implements replace behavior via manager subprocess calls (Go/npm-family/Composer) and manifest edits (Cargo/uv/Bundler).
  • Documents replace usage in the README and adds tests for dry-run output, validation, and manifest editing behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
README.md Documents the new replace command and supported behaviors/limitations.
cmd/root.go Registers the new replace subcommand.
cmd/replace.go Implements replace command logic, manager dispatch, and manifest edits.
cmd/replace_test.go Adds tests for dry-run output, validation, and manifest editing.
cmd/pager_test.go Ensures replace accepts the global --pager flag.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/replace.go Resolved
Comment thread cmd/replace.go Resolved
Comment thread cmd/replace_test.go Resolved
Comment thread cmd/replace.go Resolved
@abhinavgautam01
abhinavgautam01 force-pushed the feature/replace-command-181 branch from 0d1072b to 2c66b54 Compare June 19, 2026 17:21

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. The CLI surface matches #181 and the Gemfile/TOML round-tripping is more careful than I expected. A few structural things before this lands.

Subprocess managers should go through the managers schema. #181 says gomod/npm/composer "fit the existing managers command schema" — i.e. define a replace operation in git-pkgs/managers and call it via BuildCommands(mgr, "replace", input) the way add does. This PR hardcodes argv in buildGoReplaceCommands/buildNPMReplaceCommands/buildComposerReplaceCommands and runs them through a bespoke runShellCommands. That duplicates the runner, the dry-run formatting, and -x handling that managers already owns. The version-mode path already does it right — the other modes should match.

No capability declarations. #181 asks for replace_path/replace_git capabilities per manager so callers can check support up front. The PR switches on a hardcoded name list instead. Once the operations move into managers the capabilities fall out of the schema, but they need to be queryable.

Gemfile filter misses github: and hash-rocket syntax. filterGemReplacementArgs strips git:/path:/branch:/tag:/ref: but not github: (bundler's shorthand) or :git => "...". So replace rails --git ... on gem "rails", github: "rails/rails" writes a line with both github: and git:, which bundler rejects. Add github:/gist:/bitbucket: and the :key => form to the filter.

gomod --git --ref produces an unbuildable go.mod. go mod edit -replace x=github.com/fork/x@feature-branch writes replace x => github.com/fork/x feature-branch, but the replace directive needs a version, not a ref. go mod tidy will reject it. The test only checks argv shape so this isn't caught. Either resolve the ref to a pseudo-version first (go list -m github.com/fork/x@feature-branch) or document that --ref for gomod must be a version.

Cargo --ref always emits rev =. #181 specced branch = "...". rev works but pins to the commit at fetch time rather than tracking the branch. Probably want branch when the ref isn't a SHA.

Manifest editing location. #181 points the cargo/uv/bundler edits at the manifests package, but that package is parse-only today so I understand inlining. Fine to leave the ~300 lines of TOML/Gemfile munging here for now, but please pull updateTOMLSectionEntry/updateGemfileLine and friends into cmd/replace_manifest.go (or similar) so replace.go stays readable — it's 767 lines vs add.go at 135.

Smaller:

  • updateTOMLSectionEntry leaves an empty [patch.crates-io] header behind after --drop, and won't match a header with a trailing comment.
  • appendExtra appends -x args to every command in a multi-command sequence (composer git+ref runs two commands; extras land on both config and require).

@abhinavgautam01
abhinavgautam01 force-pushed the feature/replace-command-181 branch from 2c66b54 to 3b9cbe2 Compare June 23, 2026 18:07
@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Thanks, fixed

  • Subprocess-backed replacements now go through the managers translator path instead of hardcoded argv/builders. I added local replace-operation overlays for gomod, npm/pnpm/yarn/bun and composer so the command uses BuildCommands / RunManagerCommands consistently.
  • Added replace_path / replace_git / replace_drop capability declarations and check support before running subprocess-backed modes.
  • Removed the bespoke shell runner and appendExtra path.
  • Split manifest editing into cmd/replace_manifest.go.
  • Gemfile filtering now removes github:/gist:/bitbucket: and hash-rocket replacement keys like :git => and :branch =>.
  • gomod --git --ref now rejects branch names and documents that --ref must be a Go module version or pseudo-version.
  • Cargo/uv use branch for named refs and rev for SHA refs.
  • TOML drop handles commented headers and removes empty replacement sections.

Added regression coverage for the Go ref boundary, Cargo branch refs, Gemfile shorthand/hash-rocket filtering, empty TOML section removal and Composer extras placement.

@andrew

andrew commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

The replace operation has landed in github.com/git-pkgs/managers v0.10.0 (git-pkgs/managers#25), so most of this PR can now delegate to the library instead of overlaying definitions in cmd/.

What moves:

  • addReplaceManagerOverlay and addManagerCapabilities in cmd/managers.go can go; the replace command and replace_path/replace_git/replace_drop capabilities are in the YAML definitions now.
  • cmd/replace_manifest.go can go; cargo, uv and bundler manifest editing is in the library.
  • populateGoReplaceInput, populateNPMReplaceInput, buildComposerReplaceOperations, normalizeGoReplaceTarget, npmGitSpec, composerRepositoryName and the capability check can go; GenericManager.Replace builds the spec and runs it.

What stays in cmd/replace.go:

  • Flag parsing and validateReplaceOptions (the version positional arg is CLI-only).
  • Manager detection/selection.
  • The replaceModeVersion path that falls through to add.
  • --dry-run: the library has no dry-run, so either keep BuildCommands against the new YAML replace entries for the subprocess managers, or print the manifest path for cargo/uv/bundler.

After detection it becomes roughly:

result, err := mgr.Replace(ctx, opts.Package, managers.ReplaceOptions{
    Path:  opts.Path,
    Git:   opts.Git,
    Ref:   opts.Ref,
    Drop:  opts.Mode == replaceModeDrop,
    Extra: opts.Extra,
})

One behaviour difference: the library now requires Ref for gomod git mode (since go mod edit -replace pkg=host/path without a version is invalid), where this PR allowed it through. See docs/replace.md in the managers repo for the per-manager details.

@abhinavgautam01
abhinavgautam01 force-pushed the feature/replace-command-181 branch from b3ba23d to 08ea394 Compare June 30, 2026 17:26
@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Updated this to use github.com/git-pkgs/managers v0.10.0.

The local replace overlay/capability injection is gone, and cmd/replace_manifest.go has been removed. Source replacements now delegate to mgr.Replace(...); the version positional-arg path still falls through to the existing add flow because that is CLI-only behavior.

For --dry-run, subprocess managers use a mock managers runner to print the generated commands and manifest-edit managers print the manifest file that would be edited.

I also updated the tests around gomod ref validation, delegated dry-run command generation, cargo/uv/bundler manifest behavior, npm unsupported drop and composer extra-arg placement.

Verified with:

  • go test ./...
  • go tool golangci-lint run ./...

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor to managers v0.10.0 looks right. The MockRunner-driven dry-run is a nice way to keep the preview in lockstep with the real path; the replaceManifestPath duplication of the library's manager→manifest map is minor and can move upstream later if it grows.

@andrew
andrew merged commit 1072fd6 into git-pkgs:main Jun 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add replace command for redirecting a dependency to a local path or git ref

3 participants