git-loom 0.25.0

A Git CLI tool that weaves together multiple feature branches into integration branches
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use anyhow::{Result, bail};
use git2::{Oid, Repository};

use crate::core::hunk_select::{self, HunkArgs, Picker};
use crate::core::repo::{self, Target, TargetKind};
use crate::core::weave;
use crate::core::{agent_mode, changeid, diff, graph, msg, staging};
use crate::git;
use crate::tui::hunk_selector::FileEntry;

const COMMAND: &str = "split";

/// The invocation to repeat, carrying every argument that shapes the split, so
/// an agent can replay it with only the missing piece filled in (spec 019).
/// Dropping the `<files>` filter would list a different set of hunks and fail
/// the fingerprint check instead of splitting.
fn invocation(
    target: &str,
    message: Option<&str>,
    patch: bool,
    hunks: &HunkArgs,
    files: &[String],
) -> String {
    // The placeholder belongs only to the prompt asking for the message; every
    // other hint repeats the one the caller already gave, or replaying it
    // commits a subject of `<message>`.
    let message = message.map_or_else(|| "<message>".to_string(), hunk_select::quoted);
    let mut out = format!("loom split {} -m {message}", hunk_select::quoted(target));
    if patch {
        out.push_str(" -p");
    }
    for file in files {
        out.push(' ');
        out.push_str(&hunk_select::quoted(file));
    }
    if let Some(from) = hunks.from.as_deref().filter(|_| !hunks.is_empty()) {
        for id in &hunks.ids {
            out.push_str(&format!(" --hunks {}", hunk_select::quoted(id)));
        }
        out.push_str(&format!(" --hunks-from {}", hunk_select::quoted(from)));
    }
    out
}

/// Commit with `-m` message or open the editor; either way the new commit
/// gets a Change-Id (Spec 002).
fn commit_or_editor(
    repo: &Repository,
    workdir: &std::path::Path,
    message: Option<&str>,
) -> Result<()> {
    match message {
        Some(m) => git::commit(workdir, m),
        None => {
            git::commit_with_editor(workdir)?;
            changeid::ensure_on_head_or_warn(repo, workdir, None);
            Ok(())
        }
    }
}

/// Split a commit into two sequential commits, selecting files (or hunks with
/// `-p`) for the first one.
pub fn run(
    target: String,
    message: Option<String>,
    patch: bool,
    hunks: HunkArgs,
    files: Vec<String>,
    theme: &graph::Theme,
) -> Result<()> {
    let prompt_hint = invocation(&target, message.as_deref(), patch, &hunks, &files);

    // Without -m the first commit would open $GIT_EDITOR, which hangs a headless agent.
    if agent_mode::enabled() && message.is_none() {
        return Err(agent_mode::respond_needs_input(
            agent_mode::InputKind::Text,
            "Message for the first commit",
            vec![],
            false,
            &format!("re-run with: {prompt_hint}"),
        ));
    }

    let repo = repo::open_repo()?;

    let resolved = repo::resolve_arg(&repo, &target, &[TargetKind::Commit])?;

    let picker = patch.then_some(Picker {
        // Built without the selection flags: `respond` appends its own.
        command: invocation(
            &target,
            message.as_deref(),
            patch,
            &HunkArgs::default(),
            &files,
        ),
        hunks,
        // Both commits it writes are the one it lists.
        target_hash: None,
        git_args: String::new(),
    });

    match resolved {
        Target::Commit(hash) => split_commit(&repo, &hash, message, picker, files, theme),
        _ => unreachable!(),
    }
}

/// Split a commit, using provided files or an interactive picker if none are given.
/// `picker` is `Some` exactly when `-p` was given: hunk-level split.
fn split_commit(
    repo: &Repository,
    commit_hash: &str,
    message: Option<String>,
    picker: Option<Picker>,
    files: Vec<String>,
    theme: &graph::Theme,
) -> Result<()> {
    let workdir = repo::require_workdir(repo, COMMAND)?;
    let commit = repo.revparse_single(commit_hash)?.peel_to_commit()?;
    let commit_oid = commit.id();

    if commit.parent_count() > 1 {
        bail!("Cannot split a merge commit");
    }

    let original_msg = commit.message().unwrap_or("").trim().to_string();

    // The commit's file list is repo-relative, so the given paths must be too.
    let files: Vec<String> = files
        .iter()
        .map(|f| repo::to_repo_path(repo, f))
        .collect::<Result<_>>()?;

    if let Some(picker) = picker {
        let oid_str = commit_oid.to_string();
        let selections =
            staging::run_commit_hunk_picker(workdir, &oid_str, &files, &picker, theme)?
                .ok_or_else(msg::cancelled)?;

        let has_selected = selections
            .iter()
            .any(|f| f.hunks.iter().any(|h| h.selected));
        let has_unselected = selections
            .iter()
            .any(|f| f.hunks.iter().any(|h| !h.selected));
        if !has_selected {
            bail!("Must select at least one hunk for the first commit");
        }
        if !has_unselected {
            bail!("Must leave at least one hunk for the second commit");
        }

        return perform_split_by_hunks(
            repo,
            workdir,
            commit_oid,
            &selections,
            message.as_deref(),
            &original_msg,
        );
    }

    let all_files = repo::commit_file_paths(repo, commit_oid)?;
    if all_files.len() < 2 {
        bail!("Cannot split a commit with only one file");
    }

    let selected = if files.is_empty() {
        pick_files(&all_files)?
    } else {
        let remaining_count = all_files.iter().filter(|f| !files.contains(f)).count();
        if remaining_count == 0 {
            bail!("Must leave at least one file for the second commit");
        }
        files
    };

    let remaining: Vec<String> = all_files
        .into_iter()
        .filter(|f| !selected.contains(f))
        .collect();

    perform_split(
        repo,
        workdir,
        commit_oid,
        &selected,
        &remaining,
        message.as_deref(),
        &original_msg,
    )
}

/// Split a commit with pre-selected files, bypassing the picker (for tests).
#[cfg(test)]
pub fn split_commit_with_selection(
    repo: &Repository,
    commit_hash: &str,
    selected: Vec<String>,
    message: String,
) -> Result<()> {
    let theme = graph::Theme::dark();
    split_commit(repo, commit_hash, Some(message), None, selected, &theme)
}

/// Show an interactive file picker for splitting.
fn pick_files(files: &[String]) -> Result<Vec<String>> {
    let selected = msg::multi_select(
        "Select files for the first commit:",
        files.to_vec(),
        "re-run with: loom split <target> -m <message> <files...>",
    )?;

    if selected.len() == files.len() {
        bail!("Must leave at least one file for the second commit");
    }

    Ok(selected)
}

/// Shared split dispatcher: save staged, run split logic, restore staged, print success.
///
/// `do_split` receives `is_head: bool` and returns `(hash1, hash2)`.
fn run_split(
    repo: &Repository,
    workdir: &std::path::Path,
    commit_oid: Oid,
    do_split: impl FnOnce(bool) -> Result<(String, String)>,
) -> Result<()> {
    let is_head = repo::head_oid(repo)? == commit_oid;
    let oid_str = commit_oid.to_string();
    let short_hash = git::short_hash(&oid_str);
    // Save pre-existing staged changes so `reset --mixed` does not discard them.
    let saved_staged = staging::save_and_unstage_staged(repo, workdir)?;
    let split_result = do_split(is_head);
    // Restore pre-existing staged changes regardless of outcome.
    saved_staged.restore();
    let (h1, h2) = split_result?;
    msg::success(&format!(
        "Split `{}` into {} and {}",
        short_hash,
        repo::describe_commit(workdir, &h1),
        repo::describe_commit(workdir, &h2)
    ));
    Ok(())
}

/// Wrap a head-split closure in an edit-and-continue rebase for non-HEAD commits.
///
/// Aborts the rebase automatically on error — split does not save LoomState.
fn perform_non_head_with(
    repo: &Repository,
    workdir: &std::path::Path,
    commit_oid: Oid,
    do_head_split: impl FnOnce() -> Result<(String, String)>,
) -> Result<(String, String)> {
    weave::start_edit_rebase(repo, workdir, commit_oid)?;
    let result = match do_head_split() {
        Ok(hashes) => hashes,
        Err(e) => return Err(git::rebase_abort_then_cleanup(workdir, e, || {})),
    };
    // Continue the rebase: later commits replay on top of the split commits, so
    // hash1/hash2 stay valid. Aborts on conflict — split saves no LoomState.
    git::continue_rebase_expecting_edit(workdir, git::AfterStop::nothing())?;
    Ok(result)
}

/// Perform the file-based split operation.
fn perform_split(
    repo: &Repository,
    workdir: &std::path::Path,
    commit_oid: Oid,
    selected: &[String],
    remaining: &[String],
    msg1: Option<&str>,
    msg2: &str,
) -> Result<()> {
    let (msg1, msg2) = stamped_messages(repo, workdir, msg1, msg2)?;
    let (msg1, msg2) = (msg1.as_deref(), msg2.as_str());
    run_split(repo, workdir, commit_oid, |is_head| {
        if is_head {
            perform_head_split(repo, workdir, selected, remaining, msg1, msg2)
        } else {
            perform_non_head_with(repo, workdir, commit_oid, || {
                perform_head_split(repo, workdir, selected, remaining, msg1, msg2)
            })
        }
    })
}

