]> git.proxmox.com Git - cargo.git/blob - vendor/git2-0.6.8/examples/fetch.rs
New upstream version 0.23.0
[cargo.git] / vendor / git2-0.6.8 / examples / fetch.rs
1 /*
2 * libgit2 "fetch" example - shows how to fetch remote data
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 extern crate git2;
18 extern crate docopt;
19 #[macro_use]
20 extern crate serde_derive;
21
22 use docopt::Docopt;
23 use git2::{Repository, RemoteCallbacks, AutotagOption, FetchOptions};
24 use std::io::{self, Write};
25 use std::str;
26
27 #[derive(Deserialize)]
28 struct Args {
29 arg_remote: Option<String>,
30 }
31
32 fn run(args: &Args) -> Result<(), git2::Error> {
33 let repo = try!(Repository::open("."));
34 let remote = args.arg_remote.as_ref().map(|s| &s[..]).unwrap_or("origin");
35
36 // Figure out whether it's a named remote or a URL
37 println!("Fetching {} for repo", remote);
38 let mut cb = RemoteCallbacks::new();
39 let mut remote = try!(repo.find_remote(remote).or_else(|_| {
40 repo.remote_anonymous(remote)
41 }));
42 cb.sideband_progress(|data| {
43 print!("remote: {}", str::from_utf8(data).unwrap());
44 io::stdout().flush().unwrap();
45 true
46 });
47
48 // This callback gets called for each remote-tracking branch that gets
49 // updated. The message we output depends on whether it's a new one or an
50 // update.
51 cb.update_tips(|refname, a, b| {
52 if a.is_zero() {
53 println!("[new] {:20} {}", b, refname);
54 } else {
55 println!("[updated] {:10}..{:10} {}", a, b, refname);
56 }
57 true
58 });
59
60 // Here we show processed and total objects in the pack and the amount of
61 // received data. Most frontends will probably want to show a percentage and
62 // the download rate.
63 cb.transfer_progress(|stats| {
64 if stats.received_objects() == stats.total_objects() {
65 print!("Resolving deltas {}/{}\r", stats.indexed_deltas(),
66 stats.total_deltas());
67 } else if stats.total_objects() > 0 {
68 print!("Received {}/{} objects ({}) in {} bytes\r",
69 stats.received_objects(),
70 stats.total_objects(),
71 stats.indexed_objects(),
72 stats.received_bytes());
73 }
74 io::stdout().flush().unwrap();
75 true
76 });
77
78 // Download the packfile and index it. This function updates the amount of
79 // received data and the indexer stats which lets you inform the user about
80 // progress.
81 let mut fo = FetchOptions::new();
82 fo.remote_callbacks(cb);
83 try!(remote.download(&[], Some(&mut fo)));
84
85 {
86 // If there are local objects (we got a thin pack), then tell the user
87 // how many objects we saved from having to cross the network.
88 let stats = remote.stats();
89 if stats.local_objects() > 0 {
90 println!("\rReceived {}/{} objects in {} bytes (used {} local \
91 objects)", stats.indexed_objects(),
92 stats.total_objects(), stats.received_bytes(),
93 stats.local_objects());
94 } else {
95 println!("\rReceived {}/{} objects in {} bytes",
96 stats.indexed_objects(), stats.total_objects(),
97 stats.received_bytes());
98 }
99 }
100
101 // Disconnect the underlying connection to prevent from idling.
102 remote.disconnect();
103
104 // Update the references in the remote's namespace to point to the right
105 // commits. This may be needed even if there was no packfile to download,
106 // which can happen e.g. when the branches have been changed but all the
107 // needed objects are available locally.
108 try!(remote.update_tips(None, true,
109 AutotagOption::Unspecified, None));
110
111 Ok(())
112 }
113
114 fn main() {
115 const USAGE: &'static str = "
116 usage: fetch [options] [<remote>]
117
118 Options:
119 -h, --help show this message
120 ";
121
122 let args = Docopt::new(USAGE).and_then(|d| d.deserialize())
123 .unwrap_or_else(|e| e.exit());
124 match run(&args) {
125 Ok(()) => {}
126 Err(e) => println!("error: {}", e),
127 }
128 }