]> git.proxmox.com Git - cargo.git/blob - vendor/git2-0.6.8/examples/add.rs
New upstream version 0.23.0
[cargo.git] / vendor / git2-0.6.8 / examples / add.rs
1 /*
2 * libgit2 "add" example - shows how to modify the index
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 #![allow(trivial_casts)]
17
18 extern crate git2;
19 extern crate docopt;
20 #[macro_use]
21 extern crate serde_derive;
22
23 use std::path::Path;
24 use docopt::Docopt;
25 use git2::Repository;
26
27 #[derive(Deserialize)]
28 struct Args {
29 arg_spec: Vec<String>,
30 flag_dry_run: bool,
31 flag_verbose: bool,
32 flag_update: bool,
33 }
34
35 fn run(args: &Args) -> Result<(), git2::Error> {
36 let repo = try!(Repository::open(&Path::new(".")));
37 let mut index = try!(repo.index());
38
39 let cb = &mut |path: &Path, _matched_spec: &[u8]| -> i32 {
40 let status = repo.status_file(path).unwrap();
41
42 let ret = if status.contains(git2::STATUS_WT_MODIFIED) ||
43 status.contains(git2::STATUS_WT_NEW) {
44 println!("add '{}'", path.display());
45 0
46 } else {
47 1
48 };
49
50 if args.flag_dry_run {1} else {ret}
51 };
52 let cb = if args.flag_verbose || args.flag_update {
53 Some(cb as &mut git2::IndexMatchedPath)
54 } else {
55 None
56 };
57
58 if args.flag_update {
59 try!(index.update_all(args.arg_spec.iter(), cb));
60 } else {
61 try!(index.add_all(args.arg_spec.iter(), git2::ADD_DEFAULT, cb));
62 }
63
64 try!(index.write());
65 Ok(())
66 }
67
68 fn main() {
69 const USAGE: &'static str = "
70 usage: add [options] [--] [<spec>..]
71
72 Options:
73 -n, --dry-run dry run
74 -v, --verbose be verbose
75 -u, --update update tracked files
76 -h, --help show this message
77 ";
78
79 let args = Docopt::new(USAGE).and_then(|d| d.deserialize())
80 .unwrap_or_else(|e| e.exit());
81 match run(&args) {
82 Ok(()) => {}
83 Err(e) => println!("error: {}", e),
84 }
85 }