diff --git a/README.md b/README.md index 800d4f1..8492ed1 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ reviewing, sharing, and applying changes locally. - **Per-commit patches:** Keep the original patch content, commit messages, and authorship, with numbered filenames in commit order. - **Single commit:** Use `--commit ` to download one commit's `.patch` - with either a short or full hash. + with either a short or full hash; the hash may also come first, as in + ` --commit`. - **GitLab support:** Pass `--gitlab` to fetch merge requests and commits from gitlab.com, including projects nested under subgroups. - **Tor support:** Pass `--tor` to download through the Tor network over a local @@ -146,8 +147,11 @@ patchsplit [--out ] [--force] [--squash] patchsplit [--out ] [--force] [--squash] patchsplit --commit [--out ] [--force] patchsplit --commit [--out ] [--force] +patchsplit --commit [--out ] [--force] +patchsplit --commit [--out ] [--force] patchsplit --gitlab [--out ] [--force] [--squash] patchsplit --gitlab --commit [--out ] [--force] +patchsplit --gitlab --commit [--out ] [--force] ``` ### Split a pull request by commit @@ -213,11 +217,13 @@ the data GitHub includes in its diff; binary file contents may not be included. ### Download a single commit Pass `--commit ` (or `-commit `) with a short or full commit hash -instead of a pull request number: +instead of a pull request number. If you prefer the hash first, a bare +`--commit` after it works too: ```sh patchsplit zitzhen patchsplit -commit b430113 patchsplit zitzhen/patchsplit --commit b4301133226e5c3a464cff9649de0b321c0b0a2e +patchsplit zitzhen patchsplit b430113 --commit ``` This fetches `https://github.com///commit/.patch` and writes diff --git a/README_zh-cn.md b/README_zh-cn.md index 62a635c..b169b20 100644 --- a/README_zh-cn.md +++ b/README_zh-cn.md @@ -13,8 +13,11 @@ patchsplit [--out ] [--force] [--squash] patchsplit [--out ] [--force] [--squash] patchsplit --commit [--out ] [--force] patchsplit --commit [--out ] [--force] +patchsplit --commit [--out ] [--force] +patchsplit --commit [--out ] [--force] patchsplit --gitlab [--out ] [--force] [--squash] patchsplit --gitlab --commit [--out ] [--force] +patchsplit --gitlab --commit [--out ] [--force] ``` 示例: @@ -51,11 +54,12 @@ git apply pr-42-patches/pr-42.patch ### 下载单个 commit 不传 PR 编号,改用 `--commit `(也可写作 `-commit `)传入短哈希或 -完整哈希: +完整哈希;如果习惯哈希在前,也可以把 `--commit` 写在后面: ```sh patchsplit zitzhen patchsplit -commit b430113 patchsplit zitzhen/patchsplit --commit b4301133226e5c3a464cff9649de0b321c0b0a2e +patchsplit zitzhen patchsplit b430113 --commit ``` 对应下载 `https://github.com///commit/.patch`,并把该 commit diff --git a/debian/patchsplit.1 b/debian/patchsplit.1 index efcfd24..2422be1 100644 --- a/debian/patchsplit.1 +++ b/debian/patchsplit.1 @@ -43,6 +43,26 @@ patchsplit \- download GitHub pull request or GitLab merge request patches, spli .RE .br .B patchsplit +.RI < owner/repo > " \-\-commit" +.RS +[\c +.B \-\-out +.IR dir ] +[\c +.B \-\-force ] +.RE +.br +.B patchsplit +.RI < owner > " \-\-commit" +.RS +[\c +.B \-\-out +.IR dir ] +[\c +.B \-\-force ] +.RE +.br +.B patchsplit .B \-\-gitlab .RI < namespace/project > " " .RS @@ -65,6 +85,17 @@ patchsplit \- download GitHub pull request or GitLab merge request patches, spli [\c .B \-\-force ] .RE +.br +.B patchsplit +.B \-\-gitlab +.RI < namespace/project > " \-\-commit" +.RS +[\c +.B \-\-out +.IR dir ] +[\c +.B \-\-force ] +.RE .SH DESCRIPTION .B patchsplit downloads the patch for a GitHub pull request and splits it into @@ -85,6 +116,12 @@ download the patch of a single commit identified by a short or full hash and write it verbatim to .BR .patch . The result is a mail-formatted patch for git am. +Without a value, +.B \-\-commit +acts as a mode flag and takes the hash from the last argument instead, so +.B patchsplit owner repo b430113 \-\-commit +is equivalent to +.BR "patchsplit owner repo \-\-commit b430113" . .B \-\-commit cannot be combined with .BR \-\-squash . @@ -158,6 +195,9 @@ requests always use https. Do not include a scheme or a path. Download the .patch of a single commit instead of a pull request. .I hash may be a short or full commit hash. +Without a value, the flag selects commit mode and the hash is read from the +last argument, for example +.BR "patchsplit owner repo b430113 \-\-commit" . The output file is named .BR .patch . Cannot be combined with @@ -215,6 +255,11 @@ Download the patch for commit b430113 from and write it to .BR b430113.patch . .TP +.B patchsplit zitzhen patchsplit b430113 \-\-commit +Download the same patch with the hash before the flag; a bare +.B \-\-commit +takes the last argument as the commit. +.TP .B patchsplit \-\-gitlab zitzhen/patchsplit 1 Download and split merge request 1 from the GitLab project .BR zitzhen/patchsplit . diff --git a/packaging/patchsplit.1 b/packaging/patchsplit.1 index efcfd24..2422be1 100644 --- a/packaging/patchsplit.1 +++ b/packaging/patchsplit.1 @@ -43,6 +43,26 @@ patchsplit \- download GitHub pull request or GitLab merge request patches, spli .RE .br .B patchsplit +.RI < owner/repo > " \-\-commit" +.RS +[\c +.B \-\-out +.IR dir ] +[\c +.B \-\-force ] +.RE +.br +.B patchsplit +.RI < owner > " \-\-commit" +.RS +[\c +.B \-\-out +.IR dir ] +[\c +.B \-\-force ] +.RE +.br +.B patchsplit .B \-\-gitlab .RI < namespace/project > " " .RS @@ -65,6 +85,17 @@ patchsplit \- download GitHub pull request or GitLab merge request patches, spli [\c .B \-\-force ] .RE +.br +.B patchsplit +.B \-\-gitlab +.RI < namespace/project > " \-\-commit" +.RS +[\c +.B \-\-out +.IR dir ] +[\c +.B \-\-force ] +.RE .SH DESCRIPTION .B patchsplit downloads the patch for a GitHub pull request and splits it into @@ -85,6 +116,12 @@ download the patch of a single commit identified by a short or full hash and write it verbatim to .BR .patch . The result is a mail-formatted patch for git am. +Without a value, +.B \-\-commit +acts as a mode flag and takes the hash from the last argument instead, so +.B patchsplit owner repo b430113 \-\-commit +is equivalent to +.BR "patchsplit owner repo \-\-commit b430113" . .B \-\-commit cannot be combined with .BR \-\-squash . @@ -158,6 +195,9 @@ requests always use https. Do not include a scheme or a path. Download the .patch of a single commit instead of a pull request. .I hash may be a short or full commit hash. +Without a value, the flag selects commit mode and the hash is read from the +last argument, for example +.BR "patchsplit owner repo b430113 \-\-commit" . The output file is named .BR .patch . Cannot be combined with @@ -215,6 +255,11 @@ Download the patch for commit b430113 from and write it to .BR b430113.patch . .TP +.B patchsplit zitzhen patchsplit b430113 \-\-commit +Download the same patch with the hash before the flag; a bare +.B \-\-commit +takes the last argument as the commit. +.TP .B patchsplit \-\-gitlab zitzhen/patchsplit 1 Download and split merge request 1 from the GitLab project .BR zitzhen/patchsplit . diff --git a/po/patchsplit.pot b/po/patchsplit.pot index a306e1f..ee32c6e 100644 --- a/po/patchsplit.pot +++ b/po/patchsplit.pot @@ -6,9 +6,9 @@ #, fuzzy msgid "" msgstr "" -"Project-Id-Version: patchsplit 1.4.0\n" +"Project-Id-Version: patchsplit 1.5.0\n" "Report-Msgid-Bugs-To: https://github.com/zitzhen/patchsplit/issues\n" -"POT-Creation-Date: 2026-11-06 20:21+0800\n" +"POT-Creation-Date: 2026-11-21 22:27+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -175,10 +175,14 @@ msgid "" " patchsplit [--out ] [--force] [--squash]\n" " patchsplit --commit [--out ] [--force]\n" " patchsplit --commit [--out ] [--force]\n" +" patchsplit --commit [--out ] [--force]\n" +" patchsplit --commit [--out ] [--force]\n" " patchsplit --gitlab [--out ] [--" "force] [--squash]\n" " patchsplit --gitlab --commit [--out ] [--" "force]\n" +" patchsplit --gitlab --commit [--out ] [--" +"force]\n" "\n" "Options:\n" " -o, --out Output directory for patch files [default: patches]\n" @@ -188,7 +192,8 @@ msgid "" " --gitlab Download from gitlab.com (merge requests and commits)\n" " --host Use a custom host, e.g. GitHub Enterprise or self-hosted " "GitLab\n" -" --commit Download one commit's .patch (short or full hash)\n" +" --commit Download one commit's .patch (short or full hash);\n" +" the hash may also be given before the flag\n" " --tor[=] Download through the Tor network over SOCKS5\n" " [default: 127.0.0.1:9050]\n" " -h, --help Show this help\n" diff --git a/po/zh_CN.po b/po/zh_CN.po index 1249c5e..c1c57c6 100644 --- a/po/zh_CN.po +++ b/po/zh_CN.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: patchsplit 0.1.0\n" "Report-Msgid-Bugs-To: https://github.com/zitzhen/patchsplit/issues\n" -"POT-Creation-Date: 2026-10-07 14:32+0800\n" +"POT-Creation-Date: 2026-11-21 22:27+0800\n" "PO-Revision-Date: 2026-08-10 19:11+0800\n" "Last-Translator: Automatically generated\n" "Language-Team: none\n" @@ -180,10 +180,14 @@ msgid "" " patchsplit [--out ] [--force] [--squash]\n" " patchsplit --commit [--out ] [--force]\n" " patchsplit --commit [--out ] [--force]\n" +" patchsplit --commit [--out ] [--force]\n" +" patchsplit --commit [--out ] [--force]\n" " patchsplit --gitlab [--out ] [--" "force] [--squash]\n" " patchsplit --gitlab --commit [--out ] [--" "force]\n" +" patchsplit --gitlab --commit [--out ] [--" +"force]\n" "\n" "Options:\n" " -o, --out Output directory for patch files [default: patches]\n" @@ -193,7 +197,8 @@ msgid "" " --gitlab Download from gitlab.com (merge requests and commits)\n" " --host Use a custom host, e.g. GitHub Enterprise or self-hosted " "GitLab\n" -" --commit Download one commit's .patch (short or full hash)\n" +" --commit Download one commit's .patch (short or full hash);\n" +" the hash may also be given before the flag\n" " --tor[=] Download through the Tor network over SOCKS5\n" " [default: 127.0.0.1:9050]\n" " -h, --help Show this help\n" @@ -213,10 +218,14 @@ msgstr "" " patchsplit [--out ] [--force] [--squash]\n" " patchsplit --commit [--out ] [--force]\n" " patchsplit --commit [--out ] [--force]\n" +" patchsplit --commit [--out ] [--force]\n" +" patchsplit --commit [--out ] [--force]\n" " patchsplit --gitlab [--out ] [--" "force] [--squash]\n" " patchsplit --gitlab --commit [--out ] [--" "force]\n" +" patchsplit --gitlab --commit [--out ] [--" +"force]\n" "\n" "选项:\n" " -o, --out patch 文件输出目录 [默认:patches]\n" @@ -224,7 +233,8 @@ msgstr "" " -s, --squash 将净变化输出为一个补丁,而不是按 commit 拆分\n" " --gitlab 从 gitlab.com 下载(合并请求和 commit)\n" " --host 使用自定义主机名,例如 GitHub Enterprise 或自建 GitLab\n" -" --commit 下载单个提交的 .patch(短哈希或完整哈希)\n" +" --commit 下载单个提交的 .patch(短哈希或完整哈希),\n" +" 哈希也可以写在 --commit 之前\n" " --tor[=] 通过 SOCKS5 经 Tor 网络下载\n" " [默认:127.0.0.1:9050]\n" " -h, --help 显示帮助信息\n" diff --git a/src/main.rs b/src/main.rs index 7dc24b9..51ddf32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,9 +119,10 @@ impl Config { let mut gitlab = false; let mut tor_proxy = None; let mut commit = None; + let mut commit_mode = false; let mut host = None::; let mut positionals = Vec::new(); - let mut args = args.into_iter(); + let mut args = args.into_iter().peekable(); while let Some(arg) = args.next() { match arg.as_str() { @@ -152,10 +153,15 @@ impl Config { host = Some(value["--host=".len()..].to_string()); } "-commit" | "--commit" => { - let value = args - .next() - .ok_or(AppError::MissingOptionValue("--commit".to_string()))?; - commit = Some(value); + // Both `--commit ` and a bare `--commit` mode flag are + // accepted; the latter takes the hash from the trailing + // positional, e.g. ` --commit`. + // A hash never starts with '-', so anything option-like here + // means the flag carries no value. + match args.next_if(|value| !value.starts_with('-')) { + Some(value) => commit = Some(value), + None => commit_mode = true, + } } value if value.starts_with("--commit=") => { commit = Some(value["--commit=".len()..].to_string()); @@ -170,10 +176,19 @@ impl Config { } } - if commit.is_some() && squash { + if (commit.is_some() || commit_mode) && squash { return Err(AppError::CommitWithSquash); } + // A bare `--commit` selects commit mode but leaves the hash to the last + // positional, so both ` --commit` and `--commit ` work. + if commit.is_none() && commit_mode { + if positionals.len() < 2 { + return Err(AppError::MissingOptionValue("--commit".to_string())); + } + commit = positionals.pop(); + } + let platform = if gitlab { Platform::GitLab } else { @@ -802,7 +817,7 @@ fn repo_segment_label(kind: &str) -> String { } fn usage() -> String { - tr("Usage:\n patchsplit [--out ] [--force] [--squash]\n patchsplit [--out ] [--force] [--squash]\n patchsplit --commit [--out ] [--force]\n patchsplit --commit [--out ] [--force]\n patchsplit --gitlab [--out ] [--force] [--squash]\n patchsplit --gitlab --commit [--out ] [--force]\n\nOptions:\n -o, --out Output directory for patch files [default: patches]\n -f, --force Overwrite existing patch files\n -s, --squash Write the net diff as one patch instead of splitting by commit\n --gitlab Download from gitlab.com (merge requests and commits)\n --host Use a custom host, e.g. GitHub Enterprise or self-hosted GitLab\n --commit Download one commit's .patch (short or full hash)\n --tor[=] Download through the Tor network over SOCKS5\n [default: 127.0.0.1:9050]\n -h, --help Show this help\n -V, --version Show version\n\nExamples:\n patchsplit rust-lang/rust 12345\n patchsplit openai codex 42 -o pr-42-patches\n patchsplit openai/codex 42 --squash\n patchsplit zitzhen patchsplit -commit b430113\n patchsplit --gitlab zitzhen/patchsplit 1\n patchsplit --host git.corp.example.com owner/repo 42\n patchsplit zitzhen/patchsplit 1 --tor") + tr("Usage:\n patchsplit [--out ] [--force] [--squash]\n patchsplit [--out ] [--force] [--squash]\n patchsplit --commit [--out ] [--force]\n patchsplit --commit [--out ] [--force]\n patchsplit --commit [--out ] [--force]\n patchsplit --commit [--out ] [--force]\n patchsplit --gitlab [--out ] [--force] [--squash]\n patchsplit --gitlab --commit [--out ] [--force]\n patchsplit --gitlab --commit [--out ] [--force]\n\nOptions:\n -o, --out Output directory for patch files [default: patches]\n -f, --force Overwrite existing patch files\n -s, --squash Write the net diff as one patch instead of splitting by commit\n --gitlab Download from gitlab.com (merge requests and commits)\n --host Use a custom host, e.g. GitHub Enterprise or self-hosted GitLab\n --commit Download one commit's .patch (short or full hash);\n the hash may also be given before the flag\n --tor[=] Download through the Tor network over SOCKS5\n [default: 127.0.0.1:9050]\n -h, --help Show this help\n -V, --version Show version\n\nExamples:\n patchsplit rust-lang/rust 12345\n patchsplit openai codex 42 -o pr-42-patches\n patchsplit openai/codex 42 --squash\n patchsplit zitzhen patchsplit -commit b430113\n patchsplit --gitlab zitzhen/patchsplit 1\n patchsplit --host git.corp.example.com owner/repo 42\n patchsplit zitzhen/patchsplit 1 --tor") } #[cfg(test)] mod tests { @@ -856,6 +871,13 @@ mod tests { ), (vec!["zitzhen/patchsplit", &full_double_dash], FULL_HASH), (vec!["zitzhen/patchsplit", &full_single_dash], FULL_HASH), + // A bare `--commit` mode flag takes the hash from the positional. + ( + vec!["zitzhen", "patchsplit", "b430113", "--commit"], + "b430113", + ), + (vec!["zitzhen/patchsplit", "b430113", "-commit"], "b430113"), + (vec!["zitzhen/patchsplit", FULL_HASH, "--commit"], FULL_HASH), ]; let patch = format!( "From {FULL_HASH} Mon Sep 17 00:00:00 2001\nSubject: [PATCH] One\n\ndiff --git a/a b/a\n" @@ -892,6 +914,17 @@ mod tests { } assert!(parse_err(&["owner/repo", "--commit"]).contains("MissingOptionValue")); + // The reported form ` --commit` selects the hash from + // the positional, so a non-hex trailing argument is rejected as a hash. + assert!(parse_err(&["owner", "repo", "42", "--commit"]).contains("InvalidCommitHash")); + // A following option must not be swallowed as the hash value. + assert!(parse_err(&["owner/repo", "--commit", "--force"]).contains("MissingOptionValue")); + assert!( + parse_err(&["owner/repo", "--commit", "-o", "out"]).contains("MissingOptionValue") + ); + assert!( + parse_err(&["owner", "repo", "--commit", "--squash"]).contains("CommitWithSquash") + ); assert!( parse_err(&["owner", "repo", "42", "--commit", "b430113"]) .contains("InvalidCommitArguments") @@ -975,6 +1008,18 @@ mod tests { ], "de9ea1a", ), + // Bare `--commit` after the hash also works for GitLab projects. + (vec!["--gitlab", project, "fafbad6", "--commit"], "fafbad6"), + ( + vec![ + "--gitlab", + "group/subgroup", + "project", + "de9ea1a", + "--commit", + ], + "de9ea1a", + ), ]; let patch = format!( "From {FULL_HASH} Mon Sep 17 00:00:00 2001\nSubject: [PATCH] One\n\ndiff --git a/a b/a\n" @@ -1017,6 +1062,8 @@ mod tests { ); assert!(parse_err(&["--gitlab", "group/project", "--commit", "xyz"]) .contains("InvalidCommitHash")); + assert!(parse_err(&["--gitlab", "group/project", "--commit"]) + .contains("MissingOptionValue")); assert!( parse_err(&[ "--gitlab", diff --git a/tests/commit.rs b/tests/commit.rs index 1db67c1..9fa84bc 100644 --- a/tests/commit.rs +++ b/tests/commit.rs @@ -109,6 +109,24 @@ cat \"$PATCHSPLIT_TEST_PATCH\"\n", let full_patch = full_dir.join(format!("{FULL_HASH}.patch")); assert_eq!(fs::read(&full_patch).unwrap(), patch_body().as_bytes()); + // The form reported in issue #18: the hash precedes the bare --commit flag. + let trailing_dir = root.0.join("trailing"); + let result = run(&[ + "zitzhen", + "patchsplit", + SHORT_HASH, + "--commit", + "--out", + trailing_dir.to_str().unwrap(), + ]); + assert!( + result.status.success(), + "{}", + String::from_utf8_lossy(&result.stderr) + ); + let trailing_patch = trailing_dir.join(format!("{SHORT_HASH}.patch")); + assert_eq!(fs::read(&trailing_patch).unwrap(), patch_body().as_bytes()); + let bad = run(&["zitzhen/patchsplit", "--commit", "xyz"]); assert_eq!(bad.status.code(), Some(2)); assert!(