| 1 |
use libc::{c_char, c_int, c_uint, c_void, size_t}; |
| 2 |
use std::env; |
| 3 |
use std::ffi::{CStr, CString, OsStr}; |
| 4 |
use std::mem; |
| 5 |
use std::path::{Path, PathBuf}; |
| 6 |
use std::ptr; |
| 7 |
use std::str; |
| 8 |
|
| 9 |
use crate::build::{CheckoutBuilder, RepoBuilder}; |
| 10 |
use crate::diff::{ |
| 11 |
binary_cb_c, file_cb_c, hunk_cb_c, line_cb_c, BinaryCb, DiffCallbacks, FileCb, HunkCb, LineCb, |
| 12 |
}; |
| 13 |
use crate::oid_array::OidArray; |
| 14 |
use crate::stash::{stash_cb, StashApplyOptions, StashCbData, StashSaveOptions}; |
| 15 |
use crate::string_array::StringArray; |
| 16 |
use crate::tagforeach::{tag_foreach_cb, TagForeachCB, TagForeachData}; |
| 17 |
use crate::util::{self, path_to_repo_path, Binding}; |
| 18 |
use crate::worktree::{Worktree, WorktreeAddOptions}; |
| 19 |
use crate::CherrypickOptions; |
| 20 |
use crate::ObjectFormat; |
| 21 |
use crate::RevertOptions; |
| 22 |
use crate::{mailmap::Mailmap, panic}; |
| 23 |
use crate::{ |
| 24 |
raw, AttrCheckFlags, Buf, Error, Object, Remote, RepositoryOpenFlags, RepositoryState, Revspec, |
| 25 |
StashFlags, |
| 26 |
}; |
| 27 |
use crate::{ |
| 28 |
AnnotatedCommit, MergeAnalysis, MergeFileOptions, MergeFileResult, MergeOptions, |
| 29 |
MergePreference, SubmoduleIgnore, SubmoduleStatus, SubmoduleUpdate, |
| 30 |
}; |
| 31 |
use crate::{ApplyLocation, ApplyOptions, Rebase, RebaseOptions}; |
| 32 |
use crate::{Blame, BlameOptions, Reference, References, ResetType, Signature, Submodule}; |
| 33 |
use crate::{ |
| 34 |
Blob, BlobWriter, Branch, BranchType, Branches, Commit, Config, Index, IndexEntry, Oid, Tree, |
| 35 |
}; |
| 36 |
use crate::{Describe, IntoCString, Reflog, RepositoryInitMode, RevparseMode}; |
| 37 |
use crate::{DescribeOptions, Diff, DiffOptions, Odb, PackBuilder, Refdb, TreeBuilder}; |
| 38 |
use crate::{Note, Notes, ObjectType, Revwalk, Status, StatusOptions, Statuses, Tag, Transaction}; |
| 39 |
|
| 40 |
type MergeheadForeachCb<'a> = dyn FnMut(&Oid) -> bool + 'a; |
| 41 |
type FetchheadForeachCb<'a> = dyn FnMut(&str, &[u8], &Oid, bool) -> bool + 'a; |
| 42 |
|
| 43 |
struct FetchheadForeachCbData<'a> { |
| 44 |
callback: &'a mut FetchheadForeachCb<'a>, |
| 45 |
} |
| 46 |
|
| 47 |
struct MergeheadForeachCbData<'a> { |
| 48 |
callback: &'a mut MergeheadForeachCb<'a>, |
| 49 |
} |
| 50 |
|
| 51 |
extern "C" fn mergehead_foreach_cb(oid: *const raw::git_oid, payload: *mut c_void) -> c_int { |
| 52 |
panic::wrap(|| unsafe { |
| 53 |
let data = &mut *(payload as *mut MergeheadForeachCbData<'_>); |
| 54 |
let res = { |
| 55 |
let callback = &mut data.callback; |
| 56 |
callback(&Binding::from_raw(oid)) |
| 57 |
}; |
| 58 |
|
| 59 |
if res { |
| 60 |
0 |
| 61 |
} else { |
| 62 |
1 |
| 63 |
} |
| 64 |
}) |
| 65 |
.unwrap_or(1) |
| 66 |
} |
| 67 |
|
| 68 |
extern "C" fn fetchhead_foreach_cb( |
| 69 |
ref_name: *const c_char, |
| 70 |
remote_url: *const c_char, |
| 71 |
oid: *const raw::git_oid, |
| 72 |
is_merge: c_uint, |
| 73 |
payload: *mut c_void, |
| 74 |
) -> c_int { |
| 75 |
panic::wrap(|| unsafe { |
| 76 |
let data = &mut *(payload as *mut FetchheadForeachCbData<'_>); |
| 77 |
let res = { |
| 78 |
let callback = &mut data.callback; |
| 79 |
|
| 80 |
assert!(!ref_name.is_null()); |
| 81 |
assert!(!remote_url.is_null()); |
| 82 |
assert!(!oid.is_null()); |
| 83 |
|
| 84 |
let ref_name = str::from_utf8(CStr::from_ptr(ref_name).to_bytes()).unwrap(); |
| 85 |
let remote_url = CStr::from_ptr(remote_url).to_bytes(); |
| 86 |
let oid = Binding::from_raw(oid); |
| 87 |
let is_merge = is_merge == 1; |
| 88 |
|
| 89 |
callback(&ref_name, remote_url, &oid, is_merge) |
| 90 |
}; |
| 91 |
|
| 92 |
if res { |
| 93 |
0 |
| 94 |
} else { |
| 95 |
1 |
| 96 |
} |
| 97 |
}) |
| 98 |
.unwrap_or(1) |
| 99 |
} |
| 100 |
|
| 101 |
/// An owned git repository, representing all state associated with the |
| 102 |
/// underlying filesystem. |
| 103 |
/// |
| 104 |
/// This structure corresponds to a `git_repository` in libgit2. Many other |
| 105 |
/// types in git2-rs are derivative from this structure and are attached to its |
| 106 |
/// lifetime. |
| 107 |
/// |
| 108 |
/// When a repository goes out of scope it is freed in memory but not deleted |
| 109 |
/// from the filesystem. |
| 110 |
pub struct Repository { |
| 111 |
raw: *mut raw::git_repository, |
| 112 |
} |
| 113 |
|
| 114 |
// It is the current belief that a `Repository` can be sent among threads, or |
| 115 |
// even shared among threads in a mutex. |
| 116 |
unsafe impl Send for Repository {} |
| 117 |
|
| 118 |
/// Options which can be used to configure how a repository is initialized |
| 119 |
pub struct RepositoryInitOptions { |
| 120 |
flags: u32, |
| 121 |
mode: u32, |
| 122 |
workdir_path: Option<CString>, |
| 123 |
description: Option<CString>, |
| 124 |
template_path: Option<CString>, |
| 125 |
initial_head: Option<CString>, |
| 126 |
origin_url: Option<CString>, |
| 127 |
oid_type: Option<raw::git_oid_t>, |
| 128 |
} |
| 129 |
|
| 130 |
impl Repository { |
| 131 |
/// Attempt to open an already-existing repository at `path`. |
| 132 |
/// |
| 133 |
/// The path can point to either a normal or bare repository. |
| 134 |
pub fn open<P: AsRef<Path>>(path: P) -> Result<Repository, Error> { |
| 135 |
crate::init(); |
| 136 |
// Normal file path OK (does not need Windows conversion). |
| 137 |
let path = path.as_ref().into_c_string()?; |
| 138 |
let mut ret = ptr::null_mut(); |
| 139 |
unsafe { |
| 140 |
try_call!(raw::git_repository_open(&mut ret, path)); |
| 141 |
Ok(Binding::from_raw(ret)) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/// Attempt to open an already-existing bare repository at `path`. |
| 146 |
/// |
| 147 |
/// The path can point to only a bare repository. |
| 148 |
pub fn open_bare<P: AsRef<Path>>(path: P) -> Result<Repository, Error> { |
| 149 |
crate::init(); |
| 150 |
// Normal file path OK (does not need Windows conversion). |
| 151 |
let path = path.as_ref().into_c_string()?; |
| 152 |
let mut ret = ptr::null_mut(); |
| 153 |
unsafe { |
| 154 |
try_call!(raw::git_repository_open_bare(&mut ret, path)); |
| 155 |
Ok(Binding::from_raw(ret)) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/// Find and open an existing repository, respecting git environment |
| 160 |
/// variables. This acts like `open_ext` with the |
| 161 |
/// [FROM_ENV](RepositoryOpenFlags::FROM_ENV) flag, but additionally respects `$GIT_DIR`. |
| 162 |
/// With `$GIT_DIR` unset, this will search for a repository starting in |
| 163 |
/// the current directory. |
| 164 |
pub fn open_from_env() -> Result<Repository, Error> { |
| 165 |
crate::init(); |
| 166 |
let mut ret = ptr::null_mut(); |
| 167 |
let flags = raw::GIT_REPOSITORY_OPEN_FROM_ENV; |
| 168 |
unsafe { |
| 169 |
try_call!(raw::git_repository_open_ext( |
| 170 |
&mut ret, |
| 171 |
ptr::null(), |
| 172 |
flags as c_uint, |
| 173 |
ptr::null() |
| 174 |
)); |
| 175 |
Ok(Binding::from_raw(ret)) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/// Find and open an existing repository, with additional options. |
| 180 |
/// |
| 181 |
/// If flags contains [NO_SEARCH](RepositoryOpenFlags::NO_SEARCH), the path must point |
| 182 |
/// directly to a repository; otherwise, this may point to a subdirectory |
| 183 |
/// of a repository, and `open_ext` will search up through parent |
| 184 |
/// directories. |
| 185 |
/// |
| 186 |
/// If flags contains [CROSS_FS](RepositoryOpenFlags::CROSS_FS), the search through parent |
| 187 |
/// directories will not cross a filesystem boundary (detected when the |
| 188 |
/// stat st_dev field changes). |
| 189 |
/// |
| 190 |
/// If flags contains [BARE](RepositoryOpenFlags::BARE), force opening the repository as |
| 191 |
/// bare even if it isn't, ignoring any working directory, and defer |
| 192 |
/// loading the repository configuration for performance. |
| 193 |
/// |
| 194 |
/// If flags contains [NO_DOTGIT](RepositoryOpenFlags::NO_DOTGIT), don't try appending |
| 195 |
/// `/.git` to `path`. |
| 196 |
/// |
| 197 |
/// If flags contains [FROM_ENV](RepositoryOpenFlags::FROM_ENV), `open_ext` will ignore |
| 198 |
/// other flags and `ceiling_dirs`, and respect the same environment |
| 199 |
/// variables git does. Note, however, that `path` overrides `$GIT_DIR`; to |
| 200 |
/// respect `$GIT_DIR` as well, use `open_from_env`. |
| 201 |
/// |
| 202 |
/// ceiling_dirs specifies a list of paths that the search through parent |
| 203 |
/// directories will stop before entering. Use the functions in std::env |
| 204 |
/// to construct or manipulate such a path list. (You can use `&[] as |
| 205 |
/// &[&std::ffi::OsStr]` as an argument if there are no ceiling |
| 206 |
/// directories.) |
| 207 |
pub fn open_ext<P, O, I>( |
| 208 |
path: P, |
| 209 |
flags: RepositoryOpenFlags, |
| 210 |
ceiling_dirs: I, |
| 211 |
) -> Result<Repository, Error> |
| 212 |
where |
| 213 |
P: AsRef<Path>, |
| 214 |
O: AsRef<OsStr>, |
| 215 |
I: IntoIterator<Item = O>, |
| 216 |
{ |
| 217 |
crate::init(); |
| 218 |
// Normal file path OK (does not need Windows conversion). |
| 219 |
let path = path.as_ref().into_c_string()?; |
| 220 |
let ceiling_dirs_os = env::join_paths(ceiling_dirs)?; |
| 221 |
let ceiling_dirs = ceiling_dirs_os.into_c_string()?; |
| 222 |
let mut ret = ptr::null_mut(); |
| 223 |
unsafe { |
| 224 |
try_call!(raw::git_repository_open_ext( |
| 225 |
&mut ret, |
| 226 |
path, |
| 227 |
flags.bits() as c_uint, |
| 228 |
ceiling_dirs |
| 229 |
)); |
| 230 |
Ok(Binding::from_raw(ret)) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/// Attempt to open an already-existing repository from a worktree. |
| 235 |
pub fn open_from_worktree(worktree: &Worktree) -> Result<Repository, Error> { |
| 236 |
let mut ret = ptr::null_mut(); |
| 237 |
unsafe { |
| 238 |
try_call!(raw::git_repository_open_from_worktree( |
| 239 |
&mut ret, |
| 240 |
worktree.raw() |
| 241 |
)); |
| 242 |
Ok(Binding::from_raw(ret)) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/// Attempt to open an already-existing repository at or above `path` |
| 247 |
/// |
| 248 |
/// This starts at `path` and looks up the filesystem hierarchy |
| 249 |
/// until it finds a repository. |
| 250 |
pub fn discover<P: AsRef<Path>>(path: P) -> Result<Repository, Error> { |
| 251 |
// TODO: this diverges significantly from the libgit2 API |
| 252 |
crate::init(); |
| 253 |
let buf = Buf::new(); |
| 254 |
// Normal file path OK (does not need Windows conversion). |
| 255 |
let path = path.as_ref().into_c_string()?; |
| 256 |
unsafe { |
| 257 |
try_call!(raw::git_repository_discover( |
| 258 |
buf.raw(), |
| 259 |
path, |
| 260 |
1, |
| 261 |
ptr::null() |
| 262 |
)); |
| 263 |
} |
| 264 |
Repository::open(util::bytes2path(&*buf)) |
| 265 |
} |
| 266 |
|
| 267 |
/// Attempt to find the path to a git repo for a given path |
| 268 |
/// |
| 269 |
/// This starts at `path` and looks up the filesystem hierarchy |
| 270 |
/// until it finds a repository, stopping if it finds a member of ceiling_dirs |
| 271 |
pub fn discover_path<P: AsRef<Path>, I, O>(path: P, ceiling_dirs: I) -> Result<PathBuf, Error> |
| 272 |
where |
| 273 |
O: AsRef<OsStr>, |
| 274 |
I: IntoIterator<Item = O>, |
| 275 |
{ |
| 276 |
crate::init(); |
| 277 |
let buf = Buf::new(); |
| 278 |
// Normal file path OK (does not need Windows conversion). |
| 279 |
let path = path.as_ref().into_c_string()?; |
| 280 |
let ceiling_dirs_os = env::join_paths(ceiling_dirs)?; |
| 281 |
let ceiling_dirs = ceiling_dirs_os.into_c_string()?; |
| 282 |
unsafe { |
| 283 |
try_call!(raw::git_repository_discover( |
| 284 |
buf.raw(), |
| 285 |
path, |
| 286 |
1, |
| 287 |
ceiling_dirs |
| 288 |
)); |
| 289 |
} |
| 290 |
|
| 291 |
Ok(util::bytes2path(&*buf).to_path_buf()) |
| 292 |
} |
| 293 |
|
| 294 |
/// Creates a new repository in the specified folder. |
| 295 |
/// |
| 296 |
/// This by default will create any necessary directories to create the |
| 297 |
/// repository, and it will read any user-specified templates when creating |
| 298 |
/// the repository. This behavior can be configured through `init_opts`. |
| 299 |
pub fn init<P: AsRef<Path>>(path: P) -> Result<Repository, Error> { |
| 300 |
Repository::init_opts(path, &RepositoryInitOptions::new()) |
| 301 |
} |
| 302 |
|
| 303 |
/// Creates a new `--bare` repository in the specified folder. |
| 304 |
/// |
| 305 |
/// The folder must exist prior to invoking this function. |
| 306 |
pub fn init_bare<P: AsRef<Path>>(path: P) -> Result<Repository, Error> { |
| 307 |
Repository::init_opts(path, RepositoryInitOptions::new().bare(true)) |
| 308 |
} |
| 309 |
|
| 310 |
/// Creates a new repository in the specified folder with the given options. |
| 311 |
/// |
| 312 |
/// See `RepositoryInitOptions` struct for more information. |
| 313 |
pub fn init_opts<P: AsRef<Path>>( |
| 314 |
path: P, |
| 315 |
opts: &RepositoryInitOptions, |
| 316 |
) -> Result<Repository, Error> { |
| 317 |
crate::init(); |
| 318 |
// Normal file path OK (does not need Windows conversion). |
| 319 |
let path = path.as_ref().into_c_string()?; |
| 320 |
let mut ret = ptr::null_mut(); |
| 321 |
unsafe { |
| 322 |
let mut opts = opts.raw(); |
| 323 |
try_call!(raw::git_repository_init_ext(&mut ret, path, &mut opts)); |
| 324 |
Ok(Binding::from_raw(ret)) |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
/// Clone a remote repository. |
| 329 |
/// |
| 330 |
/// See the `RepoBuilder` struct for more information. This function will |
| 331 |
/// delegate to a fresh `RepoBuilder` |
| 332 |
pub fn clone<P: AsRef<Path>>(url: &str, into: P) -> Result<Repository, Error> { |
| 333 |
crate::init(); |
| 334 |
RepoBuilder::new().clone(url, into.as_ref()) |
| 335 |
} |
| 336 |
|
| 337 |
/// Clone a remote repository, initialize and update its submodules |
| 338 |
/// recursively. |
| 339 |
/// |
| 340 |
/// This is similar to `git clone --recursive`. |
| 341 |
pub fn clone_recurse<P: AsRef<Path>>(url: &str, into: P) -> Result<Repository, Error> { |
| 342 |
let repo = Repository::clone(url, into)?; |
| 343 |
repo.update_submodules()?; |
| 344 |
Ok(repo) |
| 345 |
} |
| 346 |
|
| 347 |
/// Attempt to wrap an object database as a repository. |
| 348 |
pub fn from_odb(odb: Odb<'_>) -> Result<Repository, Error> { |
| 349 |
crate::init(); |
| 350 |
let mut ret = ptr::null_mut(); |
| 351 |
unsafe { |
| 352 |
try_call!(raw::git_repository_wrap_odb(&mut ret, odb.raw())); |
| 353 |
Ok(Binding::from_raw(ret)) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/// Update submodules recursively. |
| 358 |
/// |
| 359 |
/// Uninitialized submodules will be initialized. |
| 360 |
fn update_submodules(&self) -> Result<(), Error> { |
| 361 |
fn add_subrepos(repo: &Repository, list: &mut Vec<Repository>) -> Result<(), Error> { |
| 362 |
for mut subm in repo.submodules()? { |
| 363 |
subm.update(true, None)?; |
| 364 |
list.push(subm.open()?); |
| 365 |
} |
| 366 |
Ok(()) |
| 367 |
} |
| 368 |
|
| 369 |
let mut repos = Vec::new(); |
| 370 |
add_subrepos(self, &mut repos)?; |
| 371 |
while let Some(repo) = repos.pop() { |
| 372 |
add_subrepos(&repo, &mut repos)?; |
| 373 |
} |
| 374 |
Ok(()) |
| 375 |
} |
| 376 |
|
| 377 |
/// Execute a rev-parse operation against the `spec` listed. |
| 378 |
/// |
| 379 |
/// The resulting revision specification is returned, or an error is |
| 380 |
/// returned if one occurs. |
| 381 |
pub fn revparse(&self, spec: &str) -> Result<Revspec<'_>, Error> { |
| 382 |
let mut raw = raw::git_revspec { |
| 383 |
from: ptr::null_mut(), |
| 384 |
to: ptr::null_mut(), |
| 385 |
flags: 0, |
| 386 |
}; |
| 387 |
let spec = CString::new(spec)?; |
| 388 |
unsafe { |
| 389 |
try_call!(raw::git_revparse(&mut raw, self.raw, spec)); |
| 390 |
let to = Binding::from_raw_opt(raw.to); |
| 391 |
let from = Binding::from_raw_opt(raw.from); |
| 392 |
let mode = RevparseMode::from_bits_truncate(raw.flags as u32); |
| 393 |
Ok(Revspec::from_objects(from, to, mode)) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/// Find a single object, as specified by a revision string. |
| 398 |
pub fn revparse_single(&self, spec: &str) -> Result<Object<'_>, Error> { |
| 399 |
let spec = CString::new(spec)?; |
| 400 |
let mut obj = ptr::null_mut(); |
| 401 |
unsafe { |
| 402 |
try_call!(raw::git_revparse_single(&mut obj, self.raw, spec)); |
| 403 |
assert!(!obj.is_null()); |
| 404 |
Ok(Binding::from_raw(obj)) |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/// Find a single object and intermediate reference by a revision string. |
| 409 |
/// |
| 410 |
/// See `man gitrevisions`, or |
| 411 |
/// <http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions> for |
| 412 |
/// information on the syntax accepted. |
| 413 |
/// |
| 414 |
/// In some cases (`@{<-n>}` or `<branchname>@{upstream}`), the expression |
| 415 |
/// may point to an intermediate reference. When such expressions are being |
| 416 |
/// passed in, this intermediate reference is returned. |
| 417 |
pub fn revparse_ext(&self, spec: &str) -> Result<(Object<'_>, Option<Reference<'_>>), Error> { |
| 418 |
let spec = CString::new(spec)?; |
| 419 |
let mut git_obj = ptr::null_mut(); |
| 420 |
let mut git_ref = ptr::null_mut(); |
| 421 |
unsafe { |
| 422 |
try_call!(raw::git_revparse_ext( |
| 423 |
&mut git_obj, |
| 424 |
&mut git_ref, |
| 425 |
self.raw, |
| 426 |
spec |
| 427 |
)); |
| 428 |
assert!(!git_obj.is_null()); |
| 429 |
Ok((Binding::from_raw(git_obj), Binding::from_raw_opt(git_ref))) |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
/// Tests whether this repository is a bare repository or not. |
| 434 |
pub fn is_bare(&self) -> bool { |
| 435 |
unsafe { raw::git_repository_is_bare(self.raw) == 1 } |
| 436 |
} |
| 437 |
|
| 438 |
/// Tests whether this repository is a shallow clone. |
| 439 |
pub fn is_shallow(&self) -> bool { |
| 440 |
unsafe { raw::git_repository_is_shallow(self.raw) == 1 } |
| 441 |
} |
| 442 |
|
| 443 |
/// Tests whether this repository is a worktree. |
| 444 |
pub fn is_worktree(&self) -> bool { |
| 445 |
unsafe { raw::git_repository_is_worktree(self.raw) == 1 } |
| 446 |
} |
| 447 |
|
| 448 |
/// Tests whether this repository is empty. |
| 449 |
pub fn is_empty(&self) -> Result<bool, Error> { |
| 450 |
let empty = unsafe { try_call!(raw::git_repository_is_empty(self.raw)) }; |
| 451 |
Ok(empty == 1) |
| 452 |
} |
| 453 |
|
| 454 |
/// Returns the path to the `.git` folder for normal repositories or the |
| 455 |
/// repository itself for bare repositories. |
| 456 |
pub fn path(&self) -> &Path { |
| 457 |
unsafe { |
| 458 |
let ptr = raw::git_repository_path(self.raw); |
| 459 |
util::bytes2path(crate::opt_bytes(self, ptr).unwrap()) |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
/// Returns the object format (hash algorithm) of this repository. |
| 464 |
pub fn object_format(&self) -> ObjectFormat { |
| 465 |
let oid_type = unsafe { raw::git_repository_oid_type(self.raw()) }; |
| 466 |
unsafe { Binding::from_raw(oid_type) } |
| 467 |
} |
| 468 |
|
| 469 |
/// Returns the path of the shared common directory for this repository. |
| 470 |
/// |
| 471 |
/// If the repository is bare, it is the root directory for the repository. |
| 472 |
/// If the repository is a worktree, it is the parent repo's gitdir. |
| 473 |
/// Otherwise, it is the gitdir. |
| 474 |
pub fn commondir(&self) -> &Path { |
| 475 |
unsafe { |
| 476 |
let ptr = raw::git_repository_commondir(self.raw); |
| 477 |
util::bytes2path(crate::opt_bytes(self, ptr).unwrap()) |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/// Returns the current state of this repository |
| 482 |
pub fn state(&self) -> RepositoryState { |
| 483 |
let state = unsafe { raw::git_repository_state(self.raw) }; |
| 484 |
macro_rules! check( ($($raw:ident => $real:ident),*) => ( |
| 485 |
$(if state == raw::$raw as c_int { |
| 486 |
super::RepositoryState::$real |
| 487 |
}) else * |
| 488 |
else { |
| 489 |
panic!("unknown repository state: {}", state) |
| 490 |
} |
| 491 |
) ); |
| 492 |
|
| 493 |
check!( |
| 494 |
GIT_REPOSITORY_STATE_NONE => Clean, |
| 495 |
GIT_REPOSITORY_STATE_MERGE => Merge, |
| 496 |
GIT_REPOSITORY_STATE_REVERT => Revert, |
| 497 |
GIT_REPOSITORY_STATE_REVERT_SEQUENCE => RevertSequence, |
| 498 |
GIT_REPOSITORY_STATE_CHERRYPICK => CherryPick, |
| 499 |
GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE => CherryPickSequence, |
| 500 |
GIT_REPOSITORY_STATE_BISECT => Bisect, |
| 501 |
GIT_REPOSITORY_STATE_REBASE => Rebase, |
| 502 |
GIT_REPOSITORY_STATE_REBASE_INTERACTIVE => RebaseInteractive, |
| 503 |
GIT_REPOSITORY_STATE_REBASE_MERGE => RebaseMerge, |
| 504 |
GIT_REPOSITORY_STATE_APPLY_MAILBOX => ApplyMailbox, |
| 505 |
GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE => ApplyMailboxOrRebase |
| 506 |
) |
| 507 |
} |
| 508 |
|
| 509 |
/// Get the path of the working directory for this repository. |
| 510 |
/// |
| 511 |
/// If this repository is bare, then `None` is returned. |
| 512 |
pub fn workdir(&self) -> Option<&Path> { |
| 513 |
unsafe { |
| 514 |
let ptr = raw::git_repository_workdir(self.raw); |
| 515 |
if ptr.is_null() { |
| 516 |
None |
| 517 |
} else { |
| 518 |
Some(util::bytes2path(CStr::from_ptr(ptr).to_bytes())) |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
/// Set the path to the working directory for this repository. |
| 524 |
/// |
| 525 |
/// If `update_link` is true, create/update the gitlink file in the workdir |
| 526 |
/// and set config "core.worktree" (if workdir is not the parent of the .git |
| 527 |
/// directory). |
| 528 |
pub fn set_workdir(&self, path: &Path, update_gitlink: bool) -> Result<(), Error> { |
| 529 |
// Normal file path OK (does not need Windows conversion). |
| 530 |
let path = path.into_c_string()?; |
| 531 |
unsafe { |
| 532 |
try_call!(raw::git_repository_set_workdir( |
| 533 |
self.raw(), |
| 534 |
path, |
| 535 |
update_gitlink |
| 536 |
)); |
| 537 |
} |
| 538 |
Ok(()) |
| 539 |
} |
| 540 |
|
| 541 |
/// Get the currently active namespace for this repository. |
| 542 |
/// |
| 543 |
/// If there is no namespace, Ok(None) is returned. |
| 544 |
pub fn namespace(&self) -> Result<Option<&str>, Error> { |
| 545 |
match self.namespace_bytes() { |
| 546 |
Some(nb) => str::from_utf8(nb).map(|s| Some(s)).map_err(|e| e.into()), |
| 547 |
None => Ok(None), |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/// Get the currently active namespace for this repository as a byte array. |
| 552 |
/// |
| 553 |
/// If there is no namespace, `None` is returned. |
| 554 |
pub fn namespace_bytes(&self) -> Option<&[u8]> { |
| 555 |
unsafe { crate::opt_bytes(self, raw::git_repository_get_namespace(self.raw)) } |
| 556 |
} |
| 557 |
|
| 558 |
/// Set the active namespace for this repository. |
| 559 |
pub fn set_namespace(&self, namespace: &str) -> Result<(), Error> { |
| 560 |
self.set_namespace_bytes(namespace.as_bytes()) |
| 561 |
} |
| 562 |
|
| 563 |
/// Set the active namespace for this repository as a byte array. |
| 564 |
pub fn set_namespace_bytes(&self, namespace: &[u8]) -> Result<(), Error> { |
| 565 |
unsafe { |
| 566 |
let namespace = CString::new(namespace)?; |
| 567 |
try_call!(raw::git_repository_set_namespace(self.raw, namespace)); |
| 568 |
Ok(()) |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
/// Remove the active namespace for this repository. |
| 573 |
pub fn remove_namespace(&self) -> Result<(), Error> { |
| 574 |
unsafe { |
| 575 |
try_call!(raw::git_repository_set_namespace(self.raw, ptr::null())); |
| 576 |
Ok(()) |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
/// Retrieves the Git merge message. |
| 581 |
/// Remember to remove the message when finished. |
| 582 |
pub fn message(&self) -> Result<String, Error> { |
| 583 |
unsafe { |
| 584 |
let buf = Buf::new(); |
| 585 |
try_call!(raw::git_repository_message(buf.raw(), self.raw)); |
| 586 |
Ok(str::from_utf8(&buf).unwrap().to_string()) |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/// Remove the Git merge message. |
| 591 |
pub fn remove_message(&self) -> Result<(), Error> { |
| 592 |
unsafe { |
| 593 |
try_call!(raw::git_repository_message_remove(self.raw)); |
| 594 |
Ok(()) |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
/// List all remotes for a given repository |
| 599 |
pub fn remotes(&self) -> Result<StringArray, Error> { |
| 600 |
let mut arr = raw::git_strarray { |
| 601 |
strings: ptr::null_mut(), |
| 602 |
count: 0, |
| 603 |
}; |
| 604 |
unsafe { |
| 605 |
try_call!(raw::git_remote_list(&mut arr, self.raw)); |
| 606 |
Ok(Binding::from_raw(arr)) |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
/// Get the information for a particular remote |
| 611 |
pub fn find_remote(&self, name: &str) -> Result<Remote<'_>, Error> { |
| 612 |
let mut ret = ptr::null_mut(); |
| 613 |
let name = CString::new(name)?; |
| 614 |
unsafe { |
| 615 |
try_call!(raw::git_remote_lookup(&mut ret, self.raw, name)); |
| 616 |
Ok(Binding::from_raw(ret)) |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
/// Add a remote with the default fetch refspec to the repository's |
| 621 |
/// configuration. |
| 622 |
pub fn remote(&self, name: &str, url: &str) -> Result<Remote<'_>, Error> { |
| 623 |
let mut ret = ptr::null_mut(); |
| 624 |
let name = CString::new(name)?; |
| 625 |
let url = CString::new(url)?; |
| 626 |
unsafe { |
| 627 |
try_call!(raw::git_remote_create(&mut ret, self.raw, name, url)); |
| 628 |
Ok(Binding::from_raw(ret)) |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
/// Add a remote with the provided fetch refspec to the repository's |
| 633 |
/// configuration. |
| 634 |
pub fn remote_with_fetch( |
| 635 |
&self, |
| 636 |
name: &str, |
| 637 |
url: &str, |
| 638 |
fetch: &str, |
| 639 |
) -> Result<Remote<'_>, Error> { |
| 640 |
let mut ret = ptr::null_mut(); |
| 641 |
let name = CString::new(name)?; |
| 642 |
let url = CString::new(url)?; |
| 643 |
let fetch = CString::new(fetch)?; |
| 644 |
unsafe { |
| 645 |
try_call!(raw::git_remote_create_with_fetchspec( |
| 646 |
&mut ret, self.raw, name, url, fetch |
| 647 |
)); |
| 648 |
Ok(Binding::from_raw(ret)) |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
/// Create an anonymous remote |
| 653 |
/// |
| 654 |
/// Create a remote with the given URL and refspec in memory. You can use |
| 655 |
/// this when you have a URL instead of a remote's name. Note that anonymous |
| 656 |
/// remotes cannot be converted to persisted remotes. |
| 657 |
pub fn remote_anonymous(&self, url: &str) -> Result<Remote<'_>, Error> { |
| 658 |
let mut ret = ptr::null_mut(); |
| 659 |
let url = CString::new(url)?; |
| 660 |
unsafe { |
| 661 |
try_call!(raw::git_remote_create_anonymous(&mut ret, self.raw, url)); |
| 662 |
Ok(Binding::from_raw(ret)) |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
/// Give a remote a new name |
| 667 |
/// |
| 668 |
/// All remote-tracking branches and configuration settings for the remote |
| 669 |
/// are updated. |
| 670 |
/// |
| 671 |
/// A temporary in-memory remote cannot be given a name with this method. |
| 672 |
/// |
| 673 |
/// No loaded instances of the remote with the old name will change their |
| 674 |
/// name or their list of refspecs. |
| 675 |
/// |
| 676 |
/// The returned array of strings is a list of the non-default refspecs |
| 677 |
/// which cannot be renamed and are returned for further processing by the |
| 678 |
/// caller. |
| 679 |
pub fn remote_rename(&self, name: &str, new_name: &str) -> Result<StringArray, Error> { |
| 680 |
let name = CString::new(name)?; |
| 681 |
let new_name = CString::new(new_name)?; |
| 682 |
let mut problems = raw::git_strarray { |
| 683 |
count: 0, |
| 684 |
strings: ptr::null_mut(), |
| 685 |
}; |
| 686 |
unsafe { |
| 687 |
try_call!(raw::git_remote_rename( |
| 688 |
&mut problems, |
| 689 |
self.raw, |
| 690 |
name, |
| 691 |
new_name |
| 692 |
)); |
| 693 |
Ok(Binding::from_raw(problems)) |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
/// Delete an existing persisted remote. |
| 698 |
/// |
| 699 |
/// All remote-tracking branches and configuration settings for the remote |
| 700 |
/// will be removed. |
| 701 |
pub fn remote_delete(&self, name: &str) -> Result<(), Error> { |
| 702 |
let name = CString::new(name)?; |
| 703 |
unsafe { |
| 704 |
try_call!(raw::git_remote_delete(self.raw, name)); |
| 705 |
} |
| 706 |
Ok(()) |
| 707 |
} |
| 708 |
|
| 709 |
/// Add a fetch refspec to the remote's configuration |
| 710 |
/// |
| 711 |
/// Add the given refspec to the fetch list in the configuration. No loaded |
| 712 |
/// remote instances will be affected. |
| 713 |
pub fn remote_add_fetch(&self, name: &str, spec: &str) -> Result<(), Error> { |
| 714 |
let name = CString::new(name)?; |
| 715 |
let spec = CString::new(spec)?; |
| 716 |
unsafe { |
| 717 |
try_call!(raw::git_remote_add_fetch(self.raw, name, spec)); |
| 718 |
} |
| 719 |
Ok(()) |
| 720 |
} |
| 721 |
|
| 722 |
/// Add a push refspec to the remote's configuration. |
| 723 |
/// |
| 724 |
/// Add the given refspec to the push list in the configuration. No |
| 725 |
/// loaded remote instances will be affected. |
| 726 |
pub fn remote_add_push(&self, name: &str, spec: &str) -> Result<(), Error> { |
| 727 |
let name = CString::new(name)?; |
| 728 |
let spec = CString::new(spec)?; |
| 729 |
unsafe { |
| 730 |
try_call!(raw::git_remote_add_push(self.raw, name, spec)); |
| 731 |
} |
| 732 |
Ok(()) |
| 733 |
} |
| 734 |
|
| 735 |
/// Set the remote's URL in the configuration |
| 736 |
/// |
| 737 |
/// Remote objects already in memory will not be affected. This assumes |
| 738 |
/// the common case of a single-url remote and will otherwise return an |
| 739 |
/// error. |
| 740 |
pub fn remote_set_url(&self, name: &str, url: &str) -> Result<(), Error> { |
| 741 |
let name = CString::new(name)?; |
| 742 |
let url = CString::new(url)?; |
| 743 |
unsafe { |
| 744 |
try_call!(raw::git_remote_set_url(self.raw, name, url)); |
| 745 |
} |
| 746 |
Ok(()) |
| 747 |
} |
| 748 |
|
| 749 |
/// Set the remote's URL for pushing in the configuration. |
| 750 |
/// |
| 751 |
/// Remote objects already in memory will not be affected. This assumes |
| 752 |
/// the common case of a single-url remote and will otherwise return an |
| 753 |
/// error. |
| 754 |
/// |
| 755 |
/// `None` indicates that it should be cleared. |
| 756 |
pub fn remote_set_pushurl(&self, name: &str, pushurl: Option<&str>) -> Result<(), Error> { |
| 757 |
let name = CString::new(name)?; |
| 758 |
let pushurl = crate::opt_cstr(pushurl)?; |
| 759 |
unsafe { |
| 760 |
try_call!(raw::git_remote_set_pushurl(self.raw, name, pushurl)); |
| 761 |
} |
| 762 |
Ok(()) |
| 763 |
} |
| 764 |
|
| 765 |
/// Sets the current head to the specified object and optionally resets |
| 766 |
/// the index and working tree to match. |
| 767 |
/// |
| 768 |
/// A soft reset means the head will be moved to the commit. |
| 769 |
/// |
| 770 |
/// A mixed reset will trigger a soft reset, plus the index will be |
| 771 |
/// replaced with the content of the commit tree. |
| 772 |
/// |
| 773 |
/// A hard reset will trigger a mixed reset and the working directory will |
| 774 |
/// be replaced with the content of the index. (Untracked and ignored files |
| 775 |
/// will be left alone, however.) |
| 776 |
/// |
| 777 |
/// The `target` is a commit-ish to which the head should be moved to. The |
| 778 |
/// object can either be a commit or a tag, but tags must be dereferenceable |
| 779 |
/// to a commit. |
| 780 |
/// |
| 781 |
/// The `checkout` options will only be used for a hard reset. |
| 782 |
pub fn reset( |
| 783 |
&self, |
| 784 |
target: &Object<'_>, |
| 785 |
kind: ResetType, |
| 786 |
checkout: Option<&mut CheckoutBuilder<'_>>, |
| 787 |
) -> Result<(), Error> { |
| 788 |
unsafe { |
| 789 |
let mut opts: raw::git_checkout_options = mem::zeroed(); |
| 790 |
try_call!(raw::git_checkout_init_options( |
| 791 |
&mut opts, |
| 792 |
raw::GIT_CHECKOUT_OPTIONS_VERSION |
| 793 |
)); |
| 794 |
let opts = checkout.map(|c| { |
| 795 |
c.configure(&mut opts); |
| 796 |
&mut opts |
| 797 |
}); |
| 798 |
try_call!(raw::git_reset(self.raw, target.raw(), kind, opts)); |
| 799 |
} |
| 800 |
Ok(()) |
| 801 |
} |
| 802 |
|
| 803 |
/// Updates some entries in the index from the target commit tree. |
| 804 |
/// |
| 805 |
/// The scope of the updated entries is determined by the paths being |
| 806 |
/// in the iterator provided. |
| 807 |
/// |
| 808 |
/// Passing a `None` target will result in removing entries in the index |
| 809 |
/// matching the provided pathspecs. |
| 810 |
pub fn reset_default<T, I>(&self, target: Option<&Object<'_>>, paths: I) -> Result<(), Error> |
| 811 |
where |
| 812 |
T: IntoCString, |
| 813 |
I: IntoIterator<Item = T>, |
| 814 |
{ |
| 815 |
let (_a, _b, mut arr) = crate::util::iter2cstrs_paths(paths)?; |
| 816 |
let target = target.map(|t| t.raw()); |
| 817 |
unsafe { |
| 818 |
try_call!(raw::git_reset_default(self.raw, target, &mut arr)); |
| 819 |
} |
| 820 |
Ok(()) |
| 821 |
} |
| 822 |
|
| 823 |
/// Retrieve and resolve the reference pointed at by HEAD. |
| 824 |
pub fn head(&self) -> Result<Reference<'_>, Error> { |
| 825 |
let mut ret = ptr::null_mut(); |
| 826 |
unsafe { |
| 827 |
try_call!(raw::git_repository_head(&mut ret, self.raw)); |
| 828 |
Ok(Binding::from_raw(ret)) |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
/// Make the repository HEAD point to the specified reference. |
| 833 |
/// |
| 834 |
/// If the provided reference points to a tree or a blob, the HEAD is |
| 835 |
/// unaltered and an error is returned. |
| 836 |
/// |
| 837 |
/// If the provided reference points to a branch, the HEAD will point to |
| 838 |
/// that branch, staying attached, or become attached if it isn't yet. If |
| 839 |
/// the branch doesn't exist yet, no error will be returned. The HEAD will |
| 840 |
/// then be attached to an unborn branch. |
| 841 |
/// |
| 842 |
/// Otherwise, the HEAD will be detached and will directly point to the |
| 843 |
/// commit. |
| 844 |
pub fn set_head(&self, refname: &str) -> Result<(), Error> { |
| 845 |
self.set_head_bytes(refname.as_bytes()) |
| 846 |
} |
| 847 |
|
| 848 |
/// Make the repository HEAD point to the specified reference as a byte array. |
| 849 |
/// |
| 850 |
/// If the provided reference points to a tree or a blob, the HEAD is |
| 851 |
/// unaltered and an error is returned. |
| 852 |
/// |
| 853 |
/// If the provided reference points to a branch, the HEAD will point to |
| 854 |
/// that branch, staying attached, or become attached if it isn't yet. If |
| 855 |
/// the branch doesn't exist yet, no error will be returned. The HEAD will |
| 856 |
/// then be attached to an unborn branch. |
| 857 |
/// |
| 858 |
/// Otherwise, the HEAD will be detached and will directly point to the |
| 859 |
/// commit. |
| 860 |
pub fn set_head_bytes(&self, refname: &[u8]) -> Result<(), Error> { |
| 861 |
let refname = CString::new(refname)?; |
| 862 |
unsafe { |
| 863 |
try_call!(raw::git_repository_set_head(self.raw, refname)); |
| 864 |
} |
| 865 |
Ok(()) |
| 866 |
} |
| 867 |
|
| 868 |
/// Determines whether the repository HEAD is detached. |
| 869 |
pub fn head_detached(&self) -> Result<bool, Error> { |
| 870 |
unsafe { |
| 871 |
let value = raw::git_repository_head_detached(self.raw); |
| 872 |
match value { |
| 873 |
0 => Ok(false), |
| 874 |
1 => Ok(true), |
| 875 |
_ => Err(Error::last_error(value)), |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
/// Make the repository HEAD directly point to the commit. |
| 881 |
/// |
| 882 |
/// If the provided commitish cannot be found in the repository, the HEAD |
| 883 |
/// is unaltered and an error is returned. |
| 884 |
/// |
| 885 |
/// If the provided commitish cannot be peeled into a commit, the HEAD is |
| 886 |
/// unaltered and an error is returned. |
| 887 |
/// |
| 888 |
/// Otherwise, the HEAD will eventually be detached and will directly point |
| 889 |
/// to the peeled commit. |
| 890 |
pub fn set_head_detached(&self, commitish: Oid) -> Result<(), Error> { |
| 891 |
unsafe { |
| 892 |
try_call!(raw::git_repository_set_head_detached( |
| 893 |
self.raw, |
| 894 |
commitish.raw() |
| 895 |
)); |
| 896 |
} |
| 897 |
Ok(()) |
| 898 |
} |
| 899 |
|
| 900 |
/// Make the repository HEAD directly point to the commit. |
| 901 |
/// |
| 902 |
/// If the provided commitish cannot be found in the repository, the HEAD |
| 903 |
/// is unaltered and an error is returned. |
| 904 |
/// If the provided commitish cannot be peeled into a commit, the HEAD is |
| 905 |
/// unaltered and an error is returned. |
| 906 |
/// Otherwise, the HEAD will eventually be detached and will directly point |
| 907 |
/// to the peeled commit. |
| 908 |
pub fn set_head_detached_from_annotated( |
| 909 |
&self, |
| 910 |
commitish: AnnotatedCommit<'_>, |
| 911 |
) -> Result<(), Error> { |
| 912 |
unsafe { |
| 913 |
try_call!(raw::git_repository_set_head_detached_from_annotated( |
| 914 |
self.raw, |
| 915 |
commitish.raw() |
| 916 |
)); |
| 917 |
} |
| 918 |
Ok(()) |
| 919 |
} |
| 920 |
|
| 921 |
/// Create an iterator for the repo's references |
| 922 |
pub fn references(&self) -> Result<References<'_>, Error> { |
| 923 |
let mut ret = ptr::null_mut(); |
| 924 |
unsafe { |
| 925 |
try_call!(raw::git_reference_iterator_new(&mut ret, self.raw)); |
| 926 |
Ok(Binding::from_raw(ret)) |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
/// Create an iterator for the repo's references that match the specified |
| 931 |
/// glob |
| 932 |
pub fn references_glob(&self, glob: &str) -> Result<References<'_>, Error> { |
| 933 |
let mut ret = ptr::null_mut(); |
| 934 |
let glob = CString::new(glob)?; |
| 935 |
unsafe { |
| 936 |
try_call!(raw::git_reference_iterator_glob_new( |
| 937 |
&mut ret, self.raw, glob |
| 938 |
)); |
| 939 |
|
| 940 |
Ok(Binding::from_raw(ret)) |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
/// Load all submodules for this repository and return them. |
| 945 |
pub fn submodules(&self) -> Result<Vec<Submodule<'_>>, Error> { |
| 946 |
struct Data<'a, 'b> { |
| 947 |
repo: &'b Repository, |
| 948 |
ret: &'a mut Vec<Submodule<'b>>, |
| 949 |
} |
| 950 |
let mut ret = Vec::new(); |
| 951 |
|
| 952 |
unsafe { |
| 953 |
let mut data = Data { |
| 954 |
repo: self, |
| 955 |
ret: &mut ret, |
| 956 |
}; |
| 957 |
let cb: raw::git_submodule_cb = Some(append); |
| 958 |
try_call!(raw::git_submodule_foreach( |
| 959 |
self.raw, |
| 960 |
cb, |
| 961 |
&mut data as *mut _ as *mut c_void |
| 962 |
)); |
| 963 |
} |
| 964 |
|
| 965 |
return Ok(ret); |
| 966 |
|
| 967 |
extern "C" fn append( |
| 968 |
_repo: *mut raw::git_submodule, |
| 969 |
name: *const c_char, |
| 970 |
data: *mut c_void, |
| 971 |
) -> c_int { |
| 972 |
unsafe { |
| 973 |
let data = &mut *(data as *mut Data<'_, '_>); |
| 974 |
let mut raw = ptr::null_mut(); |
| 975 |
let rc = raw::git_submodule_lookup(&mut raw, data.repo.raw(), name); |
| 976 |
if rc != 0 { |
| 977 |
return rc; |
| 978 |
} |
| 979 |
data.ret.push(Binding::from_raw(raw)); |
| 980 |
} |
| 981 |
0 |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
/// Gather file status information and populate the returned structure. |
| 986 |
/// |
| 987 |
/// Note that if a pathspec is given in the options to filter the |
| 988 |
/// status, then the results from rename detection (if you enable it) may |
| 989 |
/// not be accurate. To do rename detection properly, this must be called |
| 990 |
/// with no pathspec so that all files can be considered. |
| 991 |
pub fn statuses(&self, options: Option<&mut StatusOptions>) -> Result<Statuses<'_>, Error> { |
| 992 |
let mut ret = ptr::null_mut(); |
| 993 |
unsafe { |
| 994 |
try_call!(raw::git_status_list_new( |
| 995 |
&mut ret, |
| 996 |
self.raw, |
| 997 |
options.map(|s| s.raw()).unwrap_or(ptr::null()) |
| 998 |
)); |
| 999 |
Ok(Binding::from_raw(ret)) |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
/// Test if the ignore rules apply to a given file. |
| 1004 |
/// |
| 1005 |
/// This function checks the ignore rules to see if they would apply to the |
| 1006 |
/// given file. This indicates if the file would be ignored regardless of |
| 1007 |
/// whether the file is already in the index or committed to the repository. |
| 1008 |
/// |
| 1009 |
/// One way to think of this is if you were to do "git add ." on the |
| 1010 |
/// directory containing the file, would it be added or not? |
| 1011 |
pub fn status_should_ignore(&self, path: &Path) -> Result<bool, Error> { |
| 1012 |
let mut ret = 0 as c_int; |
| 1013 |
let path = util::cstring_to_repo_path(path)?; |
| 1014 |
unsafe { |
| 1015 |
try_call!(raw::git_status_should_ignore(&mut ret, self.raw, path)); |
| 1016 |
} |
| 1017 |
Ok(ret != 0) |
| 1018 |
} |
| 1019 |
|
| 1020 |
/// Get file status for a single file. |
| 1021 |
/// |
| 1022 |
/// This tries to get status for the filename that you give. If no files |
| 1023 |
/// match that name (in either the HEAD, index, or working directory), this |
| 1024 |
/// returns NotFound. |
| 1025 |
/// |
| 1026 |
/// If the name matches multiple files (for example, if the path names a |
| 1027 |
/// directory or if running on a case- insensitive filesystem and yet the |
| 1028 |
/// HEAD has two entries that both match the path), then this returns |
| 1029 |
/// Ambiguous because it cannot give correct results. |
| 1030 |
/// |
| 1031 |
/// This does not do any sort of rename detection. Renames require a set of |
| 1032 |
/// targets and because of the path filtering, there is not enough |
| 1033 |
/// information to check renames correctly. To check file status with rename |
| 1034 |
/// detection, there is no choice but to do a full `statuses` and scan |
| 1035 |
/// through looking for the path that you are interested in. |
| 1036 |
pub fn status_file(&self, path: &Path) -> Result<Status, Error> { |
| 1037 |
let mut ret = 0 as c_uint; |
| 1038 |
let path = path_to_repo_path(path)?; |
| 1039 |
unsafe { |
| 1040 |
try_call!(raw::git_status_file(&mut ret, self.raw, path)); |
| 1041 |
} |
| 1042 |
Ok(Status::from_bits_truncate(ret as u32)) |
| 1043 |
} |
| 1044 |
|
| 1045 |
/// Create an iterator which loops over the requested branches. |
| 1046 |
pub fn branches(&self, filter: Option<BranchType>) -> Result<Branches<'_>, Error> { |
| 1047 |
let mut raw = ptr::null_mut(); |
| 1048 |
unsafe { |
| 1049 |
try_call!(raw::git_branch_iterator_new(&mut raw, self.raw(), filter)); |
| 1050 |
Ok(Branches::from_raw(raw)) |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
/// Get the Index file for this repository. |
| 1055 |
/// |
| 1056 |
/// If a custom index has not been set, the default index for the repository |
| 1057 |
/// will be returned (the one located in .git/index). |
| 1058 |
/// |
| 1059 |
/// **Caution**: If the [`Repository`] of this index is dropped, then this |
| 1060 |
/// [`Index`] will become detached, and most methods on it will fail. See |
| 1061 |
/// [`Index::open`]. Be sure the repository has a binding such as a local |
| 1062 |
/// variable to keep it alive at least as long as the index. |
| 1063 |
pub fn index(&self) -> Result<Index, Error> { |
| 1064 |
let mut raw = ptr::null_mut(); |
| 1065 |
unsafe { |
| 1066 |
try_call!(raw::git_repository_index(&mut raw, self.raw())); |
| 1067 |
Ok(Binding::from_raw(raw)) |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
/// Set the Index file for this repository. |
| 1072 |
pub fn set_index(&self, index: &mut Index) -> Result<(), Error> { |
| 1073 |
unsafe { |
| 1074 |
try_call!(raw::git_repository_set_index(self.raw(), index.raw())); |
| 1075 |
} |
| 1076 |
Ok(()) |
| 1077 |
} |
| 1078 |
|
| 1079 |
/// Get the configuration file for this repository. |
| 1080 |
/// |
| 1081 |
/// If a configuration file has not been set, the default config set for the |
| 1082 |
/// repository will be returned, including global and system configurations |
| 1083 |
/// (if they are available). |
| 1084 |
pub fn config(&self) -> Result<Config, Error> { |
| 1085 |
let mut raw = ptr::null_mut(); |
| 1086 |
unsafe { |
| 1087 |
try_call!(raw::git_repository_config(&mut raw, self.raw())); |
| 1088 |
Ok(Binding::from_raw(raw)) |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
/// Set the configuration file for this repository. |
| 1093 |
/// |
| 1094 |
/// This configuration file will be used for all configuration queries involving this |
| 1095 |
/// repository. |
| 1096 |
pub fn set_config(&self, config: &Config) -> Result<(), Error> { |
| 1097 |
unsafe { |
| 1098 |
try_call!(raw::git_repository_set_config(self.raw(), config.raw())); |
| 1099 |
} |
| 1100 |
Ok(()) |
| 1101 |
} |
| 1102 |
|
| 1103 |
/// Get the value of a git attribute for a path as a string. |
| 1104 |
/// |
| 1105 |
/// This function will return a special string if the attribute is set to a special value. |
| 1106 |
/// Interpreting the special string is discouraged. You should always use |
| 1107 |
/// [`AttrValue::from_string`](crate::AttrValue::from_string) to interpret the return value |
| 1108 |
/// and avoid the special string. |
| 1109 |
/// |
| 1110 |
/// As such, the return type of this function will probably be changed in the next major version |
| 1111 |
/// to prevent interpreting the returned string without checking whether it's special. |
| 1112 |
pub fn get_attr( |
| 1113 |
&self, |
| 1114 |
path: &Path, |
| 1115 |
name: &str, |
| 1116 |
flags: AttrCheckFlags, |
| 1117 |
) -> Result<Option<&str>, Error> { |
| 1118 |
Ok(self |
| 1119 |
.get_attr_bytes(path, name, flags)? |
| 1120 |
.and_then(|a| str::from_utf8(a).ok())) |
| 1121 |
} |
| 1122 |
|
| 1123 |
/// Get the value of a git attribute for a path as a byte slice. |
| 1124 |
/// |
| 1125 |
/// This function will return a special byte slice if the attribute is set to a special value. |
| 1126 |
/// Interpreting the special byte slice is discouraged. You should always use |
| 1127 |
/// [`AttrValue::from_bytes`](crate::AttrValue::from_bytes) to interpret the return value and |
| 1128 |
/// avoid the special string. |
| 1129 |
/// |
| 1130 |
/// As such, the return type of this function will probably be changed in the next major version |
| 1131 |
/// to prevent interpreting the returned byte slice without checking whether it's special. |
| 1132 |
pub fn get_attr_bytes( |
| 1133 |
&self, |
| 1134 |
path: &Path, |
| 1135 |
name: &str, |
| 1136 |
flags: AttrCheckFlags, |
| 1137 |
) -> Result<Option<&[u8]>, Error> { |
| 1138 |
let mut ret = ptr::null(); |
| 1139 |
let path = util::cstring_to_repo_path(path)?; |
| 1140 |
let name = CString::new(name)?; |
| 1141 |
unsafe { |
| 1142 |
try_call!(raw::git_attr_get( |
| 1143 |
&mut ret, |
| 1144 |
self.raw(), |
| 1145 |
flags.bits(), |
| 1146 |
path, |
| 1147 |
name |
| 1148 |
)); |
| 1149 |
Ok(crate::opt_bytes(self, ret)) |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
/// Write an in-memory buffer to the ODB as a blob. |
| 1154 |
/// |
| 1155 |
/// The Oid returned can in turn be passed to `find_blob` to get a handle to |
| 1156 |
/// the blob. |
| 1157 |
pub fn blob(&self, data: &[u8]) -> Result<Oid, Error> { |
| 1158 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 1159 |
unsafe { |
| 1160 |
let ptr = data.as_ptr() as *const c_void; |
| 1161 |
let len = data.len() as size_t; |
| 1162 |
try_call!(raw::git_blob_create_frombuffer( |
| 1163 |
&mut raw, |
| 1164 |
self.raw(), |
| 1165 |
ptr, |
| 1166 |
len |
| 1167 |
)); |
| 1168 |
Ok(Binding::from_raw(&raw as *const _)) |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
/// Read a file from the filesystem and write its content to the Object |
| 1173 |
/// Database as a loose blob |
| 1174 |
/// |
| 1175 |
/// The Oid returned can in turn be passed to `find_blob` to get a handle to |
| 1176 |
/// the blob. |
| 1177 |
pub fn blob_path(&self, path: &Path) -> Result<Oid, Error> { |
| 1178 |
// Normal file path OK (does not need Windows conversion). |
| 1179 |
let path = path.into_c_string()?; |
| 1180 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 1181 |
unsafe { |
| 1182 |
try_call!(raw::git_blob_create_fromdisk(&mut raw, self.raw(), path)); |
| 1183 |
Ok(Binding::from_raw(&raw as *const _)) |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
/// Create a stream to write blob |
| 1188 |
/// |
| 1189 |
/// This function may need to buffer the data on disk and will in general |
| 1190 |
/// not be the right choice if you know the size of the data to write. |
| 1191 |
/// |
| 1192 |
/// Use `BlobWriter::commit()` to commit the write to the object db |
| 1193 |
/// and get the object id. |
| 1194 |
/// |
| 1195 |
/// If the `hintpath` parameter is filled, it will be used to determine |
| 1196 |
/// what git filters should be applied to the object before it is written |
| 1197 |
/// to the object database. |
| 1198 |
pub fn blob_writer(&self, hintpath: Option<&Path>) -> Result<BlobWriter<'_>, Error> { |
| 1199 |
let path_str = match hintpath { |
| 1200 |
Some(path) => Some(path.into_c_string()?), |
| 1201 |
None => None, |
| 1202 |
}; |
| 1203 |
let path = match path_str { |
| 1204 |
Some(ref path) => path.as_ptr(), |
| 1205 |
None => ptr::null(), |
| 1206 |
}; |
| 1207 |
let mut out = ptr::null_mut(); |
| 1208 |
unsafe { |
| 1209 |
try_call!(raw::git_blob_create_fromstream(&mut out, self.raw(), path)); |
| 1210 |
Ok(BlobWriter::from_raw(out)) |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
/// Lookup a reference to one of the objects in a repository. |
| 1215 |
pub fn find_blob(&self, oid: Oid) -> Result<Blob<'_>, Error> { |
| 1216 |
let mut raw = ptr::null_mut(); |
| 1217 |
unsafe { |
| 1218 |
try_call!(raw::git_blob_lookup(&mut raw, self.raw(), oid.raw())); |
| 1219 |
Ok(Binding::from_raw(raw)) |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
/// Get the object database for this repository |
| 1224 |
pub fn odb(&self) -> Result<Odb<'_>, Error> { |
| 1225 |
let mut odb = ptr::null_mut(); |
| 1226 |
unsafe { |
| 1227 |
try_call!(raw::git_repository_odb(&mut odb, self.raw())); |
| 1228 |
Ok(Odb::from_raw(odb)) |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
/// Override the object database for this repository |
| 1233 |
pub fn set_odb(&self, odb: &Odb<'_>) -> Result<(), Error> { |
| 1234 |
unsafe { |
| 1235 |
try_call!(raw::git_repository_set_odb(self.raw(), odb.raw())); |
| 1236 |
} |
| 1237 |
Ok(()) |
| 1238 |
} |
| 1239 |
|
| 1240 |
/// Suggests that the reference database compress or optimize its |
| 1241 |
/// references. This mechanism is implementation specific. For on-disk |
| 1242 |
/// reference databases, for example, this may pack all loose references. |
| 1243 |
pub fn refdb_compress(&self) -> Result<(), Error> { |
| 1244 |
self.refdb()?.compress() |
| 1245 |
} |
| 1246 |
|
| 1247 |
/// Get the reference database for this repository. |
| 1248 |
pub fn refdb(&self) -> Result<Refdb<'_>, Error> { |
| 1249 |
let mut refdb = ptr::null_mut(); |
| 1250 |
unsafe { |
| 1251 |
try_call!(raw::git_repository_refdb(&mut refdb, self.raw())); |
| 1252 |
Ok(Binding::from_raw(refdb)) |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
/// Override the reference database for this repository |
| 1257 |
pub fn set_refdb(&self, refdb: &Refdb<'_>) -> Result<(), Error> { |
| 1258 |
unsafe { |
| 1259 |
try_call!(raw::git_repository_set_refdb(self.raw(), refdb.raw())); |
| 1260 |
} |
| 1261 |
Ok(()) |
| 1262 |
} |
| 1263 |
|
| 1264 |
/// Create a new branch pointing at a target commit |
| 1265 |
/// |
| 1266 |
/// A new direct reference will be created pointing to this target commit. |
| 1267 |
/// If `force` is true and a reference already exists with the given name, |
| 1268 |
/// it'll be replaced. |
| 1269 |
pub fn branch( |
| 1270 |
&self, |
| 1271 |
branch_name: &str, |
| 1272 |
target: &Commit<'_>, |
| 1273 |
force: bool, |
| 1274 |
) -> Result<Branch<'_>, Error> { |
| 1275 |
let branch_name = CString::new(branch_name)?; |
| 1276 |
let mut raw = ptr::null_mut(); |
| 1277 |
unsafe { |
| 1278 |
try_call!(raw::git_branch_create( |
| 1279 |
&mut raw, |
| 1280 |
self.raw(), |
| 1281 |
branch_name, |
| 1282 |
target.raw(), |
| 1283 |
force |
| 1284 |
)); |
| 1285 |
Ok(Branch::wrap(Binding::from_raw(raw))) |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
/// Create a new branch pointing at a target commit |
| 1290 |
/// |
| 1291 |
/// This behaves like `Repository::branch()` but takes |
| 1292 |
/// an annotated commit, which lets you specify which |
| 1293 |
/// extended SHA syntax string was specified by a user, |
| 1294 |
/// allowing for more exact reflog messages. |
| 1295 |
/// |
| 1296 |
/// See the documentation for `Repository::branch()` |
| 1297 |
pub fn branch_from_annotated_commit( |
| 1298 |
&self, |
| 1299 |
branch_name: &str, |
| 1300 |
target: &AnnotatedCommit<'_>, |
| 1301 |
force: bool, |
| 1302 |
) -> Result<Branch<'_>, Error> { |
| 1303 |
let branch_name = CString::new(branch_name)?; |
| 1304 |
let mut raw = ptr::null_mut(); |
| 1305 |
unsafe { |
| 1306 |
try_call!(raw::git_branch_create_from_annotated( |
| 1307 |
&mut raw, |
| 1308 |
self.raw(), |
| 1309 |
branch_name, |
| 1310 |
target.raw(), |
| 1311 |
force |
| 1312 |
)); |
| 1313 |
Ok(Branch::wrap(Binding::from_raw(raw))) |
| 1314 |
} |
| 1315 |
} |
| 1316 |
|
| 1317 |
/// Lookup a branch by its name in a repository. |
| 1318 |
pub fn find_branch(&self, name: &str, branch_type: BranchType) -> Result<Branch<'_>, Error> { |
| 1319 |
let name = CString::new(name)?; |
| 1320 |
let mut ret = ptr::null_mut(); |
| 1321 |
unsafe { |
| 1322 |
try_call!(raw::git_branch_lookup( |
| 1323 |
&mut ret, |
| 1324 |
self.raw(), |
| 1325 |
name, |
| 1326 |
branch_type |
| 1327 |
)); |
| 1328 |
Ok(Branch::wrap(Binding::from_raw(ret))) |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
/// Create new commit in the repository |
| 1333 |
/// |
| 1334 |
/// If the `update_ref` is not `None`, name of the reference that will be |
| 1335 |
/// updated to point to this commit. If the reference is not direct, it will |
| 1336 |
/// be resolved to a direct reference. Use "HEAD" to update the HEAD of the |
| 1337 |
/// current branch and make it point to this commit. If the reference |
| 1338 |
/// doesn't exist yet, it will be created. If it does exist, the first |
| 1339 |
/// parent must be the tip of this branch. |
| 1340 |
pub fn commit( |
| 1341 |
&self, |
| 1342 |
update_ref: Option<&str>, |
| 1343 |
author: &Signature<'_>, |
| 1344 |
committer: &Signature<'_>, |
| 1345 |
message: &str, |
| 1346 |
tree: &Tree<'_>, |
| 1347 |
parents: &[&Commit<'_>], |
| 1348 |
) -> Result<Oid, Error> { |
| 1349 |
let update_ref = crate::opt_cstr(update_ref)?; |
| 1350 |
let mut parent_ptrs = parents |
| 1351 |
.iter() |
| 1352 |
.map(|p| p.raw() as *const raw::git_commit) |
| 1353 |
.collect::<Vec<_>>(); |
| 1354 |
let message = CString::new(message)?; |
| 1355 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 1356 |
unsafe { |
| 1357 |
try_call!(raw::git_commit_create( |
| 1358 |
&mut raw, |
| 1359 |
self.raw(), |
| 1360 |
update_ref, |
| 1361 |
author.raw(), |
| 1362 |
committer.raw(), |
| 1363 |
ptr::null(), |
| 1364 |
message, |
| 1365 |
tree.raw(), |
| 1366 |
parents.len() as size_t, |
| 1367 |
parent_ptrs.as_mut_ptr() |
| 1368 |
)); |
| 1369 |
Ok(Binding::from_raw(&raw as *const _)) |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
/// Create a commit object and return that as a Buf. |
| 1374 |
/// |
| 1375 |
/// That can be converted to a string like this `str::from_utf8(&buf).unwrap().to_string()`. |
| 1376 |
/// And that string can be passed to the `commit_signed` function, |
| 1377 |
/// the arguments behave the same as in the `commit` function. |
| 1378 |
pub fn commit_create_buffer( |
| 1379 |
&self, |
| 1380 |
author: &Signature<'_>, |
| 1381 |
committer: &Signature<'_>, |
| 1382 |
message: &str, |
| 1383 |
tree: &Tree<'_>, |
| 1384 |
parents: &[&Commit<'_>], |
| 1385 |
) -> Result<Buf, Error> { |
| 1386 |
let mut parent_ptrs = parents |
| 1387 |
.iter() |
| 1388 |
.map(|p| p.raw() as *const raw::git_commit) |
| 1389 |
.collect::<Vec<_>>(); |
| 1390 |
let message = CString::new(message)?; |
| 1391 |
let buf = Buf::new(); |
| 1392 |
unsafe { |
| 1393 |
try_call!(raw::git_commit_create_buffer( |
| 1394 |
buf.raw(), |
| 1395 |
self.raw(), |
| 1396 |
author.raw(), |
| 1397 |
committer.raw(), |
| 1398 |
ptr::null(), |
| 1399 |
message, |
| 1400 |
tree.raw(), |
| 1401 |
parents.len() as size_t, |
| 1402 |
parent_ptrs.as_mut_ptr() |
| 1403 |
)); |
| 1404 |
Ok(buf) |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
/// Create a commit object from the given buffer and signature |
| 1409 |
/// |
| 1410 |
/// Given the unsigned commit object's contents, its signature and the |
| 1411 |
/// header field in which to store the signature, attach the signature to |
| 1412 |
/// the commit and write it into the given repository. |
| 1413 |
/// |
| 1414 |
/// Use `None` in `signature_field` to use the default of `gpgsig`, which is |
| 1415 |
/// almost certainly what you want. |
| 1416 |
/// |
| 1417 |
/// Returns the resulting (signed) commit id. |
| 1418 |
pub fn commit_signed( |
| 1419 |
&self, |
| 1420 |
commit_content: &str, |
| 1421 |
signature: &str, |
| 1422 |
signature_field: Option<&str>, |
| 1423 |
) -> Result<Oid, Error> { |
| 1424 |
let commit_content = CString::new(commit_content)?; |
| 1425 |
let signature = CString::new(signature)?; |
| 1426 |
let signature_field = crate::opt_cstr(signature_field)?; |
| 1427 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 1428 |
unsafe { |
| 1429 |
try_call!(raw::git_commit_create_with_signature( |
| 1430 |
&mut raw, |
| 1431 |
self.raw(), |
| 1432 |
commit_content, |
| 1433 |
signature, |
| 1434 |
signature_field |
| 1435 |
)); |
| 1436 |
Ok(Binding::from_raw(&raw as *const _)) |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
/// Extract the signature from a commit |
| 1441 |
/// |
| 1442 |
/// Returns a tuple containing the signature in the first value and the |
| 1443 |
/// signed data in the second. |
| 1444 |
pub fn extract_signature( |
| 1445 |
&self, |
| 1446 |
commit_id: &Oid, |
| 1447 |
signature_field: Option<&str>, |
| 1448 |
) -> Result<(Buf, Buf), Error> { |
| 1449 |
let signature_field = crate::opt_cstr(signature_field)?; |
| 1450 |
let signature = Buf::new(); |
| 1451 |
let content = Buf::new(); |
| 1452 |
unsafe { |
| 1453 |
try_call!(raw::git_commit_extract_signature( |
| 1454 |
signature.raw(), |
| 1455 |
content.raw(), |
| 1456 |
self.raw(), |
| 1457 |
commit_id.raw() as *mut _, |
| 1458 |
signature_field |
| 1459 |
)); |
| 1460 |
Ok((signature, content)) |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
/// Lookup a reference to one of the commits in a repository. |
| 1465 |
pub fn find_commit(&self, oid: Oid) -> Result<Commit<'_>, Error> { |
| 1466 |
let mut raw = ptr::null_mut(); |
| 1467 |
unsafe { |
| 1468 |
try_call!(raw::git_commit_lookup(&mut raw, self.raw(), oid.raw())); |
| 1469 |
Ok(Binding::from_raw(raw)) |
| 1470 |
} |
| 1471 |
} |
| 1472 |
|
| 1473 |
/// Lookup a reference to one of the commits in a repository by short hash. |
| 1474 |
pub fn find_commit_by_prefix(&self, prefix_hash: &str) -> Result<Commit<'_>, Error> { |
| 1475 |
let mut raw = ptr::null_mut(); |
| 1476 |
let oid = Oid::from_str_ext(prefix_hash, self.object_format())?; |
| 1477 |
unsafe { |
| 1478 |
try_call!(raw::git_commit_lookup_prefix( |
| 1479 |
&mut raw, |
| 1480 |
self.raw(), |
| 1481 |
oid.raw(), |
| 1482 |
prefix_hash.len() |
| 1483 |
)); |
| 1484 |
Ok(Binding::from_raw(raw)) |
| 1485 |
} |
| 1486 |
} |
| 1487 |
|
| 1488 |
/// Creates an `AnnotatedCommit` from the given commit id. |
| 1489 |
pub fn find_annotated_commit(&self, id: Oid) -> Result<AnnotatedCommit<'_>, Error> { |
| 1490 |
unsafe { |
| 1491 |
let mut raw = ptr::null_mut(); |
| 1492 |
try_call!(raw::git_annotated_commit_lookup( |
| 1493 |
&mut raw, |
| 1494 |
self.raw(), |
| 1495 |
id.raw() |
| 1496 |
)); |
| 1497 |
Ok(Binding::from_raw(raw)) |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
/// Lookup a reference to one of the objects in a repository. |
| 1502 |
pub fn find_object(&self, oid: Oid, kind: Option<ObjectType>) -> Result<Object<'_>, Error> { |
| 1503 |
let mut raw = ptr::null_mut(); |
| 1504 |
unsafe { |
| 1505 |
try_call!(raw::git_object_lookup( |
| 1506 |
&mut raw, |
| 1507 |
self.raw(), |
| 1508 |
oid.raw(), |
| 1509 |
kind |
| 1510 |
)); |
| 1511 |
Ok(Binding::from_raw(raw)) |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
/// Lookup a reference to one of the objects by id prefix in a repository. |
| 1516 |
pub fn find_object_by_prefix( |
| 1517 |
&self, |
| 1518 |
prefix_hash: &str, |
| 1519 |
kind: Option<ObjectType>, |
| 1520 |
) -> Result<Object<'_>, Error> { |
| 1521 |
let mut raw = ptr::null_mut(); |
| 1522 |
let oid = Oid::from_str_ext(prefix_hash, self.object_format())?; |
| 1523 |
unsafe { |
| 1524 |
try_call!(raw::git_object_lookup_prefix( |
| 1525 |
&mut raw, |
| 1526 |
self.raw(), |
| 1527 |
oid.raw(), |
| 1528 |
prefix_hash.len(), |
| 1529 |
kind |
| 1530 |
)); |
| 1531 |
Ok(Binding::from_raw(raw)) |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
/// Create a new direct reference. |
| 1536 |
/// |
| 1537 |
/// This function will return an error if a reference already exists with |
| 1538 |
/// the given name unless force is true, in which case it will be |
| 1539 |
/// overwritten. |
| 1540 |
pub fn reference( |
| 1541 |
&self, |
| 1542 |
name: &str, |
| 1543 |
id: Oid, |
| 1544 |
force: bool, |
| 1545 |
log_message: &str, |
| 1546 |
) -> Result<Reference<'_>, Error> { |
| 1547 |
let name = CString::new(name)?; |
| 1548 |
let log_message = CString::new(log_message)?; |
| 1549 |
let mut raw = ptr::null_mut(); |
| 1550 |
unsafe { |
| 1551 |
try_call!(raw::git_reference_create( |
| 1552 |
&mut raw, |
| 1553 |
self.raw(), |
| 1554 |
name, |
| 1555 |
id.raw(), |
| 1556 |
force, |
| 1557 |
log_message |
| 1558 |
)); |
| 1559 |
Ok(Binding::from_raw(raw)) |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
/// Conditionally create new direct reference. |
| 1564 |
/// |
| 1565 |
/// A direct reference (also called an object id reference) refers directly |
| 1566 |
/// to a specific object id (a.k.a. OID or SHA) in the repository. The id |
| 1567 |
/// permanently refers to the object (although the reference itself can be |
| 1568 |
/// moved). For example, in libgit2 the direct ref "refs/tags/v0.17.0" |
| 1569 |
/// refers to OID 5b9fac39d8a76b9139667c26a63e6b3f204b3977. |
| 1570 |
/// |
| 1571 |
/// The direct reference will be created in the repository and written to |
| 1572 |
/// the disk. |
| 1573 |
/// |
| 1574 |
/// Valid reference names must follow one of two patterns: |
| 1575 |
/// |
| 1576 |
/// 1. Top-level names must contain only capital letters and underscores, |
| 1577 |
/// and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD"). |
| 1578 |
/// 2. Names prefixed with "refs/" can be almost anything. You must avoid |
| 1579 |
/// the characters `~`, `^`, `:`, `\\`, `?`, `[`, and `*`, and the |
| 1580 |
/// sequences ".." and "@{" which have special meaning to revparse. |
| 1581 |
/// |
| 1582 |
/// This function will return an error if a reference already exists with |
| 1583 |
/// the given name unless `force` is true, in which case it will be |
| 1584 |
/// overwritten. |
| 1585 |
/// |
| 1586 |
/// The message for the reflog will be ignored if the reference does not |
| 1587 |
/// belong in the standard set (HEAD, branches and remote-tracking |
| 1588 |
/// branches) and it does not have a reflog. |
| 1589 |
/// |
| 1590 |
/// It will return GIT_EMODIFIED if the reference's value at the time of |
| 1591 |
/// updating does not match the one passed through `current_id` (i.e. if the |
| 1592 |
/// ref has changed since the user read it). |
| 1593 |
pub fn reference_matching( |
| 1594 |
&self, |
| 1595 |
name: &str, |
| 1596 |
id: Oid, |
| 1597 |
force: bool, |
| 1598 |
current_id: Oid, |
| 1599 |
log_message: &str, |
| 1600 |
) -> Result<Reference<'_>, Error> { |
| 1601 |
let name = CString::new(name)?; |
| 1602 |
let log_message = CString::new(log_message)?; |
| 1603 |
let mut raw = ptr::null_mut(); |
| 1604 |
unsafe { |
| 1605 |
try_call!(raw::git_reference_create_matching( |
| 1606 |
&mut raw, |
| 1607 |
self.raw(), |
| 1608 |
name, |
| 1609 |
id.raw(), |
| 1610 |
force, |
| 1611 |
current_id.raw(), |
| 1612 |
log_message |
| 1613 |
)); |
| 1614 |
Ok(Binding::from_raw(raw)) |
| 1615 |
} |
| 1616 |
} |
| 1617 |
|
| 1618 |
/// Create a new symbolic reference. |
| 1619 |
/// |
| 1620 |
/// A symbolic reference is a reference name that refers to another |
| 1621 |
/// reference name. If the other name moves, the symbolic name will move, |
| 1622 |
/// too. As a simple example, the "HEAD" reference might refer to |
| 1623 |
/// "refs/heads/master" while on the "master" branch of a repository. |
| 1624 |
/// |
| 1625 |
/// Valid reference names must follow one of two patterns: |
| 1626 |
/// |
| 1627 |
/// 1. Top-level names must contain only capital letters and underscores, |
| 1628 |
/// and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD"). |
| 1629 |
/// 2. Names prefixed with "refs/" can be almost anything. You must avoid |
| 1630 |
/// the characters '~', '^', ':', '\\', '?', '[', and '*', and the |
| 1631 |
/// sequences ".." and "@{" which have special meaning to revparse. |
| 1632 |
/// |
| 1633 |
/// This function will return an error if a reference already exists with |
| 1634 |
/// the given name unless force is true, in which case it will be |
| 1635 |
/// overwritten. |
| 1636 |
pub fn reference_symbolic( |
| 1637 |
&self, |
| 1638 |
name: &str, |
| 1639 |
target: &str, |
| 1640 |
force: bool, |
| 1641 |
log_message: &str, |
| 1642 |
) -> Result<Reference<'_>, Error> { |
| 1643 |
let name = CString::new(name)?; |
| 1644 |
let target = CString::new(target)?; |
| 1645 |
let log_message = CString::new(log_message)?; |
| 1646 |
let mut raw = ptr::null_mut(); |
| 1647 |
unsafe { |
| 1648 |
try_call!(raw::git_reference_symbolic_create( |
| 1649 |
&mut raw, |
| 1650 |
self.raw(), |
| 1651 |
name, |
| 1652 |
target, |
| 1653 |
force, |
| 1654 |
log_message |
| 1655 |
)); |
| 1656 |
Ok(Binding::from_raw(raw)) |
| 1657 |
} |
| 1658 |
} |
| 1659 |
|
| 1660 |
/// Create a new symbolic reference. |
| 1661 |
/// |
| 1662 |
/// This function will return an error if a reference already exists with |
| 1663 |
/// the given name unless force is true, in which case it will be |
| 1664 |
/// overwritten. |
| 1665 |
/// |
| 1666 |
/// It will return GIT_EMODIFIED if the reference's value at the time of |
| 1667 |
/// updating does not match the one passed through current_value (i.e. if |
| 1668 |
/// the ref has changed since the user read it). |
| 1669 |
pub fn reference_symbolic_matching( |
| 1670 |
&self, |
| 1671 |
name: &str, |
| 1672 |
target: &str, |
| 1673 |
force: bool, |
| 1674 |
current_value: &str, |
| 1675 |
log_message: &str, |
| 1676 |
) -> Result<Reference<'_>, Error> { |
| 1677 |
let name = CString::new(name)?; |
| 1678 |
let target = CString::new(target)?; |
| 1679 |
let current_value = CString::new(current_value)?; |
| 1680 |
let log_message = CString::new(log_message)?; |
| 1681 |
let mut raw = ptr::null_mut(); |
| 1682 |
unsafe { |
| 1683 |
try_call!(raw::git_reference_symbolic_create_matching( |
| 1684 |
&mut raw, |
| 1685 |
self.raw(), |
| 1686 |
name, |
| 1687 |
target, |
| 1688 |
force, |
| 1689 |
current_value, |
| 1690 |
log_message |
| 1691 |
)); |
| 1692 |
Ok(Binding::from_raw(raw)) |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
/// Lookup a reference to one of the objects in a repository. |
| 1697 |
pub fn find_reference(&self, name: &str) -> Result<Reference<'_>, Error> { |
| 1698 |
let name = CString::new(name)?; |
| 1699 |
let mut raw = ptr::null_mut(); |
| 1700 |
unsafe { |
| 1701 |
try_call!(raw::git_reference_lookup(&mut raw, self.raw(), name)); |
| 1702 |
Ok(Binding::from_raw(raw)) |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
/// Lookup a reference to one of the objects in a repository. |
| 1707 |
/// `Repository::find_reference` with teeth; give the method your reference in |
| 1708 |
/// human-readable format e.g. 'main' instead of 'refs/heads/main', and it |
| 1709 |
/// will do-what-you-mean, returning the `Reference`. |
| 1710 |
pub fn resolve_reference_from_short_name(&self, refname: &str) -> Result<Reference<'_>, Error> { |
| 1711 |
let refname = CString::new(refname)?; |
| 1712 |
let mut raw = ptr::null_mut(); |
| 1713 |
unsafe { |
| 1714 |
try_call!(raw::git_reference_dwim(&mut raw, self.raw(), refname)); |
| 1715 |
Ok(Binding::from_raw(raw)) |
| 1716 |
} |
| 1717 |
} |
| 1718 |
|
| 1719 |
/// Lookup a reference by name and resolve immediately to OID. |
| 1720 |
/// |
| 1721 |
/// This function provides a quick way to resolve a reference name straight |
| 1722 |
/// through to the object id that it refers to. This avoids having to |
| 1723 |
/// allocate or free any `Reference` objects for simple situations. |
| 1724 |
pub fn refname_to_id(&self, name: &str) -> Result<Oid, Error> { |
| 1725 |
let name = CString::new(name)?; |
| 1726 |
let mut ret = crate::util::zeroed_raw_oid(); |
| 1727 |
unsafe { |
| 1728 |
try_call!(raw::git_reference_name_to_id(&mut ret, self.raw(), name)); |
| 1729 |
Ok(Binding::from_raw(&ret as *const _)) |
| 1730 |
} |
| 1731 |
} |
| 1732 |
|
| 1733 |
/// Creates a git_annotated_commit from the given reference. |
| 1734 |
pub fn reference_to_annotated_commit( |
| 1735 |
&self, |
| 1736 |
reference: &Reference<'_>, |
| 1737 |
) -> Result<AnnotatedCommit<'_>, Error> { |
| 1738 |
let mut ret = ptr::null_mut(); |
| 1739 |
unsafe { |
| 1740 |
try_call!(raw::git_annotated_commit_from_ref( |
| 1741 |
&mut ret, |
| 1742 |
self.raw(), |
| 1743 |
reference.raw() |
| 1744 |
)); |
| 1745 |
Ok(AnnotatedCommit::from_raw(ret)) |
| 1746 |
} |
| 1747 |
} |
| 1748 |
|
| 1749 |
/// Creates a git_annotated_commit from FETCH_HEAD. |
| 1750 |
pub fn annotated_commit_from_fetchhead( |
| 1751 |
&self, |
| 1752 |
branch_name: &str, |
| 1753 |
remote_url: &str, |
| 1754 |
id: &Oid, |
| 1755 |
) -> Result<AnnotatedCommit<'_>, Error> { |
| 1756 |
let branch_name = CString::new(branch_name)?; |
| 1757 |
let remote_url = CString::new(remote_url)?; |
| 1758 |
|
| 1759 |
let mut ret = ptr::null_mut(); |
| 1760 |
unsafe { |
| 1761 |
try_call!(raw::git_annotated_commit_from_fetchhead( |
| 1762 |
&mut ret, |
| 1763 |
self.raw(), |
| 1764 |
branch_name, |
| 1765 |
remote_url, |
| 1766 |
id.raw() |
| 1767 |
)); |
| 1768 |
Ok(AnnotatedCommit::from_raw(ret)) |
| 1769 |
} |
| 1770 |
} |
| 1771 |
|
| 1772 |
/// Create a new action signature with default user and now timestamp. |
| 1773 |
/// |
| 1774 |
/// This looks up the user.name and user.email from the configuration and |
| 1775 |
/// uses the current time as the timestamp, and creates a new signature |
| 1776 |
/// based on that information. It will return `NotFound` if either the |
| 1777 |
/// user.name or user.email are not set. |
| 1778 |
/// |
| 1779 |
/// This method does not check any environmental variables; to apply those, |
| 1780 |
/// use [`Repository::author_from_env()`] or |
| 1781 |
/// [`Repository::committer_from_env()`]. |
| 1782 |
pub fn signature(&self) -> Result<Signature<'static>, Error> { |
| 1783 |
let mut ret = ptr::null_mut(); |
| 1784 |
unsafe { |
| 1785 |
try_call!(raw::git_signature_default(&mut ret, self.raw())); |
| 1786 |
Ok(Binding::from_raw(ret)) |
| 1787 |
} |
| 1788 |
} |
| 1789 |
|
| 1790 |
/// Create a new action signature with default user and now timestamp, |
| 1791 |
/// supporting environmental overrides. |
| 1792 |
/// |
| 1793 |
/// The signature name is read from user.name from the configuration, but |
| 1794 |
/// can be overridden with the GIT_AUTHOR_NAME environmental variable. |
| 1795 |
/// |
| 1796 |
/// The signature email is read from user.email from the configuration, but |
| 1797 |
/// can be overridden with the GIT_AUTHOR_EMAIL environmental variable; the |
| 1798 |
/// EMAIL environmental variable is used if *both* GIT_AUTHOR_EMAIL and |
| 1799 |
/// user.email are not set. |
| 1800 |
/// |
| 1801 |
/// The signature time will be the current time, but can be overridden with |
| 1802 |
/// the GIT_AUTHOR_DATE environmental variable. |
| 1803 |
/// |
| 1804 |
/// This method will return `NotFound` if either the user name or email |
| 1805 |
/// cannot be set. |
| 1806 |
pub fn author_from_env(&self) -> Result<Signature<'static>, Error> { |
| 1807 |
let mut ret = ptr::null_mut(); |
| 1808 |
// Marked as mutable for signature support, but isn't going to be used |
| 1809 |
let mut committer = ptr::null_mut(); |
| 1810 |
unsafe { |
| 1811 |
try_call!(raw::git_signature_default_from_env( |
| 1812 |
&mut ret, |
| 1813 |
&mut committer, |
| 1814 |
self.raw() |
| 1815 |
)); |
| 1816 |
Ok(Binding::from_raw(ret)) |
| 1817 |
} |
| 1818 |
} |
| 1819 |
|
| 1820 |
/// Create a new action signature with default user and now timestamp, |
| 1821 |
/// supporting environmental overrides. |
| 1822 |
/// |
| 1823 |
/// The signature name is read from user.name from the configuration, but |
| 1824 |
/// can be overridden with the GIT_COMMITTER_NAME environmental variable. |
| 1825 |
/// |
| 1826 |
/// The signature email is read from user.email from the configuration, but |
| 1827 |
/// can be overridden with the GIT_COMMITTER_EMAIL environmental variable; |
| 1828 |
/// the EMAIL environmental variable is used if *both* GIT_COMMITTER_EMAIL |
| 1829 |
/// and user.email are not set. |
| 1830 |
/// |
| 1831 |
/// The signature time will be the current time, but can be overridden with |
| 1832 |
/// the GIT_COMMITTER_DATE environmental variable. |
| 1833 |
/// |
| 1834 |
/// This method will return `NotFound` if either the user name or email |
| 1835 |
/// cannot be set. |
| 1836 |
pub fn committer_from_env(&self) -> Result<Signature<'static>, Error> { |
| 1837 |
let mut ret = ptr::null_mut(); |
| 1838 |
// Marked as mutable for signature support, but isn't going to be used |
| 1839 |
let mut author = ptr::null_mut(); |
| 1840 |
unsafe { |
| 1841 |
try_call!(raw::git_signature_default_from_env( |
| 1842 |
&mut author, |
| 1843 |
&mut ret, |
| 1844 |
self.raw() |
| 1845 |
)); |
| 1846 |
Ok(Binding::from_raw(ret)) |
| 1847 |
} |
| 1848 |
} |
| 1849 |
|
| 1850 |
/// Set up a new git submodule for checkout. |
| 1851 |
/// |
| 1852 |
/// This does "git submodule add" up to the fetch and checkout of the |
| 1853 |
/// submodule contents. It preps a new submodule, creates an entry in |
| 1854 |
/// `.gitmodules` and creates an empty initialized repository either at the |
| 1855 |
/// given path in the working directory or in `.git/modules` with a gitlink |
| 1856 |
/// from the working directory to the new repo. |
| 1857 |
/// |
| 1858 |
/// To fully emulate "git submodule add" call this function, then `open()` |
| 1859 |
/// the submodule repo and perform the clone step as needed. Lastly, call |
| 1860 |
/// `add_finalize()` to wrap up adding the new submodule and `.gitmodules` |
| 1861 |
/// to the index to be ready to commit. |
| 1862 |
pub fn submodule( |
| 1863 |
&self, |
| 1864 |
url: &str, |
| 1865 |
path: &Path, |
| 1866 |
use_gitlink: bool, |
| 1867 |
) -> Result<Submodule<'_>, Error> { |
| 1868 |
let url = CString::new(url)?; |
| 1869 |
let path = path_to_repo_path(path)?; |
| 1870 |
let mut raw = ptr::null_mut(); |
| 1871 |
unsafe { |
| 1872 |
try_call!(raw::git_submodule_add_setup( |
| 1873 |
&mut raw, |
| 1874 |
self.raw(), |
| 1875 |
url, |
| 1876 |
path, |
| 1877 |
use_gitlink |
| 1878 |
)); |
| 1879 |
Ok(Binding::from_raw(raw)) |
| 1880 |
} |
| 1881 |
} |
| 1882 |
|
| 1883 |
/// Lookup submodule information by name or path. |
| 1884 |
/// |
| 1885 |
/// Given either the submodule name or path (they are usually the same), |
| 1886 |
/// this returns a structure describing the submodule. |
| 1887 |
pub fn find_submodule(&self, name: &str) -> Result<Submodule<'_>, Error> { |
| 1888 |
let name = CString::new(name)?; |
| 1889 |
let mut raw = ptr::null_mut(); |
| 1890 |
unsafe { |
| 1891 |
try_call!(raw::git_submodule_lookup(&mut raw, self.raw(), name)); |
| 1892 |
Ok(Binding::from_raw(raw)) |
| 1893 |
} |
| 1894 |
} |
| 1895 |
|
| 1896 |
/// Get the status for a submodule. |
| 1897 |
/// |
| 1898 |
/// This looks at a submodule and tries to determine the status. It |
| 1899 |
/// will return a combination of the `SubmoduleStatus` values. |
| 1900 |
pub fn submodule_status( |
| 1901 |
&self, |
| 1902 |
name: &str, |
| 1903 |
ignore: SubmoduleIgnore, |
| 1904 |
) -> Result<SubmoduleStatus, Error> { |
| 1905 |
let mut ret = 0; |
| 1906 |
let name = CString::new(name)?; |
| 1907 |
unsafe { |
| 1908 |
try_call!(raw::git_submodule_status(&mut ret, self.raw, name, ignore)); |
| 1909 |
} |
| 1910 |
Ok(SubmoduleStatus::from_bits_truncate(ret as u32)) |
| 1911 |
} |
| 1912 |
|
| 1913 |
/// Set the ignore rule for the submodule in the configuration |
| 1914 |
/// |
| 1915 |
/// This does not affect any currently-loaded instances. |
| 1916 |
pub fn submodule_set_ignore( |
| 1917 |
&mut self, |
| 1918 |
name: &str, |
| 1919 |
ignore: SubmoduleIgnore, |
| 1920 |
) -> Result<(), Error> { |
| 1921 |
let name = CString::new(name)?; |
| 1922 |
unsafe { |
| 1923 |
try_call!(raw::git_submodule_set_ignore(self.raw(), name, ignore)); |
| 1924 |
} |
| 1925 |
Ok(()) |
| 1926 |
} |
| 1927 |
|
| 1928 |
/// Set the update rule for the submodule in the configuration |
| 1929 |
/// |
| 1930 |
/// This setting won't affect any existing instances. |
| 1931 |
pub fn submodule_set_update( |
| 1932 |
&mut self, |
| 1933 |
name: &str, |
| 1934 |
update: SubmoduleUpdate, |
| 1935 |
) -> Result<(), Error> { |
| 1936 |
let name = CString::new(name)?; |
| 1937 |
unsafe { |
| 1938 |
try_call!(raw::git_submodule_set_update(self.raw(), name, update)); |
| 1939 |
} |
| 1940 |
Ok(()) |
| 1941 |
} |
| 1942 |
|
| 1943 |
/// Set the URL for the submodule in the configuration |
| 1944 |
/// |
| 1945 |
/// After calling this, you may wish to call [`Submodule::sync`] to write |
| 1946 |
/// the changes to the checked out submodule repository. |
| 1947 |
pub fn submodule_set_url(&mut self, name: &str, url: &str) -> Result<(), Error> { |
| 1948 |
let name = CString::new(name)?; |
| 1949 |
let url = CString::new(url)?; |
| 1950 |
unsafe { |
| 1951 |
try_call!(raw::git_submodule_set_url(self.raw(), name, url)); |
| 1952 |
} |
| 1953 |
Ok(()) |
| 1954 |
} |
| 1955 |
|
| 1956 |
/// Set the branch for the submodule in the configuration |
| 1957 |
/// |
| 1958 |
/// After calling this, you may wish to call [`Submodule::sync`] to write |
| 1959 |
/// the changes to the checked out submodule repository. |
| 1960 |
pub fn submodule_set_branch(&mut self, name: &str, branch_name: &str) -> Result<(), Error> { |
| 1961 |
let name = CString::new(name)?; |
| 1962 |
let branch_name = CString::new(branch_name)?; |
| 1963 |
unsafe { |
| 1964 |
try_call!(raw::git_submodule_set_branch(self.raw(), name, branch_name)); |
| 1965 |
} |
| 1966 |
Ok(()) |
| 1967 |
} |
| 1968 |
|
| 1969 |
/// Lookup a reference to one of the objects in a repository. |
| 1970 |
pub fn find_tree(&self, oid: Oid) -> Result<Tree<'_>, Error> { |
| 1971 |
let mut raw = ptr::null_mut(); |
| 1972 |
unsafe { |
| 1973 |
try_call!(raw::git_tree_lookup(&mut raw, self.raw(), oid.raw())); |
| 1974 |
Ok(Binding::from_raw(raw)) |
| 1975 |
} |
| 1976 |
} |
| 1977 |
|
| 1978 |
/// Create a new TreeBuilder, optionally initialized with the |
| 1979 |
/// entries of the given Tree. |
| 1980 |
/// |
| 1981 |
/// The tree builder can be used to create or modify trees in memory and |
| 1982 |
/// write them as tree objects to the database. |
| 1983 |
pub fn treebuilder(&self, tree: Option<&Tree<'_>>) -> Result<TreeBuilder<'_>, Error> { |
| 1984 |
unsafe { |
| 1985 |
let mut ret = ptr::null_mut(); |
| 1986 |
let tree = match tree { |
| 1987 |
Some(tree) => tree.raw(), |
| 1988 |
None => ptr::null_mut(), |
| 1989 |
}; |
| 1990 |
try_call!(raw::git_treebuilder_new(&mut ret, self.raw, tree)); |
| 1991 |
Ok(Binding::from_raw(ret)) |
| 1992 |
} |
| 1993 |
} |
| 1994 |
|
| 1995 |
/// Create a new tag in the repository from an object |
| 1996 |
/// |
| 1997 |
/// A new reference will also be created pointing to this tag object. If |
| 1998 |
/// `force` is true and a reference already exists with the given name, |
| 1999 |
/// it'll be replaced. |
| 2000 |
/// |
| 2001 |
/// The message will not be cleaned up. |
| 2002 |
/// |
| 2003 |
/// The tag name will be checked for validity. You must avoid the characters |
| 2004 |
/// '~', '^', ':', ' \ ', '?', '[', and '*', and the sequences ".." and " @ |
| 2005 |
/// {" which have special meaning to revparse. |
| 2006 |
pub fn tag( |
| 2007 |
&self, |
| 2008 |
name: &str, |
| 2009 |
target: &Object<'_>, |
| 2010 |
tagger: &Signature<'_>, |
| 2011 |
message: &str, |
| 2012 |
force: bool, |
| 2013 |
) -> Result<Oid, Error> { |
| 2014 |
let name = CString::new(name)?; |
| 2015 |
let message = CString::new(message)?; |
| 2016 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 2017 |
unsafe { |
| 2018 |
try_call!(raw::git_tag_create( |
| 2019 |
&mut raw, |
| 2020 |
self.raw, |
| 2021 |
name, |
| 2022 |
target.raw(), |
| 2023 |
tagger.raw(), |
| 2024 |
message, |
| 2025 |
force |
| 2026 |
)); |
| 2027 |
Ok(Binding::from_raw(&raw as *const _)) |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
/// Create a new tag in the repository from an object without creating a reference. |
| 2032 |
/// |
| 2033 |
/// The message will not be cleaned up. |
| 2034 |
/// |
| 2035 |
/// The tag name will be checked for validity. You must avoid the characters |
| 2036 |
/// '~', '^', ':', ' \ ', '?', '[', and '*', and the sequences ".." and " @ |
| 2037 |
/// {" which have special meaning to revparse. |
| 2038 |
pub fn tag_annotation_create( |
| 2039 |
&self, |
| 2040 |
name: &str, |
| 2041 |
target: &Object<'_>, |
| 2042 |
tagger: &Signature<'_>, |
| 2043 |
message: &str, |
| 2044 |
) -> Result<Oid, Error> { |
| 2045 |
let name = CString::new(name)?; |
| 2046 |
let message = CString::new(message)?; |
| 2047 |
let mut raw_oid = crate::util::zeroed_raw_oid(); |
| 2048 |
unsafe { |
| 2049 |
try_call!(raw::git_tag_annotation_create( |
| 2050 |
&mut raw_oid, |
| 2051 |
self.raw, |
| 2052 |
name, |
| 2053 |
target.raw(), |
| 2054 |
tagger.raw(), |
| 2055 |
message |
| 2056 |
)); |
| 2057 |
Ok(Binding::from_raw(&raw_oid as *const _)) |
| 2058 |
} |
| 2059 |
} |
| 2060 |
|
| 2061 |
/// Create a new lightweight tag pointing at a target object |
| 2062 |
/// |
| 2063 |
/// A new direct reference will be created pointing to this target object. |
| 2064 |
/// If force is true and a reference already exists with the given name, |
| 2065 |
/// it'll be replaced. |
| 2066 |
pub fn tag_lightweight( |
| 2067 |
&self, |
| 2068 |
name: &str, |
| 2069 |
target: &Object<'_>, |
| 2070 |
force: bool, |
| 2071 |
) -> Result<Oid, Error> { |
| 2072 |
let name = CString::new(name)?; |
| 2073 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 2074 |
unsafe { |
| 2075 |
try_call!(raw::git_tag_create_lightweight( |
| 2076 |
&mut raw, |
| 2077 |
self.raw, |
| 2078 |
name, |
| 2079 |
target.raw(), |
| 2080 |
force |
| 2081 |
)); |
| 2082 |
Ok(Binding::from_raw(&raw as *const _)) |
| 2083 |
} |
| 2084 |
} |
| 2085 |
|
| 2086 |
/// Lookup a tag object from the repository. |
| 2087 |
pub fn find_tag(&self, id: Oid) -> Result<Tag<'_>, Error> { |
| 2088 |
let mut raw = ptr::null_mut(); |
| 2089 |
unsafe { |
| 2090 |
try_call!(raw::git_tag_lookup(&mut raw, self.raw, id.raw())); |
| 2091 |
Ok(Binding::from_raw(raw)) |
| 2092 |
} |
| 2093 |
} |
| 2094 |
|
| 2095 |
/// Lookup a tag object by prefix hash from the repository. |
| 2096 |
pub fn find_tag_by_prefix(&self, prefix_hash: &str) -> Result<Tag<'_>, Error> { |
| 2097 |
let mut raw = ptr::null_mut(); |
| 2098 |
let oid = Oid::from_str_ext(prefix_hash, self.object_format())?; |
| 2099 |
unsafe { |
| 2100 |
try_call!(raw::git_tag_lookup_prefix( |
| 2101 |
&mut raw, |
| 2102 |
self.raw, |
| 2103 |
oid.raw(), |
| 2104 |
prefix_hash.len() |
| 2105 |
)); |
| 2106 |
Ok(Binding::from_raw(raw)) |
| 2107 |
} |
| 2108 |
} |
| 2109 |
|
| 2110 |
/// Delete an existing tag reference. |
| 2111 |
/// |
| 2112 |
/// The tag name will be checked for validity, see `tag` for some rules |
| 2113 |
/// about valid names. |
| 2114 |
pub fn tag_delete(&self, name: &str) -> Result<(), Error> { |
| 2115 |
let name = CString::new(name)?; |
| 2116 |
unsafe { |
| 2117 |
try_call!(raw::git_tag_delete(self.raw, name)); |
| 2118 |
Ok(()) |
| 2119 |
} |
| 2120 |
} |
| 2121 |
|
| 2122 |
/// Get a list with all the tags in the repository. |
| 2123 |
/// |
| 2124 |
/// An optional fnmatch pattern can also be specified. |
| 2125 |
pub fn tag_names(&self, pattern: Option<&str>) -> Result<StringArray, Error> { |
| 2126 |
let mut arr = raw::git_strarray { |
| 2127 |
strings: ptr::null_mut(), |
| 2128 |
count: 0, |
| 2129 |
}; |
| 2130 |
unsafe { |
| 2131 |
match pattern { |
| 2132 |
Some(s) => { |
| 2133 |
let s = CString::new(s)?; |
| 2134 |
try_call!(raw::git_tag_list_match(&mut arr, s, self.raw)); |
| 2135 |
} |
| 2136 |
None => { |
| 2137 |
try_call!(raw::git_tag_list(&mut arr, self.raw)); |
| 2138 |
} |
| 2139 |
} |
| 2140 |
Ok(Binding::from_raw(arr)) |
| 2141 |
} |
| 2142 |
} |
| 2143 |
|
| 2144 |
/// Iterate over all tags, calling the callback `cb` on each. |
| 2145 |
/// The arguments of `cb` are the tag id and name, in this order. |
| 2146 |
/// |
| 2147 |
/// Returning `false` from `cb` causes the iteration to break early. |
| 2148 |
pub fn tag_foreach<T>(&self, cb: T) -> Result<(), Error> |
| 2149 |
where |
| 2150 |
T: FnMut(Oid, &[u8]) -> bool, |
| 2151 |
{ |
| 2152 |
let mut data = TagForeachData { |
| 2153 |
cb: Box::new(cb) as TagForeachCB<'_>, |
| 2154 |
}; |
| 2155 |
|
| 2156 |
unsafe { |
| 2157 |
raw::git_tag_foreach( |
| 2158 |
self.raw, |
| 2159 |
Some(tag_foreach_cb), |
| 2160 |
(&mut data) as *mut _ as *mut _, |
| 2161 |
); |
| 2162 |
} |
| 2163 |
Ok(()) |
| 2164 |
} |
| 2165 |
|
| 2166 |
/// Updates files in the index and the working tree to match the content of |
| 2167 |
/// the commit pointed at by HEAD. |
| 2168 |
/// |
| 2169 |
/// Note that this is _not_ the correct mechanism used to switch branches; |
| 2170 |
/// do not change your `HEAD` and then call this method, that would leave |
| 2171 |
/// you with checkout conflicts since your working directory would then |
| 2172 |
/// appear to be dirty. Instead, checkout the target of the branch and |
| 2173 |
/// then update `HEAD` using [`Repository::set_head`] to point to the |
| 2174 |
/// branch you checked out. |
| 2175 |
pub fn checkout_head(&self, opts: Option<&mut CheckoutBuilder<'_>>) -> Result<(), Error> { |
| 2176 |
unsafe { |
| 2177 |
let mut raw_opts = mem::zeroed(); |
| 2178 |
try_call!(raw::git_checkout_init_options( |
| 2179 |
&mut raw_opts, |
| 2180 |
raw::GIT_CHECKOUT_OPTIONS_VERSION |
| 2181 |
)); |
| 2182 |
if let Some(c) = opts { |
| 2183 |
c.configure(&mut raw_opts); |
| 2184 |
} |
| 2185 |
|
| 2186 |
try_call!(raw::git_checkout_head(self.raw, &raw_opts)); |
| 2187 |
} |
| 2188 |
Ok(()) |
| 2189 |
} |
| 2190 |
|
| 2191 |
/// Updates files in the working tree to match the content of the index. |
| 2192 |
/// |
| 2193 |
/// If the index is `None`, the repository's index will be used. |
| 2194 |
pub fn checkout_index( |
| 2195 |
&self, |
| 2196 |
index: Option<&mut Index>, |
| 2197 |
opts: Option<&mut CheckoutBuilder<'_>>, |
| 2198 |
) -> Result<(), Error> { |
| 2199 |
unsafe { |
| 2200 |
let mut raw_opts = mem::zeroed(); |
| 2201 |
try_call!(raw::git_checkout_init_options( |
| 2202 |
&mut raw_opts, |
| 2203 |
raw::GIT_CHECKOUT_OPTIONS_VERSION |
| 2204 |
)); |
| 2205 |
if let Some(c) = opts { |
| 2206 |
c.configure(&mut raw_opts); |
| 2207 |
} |
| 2208 |
|
| 2209 |
try_call!(raw::git_checkout_index( |
| 2210 |
self.raw, |
| 2211 |
index.map(|i| &mut *i.raw()), |
| 2212 |
&raw_opts |
| 2213 |
)); |
| 2214 |
} |
| 2215 |
Ok(()) |
| 2216 |
} |
| 2217 |
|
| 2218 |
/// Updates files in the index and working tree to match the content of the |
| 2219 |
/// tree pointed at by the treeish. |
| 2220 |
pub fn checkout_tree( |
| 2221 |
&self, |
| 2222 |
treeish: &Object<'_>, |
| 2223 |
opts: Option<&mut CheckoutBuilder<'_>>, |
| 2224 |
) -> Result<(), Error> { |
| 2225 |
unsafe { |
| 2226 |
let mut raw_opts = mem::zeroed(); |
| 2227 |
try_call!(raw::git_checkout_init_options( |
| 2228 |
&mut raw_opts, |
| 2229 |
raw::GIT_CHECKOUT_OPTIONS_VERSION |
| 2230 |
)); |
| 2231 |
if let Some(c) = opts { |
| 2232 |
c.configure(&mut raw_opts); |
| 2233 |
} |
| 2234 |
|
| 2235 |
try_call!(raw::git_checkout_tree(self.raw, &*treeish.raw(), &raw_opts)); |
| 2236 |
} |
| 2237 |
Ok(()) |
| 2238 |
} |
| 2239 |
|
| 2240 |
/// Merges the given commit(s) into HEAD, writing the results into the |
| 2241 |
/// working directory. Any changes are staged for commit and any conflicts |
| 2242 |
/// are written to the index. Callers should inspect the repository's index |
| 2243 |
/// after this completes, resolve any conflicts and prepare a commit. |
| 2244 |
/// |
| 2245 |
/// For compatibility with git, the repository is put into a merging state. |
| 2246 |
/// Once the commit is done (or if the user wishes to abort), you should |
| 2247 |
/// clear this state by calling [`cleanup_state()`][Repository::cleanup_state]. |
| 2248 |
pub fn merge( |
| 2249 |
&self, |
| 2250 |
annotated_commits: &[&AnnotatedCommit<'_>], |
| 2251 |
merge_opts: Option<&mut MergeOptions>, |
| 2252 |
checkout_opts: Option<&mut CheckoutBuilder<'_>>, |
| 2253 |
) -> Result<(), Error> { |
| 2254 |
unsafe { |
| 2255 |
let mut raw_checkout_opts = mem::zeroed(); |
| 2256 |
try_call!(raw::git_checkout_init_options( |
| 2257 |
&mut raw_checkout_opts, |
| 2258 |
raw::GIT_CHECKOUT_OPTIONS_VERSION |
| 2259 |
)); |
| 2260 |
if let Some(c) = checkout_opts { |
| 2261 |
c.configure(&mut raw_checkout_opts); |
| 2262 |
} |
| 2263 |
|
| 2264 |
let mut commit_ptrs = annotated_commits |
| 2265 |
.iter() |
| 2266 |
.map(|c| c.raw() as *const raw::git_annotated_commit) |
| 2267 |
.collect::<Vec<_>>(); |
| 2268 |
|
| 2269 |
try_call!(raw::git_merge( |
| 2270 |
self.raw, |
| 2271 |
commit_ptrs.as_mut_ptr(), |
| 2272 |
annotated_commits.len() as size_t, |
| 2273 |
merge_opts.map(|o| o.raw()).unwrap_or(ptr::null()), |
| 2274 |
&raw_checkout_opts |
| 2275 |
)); |
| 2276 |
} |
| 2277 |
Ok(()) |
| 2278 |
} |
| 2279 |
|
| 2280 |
/// Merge two commits, producing an index that reflects the result of |
| 2281 |
/// the merge. The index may be written as-is to the working directory or |
| 2282 |
/// checked out. If the index is to be converted to a tree, the caller |
| 2283 |
/// should resolve any conflicts that arose as part of the merge. |
| 2284 |
pub fn merge_commits( |
| 2285 |
&self, |
| 2286 |
our_commit: &Commit<'_>, |
| 2287 |
their_commit: &Commit<'_>, |
| 2288 |
opts: Option<&MergeOptions>, |
| 2289 |
) -> Result<Index, Error> { |
| 2290 |
let mut raw = ptr::null_mut(); |
| 2291 |
unsafe { |
| 2292 |
try_call!(raw::git_merge_commits( |
| 2293 |
&mut raw, |
| 2294 |
self.raw, |
| 2295 |
our_commit.raw(), |
| 2296 |
their_commit.raw(), |
| 2297 |
opts.map(|o| o.raw()) |
| 2298 |
)); |
| 2299 |
Ok(Binding::from_raw(raw)) |
| 2300 |
} |
| 2301 |
} |
| 2302 |
|
| 2303 |
/// Merge two trees, producing an index that reflects the result of |
| 2304 |
/// the merge. The index may be written as-is to the working directory or |
| 2305 |
/// checked out. If the index is to be converted to a tree, the caller |
| 2306 |
/// should resolve any conflicts that arose as part of the merge. |
| 2307 |
pub fn merge_trees( |
| 2308 |
&self, |
| 2309 |
ancestor_tree: &Tree<'_>, |
| 2310 |
our_tree: &Tree<'_>, |
| 2311 |
their_tree: &Tree<'_>, |
| 2312 |
opts: Option<&MergeOptions>, |
| 2313 |
) -> Result<Index, Error> { |
| 2314 |
let mut raw = ptr::null_mut(); |
| 2315 |
unsafe { |
| 2316 |
try_call!(raw::git_merge_trees( |
| 2317 |
&mut raw, |
| 2318 |
self.raw, |
| 2319 |
ancestor_tree.raw(), |
| 2320 |
our_tree.raw(), |
| 2321 |
their_tree.raw(), |
| 2322 |
opts.map(|o| o.raw()) |
| 2323 |
)); |
| 2324 |
Ok(Binding::from_raw(raw)) |
| 2325 |
} |
| 2326 |
} |
| 2327 |
|
| 2328 |
/// Remove all the metadata associated with an ongoing command like merge, |
| 2329 |
/// revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc. |
| 2330 |
pub fn cleanup_state(&self) -> Result<(), Error> { |
| 2331 |
unsafe { |
| 2332 |
try_call!(raw::git_repository_state_cleanup(self.raw)); |
| 2333 |
} |
| 2334 |
Ok(()) |
| 2335 |
} |
| 2336 |
|
| 2337 |
/// Analyzes the given branch(es) and determines the opportunities for |
| 2338 |
/// merging them into the HEAD of the repository. |
| 2339 |
pub fn merge_analysis( |
| 2340 |
&self, |
| 2341 |
their_heads: &[&AnnotatedCommit<'_>], |
| 2342 |
) -> Result<(MergeAnalysis, MergePreference), Error> { |
| 2343 |
unsafe { |
| 2344 |
let mut raw_merge_analysis = 0 as raw::git_merge_analysis_t; |
| 2345 |
let mut raw_merge_preference = 0 as raw::git_merge_preference_t; |
| 2346 |
let mut their_heads = their_heads |
| 2347 |
.iter() |
| 2348 |
.map(|v| v.raw() as *const _) |
| 2349 |
.collect::<Vec<_>>(); |
| 2350 |
try_call!(raw::git_merge_analysis( |
| 2351 |
&mut raw_merge_analysis, |
| 2352 |
&mut raw_merge_preference, |
| 2353 |
self.raw, |
| 2354 |
their_heads.as_mut_ptr() as *mut _, |
| 2355 |
their_heads.len() |
| 2356 |
)); |
| 2357 |
Ok(( |
| 2358 |
MergeAnalysis::from_bits_truncate(raw_merge_analysis as u32), |
| 2359 |
MergePreference::from_bits_truncate(raw_merge_preference as u32), |
| 2360 |
)) |
| 2361 |
} |
| 2362 |
} |
| 2363 |
|
| 2364 |
/// Analyzes the given branch(es) and determines the opportunities for |
| 2365 |
/// merging them into a reference. |
| 2366 |
pub fn merge_analysis_for_ref( |
| 2367 |
&self, |
| 2368 |
our_ref: &Reference<'_>, |
| 2369 |
their_heads: &[&AnnotatedCommit<'_>], |
| 2370 |
) -> Result<(MergeAnalysis, MergePreference), Error> { |
| 2371 |
unsafe { |
| 2372 |
let mut raw_merge_analysis = 0 as raw::git_merge_analysis_t; |
| 2373 |
let mut raw_merge_preference = 0 as raw::git_merge_preference_t; |
| 2374 |
let mut their_heads = their_heads |
| 2375 |
.iter() |
| 2376 |
.map(|v| v.raw() as *const _) |
| 2377 |
.collect::<Vec<_>>(); |
| 2378 |
try_call!(raw::git_merge_analysis_for_ref( |
| 2379 |
&mut raw_merge_analysis, |
| 2380 |
&mut raw_merge_preference, |
| 2381 |
self.raw, |
| 2382 |
our_ref.raw(), |
| 2383 |
their_heads.as_mut_ptr() as *mut _, |
| 2384 |
their_heads.len() |
| 2385 |
)); |
| 2386 |
Ok(( |
| 2387 |
MergeAnalysis::from_bits_truncate(raw_merge_analysis as u32), |
| 2388 |
MergePreference::from_bits_truncate(raw_merge_preference as u32), |
| 2389 |
)) |
| 2390 |
} |
| 2391 |
} |
| 2392 |
|
| 2393 |
/// Initializes a rebase operation to rebase the changes in `branch` |
| 2394 |
/// relative to `upstream` onto another branch. To begin the rebase process, |
| 2395 |
/// call `next()`. |
| 2396 |
pub fn rebase( |
| 2397 |
&self, |
| 2398 |
branch: Option<&AnnotatedCommit<'_>>, |
| 2399 |
upstream: Option<&AnnotatedCommit<'_>>, |
| 2400 |
onto: Option<&AnnotatedCommit<'_>>, |
| 2401 |
opts: Option<&mut RebaseOptions<'_>>, |
| 2402 |
) -> Result<Rebase<'_>, Error> { |
| 2403 |
let mut rebase: *mut raw::git_rebase = ptr::null_mut(); |
| 2404 |
unsafe { |
| 2405 |
try_call!(raw::git_rebase_init( |
| 2406 |
&mut rebase, |
| 2407 |
self.raw(), |
| 2408 |
branch.map(|c| c.raw()), |
| 2409 |
upstream.map(|c| c.raw()), |
| 2410 |
onto.map(|c| c.raw()), |
| 2411 |
opts.map(|o| o.raw()).unwrap_or(ptr::null()) |
| 2412 |
)); |
| 2413 |
|
| 2414 |
Ok(Rebase::from_raw(rebase)) |
| 2415 |
} |
| 2416 |
} |
| 2417 |
|
| 2418 |
/// Opens an existing rebase that was previously started by either an |
| 2419 |
/// invocation of `rebase()` or by another client. |
| 2420 |
pub fn open_rebase(&self, opts: Option<&mut RebaseOptions<'_>>) -> Result<Rebase<'_>, Error> { |
| 2421 |
let mut rebase: *mut raw::git_rebase = ptr::null_mut(); |
| 2422 |
unsafe { |
| 2423 |
try_call!(raw::git_rebase_open( |
| 2424 |
&mut rebase, |
| 2425 |
self.raw(), |
| 2426 |
opts.map(|o| o.raw()).unwrap_or(ptr::null()) |
| 2427 |
)); |
| 2428 |
Ok(Rebase::from_raw(rebase)) |
| 2429 |
} |
| 2430 |
} |
| 2431 |
|
| 2432 |
/// Add a note for an object |
| 2433 |
/// |
| 2434 |
/// The `notes_ref` argument is the canonical name of the reference to use, |
| 2435 |
/// defaulting to "refs/notes/commits". If `force` is specified then |
| 2436 |
/// previous notes are overwritten. |
| 2437 |
pub fn note( |
| 2438 |
&self, |
| 2439 |
author: &Signature<'_>, |
| 2440 |
committer: &Signature<'_>, |
| 2441 |
notes_ref: Option<&str>, |
| 2442 |
oid: Oid, |
| 2443 |
note: &str, |
| 2444 |
force: bool, |
| 2445 |
) -> Result<Oid, Error> { |
| 2446 |
let notes_ref = crate::opt_cstr(notes_ref)?; |
| 2447 |
let note = CString::new(note)?; |
| 2448 |
let mut ret = crate::util::zeroed_raw_oid(); |
| 2449 |
unsafe { |
| 2450 |
try_call!(raw::git_note_create( |
| 2451 |
&mut ret, |
| 2452 |
self.raw, |
| 2453 |
notes_ref, |
| 2454 |
author.raw(), |
| 2455 |
committer.raw(), |
| 2456 |
oid.raw(), |
| 2457 |
note, |
| 2458 |
force |
| 2459 |
)); |
| 2460 |
Ok(Binding::from_raw(&ret as *const _)) |
| 2461 |
} |
| 2462 |
} |
| 2463 |
|
| 2464 |
/// Get the default notes reference for this repository |
| 2465 |
pub fn note_default_ref(&self) -> Result<String, Error> { |
| 2466 |
let ret = Buf::new(); |
| 2467 |
unsafe { |
| 2468 |
try_call!(raw::git_note_default_ref(ret.raw(), self.raw)); |
| 2469 |
} |
| 2470 |
Ok(str::from_utf8(&ret).unwrap().to_string()) |
| 2471 |
} |
| 2472 |
|
| 2473 |
/// Creates a new iterator for notes in this repository. |
| 2474 |
/// |
| 2475 |
/// The `notes_ref` argument is the canonical name of the reference to use, |
| 2476 |
/// defaulting to "refs/notes/commits". |
| 2477 |
/// |
| 2478 |
/// The iterator returned yields pairs of (Oid, Oid) where the first element |
| 2479 |
/// is the id of the note and the second id is the id the note is |
| 2480 |
/// annotating. |
| 2481 |
pub fn notes(&self, notes_ref: Option<&str>) -> Result<Notes<'_>, Error> { |
| 2482 |
let notes_ref = crate::opt_cstr(notes_ref)?; |
| 2483 |
let mut ret = ptr::null_mut(); |
| 2484 |
unsafe { |
| 2485 |
try_call!(raw::git_note_iterator_new(&mut ret, self.raw, notes_ref)); |
| 2486 |
Ok(Binding::from_raw(ret)) |
| 2487 |
} |
| 2488 |
} |
| 2489 |
|
| 2490 |
/// Read the note for an object. |
| 2491 |
/// |
| 2492 |
/// The `notes_ref` argument is the canonical name of the reference to use, |
| 2493 |
/// defaulting to "refs/notes/commits". |
| 2494 |
/// |
| 2495 |
/// The id specified is the Oid of the git object to read the note from. |
| 2496 |
pub fn find_note(&self, notes_ref: Option<&str>, id: Oid) -> Result<Note<'_>, Error> { |
| 2497 |
let notes_ref = crate::opt_cstr(notes_ref)?; |
| 2498 |
let mut ret = ptr::null_mut(); |
| 2499 |
unsafe { |
| 2500 |
try_call!(raw::git_note_read(&mut ret, self.raw, notes_ref, id.raw())); |
| 2501 |
Ok(Binding::from_raw(ret)) |
| 2502 |
} |
| 2503 |
} |
| 2504 |
|
| 2505 |
/// Remove the note for an object. |
| 2506 |
/// |
| 2507 |
/// The `notes_ref` argument is the canonical name of the reference to use, |
| 2508 |
/// defaulting to "refs/notes/commits". |
| 2509 |
/// |
| 2510 |
/// The id specified is the Oid of the git object to remove the note from. |
| 2511 |
pub fn note_delete( |
| 2512 |
&self, |
| 2513 |
id: Oid, |
| 2514 |
notes_ref: Option<&str>, |
| 2515 |
author: &Signature<'_>, |
| 2516 |
committer: &Signature<'_>, |
| 2517 |
) -> Result<(), Error> { |
| 2518 |
let notes_ref = crate::opt_cstr(notes_ref)?; |
| 2519 |
unsafe { |
| 2520 |
try_call!(raw::git_note_remove( |
| 2521 |
self.raw, |
| 2522 |
notes_ref, |
| 2523 |
author.raw(), |
| 2524 |
committer.raw(), |
| 2525 |
id.raw() |
| 2526 |
)); |
| 2527 |
Ok(()) |
| 2528 |
} |
| 2529 |
} |
| 2530 |
|
| 2531 |
/// Create a revwalk that can be used to traverse the commit graph. |
| 2532 |
pub fn revwalk(&self) -> Result<Revwalk<'_>, Error> { |
| 2533 |
let mut raw = ptr::null_mut(); |
| 2534 |
unsafe { |
| 2535 |
try_call!(raw::git_revwalk_new(&mut raw, self.raw())); |
| 2536 |
Ok(Binding::from_raw(raw)) |
| 2537 |
} |
| 2538 |
} |
| 2539 |
|
| 2540 |
/// Get the blame for a single file. |
| 2541 |
pub fn blame_file( |
| 2542 |
&self, |
| 2543 |
path: &Path, |
| 2544 |
opts: Option<&mut BlameOptions>, |
| 2545 |
) -> Result<Blame<'_>, Error> { |
| 2546 |
let path = path_to_repo_path(path)?; |
| 2547 |
let mut raw = ptr::null_mut(); |
| 2548 |
|
| 2549 |
unsafe { |
| 2550 |
try_call!(raw::git_blame_file( |
| 2551 |
&mut raw, |
| 2552 |
self.raw(), |
| 2553 |
path, |
| 2554 |
opts.map(|s| s.raw()) |
| 2555 |
)); |
| 2556 |
Ok(Binding::from_raw(raw)) |
| 2557 |
} |
| 2558 |
} |
| 2559 |
|
| 2560 |
/// Find a merge base between two commits |
| 2561 |
pub fn merge_base(&self, one: Oid, two: Oid) -> Result<Oid, Error> { |
| 2562 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 2563 |
unsafe { |
| 2564 |
try_call!(raw::git_merge_base( |
| 2565 |
&mut raw, |
| 2566 |
self.raw, |
| 2567 |
one.raw(), |
| 2568 |
two.raw() |
| 2569 |
)); |
| 2570 |
Ok(Binding::from_raw(&raw as *const _)) |
| 2571 |
} |
| 2572 |
} |
| 2573 |
|
| 2574 |
/// Find a merge base given a list of commits |
| 2575 |
/// |
| 2576 |
/// This behaves similar to [`git merge-base`](https://git-scm.com/docs/git-merge-base#_discussion). |
| 2577 |
/// Given three commits `a`, `b`, and `c`, `merge_base_many(&[a, b, c])` |
| 2578 |
/// will compute a hypothetical commit `m`, which is a merge between `b` |
| 2579 |
/// and `c`. |
| 2580 |
/// |
| 2581 |
/// For example, with the following topology: |
| 2582 |
/// ```text |
| 2583 |
/// o---o---o---o---C |
| 2584 |
/// / |
| 2585 |
/// / o---o---o---B |
| 2586 |
/// / / |
| 2587 |
/// ---2---1---o---o---o---A |
| 2588 |
/// ``` |
| 2589 |
/// |
| 2590 |
/// the result of `merge_base_many(&[a, b, c])` is 1. This is because the |
| 2591 |
/// equivalent topology with a merge commit `m` between `b` and `c` would |
| 2592 |
/// is: |
| 2593 |
/// ```text |
| 2594 |
/// o---o---o---o---o |
| 2595 |
/// / \ |
| 2596 |
/// / o---o---o---o---M |
| 2597 |
/// / / |
| 2598 |
/// ---2---1---o---o---o---A |
| 2599 |
/// ``` |
| 2600 |
/// |
| 2601 |
/// and the result of `merge_base_many(&[a, m])` is 1. |
| 2602 |
/// |
| 2603 |
/// --- |
| 2604 |
/// |
| 2605 |
/// If you're looking to recieve the common merge base between all the |
| 2606 |
/// given commits, use [`Self::merge_base_octopus`]. |
| 2607 |
pub fn merge_base_many(&self, oids: &[Oid]) -> Result<Oid, Error> { |
| 2608 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 2609 |
|
| 2610 |
unsafe { |
| 2611 |
try_call!(raw::git_merge_base_many( |
| 2612 |
&mut raw, |
| 2613 |
self.raw, |
| 2614 |
oids.len() as size_t, |
| 2615 |
oids.as_ptr() as *const raw::git_oid |
| 2616 |
)); |
| 2617 |
Ok(Binding::from_raw(&raw as *const _)) |
| 2618 |
} |
| 2619 |
} |
| 2620 |
|
| 2621 |
/// Find a common merge base between all given a list of commits |
| 2622 |
pub fn merge_base_octopus(&self, oids: &[Oid]) -> Result<Oid, Error> { |
| 2623 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 2624 |
|
| 2625 |
unsafe { |
| 2626 |
try_call!(raw::git_merge_base_octopus( |
| 2627 |
&mut raw, |
| 2628 |
self.raw, |
| 2629 |
oids.len() as size_t, |
| 2630 |
oids.as_ptr() as *const raw::git_oid |
| 2631 |
)); |
| 2632 |
Ok(Binding::from_raw(&raw as *const _)) |
| 2633 |
} |
| 2634 |
} |
| 2635 |
|
| 2636 |
/// Find all merge bases between two commits |
| 2637 |
pub fn merge_bases(&self, one: Oid, two: Oid) -> Result<OidArray, Error> { |
| 2638 |
let mut arr = raw::git_oidarray { |
| 2639 |
ids: ptr::null_mut(), |
| 2640 |
count: 0, |
| 2641 |
}; |
| 2642 |
unsafe { |
| 2643 |
try_call!(raw::git_merge_bases( |
| 2644 |
&mut arr, |
| 2645 |
self.raw, |
| 2646 |
one.raw(), |
| 2647 |
two.raw() |
| 2648 |
)); |
| 2649 |
Ok(Binding::from_raw(arr)) |
| 2650 |
} |
| 2651 |
} |
| 2652 |
|
| 2653 |
/// Find all merge bases given a list of commits |
| 2654 |
pub fn merge_bases_many(&self, oids: &[Oid]) -> Result<OidArray, Error> { |
| 2655 |
let mut arr = raw::git_oidarray { |
| 2656 |
ids: ptr::null_mut(), |
| 2657 |
count: 0, |
| 2658 |
}; |
| 2659 |
unsafe { |
| 2660 |
try_call!(raw::git_merge_bases_many( |
| 2661 |
&mut arr, |
| 2662 |
self.raw, |
| 2663 |
oids.len() as size_t, |
| 2664 |
oids.as_ptr() as *const raw::git_oid |
| 2665 |
)); |
| 2666 |
Ok(Binding::from_raw(arr)) |
| 2667 |
} |
| 2668 |
} |
| 2669 |
|
| 2670 |
/// Merge two files as they exist in the index, using the given common ancestor |
| 2671 |
/// as the baseline. |
| 2672 |
pub fn merge_file_from_index( |
| 2673 |
&self, |
| 2674 |
ancestor: &IndexEntry, |
| 2675 |
ours: &IndexEntry, |
| 2676 |
theirs: &IndexEntry, |
| 2677 |
opts: Option<&mut MergeFileOptions>, |
| 2678 |
) -> Result<MergeFileResult, Error> { |
| 2679 |
unsafe { |
| 2680 |
let (ancestor, _ancestor_path) = ancestor.to_raw()?; |
| 2681 |
let (ours, _ours_path) = ours.to_raw()?; |
| 2682 |
let (theirs, _theirs_path) = theirs.to_raw()?; |
| 2683 |
|
| 2684 |
let mut ret = mem::zeroed(); |
| 2685 |
try_call!(raw::git_merge_file_from_index( |
| 2686 |
&mut ret, |
| 2687 |
self.raw(), |
| 2688 |
&ancestor, |
| 2689 |
&ours, |
| 2690 |
&theirs, |
| 2691 |
opts.map(|o| o.raw()).unwrap_or(ptr::null()) |
| 2692 |
)); |
| 2693 |
Ok(Binding::from_raw(ret)) |
| 2694 |
} |
| 2695 |
} |
| 2696 |
|
| 2697 |
/// Count the number of unique commits between two commit objects |
| 2698 |
/// |
| 2699 |
/// There is no need for branches containing the commits to have any |
| 2700 |
/// upstream relationship, but it helps to think of one as a branch and the |
| 2701 |
/// other as its upstream, the ahead and behind values will be what git |
| 2702 |
/// would report for the branches. |
| 2703 |
pub fn graph_ahead_behind(&self, local: Oid, upstream: Oid) -> Result<(usize, usize), Error> { |
| 2704 |
unsafe { |
| 2705 |
let mut ahead: size_t = 0; |
| 2706 |
let mut behind: size_t = 0; |
| 2707 |
try_call!(raw::git_graph_ahead_behind( |
| 2708 |
&mut ahead, |
| 2709 |
&mut behind, |
| 2710 |
self.raw(), |
| 2711 |
local.raw(), |
| 2712 |
upstream.raw() |
| 2713 |
)); |
| 2714 |
Ok((ahead as usize, behind as usize)) |
| 2715 |
} |
| 2716 |
} |
| 2717 |
|
| 2718 |
/// Determine if a commit is the descendant of another commit |
| 2719 |
/// |
| 2720 |
/// Note that a commit is not considered a descendant of itself, in contrast |
| 2721 |
/// to `git merge-base --is-ancestor`. |
| 2722 |
pub fn graph_descendant_of(&self, commit: Oid, ancestor: Oid) -> Result<bool, Error> { |
| 2723 |
unsafe { |
| 2724 |
let rv = try_call!(raw::git_graph_descendant_of( |
| 2725 |
self.raw(), |
| 2726 |
commit.raw(), |
| 2727 |
ancestor.raw() |
| 2728 |
)); |
| 2729 |
Ok(rv != 0) |
| 2730 |
} |
| 2731 |
} |
| 2732 |
|
| 2733 |
/// Read the reflog for the given reference |
| 2734 |
/// |
| 2735 |
/// If there is no reflog file for the given reference yet, an empty reflog |
| 2736 |
/// object will be returned. |
| 2737 |
pub fn reflog(&self, name: &str) -> Result<Reflog, Error> { |
| 2738 |
let name = CString::new(name)?; |
| 2739 |
let mut ret = ptr::null_mut(); |
| 2740 |
unsafe { |
| 2741 |
try_call!(raw::git_reflog_read(&mut ret, self.raw, name)); |
| 2742 |
Ok(Binding::from_raw(ret)) |
| 2743 |
} |
| 2744 |
} |
| 2745 |
|
| 2746 |
/// Delete the reflog for the given reference |
| 2747 |
pub fn reflog_delete(&self, name: &str) -> Result<(), Error> { |
| 2748 |
let name = CString::new(name)?; |
| 2749 |
unsafe { |
| 2750 |
try_call!(raw::git_reflog_delete(self.raw, name)); |
| 2751 |
} |
| 2752 |
Ok(()) |
| 2753 |
} |
| 2754 |
|
| 2755 |
/// Rename a reflog |
| 2756 |
/// |
| 2757 |
/// The reflog to be renamed is expected to already exist. |
| 2758 |
pub fn reflog_rename(&self, old_name: &str, new_name: &str) -> Result<(), Error> { |
| 2759 |
let old_name = CString::new(old_name)?; |
| 2760 |
let new_name = CString::new(new_name)?; |
| 2761 |
unsafe { |
| 2762 |
try_call!(raw::git_reflog_rename(self.raw, old_name, new_name)); |
| 2763 |
} |
| 2764 |
Ok(()) |
| 2765 |
} |
| 2766 |
|
| 2767 |
/// Check if the given reference has a reflog. |
| 2768 |
pub fn reference_has_log(&self, name: &str) -> Result<bool, Error> { |
| 2769 |
let name = CString::new(name)?; |
| 2770 |
let ret = unsafe { try_call!(raw::git_reference_has_log(self.raw, name)) }; |
| 2771 |
Ok(ret != 0) |
| 2772 |
} |
| 2773 |
|
| 2774 |
/// Ensure that the given reference has a reflog. |
| 2775 |
pub fn reference_ensure_log(&self, name: &str) -> Result<(), Error> { |
| 2776 |
let name = CString::new(name)?; |
| 2777 |
unsafe { |
| 2778 |
try_call!(raw::git_reference_ensure_log(self.raw, name)); |
| 2779 |
} |
| 2780 |
Ok(()) |
| 2781 |
} |
| 2782 |
|
| 2783 |
/// Describes a commit |
| 2784 |
/// |
| 2785 |
/// Performs a describe operation on the current commit and the worktree. |
| 2786 |
/// After performing a describe on HEAD, a status is run and description is |
| 2787 |
/// considered to be dirty if there are. |
| 2788 |
pub fn describe(&self, opts: &DescribeOptions) -> Result<Describe<'_>, Error> { |
| 2789 |
let mut ret = ptr::null_mut(); |
| 2790 |
unsafe { |
| 2791 |
try_call!(raw::git_describe_workdir(&mut ret, self.raw, opts.raw())); |
| 2792 |
Ok(Binding::from_raw(ret)) |
| 2793 |
} |
| 2794 |
} |
| 2795 |
|
| 2796 |
/// Directly run a diff on two blobs. |
| 2797 |
/// |
| 2798 |
/// Compared to a file, a blob lacks some contextual information. As such, the |
| 2799 |
/// `DiffFile` given to the callback will have some fake data; i.e. mode will be |
| 2800 |
/// 0 and path will be `None`. |
| 2801 |
/// |
| 2802 |
/// `None` is allowed for either `old_blob` or `new_blob` and will be treated |
| 2803 |
/// as an empty blob, with the oid set to zero in the `DiffFile`. Passing `None` |
| 2804 |
/// for both blobs is a noop; no callbacks will be made at all. |
| 2805 |
/// |
| 2806 |
/// We do run a binary content check on the blob content and if either blob looks |
| 2807 |
/// like binary data, the `DiffFile` binary attribute will be set to 1 and no call to |
| 2808 |
/// the `hunk_cb` nor `line_cb` will be made (unless you set the `force_text` |
| 2809 |
/// option). |
| 2810 |
pub fn diff_blobs( |
| 2811 |
&self, |
| 2812 |
old_blob: Option<&Blob<'_>>, |
| 2813 |
old_as_path: Option<&str>, |
| 2814 |
new_blob: Option<&Blob<'_>>, |
| 2815 |
new_as_path: Option<&str>, |
| 2816 |
opts: Option<&mut DiffOptions>, |
| 2817 |
file_cb: Option<&mut FileCb<'_>>, |
| 2818 |
binary_cb: Option<&mut BinaryCb<'_>>, |
| 2819 |
hunk_cb: Option<&mut HunkCb<'_>>, |
| 2820 |
line_cb: Option<&mut LineCb<'_>>, |
| 2821 |
) -> Result<(), Error> { |
| 2822 |
let old_as_path = crate::opt_cstr(old_as_path)?; |
| 2823 |
let new_as_path = crate::opt_cstr(new_as_path)?; |
| 2824 |
let mut cbs = DiffCallbacks { |
| 2825 |
file: file_cb, |
| 2826 |
binary: binary_cb, |
| 2827 |
hunk: hunk_cb, |
| 2828 |
line: line_cb, |
| 2829 |
}; |
| 2830 |
let ptr = &mut cbs as *mut _; |
| 2831 |
unsafe { |
| 2832 |
let file_cb_c: raw::git_diff_file_cb = if cbs.file.is_some() { |
| 2833 |
Some(file_cb_c) |
| 2834 |
} else { |
| 2835 |
None |
| 2836 |
}; |
| 2837 |
let binary_cb_c: raw::git_diff_binary_cb = if cbs.binary.is_some() { |
| 2838 |
Some(binary_cb_c) |
| 2839 |
} else { |
| 2840 |
None |
| 2841 |
}; |
| 2842 |
let hunk_cb_c: raw::git_diff_hunk_cb = if cbs.hunk.is_some() { |
| 2843 |
Some(hunk_cb_c) |
| 2844 |
} else { |
| 2845 |
None |
| 2846 |
}; |
| 2847 |
let line_cb_c: raw::git_diff_line_cb = if cbs.line.is_some() { |
| 2848 |
Some(line_cb_c) |
| 2849 |
} else { |
| 2850 |
None |
| 2851 |
}; |
| 2852 |
try_call!(raw::git_diff_blobs( |
| 2853 |
old_blob.map(|s| s.raw()), |
| 2854 |
old_as_path, |
| 2855 |
new_blob.map(|s| s.raw()), |
| 2856 |
new_as_path, |
| 2857 |
opts.map(|s| s.raw()), |
| 2858 |
file_cb_c, |
| 2859 |
binary_cb_c, |
| 2860 |
hunk_cb_c, |
| 2861 |
line_cb_c, |
| 2862 |
ptr as *mut _ |
| 2863 |
)); |
| 2864 |
Ok(()) |
| 2865 |
} |
| 2866 |
} |
| 2867 |
|
| 2868 |
/// Create a diff with the difference between two tree objects. |
| 2869 |
/// |
| 2870 |
/// This is equivalent to `git diff <old-tree> <new-tree>` |
| 2871 |
/// |
| 2872 |
/// The first tree will be used for the "old_file" side of the delta and the |
| 2873 |
/// second tree will be used for the "new_file" side of the delta. You can |
| 2874 |
/// pass `None` to indicate an empty tree, although it is an error to pass |
| 2875 |
/// `None` for both the `old_tree` and `new_tree`. |
| 2876 |
pub fn diff_tree_to_tree( |
| 2877 |
&self, |
| 2878 |
old_tree: Option<&Tree<'_>>, |
| 2879 |
new_tree: Option<&Tree<'_>>, |
| 2880 |
opts: Option<&mut DiffOptions>, |
| 2881 |
) -> Result<Diff<'_>, Error> { |
| 2882 |
let mut ret = ptr::null_mut(); |
| 2883 |
unsafe { |
| 2884 |
try_call!(raw::git_diff_tree_to_tree( |
| 2885 |
&mut ret, |
| 2886 |
self.raw(), |
| 2887 |
old_tree.map(|s| s.raw()), |
| 2888 |
new_tree.map(|s| s.raw()), |
| 2889 |
opts.map(|s| s.raw()) |
| 2890 |
)); |
| 2891 |
Ok(Binding::from_raw(ret)) |
| 2892 |
} |
| 2893 |
} |
| 2894 |
|
| 2895 |
/// Create a diff between a tree and repository index. |
| 2896 |
/// |
| 2897 |
/// This is equivalent to `git diff --cached <treeish>` or if you pass |
| 2898 |
/// the HEAD tree, then like `git diff --cached`. |
| 2899 |
/// |
| 2900 |
/// The tree you pass will be used for the "old_file" side of the delta, and |
| 2901 |
/// the index will be used for the "new_file" side of the delta. |
| 2902 |
/// |
| 2903 |
/// If you pass `None` for the index, then the existing index of the `repo` |
| 2904 |
/// will be used. In this case, the index will be refreshed from disk |
| 2905 |
/// (if it has changed) before the diff is generated. |
| 2906 |
/// |
| 2907 |
/// If the tree is `None`, then it is considered an empty tree. |
| 2908 |
pub fn diff_tree_to_index( |
| 2909 |
&self, |
| 2910 |
old_tree: Option<&Tree<'_>>, |
| 2911 |
index: Option<&Index>, |
| 2912 |
opts: Option<&mut DiffOptions>, |
| 2913 |
) -> Result<Diff<'_>, Error> { |
| 2914 |
let mut ret = ptr::null_mut(); |
| 2915 |
unsafe { |
| 2916 |
try_call!(raw::git_diff_tree_to_index( |
| 2917 |
&mut ret, |
| 2918 |
self.raw(), |
| 2919 |
old_tree.map(|s| s.raw()), |
| 2920 |
index.map(|s| s.raw()), |
| 2921 |
opts.map(|s| s.raw()) |
| 2922 |
)); |
| 2923 |
Ok(Binding::from_raw(ret)) |
| 2924 |
} |
| 2925 |
} |
| 2926 |
|
| 2927 |
/// Create a diff between two index objects. |
| 2928 |
/// |
| 2929 |
/// The first index will be used for the "old_file" side of the delta, and |
| 2930 |
/// the second index will be used for the "new_file" side of the delta. |
| 2931 |
pub fn diff_index_to_index( |
| 2932 |
&self, |
| 2933 |
old_index: &Index, |
| 2934 |
new_index: &Index, |
| 2935 |
opts: Option<&mut DiffOptions>, |
| 2936 |
) -> Result<Diff<'_>, Error> { |
| 2937 |
let mut ret = ptr::null_mut(); |
| 2938 |
unsafe { |
| 2939 |
try_call!(raw::git_diff_index_to_index( |
| 2940 |
&mut ret, |
| 2941 |
self.raw(), |
| 2942 |
old_index.raw(), |
| 2943 |
new_index.raw(), |
| 2944 |
opts.map(|s| s.raw()) |
| 2945 |
)); |
| 2946 |
Ok(Binding::from_raw(ret)) |
| 2947 |
} |
| 2948 |
} |
| 2949 |
|
| 2950 |
/// Create a diff between the repository index and the workdir directory. |
| 2951 |
/// |
| 2952 |
/// This matches the `git diff` command. See the note below on |
| 2953 |
/// `tree_to_workdir` for a discussion of the difference between |
| 2954 |
/// `git diff` and `git diff HEAD` and how to emulate a `git diff <treeish>` |
| 2955 |
/// using libgit2. |
| 2956 |
/// |
| 2957 |
/// The index will be used for the "old_file" side of the delta, and the |
| 2958 |
/// working directory will be used for the "new_file" side of the delta. |
| 2959 |
/// |
| 2960 |
/// If you pass `None` for the index, then the existing index of the `repo` |
| 2961 |
/// will be used. In this case, the index will be refreshed from disk |
| 2962 |
/// (if it has changed) before the diff is generated. |
| 2963 |
pub fn diff_index_to_workdir( |
| 2964 |
&self, |
| 2965 |
index: Option<&Index>, |
| 2966 |
opts: Option<&mut DiffOptions>, |
| 2967 |
) -> Result<Diff<'_>, Error> { |
| 2968 |
let mut ret = ptr::null_mut(); |
| 2969 |
unsafe { |
| 2970 |
try_call!(raw::git_diff_index_to_workdir( |
| 2971 |
&mut ret, |
| 2972 |
self.raw(), |
| 2973 |
index.map(|s| s.raw()), |
| 2974 |
opts.map(|s| s.raw()) |
| 2975 |
)); |
| 2976 |
Ok(Binding::from_raw(ret)) |
| 2977 |
} |
| 2978 |
} |
| 2979 |
|
| 2980 |
/// Create a diff between a tree and the working directory. |
| 2981 |
/// |
| 2982 |
/// The tree you provide will be used for the "old_file" side of the delta, |
| 2983 |
/// and the working directory will be used for the "new_file" side. |
| 2984 |
/// |
| 2985 |
/// This is not the same as `git diff <treeish>` or `git diff-index |
| 2986 |
/// <treeish>`. Those commands use information from the index, whereas this |
| 2987 |
/// function strictly returns the differences between the tree and the files |
| 2988 |
/// in the working directory, regardless of the state of the index. Use |
| 2989 |
/// `tree_to_workdir_with_index` to emulate those commands. |
| 2990 |
/// |
| 2991 |
/// To see difference between this and `tree_to_workdir_with_index`, |
| 2992 |
/// consider the example of a staged file deletion where the file has then |
| 2993 |
/// been put back into the working dir and further modified. The |
| 2994 |
/// tree-to-workdir diff for that file is 'modified', but `git diff` would |
| 2995 |
/// show status 'deleted' since there is a staged delete. |
| 2996 |
/// |
| 2997 |
/// If `None` is passed for `tree`, then an empty tree is used. |
| 2998 |
pub fn diff_tree_to_workdir( |
| 2999 |
&self, |
| 3000 |
old_tree: Option<&Tree<'_>>, |
| 3001 |
opts: Option<&mut DiffOptions>, |
| 3002 |
) -> Result<Diff<'_>, Error> { |
| 3003 |
let mut ret = ptr::null_mut(); |
| 3004 |
unsafe { |
| 3005 |
try_call!(raw::git_diff_tree_to_workdir( |
| 3006 |
&mut ret, |
| 3007 |
self.raw(), |
| 3008 |
old_tree.map(|s| s.raw()), |
| 3009 |
opts.map(|s| s.raw()) |
| 3010 |
)); |
| 3011 |
Ok(Binding::from_raw(ret)) |
| 3012 |
} |
| 3013 |
} |
| 3014 |
|
| 3015 |
/// Create a diff between a tree and the working directory using index data |
| 3016 |
/// to account for staged deletes, tracked files, etc. |
| 3017 |
/// |
| 3018 |
/// This emulates `git diff <tree>` by diffing the tree to the index and |
| 3019 |
/// the index to the working directory and blending the results into a |
| 3020 |
/// single diff that includes staged deleted, etc. |
| 3021 |
pub fn diff_tree_to_workdir_with_index( |
| 3022 |
&self, |
| 3023 |
old_tree: Option<&Tree<'_>>, |
| 3024 |
opts: Option<&mut DiffOptions>, |
| 3025 |
) -> Result<Diff<'_>, Error> { |
| 3026 |
let mut ret = ptr::null_mut(); |
| 3027 |
unsafe { |
| 3028 |
try_call!(raw::git_diff_tree_to_workdir_with_index( |
| 3029 |
&mut ret, |
| 3030 |
self.raw(), |
| 3031 |
old_tree.map(|s| s.raw()), |
| 3032 |
opts.map(|s| s.raw()) |
| 3033 |
)); |
| 3034 |
Ok(Binding::from_raw(ret)) |
| 3035 |
} |
| 3036 |
} |
| 3037 |
|
| 3038 |
/// Create a PackBuilder |
| 3039 |
pub fn packbuilder(&self) -> Result<PackBuilder<'_>, Error> { |
| 3040 |
let mut ret = ptr::null_mut(); |
| 3041 |
unsafe { |
| 3042 |
try_call!(raw::git_packbuilder_new(&mut ret, self.raw())); |
| 3043 |
Ok(Binding::from_raw(ret)) |
| 3044 |
} |
| 3045 |
} |
| 3046 |
|
| 3047 |
/// Save the local modifications to a new stash. |
| 3048 |
pub fn stash_save( |
| 3049 |
&mut self, |
| 3050 |
stasher: &Signature<'_>, |
| 3051 |
message: &str, |
| 3052 |
flags: Option<StashFlags>, |
| 3053 |
) -> Result<Oid, Error> { |
| 3054 |
self.stash_save2(stasher, Some(message), flags) |
| 3055 |
} |
| 3056 |
|
| 3057 |
/// Save the local modifications to a new stash. |
| 3058 |
/// unlike `stash_save` it allows to pass a null `message` |
| 3059 |
pub fn stash_save2( |
| 3060 |
&mut self, |
| 3061 |
stasher: &Signature<'_>, |
| 3062 |
message: Option<&str>, |
| 3063 |
flags: Option<StashFlags>, |
| 3064 |
) -> Result<Oid, Error> { |
| 3065 |
unsafe { |
| 3066 |
let mut raw_oid = crate::util::zeroed_raw_oid(); |
| 3067 |
let message = crate::opt_cstr(message)?; |
| 3068 |
let flags = flags.unwrap_or_else(StashFlags::empty); |
| 3069 |
try_call!(raw::git_stash_save( |
| 3070 |
&mut raw_oid, |
| 3071 |
self.raw(), |
| 3072 |
stasher.raw(), |
| 3073 |
message, |
| 3074 |
flags.bits() as c_uint |
| 3075 |
)); |
| 3076 |
Ok(Binding::from_raw(&raw_oid as *const _)) |
| 3077 |
} |
| 3078 |
} |
| 3079 |
|
| 3080 |
/// Like `stash_save` but with more options like selective stashing via path patterns. |
| 3081 |
pub fn stash_save_ext( |
| 3082 |
&mut self, |
| 3083 |
opts: Option<&mut StashSaveOptions<'_>>, |
| 3084 |
) -> Result<Oid, Error> { |
| 3085 |
unsafe { |
| 3086 |
let mut raw_oid = crate::util::zeroed_raw_oid(); |
| 3087 |
let opts = opts.map(|opts| opts.raw()); |
| 3088 |
try_call!(raw::git_stash_save_with_opts( |
| 3089 |
&mut raw_oid, |
| 3090 |
self.raw(), |
| 3091 |
opts |
| 3092 |
)); |
| 3093 |
Ok(Binding::from_raw(&raw_oid as *const _)) |
| 3094 |
} |
| 3095 |
} |
| 3096 |
|
| 3097 |
/// Apply a single stashed state from the stash list. |
| 3098 |
pub fn stash_apply( |
| 3099 |
&mut self, |
| 3100 |
index: usize, |
| 3101 |
opts: Option<&mut StashApplyOptions<'_>>, |
| 3102 |
) -> Result<(), Error> { |
| 3103 |
unsafe { |
| 3104 |
let opts = opts.map(|opts| opts.raw()); |
| 3105 |
try_call!(raw::git_stash_apply(self.raw(), index, opts)); |
| 3106 |
Ok(()) |
| 3107 |
} |
| 3108 |
} |
| 3109 |
|
| 3110 |
/// Loop over all the stashed states and issue a callback for each one. |
| 3111 |
/// |
| 3112 |
/// Return `true` to continue iterating or `false` to stop. |
| 3113 |
pub fn stash_foreach<C>(&mut self, mut callback: C) -> Result<(), Error> |
| 3114 |
where |
| 3115 |
C: FnMut(usize, &str, &Oid) -> bool, |
| 3116 |
{ |
| 3117 |
unsafe { |
| 3118 |
let mut data = StashCbData { |
| 3119 |
callback: &mut callback, |
| 3120 |
}; |
| 3121 |
let cb: raw::git_stash_cb = Some(stash_cb); |
| 3122 |
try_call!(raw::git_stash_foreach( |
| 3123 |
self.raw(), |
| 3124 |
cb, |
| 3125 |
&mut data as *mut _ as *mut _ |
| 3126 |
)); |
| 3127 |
Ok(()) |
| 3128 |
} |
| 3129 |
} |
| 3130 |
|
| 3131 |
/// Remove a single stashed state from the stash list. |
| 3132 |
pub fn stash_drop(&mut self, index: usize) -> Result<(), Error> { |
| 3133 |
unsafe { |
| 3134 |
try_call!(raw::git_stash_drop(self.raw(), index)); |
| 3135 |
Ok(()) |
| 3136 |
} |
| 3137 |
} |
| 3138 |
|
| 3139 |
/// Apply a single stashed state from the stash list and remove it from the list if successful. |
| 3140 |
pub fn stash_pop( |
| 3141 |
&mut self, |
| 3142 |
index: usize, |
| 3143 |
opts: Option<&mut StashApplyOptions<'_>>, |
| 3144 |
) -> Result<(), Error> { |
| 3145 |
unsafe { |
| 3146 |
let opts = opts.map(|opts| opts.raw()); |
| 3147 |
try_call!(raw::git_stash_pop(self.raw(), index, opts)); |
| 3148 |
Ok(()) |
| 3149 |
} |
| 3150 |
} |
| 3151 |
|
| 3152 |
/// Add ignore rules for a repository. |
| 3153 |
/// |
| 3154 |
/// The format of the rules is the same one of the .gitignore file. |
| 3155 |
pub fn add_ignore_rule(&self, rules: &str) -> Result<(), Error> { |
| 3156 |
let rules = CString::new(rules)?; |
| 3157 |
unsafe { |
| 3158 |
try_call!(raw::git_ignore_add_rule(self.raw, rules)); |
| 3159 |
} |
| 3160 |
Ok(()) |
| 3161 |
} |
| 3162 |
|
| 3163 |
/// Clear ignore rules that were explicitly added. |
| 3164 |
pub fn clear_ignore_rules(&self) -> Result<(), Error> { |
| 3165 |
unsafe { |
| 3166 |
try_call!(raw::git_ignore_clear_internal_rules(self.raw)); |
| 3167 |
} |
| 3168 |
Ok(()) |
| 3169 |
} |
| 3170 |
|
| 3171 |
/// Test if the ignore rules apply to a given path. |
| 3172 |
pub fn is_path_ignored<P: AsRef<Path>>(&self, path: P) -> Result<bool, Error> { |
| 3173 |
let path = util::cstring_to_repo_path(path.as_ref())?; |
| 3174 |
let mut ignored: c_int = 0; |
| 3175 |
unsafe { |
| 3176 |
try_call!(raw::git_ignore_path_is_ignored( |
| 3177 |
&mut ignored, |
| 3178 |
self.raw, |
| 3179 |
path |
| 3180 |
)); |
| 3181 |
} |
| 3182 |
Ok(ignored == 1) |
| 3183 |
} |
| 3184 |
|
| 3185 |
/// Perform a cherrypick |
| 3186 |
pub fn cherrypick( |
| 3187 |
&self, |
| 3188 |
commit: &Commit<'_>, |
| 3189 |
options: Option<&mut CherrypickOptions<'_>>, |
| 3190 |
) -> Result<(), Error> { |
| 3191 |
let raw_opts = options.map(|o| o.raw()); |
| 3192 |
let ptr_raw_opts = match raw_opts.as_ref() { |
| 3193 |
Some(v) => v, |
| 3194 |
None => std::ptr::null(), |
| 3195 |
}; |
| 3196 |
unsafe { |
| 3197 |
try_call!(raw::git_cherrypick(self.raw(), commit.raw(), ptr_raw_opts)); |
| 3198 |
|
| 3199 |
Ok(()) |
| 3200 |
} |
| 3201 |
} |
| 3202 |
|
| 3203 |
/// Create an index of uncommitted changes, representing the result of |
| 3204 |
/// cherry-picking. |
| 3205 |
pub fn cherrypick_commit( |
| 3206 |
&self, |
| 3207 |
cherrypick_commit: &Commit<'_>, |
| 3208 |
our_commit: &Commit<'_>, |
| 3209 |
mainline: u32, |
| 3210 |
options: Option<&MergeOptions>, |
| 3211 |
) -> Result<Index, Error> { |
| 3212 |
let mut ret = ptr::null_mut(); |
| 3213 |
unsafe { |
| 3214 |
try_call!(raw::git_cherrypick_commit( |
| 3215 |
&mut ret, |
| 3216 |
self.raw(), |
| 3217 |
cherrypick_commit.raw(), |
| 3218 |
our_commit.raw(), |
| 3219 |
mainline, |
| 3220 |
options.map(|o| o.raw()) |
| 3221 |
)); |
| 3222 |
Ok(Binding::from_raw(ret)) |
| 3223 |
} |
| 3224 |
} |
| 3225 |
|
| 3226 |
/// Find the remote name of a remote-tracking branch |
| 3227 |
pub fn branch_remote_name(&self, refname: &str) -> Result<Buf, Error> { |
| 3228 |
let refname = CString::new(refname)?; |
| 3229 |
unsafe { |
| 3230 |
let buf = Buf::new(); |
| 3231 |
try_call!(raw::git_branch_remote_name(buf.raw(), self.raw, refname)); |
| 3232 |
Ok(buf) |
| 3233 |
} |
| 3234 |
} |
| 3235 |
|
| 3236 |
/// Retrieves the name of the reference supporting the remote tracking branch, |
| 3237 |
/// given the name of a local branch reference. |
| 3238 |
pub fn branch_upstream_name(&self, refname: &str) -> Result<Buf, Error> { |
| 3239 |
let refname = CString::new(refname)?; |
| 3240 |
unsafe { |
| 3241 |
let buf = Buf::new(); |
| 3242 |
try_call!(raw::git_branch_upstream_name(buf.raw(), self.raw, refname)); |
| 3243 |
Ok(buf) |
| 3244 |
} |
| 3245 |
} |
| 3246 |
|
| 3247 |
/// Retrieve the name of the upstream remote of a local branch. |
| 3248 |
/// |
| 3249 |
/// `refname` must be in the form `refs/heads/{branch_name}` |
| 3250 |
pub fn branch_upstream_remote(&self, refname: &str) -> Result<Buf, Error> { |
| 3251 |
let refname = CString::new(refname)?; |
| 3252 |
unsafe { |
| 3253 |
let buf = Buf::new(); |
| 3254 |
try_call!(raw::git_branch_upstream_remote( |
| 3255 |
buf.raw(), |
| 3256 |
self.raw, |
| 3257 |
refname |
| 3258 |
)); |
| 3259 |
Ok(buf) |
| 3260 |
} |
| 3261 |
} |
| 3262 |
|
| 3263 |
/// Retrieve the upstream merge of a local branch, |
| 3264 |
/// configured in "branch.*.merge" |
| 3265 |
/// |
| 3266 |
/// `refname` must be in the form `refs/heads/{branch_name}` |
| 3267 |
pub fn branch_upstream_merge(&self, refname: &str) -> Result<Buf, Error> { |
| 3268 |
let refname = CString::new(refname)?; |
| 3269 |
unsafe { |
| 3270 |
let buf = Buf::new(); |
| 3271 |
try_call!(raw::git_branch_upstream_merge(buf.raw(), self.raw, refname)); |
| 3272 |
Ok(buf) |
| 3273 |
} |
| 3274 |
} |
| 3275 |
|
| 3276 |
/// Apply a Diff to the given repo, making changes directly in the working directory, the index, or both. |
| 3277 |
pub fn apply( |
| 3278 |
&self, |
| 3279 |
diff: &Diff<'_>, |
| 3280 |
location: ApplyLocation, |
| 3281 |
options: Option<&mut ApplyOptions<'_>>, |
| 3282 |
) -> Result<(), Error> { |
| 3283 |
unsafe { |
| 3284 |
try_call!(raw::git_apply( |
| 3285 |
self.raw, |
| 3286 |
diff.raw(), |
| 3287 |
location.raw(), |
| 3288 |
options.map(|s| s.raw()).unwrap_or(ptr::null()) |
| 3289 |
)); |
| 3290 |
|
| 3291 |
Ok(()) |
| 3292 |
} |
| 3293 |
} |
| 3294 |
|
| 3295 |
/// Apply a Diff to the provided tree, and return the resulting Index. |
| 3296 |
pub fn apply_to_tree( |
| 3297 |
&self, |
| 3298 |
tree: &Tree<'_>, |
| 3299 |
diff: &Diff<'_>, |
| 3300 |
options: Option<&mut ApplyOptions<'_>>, |
| 3301 |
) -> Result<Index, Error> { |
| 3302 |
let mut ret = ptr::null_mut(); |
| 3303 |
unsafe { |
| 3304 |
try_call!(raw::git_apply_to_tree( |
| 3305 |
&mut ret, |
| 3306 |
self.raw, |
| 3307 |
tree.raw(), |
| 3308 |
diff.raw(), |
| 3309 |
options.map(|s| s.raw()).unwrap_or(ptr::null()) |
| 3310 |
)); |
| 3311 |
Ok(Binding::from_raw(ret)) |
| 3312 |
} |
| 3313 |
} |
| 3314 |
|
| 3315 |
/// Reverts the given commit, producing changes in the index and working directory. |
| 3316 |
pub fn revert( |
| 3317 |
&self, |
| 3318 |
commit: &Commit<'_>, |
| 3319 |
options: Option<&mut RevertOptions<'_>>, |
| 3320 |
) -> Result<(), Error> { |
| 3321 |
let raw_opts = options.map(|o| o.raw()); |
| 3322 |
let ptr_raw_opts = match raw_opts.as_ref() { |
| 3323 |
Some(v) => v, |
| 3324 |
None => 0 as *const _, |
| 3325 |
}; |
| 3326 |
unsafe { |
| 3327 |
try_call!(raw::git_revert(self.raw(), commit.raw(), ptr_raw_opts)); |
| 3328 |
Ok(()) |
| 3329 |
} |
| 3330 |
} |
| 3331 |
|
| 3332 |
/// Reverts the given commit against the given "our" commit, |
| 3333 |
/// producing an index that reflects the result of the revert. |
| 3334 |
pub fn revert_commit( |
| 3335 |
&self, |
| 3336 |
revert_commit: &Commit<'_>, |
| 3337 |
our_commit: &Commit<'_>, |
| 3338 |
mainline: u32, |
| 3339 |
options: Option<&MergeOptions>, |
| 3340 |
) -> Result<Index, Error> { |
| 3341 |
let mut ret = ptr::null_mut(); |
| 3342 |
unsafe { |
| 3343 |
try_call!(raw::git_revert_commit( |
| 3344 |
&mut ret, |
| 3345 |
self.raw(), |
| 3346 |
revert_commit.raw(), |
| 3347 |
our_commit.raw(), |
| 3348 |
mainline, |
| 3349 |
options.map(|o| o.raw()) |
| 3350 |
)); |
| 3351 |
Ok(Binding::from_raw(ret)) |
| 3352 |
} |
| 3353 |
} |
| 3354 |
|
| 3355 |
/// Lists all the worktrees for the repository |
| 3356 |
pub fn worktrees(&self) -> Result<StringArray, Error> { |
| 3357 |
let mut arr = raw::git_strarray { |
| 3358 |
strings: ptr::null_mut(), |
| 3359 |
count: 0, |
| 3360 |
}; |
| 3361 |
unsafe { |
| 3362 |
try_call!(raw::git_worktree_list(&mut arr, self.raw)); |
| 3363 |
Ok(Binding::from_raw(arr)) |
| 3364 |
} |
| 3365 |
} |
| 3366 |
|
| 3367 |
/// Opens a worktree by name for the given repository |
| 3368 |
/// |
| 3369 |
/// This can open any worktree that the worktrees method returns. |
| 3370 |
pub fn find_worktree(&self, name: &str) -> Result<Worktree, Error> { |
| 3371 |
let mut raw = ptr::null_mut(); |
| 3372 |
let raw_name = CString::new(name)?; |
| 3373 |
unsafe { |
| 3374 |
try_call!(raw::git_worktree_lookup(&mut raw, self.raw, raw_name)); |
| 3375 |
Ok(Binding::from_raw(raw)) |
| 3376 |
} |
| 3377 |
} |
| 3378 |
|
| 3379 |
/// Creates a new worktree for the repository |
| 3380 |
pub fn worktree<'a>( |
| 3381 |
&'a self, |
| 3382 |
name: &str, |
| 3383 |
path: &Path, |
| 3384 |
opts: Option<&WorktreeAddOptions<'a>>, |
| 3385 |
) -> Result<Worktree, Error> { |
| 3386 |
let mut raw = ptr::null_mut(); |
| 3387 |
let raw_name = CString::new(name)?; |
| 3388 |
let raw_path = path.into_c_string()?; |
| 3389 |
|
| 3390 |
unsafe { |
| 3391 |
try_call!(raw::git_worktree_add( |
| 3392 |
&mut raw, |
| 3393 |
self.raw, |
| 3394 |
raw_name, |
| 3395 |
raw_path, |
| 3396 |
opts.map(|o| o.raw()) |
| 3397 |
)); |
| 3398 |
Ok(Binding::from_raw(raw)) |
| 3399 |
} |
| 3400 |
} |
| 3401 |
|
| 3402 |
/// Create a new transaction |
| 3403 |
pub fn transaction<'a>(&'a self) -> Result<Transaction<'a>, Error> { |
| 3404 |
let mut raw = ptr::null_mut(); |
| 3405 |
unsafe { |
| 3406 |
try_call!(raw::git_transaction_new(&mut raw, self.raw)); |
| 3407 |
Ok(Binding::from_raw(raw)) |
| 3408 |
} |
| 3409 |
} |
| 3410 |
|
| 3411 |
/// Gets this repository's mailmap. |
| 3412 |
pub fn mailmap(&self) -> Result<Mailmap, Error> { |
| 3413 |
let mut ret = ptr::null_mut(); |
| 3414 |
unsafe { |
| 3415 |
try_call!(raw::git_mailmap_from_repository(&mut ret, self.raw)); |
| 3416 |
Ok(Binding::from_raw(ret)) |
| 3417 |
} |
| 3418 |
} |
| 3419 |
|
| 3420 |
/// If a merge is in progress, invoke 'callback' for each commit ID in the |
| 3421 |
/// MERGE_HEAD file. |
| 3422 |
pub fn mergehead_foreach<C>(&mut self, mut callback: C) -> Result<(), Error> |
| 3423 |
where |
| 3424 |
C: FnMut(&Oid) -> bool, |
| 3425 |
{ |
| 3426 |
unsafe { |
| 3427 |
let mut data = MergeheadForeachCbData { |
| 3428 |
callback: &mut callback, |
| 3429 |
}; |
| 3430 |
let cb: raw::git_repository_mergehead_foreach_cb = Some(mergehead_foreach_cb); |
| 3431 |
try_call!(raw::git_repository_mergehead_foreach( |
| 3432 |
self.raw(), |
| 3433 |
cb, |
| 3434 |
&mut data as *mut _ as *mut _ |
| 3435 |
)); |
| 3436 |
Ok(()) |
| 3437 |
} |
| 3438 |
} |
| 3439 |
|
| 3440 |
/// Invoke 'callback' for each entry in the given FETCH_HEAD file. |
| 3441 |
/// |
| 3442 |
/// `callback` will be called with with following arguments: |
| 3443 |
/// |
| 3444 |
/// - `&str`: the reference name |
| 3445 |
/// - `&[u8]`: the remote URL |
| 3446 |
/// - `&Oid`: the reference target OID |
| 3447 |
/// - `bool`: was the reference the result of a merge |
| 3448 |
pub fn fetchhead_foreach<C>(&self, mut callback: C) -> Result<(), Error> |
| 3449 |
where |
| 3450 |
C: FnMut(&str, &[u8], &Oid, bool) -> bool, |
| 3451 |
{ |
| 3452 |
unsafe { |
| 3453 |
let mut data = FetchheadForeachCbData { |
| 3454 |
callback: &mut callback, |
| 3455 |
}; |
| 3456 |
let cb: raw::git_repository_fetchhead_foreach_cb = Some(fetchhead_foreach_cb); |
| 3457 |
try_call!(raw::git_repository_fetchhead_foreach( |
| 3458 |
self.raw(), |
| 3459 |
cb, |
| 3460 |
&mut data as *mut _ as *mut _ |
| 3461 |
)); |
| 3462 |
Ok(()) |
| 3463 |
} |
| 3464 |
} |
| 3465 |
} |
| 3466 |
|
| 3467 |
impl Binding for Repository { |
| 3468 |
type Raw = *mut raw::git_repository; |
| 3469 |
unsafe fn from_raw(ptr: *mut raw::git_repository) -> Repository { |
| 3470 |
Repository { raw: ptr } |
| 3471 |
} |
| 3472 |
fn raw(&self) -> *mut raw::git_repository { |
| 3473 |
self.raw |
| 3474 |
} |
| 3475 |
} |
| 3476 |
|
| 3477 |
impl Drop for Repository { |
| 3478 |
fn drop(&mut self) { |
| 3479 |
unsafe { raw::git_repository_free(self.raw) } |
| 3480 |
} |
| 3481 |
} |
| 3482 |
|
| 3483 |
impl RepositoryInitOptions { |
| 3484 |
/// Creates a default set of initialization options. |
| 3485 |
/// |
| 3486 |
/// By default this will set flags for creating all necessary directories |
| 3487 |
/// and initializing a directory from the user-configured templates path. |
| 3488 |
pub fn new() -> RepositoryInitOptions { |
| 3489 |
RepositoryInitOptions { |
| 3490 |
flags: raw::GIT_REPOSITORY_INIT_MKDIR as u32 |
| 3491 |
| raw::GIT_REPOSITORY_INIT_MKPATH as u32 |
| 3492 |
| raw::GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE as u32, |
| 3493 |
mode: 0, |
| 3494 |
workdir_path: None, |
| 3495 |
description: None, |
| 3496 |
template_path: None, |
| 3497 |
initial_head: None, |
| 3498 |
origin_url: None, |
| 3499 |
oid_type: None, |
| 3500 |
} |
| 3501 |
} |
| 3502 |
|
| 3503 |
/// Create a bare repository with no working directory. |
| 3504 |
/// |
| 3505 |
/// Defaults to false. |
| 3506 |
pub fn bare(&mut self, bare: bool) -> &mut RepositoryInitOptions { |
| 3507 |
self.flag(raw::GIT_REPOSITORY_INIT_BARE, bare) |
| 3508 |
} |
| 3509 |
|
| 3510 |
/// Return an error if the repository path appears to already be a git |
| 3511 |
/// repository. |
| 3512 |
/// |
| 3513 |
/// Defaults to false. |
| 3514 |
pub fn no_reinit(&mut self, enabled: bool) -> &mut RepositoryInitOptions { |
| 3515 |
self.flag(raw::GIT_REPOSITORY_INIT_NO_REINIT, enabled) |
| 3516 |
} |
| 3517 |
|
| 3518 |
/// Normally a '/.git/' will be appended to the repo path for non-bare repos |
| 3519 |
/// (if it is not already there), but passing this flag prevents that |
| 3520 |
/// behavior. |
| 3521 |
/// |
| 3522 |
/// Defaults to false. |
| 3523 |
pub fn no_dotgit_dir(&mut self, enabled: bool) -> &mut RepositoryInitOptions { |
| 3524 |
self.flag(raw::GIT_REPOSITORY_INIT_NO_DOTGIT_DIR, enabled) |
| 3525 |
} |
| 3526 |
|
| 3527 |
/// Make the repo path (and workdir path) as needed. The ".git" directory |
| 3528 |
/// will always be created regardless of this flag. |
| 3529 |
/// |
| 3530 |
/// Defaults to true. |
| 3531 |
pub fn mkdir(&mut self, enabled: bool) -> &mut RepositoryInitOptions { |
| 3532 |
self.flag(raw::GIT_REPOSITORY_INIT_MKDIR, enabled) |
| 3533 |
} |
| 3534 |
|
| 3535 |
/// Recursively make all components of the repo and workdir path as |
| 3536 |
/// necessary. |
| 3537 |
/// |
| 3538 |
/// Defaults to true. |
| 3539 |
pub fn mkpath(&mut self, enabled: bool) -> &mut RepositoryInitOptions { |
| 3540 |
self.flag(raw::GIT_REPOSITORY_INIT_MKPATH, enabled) |
| 3541 |
} |
| 3542 |
|
| 3543 |
/// Set to one of the `RepositoryInit` constants, or a custom value. |
| 3544 |
pub fn mode(&mut self, mode: RepositoryInitMode) -> &mut RepositoryInitOptions { |
| 3545 |
self.mode = mode.bits(); |
| 3546 |
self |
| 3547 |
} |
| 3548 |
|
| 3549 |
/// Enable or disable using external templates. |
| 3550 |
/// |
| 3551 |
/// If enabled, then the `template_path` option will be queried first, then |
| 3552 |
/// `init.templatedir` from the global config, and finally |
| 3553 |
/// `/usr/share/git-core-templates` will be used (if it exists). |
| 3554 |
/// |
| 3555 |
/// Defaults to true. |
| 3556 |
pub fn external_template(&mut self, enabled: bool) -> &mut RepositoryInitOptions { |
| 3557 |
self.flag(raw::GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE, enabled) |
| 3558 |
} |
| 3559 |
|
| 3560 |
fn flag( |
| 3561 |
&mut self, |
| 3562 |
flag: raw::git_repository_init_flag_t, |
| 3563 |
on: bool, |
| 3564 |
) -> &mut RepositoryInitOptions { |
| 3565 |
if on { |
| 3566 |
self.flags |= flag as u32; |
| 3567 |
} else { |
| 3568 |
self.flags &= !(flag as u32); |
| 3569 |
} |
| 3570 |
self |
| 3571 |
} |
| 3572 |
|
| 3573 |
/// The path to the working directory. |
| 3574 |
/// |
| 3575 |
/// If this is a relative path it will be evaluated relative to the repo |
| 3576 |
/// path. If this is not the "natural" working directory, a .git gitlink |
| 3577 |
/// file will be created here linking to the repo path. |
| 3578 |
pub fn workdir_path(&mut self, path: &Path) -> &mut RepositoryInitOptions { |
| 3579 |
// Normal file path OK (does not need Windows conversion). |
| 3580 |
self.workdir_path = Some(path.into_c_string().unwrap()); |
| 3581 |
self |
| 3582 |
} |
| 3583 |
|
| 3584 |
/// If set, this will be used to initialize the "description" file in the |
| 3585 |
/// repository instead of using the template content. |
| 3586 |
pub fn description(&mut self, desc: &str) -> &mut RepositoryInitOptions { |
| 3587 |
self.description = Some(CString::new(desc).unwrap()); |
| 3588 |
self |
| 3589 |
} |
| 3590 |
|
| 3591 |
/// When the `external_template` option is set, this is the first location |
| 3592 |
/// to check for the template directory. |
| 3593 |
/// |
| 3594 |
/// If this is not configured, then the default locations will be searched |
| 3595 |
/// instead. |
| 3596 |
pub fn template_path(&mut self, path: &Path) -> &mut RepositoryInitOptions { |
| 3597 |
// Normal file path OK (does not need Windows conversion). |
| 3598 |
self.template_path = Some(path.into_c_string().unwrap()); |
| 3599 |
self |
| 3600 |
} |
| 3601 |
|
| 3602 |
/// The name of the head to point HEAD at. |
| 3603 |
/// |
| 3604 |
/// If not configured, this will be taken from your git configuration. |
| 3605 |
/// If this begins with `refs/` it will be used verbatim; |
| 3606 |
/// otherwise `refs/heads/` will be prefixed |
| 3607 |
pub fn initial_head(&mut self, head: &str) -> &mut RepositoryInitOptions { |
| 3608 |
self.initial_head = Some(CString::new(head).unwrap()); |
| 3609 |
self |
| 3610 |
} |
| 3611 |
|
| 3612 |
/// If set, then after the rest of the repository initialization is |
| 3613 |
/// completed an `origin` remote will be added pointing to this URL. |
| 3614 |
pub fn origin_url(&mut self, url: &str) -> &mut RepositoryInitOptions { |
| 3615 |
self.origin_url = Some(CString::new(url).unwrap()); |
| 3616 |
self |
| 3617 |
} |
| 3618 |
|
| 3619 |
/// Set the object format (hash algorithm) for the repository. |
| 3620 |
/// |
| 3621 |
/// The default is [`ObjectFormat::Sha1`]. |
| 3622 |
/// Setting this to [`ObjectFormat::Sha256`] (requires `unstable-sha256`) |
| 3623 |
/// will create a repository that uses SHA256 object IDs. |
| 3624 |
pub fn object_format(&mut self, format: ObjectFormat) -> &mut RepositoryInitOptions { |
| 3625 |
self.oid_type = Some(format.raw()); |
| 3626 |
self |
| 3627 |
} |
| 3628 |
|
| 3629 |
/// Creates a set of raw init options to be used with |
| 3630 |
/// `git_repository_init_ext`. |
| 3631 |
/// |
| 3632 |
/// This method is unsafe as the returned value may have pointers to the |
| 3633 |
/// interior of this structure. |
| 3634 |
pub unsafe fn raw(&self) -> raw::git_repository_init_options { |
| 3635 |
let mut opts = mem::zeroed(); |
| 3636 |
assert_eq!( |
| 3637 |
raw::git_repository_init_init_options( |
| 3638 |
&mut opts, |
| 3639 |
raw::GIT_REPOSITORY_INIT_OPTIONS_VERSION |
| 3640 |
), |
| 3641 |
0 |
| 3642 |
); |
| 3643 |
opts.flags = self.flags; |
| 3644 |
opts.mode = self.mode; |
| 3645 |
opts.workdir_path = crate::call::convert(&self.workdir_path); |
| 3646 |
opts.description = crate::call::convert(&self.description); |
| 3647 |
opts.template_path = crate::call::convert(&self.template_path); |
| 3648 |
opts.initial_head = crate::call::convert(&self.initial_head); |
| 3649 |
opts.origin_url = crate::call::convert(&self.origin_url); |
| 3650 |
#[cfg(feature = "unstable-sha256")] |
| 3651 |
if let Some(oid_type) = self.oid_type { |
| 3652 |
opts.oid_type = oid_type; |
| 3653 |
} |
| 3654 |
opts |
| 3655 |
} |
| 3656 |
} |
| 3657 |
|
| 3658 |
#[cfg(test)] |
| 3659 |
mod tests { |
| 3660 |
use crate::build::CheckoutBuilder; |
| 3661 |
use crate::ObjectFormat; |
| 3662 |
#[cfg(feature = "unstable-sha256")] |
| 3663 |
use crate::RepositoryInitOptions; |
| 3664 |
use crate::{CherrypickOptions, MergeFileOptions}; |
| 3665 |
use crate::{ |
| 3666 |
Config, ObjectType, Oid, Repository, ResetType, Signature, SubmoduleIgnore, SubmoduleUpdate, |
| 3667 |
}; |
| 3668 |
|
| 3669 |
use std::ffi::OsStr; |
| 3670 |
use std::fs; |
| 3671 |
use std::path::Path; |
| 3672 |
|
| 3673 |
use libgit2_sys as raw; |
| 3674 |
use tempfile::TempDir; |
| 3675 |
|
| 3676 |
#[test] |
| 3677 |
fn smoke_init() { |
| 3678 |
let td = TempDir::new().unwrap(); |
| 3679 |
let path = td.path(); |
| 3680 |
|
| 3681 |
let repo = Repository::init(path).unwrap(); |
| 3682 |
assert!(!repo.is_bare()); |
| 3683 |
assert_eq!(repo.object_format(), ObjectFormat::Sha1); |
| 3684 |
|
| 3685 |
let oid = repo.blob(b"test").unwrap(); |
| 3686 |
assert_eq!(oid.as_bytes().len(), raw::GIT_OID_SHA1_SIZE); |
| 3687 |
assert_eq!(oid.to_string().len(), raw::GIT_OID_SHA1_HEXSIZE); |
| 3688 |
} |
| 3689 |
|
| 3690 |
#[test] |
| 3691 |
#[cfg(feature = "unstable-sha256")] |
| 3692 |
fn smoke_init_sha256() { |
| 3693 |
let td = TempDir::new().unwrap(); |
| 3694 |
let path = td.path(); |
| 3695 |
|
| 3696 |
let mut opts = RepositoryInitOptions::new(); |
| 3697 |
opts.object_format(ObjectFormat::Sha256); |
| 3698 |
|
| 3699 |
let repo = Repository::init_opts(path, &opts).unwrap(); |
| 3700 |
assert!(!repo.is_bare()); |
| 3701 |
assert_eq!(repo.object_format(), ObjectFormat::Sha256); |
| 3702 |
|
| 3703 |
let oid = repo.blob(b"test").unwrap(); |
| 3704 |
assert_eq!(oid.as_bytes().len(), raw::GIT_OID_SHA256_SIZE); |
| 3705 |
assert_eq!(oid.to_string().len(), raw::GIT_OID_SHA256_HEXSIZE); |
| 3706 |
|
| 3707 |
let config = repo.config().unwrap(); |
| 3708 |
let format = config.get_string("extensions.objectformat").unwrap(); |
| 3709 |
assert_eq!(format, "sha256"); |
| 3710 |
} |
| 3711 |
|
| 3712 |
#[test] |
| 3713 |
fn smoke_init_bare() { |
| 3714 |
let td = TempDir::new().unwrap(); |
| 3715 |
let path = td.path(); |
| 3716 |
|
| 3717 |
let repo = Repository::init_bare(path).unwrap(); |
| 3718 |
assert!(repo.is_bare()); |
| 3719 |
assert!(repo.namespace().expect("Okay even if none").is_none()); |
| 3720 |
assert_eq!(repo.object_format(), ObjectFormat::Sha1); |
| 3721 |
} |
| 3722 |
|
| 3723 |
#[test] |
| 3724 |
#[cfg(feature = "unstable-sha256")] |
| 3725 |
fn smoke_init_bare_sha256() { |
| 3726 |
let td = TempDir::new().unwrap(); |
| 3727 |
let path = td.path(); |
| 3728 |
|
| 3729 |
let mut opts = RepositoryInitOptions::new(); |
| 3730 |
opts.object_format(ObjectFormat::Sha256); |
| 3731 |
opts.bare(true); |
| 3732 |
|
| 3733 |
let repo = Repository::init_opts(path, &opts).unwrap(); |
| 3734 |
assert!(repo.is_bare()); |
| 3735 |
assert!(repo.namespace().expect("Okay even if none").is_none()); |
| 3736 |
assert_eq!(repo.object_format(), ObjectFormat::Sha256); |
| 3737 |
} |
| 3738 |
|
| 3739 |
#[test] |
| 3740 |
fn smoke_open() { |
| 3741 |
let td = TempDir::new().unwrap(); |
| 3742 |
let path = td.path(); |
| 3743 |
Repository::init(td.path()).unwrap(); |
| 3744 |
let repo = Repository::open(path).unwrap(); |
| 3745 |
assert!(!repo.is_bare()); |
| 3746 |
assert!(!repo.is_shallow()); |
| 3747 |
assert!(repo.is_empty().unwrap()); |
| 3748 |
assert_eq!( |
| 3749 |
crate::test::realpath(&repo.path()).unwrap(), |
| 3750 |
crate::test::realpath(&td.path().join(".git/")).unwrap() |
| 3751 |
); |
| 3752 |
assert_eq!(repo.state(), crate::RepositoryState::Clean); |
| 3753 |
|
| 3754 |
let oid = repo.blob(b"test").unwrap(); |
| 3755 |
assert_eq!(oid.as_bytes().len(), raw::GIT_OID_SHA1_SIZE); |
| 3756 |
assert_eq!(oid.to_string().len(), raw::GIT_OID_SHA1_HEXSIZE); |
| 3757 |
} |
| 3758 |
|
| 3759 |
#[test] |
| 3760 |
#[cfg(feature = "unstable-sha256")] |
| 3761 |
fn smoke_open_sha256() { |
| 3762 |
let td = TempDir::new().unwrap(); |
| 3763 |
let path = td.path(); |
| 3764 |
|
| 3765 |
let mut opts = RepositoryInitOptions::new(); |
| 3766 |
opts.object_format(ObjectFormat::Sha256); |
| 3767 |
Repository::init_opts(path, &opts).unwrap(); |
| 3768 |
|
| 3769 |
let repo = Repository::open(path).unwrap(); |
| 3770 |
assert_eq!(repo.object_format(), ObjectFormat::Sha256); |
| 3771 |
assert!(!repo.is_bare()); |
| 3772 |
assert!(repo.is_empty().unwrap()); |
| 3773 |
assert_eq!( |
| 3774 |
crate::test::realpath(&repo.path()).unwrap(), |
| 3775 |
crate::test::realpath(&td.path().join(".git/")).unwrap() |
| 3776 |
); |
| 3777 |
assert_eq!(repo.state(), crate::RepositoryState::Clean); |
| 3778 |
|
| 3779 |
let oid = repo.blob(b"test").unwrap(); |
| 3780 |
assert_eq!(oid.as_bytes().len(), raw::GIT_OID_SHA256_SIZE); |
| 3781 |
assert_eq!(oid.to_string().len(), raw::GIT_OID_SHA256_HEXSIZE); |
| 3782 |
|
| 3783 |
let config = repo.config().unwrap(); |
| 3784 |
let format = config.get_string("extensions.objectformat").unwrap(); |
| 3785 |
assert_eq!(format, "sha256"); |
| 3786 |
} |
| 3787 |
|
| 3788 |
#[test] |
| 3789 |
fn smoke_open_bare() { |
| 3790 |
let td = TempDir::new().unwrap(); |
| 3791 |
let path = td.path(); |
| 3792 |
Repository::init_bare(td.path()).unwrap(); |
| 3793 |
|
| 3794 |
let repo = Repository::open(path).unwrap(); |
| 3795 |
assert!(repo.is_bare()); |
| 3796 |
assert_eq!( |
| 3797 |
crate::test::realpath(&repo.path()).unwrap(), |
| 3798 |
crate::test::realpath(&td.path().join("")).unwrap() |
| 3799 |
); |
| 3800 |
} |
| 3801 |
|
| 3802 |
#[test] |
| 3803 |
#[cfg(feature = "unstable-sha256")] |
| 3804 |
fn smoke_open_bare_sha256() { |
| 3805 |
let td = TempDir::new().unwrap(); |
| 3806 |
let path = td.path(); |
| 3807 |
|
| 3808 |
let mut opts = RepositoryInitOptions::new(); |
| 3809 |
opts.object_format(ObjectFormat::Sha256); |
| 3810 |
opts.bare(true); |
| 3811 |
|
| 3812 |
Repository::init_opts(path, &opts).unwrap(); |
| 3813 |
|
| 3814 |
let repo = Repository::open(path).unwrap(); |
| 3815 |
assert!(repo.is_bare()); |
| 3816 |
assert_eq!( |
| 3817 |
crate::test::realpath(&repo.path()).unwrap(), |
| 3818 |
crate::test::realpath(&td.path().join("")).unwrap() |
| 3819 |
); |
| 3820 |
} |
| 3821 |
|
| 3822 |
#[test] |
| 3823 |
fn smoke_checkout() { |
| 3824 |
let (_td, repo) = crate::test::repo_init(); |
| 3825 |
repo.checkout_head(None).unwrap(); |
| 3826 |
} |
| 3827 |
|
| 3828 |
#[test] |
| 3829 |
#[cfg(feature = "unstable-sha256")] |
| 3830 |
fn smoke_checkout_sha256() { |
| 3831 |
let (_td, repo) = crate::test::repo_init_sha256(); |
| 3832 |
repo.checkout_head(None).unwrap(); |
| 3833 |
|
| 3834 |
let config = repo.config().unwrap(); |
| 3835 |
let format = config.get_string("extensions.objectformat").unwrap(); |
| 3836 |
assert_eq!(format, "sha256"); |
| 3837 |
} |
| 3838 |
|
| 3839 |
#[test] |
| 3840 |
fn smoke_revparse() { |
| 3841 |
let (_td, repo) = crate::test::repo_init(); |
| 3842 |
let rev = repo.revparse("HEAD").unwrap(); |
| 3843 |
assert!(rev.to().is_none()); |
| 3844 |
let from = rev.from().unwrap(); |
| 3845 |
assert!(rev.from().is_some()); |
| 3846 |
|
| 3847 |
assert_eq!(repo.revparse_single("HEAD").unwrap().id(), from.id()); |
| 3848 |
let obj = repo.find_object(from.id(), None).unwrap().clone(); |
| 3849 |
obj.peel(ObjectType::Any).unwrap(); |
| 3850 |
obj.short_id().unwrap(); |
| 3851 |
repo.reset(&obj, ResetType::Hard, None).unwrap(); |
| 3852 |
let mut opts = CheckoutBuilder::new(); |
| 3853 |
t!(repo.reset(&obj, ResetType::Soft, Some(&mut opts))); |
| 3854 |
} |
| 3855 |
|
| 3856 |
#[test] |
| 3857 |
#[cfg(feature = "unstable-sha256")] |
| 3858 |
fn smoke_revparse_sha256() { |
| 3859 |
let (_td, repo) = crate::test::repo_init_sha256(); |
| 3860 |
let rev = repo.revparse("HEAD").unwrap(); |
| 3861 |
assert!(rev.to().is_none()); |
| 3862 |
let from = rev.from().unwrap(); |
| 3863 |
assert!(rev.from().is_some()); |
| 3864 |
|
| 3865 |
assert_eq!(repo.revparse_single("HEAD").unwrap().id(), from.id()); |
| 3866 |
let obj = repo.find_object(from.id(), None).unwrap().clone(); |
| 3867 |
obj.peel(ObjectType::Any).unwrap(); |
| 3868 |
obj.short_id().unwrap(); |
| 3869 |
repo.reset(&obj, ResetType::Hard, None).unwrap(); |
| 3870 |
let mut opts = CheckoutBuilder::new(); |
| 3871 |
t!(repo.reset(&obj, ResetType::Soft, Some(&mut opts))); |
| 3872 |
|
| 3873 |
let config = repo.config().unwrap(); |
| 3874 |
let format = config.get_string("extensions.objectformat").unwrap(); |
| 3875 |
assert_eq!(format, "sha256"); |
| 3876 |
} |
| 3877 |
|
| 3878 |
#[test] |
| 3879 |
fn makes_dirs() { |
| 3880 |
let td = TempDir::new().unwrap(); |
| 3881 |
Repository::init(&td.path().join("a/b/c/d")).unwrap(); |
| 3882 |
} |
| 3883 |
|
| 3884 |
#[test] |
| 3885 |
fn smoke_discover() { |
| 3886 |
let td = TempDir::new().unwrap(); |
| 3887 |
let subdir = td.path().join("subdi"); |
| 3888 |
fs::create_dir(&subdir).unwrap(); |
| 3889 |
Repository::init_bare(td.path()).unwrap(); |
| 3890 |
let repo = Repository::discover(&subdir).unwrap(); |
| 3891 |
assert_eq!( |
| 3892 |
crate::test::realpath(&repo.path()).unwrap(), |
| 3893 |
crate::test::realpath(&td.path().join("")).unwrap() |
| 3894 |
); |
| 3895 |
} |
| 3896 |
|
| 3897 |
#[test] |
| 3898 |
fn smoke_discover_path() { |
| 3899 |
let td = TempDir::new().unwrap(); |
| 3900 |
let subdir = td.path().join("subdi"); |
| 3901 |
fs::create_dir(&subdir).unwrap(); |
| 3902 |
Repository::init_bare(td.path()).unwrap(); |
| 3903 |
let path = Repository::discover_path(&subdir, &[] as &[&OsStr]).unwrap(); |
| 3904 |
assert_eq!( |
| 3905 |
crate::test::realpath(&path).unwrap(), |
| 3906 |
crate::test::realpath(&td.path().join("")).unwrap() |
| 3907 |
); |
| 3908 |
} |
| 3909 |
|
| 3910 |
#[test] |
| 3911 |
fn smoke_discover_path_ceiling_dir() { |
| 3912 |
let td = TempDir::new().unwrap(); |
| 3913 |
let subdir = td.path().join("subdi"); |
| 3914 |
fs::create_dir(&subdir).unwrap(); |
| 3915 |
let ceilingdir = subdir.join("ceiling"); |
| 3916 |
fs::create_dir(&ceilingdir).unwrap(); |
| 3917 |
let testdir = ceilingdir.join("testdi"); |
| 3918 |
fs::create_dir(&testdir).unwrap(); |
| 3919 |
Repository::init_bare(td.path()).unwrap(); |
| 3920 |
let path = Repository::discover_path(&testdir, &[ceilingdir.as_os_str()]); |
| 3921 |
|
| 3922 |
assert!(path.is_err()); |
| 3923 |
} |
| 3924 |
|
| 3925 |
#[test] |
| 3926 |
fn smoke_open_ext() { |
| 3927 |
let td = TempDir::new().unwrap(); |
| 3928 |
let subdir = td.path().join("subdir"); |
| 3929 |
fs::create_dir(&subdir).unwrap(); |
| 3930 |
Repository::init(td.path()).unwrap(); |
| 3931 |
|
| 3932 |
let repo = Repository::open_ext( |
| 3933 |
&subdir, |
| 3934 |
crate::RepositoryOpenFlags::empty(), |
| 3935 |
&[] as &[&OsStr], |
| 3936 |
) |
| 3937 |
.unwrap(); |
| 3938 |
assert!(!repo.is_bare()); |
| 3939 |
assert_eq!( |
| 3940 |
crate::test::realpath(&repo.path()).unwrap(), |
| 3941 |
crate::test::realpath(&td.path().join(".git")).unwrap() |
| 3942 |
); |
| 3943 |
|
| 3944 |
let repo = |
| 3945 |
Repository::open_ext(&subdir, crate::RepositoryOpenFlags::BARE, &[] as &[&OsStr]) |
| 3946 |
.unwrap(); |
| 3947 |
assert!(repo.is_bare()); |
| 3948 |
assert_eq!( |
| 3949 |
crate::test::realpath(&repo.path()).unwrap(), |
| 3950 |
crate::test::realpath(&td.path().join(".git")).unwrap() |
| 3951 |
); |
| 3952 |
|
| 3953 |
let err = Repository::open_ext( |
| 3954 |
&subdir, |
| 3955 |
crate::RepositoryOpenFlags::NO_SEARCH, |
| 3956 |
&[] as &[&OsStr], |
| 3957 |
) |
| 3958 |
.err() |
| 3959 |
.unwrap(); |
| 3960 |
assert_eq!(err.code(), crate::ErrorCode::NotFound); |
| 3961 |
|
| 3962 |
assert!( |
| 3963 |
Repository::open_ext(&subdir, crate::RepositoryOpenFlags::empty(), &[&subdir]).is_ok() |
| 3964 |
); |
| 3965 |
} |
| 3966 |
|
| 3967 |
fn graph_repo_init() -> (TempDir, Repository) { |
| 3968 |
let (_td, repo) = crate::test::repo_init(); |
| 3969 |
{ |
| 3970 |
let head = repo.head().unwrap().target().unwrap(); |
| 3971 |
let head = repo.find_commit(head).unwrap(); |
| 3972 |
|
| 3973 |
let mut index = repo.index().unwrap(); |
| 3974 |
let id = index.write_tree().unwrap(); |
| 3975 |
|
| 3976 |
let tree = repo.find_tree(id).unwrap(); |
| 3977 |
let sig = repo.signature().unwrap(); |
| 3978 |
repo.commit(Some("HEAD"), &sig, &sig, "second", &tree, &[&head]) |
| 3979 |
.unwrap(); |
| 3980 |
} |
| 3981 |
(_td, repo) |
| 3982 |
} |
| 3983 |
|
| 3984 |
#[test] |
| 3985 |
fn smoke_graph_ahead_behind() { |
| 3986 |
let (_td, repo) = graph_repo_init(); |
| 3987 |
let head = repo.head().unwrap().target().unwrap(); |
| 3988 |
let head = repo.find_commit(head).unwrap(); |
| 3989 |
let head_id = head.id(); |
| 3990 |
let head_parent_id = head.parent(0).unwrap().id(); |
| 3991 |
let (ahead, behind) = repo.graph_ahead_behind(head_id, head_parent_id).unwrap(); |
| 3992 |
assert_eq!(ahead, 1); |
| 3993 |
assert_eq!(behind, 0); |
| 3994 |
let (ahead, behind) = repo.graph_ahead_behind(head_parent_id, head_id).unwrap(); |
| 3995 |
assert_eq!(ahead, 0); |
| 3996 |
assert_eq!(behind, 1); |
| 3997 |
} |
| 3998 |
|
| 3999 |
#[test] |
| 4000 |
fn smoke_graph_descendant_of() { |
| 4001 |
let (_td, repo) = graph_repo_init(); |
| 4002 |
let head = repo.head().unwrap().target().unwrap(); |
| 4003 |
let head = repo.find_commit(head).unwrap(); |
| 4004 |
let head_id = head.id(); |
| 4005 |
let head_parent_id = head.parent(0).unwrap().id(); |
| 4006 |
assert!(repo.graph_descendant_of(head_id, head_parent_id).unwrap()); |
| 4007 |
assert!(!repo.graph_descendant_of(head_parent_id, head_id).unwrap()); |
| 4008 |
} |
| 4009 |
|
| 4010 |
#[test] |
| 4011 |
fn smoke_reference_has_log_ensure_log() { |
| 4012 |
let (_td, repo) = crate::test::repo_init(); |
| 4013 |
|
| 4014 |
assert_eq!(repo.reference_has_log("HEAD").unwrap(), true); |
| 4015 |
assert_eq!(repo.reference_has_log("refs/heads/main").unwrap(), true); |
| 4016 |
assert_eq!(repo.reference_has_log("NOT_HEAD").unwrap(), false); |
| 4017 |
let main_oid = repo.revparse_single("main").unwrap().id(); |
| 4018 |
assert!(repo |
| 4019 |
.reference("NOT_HEAD", main_oid, false, "creating a new branch") |
| 4020 |
.is_ok()); |
| 4021 |
assert_eq!(repo.reference_has_log("NOT_HEAD").unwrap(), false); |
| 4022 |
assert!(repo.reference_ensure_log("NOT_HEAD").is_ok()); |
| 4023 |
assert_eq!(repo.reference_has_log("NOT_HEAD").unwrap(), true); |
| 4024 |
} |
| 4025 |
|
| 4026 |
#[test] |
| 4027 |
fn smoke_set_head() { |
| 4028 |
let (_td, repo) = crate::test::repo_init(); |
| 4029 |
|
| 4030 |
assert!(repo.set_head("refs/heads/does-not-exist").is_ok()); |
| 4031 |
assert!(repo.head().is_err()); |
| 4032 |
|
| 4033 |
assert!(repo.set_head("refs/heads/main").is_ok()); |
| 4034 |
assert!(repo.head().is_ok()); |
| 4035 |
|
| 4036 |
assert!(repo.set_head("*").is_err()); |
| 4037 |
} |
| 4038 |
|
| 4039 |
#[test] |
| 4040 |
fn smoke_set_head_bytes() { |
| 4041 |
let (_td, repo) = crate::test::repo_init(); |
| 4042 |
|
| 4043 |
assert!(repo.set_head_bytes(b"refs/heads/does-not-exist").is_ok()); |
| 4044 |
assert!(repo.head().is_err()); |
| 4045 |
|
| 4046 |
assert!(repo.set_head_bytes(b"refs/heads/main").is_ok()); |
| 4047 |
assert!(repo.head().is_ok()); |
| 4048 |
|
| 4049 |
assert!(repo.set_head_bytes(b"*").is_err()); |
| 4050 |
} |
| 4051 |
|
| 4052 |
#[test] |
| 4053 |
fn smoke_set_head_detached() { |
| 4054 |
let (_td, repo) = crate::test::repo_init(); |
| 4055 |
|
| 4056 |
let void_oid = match repo.object_format() { |
| 4057 |
ObjectFormat::Sha1 => Oid::ZERO_SHA1, |
| 4058 |
#[cfg(feature = "unstable-sha256")] |
| 4059 |
ObjectFormat::Sha256 => Oid::ZERO_SHA256, |
| 4060 |
}; |
| 4061 |
assert!(repo.set_head_detached(void_oid).is_err()); |
| 4062 |
|
| 4063 |
let main_oid = repo.revparse_single("main").unwrap().id(); |
| 4064 |
assert!(repo.set_head_detached(main_oid).is_ok()); |
| 4065 |
assert_eq!(repo.head().unwrap().target().unwrap(), main_oid); |
| 4066 |
} |
| 4067 |
|
| 4068 |
#[test] |
| 4069 |
fn smoke_find_object_by_prefix() { |
| 4070 |
let (_td, repo) = crate::test::repo_init(); |
| 4071 |
let head = repo.head().unwrap().target().unwrap(); |
| 4072 |
let head = repo.find_commit(head).unwrap(); |
| 4073 |
let head_id = head.id(); |
| 4074 |
let head_prefix = &head_id.to_string()[..7]; |
| 4075 |
let obj = repo.find_object_by_prefix(head_prefix, None).unwrap(); |
| 4076 |
assert_eq!(obj.id(), head_id); |
| 4077 |
} |
| 4078 |
|
| 4079 |
/// create the following: |
| 4080 |
/// /---o4 |
| 4081 |
/// /---o3 |
| 4082 |
/// o1---o2 |
| 4083 |
#[test] |
| 4084 |
fn smoke_merge_base() { |
| 4085 |
let (_td, repo) = graph_repo_init(); |
| 4086 |
let sig = repo.signature().unwrap(); |
| 4087 |
|
| 4088 |
// let oid1 = head |
| 4089 |
let oid1 = repo.head().unwrap().target().unwrap(); |
| 4090 |
let commit1 = repo.find_commit(oid1).unwrap(); |
| 4091 |
println!("created oid1 {:?}", oid1); |
| 4092 |
|
| 4093 |
repo.branch("branch_a", &commit1, true).unwrap(); |
| 4094 |
repo.branch("branch_b", &commit1, true).unwrap(); |
| 4095 |
repo.branch("branch_c", &commit1, true).unwrap(); |
| 4096 |
|
| 4097 |
// create commit oid2 on branch_a |
| 4098 |
let mut index = repo.index().unwrap(); |
| 4099 |
let p = Path::new(repo.workdir().unwrap()).join("file_a"); |
| 4100 |
println!("using path {:?}", p); |
| 4101 |
fs::File::create(&p).unwrap(); |
| 4102 |
index.add_path(Path::new("file_a")).unwrap(); |
| 4103 |
let id_a = index.write_tree().unwrap(); |
| 4104 |
let tree_a = repo.find_tree(id_a).unwrap(); |
| 4105 |
let oid2 = repo |
| 4106 |
.commit( |
| 4107 |
Some("refs/heads/branch_a"), |
| 4108 |
&sig, |
| 4109 |
&sig, |
| 4110 |
"commit 2", |
| 4111 |
&tree_a, |
| 4112 |
&[&commit1], |
| 4113 |
) |
| 4114 |
.unwrap(); |
| 4115 |
repo.find_commit(oid2).unwrap(); |
| 4116 |
println!("created oid2 {:?}", oid2); |
| 4117 |
|
| 4118 |
t!(repo.reset(commit1.as_object(), ResetType::Hard, None)); |
| 4119 |
|
| 4120 |
// create commit oid3 on branch_b |
| 4121 |
let mut index = repo.index().unwrap(); |
| 4122 |
let p = Path::new(repo.workdir().unwrap()).join("file_b"); |
| 4123 |
fs::File::create(&p).unwrap(); |
| 4124 |
index.add_path(Path::new("file_b")).unwrap(); |
| 4125 |
let id_b = index.write_tree().unwrap(); |
| 4126 |
let tree_b = repo.find_tree(id_b).unwrap(); |
| 4127 |
let oid3 = repo |
| 4128 |
.commit( |
| 4129 |
Some("refs/heads/branch_b"), |
| 4130 |
&sig, |
| 4131 |
&sig, |
| 4132 |
"commit 3", |
| 4133 |
&tree_b, |
| 4134 |
&[&commit1], |
| 4135 |
) |
| 4136 |
.unwrap(); |
| 4137 |
repo.find_commit(oid3).unwrap(); |
| 4138 |
println!("created oid3 {:?}", oid3); |
| 4139 |
|
| 4140 |
t!(repo.reset(commit1.as_object(), ResetType::Hard, None)); |
| 4141 |
|
| 4142 |
// create commit oid4 on branch_c |
| 4143 |
let mut index = repo.index().unwrap(); |
| 4144 |
let p = Path::new(repo.workdir().unwrap()).join("file_c"); |
| 4145 |
fs::File::create(&p).unwrap(); |
| 4146 |
index.add_path(Path::new("file_c")).unwrap(); |
| 4147 |
let id_c = index.write_tree().unwrap(); |
| 4148 |
let tree_c = repo.find_tree(id_c).unwrap(); |
| 4149 |
let oid4 = repo |
| 4150 |
.commit( |
| 4151 |
Some("refs/heads/branch_c"), |
| 4152 |
&sig, |
| 4153 |
&sig, |
| 4154 |
"commit 3", |
| 4155 |
&tree_c, |
| 4156 |
&[&commit1], |
| 4157 |
) |
| 4158 |
.unwrap(); |
| 4159 |
repo.find_commit(oid4).unwrap(); |
| 4160 |
println!("created oid4 {:?}", oid4); |
| 4161 |
|
| 4162 |
// the merge base of (oid2,oid3) should be oid1 |
| 4163 |
let merge_base = repo.merge_base(oid2, oid3).unwrap(); |
| 4164 |
assert_eq!(merge_base, oid1); |
| 4165 |
|
| 4166 |
// the merge base of (oid2,oid3,oid4) should be oid1 |
| 4167 |
let merge_base = repo.merge_base_many(&[oid2, oid3, oid4]).unwrap(); |
| 4168 |
assert_eq!(merge_base, oid1); |
| 4169 |
|
| 4170 |
// the octopus merge base of (oid2,oid3,oid4) should be oid1 |
| 4171 |
let merge_base = repo.merge_base_octopus(&[oid2, oid3, oid4]).unwrap(); |
| 4172 |
assert_eq!(merge_base, oid1); |
| 4173 |
} |
| 4174 |
|
| 4175 |
/// create an octopus: |
| 4176 |
/// /---o2-o4 |
| 4177 |
/// o1 X |
| 4178 |
/// \---o3-o5 |
| 4179 |
/// and checks that the merge bases of (o4,o5) are (o2,o3) |
| 4180 |
#[test] |
| 4181 |
fn smoke_merge_bases() { |
| 4182 |
let (_td, repo) = graph_repo_init(); |
| 4183 |
let sig = repo.signature().unwrap(); |
| 4184 |
|
| 4185 |
// let oid1 = head |
| 4186 |
let oid1 = repo.head().unwrap().target().unwrap(); |
| 4187 |
let commit1 = repo.find_commit(oid1).unwrap(); |
| 4188 |
println!("created oid1 {:?}", oid1); |
| 4189 |
|
| 4190 |
repo.branch("branch_a", &commit1, true).unwrap(); |
| 4191 |
repo.branch("branch_b", &commit1, true).unwrap(); |
| 4192 |
|
| 4193 |
// create commit oid2 on branchA |
| 4194 |
let mut index = repo.index().unwrap(); |
| 4195 |
let p = Path::new(repo.workdir().unwrap()).join("file_a"); |
| 4196 |
println!("using path {:?}", p); |
| 4197 |
fs::File::create(&p).unwrap(); |
| 4198 |
index.add_path(Path::new("file_a")).unwrap(); |
| 4199 |
let id_a = index.write_tree().unwrap(); |
| 4200 |
let tree_a = repo.find_tree(id_a).unwrap(); |
| 4201 |
let oid2 = repo |
| 4202 |
.commit( |
| 4203 |
Some("refs/heads/branch_a"), |
| 4204 |
&sig, |
| 4205 |
&sig, |
| 4206 |
"commit 2", |
| 4207 |
&tree_a, |
| 4208 |
&[&commit1], |
| 4209 |
) |
| 4210 |
.unwrap(); |
| 4211 |
let commit2 = repo.find_commit(oid2).unwrap(); |
| 4212 |
println!("created oid2 {:?}", oid2); |
| 4213 |
|
| 4214 |
t!(repo.reset(commit1.as_object(), ResetType::Hard, None)); |
| 4215 |
|
| 4216 |
// create commit oid3 on branchB |
| 4217 |
let mut index = repo.index().unwrap(); |
| 4218 |
let p = Path::new(repo.workdir().unwrap()).join("file_b"); |
| 4219 |
fs::File::create(&p).unwrap(); |
| 4220 |
index.add_path(Path::new("file_b")).unwrap(); |
| 4221 |
let id_b = index.write_tree().unwrap(); |
| 4222 |
let tree_b = repo.find_tree(id_b).unwrap(); |
| 4223 |
let oid3 = repo |
| 4224 |
.commit( |
| 4225 |
Some("refs/heads/branch_b"), |
| 4226 |
&sig, |
| 4227 |
&sig, |
| 4228 |
"commit 3", |
| 4229 |
&tree_b, |
| 4230 |
&[&commit1], |
| 4231 |
) |
| 4232 |
.unwrap(); |
| 4233 |
let commit3 = repo.find_commit(oid3).unwrap(); |
| 4234 |
println!("created oid3 {:?}", oid3); |
| 4235 |
|
| 4236 |
// create merge commit oid4 on branchA with parents oid2 and oid3 |
| 4237 |
//let mut index4 = repo.merge_commits(&commit2, &commit3, None).unwrap(); |
| 4238 |
repo.set_head("refs/heads/branch_a").unwrap(); |
| 4239 |
repo.checkout_head(None).unwrap(); |
| 4240 |
let oid4 = repo |
| 4241 |
.commit( |
| 4242 |
Some("refs/heads/branch_a"), |
| 4243 |
&sig, |
| 4244 |
&sig, |
| 4245 |
"commit 4", |
| 4246 |
&tree_a, |
| 4247 |
&[&commit2, &commit3], |
| 4248 |
) |
| 4249 |
.unwrap(); |
| 4250 |
//index4.write_tree_to(&repo).unwrap(); |
| 4251 |
println!("created oid4 {:?}", oid4); |
| 4252 |
|
| 4253 |
// create merge commit oid5 on branchB with parents oid2 and oid3 |
| 4254 |
//let mut index5 = repo.merge_commits(&commit3, &commit2, None).unwrap(); |
| 4255 |
repo.set_head("refs/heads/branch_b").unwrap(); |
| 4256 |
repo.checkout_head(None).unwrap(); |
| 4257 |
let oid5 = repo |
| 4258 |
.commit( |
| 4259 |
Some("refs/heads/branch_b"), |
| 4260 |
&sig, |
| 4261 |
&sig, |
| 4262 |
"commit 5", |
| 4263 |
&tree_a, |
| 4264 |
&[&commit3, &commit2], |
| 4265 |
) |
| 4266 |
.unwrap(); |
| 4267 |
//index5.write_tree_to(&repo).unwrap(); |
| 4268 |
println!("created oid5 {:?}", oid5); |
| 4269 |
|
| 4270 |
// merge bases of (oid4,oid5) should be (oid2,oid3) |
| 4271 |
let merge_bases = repo.merge_bases(oid4, oid5).unwrap(); |
| 4272 |
let mut found_oid2 = false; |
| 4273 |
let mut found_oid3 = false; |
| 4274 |
for mg in merge_bases.iter() { |
| 4275 |
println!("found merge base {:?}", mg); |
| 4276 |
if mg == &oid2 { |
| 4277 |
found_oid2 = true; |
| 4278 |
} else if mg == &oid3 { |
| 4279 |
found_oid3 = true; |
| 4280 |
} else { |
| 4281 |
assert!(false); |
| 4282 |
} |
| 4283 |
} |
| 4284 |
assert!(found_oid2); |
| 4285 |
assert!(found_oid3); |
| 4286 |
assert_eq!(merge_bases.len(), 2); |
| 4287 |
|
| 4288 |
// merge bases of (oid4,oid5) should be (oid2,oid3) |
| 4289 |
let merge_bases = repo.merge_bases_many(&[oid4, oid5]).unwrap(); |
| 4290 |
let mut found_oid2 = false; |
| 4291 |
let mut found_oid3 = false; |
| 4292 |
for mg in merge_bases.iter() { |
| 4293 |
println!("found merge base {:?}", mg); |
| 4294 |
if mg == &oid2 { |
| 4295 |
found_oid2 = true; |
| 4296 |
} else if mg == &oid3 { |
| 4297 |
found_oid3 = true; |
| 4298 |
} else { |
| 4299 |
assert!(false); |
| 4300 |
} |
| 4301 |
} |
| 4302 |
assert!(found_oid2); |
| 4303 |
assert!(found_oid3); |
| 4304 |
assert_eq!(merge_bases.len(), 2); |
| 4305 |
} |
| 4306 |
|
| 4307 |
#[test] |
| 4308 |
fn smoke_merge_file_from_index() { |
| 4309 |
let (_td, repo) = crate::test::repo_init(); |
| 4310 |
|
| 4311 |
let head_commit = { |
| 4312 |
let head = t!(repo.head()).target().unwrap(); |
| 4313 |
t!(repo.find_commit(head)) |
| 4314 |
}; |
| 4315 |
|
| 4316 |
let file_path = Path::new("file"); |
| 4317 |
let author = t!(Signature::now("committer", "committer@email")); |
| 4318 |
|
| 4319 |
let base_commit = { |
| 4320 |
t!(fs::write(repo.workdir().unwrap().join(&file_path), "base")); |
| 4321 |
let mut index = t!(repo.index()); |
| 4322 |
t!(index.add_path(&file_path)); |
| 4323 |
let tree_id = t!(index.write_tree()); |
| 4324 |
let tree = t!(repo.find_tree(tree_id)); |
| 4325 |
|
| 4326 |
let commit_id = t!(repo.commit( |
| 4327 |
Some("HEAD"), |
| 4328 |
&author, |
| 4329 |
&author, |
| 4330 |
r"Add file with contents 'base'", |
| 4331 |
&tree, |
| 4332 |
&[&head_commit], |
| 4333 |
)); |
| 4334 |
t!(repo.find_commit(commit_id)) |
| 4335 |
}; |
| 4336 |
|
| 4337 |
let foo_commit = { |
| 4338 |
t!(fs::write(repo.workdir().unwrap().join(&file_path), "foo")); |
| 4339 |
let mut index = t!(repo.index()); |
| 4340 |
t!(index.add_path(&file_path)); |
| 4341 |
let tree_id = t!(index.write_tree()); |
| 4342 |
let tree = t!(repo.find_tree(tree_id)); |
| 4343 |
|
| 4344 |
let commit_id = t!(repo.commit( |
| 4345 |
Some("refs/heads/foo"), |
| 4346 |
&author, |
| 4347 |
&author, |
| 4348 |
r"Update file with contents 'foo'", |
| 4349 |
&tree, |
| 4350 |
&[&base_commit], |
| 4351 |
)); |
| 4352 |
t!(repo.find_commit(commit_id)) |
| 4353 |
}; |
| 4354 |
|
| 4355 |
let bar_commit = { |
| 4356 |
t!(fs::write(repo.workdir().unwrap().join(&file_path), "bar")); |
| 4357 |
let mut index = t!(repo.index()); |
| 4358 |
t!(index.add_path(&file_path)); |
| 4359 |
let tree_id = t!(index.write_tree()); |
| 4360 |
let tree = t!(repo.find_tree(tree_id)); |
| 4361 |
|
| 4362 |
let commit_id = t!(repo.commit( |
| 4363 |
Some("refs/heads/bar"), |
| 4364 |
&author, |
| 4365 |
&author, |
| 4366 |
r"Update file with contents 'bar'", |
| 4367 |
&tree, |
| 4368 |
&[&base_commit], |
| 4369 |
)); |
| 4370 |
t!(repo.find_commit(commit_id)) |
| 4371 |
}; |
| 4372 |
|
| 4373 |
let index = t!(repo.merge_commits(&foo_commit, &bar_commit, None)); |
| 4374 |
|
| 4375 |
let base = index.get_path(file_path, 1).unwrap(); |
| 4376 |
let ours = index.get_path(file_path, 2).unwrap(); |
| 4377 |
let theirs = index.get_path(file_path, 3).unwrap(); |
| 4378 |
|
| 4379 |
let mut opts = MergeFileOptions::new(); |
| 4380 |
opts.ancestor_label("ancestor"); |
| 4381 |
opts.our_label("ours"); |
| 4382 |
opts.their_label("theirs"); |
| 4383 |
opts.style_diff3(true); |
| 4384 |
let merge_file_result = repo |
| 4385 |
.merge_file_from_index(&base, &ours, &theirs, Some(&mut opts)) |
| 4386 |
.unwrap(); |
| 4387 |
|
| 4388 |
assert!(!merge_file_result.is_automergeable()); |
| 4389 |
assert_eq!(merge_file_result.path(), Ok(Some("file"))); |
| 4390 |
assert_eq!( |
| 4391 |
String::from_utf8_lossy(merge_file_result.content()).to_string(), |
| 4392 |
r"<<<<<<< ours |
| 4393 |
foo |
| 4394 |
||||||| ancestor |
| 4395 |
base |
| 4396 |
======= |
| 4397 |
bar |
| 4398 |
>>>>>>> theirs |
| 4399 |
", |
| 4400 |
); |
| 4401 |
} |
| 4402 |
|
| 4403 |
#[test] |
| 4404 |
fn smoke_revparse_ext() { |
| 4405 |
let (_td, repo) = graph_repo_init(); |
| 4406 |
|
| 4407 |
{ |
| 4408 |
let short_refname = "main"; |
| 4409 |
let expected_refname = "refs/heads/main"; |
| 4410 |
let (obj, reference) = repo.revparse_ext(short_refname).unwrap(); |
| 4411 |
let expected_obj = repo.revparse_single(expected_refname).unwrap(); |
| 4412 |
assert_eq!(obj.id(), expected_obj.id()); |
| 4413 |
assert_eq!(reference.unwrap().name().unwrap(), expected_refname); |
| 4414 |
} |
| 4415 |
{ |
| 4416 |
let missing_refname = "refs/heads/does-not-exist"; |
| 4417 |
assert!(repo.revparse_ext(missing_refname).is_err()); |
| 4418 |
} |
| 4419 |
{ |
| 4420 |
let (_obj, reference) = repo.revparse_ext("HEAD^").unwrap(); |
| 4421 |
assert!(reference.is_none()); |
| 4422 |
} |
| 4423 |
} |
| 4424 |
|
| 4425 |
#[test] |
| 4426 |
fn smoke_is_path_ignored() { |
| 4427 |
let (_td, repo) = graph_repo_init(); |
| 4428 |
|
| 4429 |
assert!(!repo.is_path_ignored(Path::new("foo")).unwrap()); |
| 4430 |
|
| 4431 |
let _ = repo.add_ignore_rule("/foo"); |
| 4432 |
assert!(repo.is_path_ignored(Path::new("foo")).unwrap()); |
| 4433 |
if cfg!(windows) { |
| 4434 |
assert!(repo.is_path_ignored(Path::new("foo\\thing")).unwrap()); |
| 4435 |
} |
| 4436 |
|
| 4437 |
let _ = repo.clear_ignore_rules(); |
| 4438 |
assert!(!repo.is_path_ignored(Path::new("foo")).unwrap()); |
| 4439 |
if cfg!(windows) { |
| 4440 |
assert!(!repo.is_path_ignored(Path::new("foo\\thing")).unwrap()); |
| 4441 |
} |
| 4442 |
} |
| 4443 |
|
| 4444 |
#[test] |
| 4445 |
fn smoke_cherrypick() { |
| 4446 |
let (_td, repo) = crate::test::repo_init(); |
| 4447 |
let sig = repo.signature().unwrap(); |
| 4448 |
|
| 4449 |
let oid1 = repo.head().unwrap().target().unwrap(); |
| 4450 |
let commit1 = repo.find_commit(oid1).unwrap(); |
| 4451 |
|
| 4452 |
repo.branch("branch_a", &commit1, true).unwrap(); |
| 4453 |
|
| 4454 |
// Add 2 commits on top of the initial one in branch_a |
| 4455 |
let mut index = repo.index().unwrap(); |
| 4456 |
let p1 = Path::new(repo.workdir().unwrap()).join("file_c"); |
| 4457 |
fs::File::create(&p1).unwrap(); |
| 4458 |
index.add_path(Path::new("file_c")).unwrap(); |
| 4459 |
let id = index.write_tree().unwrap(); |
| 4460 |
let tree_c = repo.find_tree(id).unwrap(); |
| 4461 |
let oid2 = repo |
| 4462 |
.commit( |
| 4463 |
Some("refs/heads/branch_a"), |
| 4464 |
&sig, |
| 4465 |
&sig, |
| 4466 |
"commit 2", |
| 4467 |
&tree_c, |
| 4468 |
&[&commit1], |
| 4469 |
) |
| 4470 |
.unwrap(); |
| 4471 |
let commit2 = repo.find_commit(oid2).unwrap(); |
| 4472 |
println!("created oid2 {:?}", oid2); |
| 4473 |
assert!(p1.exists()); |
| 4474 |
|
| 4475 |
let mut index = repo.index().unwrap(); |
| 4476 |
let p2 = Path::new(repo.workdir().unwrap()).join("file_d"); |
| 4477 |
fs::File::create(&p2).unwrap(); |
| 4478 |
index.add_path(Path::new("file_d")).unwrap(); |
| 4479 |
let id = index.write_tree().unwrap(); |
| 4480 |
let tree_d = repo.find_tree(id).unwrap(); |
| 4481 |
let oid3 = repo |
| 4482 |
.commit( |
| 4483 |
Some("refs/heads/branch_a"), |
| 4484 |
&sig, |
| 4485 |
&sig, |
| 4486 |
"commit 3", |
| 4487 |
&tree_d, |
| 4488 |
&[&commit2], |
| 4489 |
) |
| 4490 |
.unwrap(); |
| 4491 |
let commit3 = repo.find_commit(oid3).unwrap(); |
| 4492 |
println!("created oid3 {:?}", oid3); |
| 4493 |
assert!(p1.exists()); |
| 4494 |
assert!(p2.exists()); |
| 4495 |
|
| 4496 |
// cherry-pick commit3 on top of commit1 in branch b |
| 4497 |
repo.reset(commit1.as_object(), ResetType::Hard, None) |
| 4498 |
.unwrap(); |
| 4499 |
let mut cherrypick_opts = CherrypickOptions::new(); |
| 4500 |
repo.cherrypick(&commit3, Some(&mut cherrypick_opts)) |
| 4501 |
.unwrap(); |
| 4502 |
let id = repo.index().unwrap().write_tree().unwrap(); |
| 4503 |
let tree_d = repo.find_tree(id).unwrap(); |
| 4504 |
let oid4 = repo |
| 4505 |
.commit(Some("HEAD"), &sig, &sig, "commit 4", &tree_d, &[&commit1]) |
| 4506 |
.unwrap(); |
| 4507 |
let commit4 = repo.find_commit(oid4).unwrap(); |
| 4508 |
// should have file from commit3, but not the file from commit2 |
| 4509 |
assert_eq!(commit4.parent(0).unwrap().id(), commit1.id()); |
| 4510 |
assert!(!p1.exists()); |
| 4511 |
assert!(p2.exists()); |
| 4512 |
} |
| 4513 |
|
| 4514 |
#[test] |
| 4515 |
fn smoke_revert() { |
| 4516 |
let (_td, repo) = crate::test::repo_init(); |
| 4517 |
let foo_file = Path::new(repo.workdir().unwrap()).join("foo"); |
| 4518 |
assert!(!foo_file.exists()); |
| 4519 |
|
| 4520 |
let (oid1, _id) = crate::test::commit(&repo); |
| 4521 |
let commit1 = repo.find_commit(oid1).unwrap(); |
| 4522 |
t!(repo.reset(commit1.as_object(), ResetType::Hard, None)); |
| 4523 |
assert!(foo_file.exists()); |
| 4524 |
|
| 4525 |
repo.revert(&commit1, None).unwrap(); |
| 4526 |
let id = repo.index().unwrap().write_tree().unwrap(); |
| 4527 |
let tree2 = repo.find_tree(id).unwrap(); |
| 4528 |
let sig = repo.signature().unwrap(); |
| 4529 |
repo.commit(Some("HEAD"), &sig, &sig, "commit 1", &tree2, &[&commit1]) |
| 4530 |
.unwrap(); |
| 4531 |
// reverting once removes `foo` file |
| 4532 |
assert!(!foo_file.exists()); |
| 4533 |
|
| 4534 |
let oid2 = repo.head().unwrap().target().unwrap(); |
| 4535 |
let commit2 = repo.find_commit(oid2).unwrap(); |
| 4536 |
repo.revert(&commit2, None).unwrap(); |
| 4537 |
let id = repo.index().unwrap().write_tree().unwrap(); |
| 4538 |
let tree3 = repo.find_tree(id).unwrap(); |
| 4539 |
repo.commit(Some("HEAD"), &sig, &sig, "commit 2", &tree3, &[&commit2]) |
| 4540 |
.unwrap(); |
| 4541 |
// reverting twice restores `foo` file |
| 4542 |
assert!(foo_file.exists()); |
| 4543 |
} |
| 4544 |
|
| 4545 |
#[test] |
| 4546 |
fn smoke_config_write_and_read() { |
| 4547 |
let (td, repo) = crate::test::repo_init(); |
| 4548 |
|
| 4549 |
let mut config = repo.config().unwrap(); |
| 4550 |
|
| 4551 |
config.set_bool("commit.gpgsign", false).unwrap(); |
| 4552 |
|
| 4553 |
let c = fs::read_to_string(td.path().join(".git").join("config")).unwrap(); |
| 4554 |
|
| 4555 |
assert!(c.contains("[commit]")); |
| 4556 |
assert!(c.contains("gpgsign = false")); |
| 4557 |
|
| 4558 |
let config = repo.config().unwrap(); |
| 4559 |
|
| 4560 |
assert!(!config.get_bool("commit.gpgsign").unwrap()); |
| 4561 |
} |
| 4562 |
|
| 4563 |
#[test] |
| 4564 |
fn smoke_set_config() { |
| 4565 |
let (td, repo) = crate::test::repo_init(); |
| 4566 |
|
| 4567 |
let config = { |
| 4568 |
let config_file = td.path().join(".gitconfig"); |
| 4569 |
|
| 4570 |
t!(fs::write(&config_file, "[diff]\nnoPrefix = true")); |
| 4571 |
|
| 4572 |
t!(Config::open(&config_file)) |
| 4573 |
}; |
| 4574 |
|
| 4575 |
repo.set_config(&config).unwrap(); |
| 4576 |
|
| 4577 |
let config = repo.config().unwrap(); |
| 4578 |
|
| 4579 |
assert!(config.get_bool("diff.noPrefix").unwrap()); |
| 4580 |
} |
| 4581 |
|
| 4582 |
#[test] |
| 4583 |
fn smoke_merge_analysis_for_ref() -> Result<(), crate::Error> { |
| 4584 |
let (_td, repo) = graph_repo_init(); |
| 4585 |
|
| 4586 |
// Set up this repo state: |
| 4587 |
// * second (their-branch) |
| 4588 |
// * initial (HEAD -> main) |
| 4589 |
// |
| 4590 |
// We expect that their-branch can be fast-forward merged into main. |
| 4591 |
|
| 4592 |
// git checkout --detach HEAD |
| 4593 |
let head_commit = repo.head()?.peel_to_commit()?; |
| 4594 |
repo.set_head_detached(head_commit.id())?; |
| 4595 |
|
| 4596 |
// git branch their-branch HEAD |
| 4597 |
let their_branch = repo.branch("their-branch", &head_commit, false)?; |
| 4598 |
|
| 4599 |
// git branch -f main HEAD~ |
| 4600 |
let mut parents_iter = head_commit.parents(); |
| 4601 |
let parent = parents_iter.next().unwrap(); |
| 4602 |
assert!(parents_iter.next().is_none()); |
| 4603 |
|
| 4604 |
let main = repo.branch("main", &parent, true)?; |
| 4605 |
|
| 4606 |
// git checkout main |
| 4607 |
repo.set_head(main.get().name().expect("should be utf-8"))?; |
| 4608 |
|
| 4609 |
let (merge_analysis, _merge_preference) = repo.merge_analysis_for_ref( |
| 4610 |
main.get(), |
| 4611 |
&[&repo.reference_to_annotated_commit(their_branch.get())?], |
| 4612 |
)?; |
| 4613 |
|
| 4614 |
assert!(merge_analysis.contains(crate::MergeAnalysis::ANALYSIS_FASTFORWARD)); |
| 4615 |
|
| 4616 |
Ok(()) |
| 4617 |
} |
| 4618 |
|
| 4619 |
#[test] |
| 4620 |
fn smoke_submodule_set() -> Result<(), crate::Error> { |
| 4621 |
let (td1, _repo) = crate::test::repo_init(); |
| 4622 |
let (td2, mut repo2) = crate::test::repo_init(); |
| 4623 |
let url = crate::test::path2url(td1.path()); |
| 4624 |
let name = "bar"; |
| 4625 |
{ |
| 4626 |
let mut s = repo2.submodule(&url, Path::new(name), true)?; |
| 4627 |
fs::remove_dir_all(td2.path().join("bar")).unwrap(); |
| 4628 |
Repository::clone(&url, td2.path().join("bar"))?; |
| 4629 |
s.add_to_index(false)?; |
| 4630 |
s.add_finalize()?; |
| 4631 |
} |
| 4632 |
|
| 4633 |
// update strategy |
| 4634 |
repo2.submodule_set_update(name, SubmoduleUpdate::None)?; |
| 4635 |
assert!(matches!( |
| 4636 |
repo2.find_submodule(name)?.update_strategy(), |
| 4637 |
SubmoduleUpdate::None |
| 4638 |
)); |
| 4639 |
repo2.submodule_set_update(name, SubmoduleUpdate::Rebase)?; |
| 4640 |
assert!(matches!( |
| 4641 |
repo2.find_submodule(name)?.update_strategy(), |
| 4642 |
SubmoduleUpdate::Rebase |
| 4643 |
)); |
| 4644 |
|
| 4645 |
// ignore rule |
| 4646 |
repo2.submodule_set_ignore(name, SubmoduleIgnore::Untracked)?; |
| 4647 |
assert!(matches!( |
| 4648 |
repo2.find_submodule(name)?.ignore_rule(), |
| 4649 |
SubmoduleIgnore::Untracked |
| 4650 |
)); |
| 4651 |
repo2.submodule_set_ignore(name, SubmoduleIgnore::Dirty)?; |
| 4652 |
assert!(matches!( |
| 4653 |
repo2.find_submodule(name)?.ignore_rule(), |
| 4654 |
SubmoduleIgnore::Dirty |
| 4655 |
)); |
| 4656 |
|
| 4657 |
// url |
| 4658 |
repo2.submodule_set_url(name, "fake-url")?; |
| 4659 |
assert_eq!(repo2.find_submodule(name)?.url(), Ok(Some("fake-url"))); |
| 4660 |
|
| 4661 |
// branch |
| 4662 |
repo2.submodule_set_branch(name, "fake-branch")?; |
| 4663 |
assert_eq!( |
| 4664 |
repo2.find_submodule(name)?.branch(), |
| 4665 |
Ok(Some("fake-branch")) |
| 4666 |
); |
| 4667 |
|
| 4668 |
Ok(()) |
| 4669 |
} |
| 4670 |
|
| 4671 |
#[test] |
| 4672 |
fn smoke_mailmap_from_repository() { |
| 4673 |
let (_td, repo) = crate::test::repo_init(); |
| 4674 |
|
| 4675 |
let commit = { |
| 4676 |
let head = t!(repo.head()).target().unwrap(); |
| 4677 |
t!(repo.find_commit(head)) |
| 4678 |
}; |
| 4679 |
|
| 4680 |
// This is our baseline for HEAD. |
| 4681 |
let author = commit.author(); |
| 4682 |
let committer = commit.committer(); |
| 4683 |
assert_eq!(author.name(), Ok("name")); |
| 4684 |
assert_eq!(author.email(), Ok("email")); |
| 4685 |
assert_eq!(committer.name(), Ok("name")); |
| 4686 |
assert_eq!(committer.email(), Ok("email")); |
| 4687 |
|
| 4688 |
// There is no .mailmap file in the test repo so all signature identities are equal. |
| 4689 |
let mailmap = t!(repo.mailmap()); |
| 4690 |
let mailmapped_author = t!(commit.author_with_mailmap(&mailmap)); |
| 4691 |
let mailmapped_committer = t!(commit.committer_with_mailmap(&mailmap)); |
| 4692 |
assert_eq!(mailmapped_author.name(), author.name()); |
| 4693 |
assert_eq!(mailmapped_author.email(), author.email()); |
| 4694 |
assert_eq!(mailmapped_committer.name(), committer.name()); |
| 4695 |
assert_eq!(mailmapped_committer.email(), committer.email()); |
| 4696 |
|
| 4697 |
let commit = { |
| 4698 |
// - Add a .mailmap file to the repository. |
| 4699 |
// - Commit with a signature identity different from the author's. |
| 4700 |
// - Include entries for both author and committer to prove we call |
| 4701 |
// the right raw functions. |
| 4702 |
let mailmap_file = Path::new(".mailmap"); |
| 4703 |
let p = Path::new(repo.workdir().unwrap()).join(&mailmap_file); |
| 4704 |
t!(fs::write( |
| 4705 |
p, |
| 4706 |
r#" |
| 4707 |
Author Name <author.proper@email> name <email> |
| 4708 |
Committer Name <committer.proper@email> <committer@email>"#, |
| 4709 |
)); |
| 4710 |
let mut index = t!(repo.index()); |
| 4711 |
t!(index.add_path(&mailmap_file)); |
| 4712 |
let id_mailmap = t!(index.write_tree()); |
| 4713 |
let tree_mailmap = t!(repo.find_tree(id_mailmap)); |
| 4714 |
|
| 4715 |
let head = t!(repo.commit( |
| 4716 |
Some("HEAD"), |
| 4717 |
&author, |
| 4718 |
t!(&Signature::now("committer", "committer@email")), |
| 4719 |
"Add mailmap", |
| 4720 |
&tree_mailmap, |
| 4721 |
&[&commit], |
| 4722 |
)); |
| 4723 |
t!(repo.find_commit(head)) |
| 4724 |
}; |
| 4725 |
|
| 4726 |
// Sanity check that we're working with the right commit and that its |
| 4727 |
// author and committer identities differ. |
| 4728 |
let author = commit.author(); |
| 4729 |
let committer = commit.committer(); |
| 4730 |
assert_ne!(author.name(), committer.name()); |
| 4731 |
assert_ne!(author.email(), committer.email()); |
| 4732 |
assert_eq!(author.name(), Ok("name")); |
| 4733 |
assert_eq!(author.email(), Ok("email")); |
| 4734 |
assert_eq!(committer.name(), Ok("committer")); |
| 4735 |
assert_eq!(committer.email(), Ok("committer@email")); |
| 4736 |
|
| 4737 |
// Fetch the newly added .mailmap from the repository. |
| 4738 |
let mailmap = t!(repo.mailmap()); |
| 4739 |
let mailmapped_author = t!(commit.author_with_mailmap(&mailmap)); |
| 4740 |
let mailmapped_committer = t!(commit.committer_with_mailmap(&mailmap)); |
| 4741 |
|
| 4742 |
let mm_resolve_author = t!(mailmap.resolve_signature(&author)); |
| 4743 |
let mm_resolve_committer = t!(mailmap.resolve_signature(&committer)); |
| 4744 |
|
| 4745 |
// Mailmap Signature lifetime is independent of Commit lifetime. |
| 4746 |
drop(author); |
| 4747 |
drop(committer); |
| 4748 |
drop(commit); |
| 4749 |
|
| 4750 |
// author_with_mailmap() + committer_with_mailmap() work |
| 4751 |
assert_eq!(mailmapped_author.name(), Ok("Author Name")); |
| 4752 |
assert_eq!(mailmapped_author.email(), Ok("author.proper@email")); |
| 4753 |
assert_eq!(mailmapped_committer.name(), Ok("Committer Name")); |
| 4754 |
assert_eq!(mailmapped_committer.email(), Ok("committer.proper@email")); |
| 4755 |
|
| 4756 |
// resolve_signature() works |
| 4757 |
assert_eq!(mm_resolve_author.email(), mailmapped_author.email()); |
| 4758 |
assert_eq!(mm_resolve_committer.email(), mailmapped_committer.email()); |
| 4759 |
} |
| 4760 |
|
| 4761 |
#[test] |
| 4762 |
fn smoke_find_tag_by_prefix() { |
| 4763 |
let (_td, repo) = crate::test::repo_init(); |
| 4764 |
let head = repo.head().unwrap(); |
| 4765 |
let tag_oid = repo |
| 4766 |
.tag( |
| 4767 |
"tag", |
| 4768 |
&repo |
| 4769 |
.find_object(head.peel_to_commit().unwrap().id(), None) |
| 4770 |
.unwrap(), |
| 4771 |
&repo.signature().unwrap(), |
| 4772 |
"message", |
| 4773 |
false, |
| 4774 |
) |
| 4775 |
.unwrap(); |
| 4776 |
let tag = repo.find_tag(tag_oid).unwrap(); |
| 4777 |
let found_tag = repo |
| 4778 |
.find_tag_by_prefix(&tag.id().to_string()[0..7]) |
| 4779 |
.unwrap(); |
| 4780 |
assert_eq!(tag.id(), found_tag.id()); |
| 4781 |
} |
| 4782 |
|
| 4783 |
#[test] |
| 4784 |
fn smoke_commondir() { |
| 4785 |
let (td, repo) = crate::test::repo_init(); |
| 4786 |
assert_eq!( |
| 4787 |
crate::test::realpath(repo.path()).unwrap(), |
| 4788 |
crate::test::realpath(repo.commondir()).unwrap() |
| 4789 |
); |
| 4790 |
|
| 4791 |
let worktree = repo |
| 4792 |
.worktree("test", &td.path().join("worktree"), None) |
| 4793 |
.unwrap(); |
| 4794 |
let worktree_repo = Repository::open_from_worktree(&worktree).unwrap(); |
| 4795 |
assert_eq!( |
| 4796 |
crate::test::realpath(repo.path()).unwrap(), |
| 4797 |
crate::test::realpath(worktree_repo.commondir()).unwrap() |
| 4798 |
); |
| 4799 |
} |
| 4800 |
|
| 4801 |
#[test] |
| 4802 |
fn smoke_refdb_compress() { |
| 4803 |
let (_td, repo) = crate::test::repo_init(); |
| 4804 |
// Compressing an empty-ish refdb should succeed. |
| 4805 |
repo.refdb_compress().unwrap(); |
| 4806 |
} |
| 4807 |
|
| 4808 |
#[test] |
| 4809 |
fn refdb_compress_with_loose_refs() { |
| 4810 |
let (_td, repo) = crate::test::repo_init(); |
| 4811 |
let head_id = repo.refname_to_id("HEAD").unwrap(); |
| 4812 |
|
| 4813 |
// Create several loose refs. |
| 4814 |
for i in 0..20 { |
| 4815 |
repo.reference(&format!("refs/tags/test-{}", i), head_id, false, "test ref") |
| 4816 |
.unwrap(); |
| 4817 |
} |
| 4818 |
|
| 4819 |
// Verify refs exist. |
| 4820 |
assert!(repo.references_glob("refs/tags/test-*").unwrap().count() == 20); |
| 4821 |
|
| 4822 |
// packed-refs should not exist yet (refs are still loose). |
| 4823 |
let packed_refs = repo.path().join("packed-refs"); |
| 4824 |
assert!( |
| 4825 |
!packed_refs.exists(), |
| 4826 |
"packed-refs should not exist before compress" |
| 4827 |
); |
| 4828 |
|
| 4829 |
// Compress should pack them without error. |
| 4830 |
repo.refdb_compress().unwrap(); |
| 4831 |
|
| 4832 |
// packed-refs should now exist after compressing. |
| 4833 |
assert!( |
| 4834 |
packed_refs.exists(), |
| 4835 |
"packed-refs should exist after compress" |
| 4836 |
); |
| 4837 |
|
| 4838 |
// Refs should still be resolvable after packing. |
| 4839 |
assert!(repo.references_glob("refs/tags/test-*").unwrap().count() == 20); |
| 4840 |
for i in 0..20 { |
| 4841 |
let r = repo |
| 4842 |
.find_reference(&format!("refs/tags/test-{}", i)) |
| 4843 |
.unwrap(); |
| 4844 |
assert_eq!(r.target().unwrap(), head_id); |
| 4845 |
} |
| 4846 |
} |
| 4847 |
|
| 4848 |
#[test] |
| 4849 |
fn refdb_compress_bare_repo() { |
| 4850 |
let td = TempDir::new().unwrap(); |
| 4851 |
let repo = Repository::init_bare(td.path()).unwrap(); |
| 4852 |
// Compressing a bare repo with no refs should succeed. |
| 4853 |
repo.refdb_compress().unwrap(); |
| 4854 |
} |
| 4855 |
|
| 4856 |
#[test] |
| 4857 |
fn refdb_compress_idempotent() { |
| 4858 |
let (_td, repo) = crate::test::repo_init(); |
| 4859 |
let head_id = repo.refname_to_id("HEAD").unwrap(); |
| 4860 |
|
| 4861 |
for i in 0..5 { |
| 4862 |
repo.reference(&format!("refs/tags/idem-{}", i), head_id, false, "test") |
| 4863 |
.unwrap(); |
| 4864 |
} |
| 4865 |
|
| 4866 |
// Compress multiple times — should be safe and idempotent. |
| 4867 |
repo.refdb_compress().unwrap(); |
| 4868 |
repo.refdb_compress().unwrap(); |
| 4869 |
repo.refdb_compress().unwrap(); |
| 4870 |
|
| 4871 |
assert!(repo.references_glob("refs/tags/idem-*").unwrap().count() == 5); |
| 4872 |
} |
| 4873 |
|
| 4874 |
#[test] |
| 4875 |
fn author_from_env() { |
| 4876 |
let (_td, repo) = crate::test::repo_init(); |
| 4877 |
// This is going to be the tested author date, 2026-04-21T17:59:06-07:00 |
| 4878 |
const AUTHOR_DATE: &str = "2026-04-21T17:59:06-07:00"; |
| 4879 |
let time = crate::Time::new(1776819546, -7 * 60); |
| 4880 |
|
| 4881 |
macro_rules! expect { |
| 4882 |
($name:literal, $email:literal, ==) => { |
| 4883 |
let sig = repo.author_from_env().unwrap(); |
| 4884 |
assert_eq!(Ok($name), sig.name()); |
| 4885 |
assert_eq!(Ok($email), sig.email()); |
| 4886 |
assert_eq!(time, sig.when()); |
| 4887 |
}; |
| 4888 |
($name:literal, $email:literal, !=) => { |
| 4889 |
let sig = repo.author_from_env().unwrap(); |
| 4890 |
assert_eq!(Ok($name), sig.name()); |
| 4891 |
assert_eq!(Ok($email), sig.email()); |
| 4892 |
assert_ne!(time, sig.when()); |
| 4893 |
}; |
| 4894 |
} |
| 4895 |
|
| 4896 |
// See repo_init() for the default user.name "name" and user.email "email" |
| 4897 |
expect!("name", "email", !=); |
| 4898 |
|
| 4899 |
// SAFETY: no other tests are writing to these variables |
| 4900 |
unsafe { |
| 4901 |
std::env::set_var("GIT_AUTHOR_DATE", AUTHOR_DATE); |
| 4902 |
} |
| 4903 |
expect!("name", "email", ==); |
| 4904 |
|
| 4905 |
// SAFETY: no other tests are writing to these variables |
| 4906 |
unsafe { |
| 4907 |
std::env::set_var("GIT_AUTHOR_NAME", "Daniel Scherzer"); |
| 4908 |
} |
| 4909 |
expect!("Daniel Scherzer", "email", ==); |
| 4910 |
|
| 4911 |
// SAFETY: no other tests are writing to these variables |
| 4912 |
unsafe { |
| 4913 |
std::env::set_var("GIT_AUTHOR_EMAIL", "[email protected]"); |
| 4914 |
} |
| 4915 |
expect!("Daniel Scherzer", "[email protected]", ==); |
| 4916 |
|
| 4917 |
// Clear afterwards, otherwise could pollute other tests |
| 4918 |
// SAFETY: no other tests are writing to these variables |
| 4919 |
unsafe { |
| 4920 |
std::env::remove_var("GIT_AUTHOR_DATE"); |
| 4921 |
std::env::remove_var("GIT_AUTHOR_NAME"); |
| 4922 |
std::env::remove_var("GIT_AUTHOR_EMAIL"); |
| 4923 |
} |
| 4924 |
} |
| 4925 |
|
| 4926 |
#[test] |
| 4927 |
fn committer_from_env() { |
| 4928 |
let (_td, repo) = crate::test::repo_init(); |
| 4929 |
// This is going to be the tested commit date, 2026-04-21T17:59:06-07:00 |
| 4930 |
const COMMIT_DATE: &str = "2026-04-21T17:59:06-07:00"; |
| 4931 |
let time = crate::Time::new(1776819546, -7 * 60); |
| 4932 |
|
| 4933 |
macro_rules! expect { |
| 4934 |
($name:literal, $email:literal, ==) => { |
| 4935 |
let sig = repo.committer_from_env().unwrap(); |
| 4936 |
assert_eq!(Ok($name), sig.name()); |
| 4937 |
assert_eq!(Ok($email), sig.email()); |
| 4938 |
assert_eq!(time, sig.when()); |
| 4939 |
}; |
| 4940 |
($name:literal, $email:literal, !=) => { |
| 4941 |
let sig = repo.committer_from_env().unwrap(); |
| 4942 |
assert_eq!(Ok($name), sig.name()); |
| 4943 |
assert_eq!(Ok($email), sig.email()); |
| 4944 |
assert_ne!(time, sig.when()); |
| 4945 |
}; |
| 4946 |
} |
| 4947 |
|
| 4948 |
// See repo_init() for the default user.name "name" and user.email "email" |
| 4949 |
expect!("name", "email", !=); |
| 4950 |
|
| 4951 |
// SAFETY: no other tests are writing to these variables |
| 4952 |
unsafe { |
| 4953 |
std::env::set_var("GIT_COMMITTER_DATE", COMMIT_DATE); |
| 4954 |
} |
| 4955 |
expect!("name", "email", ==); |
| 4956 |
|
| 4957 |
// SAFETY: no other tests are writing to these variables |
| 4958 |
unsafe { |
| 4959 |
std::env::set_var("GIT_COMMITTER_NAME", "Daniel Scherzer"); |
| 4960 |
} |
| 4961 |
expect!("Daniel Scherzer", "email", ==); |
| 4962 |
|
| 4963 |
// SAFETY: no other tests are writing to these variables |
| 4964 |
unsafe { |
| 4965 |
std::env::set_var("GIT_COMMITTER_EMAIL", "[email protected]"); |
| 4966 |
} |
| 4967 |
|
| 4968 |
expect!("Daniel Scherzer", "[email protected]", ==); |
| 4969 |
|
| 4970 |
// Clear afterwards, otherwise could pollute other tests |
| 4971 |
// SAFETY: no other tests are writing to these variables |
| 4972 |
unsafe { |
| 4973 |
std::env::remove_var("GIT_COMMITTER_DATE"); |
| 4974 |
std::env::remove_var("GIT_COMMITTER_NAME"); |
| 4975 |
std::env::remove_var("GIT_COMMITTER_EMAIL"); |
| 4976 |
} |
| 4977 |
} |
| 4978 |
} |