heddle-cli 0.2.0

An AI-native version control system
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// SPDX-License-Identifier: Apache-2.0
use std::{path::Path, process::Command};

use anyhow::{Result, anyhow};
use repo::{
    GitOverlayImportHint, GitRemoteTrackingStatus, OperationKind, OperationScope, Repository,
    RepositoryOperationStatus,
};
use serde::Serialize;

use super::{
    bisect::reset_bisect_state,
    rebase::{
        OperatorContinueStatus, cmd_rebase_silent, continue_rebase_for_operator,
        has_persisted_rebase_state,
    },
    resolve::abort_merge_state,
    snapshot::{SnapshotAgentOverrides, create_snapshot},
};
use crate::config::UserConfig;

#[derive(Debug, Clone, Serialize, Default)]
pub(crate) struct OperatorCommandOutput {
    pub status: String,
    pub action: String,
    pub message: String,
    /// Reasons the operation could not advance state. Only populated
    /// when `status == "blocked"` or `status == "failed"`. When the
    /// operation succeeded with caveats, use `warnings` instead.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub blockers: Vec<String>,
    /// Non-blocking nudges surfaced when the operation actually
    /// advanced state but the caller may still want a follow-up
    /// (e.g. a heavy-impact change worth reviewing for broader impact).
    /// Always omitted when empty.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_action: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recommended_action: Option<String>,
}

pub(crate) fn open_operator_repo_from_path(path: &Path) -> Result<Repository> {
    let cwd_repo = Repository::open(path)?;
    let target_path = cwd_repo.active_worktree_path()?;
    if target_path == *cwd_repo.root() {
        Ok(cwd_repo)
    } else {
        Ok(Repository::open(&target_path)?)
    }
}

pub(crate) fn continue_operator(repo: &Repository) -> Result<OperatorCommandOutput> {
    if repo.merge_state_manager().is_merge_in_progress() {
        let unresolved = repo.merge_state_manager().unresolved()?;
        if !unresolved.is_empty() {
            let recommended_action = format!("heddle resolve {}", unresolved[0]);
            return Ok(OperatorCommandOutput {
                status: "blocked".to_string(),
                action: "continue".to_string(),
                message: format!(
                    "Merge still has unresolved conflicts: {}. After removing conflict markers, mark each file resolved with `heddle resolve <path>`.",
                    unresolved.join(", ")
                ),
                blockers: unresolved,
                warnings: Vec::new(),
                next_action: Some("heddle resolve --list".to_string()),
                recommended_action: Some(recommended_action),
            });
        }

        create_snapshot(
            repo,
            &UserConfig::load_default().unwrap_or_default(),
            Some("Continue merge".to_string()),
            None,
            SnapshotAgentOverrides {
                provider: None,
                model: None,
                session: None,
                segment: None,
                policy: None,
                no_policy: false,
                no_agent: false,
            },
        )?;
        return Ok(OperatorCommandOutput {
            status: "continued".to_string(),
            action: "merge".to_string(),
            message: "Completed the in-progress Heddle merge".to_string(),
            blockers: Vec::new(),
            warnings: Vec::new(),
            next_action: None,
            recommended_action: None,
        });
    }

    if let Some(operation) = repo.operation_status()? {
        return continue_from_operation(repo, &operation);
    }

    Ok(OperatorCommandOutput {
        status: "noop".to_string(),
        action: "continue".to_string(),
        message: "No in-progress operation needs continuing".to_string(),
        blockers: Vec::new(),
        warnings: Vec::new(),
        next_action: None,
        recommended_action: None,
    })
}

pub(crate) fn abort_operator(repo: &Repository) -> Result<OperatorCommandOutput> {
    if repo.merge_state_manager().is_merge_in_progress() {
        abort_merge_state(repo, &repo.merge_state_manager())?;
        return Ok(OperatorCommandOutput {
            status: "aborted".to_string(),
            action: "merge".to_string(),
            message: "Aborted the in-progress Heddle merge".to_string(),
            blockers: Vec::new(),
            warnings: Vec::new(),
            next_action: None,
            recommended_action: None,
        });
    }

    if has_persisted_rebase_state(repo) {
        cmd_rebase_silent(repo, None, true, false)?;
        return Ok(OperatorCommandOutput {
            status: "aborted".to_string(),
            action: "rebase".to_string(),
            message: "Aborted the in-progress Heddle rebase".to_string(),
            blockers: Vec::new(),
            warnings: Vec::new(),
            next_action: None,
            recommended_action: None,
        });
    }

    if let Some(operation) = repo.operation_status()? {
        return abort_from_operation(repo, &operation);
    }

    Ok(OperatorCommandOutput {
        status: "noop".to_string(),
        action: "abort".to_string(),
        message: "No in-progress operation can be aborted".to_string(),
        blockers: Vec::new(),
        warnings: Vec::new(),
        next_action: None,
        recommended_action: None,
    })
}

fn continue_from_operation(
    repo: &Repository,
    operation: &RepositoryOperationStatus,
) -> Result<OperatorCommandOutput> {
    match (&operation.scope, &operation.kind) {
        (OperationScope::Heddle, OperationKind::Rebase) => {
            Ok(match continue_rebase_for_operator(repo)? {
                OperatorContinueStatus::Blocked => OperatorCommandOutput {
                    status: "blocked".to_string(),
                    action: "rebase".to_string(),
                    message:
                        "Rebase still needs a captured manual resolution before it can continue"
                            .to_string(),
                    blockers: Vec::new(),
                    warnings: Vec::new(),
                    next_action: Some("heddle capture -m \"Manual resolution\"".to_string()),
                    recommended_action: Some("heddle capture -m \"Manual resolution\"".to_string()),
                },
                OperatorContinueStatus::Continued => OperatorCommandOutput {
                    status: "continued".to_string(),
                    action: "rebase".to_string(),
                    message: "Continued the in-progress Heddle rebase".to_string(),
                    blockers: Vec::new(),
                    warnings: Vec::new(),
                    next_action: None,
                    recommended_action: None,
                },
                OperatorContinueStatus::Completed => OperatorCommandOutput {
                    status: "completed".to_string(),
                    action: "rebase".to_string(),
                    message: "Completed the in-progress Heddle rebase".to_string(),
                    blockers: Vec::new(),
                    warnings: Vec::new(),
                    next_action: None,
                    recommended_action: None,
                },
            })
        }
        (OperationScope::Heddle, OperationKind::Bisect) => Ok(OperatorCommandOutput {
            status: "blocked".to_string(),
            action: "bisect".to_string(),
            message: "Bisect needs a good/bad decision before it can continue".to_string(),
            blockers: Vec::new(),
            warnings: Vec::new(),
            next_action: Some(
                "heddle bisect good <state> or heddle bisect bad <state>".to_string(),
            ),
            recommended_action: Some(
                "heddle bisect good <state> or heddle bisect bad <state>".to_string(),
            ),
        }),
        (OperationScope::Git, OperationKind::Rebase) => {
            let unresolved = git_unmerged_paths(repo)?;
            if !unresolved.is_empty() {
                return Ok(git_conflict_blocked_action(
                    "rebase",
                    "Git rebase still has unresolved conflicts",
                    unresolved,
                ));
            }
            run_git_control(repo, &["rebase", "--continue"])?;
            Ok(simple_action(
                "continued",
                "rebase",
                "Continued the in-progress Git rebase",
            ))
        }
        (OperationScope::Git, OperationKind::Merge) => {
            let unresolved = git_unmerged_paths(repo)?;
            if !unresolved.is_empty() {
                return Ok(git_conflict_blocked_action(
                    "merge",
                    "Git merge still has unresolved conflicts",
                    unresolved,
                ));
            }
            run_git_control(repo, &["merge", "--continue"])?;
            Ok(simple_action(
                "continued",
                "merge",
                "Continued the in-progress Git merge",
            ))
        }
        (OperationScope::Git, OperationKind::CherryPick) => {
            continue_git_cherry_pick(repo)?;
            Ok(simple_action(
                "continued",
                "cherry-pick",
                "Continued the in-progress Git cherry-pick",
            ))
        }
        (OperationScope::Git, OperationKind::Revert) => {
            continue_git_revert(repo)?;
            Ok(simple_action(
                "continued",
                "revert",
                "Continued the in-progress Git revert",
            ))
        }
        (OperationScope::Git, OperationKind::Bisect) => Ok(OperatorCommandOutput {
            status: "blocked".to_string(),
            action: "bisect".to_string(),
            message: "Git bisect needs a good/bad decision before it can continue".to_string(),
            blockers: Vec::new(),
            warnings: Vec::new(),
            next_action: Some("git bisect good or git bisect bad".to_string()),
            recommended_action: Some("git bisect good or git bisect bad".to_string()),
        }),
        (OperationScope::Heddle, OperationKind::Merge) => unreachable!(),
        _ => Ok(OperatorCommandOutput {
            status: "noop".to_string(),
            action: "continue".to_string(),
            message: "No in-progress operation needs continuing".to_string(),
            blockers: Vec::new(),
            warnings: Vec::new(),
            next_action: None,
            recommended_action: None,
        }),
    }
}

