Skip to content

Commit 9fe8cbd

Browse files
committed
refactor: move cli into private module
refactor: move `exe` into library target assisted-by: Zed (Claude Sonnet 4.6)
1 parent 2f252d4 commit 9fe8cbd

7 files changed

Lines changed: 65 additions & 49 deletions

File tree

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
1-
use clap::{Args, Parser};
2-
3-
/// Arguments for filtering a Git tree by gitattributes-style patterns.
4-
/// Defined here so it can be re-used as a subcommand in the porcelain `git-rewrite` CLI.
5-
#[derive(Args, Clone)]
6-
pub struct FilterTreeArgs {
7-
/// Tree-ish reference (commit, branch, tag, or tree SHA)
8-
pub treeish: String,
9-
10-
/// Gitattributes-style patterns to filter tree entries
11-
#[arg(required = true)]
12-
pub patterns: Vec<String>,
13-
14-
/// Output format
15-
#[arg(short, long, value_enum, default_value = "tree-sha")]
16-
pub format: OutputFormat,
17-
}
1+
use clap::Parser;
2+
use git_filter_tree::exe::FilterTreeArgs;
183

194
#[derive(Parser)]
205
#[command(name = "git filter-tree", bin_name = "git filter-tree")]
21-
#[command(author, version, about = "Filter Git tree entries by gitattributes-style patterns", long_about = None)]
6+
#[command(author, version, about = "Filter Git tree entries by glob patterns")]
227
pub struct Cli {
238
#[command(flatten)]
249
pub args: FilterTreeArgs,
2510
}
26-
27-
#[derive(Clone, Copy, clap::ValueEnum)]
28-
pub enum OutputFormat {
29-
/// Output only the tree SHA
30-
TreeSha,
31-
/// Output tree entries (name and type)
32-
Entries,
33-
/// Output detailed tree information
34-
Detailed,
35-
}

plumbing/git-filter-tree/src/exe.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
use crate::FilterTree;
2-
use crate::cli::{FilterTreeArgs, OutputFormat};
32
use git2 as git;
43

4+
/// Arguments for filtering a Git tree by glob patterns.
5+
///
6+
/// Defined here so that the library exposes a self-contained type that
7+
/// downstream code can construct directly without going through the CLI.
8+
#[derive(clap::Args, Clone)]
9+
pub struct FilterTreeArgs {
10+
/// Tree-ish reference (commit, branch, tag, or tree SHA).
11+
pub treeish: String,
12+
13+
/// Glob patterns for entries to keep in the tree.
14+
#[arg(required = true)]
15+
pub patterns: Vec<String>,
16+
17+
/// Output format.
18+
#[arg(short, long, value_enum, default_value = "tree-sha")]
19+
pub format: OutputFormat,
20+
}
21+
22+
/// Output format for [`print_tree`].
23+
#[derive(Clone, Copy, Default, clap::ValueEnum)]
24+
pub enum OutputFormat {
25+
/// Print only the tree SHA (default).
26+
#[default]
27+
TreeSha,
28+
/// Print each entry's type and name.
29+
Entries,
30+
/// Print mode, type, OID, and name for every entry.
31+
Detailed,
32+
}
33+
534
/// Core filter-tree operation: resolve the treeish, filter by patterns, and
635
/// return the OID of the resulting tree. This is the reusable building-block
736
/// that both the plumbing binary and the porcelain CLI can call.

plumbing/git-filter-tree/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
mod cli;
2+
13
use clap::{CommandFactory, Parser};
2-
use git_filter_tree::cli::Cli;
4+
use cli::Cli;
35
use std::path::PathBuf;
46
use std::process;
57

src/cli.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use clap::Parser;
2+
use git_rewrite::exe::TreeArgs;
23

34
#[derive(Parser)]
45
#[command(name = "git rewrite", bin_name = "git rewrite")]
5-
#[command(author, version, about = "Rewrite Git repository history, trees, and blobs", long_about = None)]
6+
#[command(
7+
author,
8+
version,
9+
about = "Rewrite Git repository history, trees, and blobs"
10+
)]
611
pub struct Cli {
712
#[command(subcommand)]
813
pub command: Command,
@@ -13,18 +18,3 @@ pub enum Command {
1318
/// Rewrite a Git tree, keeping only matching entries
1419
Tree(TreeArgs),
1520
}
16-
17-
#[derive(clap::Args)]
18-
pub struct TreeArgs {
19-
/// Tree-ish reference (commit, branch, tag, or tree SHA) [default: HEAD]
20-
#[arg(default_value = "HEAD")]
21-
pub treeish: String,
22-
23-
/// Glob patterns for entries to keep in the tree (may be repeated)
24-
#[arg(long = "only", required = true)]
25-
pub patterns: Vec<String>,
26-
27-
/// Allow running with uncommitted changes in the working tree
28-
#[arg(long)]
29-
pub allow_dirty: bool,
30-
}

src/exe.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
use crate::cli::TreeArgs;
21
use git2 as git;
32

3+
/// Arguments for the `tree` subcommand.
4+
///
5+
/// Defined here so that the library exposes a self-contained type that
6+
/// downstream code can construct directly without going through the CLI.
7+
#[derive(clap::Args, Clone)]
8+
pub struct TreeArgs {
9+
/// Tree-ish reference (commit, branch, tag, or tree SHA).
10+
#[arg(default_value = "HEAD")]
11+
pub treeish: String,
12+
13+
/// Glob patterns for entries to keep in the tree (may be repeated).
14+
#[arg(long = "only", required = true)]
15+
pub patterns: Vec<String>,
16+
17+
/// Allow running with uncommitted changes in the working tree.
18+
#[arg(long)]
19+
pub allow_dirty: bool,
20+
}
21+
422
pub fn tree(args: &TreeArgs) -> Result<(), Box<dyn std::error::Error>> {
523
let repo = git::Repository::open_from_env()?;
624

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@
4444
//! # Ok::<(), Box<dyn std::error::Error>>(())
4545
//! ```
4646
47-
pub mod cli;
47+
pub mod exe;
48+
49+
pub use exe::TreeArgs;

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod cli;
2-
mod exe;
32

43
use clap::{CommandFactory, Parser};
54
use cli::{Cli, Command};
5+
use git_rewrite::exe;
66
use std::path::PathBuf;
77
use std::process;
88

0 commit comments

Comments
 (0)