| 1 |
/* |
| 2 |
* libgit2 "cat-file" example - shows how to print data from the ODB |
| 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 std::io::{self, Write}; |
| 18 |
|
| 19 |
use clap::Parser; |
| 20 |
use git2::{Blob, Commit, ObjectType, Repository, Signature, Tag, Tree}; |
| 21 |
|
| 22 |
#[derive(Parser)] |
| 23 |
struct Args { |
| 24 |
#[structopt(name = "object")] |
| 25 |
arg_object: String, |
| 26 |
#[structopt(short = 't')] |
| 27 |
/// show the object type |
| 28 |
flag_t: bool, |
| 29 |
#[structopt(short = 's')] |
| 30 |
/// show the object size |
| 31 |
flag_s: bool, |
| 32 |
#[structopt(short = 'e')] |
| 33 |
/// suppress all output |
| 34 |
flag_e: bool, |
| 35 |
#[structopt(short = 'p')] |
| 36 |
/// pretty print the contents of the object |
| 37 |
flag_p: bool, |
| 38 |
#[structopt(name = "quiet", short, long)] |
| 39 |
/// suppress output |
| 40 |
flag_q: bool, |
| 41 |
#[structopt(name = "verbose", short, long)] |
| 42 |
flag_v: bool, |
| 43 |
#[structopt(name = "dir", long = "git-dir")] |
| 44 |
/// use the specified directory as the base directory |
| 45 |
flag_git_dir: Option<String>, |
| 46 |
} |
| 47 |
|
| 48 |
fn run(args: &Args) -> Result<(), git2::Error> { |
| 49 |
let path = args.flag_git_dir.as_ref().map(|s| &s[..]).unwrap_or("."); |
| 50 |
let repo = Repository::open(path)?; |
| 51 |
|
| 52 |
let obj = repo.revparse_single(&args.arg_object)?; |
| 53 |
if args.flag_v && !args.flag_q { |
| 54 |
println!("{} {}\n--", obj.kind().unwrap().str(), obj.id()); |
| 55 |
} |
| 56 |
|
| 57 |
if args.flag_t { |
| 58 |
println!("{}", obj.kind().unwrap().str()); |
| 59 |
} else if args.flag_s || args.flag_e { |
| 60 |
/* ... */ |
| 61 |
} else if args.flag_p { |
| 62 |
match obj.kind() { |
| 63 |
Some(ObjectType::Blob) => { |
| 64 |
show_blob(obj.as_blob().unwrap()); |
| 65 |
} |
| 66 |
Some(ObjectType::Commit) => { |
| 67 |
show_commit(obj.as_commit().unwrap()); |
| 68 |
} |
| 69 |
Some(ObjectType::Tag) => { |
| 70 |
show_tag(obj.as_tag().unwrap()); |
| 71 |
} |
| 72 |
Some(ObjectType::Tree) => { |
| 73 |
show_tree(obj.as_tree().unwrap()); |
| 74 |
} |
| 75 |
Some(ObjectType::Any) | None => println!("unknown {}", obj.id()), |
| 76 |
} |
| 77 |
} |
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
fn show_blob(blob: &Blob) { |
| 82 |
io::stdout().write_all(blob.content()).unwrap(); |
| 83 |
} |
| 84 |
|
| 85 |
fn show_commit(commit: &Commit) { |
| 86 |
println!("tree {}", commit.tree_id()); |
| 87 |
for parent in commit.parent_ids() { |
| 88 |
println!("parent {}", parent); |
| 89 |
} |
| 90 |
show_sig("author", Some(commit.author())); |
| 91 |
show_sig("committer", Some(commit.committer())); |
| 92 |
if let Ok(msg) = commit.message() { |
| 93 |
println!("\n{}", msg); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
fn show_tag(tag: &Tag) { |
| 98 |
println!("object {}", tag.target_id()); |
| 99 |
println!("type {}", tag.target_type().unwrap().str()); |
| 100 |
println!("tag {}", tag.name().unwrap()); |
| 101 |
show_sig("tagger", tag.tagger()); |
| 102 |
|
| 103 |
if let Ok(Some(msg)) = tag.message() { |
| 104 |
println!("\n{}", msg); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
fn show_tree(tree: &Tree) { |
| 109 |
for entry in tree.iter() { |
| 110 |
println!( |
| 111 |
"{:06o} {} {}\t{}", |
| 112 |
entry.filemode(), |
| 113 |
entry.kind().unwrap().str(), |
| 114 |
entry.id(), |
| 115 |
entry.name().unwrap() |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
fn show_sig(header: &str, sig: Option<Signature>) { |
| 121 |
let sig = match sig { |
| 122 |
Some(s) => s, |
| 123 |
None => return, |
| 124 |
}; |
| 125 |
let offset = sig.when().offset_minutes(); |
| 126 |
let (sign, offset) = if offset < 0 { |
| 127 |
('-', -offset) |
| 128 |
} else { |
| 129 |
('+', offset) |
| 130 |
}; |
| 131 |
let (hours, minutes) = (offset / 60, offset % 60); |
| 132 |
println!( |
| 133 |
"{} {} {} {}{:02}{:02}", |
| 134 |
header, |
| 135 |
sig, |
| 136 |
sig.when().seconds(), |
| 137 |
sign, |
| 138 |
hours, |
| 139 |
minutes |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
fn main() { |
| 144 |
let args = Args::parse(); |
| 145 |
match run(&args) { |
| 146 |
Ok(()) => {} |
| 147 |
Err(e) => println!("error: {}", e), |
| 148 |
} |
| 149 |
} |