Skip to content

Add auto-vendor support to git go-patch#506

Draft
gdams wants to merge 2 commits into
mainfrom
dev/gadams/auto-vendor
Draft

Add auto-vendor support to git go-patch#506
gdams wants to merge 2 commits into
mainfrom
dev/gadams/auto-vendor

Conversation

@gdams

@gdams gdams commented Jun 12, 2026

Copy link
Copy Markdown
Member

Summary

Add a new git go-patch command that eliminates the need to store vendor directory contents in patch files. This reduces the microsoft-go vendor patch (0001) from ~43,000 lines to ~240 lines.

How it works

A patch commit message can include the command:

github.com/microsoft/go-infra/cmd/git-go-patch command: auto vendor src src/cmd

During extract

Diff hunks for vendor directories (src/vendor/, src/cmd/vendor/) are automatically stripped from the patch file. Only go.mod, go.sum, and other non-vendor files are preserved.

During apply

Patches with auto-vendor commands are applied individually. After each such patch, go mod vendor is run in the specified module directories and the commit is amended to include the regenerated vendor tree.

Example: new patch 0001

The current 0001-Vendor-external-dependencies.patch in microsoft-go is ~43,000 lines of vendored Go source. With auto-vendor, it becomes ~240 lines:

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: bot-for-go[bot] <...>
Subject: [PATCH] Vendor external dependencies

Vendor directories are automatically regenerated by 'git go-patch apply'
using 'go mod vendor' in 'src' and 'src/cmd'. This patch only tracks
go.mod, go.sum, and related non-vendor files in source control.

github.com/microsoft/go-infra/cmd/git-go-patch command: auto vendor src src/cmd
---
diff --git a/src/cmd/go.mod b/src/cmd/go.mod
  (go.mod changes)
diff --git a/src/cmd/go.sum b/src/cmd/go.sum
  (go.sum changes)
diff --git a/src/go.mod b/src/go.mod
  (go.mod changes)
diff --git a/src/go.sum b/src/go.sum
  (go.sum changes)
  ... plus a few small non-vendor files (~240 lines total)

Changes

New files

  • patch/commands.go — Shared command parsing (constants + ReadPatchCommands, ScanAutoVendorPatches)
  • patch/vendor.goFilterVendorContent (strips vendor diffs), VendorPathsFromModDirs, RunGoModVendor
  • patch/vendor_test.go — Tests for filtering and vendor path logic

Modified files

  • patch/patch.go — Added ApplyIndividually (per-patch apply with callback)
  • cmd/git-go-patch/extract.go — Uses shared constants/functions from patch package; filters vendor content during extract
  • cmd/git-go-patch/apply.go — Pre-scans for auto-vendor patches; uses ApplyIndividually + RunGoModVendor
  • cmd/git-go-patch/git-go-patch.go — Version bump to v1.1.0

Companion change

The microsoft-go repo will need a follow-up PR to:

  • Bump .git-go-patch MinimumToolVersion to v1.1.0
  • Replace the current ~43k-line 0001-Vendor-external-dependencies.patch with the ~240-line version
  • Update eng/_util/cmd/submodule-refresh/submodule-refresh.go to use ScanAutoVendorPatches + ApplyIndividually

@gdams gdams requested a review from a team as a code owner June 12, 2026 12:43
Copilot AI review requested due to automatic review settings June 12, 2026 12:43

Copilot AI 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.

Pull request overview

This PR adds an “auto-vendor” workflow to git go-patch so vendor directory contents don’t need to be stored in patch files. It does this by stripping vendor diffs during extract, and regenerating/staging vendor trees via go mod vendor during apply, dramatically reducing patch size for vendored changes.

Changes:

  • Introduces shared patch-command parsing and auto-vendor patch scanning in the patch package.
  • Adds vendor-diff filtering during extract and per-patch apply support (with post-apply callbacks) to enable go mod vendor between patches.
  • Updates apply to detect auto-vendor patches and amend commits with regenerated vendor trees; bumps tool version to v1.1.0.
