Skip to content

Commit 147c5b4

Browse files
authored
Add examples (#519)
While starting to use this library, I spent way too much time trying to perform the 3 following tasks, therefore I thought I would contribute some examples to the documentation. The examples should help to: * Clone/push using SSH * Stage all files
1 parent 43b8e28 commit 147c5b4

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/build.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,41 @@ use crate::{CheckoutNotificationType, DiffFile, Remote};
1212

1313
/// A builder struct which is used to build configuration for cloning a new git
1414
/// repository.
15+
///
16+
/// # Example
17+
///
18+
/// Cloning using SSH:
19+
///
20+
/// ```no_run
21+
/// use git2::{Cred, Error, RemoteCallbacks};
22+
/// use std::env;
23+
/// use std::path::Path;
24+
///
25+
/// // Prepare callbacks.
26+
/// let mut callbacks = RemoteCallbacks::new();
27+
/// callbacks.credentials(|_url, username_from_url, _allowed_types| {
28+
/// Cred::ssh_key(
29+
/// username_from_url.unwrap(),
30+
/// None,
31+
/// std::path::Path::new(&format!("{}/.ssh/id_rsa", env::var("HOME").unwrap())),
32+
/// None,
33+
/// )
34+
/// });
35+
///
36+
/// // Prepare fetch options.
37+
/// let mut fo = git2::FetchOptions::new();
38+
/// fo.remote_callbacks(callbacks);
39+
///
40+
/// // Prepare builder.
41+
/// let mut builder = git2::build::RepoBuilder::new();
42+
/// builder.fetch_options(fo);
43+
///
44+
/// // Clone the project.
45+
/// builder.clone(
46+
/// "[email protected]:rust-lang/git2-rs.git",
47+
/// Path::new("/tmp/git2-rs"),
48+
/// );
49+
/// ```
1550
pub struct RepoBuilder<'cb> {
1651
bare: bool,
1752
branch: Option<CString>,

src/index.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ impl Index {
268268
/// updated in the index. Returning zero will add the item to the index,
269269
/// greater than zero will skip the item, and less than zero will abort the
270270
/// scan an return an error to the caller.
271+
///
272+
/// # Example
273+
///
274+
/// Emulate `git add *`:
275+
///
276+
/// ```no_run
277+
/// use git2::{Index, IndexAddOption, Repository};
278+
///
279+
/// let repo = Repository::open("/path/to/a/repo").expect("failed to open");
280+
/// let mut index = repo.index().expect("cannot get the Index file");
281+
/// index.add_all(["*"].iter(), IndexAddOption::DEFAULT, None);
282+
/// index.write();
283+
/// ```
271284
pub fn add_all<T, I>(
272285
&mut self,
273286
pathspecs: I,

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
//! };
5858
//! ```
5959
//!
60+
//! To clone using SSH, refer to [RepoBuilder](./build/struct.RepoBuilder.html).
61+
//!
6062
//! ## Working with a `Repository`
6163
//!
6264
//! All deriviative objects, references, etc are attached to the lifetime of the

src/remote_callbacks.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ impl<'a> RemoteCallbacks<'a> {
7676
}
7777

7878
/// The callback through which to fetch credentials if required.
79+
///
80+
/// # Example
81+
///
82+
/// Prepare a callback to authenticate using the `$HOME/.ssh/id_rsa` SSH key, and
83+
/// extracting the username from the URL (i.e. [email protected]:rust-lang/git2-rs.git):
84+
///
85+
/// ```no_run
86+
/// use git2::{Cred, RemoteCallbacks};
87+
/// use std::env;
88+
///
89+
/// let mut callbacks = RemoteCallbacks::new();
90+
/// callbacks.credentials(|_url, username_from_url, _allowed_types| {
91+
/// Cred::ssh_key(
92+
/// username_from_url.unwrap(),
93+
/// None,
94+
/// std::path::Path::new(&format!("{}/.ssh/id_rsa", env::var("HOME").unwrap())),
95+
/// None,
96+
/// )
97+
/// });
98+
/// ```
7999
pub fn credentials<F>(&mut self, cb: F) -> &mut RemoteCallbacks<'a>
80100
where
81101
F: FnMut(&str, Option<&str>, CredentialType) -> Result<Cred, Error> + 'a,

0 commit comments

Comments
 (0)