dump-shitty-claude-md 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1049 -0
- package/Cargo.toml +20 -0
- package/LICENSE +21 -0
- package/README.md +97 -0
- package/bin/darwin-arm64/dump-shitty-claude-md +0 -0
- package/bin/darwin-x64/dump-shitty-claude-md +0 -0
- package/bin/dump-shitty-claude-md.js +26 -0
- package/bin/linux-arm64/dump-shitty-claude-md +0 -0
- package/bin/linux-x64/dump-shitty-claude-md +0 -0
- package/bin/win32-x64/dump-shitty-claude-md.exe +0 -0
- package/package.json +38 -0
- package/src/gitops.rs +336 -0
- package/src/main.rs +550 -0
- package/src/plan.rs +467 -0
- package/src/scan.rs +501 -0
package/Cargo.toml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "dump-shitty-claude-md"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
description = "Migrate project CLAUDE.md files to AGENTS.md across all repos in your home directory"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
|
|
8
|
+
[dependencies]
|
|
9
|
+
anyhow = "1"
|
|
10
|
+
clap = { version = "4", features = ["derive"] }
|
|
11
|
+
dialoguer = "0.11"
|
|
12
|
+
dirs = "7.0.0"
|
|
13
|
+
ignore = "0.4"
|
|
14
|
+
serde = { version = "1", features = ["derive"] }
|
|
15
|
+
serde_json = "1"
|
|
16
|
+
trash = "5"
|
|
17
|
+
|
|
18
|
+
[profile.release]
|
|
19
|
+
lto = true
|
|
20
|
+
strip = true
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Livio Gama
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# ๐๏ธ dump-shitty-claude-md
|
|
2
|
+
|
|
3
|
+
> **One `AGENTS.md` to rule them all โ consolidate every agent instruction file in your home directory.**
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bunx dump-shitty-claude-md
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
DRY RUN โ 290 CLAUDE.md in repos, 25 strays
|
|
11
|
+
โ dedupe ~/code/api-server identical content โ delete CLAUDE.md
|
|
12
|
+
~ merge ~/code/dashboard content differs โ append CLAUDE.md into AGENTS.md
|
|
13
|
+
โ rename ~/code/cli-tool no AGENTS.md โ rename
|
|
14
|
+
! skip ~/code/dotfiles CLAUDE.md is a symlink โ left alone
|
|
15
|
+
! stray ~/Desktop/playground/CLAUDE.md not inside a git repo
|
|
16
|
+
ยท global ~/CLAUDE.md protected โ left alone
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## ๐ง The decision matrix
|
|
20
|
+
|
|
21
|
+
Every file is classified before anything is touched:
|
|
22
|
+
|
|
23
|
+
| `CLAUDE.md` | `AGENTS.md` | Action |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| exists | absent | rename โ `AGENTS.md` |
|
|
26
|
+
| exists | identical / whitespace-equal (BOM/CRLF-tolerant) | delete `CLAUDE.md` |
|
|
27
|
+
| exists | superset of `CLAUDE.md` | delete `CLAUDE.md` |
|
|
28
|
+
| exists | subset of `CLAUDE.md` | `CLAUDE.md` replaces `AGENTS.md` |
|
|
29
|
+
| exists | partial overlap | append with a `<!-- merged from CLAUDE.md -->` marker |
|
|
30
|
+
| symlink | any | skip + report |
|
|
31
|
+
| bare repo / FIFO / socket | โ | skip + report |
|
|
32
|
+
| outside a git repo | โ | never touched (Trash is opt-in) |
|
|
33
|
+
| `~/CLAUDE.md`, `~/.claude/**` | โ | protected, never touched |
|
|
34
|
+
|
|
35
|
+
Also warns on: `@import` syntax, `CLAUDE.local.md`, `AGENTS.local.md`,
|
|
36
|
+
`.claude/CLAUDE.md`, non-UTF8, hardlinks, nested files, dead spellings
|
|
37
|
+
(`claude.md`, `AGENT.md`).
|
|
38
|
+
|
|
39
|
+
## ๐งฉ Supported formats
|
|
40
|
+
|
|
41
|
+
`CLAUDE.md` is always in scope. Everything else is **off by default** โ pick
|
|
42
|
+
in the interactive prompt or pass `--migrate` / `--all-formats`:
|
|
43
|
+
|
|
44
|
+
| File | Tool |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `CLAUDE.md` | Claude Code โ โ
default |
|
|
47
|
+
| `.github/copilot-instructions.md` | GitHub Copilot |
|
|
48
|
+
| `GEMINI.md` | Gemini CLI โ only when `.gemini/settings.json` allows `AGENTS.md` |
|
|
49
|
+
| `.cursorrules` `.windsurfrules` `.clinerules` `.roorules` `.kilocoderules` `.rules` | Cursor, Windsurf, Cline, Roo, Kilo, Zed (legacy files) |
|
|
50
|
+
| `.goosehints` | Goose |
|
|
51
|
+
| `WARP.md` | Warp |
|
|
52
|
+
| `CONVENTIONS.md` | Aider |
|
|
53
|
+
| `.cursor/rules`, `.claude/rules`, `.github/instructions`, `.kiro/steering`, `.trae/rules`, `.openhands`, `.junie`, `.amazonq`, โฆ | โ report-only โ glob/frontmatter semantics `AGENTS.md` can't express |
|
|
54
|
+
|
|
55
|
+
Multiple files in one repo merge in a single pass โ `CLAUDE.md` wins the
|
|
56
|
+
rename, the rest append with provenance markers.
|
|
57
|
+
|
|
58
|
+
## ๐ฉ Flags
|
|
59
|
+
|
|
60
|
+
| Flag | Effect |
|
|
61
|
+
|---|---|
|
|
62
|
+
| *(none)* | dry run โ print the plan, touch nothing |
|
|
63
|
+
| `--apply` | migrate files in place |
|
|
64
|
+
| `--pr` | migrate on a branch + push + open PR/MR (`gh` / `glab`) |
|
|
65
|
+
| `--migrate <fmt>` | also migrate formats (`cursor,copilot,gemini,โฆ`) |
|
|
66
|
+
| `--all-formats` | migrate every supported format |
|
|
67
|
+
| `-y` | non-interactive, safe defaults (CLAUDE.md only, worktree) |
|
|
68
|
+
| `--trivial` | lossless actions only โ merges report-only |
|
|
69
|
+
| `--trash-strays` | non-repo strays โ OS Trash (Put Back-able) |
|
|
70
|
+
| `--json` | machine-readable plan |
|
|
71
|
+
| `PATH`, `--exclude`, `--follow-links` | scan control |
|
|
72
|
+
|
|
73
|
+
## ๐ก๏ธ Safety model
|
|
74
|
+
|
|
75
|
+
- **Dry run first** โ nothing mutates without `--apply` / `--pr`.
|
|
76
|
+
- **No data loss** โ content always lands in `AGENTS.md`; committed files recoverable via git.
|
|
77
|
+
- **Protected zones** โ `~/CLAUDE.md`, `~/.claude/**`, bare repos, symlinks, strays.
|
|
78
|
+
- **Idempotent** โ re-runs converge, no double merges.
|
|
79
|
+
- **Headless-safe** โ `GIT_TERMINAL_PROMPT=0`, ssh `BatchMode`: no prompt can hang it.
|
|
80
|
+
|
|
81
|
+
## Install
|
|
82
|
+
|
|
83
|
+
`bunx` is the way โ no install. The npm package ships prebuilt binaries for
|
|
84
|
+
macOS arm64/x64, Linux x64/arm64 and Windows x64, all cross-compiled and
|
|
85
|
+
published by the GitHub release workflow on every version tag:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
bunx dump-shitty-claude-md # dry run
|
|
89
|
+
bunx dump-shitty-claude-md --apply # migrate
|
|
90
|
+
bunx dump-shitty-claude-md --pr # migrate via PRs
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Alternatives: `npm i -g dump-shitty-claude-md` ยท `cargo install dump-shitty-claude-md`
|
|
94
|
+
|
|
95
|
+
## ๐ License
|
|
96
|
+
|
|
97
|
+
MIT
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Shim: exec the prebuilt binary for this platform.
|
|
3
|
+
// Binaries live in bin/<platform>-<arch>/ โ populated by scripts/build-all.sh
|
|
4
|
+
// before npm publish. Source channel for other platforms: crates.io.
|
|
5
|
+
const { spawnSync } = require("child_process");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
|
|
9
|
+
const isWin = process.platform === "win32";
|
|
10
|
+
const exe = `dump-shitty-claude-md${isWin ? ".exe" : ""}`;
|
|
11
|
+
const bin = path.join(
|
|
12
|
+
__dirname,
|
|
13
|
+
`${process.platform}-${process.arch}`,
|
|
14
|
+
exe,
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
if (!fs.existsSync(bin)) {
|
|
18
|
+
console.error(
|
|
19
|
+
`dump-shitty-claude-md: no prebuilt binary for ${process.platform}-${process.arch}.\n` +
|
|
20
|
+
"Install via cargo instead: cargo install dump-shitty-claude-md",
|
|
21
|
+
);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const r = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" });
|
|
26
|
+
process.exit(r.status ?? (r.error ? 1 : 0));
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dump-shitty-claude-md",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Migrate project CLAUDE.md files to AGENTS.md across all repos in your home directory",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"claude",
|
|
7
|
+
"agents-md",
|
|
8
|
+
"claude-md",
|
|
9
|
+
"codex",
|
|
10
|
+
"copilot",
|
|
11
|
+
"agent",
|
|
12
|
+
"cli"
|
|
13
|
+
],
|
|
14
|
+
"bin": {
|
|
15
|
+
"dump-shitty-claude-md": "bin/dump-shitty-claude-md.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"bin",
|
|
19
|
+
"src",
|
|
20
|
+
"Cargo.toml",
|
|
21
|
+
"Cargo.lock",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "cargo build --release && mkdir -p bin/$(node -p 'process.platform')-$(node -p 'process.arch') && cp target/release/dump-shitty-claude-md bin/$(node -p 'process.platform')-$(node -p 'process.arch')/",
|
|
27
|
+
"test": "cargo test"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=18"
|
|
31
|
+
},
|
|
32
|
+
"os": [
|
|
33
|
+
"darwin",
|
|
34
|
+
"linux",
|
|
35
|
+
"win32"
|
|
36
|
+
],
|
|
37
|
+
"license": "MIT"
|
|
38
|
+
}
|
package/src/gitops.rs
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
use anyhow::{bail, Context, Result};
|
|
2
|
+
use serde::Serialize;
|
|
3
|
+
use std::path::{Path, PathBuf};
|
|
4
|
+
use std::process::Command;
|
|
5
|
+
|
|
6
|
+
use crate::plan::{self, Plan, AGENTS};
|
|
7
|
+
|
|
8
|
+
pub const BRANCH: &str = "chore/consolidate-agent-instructions";
|
|
9
|
+
pub const COMMIT_MSG: &str = "chore: consolidate agent instruction files into AGENTS.md";
|
|
10
|
+
|
|
11
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum, Serialize)]
|
|
12
|
+
#[serde(rename_all = "kebab-case")]
|
|
13
|
+
pub enum Strategy {
|
|
14
|
+
/// Migrate inside a temporary `git worktree` โ the checkout is never touched.
|
|
15
|
+
Worktree,
|
|
16
|
+
/// Create the branch in the repo itself (requires clean tree), restore after.
|
|
17
|
+
InPlace,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#[derive(Debug, Default, Serialize)]
|
|
21
|
+
pub struct PrOutcome {
|
|
22
|
+
pub strategy: Option<Strategy>,
|
|
23
|
+
pub branch: Option<String>,
|
|
24
|
+
pub committed: bool,
|
|
25
|
+
pub pushed: bool,
|
|
26
|
+
pub pr_url: Option<String>,
|
|
27
|
+
pub skipped: Option<String>,
|
|
28
|
+
pub warnings: Vec<String>,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Env that makes git/gh fail fast instead of hanging on prompts
|
|
32
|
+
/// (credentials, ssh passphrases, pinentry) in headless runs.
|
|
33
|
+
fn no_prompt_env(cmd: &mut Command) -> &mut Command {
|
|
34
|
+
cmd.env("GIT_TERMINAL_PROMPT", "0")
|
|
35
|
+
.env("GIT_SSH_COMMAND", "ssh -oBatchMode=yes")
|
|
36
|
+
.env("GCM_INTERACTIVE", "Never")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fn git(repo: &Path, args: &[&str]) -> Result<String> {
|
|
40
|
+
let out = no_prompt_env(Command::new("git").arg("-C").arg(repo).args(args))
|
|
41
|
+
.output()
|
|
42
|
+
.context("failed to spawn git")?;
|
|
43
|
+
if !out.status.success() {
|
|
44
|
+
bail!(
|
|
45
|
+
"git {} failed: {}",
|
|
46
|
+
args.join(" "),
|
|
47
|
+
String::from_utf8_lossy(&out.stderr).trim()
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fn git_ok(repo: &Path, args: &[&str]) -> bool {
|
|
54
|
+
no_prompt_env(Command::new("git").arg("-C").arg(repo).args(args))
|
|
55
|
+
.output()
|
|
56
|
+
.map(|o| o.status.success())
|
|
57
|
+
.unwrap_or(false)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// True when `git status --porcelain` is empty (no modified or untracked files).
|
|
61
|
+
pub fn is_clean(repo: &Path) -> bool {
|
|
62
|
+
git(repo, &["status", "--porcelain"])
|
|
63
|
+
.map(|s| s.is_empty())
|
|
64
|
+
.unwrap_or(false)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Base ref to branch from: `origin/HEAD` after a best-effort fetch, else `HEAD`.
|
|
68
|
+
fn base_ref(repo: &Path) -> String {
|
|
69
|
+
let _ = git(repo, &["fetch", "origin", "--quiet"]);
|
|
70
|
+
if git_ok(repo, &["rev-parse", "--verify", "origin/HEAD"]) {
|
|
71
|
+
"origin/HEAD".into()
|
|
72
|
+
} else {
|
|
73
|
+
"HEAD".into()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
78
|
+
pub enum RemoteKind {
|
|
79
|
+
Github,
|
|
80
|
+
Gitlab,
|
|
81
|
+
Other,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
pub fn remote_kind(repo: &Path) -> RemoteKind {
|
|
85
|
+
let Ok(url) = git(repo, &["remote", "get-url", "origin"]) else {
|
|
86
|
+
return RemoteKind::Other;
|
|
87
|
+
};
|
|
88
|
+
if url.contains("github.") {
|
|
89
|
+
RemoteKind::Github
|
|
90
|
+
} else if url.contains("gitlab.") {
|
|
91
|
+
RemoteKind::Gitlab
|
|
92
|
+
} else {
|
|
93
|
+
RemoteKind::Other
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fn cli_available(name: &str) -> bool {
|
|
98
|
+
Command::new(name)
|
|
99
|
+
.arg("--version")
|
|
100
|
+
.output()
|
|
101
|
+
.map(|o| o.status.success())
|
|
102
|
+
.unwrap_or(false)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const PR_BODY: &str = "Consolidates agent instruction files (`CLAUDE.md` and friends) into `AGENTS.md`.\n\
|
|
106
|
+
\n\
|
|
107
|
+
Claude Code โฅ2.1.277 falls back to `AGENTS.md` when no `CLAUDE.md` exists,\n\
|
|
108
|
+
and Codex, Cursor, Copilot, Zed and friends read it natively โ\n\
|
|
109
|
+
so a single file now serves every agent.\n\
|
|
110
|
+
\n\
|
|
111
|
+
Generated by [dump-shitty-claude-md](https://github.com/LivioGama/dump-shitty-claude-md).";
|
|
112
|
+
|
|
113
|
+
/// `gh pr create`, falling back to the URL of an existing PR for the branch.
|
|
114
|
+
fn create_github_pr(dir: &Path, outcome: &mut PrOutcome) -> Result<()> {
|
|
115
|
+
if !cli_available("gh") {
|
|
116
|
+
outcome.warnings.push("gh CLI not found โ PR not created".into());
|
|
117
|
+
return Ok(());
|
|
118
|
+
}
|
|
119
|
+
let out = no_prompt_env(
|
|
120
|
+
Command::new("gh")
|
|
121
|
+
.arg("pr")
|
|
122
|
+
.arg("create")
|
|
123
|
+
.args(["--title", COMMIT_MSG, "--body", PR_BODY, "--head", BRANCH])
|
|
124
|
+
.current_dir(dir),
|
|
125
|
+
)
|
|
126
|
+
.output()
|
|
127
|
+
.context("failed to spawn gh")?;
|
|
128
|
+
if out.status.success() {
|
|
129
|
+
outcome.pr_url = Some(String::from_utf8_lossy(&out.stdout).trim().to_string());
|
|
130
|
+
return Ok(());
|
|
131
|
+
}
|
|
132
|
+
// Create failed โ maybe a PR for this branch already exists.
|
|
133
|
+
let existing = no_prompt_env(
|
|
134
|
+
Command::new("gh")
|
|
135
|
+
.args(["pr", "list", "--head", BRANCH, "--json", "url", "--jq"])
|
|
136
|
+
.arg(".[0].url")
|
|
137
|
+
.current_dir(dir),
|
|
138
|
+
)
|
|
139
|
+
.output()
|
|
140
|
+
.ok()
|
|
141
|
+
.filter(|o| o.status.success())
|
|
142
|
+
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
|
|
143
|
+
.filter(|u| !u.is_empty());
|
|
144
|
+
if let Some(url) = existing {
|
|
145
|
+
outcome.pr_url = Some(url);
|
|
146
|
+
outcome.warnings.push("PR already existed for branch".into());
|
|
147
|
+
} else {
|
|
148
|
+
outcome.warnings.push(format!(
|
|
149
|
+
"gh pr create failed: {}",
|
|
150
|
+
String::from_utf8_lossy(&out.stderr).trim()
|
|
151
|
+
));
|
|
152
|
+
}
|
|
153
|
+
Ok(())
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// `glab mr create`; the MR URL is scraped from the last http line of output.
|
|
157
|
+
fn create_gitlab_pr(dir: &Path, outcome: &mut PrOutcome) -> Result<()> {
|
|
158
|
+
if !cli_available("glab") {
|
|
159
|
+
outcome.warnings.push("glab CLI not found โ MR not created".into());
|
|
160
|
+
return Ok(());
|
|
161
|
+
}
|
|
162
|
+
let out = no_prompt_env(
|
|
163
|
+
Command::new("glab")
|
|
164
|
+
.args(["mr", "create", "--title", COMMIT_MSG, "--description"])
|
|
165
|
+
.arg(PR_BODY)
|
|
166
|
+
.args(["--source-branch", BRANCH, "--yes"])
|
|
167
|
+
.current_dir(dir),
|
|
168
|
+
)
|
|
169
|
+
.output()
|
|
170
|
+
.context("failed to spawn glab")?;
|
|
171
|
+
if out.status.success() {
|
|
172
|
+
let text = String::from_utf8_lossy(&out.stdout);
|
|
173
|
+
outcome.pr_url = text
|
|
174
|
+
.lines()
|
|
175
|
+
.rfind(|l| l.contains("http"))
|
|
176
|
+
.map(str::trim)
|
|
177
|
+
.map(str::to_owned);
|
|
178
|
+
} else {
|
|
179
|
+
outcome.warnings.push(format!(
|
|
180
|
+
"glab mr create failed: {}",
|
|
181
|
+
String::from_utf8_lossy(&out.stderr).trim()
|
|
182
|
+
));
|
|
183
|
+
}
|
|
184
|
+
Ok(())
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
fn commit_push_pr(dir: &Path, repo_for_push: &Path, rels: &[PathBuf], outcome: &mut PrOutcome) -> Result<()> {
|
|
188
|
+
// Per-path best-effort adds: a pathspec that matches neither worktree nor
|
|
189
|
+
// index (e.g. a source file never tracked at the worktree base) is a hard
|
|
190
|
+
// error, but there may simply be nothing to stage for that path.
|
|
191
|
+
let mut paths: Vec<String> = rels
|
|
192
|
+
.iter()
|
|
193
|
+
.map(|r| r.to_string_lossy().to_string())
|
|
194
|
+
.collect();
|
|
195
|
+
paths.push(AGENTS.into());
|
|
196
|
+
for p in &paths {
|
|
197
|
+
let _ = git_ok(dir, &["add", "-A", "--", p]);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Nothing staged โ nothing to commit (can happen if files were untracked+ignored).
|
|
201
|
+
let pathspecs: Vec<&str> = paths.iter().map(String::as_str).collect();
|
|
202
|
+
let mut args = vec!["status", "--porcelain", "--"];
|
|
203
|
+
args.extend(pathspecs.iter());
|
|
204
|
+
if git(dir, &args)?.is_empty() {
|
|
205
|
+
outcome.warnings.push("nothing staged โ files may be gitignored".into());
|
|
206
|
+
return Ok(());
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Index already contains exactly our staged paths โ commit it wholesale
|
|
210
|
+
// (a pathspec here could itself fail to match).
|
|
211
|
+
git(dir, &["commit", "-m", COMMIT_MSG])?;
|
|
212
|
+
outcome.committed = true;
|
|
213
|
+
|
|
214
|
+
match git(dir, &["push", "-u", "origin", BRANCH]) {
|
|
215
|
+
Ok(_) => outcome.pushed = true,
|
|
216
|
+
Err(e) => {
|
|
217
|
+
outcome.warnings.push(format!("push failed: {e:#}"));
|
|
218
|
+
return Ok(());
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
match remote_kind(repo_for_push) {
|
|
223
|
+
RemoteKind::Github => create_github_pr(dir, outcome),
|
|
224
|
+
RemoteKind::Gitlab => create_gitlab_pr(dir, outcome),
|
|
225
|
+
RemoteKind::Other => {
|
|
226
|
+
outcome
|
|
227
|
+
.warnings
|
|
228
|
+
.push("origin is not github/gitlab โ PR not created".into());
|
|
229
|
+
Ok(())
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// Source paths of the migrating plans, relative to the repo root.
|
|
235
|
+
fn rel_paths(plans: &[&Plan]) -> Vec<PathBuf> {
|
|
236
|
+
plans.iter().map(|p| p.file.clone()).collect()
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/// Worktree strategy: branch off `origin/HEAD` in a temp worktree, mirror the
|
|
240
|
+
/// working-tree state of all affected files, migrate, commit, push, PR, clean up.
|
|
241
|
+
fn run_worktree(repo: &Path, plans: &[&Plan], outcome: &mut PrOutcome) -> Result<()> {
|
|
242
|
+
let base = base_ref(repo);
|
|
243
|
+
let tmp = std::env::temp_dir().join(format!(
|
|
244
|
+
"dscm-wt-{}-{}",
|
|
245
|
+
std::process::id(),
|
|
246
|
+
repo.file_name()
|
|
247
|
+
.and_then(|n| n.to_str())
|
|
248
|
+
.unwrap_or("repo")
|
|
249
|
+
));
|
|
250
|
+
let tmp_str = tmp.to_string_lossy().to_string();
|
|
251
|
+
|
|
252
|
+
// Branch may already exist from a previous run โ retry without -b.
|
|
253
|
+
if git(repo, &["worktree", "add", "-b", BRANCH, &tmp_str, &base]).is_err() {
|
|
254
|
+
git(repo, &["worktree", "add", &tmp_str, BRANCH])
|
|
255
|
+
.context("git worktree add failed")?;
|
|
256
|
+
}
|
|
257
|
+
outcome.branch = Some(BRANCH.into());
|
|
258
|
+
|
|
259
|
+
let rels = rel_paths(plans);
|
|
260
|
+
let result = (|| -> Result<()> {
|
|
261
|
+
// Mirror working-tree state of every affected file into the worktree so
|
|
262
|
+
// uncommitted edits/untracked files are preserved in the PR.
|
|
263
|
+
for rel in rels.iter().chain(std::iter::once(&PathBuf::from(AGENTS))) {
|
|
264
|
+
let src = repo.join(rel);
|
|
265
|
+
let dst = tmp.join(rel);
|
|
266
|
+
if src.symlink_metadata().is_ok() {
|
|
267
|
+
if let Some(parent) = dst.parent() {
|
|
268
|
+
std::fs::create_dir_all(parent)?;
|
|
269
|
+
}
|
|
270
|
+
std::fs::copy(&src, &dst)
|
|
271
|
+
.with_context(|| format!("copy {}", src.display()))?;
|
|
272
|
+
} else if dst.exists() {
|
|
273
|
+
std::fs::remove_file(&dst)?;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
for p in plans {
|
|
277
|
+
plan::apply(&tmp, &p.file, p.action)?;
|
|
278
|
+
}
|
|
279
|
+
commit_push_pr(&tmp, repo, &rels, outcome)
|
|
280
|
+
})();
|
|
281
|
+
|
|
282
|
+
let _ = git(repo, &["worktree", "remove", "--force", &tmp_str]);
|
|
283
|
+
result
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/// In-place strategy: branch in the user's checkout (requires clean tree),
|
|
287
|
+
/// migrate, commit, push, PR, then restore the previous branch.
|
|
288
|
+
fn run_in_place(repo: &Path, plans: &[&Plan], outcome: &mut PrOutcome) -> Result<()> {
|
|
289
|
+
if !is_clean(repo) {
|
|
290
|
+
bail!("working tree is dirty โ use the worktree strategy instead");
|
|
291
|
+
}
|
|
292
|
+
let prev = git(repo, &["branch", "--show-current"])?;
|
|
293
|
+
|
|
294
|
+
let base = base_ref(repo);
|
|
295
|
+
if base == "origin/HEAD" {
|
|
296
|
+
let ahead = git(repo, &["rev-list", "--count", "origin/HEAD..HEAD"])
|
|
297
|
+
.unwrap_or_else(|_| "0".into());
|
|
298
|
+
if ahead != "0" {
|
|
299
|
+
outcome.warnings.push(format!(
|
|
300
|
+
"HEAD is {ahead} commit(s) ahead of origin/HEAD โ PR will include them"
|
|
301
|
+
));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if git(repo, &["checkout", "-b", BRANCH]).is_err() {
|
|
306
|
+
git(repo, &["checkout", BRANCH])?;
|
|
307
|
+
}
|
|
308
|
+
outcome.branch = Some(BRANCH.into());
|
|
309
|
+
|
|
310
|
+
let result = (|| -> Result<()> {
|
|
311
|
+
for p in plans {
|
|
312
|
+
plan::apply(repo, &p.file, p.action)?;
|
|
313
|
+
}
|
|
314
|
+
commit_push_pr(repo, repo, &rel_paths(plans), outcome)
|
|
315
|
+
})();
|
|
316
|
+
|
|
317
|
+
if !prev.is_empty() {
|
|
318
|
+
let _ = git(repo, &["checkout", &prev]);
|
|
319
|
+
}
|
|
320
|
+
result
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
pub fn run(repo: &Path, plans: &[&Plan], strategy: Strategy) -> PrOutcome {
|
|
324
|
+
let mut outcome = PrOutcome {
|
|
325
|
+
strategy: Some(strategy),
|
|
326
|
+
..Default::default()
|
|
327
|
+
};
|
|
328
|
+
let res = match strategy {
|
|
329
|
+
Strategy::Worktree => run_worktree(repo, plans, &mut outcome),
|
|
330
|
+
Strategy::InPlace => run_in_place(repo, plans, &mut outcome),
|
|
331
|
+
};
|
|
332
|
+
if let Err(e) = res {
|
|
333
|
+
outcome.warnings.push(format!("{e:#}"));
|
|
334
|
+
}
|
|
335
|
+
outcome
|
|
336
|
+
}
|