heddle-core 0.8.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
// SPDX-License-Identifier: Apache-2.0
//! Shared status and command next-action selection.

use repo::{
    GitOverlayImportHint, GitRemoteTrackingStatus, Repository, RepositoryOperationStatus,
    shell_quote,
};

use crate::RepositoryVerificationState;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NextActionScope {
    Default,
    CurrentThread,
    Ready,
}

pub struct NextActionInput<'a> {
    pub operation: Option<&'a RepositoryOperationStatus>,
    pub remote_tracking: Option<&'a GitRemoteTrackingStatus>,
    pub import_hint: Option<&'a GitOverlayImportHint>,
    pub fallback: Option<&'a str>,
    pub thread_health: Option<&'a str>,
    pub trust: Option<&'a RepositoryVerificationState>,
    pub scope: NextActionScope,
}

impl<'a> NextActionInput<'a> {
    pub fn default(
        operation: Option<&'a RepositoryOperationStatus>,
        remote_tracking: Option<&'a GitRemoteTrackingStatus>,
        import_hint: Option<&'a GitOverlayImportHint>,
        fallback: Option<&'a str>,
    ) -> Self {
        Self {
            operation,
            remote_tracking,
            import_hint,
            fallback,
            thread_health: None,
            trust: None,
            scope: NextActionScope::Default,
        }
    }

    pub fn with_verification(mut self, trust: &'a RepositoryVerificationState) -> Self {
        self.trust = Some(trust);
        self
    }

    pub fn current_thread(mut self, thread_health: Option<&'a str>) -> Self {
        self.thread_health = thread_health;
        self.scope = NextActionScope::CurrentThread;
        self
    }

    pub fn ready(mut self) -> Self {
        self.scope = NextActionScope::Ready;
        self
    }
}

pub fn effective_next_action(input: NextActionInput<'_>) -> String {
    if let Some(trust) = input.trust
        && !trust.verified
    {
        return trust.recommended_action.clone();
    }

    match input.scope {
        NextActionScope::Ready => ready_next_action(input),
        NextActionScope::CurrentThread => current_thread_next_action(input),
        NextActionScope::Default => default_next_action(input),
    }
}

fn ready_next_action(input: NextActionInput<'_>) -> String {
    if let Some(operation) = input.operation {
        return operation.next_action.clone();
    }
    if let Some(action) = non_empty_action(input.fallback) {
        return action.to_string();
    }
    default_next_action(NextActionInput {
        operation: None,
        remote_tracking: input.remote_tracking,
        import_hint: input.import_hint,
        fallback: None,
        thread_health: None,
        trust: None,
        scope: NextActionScope::Default,
    })
}

fn current_thread_next_action(input: NextActionInput<'_>) -> String {
    let thread_action = non_empty_action(input.fallback);
    if input.operation.is_none()
        && thread_recovery_precedes_publish(input.remote_tracking, input.thread_health, thread_action)
    {
        return thread_action.unwrap_or_default().to_string();
    }
    default_next_action(NextActionInput {
        operation: input.operation,
        remote_tracking: input.remote_tracking,
        import_hint: input.import_hint,
        fallback: thread_action,
        thread_health: None,
        trust: None,
        scope: NextActionScope::Default,
    })
}

fn default_next_action(input: NextActionInput<'_>) -> String {
    if let Some(operation) = input.operation {
        return operation.next_action.clone();
    }
    if let Some(remote_tracking) = input.remote_tracking
        && let Some(action) = remote_tracking_next_action(remote_tracking)
    {
        return action;
    }
    if let Some(action) = non_empty_action(input.fallback) {
        return action.to_string();
    }
    if let Some(hint) = input.import_hint
        && import_hint_includes_active_branch(hint)
    {
        return hint.recommended_command.clone();
    }
    String::new()
}

pub fn remote_tracking_status(remote: &GitRemoteTrackingStatus) -> &'static str {
    if remote.upstream.is_empty() {
        return "remote_untracked";
    }
    if remote.upstream_is_undone_checkpoint && remote.ahead == 0 && remote.behind > 0 {
        return "remote_contains_undone_checkpoint";
    }
    match (remote.ahead, remote.behind) {
        (0, 0) => "clean",
        (0, _) => "remote_behind",
        (_, 0) => "remote_ahead",
        _ => "remote_diverged",
    }
}

