| 1 |
/* |
| 2 |
* libgit2 "rev-parse" example - shows how to parse revspecs |
| 3 |
* |
| 4 |
* Written by the libgit2 contributors |
| 5 |
* |
| 6 |
* To the extent possible under law, the author(s) have dedicated all copyright |
| 7 |
* and related and neighboring rights to this software to the public domain |
| 8 |
* worldwide. This software is distributed without any warranty. |
| 9 |
* |
| 10 |
* You should have received a copy of the CC0 Public Domain Dedication along |
| 11 |
* with this software. If not, see |
| 12 |
* <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 13 |
*/ |
| 14 |
|
| 15 |
#![deny(warnings)] |
| 16 |
|
| 17 |
use clap::Parser; |
| 18 |
use git2::Repository; |
| 19 |
|
| 20 |
#[derive(Parser)] |
| 21 |
struct Args { |
| 22 |
#[structopt(name = "spec")] |
| 23 |
arg_spec: String, |
| 24 |
#[structopt(name = "dir", long = "git-dir")] |
| 25 |
/// directory of the git repository to check |
| 26 |
flag_git_dir: Option<String>, |
| 27 |
} |
| 28 |
|
| 29 |
fn run(args: &Args) -> Result<(), git2::Error> { |
| 30 |
let path = args.flag_git_dir.as_ref().map(|s| &s[..]).unwrap_or("."); |
| 31 |
let repo = Repository::open(path)?; |
| 32 |
|
| 33 |
let revspec = repo.revparse(&args.arg_spec)?; |
| 34 |
|
| 35 |
if revspec.mode().contains(git2::RevparseMode::SINGLE) { |
| 36 |
println!("{}", revspec.from().unwrap().id()); |
| 37 |
} else if revspec.mode().contains(git2::RevparseMode::RANGE) { |
| 38 |
let to = revspec.to().unwrap(); |
| 39 |
let from = revspec.from().unwrap(); |
| 40 |
println!("{}", to.id()); |
| 41 |
|
| 42 |
if revspec.mode().contains(git2::RevparseMode::MERGE_BASE) { |
| 43 |
let base = repo.merge_base(from.id(), to.id())?; |
| 44 |
println!("{}", base); |
| 45 |
} |
| 46 |
|
| 47 |
println!("^{}", from.id()); |
| 48 |
} else { |
| 49 |
return Err(git2::Error::from_str("invalid results from revparse")); |
| 50 |
} |
| 51 |
Ok(()) |
| 52 |
} |
| 53 |
|
| 54 |
fn main() { |
| 55 |
let args = Args::parse(); |
| 56 |
match run(&args) { |
| 57 |
Ok(()) => {} |
| 58 |
Err(e) => println!("error: {}", e), |
| 59 |
} |
| 60 |
} |