Skip to content

Commit e1d5921

Browse files
authored
Merge pull request #428 from ehuss/edition
Update edition and rustfmt
2 parents 4c9480a + d327bb1 commit e1d5921

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6122
-4498
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ both threadsafe and memory safe and allows both reading and writing git
1414
repositories.
1515
"""
1616
categories = ["api-bindings"]
17+
edition = "2018"
1718

1819
[badges]
1920
travis-ci = { repository = "rust-lang/git2-rs" }

examples/add.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
#![deny(warnings)]
1616
#![allow(trivial_casts)]
1717

18-
extern crate git2;
19-
extern crate docopt;
20-
#[macro_use]
21-
extern crate serde_derive;
22-
23-
use std::path::Path;
2418
use docopt::Docopt;
2519
use git2::Repository;
20+
use serde_derive::Deserialize;
21+
use std::path::Path;
2622

2723
#[derive(Deserialize)]
2824
struct Args {
@@ -33,21 +29,26 @@ struct Args {
3329
}
3430

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

3935
let cb = &mut |path: &Path, _matched_spec: &[u8]| -> i32 {
4036
let status = repo.status_file(path).unwrap();
4137

42-
let ret = if status.contains(git2::Status::WT_MODIFIED) ||
43-
status.contains(git2::Status::WT_NEW) {
38+
let ret = if status.contains(git2::Status::WT_MODIFIED)
39+
|| status.contains(git2::Status::WT_NEW)
40+
{
4441
println!("add '{}'", path.display());
4542
0
4643
} else {
4744
1
4845
};
4946

50-
if args.flag_dry_run {1} else {ret}
47+
if args.flag_dry_run {
48+
1
49+
} else {
50+
ret
51+
}
5152
};
5253
let cb = if args.flag_verbose || args.flag_update {
5354
Some(cb as &mut git2::IndexMatchedPath)
@@ -56,17 +57,17 @@ fn run(args: &Args) -> Result<(), git2::Error> {
5657
};
5758

5859
if args.flag_update {
59-
try!(index.update_all(args.arg_spec.iter(), cb));
60+
index.update_all(args.arg_spec.iter(), cb)?;
6061
} else {
61-
try!(index.add_all(args.arg_spec.iter(), git2::IndexAddOption::DEFAULT, cb));
62+
index.add_all(args.arg_spec.iter(), git2::IndexAddOption::DEFAULT, cb)?;
6263
}
6364

64-
try!(index.write());
65+
index.write()?;
6566
Ok(())
6667
}
6768

6869
fn main() {
69-
const USAGE: &'static str = "
70+
const USAGE: &str = "
7071
usage: add [options] [--] [<spec>..]
7172
7273
Options:
@@ -76,8 +77,9 @@ Options:
7677
-h, --help show this message
7778
";
7879

79-
let args = Docopt::new(USAGE).and_then(|d| d.deserialize())
80-
.unwrap_or_else(|e| e.exit());
80+
let args = Docopt::new(USAGE)
81+
.and_then(|d| d.deserialize())
82+
.unwrap_or_else(|e| e.exit());
8183
match run(&args) {
8284
Ok(()) => {}
8385
Err(e) => println!("error: {}", e),

examples/blame.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212
* <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
*/
1414

15-
extern crate git2;
16-
extern crate docopt;
17-
#[macro_use]
18-
extern crate serde_derive;
19-
2015
use docopt::Docopt;
21-
use git2::{Repository, BlameOptions};
16+
use git2::{BlameOptions, Repository};
17+
use serde_derive::Deserialize;
18+
use std::io::{BufRead, BufReader};
2219
use std::path::Path;
23-
use std::io::{BufReader, BufRead};
2420

25-
#[derive(Deserialize)] #[allow(non_snake_case)]
21+
#[derive(Deserialize)]
22+
#[allow(non_snake_case)]
2623
struct Args {
2724
arg_path: String,
2825
arg_spec: Option<String>,
@@ -32,7 +29,7 @@ struct Args {
3229
}
3330

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

3835
// Prepare our blame options
@@ -45,8 +42,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
4542

4643
// Parse spec
4744
if let Some(spec) = args.arg_spec.as_ref() {
48-
49-
let revspec = try!(repo.revparse(spec));
45+
let revspec = repo.revparse(spec)?;
5046

5147
let (oldest, newest) = if revspec.mode().contains(git2::RevparseMode::SINGLE) {
5248
(None, revspec.from())
@@ -66,29 +62,32 @@ fn run(args: &Args) -> Result<(), git2::Error> {
6662
commit_id = format!("{}", commit.id())
6763
}
6864
}
69-
7065
}
7166

7267
let spec = format!("{}:{}", commit_id, path.display());
73-
let blame = try!(repo.blame_file(path, Some(&mut opts)));
74-
let object = try!(repo.revparse_single(&spec[..]));
75-
let blob = try!(repo.find_blob(object.id()));
68+
let blame = repo.blame_file(path, Some(&mut opts))?;
69+
let object = repo.revparse_single(&spec[..])?;
70+
let blob = repo.find_blob(object.id())?;
7671
let reader = BufReader::new(blob.content());
7772

7873
for (i, line) in reader.lines().enumerate() {
79-
if let (Ok(line), Some(hunk)) = (line, blame.get_line(i+1)) {
74+
if let (Ok(line), Some(hunk)) = (line, blame.get_line(i + 1)) {
8075
let sig = hunk.final_signature();
81-
println!("{} {} <{}> {}", hunk.final_commit_id(),
82-
String::from_utf8_lossy(sig.name_bytes()),
83-
String::from_utf8_lossy(sig.email_bytes()), line);
76+
println!(
77+
"{} {} <{}> {}",
78+
hunk.final_commit_id(),
79+
String::from_utf8_lossy(sig.name_bytes()),
80+
String::from_utf8_lossy(sig.email_bytes()),
81+
line
82+
);
8483
}
8584
}
8685

8786
Ok(())
8887
}
8988

9089
fn main() {
91-
const USAGE: &'static str = "
90+
const USAGE: &str = "
9291
usage: blame [options] [<spec>] <path>
9392
9493
Options:
@@ -97,8 +96,9 @@ Options:
9796
-F follow only the first parent commits
9897
";
9998

100-
let args = Docopt::new(USAGE).and_then(|d| d.deserialize())
101-
.unwrap_or_else(|e| e.exit());
99+
let args = Docopt::new(USAGE)
100+
.and_then(|d| d.deserialize())
101+
.unwrap_or_else(|e| e.exit());
102102
match run(&args) {
103103
Ok(()) => {}
104104
Err(e) => println!("error: {}", e),

examples/cat-file.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414

1515
#![deny(warnings)]
1616

17-
extern crate git2;
18-
extern crate docopt;
19-
#[macro_use]
20-
extern crate serde_derive;
21-
2217
use std::io::{self, Write};
2318

2419
use docopt::Docopt;
25-
use git2::{Repository, ObjectType, Blob, Commit, Signature, Tag, Tree};
20+
use git2::{Blob, Commit, ObjectType, Repository, Signature, Tag, Tree};
21+
use serde_derive::Deserialize;
2622

2723
#[derive(Deserialize)]
2824
struct Args {
@@ -38,9 +34,9 @@ struct Args {
3834

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

43-
let obj = try!(repo.revparse_single(&args.arg_object));
39+
let obj = repo.revparse_single(&args.arg_object)?;
4440
if args.flag_v && !args.flag_q {
4541
println!("{} {}\n--", obj.kind().unwrap().str(), obj.id());
4642
}
@@ -63,9 +59,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
6359
Some(ObjectType::Tree) => {
6460
show_tree(obj.as_tree().unwrap());
6561
}
66-
Some(ObjectType::Any) | None => {
67-
println!("unknown {}", obj.id())
68-
}
62+
Some(ObjectType::Any) | None => println!("unknown {}", obj.id()),
6963
}
7064
}
7165
Ok(())
@@ -100,26 +94,41 @@ fn show_tag(tag: &Tag) {
10094

10195
fn show_tree(tree: &Tree) {
10296
for entry in tree.iter() {
103-
println!("{:06o} {} {}\t{}",
104-
entry.filemode(),
105-
entry.kind().unwrap().str(),
106-
entry.id(),
107-
entry.name().unwrap());
97+
println!(
98+
"{:06o} {} {}\t{}",
99+
entry.filemode(),
100+
entry.kind().unwrap().str(),
101+
entry.id(),
102+
entry.name().unwrap()
103+
);
108104
}
109105
}
110106

111107
fn show_sig(header: &str, sig: Option<Signature>) {
112-
let sig = match sig { Some(s) => s, None => return };
108+
let sig = match sig {
109+
Some(s) => s,
110+
None => return,
111+
};
113112
let offset = sig.when().offset_minutes();
114-
let (sign, offset) = if offset < 0 {('-', -offset)} else {('+', offset)};
113+
let (sign, offset) = if offset < 0 {
114+
('-', -offset)
115+
} else {
116+
('+', offset)
117+
};
115118
let (hours, minutes) = (offset / 60, offset % 60);
116-
println!("{} {} {} {}{:02}{:02}",
117-
header, sig, sig.when().seconds(), sign, hours, minutes);
118-
119+
println!(
120+
"{} {} {} {}{:02}{:02}",
121+
header,
122+
sig,
123+
sig.when().seconds(),
124+
sign,
125+
hours,
126+
minutes
127+
);
119128
}
120129

121130
fn main() {
122-
const USAGE: &'static str = "
131+
const USAGE: &str = "
123132
usage: cat-file (-t | -s | -e | -p) [options] <object>
124133
125134
Options:
@@ -133,8 +142,9 @@ Options:
133142
-h, --help show this message
134143
";
135144

136-
let args = Docopt::new(USAGE).and_then(|d| d.deserialize())
137-
.unwrap_or_else(|e| e.exit());
145+
let args = Docopt::new(USAGE)
146+
.and_then(|d| d.deserialize())
147+
.unwrap_or_else(|e| e.exit());
138148
match run(&args) {
139149
Ok(()) => {}
140150
Err(e) => println!("error: {}", e),

examples/clone.rs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414

1515
#![deny(warnings)]
1616

17-
extern crate git2;
18-
extern crate docopt;
19-
#[macro_use]
20-
extern crate serde_derive;
21-
2217
use docopt::Docopt;
23-
use git2::build::{RepoBuilder, CheckoutBuilder};
24-
use git2::{RemoteCallbacks, Progress, FetchOptions};
18+
use git2::build::{CheckoutBuilder, RepoBuilder};
19+
use git2::{FetchOptions, Progress, RemoteCallbacks};
20+
use serde_derive::Deserialize;
2521
use std::cell::RefCell;
2622
use std::io::{self, Write};
2723
use std::path::{Path, PathBuf};
@@ -52,22 +48,34 @@ fn print(state: &mut State) {
5248
let kbytes = stats.received_bytes() / 1024;
5349
if stats.received_objects() == stats.total_objects() {
5450
if !state.newline {
55-
println!("");
51+
println!();
5652
state.newline = true;
5753
}
58-
print!("Resolving deltas {}/{}\r", stats.indexed_deltas(),
59-
stats.total_deltas());
54+
print!(
55+
"Resolving deltas {}/{}\r",
56+
stats.indexed_deltas(),
57+
stats.total_deltas()
58+
);
6059
} else {
61-
print!("net {:3}% ({:4} kb, {:5}/{:5}) / idx {:3}% ({:5}/{:5}) \
62-
/ chk {:3}% ({:4}/{:4}) {}\r",
63-
network_pct, kbytes, stats.received_objects(),
64-
stats.total_objects(),
65-
index_pct, stats.indexed_objects(), stats.total_objects(),
66-
co_pct, state.current, state.total,
67-
state.path
68-
.as_ref()
69-
.map(|s| s.to_string_lossy().into_owned())
70-
.unwrap_or_default())
60+
print!(
61+
"net {:3}% ({:4} kb, {:5}/{:5}) / idx {:3}% ({:5}/{:5}) \
62+
/ chk {:3}% ({:4}/{:4}) {}\r",
63+
network_pct,
64+
kbytes,
65+
stats.received_objects(),
66+
stats.total_objects(),
67+
index_pct,
68+
stats.indexed_objects(),
69+
stats.total_objects(),
70+
co_pct,
71+
state.current,
72+
state.total,
73+
state
74+
.path
75+
.as_ref()
76+
.map(|s| s.to_string_lossy().into_owned())
77+
.unwrap_or_default()
78+
)
7179
}
7280
io::stdout().flush().unwrap();
7381
}
@@ -99,26 +107,28 @@ fn run(args: &Args) -> Result<(), git2::Error> {
99107

100108
let mut fo = FetchOptions::new();
101109
fo.remote_callbacks(cb);
102-
try!(RepoBuilder::new().fetch_options(fo).with_checkout(co)
103-
.clone(&args.arg_url, Path::new(&args.arg_path)));
104-
println!("");
110+
RepoBuilder::new()
111+
.fetch_options(fo)
112+
.with_checkout(co)
113+
.clone(&args.arg_url, Path::new(&args.arg_path))?;
114+
println!();
105115

106116
Ok(())
107117
}
108118

109119
fn main() {
110-
const USAGE: &'static str = "
120+
const USAGE: &str = "
111121
usage: add [options] <url> <path>
112122
113123
Options:
114124
-h, --help show this message
115125
";
116126

117-
let args = Docopt::new(USAGE).and_then(|d| d.deserialize())
118-
.unwrap_or_else(|e| e.exit());
127+
let args = Docopt::new(USAGE)
128+
.and_then(|d| d.deserialize())
129+
.unwrap_or_else(|e| e.exit());
119130
match run(&args) {
120131
Ok(()) => {}
121132
Err(e) => println!("error: {}", e),
122133
}
123134
}
124-

0 commit comments

Comments
 (0)