Stage exactly the listed trace-relative paths and commit. Replaces the `add -A` sweep with an explicit declaration of what this commit contains. Caller is responsible for ensuring every path exists on disk.
(root: &Path, msg: &str, files: &[&str])
| 110 | /// `add -A` sweep with an explicit declaration of what this commit contains. |
| 111 | /// Caller is responsible for ensuring every path exists on disk. |
| 112 | pub fn commit_files(root: &Path, msg: &str, files: &[&str]) -> Result<()> { |
| 113 | let repo = open_or_init(root)?; |
| 114 | // Serialize cross-process writes to this repo. Held until function exits. |
| 115 | let _lock = CommitLock::acquire(&repo)?; |
| 116 | let mut index = repo.index().context("reading git index")?; |
| 117 | |
| 118 | let translate_one = |rel: &str| -> String { |
| 119 | match repo_subpath(&repo, root) { |
| 120 | Some(sub) => sub.join(rel).to_string_lossy().replace('\\', "/"), |
| 121 | None => rel.to_string(), |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | for f in files { |
| 126 | let translated = translate_one(f); |
| 127 | index |
| 128 | .add_path(Path::new(&translated)) |
| 129 | .with_context(|| format!("staging {}", translated))?; |
| 130 | } |
| 131 | index.write().context("writing git index")?; |
| 132 | |
| 133 | let head = repo.head().ok(); |
| 134 | let tree_oid = index.write_tree().context("writing tree from index")?; |
| 135 | |
| 136 | // No-op guard: tree unchanged vs HEAD. |
| 137 | if let Some(head_ref) = head.as_ref() { |
| 138 | if let Ok(parent_commit) = head_ref.peel_to_commit() { |
| 139 | if parent_commit.tree_id() == tree_oid { |
| 140 | return Ok(()); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | let tree = repo.find_tree(tree_oid)?; |
| 146 | let sig = signature()?; |
| 147 | let parents: Vec<git2::Commit> = match head.as_ref() { |
| 148 | Some(h) => vec![h.peel_to_commit()?], |
| 149 | None => vec![], |
| 150 | }; |
| 151 | let parent_refs: Vec<&git2::Commit> = parents.iter().collect(); |
| 152 | repo.commit(Some("HEAD"), &sig, &sig, msg, &tree, &parent_refs) |
| 153 | .context("git commit")?; |
| 154 | Ok(()) |
| 155 | } |
| 156 | |
| 157 | pub async fn commit_files_async(root: PathBuf, msg: String, files: Vec<String>) -> Result<()> { |
| 158 | tokio::task::spawn_blocking(move || { |
searching dependent graphs…