| 570 | } |
| 571 | |
| 572 | fn create_intent_to_add_entries(repo_root: &Path, requested: &[PathBuf]) -> CliResult<()> { |
| 573 | use git2::{IndexEntry, IndexTime, ObjectType, Oid}; |
| 574 | |
| 575 | const INTENT_TO_ADD: u16 = 0x2000; |
| 576 | |
| 577 | let git_repo = git2::Repository::open(repo_root).map_err(|error| CliError::GitError { |
| 578 | message: format!("cannot open Git repository for intent-to-add: {error}"), |
| 579 | })?; |
| 580 | let mut index = git_repo.index().map_err(|error| CliError::GitError { |
| 581 | message: format!("cannot read Git index: {error}"), |
| 582 | })?; |
| 583 | |
| 584 | let already_indexed: std::collections::BTreeSet<Vec<u8>> = |
| 585 | index.iter().map(|entry| entry.path).collect(); |
| 586 | |
| 587 | // Empty blob OID for the repository's object format. libgit2 requires |
| 588 | // the object to exist before `index.add` accepts the entry (mirroring |
| 589 | // `git add -N`, which materializes the same well-known empty blob). |
| 590 | let empty_oid = { |
| 591 | let mut odb = git_repo.odb().map_err(|error| CliError::GitError { |
| 592 | message: format!("cannot open Git object database: {error}"), |
| 593 | })?; |
| 594 | odb.write(ObjectType::Blob, &[]) |
| 595 | .map_err(|error| CliError::GitError { |
| 596 | message: format!("cannot write the empty blob: {error}"), |
| 597 | })? |
| 598 | }; |
| 599 | |
| 600 | let mut created = Vec::new(); |
| 601 | for requested_path in requested { |
| 602 | let Ok(metadata) = std::fs::symlink_metadata(repo_root.join(requested_path)) else { |
| 603 | continue; |
| 604 | }; |
| 605 | if metadata.is_dir() { |
| 606 | // Directory adds expand into many files; only explicit files get |
| 607 | // intent-to-add entries here. `atomic stage` stages content. |
| 608 | continue; |
| 609 | } |
| 610 | let raw = requested_path |
| 611 | .to_string_lossy() |
| 612 | .replace('\\', "/") |
| 613 | .into_bytes(); |
| 614 | if already_indexed.contains(&raw) { |
| 615 | continue; |
| 616 | } |
| 617 | let mode = if metadata.file_type().is_symlink() { |
| 618 | 0o120000 |
| 619 | } else { |
| 620 | 0o100644 |
| 621 | }; |
| 622 | let entry = IndexEntry { |
| 623 | ctime: IndexTime::new(0, 0), |
| 624 | mtime: IndexTime::new(0, 0), |
| 625 | dev: 0, |
| 626 | ino: 0, |
| 627 | mode, |
| 628 | uid: 0, |
| 629 | gid: 0, |