Skip to content

Commit

Permalink
Update to 2018 edition.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 4, 2019
1 parent 9f1f9d8 commit e39a0ac
Show file tree
Hide file tree
Showing 65 changed files with 699 additions and 676 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ both threadsafe and memory safe and allows both reading and writing git
repositories.
"""
categories = ["api-bindings"]
edition = "2018"

[badges]
travis-ci = { repository = "rust-lang/git2-rs" }
Expand Down
10 changes: 5 additions & 5 deletions examples/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ struct Args {
}

fn run(args: &Args) -> Result<(), git2::Error> {
let repo = try!(Repository::open(&Path::new(".")));
let mut index = try!(repo.index());
let repo = Repository::open(&Path::new("."))?;
let mut index = repo.index()?;

let cb = &mut |path: &Path, _matched_spec: &[u8]| -> i32 {
let status = repo.status_file(path).unwrap();
Expand All @@ -61,12 +61,12 @@ fn run(args: &Args) -> Result<(), git2::Error> {
};

if args.flag_update {
try!(index.update_all(args.arg_spec.iter(), cb));
index.update_all(args.arg_spec.iter(), cb)?;
} else {
try!(index.add_all(args.arg_spec.iter(), git2::IndexAddOption::DEFAULT, cb));
index.add_all(args.arg_spec.iter(), git2::IndexAddOption::DEFAULT, cb)?;
}

try!(index.write());
index.write()?;
Ok(())
}

Expand Down
10 changes: 5 additions & 5 deletions examples/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Args {
}

fn run(args: &Args) -> Result<(), git2::Error> {
let repo = try!(Repository::open("."));
let repo = Repository::open(".")?;
let path = Path::new(&args.arg_path[..]);

// Prepare our blame options
Expand All @@ -46,7 +46,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {

// Parse spec
if let Some(spec) = args.arg_spec.as_ref() {
let revspec = try!(repo.revparse(spec));
let revspec = repo.revparse(spec)?;

let (oldest, newest) = if revspec.mode().contains(git2::RevparseMode::SINGLE) {
(None, revspec.from())
Expand All @@ -69,9 +69,9 @@ fn run(args: &Args) -> Result<(), git2::Error> {
}

let spec = format!("{}:{}", commit_id, path.display());
let blame = try!(repo.blame_file(path, Some(&mut opts)));
let object = try!(repo.revparse_single(&spec[..]));
let blob = try!(repo.find_blob(object.id()));
let blame = repo.blame_file(path, Some(&mut opts))?;
let object = repo.revparse_single(&spec[..])?;
let blob = repo.find_blob(object.id())?;
let reader = BufReader::new(blob.content());

for (i, line) in reader.lines().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions examples/cat-file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ struct Args {

fn run(args: &Args) -> Result<(), git2::Error> {
let path = args.flag_git_dir.as_ref().map(|s| &s[..]).unwrap_or(".");
let repo = try!(Repository::open(path));
let repo = Repository::open(path)?;

let obj = try!(repo.revparse_single(&args.arg_object));
let obj = repo.revparse_single(&args.arg_object)?;
if args.flag_v && !args.flag_q {
println!("{} {}\n--", obj.kind().unwrap().str(), obj.id());
}
Expand Down
4 changes: 2 additions & 2 deletions examples/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ fn run(args: &Args) -> Result<(), git2::Error> {

let mut fo = FetchOptions::new();
fo.remote_callbacks(cb);
try!(RepoBuilder::new()
RepoBuilder::new()
.fetch_options(fo)
.with_checkout(co)
.clone(&args.arg_url, Path::new(&args.arg_path)));
.clone(&args.arg_url, Path::new(&args.arg_path))?;
println!("");

Ok(())
Expand Down
34 changes: 17 additions & 17 deletions examples/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ enum Cache {

fn run(args: &Args) -> Result<(), Error> {
let path = args.flag_git_dir.as_ref().map(|s| &s[..]).unwrap_or(".");
let repo = try!(Repository::open(path));
let repo = Repository::open(path)?;

// Prepare our diff options based on the arguments given
let mut opts = DiffOptions::new();
Expand Down Expand Up @@ -112,25 +112,25 @@ fn run(args: &Args) -> Result<(), Error> {
}

// Prepare the diff to inspect
let t1 = try!(tree_to_treeish(&repo, args.arg_from_oid.as_ref()));
let t2 = try!(tree_to_treeish(&repo, args.arg_to_oid.as_ref()));
let head = try!(tree_to_treeish(&repo, Some(&"HEAD".to_string()))).unwrap();
let t1 = tree_to_treeish(&repo, args.arg_from_oid.as_ref())?;
let t2 = tree_to_treeish(&repo, args.arg_to_oid.as_ref())?;
let head = tree_to_treeish(&repo, Some(&"HEAD".to_string()))?.unwrap();
let mut diff = match (t1, t2, args.cache()) {
(Some(t1), Some(t2), _) => {
try!(repo.diff_tree_to_tree(t1.as_tree(), t2.as_tree(), Some(&mut opts)))
repo.diff_tree_to_tree(t1.as_tree(), t2.as_tree(), Some(&mut opts))?
}
(t1, None, Cache::None) => {
let t1 = t1.unwrap_or(head);
try!(repo.diff_tree_to_workdir(t1.as_tree(), Some(&mut opts)))
repo.diff_tree_to_workdir(t1.as_tree(), Some(&mut opts))?
}
(t1, None, Cache::Only) => {
let t1 = t1.unwrap_or(head);
try!(repo.diff_tree_to_index(t1.as_tree(), None, Some(&mut opts)))
repo.diff_tree_to_index(t1.as_tree(), None, Some(&mut opts))?
}
(Some(t1), None, _) => {
try!(repo.diff_tree_to_workdir_with_index(t1.as_tree(), Some(&mut opts)))
repo.diff_tree_to_workdir_with_index(t1.as_tree(), Some(&mut opts))?
}
(None, None, _) => try!(repo.diff_index_to_workdir(None, Some(&mut opts))),
(None, None, _) => repo.diff_index_to_workdir(None, Some(&mut opts))?,
(None, Some(_), _) => unreachable!(),
};

Expand All @@ -151,20 +151,20 @@ fn run(args: &Args) -> Result<(), Error> {
}
opts.copies_from_unmodified(args.flag_find_copies_harder)
.rewrites(args.flag_break_rewrites);
try!(diff.find_similar(Some(&mut opts)));
diff.find_similar(Some(&mut opts))?;
}

// Generate simple output
let stats = args.flag_stat | args.flag_numstat | args.flag_shortstat | args.flag_summary;
if stats {
try!(print_stats(&diff, args));
print_stats(&diff, args)?;
}
if args.flag_patch || !stats {
if args.color() {
print!("{}", RESET);
}
let mut last_color = None;
try!(diff.print(args.diff_format(), |_delta, _hunk, line| {
diff.print(args.diff_format(), |_delta, _hunk, line| {
if args.color() {
let next = match line.origin() {
'+' => Some(GREEN),
Expand All @@ -190,7 +190,7 @@ fn run(args: &Args) -> Result<(), Error> {
}
print!("{}", str::from_utf8(line.content()).unwrap());
true
}));
})?;
if args.color() {
print!("{}", RESET);
}
Expand All @@ -200,7 +200,7 @@ fn run(args: &Args) -> Result<(), Error> {
}

fn print_stats(diff: &Diff, args: &Args) -> Result<(), Error> {
let stats = try!(diff.stats());
let stats = diff.stats()?;
let mut format = git2::DiffStatsFormat::NONE;
if args.flag_stat {
format |= git2::DiffStatsFormat::FULL;
Expand All @@ -214,7 +214,7 @@ fn print_stats(diff: &Diff, args: &Args) -> Result<(), Error> {
if args.flag_summary {
format |= git2::DiffStatsFormat::INCLUDE_SUMMARY;
}
let buf = try!(stats.to_buf(format, 80));
let buf = stats.to_buf(format, 80)?;
print!("{}", str::from_utf8(&*buf).unwrap());
Ok(())
}
Expand All @@ -227,8 +227,8 @@ fn tree_to_treeish<'a>(
Some(s) => s,
None => return Ok(None),
};
let obj = try!(repo.revparse_single(arg));
let tree = try!(obj.peel(ObjectType::Tree));
let obj = repo.revparse_single(arg)?;
let tree = obj.peel(ObjectType::Tree)?;
Ok(Some(tree))
}

Expand Down
10 changes: 5 additions & 5 deletions examples/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ struct Args {
}

fn run(args: &Args) -> Result<(), git2::Error> {
let repo = try!(Repository::open("."));
let repo = Repository::open(".")?;
let remote = args.arg_remote.as_ref().map(|s| &s[..]).unwrap_or("origin");

// Figure out whether it's a named remote or a URL
println!("Fetching {} for repo", remote);
let mut cb = RemoteCallbacks::new();
let mut remote = try!(repo
let mut remote = repo
.find_remote(remote)
.or_else(|_| { repo.remote_anonymous(remote) }));
.or_else(|_| repo.remote_anonymous(remote))?;
cb.sideband_progress(|data| {
print!("remote: {}", str::from_utf8(data).unwrap());
io::stdout().flush().unwrap();
Expand Down Expand Up @@ -85,7 +85,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
// progress.
let mut fo = FetchOptions::new();
fo.remote_callbacks(cb);
try!(remote.download(&[], Some(&mut fo)));
remote.download(&[], Some(&mut fo))?;

{
// If there are local objects (we got a thin pack), then tell the user
Expand Down Expand Up @@ -117,7 +117,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
// commits. This may be needed even if there was no packfile to download,
// which can happen e.g. when the branches have been changed but all the
// needed objects are available locally.
try!(remote.update_tips(None, true, AutotagOption::Unspecified, None));
remote.update_tips(None, true, AutotagOption::Unspecified, None)?;

Ok(())
}
Expand Down
18 changes: 9 additions & 9 deletions examples/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn run(args: &Args) -> Result<(), Error> {
&& args.flag_shared.is_none()
&& args.flag_separate_git_dir.is_none()
{
try!(Repository::init(&path))
Repository::init(&path)?
} else {
let mut opts = RepositoryInitOptions::new();
opts.bare(args.flag_bare);
Expand All @@ -58,9 +58,9 @@ fn run(args: &Args) -> Result<(), Error> {
}

if let Some(ref s) = args.flag_shared {
opts.mode(try!(parse_shared(s)));
opts.mode(parse_shared(s)?);
}
try!(Repository::init_opts(&path, &opts))
Repository::init_opts(&path, &opts)?
};

// Print a message to stdout like "git init" does
Expand All @@ -74,7 +74,7 @@ fn run(args: &Args) -> Result<(), Error> {
}

if args.flag_initial_commit {
try!(create_initial_commit(&repo));
create_initial_commit(&repo)?;
println!("Created empty initial commit");
}

Expand All @@ -85,27 +85,27 @@ fn run(args: &Args) -> Result<(), Error> {
/// commit in the repository. This is the helper function that does that.
fn create_initial_commit(repo: &Repository) -> Result<(), Error> {
// First use the config to initialize a commit signature for the user.
let sig = try!(repo.signature());
let sig = repo.signature()?;

// Now let's create an empty tree for this commit
let tree_id = {
let mut index = try!(repo.index());
let mut index = repo.index()?;

// Outside of this example, you could call index.add_path()
// here to put actual files into the index. For our purposes, we'll
// leave it empty for now.

try!(index.write_tree())
index.write_tree()?
};

let tree = try!(repo.find_tree(tree_id));
let tree = repo.find_tree(tree_id)?;

// Ready to create the initial commit.
//
// Normally creating a commit would involve looking up the current HEAD
// commit and making that be the parent of the initial commit, but here this
// is the first commit so there will be no parent.
try!(repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[]));
repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])?;

Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions examples/ls-remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ struct Args {
}

fn run(args: &Args) -> Result<(), git2::Error> {
let repo = try!(Repository::open("."));
let repo = Repository::open(".")?;
let remote = &args.arg_remote;
let mut remote = try!(repo
let mut remote = repo
.find_remote(remote)
.or_else(|_| { repo.remote_anonymous(remote) }));
.or_else(|_| repo.remote_anonymous(remote))?;

// Connect to the remote and call the printing function for each of the
// remote references.
let connection = try!(remote.connect_auth(Direction::Fetch, None, None));
let connection = remote.connect_auth(Direction::Fetch, None, None)?;

// Get the list of references on the remote and print out their name next to
// what they point to.
for head in try!(connection.list()).iter() {
for head in connection.list()?.iter() {
println!("{}\t{}", head.oid(), head.name());
}
Ok(())
Expand Down
14 changes: 7 additions & 7 deletions examples/rev-list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ struct Args {
}

fn run(args: &Args) -> Result<(), git2::Error> {
let repo = try!(Repository::open("."));
let mut revwalk = try!(repo.revwalk());
let repo = Repository::open(".")?;
let mut revwalk = repo.revwalk()?;

let base = if args.flag_reverse {
git2::Sort::REVERSE
Expand Down Expand Up @@ -65,20 +65,20 @@ fn run(args: &Args) -> Result<(), git2::Error> {
});
for (spec, hide) in specs {
let id = if spec.contains("..") {
let revspec = try!(repo.revparse(spec));
let revspec = repo.revparse(spec)?;
if revspec.mode().contains(git2::RevparseMode::MERGE_BASE) {
return Err(Error::from_str("merge bases not implemented"));
}
try!(push(&mut revwalk, revspec.from().unwrap().id(), !hide));
push(&mut revwalk, revspec.from().unwrap().id(), !hide)?;
revspec.to().unwrap().id()
} else {
try!(repo.revparse_single(spec)).id()
repo.revparse_single(spec)?.id()
};
try!(push(&mut revwalk, id, hide));
push(&mut revwalk, id, hide)?;
}

for id in revwalk {
let id = try!(id);
let id = id?;
println!("{}", id);
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions examples/rev-parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct Args {

fn run(args: &Args) -> Result<(), git2::Error> {
let path = args.flag_git_dir.as_ref().map(|s| &s[..]).unwrap_or(".");
let repo = try!(Repository::open(path));
let repo = Repository::open(path)?;

let revspec = try!(repo.revparse(&args.arg_spec));
let revspec = repo.revparse(&args.arg_spec)?;

if revspec.mode().contains(git2::RevparseMode::SINGLE) {
println!("{}", revspec.from().unwrap().id());
Expand All @@ -42,7 +42,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
println!("{}", to.id());

if revspec.mode().contains(git2::RevparseMode::MERGE_BASE) {
let base = try!(repo.merge_base(from.id(), to.id()));
let base = repo.merge_base(from.id(), to.id())?;
println!("{}", base);
}

Expand Down
Loading

0 comments on commit e39a0ac

Please sign in to comment.