{"id":51043745,"url":"https://github.com/floatpane/go-patchapply","last_synced_at":"2026-06-22T12:02:24.714Z","repository":{"id":362778851,"uuid":"1257619785","full_name":"floatpane/go-patchapply","owner":"floatpane","description":"Apply parsed patches to files, in Go. Add / modify / delete / rename, reverse, dry-run — confined to a directory, transactional, never runs git.","archived":false,"fork":false,"pushed_at":"2026-06-05T20:52:30.000Z","size":49,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-06-05T22:21:43.484Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://patchapply.floatpane.com","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/floatpane.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"floatpane"}},"created_at":"2026-06-02T21:14:14.000Z","updated_at":"2026-06-03T06:18:04.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/floatpane/go-patchapply","commit_stats":null,"previous_names":["floatpane/go-patchapply"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/floatpane/go-patchapply","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatpane%2Fgo-patchapply","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatpane%2Fgo-patchapply/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatpane%2Fgo-patchapply/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatpane%2Fgo-patchapply/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/floatpane","download_url":"https://codeload.github.com/floatpane/go-patchapply/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatpane%2Fgo-patchapply/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34647750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-22T02:00:06.391Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-06-22T12:02:23.919Z","updated_at":"2026-06-22T12:02:24.682Z","avatar_url":"https://github.com/floatpane.png","language":"Go","funding_links":["https://github.com/sponsors/floatpane"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# go-patchapply\n\n**Apply parsed patches to files, in Go. Add / modify / delete / rename, reverse, dry-run — confined to a directory, transactional, never runs git.**\n\n[![Go Version](https://img.shields.io/github/go-mod/go-version/floatpane/go-patchapply)](https://golang.org)\n[![Go Reference](https://pkg.go.dev/badge/github.com/floatpane/go-patchapply.svg)](https://pkg.go.dev/github.com/floatpane/go-patchapply)\n[![GitHub release (latest by date)](https://img.shields.io/github/v/release/floatpane/go-patchapply)](https://github.com/floatpane/go-patchapply/releases)\n[![CI](https://github.com/floatpane/go-patchapply/actions/workflows/ci.yml/badge.svg)](https://github.com/floatpane/go-patchapply/actions/workflows/ci.yml)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)\n\n\u003c/div\u003e\n\n`go-patchapply` is the **apply** half of [go-mailpatch](https://github.com/floatpane/go-mailpatch). mailpatch turns a `git format-patch` email into structured `FileChange`s; patchapply takes those — or a bare unified diff — and writes the changes to a filesystem: creating, modifying, deleting, renaming, and copying files. It can reverse a patch and dry-run one. It **never executes git**.\n\nIt grew out of [matcha](https://github.com/floatpane/matcha)'s `git-mail` feature, where applying a mailed patch must be safe against hostile paths and must not half-apply.\n\n## Features\n\n- **Every change type.** Added, deleted, modified, renamed, copied — driven straight off `mailpatch.FileChange`.\n- **Offset-tolerant, exact-context hunks.** A hunk still applies when earlier edits shifted the file; context lines must match (no silent fuzz), and a hunk that can't be placed fails with `ErrConflict`.\n- **Transactional.** Every file is read and every hunk placed in memory first. If any hunk fails, **nothing is written** — no half-applied trees.\n- **Confined by construction.** `DirFS` pins every path inside a root; a patch that tries to escape via `../` or an absolute path fails with `ErrUnsafePath` before touching disk.\n- **Reverse \u0026 dry-run.** `Options{Reverse: true}` unapplies; `Options{DryRun: true}` validates the whole patch and writes nothing.\n- **Apply anywhere.** Run against the OS (`DirFS`), entirely in memory (`MemFS`), or your own `FS` (a git object store, a virtual tree…).\n- **Zero third-party deps beyond go-mailpatch.** Standard library otherwise.\n\n## Install\n\n```bash\ngo get github.com/floatpane/go-patchapply\n```\n\nRequires Go 1.26+.\n\n## Usage\n\n### Apply a patch email to a directory\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/floatpane/go-mailpatch\"\n\t\"github.com/floatpane/go-patchapply\"\n)\n\nfunc main() {\n\traw, _ := os.ReadFile(\"fix.patch\")\n\n\tp, err := mailpatch.ParseBytes(raw) // parse the format-patch email\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tfsys := patchapply.NewDirFS(\"/path/to/repo\") // confined to this root\n\tres, err := patchapply.ApplyPatch(fsys, p, nil)\n\tif err != nil {\n\t\tlog.Fatal(err) // ErrConflict, ErrUnsafePath, ErrMissing, ErrExists\n\t}\n\n\tfor _, f := range res.Files {\n\t\tlog.Printf(\"%s %s\", f.Status, f.Path) // updated/created/removed/renamed\n\t}\n}\n```\n\n### Apply a bare diff in memory\n\n```go\nfsys := patchapply.NewMemFS(map[string][]byte{\n\t\"greet.txt\": []byte(\"hello\\nworld\\nbye\\n\"),\n})\n\n_, err := patchapply.ApplyDiff(fsys, diffText, nil)\n\nout, _ := fsys.ReadFile(\"greet.txt\")\n```\n\n### Dry-run, then reverse\n\n```go\n// Validate without writing — nil error means a real apply would succeed.\nif _, err := patchapply.Apply(fsys, files, \u0026patchapply.Options{DryRun: true}); err != nil {\n\tlog.Fatal(\"patch will not apply cleanly:\", err)\n}\n\n// Apply, then later undo.\npatchapply.Apply(fsys, files, nil)\npatchapply.Apply(fsys, files, \u0026patchapply.Options{Reverse: true})\n```\n\n### Just transform bytes\n\n```go\n// No filesystem at all: apply one file's hunks to content you hold.\nnewContent, err := patchapply.ApplyToBytes(oldContent, fileChange)\n```\n\n## API at a glance\n\n| Function | Does |\n|----------|------|\n| `Apply(fs, []FileChange, *Options)` | apply already-parsed changes |\n| `ApplyDiff(fs, diff, *Options)` | parse a bare diff (via go-mailpatch) and apply |\n| `ApplyPatch(fs, *mailpatch.Patch, *Options)` | apply a parsed patch email |\n| `ApplyToBytes(orig, FileChange)` | pure, filesystem-free single-file apply |\n| `NewDirFS(root)` / `NewMemFS(seed)` | the two built-in `FS` implementations |\n\n`Options{Reverse, DryRun}`. Errors: `ErrConflict`, `ErrUnsafePath`, `ErrMissing`, `ErrExists` (compare with `errors.Is`).\n\n## What this is not\n\n- **Not a merge tool.** No 3-way merge, no conflict markers. A hunk that doesn't apply is an error, not a `\u003c\u003c\u003c\u003c\u003c\u003c\u003c`.\n- **Not `git am`.** It writes files; it does not create commits, move HEAD, or touch the index.\n- **Not a fuzzy patcher.** It tolerates line *offset*, not changed context. That's deliberate — applying a security-relevant patch to the wrong place silently is worse than failing.\n\n\u003e [!NOTE]\n\u003e End-of-file newline state (`\\ No newline at end of file`) is not tracked; a\n\u003e file produced from an addition ends with a newline.\n\n## Documentation\n\nFull API reference: [pkg.go.dev/github.com/floatpane/go-patchapply](https://pkg.go.dev/github.com/floatpane/go-patchapply)\n\nGuides and diagrams: see [`docs/`](docs/).\n\n## Sister projects\n\n| Project | Role |\n|---------|------|\n| [floatpane/go-mailpatch](https://github.com/floatpane/go-mailpatch) | The parser half — turns format-patch email into the `FileChange`s this library applies. |\n| [floatpane/matcha](https://github.com/floatpane/matcha) | Reference consumer — `git-mail` patch apply. |\n| [floatpane/go-secretbox](https://github.com/floatpane/go-secretbox) | Sibling extraction — password-based encryption for data at rest. |\n\n## Contributing\n\nPRs welcome. See [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## Security\n\nIt writes files from untrusted patches. Path-confinement matters — report vulnerabilities privately via [SECURITY.md](SECURITY.md).\n\n## License\n\nMIT. See [LICENSE](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatpane%2Fgo-patchapply","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffloatpane%2Fgo-patchapply","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatpane%2Fgo-patchapply/lists"}