pub fn remote_tracking_next_action(remote: &GitRemoteTrackingStatus) -> Option<String> {
    match remote_tracking_status(remote) {
        "clean" => None,
        "remote_untracked" => Some(remote_untracked_action(remote)),
        "remote_contains_undone_checkpoint" => Some(heddle_action(["push", "--force"])),
        "remote_behind" => Some("heddle pull".to_string()),
        "remote_ahead" => Some("heddle push".to_string()),
        "remote_diverged" => {
            let upstream = remote.upstream.trim();
            if upstream.is_empty() {
                Some("heddle fetch".to_string())
            } else {
                Some(canonical_bridge_import_ref_command(upstream))
            }
        }
        _ => None,
    }
}

pub fn remote_untracked_action(remote: &GitRemoteTrackingStatus) -> String {
    if remote.next_action.trim().is_empty() {
        "heddle push".to_string()
    } else {
        remote.next_action.clone()
    }
}

pub fn canonical_adopt_ref_command(ref_name: &str) -> String {
    heddle_action(["adopt", "--ref", ref_name])
}

pub fn canonical_bridge_import_ref_command(ref_name: &str) -> String {
    heddle_action(["bridge", "git", "import", "--ref", ref_name])
}

pub fn canonical_bridge_reconcile_ref_preview_command(
    prefer: Option<&str>,
    ref_name: &str,
) -> String {
    match prefer {
        Some(prefer) => heddle_action([
            "bridge",
            "git",
            "reconcile",
            "--prefer",
            prefer,
            "--ref",
            ref_name,
            "--preview",
        ]),
        None => heddle_action(["bridge", "git", "reconcile", "--ref", ref_name, "--preview"]),
    }
}

pub fn canonical_bridge_reconcile_ref_command(prefer: &str, ref_name: &str) -> String {
    heddle_action([
        "bridge",
        "git",
        "reconcile",
        "--prefer",
        prefer,
        "--ref",
        ref_name,
    ])
}

pub fn heddle_action<I, S>(args: I) -> String
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    std::iter::once("heddle".to_string())
        .chain(args.into_iter().map(|arg| shell_quote(arg.as_ref())))
        .collect::<Vec<_>>()
        .join(" ")
}

pub fn thread_flag_args(thread_id: &str) -> Vec<String> {
    if thread_id.starts_with('-') {
        vec![format!("--thread={thread_id}")]
    } else {
        vec!["--thread".to_string(), thread_id.to_string()]
    }
}

pub fn merge_preview_command(thread_id: &str) -> String {
    if thread_id.starts_with('-') {
        heddle_action(vec![
            "merge".to_string(),
            "--preview".to_string(),
            "--".to_string(),
            thread_id.to_string(),
        ])
    } else {
        heddle_action(["merge", thread_id, "--preview"])
    }
}

pub fn land_local_command(thread_id: &str) -> String {
    let mut argv = vec!["land".to_string()];
    argv.extend(thread_flag_args(thread_id));
    argv.push("--no-push".to_string());
    heddle_action(argv)
}

pub fn land_push_command(thread_id: &str) -> String {
    let mut argv = vec!["land".to_string()];
    argv.extend(thread_flag_args(thread_id));
    argv.push("--push".to_string());
    heddle_action(argv)
}

pub fn contextual_thread_action(
    repo: &Repository,
    thread_id: &str,
    target_thread: Option<&str>,
    action: &str,
) -> String {
    let Some(main_root) = repo.heddle_dir().parent() else {
        return action.to_string();
    };
    if main_root == repo.root() || target_thread.is_none() {
        return action.to_string();
    }
    if action == merge_preview_command(thread_id) {
        let mut argv = vec!["--repo".to_string(), main_root.display().to_string()];
        if thread_id.starts_with('-') {
            argv.extend([
                "merge".to_string(),
                "--preview".to_string(),
                "--".to_string(),
                thread_id.to_string(),
            ]);
        } else {
            argv.extend([
                "merge".to_string(),
                thread_id.to_string(),
                "--preview".to_string(),
            ]);
        }
        return heddle_action(argv);
    }
    if action == land_local_command(thread_id) {
        let mut argv = vec![
            "--repo".to_string(),
            main_root.display().to_string(),
            "land".to_string(),
        ];
        argv.extend(thread_flag_args(thread_id));
        argv.push("--no-push".to_string());
        return heddle_action(argv);
    }
    if action == land_push_command(thread_id) {
        let mut argv = vec![
            "--repo".to_string(),
            main_root.display().to_string(),
            "land".to_string(),
        ];
        argv.extend(thread_flag_args(thread_id));
        argv.push("--push".to_string());
        return heddle_action(argv);
    }
    action.to_string()
}