Show a summary per file
File Description
patch/vendor.go Adds vendor-diff filtering plus go mod vendor execution/staging/amend support.
patch/vendor_test.go Adds unit tests for vendor filtering and vendor-path detection logic.
patch/patch.go Adds ApplyIndividually to apply patches one-by-one with an after-apply hook.
patch/commands.go Adds shared command constants, patch command parsing, and auto-vendor patch scanning.
cmd/git-go-patch/extract.go Uses shared command parsing; strips vendor diffs when auto-vendor is specified.
cmd/git-go-patch/apply.go Pre-scans for auto-vendor patches and applies patches individually with vendor regeneration.
cmd/git-go-patch/git-go-patch.go Bumps tool version to v1.1.0.

Copilot's findings

Tip

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

  • Files reviewed: 7/7 changed files
  • Comments generated: 4

Comment thread patch/commands.go
Comment thread patch/vendor.go
Comment thread patch/vendor.go
Comment thread patch/commands.go
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from 5d08a3b to 6cff696 Compare June 12, 2026 13:01
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from 6cff696 to 8a69be6 Compare June 12, 2026 13:17
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from 8a69be6 to 81a6731 Compare June 12, 2026 13:27
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from 81a6731 to a17ffb5 Compare June 12, 2026 13:31
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from a17ffb5 to 2eed36b Compare June 12, 2026 13:40
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from 2eed36b to bf9c939 Compare June 12, 2026 13:48
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from bf9c939 to d2e134e Compare June 12, 2026 13:52
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from d2e134e to 508cb26 Compare June 12, 2026 13:56
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from 508cb26 to 049efd7 Compare June 12, 2026 14:01
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
Add a new patch command 'auto vendor <dir1> [<dir2> ...]' that eliminates
the need to store vendor directory contents in patch files. When a patch
includes this command:

- During 'extract': diff hunks for vendor directories (e.g. <dir>/vendor/)
  are stripped from the patch file, dramatically reducing patch size.

- During 'apply': patches are applied individually, and after each
  auto-vendor patch, 'go mod vendor' is run in the specified module
  directories, then the commit is amended to include the vendor changes.

This reduces the microsoft-go vendor patch (0001) from ~43,000 lines to
~240 lines, making patches easier to review, rebase, and maintain.

New patch package exports:
- CommandPrefix, PatchNumberCommand, AutoVendorPrefix constants
- ReadPatchCommands, ScanAutoVendorPatches functions
- FilterVendorContent, VendorPathsFromModDirs, RunGoModVendor functions
- ApplyIndividually function for per-patch callbacks

Bumps version to v1.1.0.
@gdams gdams force-pushed the dev/gadams/auto-vendor branch from 049efd7 to 70acb19 Compare June 12, 2026 14:08
gdams added a commit to microsoft/go that referenced this pull request Jun 12, 2026
Replace the ~45,000-line vendor patch with a ~240-line patch that only
tracks go.mod, go.sum, and related non-vendor files. Vendor directories
are now regenerated automatically during 'git go-patch apply' and
'submodule-refresh' using 'go mod vendor'.

Changes:
- .git-go-patch: bump MinimumToolVersion to v1.1.0
- patches/0001: strip vendor diffs, add 'auto vendor src src/cmd' command
- submodule-refresh.go: use ScanAutoVendorPatches + ApplyIndividually
- eng/_util/go.mod: update go-infra dependency

Depends on: microsoft/go-infra#506
After go mod vendor runs with lowered go directives, modules.txt records
the lowered versions. When we restore the original go directives in
go.mod files, modules.txt becomes stale. Post-build vendor checks then
fail because re-running go mod vendor with the real Go version produces
different modules.txt content.

Add fixModulesTxtGoVersions to update modules.txt entries for local
replace targets after restoring their go directives.
@gdams gdams marked this pull request as draft June 12, 2026 14:42
@gdams gdams added the enhancement New feature or request label Jun 12, 2026
@gdams gdams added the release-on-merge Triggers an automated release when this PR is merged label Jun 12, 2026

@dagood dagood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

After each such patch, go mod vendor is run in the specified module directories

The go used to run go mod vendor has to match the source tree exactly (not just >), how is that handled?

Overall, I don't think this approach makes sense (at least I can't justify it in my head as a reviewer vs. what seems possible), it seems super complicated and has many performance implications.

(I know this is a draft, but it already has a release on merge label...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request release-on-merge Triggers an automated release when this PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants