(&self)
| 161 | |
| 162 | impl Command for Stage { |
| 163 | fn run(&self) -> CliResult<()> { |
| 164 | if self.paths.is_empty() { |
| 165 | return Err(CliError::InvalidArgument { |
| 166 | message: "stage requires at least one path".to_string(), |
| 167 | }); |
| 168 | } |
| 169 | let repo_root = find_repository_root()?; |
| 170 | let mut repo = Repository::open_for_workspace_transaction(&repo_root).map_err(|e| { |
| 171 | CliError::InvalidRepository { |
| 172 | reason: e.to_string(), |
| 173 | } |
| 174 | })?; |
| 175 | let workspace = enter_workspace(&mut repo, WorkspaceTxnMode::Reconcile)?; |
| 176 | let working_copy = workspace.working_copy(); |
| 177 | |
| 178 | let git = colocated_git_repository(&repo_root)?; |
| 179 | let candidates = resolve_candidates(&repo, &self.paths)?; |
| 180 | |
| 181 | // Tracking gate: stage stages content, it does not decide tracking. |
| 182 | // Durable tracking (`TREE`) is read for the gate and never mutated. |
| 183 | let tracked_files = repo |
| 184 | .list_tracked_files() |
| 185 | .map_err(|e| CliError::Internal(e.into()))?; |
| 186 | let durably_tracked: std::collections::BTreeSet<Vec<u8>> = tracked_files |
| 187 | .iter() |
| 188 | .filter(|file| !file.is_directory) |
| 189 | .map(|file| file.path.display().to_string().into_bytes()) |
| 190 | .collect(); |
| 191 | |
| 192 | let mut index = git.index().map_err(|error| CliError::GitError { |
| 193 | message: format!("cannot read Git index: {error}"), |
| 194 | })?; |
| 195 | |
| 196 | let mut staged = Vec::new(); |
| 197 | let mut refused = Vec::new(); |
| 198 | for candidate in candidates { |
| 199 | let (intent_to_add, unmerged) = index_entry_flags(&candidate.raw, &index); |
| 200 | if unmerged { |
| 201 | return Err(CliError::InvalidArgument { |
| 202 | message: format!( |
| 203 | "path '{}' is unmerged in the Git index; finish or abort the Git operation in Git first", |
| 204 | String::from_utf8_lossy(&candidate.raw) |
| 205 | ), |
| 206 | }); |
| 207 | } |
| 208 | let tracked = durably_tracked.contains(&candidate.raw) || intent_to_add; |
| 209 | if !tracked { |
| 210 | refused.push(String::from_utf8_lossy(&candidate.raw).into_owned()); |
| 211 | continue; |
| 212 | } |
| 213 | if self.dry_run { |
| 214 | staged.push(String::from_utf8_lossy(&candidate.raw).into_owned()); |
| 215 | continue; |
| 216 | } |
| 217 | let relative = path_relative_to_root(&candidate.native, repo.root())?; |
| 218 | index |
| 219 | .add_path(&relative) |
| 220 | .map_err(|error| CliError::GitError { |
nothing calls this directly
no test coverage detected