pub fn import_hint_includes_active_branch(hint: &GitOverlayImportHint) -> bool {
    hint.missing_branches
        .iter()
        .any(|branch| branch == &hint.current_branch)
}

pub fn thread_recovery_precedes_publish(
    remote_tracking: Option<&GitRemoteTrackingStatus>,
    thread_health: Option<&str>,
    thread_action: Option<&str>,
) -> bool {
    let Some(remote_tracking) = remote_tracking else {
        return false;
    };
    if remote_tracking.ahead == 0 || remote_tracking.behind > 0 {
        return false;
    }
    let Some(thread_action) = thread_action else {
        return false;
    };
    thread_recovery_action_is_primary(thread_health, thread_action)
}

pub fn thread_recovery_action_is_primary(thread_health: Option<&str>, thread_action: &str) -> bool {
    matches!(
        thread_health.unwrap_or_default(),
        "blocked" | "dirty_worktree" | "uncaptured"
    ) || thread_action == "heddle commit"
        || thread_action.starts_with("heddle commit ")
        || thread_action.starts_with("heddle sync ")
        || thread_action.starts_with("heddle resolve ")
        || thread_action.starts_with("heddle thread promote ")
}

pub fn non_empty_action(action: Option<&str>) -> Option<&str> {
    action.filter(|action| !action.trim().is_empty())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn remote(
        branch: &str,
        upstream: &str,
        ahead: usize,
        behind: usize,
        next_action: &str,
    ) -> GitRemoteTrackingStatus {
        GitRemoteTrackingStatus {
            branch: branch.to_string(),
            upstream: upstream.to_string(),
            ahead,
            behind,
            local_oid: Some("head".to_string()),
            upstream_oid: Some("upstream".to_string()),
            upstream_is_undone_checkpoint: false,
            message: String::new(),
            next_action: next_action.to_string(),
        }
    }

    #[test]
    fn remote_tracking_next_action_covers_basic_git_states_without_repo_context() {
        assert_eq!(
            remote_tracking_next_action(&remote("main", "origin/main", 0, 1, "heddle pull"))
                .as_deref(),
            Some("heddle pull")
        );
        assert_eq!(
            remote_tracking_next_action(&remote("main", "origin/main", 1, 0, "heddle push"))
                .as_deref(),
            Some("heddle push")
        );
        assert_eq!(
            remote_tracking_next_action(&remote("main", "origin/main", 1, 1, "heddle fetch"))
                .as_deref(),
            Some("heddle bridge git import --ref origin/main")
        );
        assert_eq!(
            remote_tracking_next_action(&remote("main", "", 1, 0, "heddle push")).as_deref(),
            Some("heddle push")
        );
    }

    #[test]
    fn current_thread_recovery_precedes_publish_when_thread_action_is_primary() {
        let remote = remote("feature", "origin/feature", 1, 0, "heddle push");
        let action = effective_next_action(
            NextActionInput::default(
                None,
                Some(&remote),
                None,
                Some("heddle commit -m \"...\""),
            )
            .current_thread(Some("dirty_worktree")),
        );
        assert_eq!(action, "heddle commit -m \"...\"");
    }

    #[test]
    fn ready_scope_prefers_thread_action_before_publish() {
        let remote = remote("feature", "origin/feature", 1, 0, "heddle push");
        let action = effective_next_action(
            NextActionInput::default(None, Some(&remote), None, Some("heddle land --thread f"))
                .ready(),
        );
        assert_eq!(action, "heddle land --thread f");
    }
}