fn abort_from_operation(
    repo: &Repository,
    operation: &RepositoryOperationStatus,
) -> Result<OperatorCommandOutput> {
    match (&operation.scope, &operation.kind) {
        (OperationScope::Heddle, OperationKind::Rebase) => {
            cmd_rebase_silent(repo, None, true, false)?;
        }
        (OperationScope::Heddle, OperationKind::Bisect) => {
            reset_bisect_state(repo)?;
        }
        (OperationScope::Git, OperationKind::Rebase) => {
            run_git_control(repo, &["rebase", "--abort"])?
        }
        (OperationScope::Git, OperationKind::Merge) => {
            run_git_control(repo, &["merge", "--abort"])?
        }
        (OperationScope::Git, OperationKind::CherryPick) => {
            run_git_control(repo, &["cherry-pick", "--abort"])?
        }
        (OperationScope::Git, OperationKind::Revert) => {
            run_git_control(repo, &["revert", "--abort"])?
        }
        (OperationScope::Git, OperationKind::Bisect) => {
            run_git_control(repo, &["bisect", "reset"])?
        }
        _ => {}
    }

    Ok(OperatorCommandOutput {
        status: "aborted".to_string(),
        action: operation.kind.to_string(),
        message: format!(
            "Aborted the in-progress {} {}",
            operation.scope, operation.kind
        ),
        blockers: Vec::new(),
        warnings: Vec::new(),
        next_action: None,
        recommended_action: None,
    })
}

fn simple_action(status: &str, action: &str, message: &str) -> OperatorCommandOutput {
    OperatorCommandOutput {
        status: status.to_string(),
        action: action.to_string(),
        message: message.to_string(),
        blockers: Vec::new(),
        warnings: Vec::new(),
        next_action: None,
        recommended_action: None,
    }
}

fn git_conflict_blocked_action(
    action: &str,
    message: &str,
    unresolved: Vec<String>,
) -> OperatorCommandOutput {
    OperatorCommandOutput {
        status: "blocked".to_string(),
        action: action.to_string(),
        message: format!("{message}: {}", unresolved.join(", ")),
        blockers: unresolved,
        warnings: Vec::new(),
        next_action: Some(
            "Resolve the files, stage them with `git add <files>`, then run `heddle continue`"
                .to_string(),
        ),
        recommended_action: Some("git add <files> && heddle continue".to_string()),
    }
}

pub(crate) fn recommend_next_action(
    operation: Option<&RepositoryOperationStatus>,
    remote_tracking: Option<&GitRemoteTrackingStatus>,
    import_hint: Option<&GitOverlayImportHint>,
    fallback: Option<&str>,
) -> String {
    if let Some(operation) = operation {
        return operation.next_action.clone();
    }
    if let Some(remote_tracking) = remote_tracking {
        if remote_tracking.behind > 0 {
            return "heddle sync".to_string();
        }
        return remote_tracking.next_action.clone();
    }
    let fallback = fallback.unwrap_or_default();
    if !fallback.is_empty() {
        return fallback.to_string();
    }
    if let Some(hint) = import_hint {
        return hint.recommended_command.clone();
    }
    String::new()
}

pub(crate) fn run_git_control(repo: &Repository, args: &[&str]) -> Result<()> {
    let (success, stderr) = run_git_control_attempt(repo, args)?;
    if success {
        return Ok(());
    }

    Err(anyhow!(
        "git {} failed at '{}': {}",
        args.join(" "),
        repo.root().display(),
        stderr
    ))
}

fn git_unmerged_paths(repo: &Repository) -> Result<Vec<String>> {
    let output = Command::new("git")
        .arg("-C")
        .arg(repo.root())
        .args(["diff", "--name-only", "--diff-filter=U"])
        .output()
        .map_err(|error| anyhow!("failed to inspect Git conflicts: {error}"))?;
    if !output.status.success() {
        return Ok(Vec::new());
    }
    Ok(String::from_utf8_lossy(&output.stdout)
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .map(ToString::to_string)
        .collect())
}

fn run_git_control_attempt(repo: &Repository, args: &[&str]) -> Result<(bool, String)> {
    let output = Command::new("git")
        .env("GIT_EDITOR", "true")
        .env("GIT_MERGE_AUTOEDIT", "no")
        .arg("-C")
        .arg(repo.root())
        .args(args)
        .output()
        .map_err(|error| anyhow!("failed to run git {}: {}", args.join(" "), error))?;

    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
    Ok((output.status.success(), stderr))
}

fn continue_git_cherry_pick(repo: &Repository) -> Result<()> {
    let (success, stderr) = run_git_control_attempt(repo, &["cherry-pick", "--continue"])?;
    if success {
        return Ok(());
    }
    if stderr.contains("previous cherry-pick is now empty") {
        return run_git_control(repo, &["cherry-pick", "--skip"]);
    }
    Err(anyhow!(
        "git cherry-pick --continue failed at '{}': {}",
        repo.root().display(),
        stderr
    ))
}

fn continue_git_revert(repo: &Repository) -> Result<()> {
    let (success, stderr) = run_git_control_attempt(repo, &["revert", "--continue"])?;
    if success {
        return Ok(());
    }
    if stderr.contains("previous cherry-pick is now empty")
        || stderr.contains("previous revert is now empty")
    {
        return run_git_control(repo, &["revert", "--skip"]);
    }
    Err(anyhow!(
        "git revert --continue failed at '{}': {}",
        repo.root().display(),
        stderr
    ))
}