Skip to content

Add examples #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,41 @@ use crate::{CheckoutNotificationType, DiffFile, Remote};

/// A builder struct which is used to build configuration for cloning a new git
/// repository.
///
/// # Example
///
/// Cloning using SSH:
///
/// ```no_run
/// use git2::{Cred, Error, RemoteCallbacks};
/// use std::env;
/// use std::path::Path;
///
/// // Prepare callbacks.
/// let mut callbacks = RemoteCallbacks::new();
/// callbacks.credentials(|_url, username_from_url, _allowed_types| {
/// Cred::ssh_key(
/// username_from_url.unwrap(),
/// None,
/// std::path::Path::new(&format!("{}/.ssh/id_rsa", env::var("HOME").unwrap())),
/// None,
/// )
/// });
///
/// // Prepare fetch options.
/// let mut fo = git2::FetchOptions::new();
/// fo.remote_callbacks(callbacks);
///
/// // Prepare builder.
/// let mut builder = git2::build::RepoBuilder::new();
/// builder.fetch_options(fo);
///
/// // Clone the project.
/// builder.clone(
/// "[email protected]:rust-lang/git2-rs.git",
/// Path::new("/tmp/git2-rs"),
/// );
/// ```
pub struct RepoBuilder<'cb> {
bare: bool,
branch: Option<CString>,
Expand Down
13 changes: 13 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ impl Index {
/// updated in the index. Returning zero will add the item to the index,
/// greater than zero will skip the item, and less than zero will abort the
/// scan an return an error to the caller.
///
/// # Example
///
/// Emulate `git add *`:
///
/// ```no_run
/// use git2::{Index, IndexAddOption, Repository};
///
/// let repo = Repository::open("/path/to/a/repo").expect("failed to open");
/// let mut index = repo.index().expect("cannot get the Index file");
/// index.add_all(["*"].iter(), IndexAddOption::DEFAULT, None);
/// index.write();
/// ```
pub fn add_all<T, I>(
&mut self,
pathspecs: I,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
//! };
//! ```
//!
//! To clone using SSH, refer to [RepoBuilder](./build/struct.RepoBuilder.html).
//!
//! ## Working with a `Repository`
//!
//! All deriviative objects, references, etc are attached to the lifetime of the
Expand Down
20 changes: 20 additions & 0 deletions src/remote_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ impl<'a> RemoteCallbacks<'a> {
}

/// The callback through which to fetch credentials if required.
///
/// # Example
///
/// Prepare a callback to authenticate using the `$HOME/.ssh/id_rsa` SSH key, and
/// extracting the username from the URL (i.e. [email protected]:rust-lang/git2-rs.git):
///
/// ```no_run
/// use git2::{Cred, RemoteCallbacks};
/// use std::env;
///
/// let mut callbacks = RemoteCallbacks::new();
/// callbacks.credentials(|_url, username_from_url, _allowed_types| {
/// Cred::ssh_key(
/// username_from_url.unwrap(),
/// None,
/// std::path::Path::new(&format!("{}/.ssh/id_rsa", env::var("HOME").unwrap())),
/// None,
/// )
/// });
/// ```
pub fn credentials<F>(&mut self, cb: F) -> &mut RemoteCallbacks<'a>
where
F: FnMut(&str, Option<&str>, CredentialType) -> Result<Cred, Error> + 'a,
Expand Down