| 1 |
use std::ffi::{CStr, CString}; |
| 2 |
use std::marker; |
| 3 |
use std::ops::Range; |
| 4 |
use std::path::Path; |
| 5 |
use std::ptr; |
| 6 |
use std::slice; |
| 7 |
|
| 8 |
use libc::{c_char, c_int, c_uint, c_void, size_t}; |
| 9 |
|
| 10 |
use crate::util::{self, path_to_repo_path, Binding}; |
| 11 |
use crate::IntoCString; |
| 12 |
use crate::ObjectFormat; |
| 13 |
use crate::{panic, raw, Error, IndexAddOption, IndexTime, Oid, Repository, Tree}; |
| 14 |
|
| 15 |
/// A structure to represent a git [index][1] |
| 16 |
/// |
| 17 |
/// [1]: http://git-scm.com/book/en/Git-Internals-Git-Objects |
| 18 |
pub struct Index { |
| 19 |
raw: *mut raw::git_index, |
| 20 |
} |
| 21 |
|
| 22 |
/// An iterator over the entries in an index |
| 23 |
pub struct IndexEntries<'index> { |
| 24 |
range: Range<usize>, |
| 25 |
index: &'index Index, |
| 26 |
} |
| 27 |
|
| 28 |
/// An iterator over the conflicting entries in an index |
| 29 |
pub struct IndexConflicts<'index> { |
| 30 |
conflict_iter: *mut raw::git_index_conflict_iterator, |
| 31 |
_marker: marker::PhantomData<&'index Index>, |
| 32 |
} |
| 33 |
|
| 34 |
/// A structure to represent the information returned when a conflict is detected in an index entry |
| 35 |
pub struct IndexConflict { |
| 36 |
/// The ancestor index entry of the two conflicting index entries |
| 37 |
pub ancestor: Option<IndexEntry>, |
| 38 |
/// The index entry originating from the user's copy of the repository. |
| 39 |
/// Its contents conflict with 'their' index entry |
| 40 |
pub our: Option<IndexEntry>, |
| 41 |
/// The index entry originating from the external repository. |
| 42 |
/// Its contents conflict with 'our' index entry |
| 43 |
pub their: Option<IndexEntry>, |
| 44 |
} |
| 45 |
|
| 46 |
/// A callback function to filter index matches. |
| 47 |
/// |
| 48 |
/// Used by `Index::{add_all,remove_all,update_all}`. The first argument is the |
| 49 |
/// path, and the second is the pathspec that matched it. Return 0 to confirm |
| 50 |
/// the operation on the item, > 0 to skip the item, and < 0 to abort the scan. |
| 51 |
pub type IndexMatchedPath<'a> = dyn FnMut(&Path, &[u8]) -> i32 + 'a; |
| 52 |
|
| 53 |
/// A structure to represent an entry or a file inside of an index. |
| 54 |
/// |
| 55 |
/// All fields of an entry are public for modification and inspection. This is |
| 56 |
/// also how a new index entry is created. |
| 57 |
#[allow(missing_docs)] |
| 58 |
#[derive(Debug)] |
| 59 |
pub struct IndexEntry { |
| 60 |
pub ctime: IndexTime, |
| 61 |
pub mtime: IndexTime, |
| 62 |
pub dev: u32, |
| 63 |
pub ino: u32, |
| 64 |
pub mode: u32, |
| 65 |
pub uid: u32, |
| 66 |
pub gid: u32, |
| 67 |
pub file_size: u32, |
| 68 |
pub id: Oid, |
| 69 |
pub flags: u16, |
| 70 |
pub flags_extended: u16, |
| 71 |
|
| 72 |
/// The path of this index entry as a byte vector. Regardless of the |
| 73 |
/// current platform, the directory separator is an ASCII forward slash |
| 74 |
/// (`0x2F`). There are no terminating or internal NUL characters, and no |
| 75 |
/// trailing slashes. Most of the time, paths will be valid utf-8 — but |
| 76 |
/// not always. For more information on the path storage format, see |
| 77 |
/// [these git docs][git-index-docs]. Note that libgit2 will take care of |
| 78 |
/// handling the prefix compression mentioned there. |
| 79 |
/// |
| 80 |
/// [git-index-docs]: https://github.com/git/git/blob/a08a83db2bf27f015bec9a435f6d73e223c21c5e/Documentation/technical/index-format.txt#L107-L124 |
| 81 |
/// |
| 82 |
/// You can turn this value into a `std::ffi::CString` with |
| 83 |
/// `CString::new(&entry.path[..]).unwrap()`. To turn a reference into a |
| 84 |
/// `&std::path::Path`, see the `bytes2path()` function in the private, |
| 85 |
/// internal `util` module in this crate’s source code. |
| 86 |
pub path: Vec<u8>, |
| 87 |
} |
| 88 |
|
| 89 |
impl Index { |
| 90 |
/// Creates a new in-memory index. |
| 91 |
/// |
| 92 |
/// This index object cannot be read/written to the filesystem, but may be |
| 93 |
/// used to perform in-memory index operations. |
| 94 |
/// |
| 95 |
/// This always creates a SHA1 index. |
| 96 |
/// Use [`Index::new_ext`] to create an index with a specific object format. |
| 97 |
pub fn new() -> Result<Index, Error> { |
| 98 |
Self::new_ext(ObjectFormat::Sha1) |
| 99 |
} |
| 100 |
|
| 101 |
/// Creates a new in-memory index with a specific object format. |
| 102 |
/// |
| 103 |
/// See [`Index::new`] for more details. |
| 104 |
pub fn new_ext(format: ObjectFormat) -> Result<Index, Error> { |
| 105 |
crate::init(); |
| 106 |
let mut raw = ptr::null_mut(); |
| 107 |
unsafe { |
| 108 |
#[cfg(not(feature = "unstable-sha256"))] |
| 109 |
{ |
| 110 |
let _ = format; |
| 111 |
try_call!(raw::git_index_new(&mut raw)); |
| 112 |
} |
| 113 |
#[cfg(feature = "unstable-sha256")] |
| 114 |
{ |
| 115 |
let mut opts: raw::git_index_options = std::mem::zeroed(); |
| 116 |
opts.version = raw::GIT_INDEX_OPTIONS_VERSION; |
| 117 |
opts.oid_type = format.raw(); |
| 118 |
try_call!(raw::git_index_new(&mut raw, &opts)); |
| 119 |
} |
| 120 |
Ok(Binding::from_raw(raw)) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/// Create a new bare Git index object as a memory representation of the Git |
| 125 |
/// index file in 'index_path', without a repository to back it. |
| 126 |
/// |
| 127 |
/// Since there is no ODB or working directory behind this index, any Index |
| 128 |
/// methods which rely on these (e.g. add_path) will fail. |
| 129 |
/// |
| 130 |
/// If you need an index attached to a repository, use the `index()` method |
| 131 |
/// on `Repository`. |
| 132 |
/// |
| 133 |
/// This opens the index assuming SHA1 object format. Use |
| 134 |
/// [`Index::open_ext`] to specify a different format. |
| 135 |
pub fn open(index_path: &Path) -> Result<Index, Error> { |
| 136 |
Self::open_ext(index_path, ObjectFormat::Sha1) |
| 137 |
} |
| 138 |
|
| 139 |
/// Opens a Git index with a specific object format. |
| 140 |
/// |
| 141 |
/// See [`Index::open`] for more details. |
| 142 |
pub fn open_ext(index_path: &Path, format: ObjectFormat) -> Result<Index, Error> { |
| 143 |
crate::init(); |
| 144 |
let mut raw = ptr::null_mut(); |
| 145 |
// Normal file path OK (does not need Windows conversion). |
| 146 |
let index_path = index_path.into_c_string()?; |
| 147 |
unsafe { |
| 148 |
#[cfg(not(feature = "unstable-sha256"))] |
| 149 |
{ |
| 150 |
let _ = format; |
| 151 |
try_call!(raw::git_index_open(&mut raw, index_path)); |
| 152 |
} |
| 153 |
#[cfg(feature = "unstable-sha256")] |
| 154 |
{ |
| 155 |
let mut opts: raw::git_index_options = std::mem::zeroed(); |
| 156 |
opts.version = raw::GIT_INDEX_OPTIONS_VERSION; |
| 157 |
opts.oid_type = format.raw(); |
| 158 |
try_call!(raw::git_index_open(&mut raw, index_path, &opts)); |
| 159 |
} |
| 160 |
Ok(Binding::from_raw(raw)) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/// Get index on-disk version. |
| 165 |
/// |
| 166 |
/// Valid return values are 2, 3, or 4. If 3 is returned, an index |
| 167 |
/// with version 2 may be written instead, if the extension data in |
| 168 |
/// version 3 is not necessary. |
| 169 |
pub fn version(&self) -> u32 { |
| 170 |
unsafe { raw::git_index_version(self.raw) } |
| 171 |
} |
| 172 |
|
| 173 |
/// Set index on-disk version. |
| 174 |
/// |
| 175 |
/// Valid values are 2, 3, or 4. If 2 is given, git_index_write may |
| 176 |
/// write an index with version 3 instead, if necessary to accurately |
| 177 |
/// represent the index. |
| 178 |
pub fn set_version(&mut self, version: u32) -> Result<(), Error> { |
| 179 |
unsafe { |
| 180 |
try_call!(raw::git_index_set_version(self.raw, version)); |
| 181 |
} |
| 182 |
Ok(()) |
| 183 |
} |
| 184 |
|
| 185 |
/// Add or update an index entry from an in-memory struct |
| 186 |
/// |
| 187 |
/// If a previous index entry exists that has the same path and stage as the |
| 188 |
/// given 'source_entry', it will be replaced. Otherwise, the 'source_entry' |
| 189 |
/// will be added. |
| 190 |
pub fn add(&mut self, entry: &IndexEntry) -> Result<(), Error> { |
| 191 |
let path = CString::new(&entry.path[..])?; |
| 192 |
|
| 193 |
// libgit2 encodes the length of the path in the lower bits of the |
| 194 |
// `flags` entry, so mask those out and recalculate here to ensure we |
| 195 |
// don't corrupt anything. |
| 196 |
let mut flags = entry.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK; |
| 197 |
|
| 198 |
if entry.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize { |
| 199 |
flags |= entry.path.len() as u16; |
| 200 |
} else { |
| 201 |
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK; |
| 202 |
} |
| 203 |
|
| 204 |
unsafe { |
| 205 |
let raw = raw::git_index_entry { |
| 206 |
dev: entry.dev, |
| 207 |
ino: entry.ino, |
| 208 |
mode: entry.mode, |
| 209 |
uid: entry.uid, |
| 210 |
gid: entry.gid, |
| 211 |
file_size: entry.file_size, |
| 212 |
id: *entry.id.raw(), |
| 213 |
flags, |
| 214 |
flags_extended: entry.flags_extended, |
| 215 |
path: path.as_ptr(), |
| 216 |
mtime: raw::git_index_time { |
| 217 |
seconds: entry.mtime.seconds(), |
| 218 |
nanoseconds: entry.mtime.nanoseconds(), |
| 219 |
}, |
| 220 |
ctime: raw::git_index_time { |
| 221 |
seconds: entry.ctime.seconds(), |
| 222 |
nanoseconds: entry.ctime.nanoseconds(), |
| 223 |
}, |
| 224 |
}; |
| 225 |
try_call!(raw::git_index_add(self.raw, &raw)); |
| 226 |
Ok(()) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/// Add or update an index entry from a buffer in memory |
| 231 |
/// |
| 232 |
/// This method will create a blob in the repository that owns the index and |
| 233 |
/// then add the index entry to the index. The path of the entry represents |
| 234 |
/// the position of the blob relative to the repository's root folder. |
| 235 |
/// |
| 236 |
/// If a previous index entry exists that has the same path as the given |
| 237 |
/// 'entry', it will be replaced. Otherwise, the 'entry' will be added. |
| 238 |
/// The id and the file_size of the 'entry' are updated with the real value |
| 239 |
/// of the blob. |
| 240 |
/// |
| 241 |
/// This forces the file to be added to the index, not looking at gitignore |
| 242 |
/// rules. |
| 243 |
/// |
| 244 |
/// If this file currently is the result of a merge conflict, this file will |
| 245 |
/// no longer be marked as conflicting. The data about the conflict will be |
| 246 |
/// moved to the "resolve undo" (REUC) section. |
| 247 |
pub fn add_frombuffer(&mut self, entry: &IndexEntry, data: &[u8]) -> Result<(), Error> { |
| 248 |
let path = CString::new(&entry.path[..])?; |
| 249 |
|
| 250 |
// libgit2 encodes the length of the path in the lower bits of the |
| 251 |
// `flags` entry, so mask those out and recalculate here to ensure we |
| 252 |
// don't corrupt anything. |
| 253 |
let mut flags = entry.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK; |
| 254 |
|
| 255 |
if entry.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize { |
| 256 |
flags |= entry.path.len() as u16; |
| 257 |
} else { |
| 258 |
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK; |
| 259 |
} |
| 260 |
|
| 261 |
unsafe { |
| 262 |
let raw = raw::git_index_entry { |
| 263 |
dev: entry.dev, |
| 264 |
ino: entry.ino, |
| 265 |
mode: entry.mode, |
| 266 |
uid: entry.uid, |
| 267 |
gid: entry.gid, |
| 268 |
file_size: entry.file_size, |
| 269 |
id: *entry.id.raw(), |
| 270 |
flags, |
| 271 |
flags_extended: entry.flags_extended, |
| 272 |
path: path.as_ptr(), |
| 273 |
mtime: raw::git_index_time { |
| 274 |
seconds: entry.mtime.seconds(), |
| 275 |
nanoseconds: entry.mtime.nanoseconds(), |
| 276 |
}, |
| 277 |
ctime: raw::git_index_time { |
| 278 |
seconds: entry.ctime.seconds(), |
| 279 |
nanoseconds: entry.ctime.nanoseconds(), |
| 280 |
}, |
| 281 |
}; |
| 282 |
|
| 283 |
let ptr = data.as_ptr() as *const c_void; |
| 284 |
let len = data.len() as size_t; |
| 285 |
try_call!(raw::git_index_add_frombuffer(self.raw, &raw, ptr, len)); |
| 286 |
Ok(()) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/// Add or update an index entry from a file on disk |
| 291 |
/// |
| 292 |
/// The file path must be relative to the repository's working folder and |
| 293 |
/// must be readable. |
| 294 |
/// |
| 295 |
/// This method will fail in bare index instances. |
| 296 |
/// |
| 297 |
/// This forces the file to be added to the index, not looking at gitignore |
| 298 |
/// rules. |
| 299 |
/// |
| 300 |
/// If this file currently is the result of a merge conflict, this file will |
| 301 |
/// no longer be marked as conflicting. The data about the conflict will be |
| 302 |
/// moved to the "resolve undo" (REUC) section. |
| 303 |
pub fn add_path(&mut self, path: &Path) -> Result<(), Error> { |
| 304 |
let posix_path = path_to_repo_path(path)?; |
| 305 |
unsafe { |
| 306 |
try_call!(raw::git_index_add_bypath(self.raw, posix_path)); |
| 307 |
Ok(()) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/// Add or update index entries matching files in the working directory. |
| 312 |
/// |
| 313 |
/// This method will fail in bare index instances. |
| 314 |
/// |
| 315 |
/// The `pathspecs` are a list of file names or shell glob patterns that |
| 316 |
/// will matched against files in the repository's working directory. Each |
| 317 |
/// file that matches will be added to the index (either updating an |
| 318 |
/// existing entry or adding a new entry). You can disable glob expansion |
| 319 |
/// and force exact matching with the `AddDisablePathspecMatch` flag. |
| 320 |
/// |
| 321 |
/// Files that are ignored will be skipped (unlike `add_path`). If a file is |
| 322 |
/// already tracked in the index, then it will be updated even if it is |
| 323 |
/// ignored. Pass the `AddForce` flag to skip the checking of ignore rules. |
| 324 |
/// |
| 325 |
/// To emulate `git add -A` and generate an error if the pathspec contains |
| 326 |
/// the exact path of an ignored file (when not using `AddForce`), add the |
| 327 |
/// `AddCheckPathspec` flag. This checks that each entry in `pathspecs` |
| 328 |
/// that is an exact match to a filename on disk is either not ignored or |
| 329 |
/// already in the index. If this check fails, the function will return |
| 330 |
/// an error. |
| 331 |
/// |
| 332 |
/// To emulate `git add -A` with the "dry-run" option, just use a callback |
| 333 |
/// function that always returns a positive value. See below for details. |
| 334 |
/// |
| 335 |
/// If any files are currently the result of a merge conflict, those files |
| 336 |
/// will no longer be marked as conflicting. The data about the conflicts |
| 337 |
/// will be moved to the "resolve undo" (REUC) section. |
| 338 |
/// |
| 339 |
/// If you provide a callback function, it will be invoked on each matching |
| 340 |
/// item in the working directory immediately before it is added to / |
| 341 |
/// updated in the index. Returning zero will add the item to the index, |
| 342 |
/// greater than zero will skip the item, and less than zero will abort the |
| 343 |
/// scan an return an error to the caller. |
| 344 |
/// |
| 345 |
/// # Example |
| 346 |
/// |
| 347 |
/// Emulate `git add *`: |
| 348 |
/// |
| 349 |
/// ```no_run |
| 350 |
/// use git2::{Index, IndexAddOption, Repository}; |
| 351 |
/// |
| 352 |
/// let repo = Repository::open("/path/to/a/repo").expect("failed to open"); |
| 353 |
/// let mut index = repo.index().expect("cannot get the Index file"); |
| 354 |
/// index.add_all(["*"].iter(), IndexAddOption::DEFAULT, None); |
| 355 |
/// index.write(); |
| 356 |
/// ``` |
| 357 |
pub fn add_all<T, I>( |
| 358 |
&mut self, |
| 359 |
pathspecs: I, |
| 360 |
flag: IndexAddOption, |
| 361 |
mut cb: Option<&mut IndexMatchedPath<'_>>, |
| 362 |
) -> Result<(), Error> |
| 363 |
where |
| 364 |
T: IntoCString, |
| 365 |
I: IntoIterator<Item = T>, |
| 366 |
{ |
| 367 |
let (_a, _b, raw_strarray) = crate::util::iter2cstrs_paths(pathspecs)?; |
| 368 |
let ptr = cb.as_mut(); |
| 369 |
let callback = ptr |
| 370 |
.as_ref() |
| 371 |
.map(|_| index_matched_path_cb as extern "C" fn(_, _, _) -> _); |
| 372 |
unsafe { |
| 373 |
try_call!(raw::git_index_add_all( |
| 374 |
self.raw, |
| 375 |
&raw_strarray, |
| 376 |
flag.bits() as c_uint, |
| 377 |
callback, |
| 378 |
ptr.map(|p| p as *mut _).unwrap_or(ptr::null_mut()) as *mut c_void |
| 379 |
)); |
| 380 |
} |
| 381 |
Ok(()) |
| 382 |
} |
| 383 |
|
| 384 |
/// Clear the contents (all the entries) of an index object. |
| 385 |
/// |
| 386 |
/// This clears the index object in memory; changes must be explicitly |
| 387 |
/// written to disk for them to take effect persistently via `write_*`. |
| 388 |
pub fn clear(&mut self) -> Result<(), Error> { |
| 389 |
unsafe { |
| 390 |
try_call!(raw::git_index_clear(self.raw)); |
| 391 |
} |
| 392 |
Ok(()) |
| 393 |
} |
| 394 |
|
| 395 |
/// Get the count of entries currently in the index |
| 396 |
pub fn len(&self) -> usize { |
| 397 |
unsafe { raw::git_index_entrycount(&*self.raw) as usize } |
| 398 |
} |
| 399 |
|
| 400 |
/// Return `true` is there is no entry in the index |
| 401 |
pub fn is_empty(&self) -> bool { |
| 402 |
self.len() == 0 |
| 403 |
} |
| 404 |
|
| 405 |
/// Get one of the entries in the index by its position. |
| 406 |
pub fn get(&self, n: usize) -> Option<IndexEntry> { |
| 407 |
unsafe { |
| 408 |
let ptr = raw::git_index_get_byindex(self.raw, n as size_t); |
| 409 |
if ptr.is_null() { |
| 410 |
None |
| 411 |
} else { |
| 412 |
Some(Binding::from_raw(*ptr)) |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
/// Get an iterator over the entries in this index. |
| 418 |
pub fn iter(&self) -> IndexEntries<'_> { |
| 419 |
IndexEntries { |
| 420 |
range: 0..self.len(), |
| 421 |
index: self, |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/// Get an iterator over the index entries that have conflicts |
| 426 |
pub fn conflicts(&self) -> Result<IndexConflicts<'_>, Error> { |
| 427 |
crate::init(); |
| 428 |
let mut conflict_iter = ptr::null_mut(); |
| 429 |
unsafe { |
| 430 |
try_call!(raw::git_index_conflict_iterator_new( |
| 431 |
&mut conflict_iter, |
| 432 |
self.raw |
| 433 |
)); |
| 434 |
Ok(Binding::from_raw(conflict_iter)) |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
/// Get one of the entries in the index by its path. |
| 439 |
pub fn get_path(&self, path: &Path, stage: i32) -> Option<IndexEntry> { |
| 440 |
let path = path_to_repo_path(path).unwrap(); |
| 441 |
unsafe { |
| 442 |
let ptr = call!(raw::git_index_get_bypath(self.raw, path, stage as c_int)); |
| 443 |
if ptr.is_null() { |
| 444 |
None |
| 445 |
} else { |
| 446 |
Some(Binding::from_raw(*ptr)) |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
/// Does this index have conflicts? |
| 452 |
/// |
| 453 |
/// Returns `true` if the index contains conflicts, `false` if it does not. |
| 454 |
pub fn has_conflicts(&self) -> bool { |
| 455 |
unsafe { raw::git_index_has_conflicts(self.raw) == 1 } |
| 456 |
} |
| 457 |
|
| 458 |
/// Get the index entries that represent a conflict of a single file. |
| 459 |
pub fn conflict_get(&self, path: &Path) -> Result<IndexConflict, Error> { |
| 460 |
let path = path_to_repo_path(path)?; |
| 461 |
let mut ancestor = ptr::null(); |
| 462 |
let mut our = ptr::null(); |
| 463 |
let mut their = ptr::null(); |
| 464 |
|
| 465 |
unsafe { |
| 466 |
try_call!(raw::git_index_conflict_get( |
| 467 |
&mut ancestor, |
| 468 |
&mut our, |
| 469 |
&mut their, |
| 470 |
self.raw, |
| 471 |
path |
| 472 |
)); |
| 473 |
|
| 474 |
Ok(IndexConflict { |
| 475 |
ancestor: match ancestor.is_null() { |
| 476 |
false => Some(IndexEntry::from_raw(*ancestor)), |
| 477 |
true => None, |
| 478 |
}, |
| 479 |
our: match our.is_null() { |
| 480 |
false => Some(IndexEntry::from_raw(*our)), |
| 481 |
true => None, |
| 482 |
}, |
| 483 |
their: match their.is_null() { |
| 484 |
false => Some(IndexEntry::from_raw(*their)), |
| 485 |
true => None, |
| 486 |
}, |
| 487 |
}) |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
/// Get the full path to the index file on disk. |
| 492 |
/// |
| 493 |
/// Returns `None` if this is an in-memory index. |
| 494 |
pub fn path(&self) -> Option<&Path> { |
| 495 |
unsafe { crate::opt_bytes(self, raw::git_index_path(&*self.raw)).map(util::bytes2path) } |
| 496 |
} |
| 497 |
|
| 498 |
/// Update the contents of an existing index object in memory by reading |
| 499 |
/// from the hard disk. |
| 500 |
/// |
| 501 |
/// If force is true, this performs a "hard" read that discards in-memory |
| 502 |
/// changes and always reloads the on-disk index data. If there is no |
| 503 |
/// on-disk version, the index will be cleared. |
| 504 |
/// |
| 505 |
/// If force is false, this does a "soft" read that reloads the index data |
| 506 |
/// from disk only if it has changed since the last time it was loaded. |
| 507 |
/// Purely in-memory index data will be untouched. Be aware: if there are |
| 508 |
/// changes on disk, unwritten in-memory changes are discarded. |
| 509 |
pub fn read(&mut self, force: bool) -> Result<(), Error> { |
| 510 |
unsafe { |
| 511 |
try_call!(raw::git_index_read(self.raw, force)); |
| 512 |
} |
| 513 |
Ok(()) |
| 514 |
} |
| 515 |
|
| 516 |
/// Read a tree into the index file with stats |
| 517 |
/// |
| 518 |
/// The current index contents will be replaced by the specified tree. |
| 519 |
pub fn read_tree(&mut self, tree: &Tree<'_>) -> Result<(), Error> { |
| 520 |
unsafe { |
| 521 |
try_call!(raw::git_index_read_tree(self.raw, &*tree.raw())); |
| 522 |
} |
| 523 |
Ok(()) |
| 524 |
} |
| 525 |
|
| 526 |
/// Remove an entry from the index |
| 527 |
pub fn remove(&mut self, path: &Path, stage: i32) -> Result<(), Error> { |
| 528 |
let path = path_to_repo_path(path)?; |
| 529 |
unsafe { |
| 530 |
try_call!(raw::git_index_remove(self.raw, path, stage as c_int)); |
| 531 |
} |
| 532 |
Ok(()) |
| 533 |
} |
| 534 |
|
| 535 |
/// Remove an index entry corresponding to a file on disk. |
| 536 |
/// |
| 537 |
/// The file path must be relative to the repository's working folder. It |
| 538 |
/// may exist. |
| 539 |
/// |
| 540 |
/// If this file currently is the result of a merge conflict, this file will |
| 541 |
/// no longer be marked as conflicting. The data about the conflict will be |
| 542 |
/// moved to the "resolve undo" (REUC) section. |
| 543 |
pub fn remove_path(&mut self, path: &Path) -> Result<(), Error> { |
| 544 |
let path = path_to_repo_path(path)?; |
| 545 |
unsafe { |
| 546 |
try_call!(raw::git_index_remove_bypath(self.raw, path)); |
| 547 |
} |
| 548 |
Ok(()) |
| 549 |
} |
| 550 |
|
| 551 |
/// Remove all entries from the index under a given directory. |
| 552 |
pub fn remove_dir(&mut self, path: &Path, stage: i32) -> Result<(), Error> { |
| 553 |
let path = path_to_repo_path(path)?; |
| 554 |
unsafe { |
| 555 |
try_call!(raw::git_index_remove_directory( |
| 556 |
self.raw, |
| 557 |
path, |
| 558 |
stage as c_int |
| 559 |
)); |
| 560 |
} |
| 561 |
Ok(()) |
| 562 |
} |
| 563 |
|
| 564 |
/// Removes the index entries that represent a conflict of a single file. |
| 565 |
pub fn conflict_remove(&mut self, path: &Path) -> Result<(), Error> { |
| 566 |
let path = path_to_repo_path(path)?; |
| 567 |
unsafe { |
| 568 |
try_call!(raw::git_index_conflict_remove(self.raw, path)); |
| 569 |
} |
| 570 |
Ok(()) |
| 571 |
} |
| 572 |
|
| 573 |
/// Remove all matching index entries. |
| 574 |
/// |
| 575 |
/// If you provide a callback function, it will be invoked on each matching |
| 576 |
/// item in the index immediately before it is removed. Return 0 to remove |
| 577 |
/// the item, > 0 to skip the item, and < 0 to abort the scan. |
| 578 |
pub fn remove_all<T, I>( |
| 579 |
&mut self, |
| 580 |
pathspecs: I, |
| 581 |
mut cb: Option<&mut IndexMatchedPath<'_>>, |
| 582 |
) -> Result<(), Error> |
| 583 |
where |
| 584 |
T: IntoCString, |
| 585 |
I: IntoIterator<Item = T>, |
| 586 |
{ |
| 587 |
let (_a, _b, raw_strarray) = crate::util::iter2cstrs_paths(pathspecs)?; |
| 588 |
let ptr = cb.as_mut(); |
| 589 |
let callback = ptr |
| 590 |
.as_ref() |
| 591 |
.map(|_| index_matched_path_cb as extern "C" fn(_, _, _) -> _); |
| 592 |
unsafe { |
| 593 |
try_call!(raw::git_index_remove_all( |
| 594 |
self.raw, |
| 595 |
&raw_strarray, |
| 596 |
callback, |
| 597 |
ptr.map(|p| p as *mut _).unwrap_or(ptr::null_mut()) as *mut c_void |
| 598 |
)); |
| 599 |
} |
| 600 |
Ok(()) |
| 601 |
} |
| 602 |
|
| 603 |
/// Update all index entries to match the working directory |
| 604 |
/// |
| 605 |
/// This method will fail in bare index instances. |
| 606 |
/// |
| 607 |
/// This scans the existing index entries and synchronizes them with the |
| 608 |
/// working directory, deleting them if the corresponding working directory |
| 609 |
/// file no longer exists otherwise updating the information (including |
| 610 |
/// adding the latest version of file to the ODB if needed). |
| 611 |
/// |
| 612 |
/// If you provide a callback function, it will be invoked on each matching |
| 613 |
/// item in the index immediately before it is updated (either refreshed or |
| 614 |
/// removed depending on working directory state). Return 0 to proceed with |
| 615 |
/// updating the item, > 0 to skip the item, and < 0 to abort the scan. |
| 616 |
pub fn update_all<T, I>( |
| 617 |
&mut self, |
| 618 |
pathspecs: I, |
| 619 |
mut cb: Option<&mut IndexMatchedPath<'_>>, |
| 620 |
) -> Result<(), Error> |
| 621 |
where |
| 622 |
T: IntoCString, |
| 623 |
I: IntoIterator<Item = T>, |
| 624 |
{ |
| 625 |
let (_a, _b, raw_strarray) = crate::util::iter2cstrs_paths(pathspecs)?; |
| 626 |
let ptr = cb.as_mut(); |
| 627 |
let callback = ptr |
| 628 |
.as_ref() |
| 629 |
.map(|_| index_matched_path_cb as extern "C" fn(_, _, _) -> _); |
| 630 |
unsafe { |
| 631 |
try_call!(raw::git_index_update_all( |
| 632 |
self.raw, |
| 633 |
&raw_strarray, |
| 634 |
callback, |
| 635 |
ptr.map(|p| p as *mut _).unwrap_or(ptr::null_mut()) as *mut c_void |
| 636 |
)); |
| 637 |
} |
| 638 |
Ok(()) |
| 639 |
} |
| 640 |
|
| 641 |
/// Write an existing index object from memory back to disk using an atomic |
| 642 |
/// file lock. |
| 643 |
pub fn write(&mut self) -> Result<(), Error> { |
| 644 |
unsafe { |
| 645 |
try_call!(raw::git_index_write(self.raw)); |
| 646 |
} |
| 647 |
Ok(()) |
| 648 |
} |
| 649 |
|
| 650 |
/// Write the index as a tree. |
| 651 |
/// |
| 652 |
/// This method will scan the index and write a representation of its |
| 653 |
/// current state back to disk; it recursively creates tree objects for each |
| 654 |
/// of the subtrees stored in the index, but only returns the OID of the |
| 655 |
/// root tree. This is the OID that can be used e.g. to create a commit. |
| 656 |
/// |
| 657 |
/// The index instance cannot be bare, and needs to be associated to an |
| 658 |
/// existing repository. |
| 659 |
/// |
| 660 |
/// The index must not contain any file in conflict. |
| 661 |
pub fn write_tree(&mut self) -> Result<Oid, Error> { |
| 662 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 663 |
unsafe { |
| 664 |
try_call!(raw::git_index_write_tree(&mut raw, self.raw)); |
| 665 |
Ok(Binding::from_raw(&raw as *const _)) |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
/// Write the index as a tree to the given repository |
| 670 |
/// |
| 671 |
/// This is the same as `write_tree` except that the destination repository |
| 672 |
/// can be chosen. |
| 673 |
pub fn write_tree_to(&mut self, repo: &Repository) -> Result<Oid, Error> { |
| 674 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 675 |
unsafe { |
| 676 |
try_call!(raw::git_index_write_tree_to(&mut raw, self.raw, repo.raw())); |
| 677 |
Ok(Binding::from_raw(&raw as *const _)) |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
/// Find the first position of any entries matching a prefix. |
| 682 |
/// |
| 683 |
/// To find the first position of a path inside a given folder, suffix the prefix with a '/'. |
| 684 |
pub fn find_prefix<T: IntoCString>(&self, prefix: T) -> Result<usize, Error> { |
| 685 |
let mut at_pos: size_t = 0; |
| 686 |
let entry_path = prefix.into_c_string()?; |
| 687 |
unsafe { |
| 688 |
try_call!(raw::git_index_find_prefix( |
| 689 |
&mut at_pos, |
| 690 |
self.raw, |
| 691 |
entry_path |
| 692 |
)); |
| 693 |
Ok(at_pos) |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
impl IndexEntry { |
| 699 |
/// Create a raw index entry. |
| 700 |
/// |
| 701 |
/// The returned `raw::git_index_entry` contains a pointer to a `CString` path, which is also |
| 702 |
/// returned because it's lifetime must exceed the lifetime of the `raw::git_index_entry`. |
| 703 |
pub(crate) unsafe fn to_raw(&self) -> Result<(raw::git_index_entry, CString), Error> { |
| 704 |
let path = CString::new(&self.path[..])?; |
| 705 |
|
| 706 |
// libgit2 encodes the length of the path in the lower bits of the |
| 707 |
// `flags` entry, so mask those out and recalculate here to ensure we |
| 708 |
// don't corrupt anything. |
| 709 |
let mut flags = self.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK; |
| 710 |
|
| 711 |
if self.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize { |
| 712 |
flags |= self.path.len() as u16; |
| 713 |
} else { |
| 714 |
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK; |
| 715 |
} |
| 716 |
|
| 717 |
unsafe { |
| 718 |
let raw = raw::git_index_entry { |
| 719 |
dev: self.dev, |
| 720 |
ino: self.ino, |
| 721 |
mode: self.mode, |
| 722 |
uid: self.uid, |
| 723 |
gid: self.gid, |
| 724 |
file_size: self.file_size, |
| 725 |
id: *self.id.raw(), |
| 726 |
flags, |
| 727 |
flags_extended: self.flags_extended, |
| 728 |
path: path.as_ptr(), |
| 729 |
mtime: raw::git_index_time { |
| 730 |
seconds: self.mtime.seconds(), |
| 731 |
nanoseconds: self.mtime.nanoseconds(), |
| 732 |
}, |
| 733 |
ctime: raw::git_index_time { |
| 734 |
seconds: self.ctime.seconds(), |
| 735 |
nanoseconds: self.ctime.nanoseconds(), |
| 736 |
}, |
| 737 |
}; |
| 738 |
|
| 739 |
Ok((raw, path)) |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
impl Binding for Index { |
| 745 |
type Raw = *mut raw::git_index; |
| 746 |
unsafe fn from_raw(raw: *mut raw::git_index) -> Index { |
| 747 |
Index { raw } |
| 748 |
} |
| 749 |
fn raw(&self) -> *mut raw::git_index { |
| 750 |
self.raw |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
impl<'index> Binding for IndexConflicts<'index> { |
| 755 |
type Raw = *mut raw::git_index_conflict_iterator; |
| 756 |
|
| 757 |
unsafe fn from_raw(raw: *mut raw::git_index_conflict_iterator) -> IndexConflicts<'index> { |
| 758 |
IndexConflicts { |
| 759 |
conflict_iter: raw, |
| 760 |
_marker: marker::PhantomData, |
| 761 |
} |
| 762 |
} |
| 763 |
fn raw(&self) -> *mut raw::git_index_conflict_iterator { |
| 764 |
self.conflict_iter |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
extern "C" fn index_matched_path_cb( |
| 769 |
path: *const c_char, |
| 770 |
matched_pathspec: *const c_char, |
| 771 |
payload: *mut c_void, |
| 772 |
) -> c_int { |
| 773 |
unsafe { |
| 774 |
let path = CStr::from_ptr(path).to_bytes(); |
| 775 |
let matched_pathspec = CStr::from_ptr(matched_pathspec).to_bytes(); |
| 776 |
|
| 777 |
panic::wrap(|| { |
| 778 |
let payload = payload as *mut &mut IndexMatchedPath<'_>; |
| 779 |
(*payload)(util::bytes2path(path), matched_pathspec) as c_int |
| 780 |
}) |
| 781 |
.unwrap_or(-1) |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
impl Drop for Index { |
| 786 |
fn drop(&mut self) { |
| 787 |
unsafe { raw::git_index_free(self.raw) } |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
impl<'index> Drop for IndexConflicts<'index> { |
| 792 |
fn drop(&mut self) { |
| 793 |
unsafe { raw::git_index_conflict_iterator_free(self.conflict_iter) } |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
impl<'index> Iterator for IndexEntries<'index> { |
| 798 |
type Item = IndexEntry; |
| 799 |
fn next(&mut self) -> Option<IndexEntry> { |
| 800 |
self.range.next().map(|i| self.index.get(i).unwrap()) |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
impl<'index> Iterator for IndexConflicts<'index> { |
| 805 |
type Item = Result<IndexConflict, Error>; |
| 806 |
fn next(&mut self) -> Option<Result<IndexConflict, Error>> { |
| 807 |
let mut ancestor = ptr::null(); |
| 808 |
let mut our = ptr::null(); |
| 809 |
let mut their = ptr::null(); |
| 810 |
unsafe { |
| 811 |
try_call_iter!(raw::git_index_conflict_next( |
| 812 |
&mut ancestor, |
| 813 |
&mut our, |
| 814 |
&mut their, |
| 815 |
self.conflict_iter |
| 816 |
)); |
| 817 |
Some(Ok(IndexConflict { |
| 818 |
ancestor: match ancestor.is_null() { |
| 819 |
false => Some(IndexEntry::from_raw(*ancestor)), |
| 820 |
true => None, |
| 821 |
}, |
| 822 |
our: match our.is_null() { |
| 823 |
false => Some(IndexEntry::from_raw(*our)), |
| 824 |
true => None, |
| 825 |
}, |
| 826 |
their: match their.is_null() { |
| 827 |
false => Some(IndexEntry::from_raw(*their)), |
| 828 |
true => None, |
| 829 |
}, |
| 830 |
})) |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
impl Binding for IndexEntry { |
| 836 |
type Raw = raw::git_index_entry; |
| 837 |
|
| 838 |
unsafe fn from_raw(raw: raw::git_index_entry) -> IndexEntry { |
| 839 |
let raw::git_index_entry { |
| 840 |
ctime, |
| 841 |
mtime, |
| 842 |
dev, |
| 843 |
ino, |
| 844 |
mode, |
| 845 |
uid, |
| 846 |
gid, |
| 847 |
file_size, |
| 848 |
id, |
| 849 |
flags, |
| 850 |
flags_extended, |
| 851 |
path, |
| 852 |
} = raw; |
| 853 |
|
| 854 |
// libgit2 encodes the length of the path in the lower bits of `flags`, |
| 855 |
// but if the length exceeds the number of bits then the path is |
| 856 |
// nul-terminated. |
| 857 |
let mut pathlen = (flags & raw::GIT_INDEX_ENTRY_NAMEMASK) as usize; |
| 858 |
if pathlen == raw::GIT_INDEX_ENTRY_NAMEMASK as usize { |
| 859 |
pathlen = CStr::from_ptr(path).to_bytes().len(); |
| 860 |
} |
| 861 |
|
| 862 |
let path = slice::from_raw_parts(path as *const u8, pathlen); |
| 863 |
|
| 864 |
IndexEntry { |
| 865 |
dev, |
| 866 |
ino, |
| 867 |
mode, |
| 868 |
uid, |
| 869 |
gid, |
| 870 |
file_size, |
| 871 |
id: Binding::from_raw(&id as *const _), |
| 872 |
flags, |
| 873 |
flags_extended, |
| 874 |
path: path.to_vec(), |
| 875 |
mtime: Binding::from_raw(mtime), |
| 876 |
ctime: Binding::from_raw(ctime), |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
fn raw(&self) -> raw::git_index_entry { |
| 881 |
// not implemented, may require a CString in storage |
| 882 |
panic!() |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
#[cfg(test)] |
| 887 |
mod tests { |
| 888 |
use std::fs::{self, File}; |
| 889 |
use std::path::Path; |
| 890 |
use tempfile::TempDir; |
| 891 |
|
| 892 |
use crate::{ |
| 893 |
ErrorCode, Index, IndexEntry, IndexTime, ObjectFormat, Oid, Repository, ResetType, |
| 894 |
}; |
| 895 |
|
| 896 |
#[test] |
| 897 |
fn smoke() { |
| 898 |
let mut index = Index::new().unwrap(); |
| 899 |
assert!(index.add_path(&Path::new(".")).is_err()); |
| 900 |
index.clear().unwrap(); |
| 901 |
assert_eq!(index.len(), 0); |
| 902 |
assert!(index.get(0).is_none()); |
| 903 |
assert!(index.path().is_none()); |
| 904 |
assert!(index.read(true).is_err()); |
| 905 |
} |
| 906 |
|
| 907 |
#[test] |
| 908 |
fn smoke_from_repo() { |
| 909 |
let (_td, repo) = crate::test::repo_init(); |
| 910 |
let mut index = repo.index().unwrap(); |
| 911 |
assert_eq!( |
| 912 |
index.path().map(|s| s.to_path_buf()), |
| 913 |
Some(repo.path().join("index")) |
| 914 |
); |
| 915 |
Index::open(&repo.path().join("index")).unwrap(); |
| 916 |
|
| 917 |
index.clear().unwrap(); |
| 918 |
index.read(true).unwrap(); |
| 919 |
index.write().unwrap(); |
| 920 |
index.write_tree().unwrap(); |
| 921 |
index.write_tree_to(&repo).unwrap(); |
| 922 |
} |
| 923 |
|
| 924 |
#[test] |
| 925 |
fn add_all() { |
| 926 |
let (_td, repo) = crate::test::repo_init(); |
| 927 |
let mut index = repo.index().unwrap(); |
| 928 |
|
| 929 |
let root = repo.path().parent().unwrap(); |
| 930 |
fs::create_dir(&root.join("foo")).unwrap(); |
| 931 |
File::create(&root.join("foo/bar")).unwrap(); |
| 932 |
let mut called = false; |
| 933 |
index |
| 934 |
.add_all( |
| 935 |
["foo"].iter(), |
| 936 |
crate::IndexAddOption::DEFAULT, |
| 937 |
Some(&mut |a: &Path, b: &[u8]| { |
| 938 |
assert!(!called); |
| 939 |
called = true; |
| 940 |
assert_eq!(b, b"foo"); |
| 941 |
assert_eq!(a, Path::new("foo/bar")); |
| 942 |
0 |
| 943 |
}), |
| 944 |
) |
| 945 |
.unwrap(); |
| 946 |
assert!(called); |
| 947 |
|
| 948 |
called = false; |
| 949 |
index |
| 950 |
.remove_all( |
| 951 |
["."].iter(), |
| 952 |
Some(&mut |a: &Path, b: &[u8]| { |
| 953 |
assert!(!called); |
| 954 |
called = true; |
| 955 |
assert_eq!(b, b"."); |
| 956 |
assert_eq!(a, Path::new("foo/bar")); |
| 957 |
0 |
| 958 |
}), |
| 959 |
) |
| 960 |
.unwrap(); |
| 961 |
assert!(called); |
| 962 |
} |
| 963 |
|
| 964 |
#[test] |
| 965 |
fn smoke_add() { |
| 966 |
let (_td, repo) = crate::test::repo_init(); |
| 967 |
let mut index = repo.index().unwrap(); |
| 968 |
|
| 969 |
let root = repo.path().parent().unwrap(); |
| 970 |
fs::create_dir(&root.join("foo")).unwrap(); |
| 971 |
File::create(&root.join("foo/bar")).unwrap(); |
| 972 |
index.add_path(Path::new("foo/bar")).unwrap(); |
| 973 |
index.write().unwrap(); |
| 974 |
assert_eq!(index.iter().count(), 1); |
| 975 |
|
| 976 |
// Make sure we can use this repo somewhere else now. |
| 977 |
let id = index.write_tree().unwrap(); |
| 978 |
let tree = repo.find_tree(id).unwrap(); |
| 979 |
let sig = repo.signature().unwrap(); |
| 980 |
let id = repo.refname_to_id("HEAD").unwrap(); |
| 981 |
let parent = repo.find_commit(id).unwrap(); |
| 982 |
let commit = repo |
| 983 |
.commit(Some("HEAD"), &sig, &sig, "commit", &tree, &[&parent]) |
| 984 |
.unwrap(); |
| 985 |
let obj = repo.find_object(commit, None).unwrap(); |
| 986 |
repo.reset(&obj, ResetType::Hard, None).unwrap(); |
| 987 |
|
| 988 |
let td2 = TempDir::new().unwrap(); |
| 989 |
let url = crate::test::path2url(&root); |
| 990 |
let repo = Repository::clone(&url, td2.path()).unwrap(); |
| 991 |
let obj = repo.find_object(commit, None).unwrap(); |
| 992 |
repo.reset(&obj, ResetType::Hard, None).unwrap(); |
| 993 |
} |
| 994 |
|
| 995 |
#[test] |
| 996 |
fn add_then_read() { |
| 997 |
let mut index = Index::new_ext(ObjectFormat::Sha1).unwrap(); |
| 998 |
let mut e = entry(); |
| 999 |
e.path = b"foobar".to_vec(); |
| 1000 |
index.add(&e).unwrap(); |
| 1001 |
let e = index.get(0).unwrap(); |
| 1002 |
assert_eq!(e.path.len(), 6); |
| 1003 |
} |
| 1004 |
|
| 1005 |
#[test] |
| 1006 |
fn add_then_find() { |
| 1007 |
let mut index = Index::new_ext(ObjectFormat::Sha1).unwrap(); |
| 1008 |
let mut e = entry(); |
| 1009 |
e.path = b"foo/bar".to_vec(); |
| 1010 |
index.add(&e).unwrap(); |
| 1011 |
let mut e = entry(); |
| 1012 |
e.path = b"foo2/bar".to_vec(); |
| 1013 |
index.add(&e).unwrap(); |
| 1014 |
assert_eq!(index.get(0).unwrap().path, b"foo/bar"); |
| 1015 |
assert_eq!( |
| 1016 |
index.get_path(Path::new("foo/bar"), 0).unwrap().path, |
| 1017 |
b"foo/bar" |
| 1018 |
); |
| 1019 |
assert_eq!(index.find_prefix(Path::new("foo2/")), Ok(1)); |
| 1020 |
assert_eq!( |
| 1021 |
index.find_prefix(Path::new("empty/")).unwrap_err().code(), |
| 1022 |
ErrorCode::NotFound |
| 1023 |
); |
| 1024 |
} |
| 1025 |
|
| 1026 |
#[test] |
| 1027 |
fn add_frombuffer_then_read() { |
| 1028 |
let (_td, repo) = crate::test::repo_init(); |
| 1029 |
let mut index = repo.index().unwrap(); |
| 1030 |
|
| 1031 |
let mut e = entry(); |
| 1032 |
e.path = b"foobar".to_vec(); |
| 1033 |
let content = b"the contents"; |
| 1034 |
index.add_frombuffer(&e, content).unwrap(); |
| 1035 |
let e = index.get(0).unwrap(); |
| 1036 |
assert_eq!(e.path.len(), 6); |
| 1037 |
|
| 1038 |
let b = repo.find_blob(e.id).unwrap(); |
| 1039 |
assert_eq!(b.content(), content); |
| 1040 |
} |
| 1041 |
|
| 1042 |
fn entry() -> IndexEntry { |
| 1043 |
IndexEntry { |
| 1044 |
ctime: IndexTime::new(0, 0), |
| 1045 |
mtime: IndexTime::new(0, 0), |
| 1046 |
dev: 0, |
| 1047 |
ino: 0, |
| 1048 |
mode: 0o100644, |
| 1049 |
uid: 0, |
| 1050 |
gid: 0, |
| 1051 |
file_size: 0, |
| 1052 |
#[cfg(not(feature = "unstable-sha256"))] |
| 1053 |
id: Oid::ZERO_SHA1, |
| 1054 |
#[cfg(feature = "unstable-sha256")] |
| 1055 |
id: Oid::ZERO_SHA256, |
| 1056 |
flags: 0, |
| 1057 |
flags_extended: 0, |
| 1058 |
path: Vec::new(), |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
#[test] |
| 1063 |
#[cfg(feature = "unstable-sha256")] |
| 1064 |
fn index_sha256() { |
| 1065 |
let (_td, repo) = crate::test::repo_init_sha256(); |
| 1066 |
let mut index = repo.index().unwrap(); |
| 1067 |
|
| 1068 |
// Test opening with correct format |
| 1069 |
Index::open_ext(&repo.path().join("index"), ObjectFormat::Sha256).unwrap(); |
| 1070 |
|
| 1071 |
// Test basic operations with SHA256 |
| 1072 |
index.clear().unwrap(); |
| 1073 |
index.read(true).unwrap(); |
| 1074 |
index.write().unwrap(); |
| 1075 |
let tree_id = index.write_tree().unwrap(); |
| 1076 |
|
| 1077 |
// Verify OID is 32 bytes (SHA256) |
| 1078 |
assert_eq!(tree_id.as_bytes().len(), 32); |
| 1079 |
} |
| 1080 |
|
| 1081 |
#[test] |
| 1082 |
#[cfg(feature = "unstable-sha256")] |
| 1083 |
fn smoke_in_memory_index_sha256() { |
| 1084 |
let _index = Index::new_ext(ObjectFormat::Sha256).unwrap(); |
| 1085 |
} |
| 1086 |
} |