/// Both messages with their Change-Id (Spec 013), stamped before the split
/// dismantles the commit so no generation failure can strike in between. The
/// original message keeps its id; one without gets a fresh one.
fn stamped_messages(
    repo: &Repository,
    workdir: &std::path::Path,
    msg1: Option<&str>,
    msg2: &str,
) -> Result<(Option<String>, String)> {
    let stamped1 = match msg1 {
        Some(m) => Some(changeid::for_message(repo, workdir, m, None)?),
        None => None,
    };
    let mut stamped2 = changeid::for_message(repo, workdir, msg2, None)?;
    // Same ident, HEAD, and text give the same fresh id twice; the first
    // half's id then salts the second's.
    let id1 = stamped1.as_deref().and_then(changeid::from_message);
    if let Some(id1) = &id1
        && changeid::from_message(&stamped2).as_deref() == Some(id1)
    {
        let id2 = changeid::generate_unlike(workdir, msg2, id1)?;
        stamped2 = changeid::for_message(repo, workdir, msg2, Some(&id2))?;
    }
    Ok((stamped1, stamped2))
}

/// Split HEAD commit (no rebase needed).
///
/// ```text
/// reset_mixed(HEAD~1) → stage selected → commit(msg1) → stage remaining → commit(msg2)
/// ```
///
/// Both halves are staged from the commit, never the working tree (Spec 013).
///
/// Returns `(hash1, hash2)` — the two new commit hashes.
fn perform_head_split(
    repo: &Repository,
    workdir: &std::path::Path,
    selected: &[String],
    remaining: &[String],
    msg1: Option<&str>,
    msg2: &str,
) -> Result<(String, String)> {
    let original = git::rev_parse(workdir, "HEAD")?;
    git::reset_mixed(workdir, "HEAD~1")?;

    let selected_refs: Vec<&str> = selected.iter().map(|s| s.as_str()).collect();
    git::stage_from(workdir, &original, &selected_refs)?;
    commit_or_editor(repo, workdir, msg1)?;

    let remaining_refs: Vec<&str> = remaining.iter().map(|s| s.as_str()).collect();
    git::stage_from(workdir, &original, &remaining_refs)?;
    git::commit(workdir, msg2)?;

    let hash2 = git::rev_parse(workdir, "HEAD")?;
    let hash1 = git::rev_parse(workdir, "HEAD~1")?;

    Ok((hash1, hash2))
}

/// Perform the hunk-based split operation.
fn perform_split_by_hunks(
    repo: &Repository,
    workdir: &std::path::Path,
    commit_oid: Oid,
    selections: &[FileEntry],
    msg1: Option<&str>,
    msg2: &str,
) -> Result<()> {
    let (msg1, msg2) = stamped_messages(repo, workdir, msg1, msg2)?;
    let (msg1, msg2) = (msg1.as_deref(), msg2.as_str());
    run_split(repo, workdir, commit_oid, |is_head| {
        if is_head {
            perform_head_split_by_hunks(repo, workdir, selections, msg1, msg2)
        } else {
            perform_non_head_with(repo, workdir, commit_oid, || {
                perform_head_split_by_hunks(repo, workdir, selections, msg1, msg2)
            })
        }
    })
}

/// HEAD hunk-based split.
///
/// ```text
/// reset_mixed(HEAD~1) → apply selected hunks → commit(msg1) → stage remaining → commit(msg2)
/// ```
///
/// Whole files are staged from the commit, as in [`perform_head_split`].
fn perform_head_split_by_hunks(
    repo: &Repository,
    workdir: &std::path::Path,
    selections: &[FileEntry],
    msg1: Option<&str>,
    msg2: &str,
) -> Result<(String, String)> {
    let original = git::rev_parse(workdir, "HEAD")?;

    git::reset_mixed(workdir, "HEAD~1")?;

    let mut selected_patch = String::new();
    for file in selections {
        let selected: Vec<_> = file
            .hunks
            .iter()
            .filter(|h| h.selected)
            .map(|h| &h.hunk)
            .collect();
        if selected.is_empty() {
            continue;
        }
        // A submodule is listed as binary, so it too comes whole from the commit.
        if file.binary || file.index_status == 'D' {
            git::stage_from(workdir, &original, &[&file.path])?;
        } else {
            selected_patch.push_str(&diff::build_hunk_patch(&file.path, &selected));
        }
    }
    if !selected_patch.is_empty() {
        git::apply_cached_patch(workdir, &selected_patch)?;
    }

    commit_or_editor(repo, workdir, msg1)?;

    for file in selections {
        if file.hunks.iter().any(|h| !h.selected) {
            git::stage_from(workdir, &original, &[&file.path])?;
        }
    }
    git::commit(workdir, msg2)?;

    let hash2 = git::rev_parse(workdir, "HEAD")?;
    let hash1 = git::rev_parse(workdir, "HEAD~1")?;
    Ok((hash1, hash2))
}

#[cfg(test)]
#[path = "split_test.rs"]
mod tests;