2 * libgit2 "fetch" example - shows how to fetch remote data
4 * Written by the libgit2 contributors
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.
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/>.
20 extern crate serde_derive
;
23 use git2
::{Repository, RemoteCallbacks, AutotagOption, FetchOptions}
;
24 use std
::io
::{self, Write}
;
27 #[derive(Deserialize)]
29 arg_remote
: Option
<String
>,
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");
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
)
42 cb
.sideband_progress(|data
| {
43 print
!("remote: {}", str::from_utf8(data
).unwrap());
44 io
::stdout().flush().unwrap();
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
51 cb
.update_tips(|refname
, a
, b
| {
53 println
!("[new] {:20} {}", b
, refname
);
55 println
!("[updated] {:10}..{:10} {}", a
, b
, refname
);
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
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());
74 io
::stdout().flush().unwrap();
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
81 let mut fo
= FetchOptions
::new();
82 fo
.remote_callbacks(cb
);
83 try
!(remote
.download(&[], Some(&mut fo
)));
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());
95 println
!("\rReceived {}/{} objects in {} bytes",
96 stats
.indexed_objects(), stats
.total_objects(),
97 stats
.received_bytes());
101 // Disconnect the underlying connection to prevent from idling.
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
));
115 const USAGE
: &'
static str = "
116 usage: fetch [options] [<remote>]
119 -h, --help show this message
122 let args
= Docopt
::new(USAGE
).and_then(|d
| d
.deserialize())
123 .unwrap_or_else(|e
| e
.exit());
126 Err(e
) => println
!("error: {}", e
),