diff --git a/libgit2-sys/lib.rs b/libgit2-sys/lib.rs index e39584f16f..e3f1fb51aa 100644 --- a/libgit2-sys/lib.rs +++ b/libgit2-sys/lib.rs @@ -867,6 +867,8 @@ git_enum! { GIT_REPOSITORY_OPEN_NO_SEARCH = (1 << 0), GIT_REPOSITORY_OPEN_CROSS_FS = (1 << 1), GIT_REPOSITORY_OPEN_BARE = (1 << 2), + GIT_REPOSITORY_OPEN_NO_DOTGIT = (1 << 3), + GIT_REPOSITORY_OPEN_FROM_ENV = (1 << 4), } } diff --git a/src/lib.rs b/src/lib.rs index 2cdebf9551..7d9cbd69a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -461,6 +461,10 @@ bitflags! { const REPOSITORY_OPEN_CROSS_FS = raw::GIT_REPOSITORY_OPEN_CROSS_FS as u32, /// Force opening as bare repository, and defer loading its config. const REPOSITORY_OPEN_BARE = raw::GIT_REPOSITORY_OPEN_BARE as u32, + /// Don't try appending `/.git` to the specified repository path. + const REPOSITORY_OPEN_NO_DOTGIT = raw::GIT_REPOSITORY_OPEN_NO_DOTGIT as u32, + /// Respect environment variables like `$GIT_DIR`. + const REPOSITORY_OPEN_FROM_ENV = raw::GIT_REPOSITORY_OPEN_FROM_ENV as u32, } } diff --git a/src/repo.rs b/src/repo.rs index 10eb92a71e..64cc71e02a 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -60,6 +60,23 @@ impl Repository { } } + /// Find and open an existing repository, respecting git environment + /// variables. This acts like `open_ext` with the + /// `REPOSITORY_OPEN_FROM_ENV` flag, but additionally respects `$GIT_DIR`. + /// With `$GIT_DIR` unset, this will search for a repository starting in + /// the current directory. + pub fn open_from_env() -> Result { + init(); + let mut ret = 0 as *mut raw::git_repository; + unsafe { + try_call!(raw::git_repository_open_ext(&mut ret, + 0 as *const _, + raw::GIT_REPOSITORY_OPEN_FROM_ENV, + 0 as *const _)); + Ok(Binding::from_raw(ret)) + } + } + /// Find and open an existing repository, with additional options. /// /// If flags contains REPOSITORY_OPEN_NO_SEARCH, the path must point @@ -75,6 +92,14 @@ impl Repository { /// bare even if it isn't, ignoring any working directory, and defer /// loading the repository configuration for performance. /// + /// If flags contains REPOSITORY_OPEN_NO_DOTGIT, don't try appending + /// `/.git` to `path`. + /// + /// If flags contains REPOSITORY_OPEN_FROM_ENV, `open_ext` will ignore + /// other flags and `ceiling_dirs`, and respect the same environment + /// variables git does. Note, however, that `path` overrides `$GIT_DIR`; to + /// respect `$GIT_DIR` as well, use `open_from_env`. + /// /// ceiling_dirs specifies a list of paths that the search through parent /// directories will stop before entering. Use the functions in std::env /// to construct or manipulate such a path list.