Skip to content
This repository was archived by the owner on Mar 27, 2026. It is now read-only.

Commit 4f106cb

Browse files
committed
extend -p
1 parent 8064196 commit 4f106cb

1 file changed

Lines changed: 50 additions & 41 deletions

File tree

src/cli.rs

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,83 @@ pub struct Args {
1313
#[arg(short, long)]
1414
pub output_file: Option<String>,
1515

16-
/// The first commit hash
17-
#[arg(short = 'c', long = "commit1")]
18-
pub commit1: Option<String>,
19-
20-
/// The second commit hash
21-
#[arg(short = 'd', long = "commit2")]
22-
pub commit2: Option<String>,
16+
/// The commit hash to compare (the 'newer' commit)
17+
#[arg(short = 'c', long = "commit")]
18+
pub commit: Option<String>,
2319

2420
/// Compare the latest commit on the current branch to the latest common commit with another branch
25-
#[arg(short, long)]
21+
#[arg(short, long, conflicts_with_all = ["commit", "previous"])]
2622
pub branch: Option<String>,
2723

28-
/// Compare the specified commit with its parent (previous) commit
29-
#[arg(short = 'p', long = "previous", requires = "commit1", conflicts_with_all = ["commit2", "branch"])]
30-
pub use_previous: bool,
24+
/// Compare the specified commit (--commit) with a previous commit.
25+
/// If a hash is provided, compare with that specific hash.
26+
/// If no hash is provided, compare with the parent of the commit specified by --commit.
27+
#[arg(short = 'p', long = "previous", value_name = "PREVIOUS_COMMIT_HASH", num_args = 0..=1, requires = "commit", conflicts_with = "branch")]
28+
pub previous: Option<Option<String>>,
3129
}
3230

3331
/// Main entry point for the CLI
3432
pub fn run() -> Result<()> {
3533
let args = Args::parse();
3634

37-
// Initialize the RepoDiff tool
35+
// Initialize the RepoDiff tool and GitOperations
3836
let mut repodiff = RepoDiff::new("config.json")?;
3937
let git_ops = GitOperations::new();
4038

41-
// Determine the commit hashes
39+
// Determine the commit hashes based on provided arguments
4240
let (commit1, commit2) = if let Some(branch) = args.branch {
41+
// Branch comparison logic
4342
let commit1 = git_ops.get_latest_common_commit_with_branch(&branch)?;
4443
let commit2 = git_ops.get_latest_commit()?;
4544

46-
// Print the commits being used for the comparison
4745
println!(
4846
"Comparing latest common commit with branch '{}' ({}) and the latest commit on the current branch ({}).",
4947
branch,
5048
&commit1[..12.min(commit1.len())],
5149
&commit2[..12.min(commit2.len())]
5250
);
53-
54-
(commit1, commit2)
55-
} else if args.use_previous && args.commit1.is_some() {
56-
let commit2 = args.commit1.clone().unwrap();
57-
let commit1 = git_ops.get_previous_commit(&commit2)?;
58-
59-
// Print the commits being used for the comparison
60-
println!(
61-
"Comparing commit {} with its parent commit {}.",
62-
&commit2[..12.min(commit2.len())],
63-
&commit1[..12.min(commit1.len())]
64-
);
65-
6651
(commit1, commit2)
67-
} else {
68-
if args.commit1.is_none() || args.commit2.is_none() {
69-
eprintln!("You must either provide two commit hashes using --commit1 and --commit2, or use the -b option to compare against another branch, or use -p with -c to compare with the previous commit.");
70-
process::exit(1);
52+
53+
} else if let Some(commit_to_compare) = args.commit {
54+
// Commit comparison logic (using --commit and --previous)
55+
match args.previous {
56+
Some(Some(prev_commit_hash)) => {
57+
// -p <hash> provided: Compare commit_to_compare with prev_commit_hash
58+
let commit1 = prev_commit_hash;
59+
let commit2 = commit_to_compare;
60+
println!(
61+
"Comparing specified commit {} with previous commit {}.",
62+
&commit2[..12.min(commit2.len())],
63+
&commit1[..12.min(commit1.len())]
64+
);
65+
(commit1, commit2)
66+
}
67+
Some(None) => {
68+
// -p flag provided without value: Compare commit_to_compare with its parent
69+
let commit2 = commit_to_compare;
70+
let commit1 = git_ops.get_previous_commit(&commit2)?;
71+
println!(
72+
"Comparing specified commit {} with its parent commit {}.",
73+
&commit2[..12.min(commit2.len())],
74+
&commit1[..12.min(commit1.len())]
75+
);
76+
(commit1, commit2)
77+
}
78+
None => {
79+
// Only -c provided, which is not enough for comparison.
80+
eprintln!("Missing comparison target. Use --previous (-p) to compare with a parent or specific commit when using --commit, or use --branch (-b) to compare with another branch.");
81+
process::exit(1);
82+
}
7183
}
72-
73-
(args.commit1.unwrap(), args.commit2.unwrap())
74-
};
75-
76-
// Set output file or default to the user's temporary directory
77-
let output_file = if let Some(output_file) = args.output_file {
78-
output_file
7984
} else {
80-
let default_output = "repodiff_output.txt".to_string(); // Default filename in the working directory
81-
default_output
85+
// Neither --branch nor --commit specified.
86+
eprintln!("You must specify either a branch to compare (--branch) or a commit to compare (--commit) along with a comparison target (--previous).");
87+
process::exit(1);
8288
};
8389

90+
// Set output file or default
91+
let output_file = args.output_file.unwrap_or_else(|| "repodiff_output.txt".to_string());
92+
8493
// Process the diff and get the token count
8594
let token_count = repodiff.process_diff(&commit1, &commit2, &output_file)?;
8695

@@ -89,4 +98,4 @@ pub fn run() -> Result<()> {
8998
println!("Total number of tokens: {}", token_count);
9099

91100
Ok(())
92-
}
101+
}

0 commit comments

Comments
 (0)