gitpoulpi-core 1.2.0

GitPoulpi domain model, GitBackend and trait. No I/O-framework dependencies.
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! "Pull all branches": bring every local branch that tracks an upstream up
//! to date in one run - fetch, fast-forward the branches that are only
//! behind, integrate the checked-out branch like the Pull button does.
//!
//! Pure data and the planner, no I/O. The engine lives in the daemon
//! (`branch_sync.rs`); the CLI, the MCP server and the TUI read these types.
//!
//! The rules (the owner's decisions of the feature):
//!
//! * A branch that is not checked out is **never merged or rebased**: only
//!   behind (a fast-forward, a ref move) is applied; a diverged one is
//!   skipped and reported.
//! * The checked-out branch is integrated through the ordinary pull path, in
//!   the mode the caller chose (`head_mode`, else the configured pull mode).
//! * A behind branch checked out in another worktree is skipped or
//!   fast-forwarded in that worktree, as the caller asked
//!   ([`WorktreePolicy`]).
//! * Only the primary upstream of a branch counts; GitPoulpi's extra pull
//!   sources are ignored. A branch with no upstream is not part of the run.

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use super::{FetchOptions, Oid, PullMode};

/// What to do with a behind branch that is checked out in another worktree.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorktreePolicy {
    /// Leave it, report it as `checked_out_elsewhere`.
    #[default]
    Skip,
    /// Fast-forward it inside that worktree (refused, and reported as
    /// `failed`, when local changes there would be overwritten).
    Update,
}

/// Arguments of `PullAllBranches`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct PullAllBranchesRequest {
    /// Fetch the upstream remotes of the local branches first (the default).
    /// Off: work from the remote-tracking refs as they are.
    pub fetch: bool,
    /// Options of those fetches (`prune`).
    pub fetch_opts: FetchOptions,
    /// How the checked-out branch is integrated. `None`: the configured pull
    /// mode (what the Pull button uses).
    pub head_mode: Option<PullMode>,
    /// What to do with behind branches checked out in other worktrees.
    pub worktrees: WorktreePolicy,
}

impl Default for PullAllBranchesRequest {
    fn default() -> Self {
        PullAllBranchesRequest {
            fetch: true,
            fetch_opts: FetchOptions::default(),
            head_mode: None,
            worktrees: WorktreePolicy::Skip,
        }
    }
}

/// How one branch ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BranchSyncStatus {
    /// Moved to its upstream (a fast-forward, or the checked-out branch
    /// merged or rebased).
    Updated,
    UpToDate,
    /// Only ahead of its upstream: nothing to pull.
    Ahead,
    /// Has commits its upstream lacks and the reverse: not touched (a branch
    /// that is not checked out is never merged or rebased; a fast-forward
    /// only checked-out branch cannot fast-forward).
    Diverged,
    /// The remote-tracking branch it follows no longer exists.
    UpstreamGone,
    /// Behind, but checked out in another worktree and the run was told to
    /// skip those.
    CheckedOutElsewhere,
    /// Behind and checked out in another worktree, which was fast-forwarded.
    WorktreeUpdated,
    /// Its upstream is another local branch (`branch.<x>.remote = .`):
    /// nothing was downloaded for it.
    LocalUpstream,
    /// The fetch of its remote failed, so it was left alone.
    FetchFailed,
    /// The move was refused or failed (see `detail`).
    Failed,
    /// The checked-out branch's integration stopped on conflicts: the
    /// repository is in the same state a conflicting pull leaves.
    Conflicted,
}

impl BranchSyncStatus {
    /// The branch moved (counts for "N updated").
    pub fn moved(self) -> bool {
        matches!(
            self,
            BranchSyncStatus::Updated | BranchSyncStatus::WorktreeUpdated
        )
    }

    /// A status that makes the run's exit code non-zero.
    pub fn is_failure(self) -> bool {
        matches!(
            self,
            BranchSyncStatus::Failed | BranchSyncStatus::FetchFailed
        )
    }
}

/// One branch of the run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BranchSyncItem {
    pub branch: String,
    /// The upstream's shorthand name (`origin/feature/x`).
    pub upstream: Option<String>,
    pub status: BranchSyncStatus,
    /// The tip before the run.
    pub from: Option<Oid>,
    /// The tip after it (equal to `from` when nothing moved).
    pub to: Option<Oid>,
    /// Commits the branch has that its upstream lacks / the reverse, as
    /// the run found them (before any move).
    pub ahead: u32,
    pub behind: u32,
    /// The other worktree the branch is checked out in, if any.
    #[serde(default)]
    pub worktree: Option<PathBuf>,
    /// A sentence for `failed`, `fetch_failed` and `conflicted`.
    #[serde(default)]
    pub detail: Option<String>,
    /// The branch checked out in the repository the run was started in.
    #[serde(default)]
    pub is_head: bool,
}

/// A remote whose fetch failed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RemoteFetchFailure {
    pub remote: String,
    pub error: String,
}

/// Answer of `PullAllBranches`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PullAllBranchesOutcome {
    /// The remotes fetched successfully.
    pub fetched: Vec<String>,
    pub fetch_failed: Vec<RemoteFetchFailure>,
    /// Every branch that has an upstream, checked-out one first, then by
    /// name.
    pub branches: Vec<BranchSyncItem>,
    /// The mode the checked-out branch was integrated in; `None` when there
    /// was no such step (detached HEAD, no upstream, nothing behind).
    #[serde(default)]
    pub head_mode: Option<PullMode>,
}

impl PullAllBranchesOutcome {
    /// Branches that moved.
    pub fn updated(&self) -> usize {
        self.branches.iter().filter(|b| b.status.moved()).count()
    }

    /// The checked-out branch's integration stopped on conflicts.
    pub fn conflicted(&self) -> bool {
        self.branches
            .iter()
            .any(|b| b.status == BranchSyncStatus::Conflicted)
    }

    /// Something failed (a branch move or a fetch).
    pub fn has_failures(&self) -> bool {
        !self.fetch_failed.is_empty() || self.branches.iter().any(|b| b.status.is_failure())
    }
}

// ------------------------------------------------------------------ planner

/// Where a branch's upstream lives.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpstreamKind {
    /// A remote-tracking branch.
    Remote,
    /// Another local branch (`branch.<x>.remote = .`).
    Local,
}

/// What the daemon read about a branch's upstream.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpstreamFacts {
    pub name: String,
    pub kind: UpstreamKind,
    /// The upstream's tip; `None` when the tracking ref is gone.
    pub target: Option<Oid>,
    /// `(ahead, behind)` of the branch against the upstream (0, 0 when the
    /// tracking ref is gone).
    pub ahead: u32,
    pub behind: u32,
    /// The fetch of its remote failed.
    pub fetch_failed: bool,
}

/// What the daemon read about a local branch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchFacts {
    pub branch: String,
    pub tip: Oid,
    /// Checked out in the worktree the run was started in.
    pub is_head: bool,
    /// Checked out in another linked worktree, at this path.
    pub other_worktree: Option<PathBuf>,
    pub upstream: Option<UpstreamFacts>,
}

/// What the run does with a branch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PlannedAction {
    /// Nothing to do: this is its final status.
    Report(BranchSyncStatus),
    /// Move the ref to `to` (not checked out anywhere).
    FastForward { to: Oid },
    /// Fast-forward inside the other worktree.
    FastForwardInWorktree { to: Oid, path: PathBuf },
    /// The checked-out branch: integrate through the pull path.
    IntegrateHead,
}

/// One planned branch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlannedBranch {
    pub branch: String,
    pub upstream: String,
    pub from: Oid,
    pub ahead: u32,
    pub behind: u32,
    pub worktree: Option<PathBuf>,
    pub is_head: bool,
    pub action: PlannedAction,
}

/// Decides, per branch with an upstream, what the run does. Branches
/// without one are not part of the run and are left out. The checked-out
/// branch comes first, the others by name.
pub fn plan(facts: &[BranchFacts], worktrees: WorktreePolicy) -> Vec<PlannedBranch> {
    use BranchSyncStatus as S;
    let mut planned: Vec<PlannedBranch> = facts
        .iter()
        .filter_map(|f| {
            let up = f.upstream.as_ref()?;
            let action = if up.kind == UpstreamKind::Local {
                PlannedAction::Report(S::LocalUpstream)
            } else if up.fetch_failed {
                PlannedAction::Report(S::FetchFailed)
            } else if let Some(target) = up.target {
                match (up.ahead, up.behind) {
                    (0, 0) => PlannedAction::Report(S::UpToDate),
                    (_, 0) => PlannedAction::Report(S::Ahead),
                    _ if f.is_head => PlannedAction::IntegrateHead,
                    (ahead, _) if ahead > 0 => PlannedAction::Report(S::Diverged),
                    _ => match (&f.other_worktree, worktrees) {
                        (Some(path), WorktreePolicy::Update) => {
                            PlannedAction::FastForwardInWorktree {
                                to: target,
                                path: path.clone(),
                            }
                        }
                        (Some(_), WorktreePolicy::Skip) => {
                            PlannedAction::Report(S::CheckedOutElsewhere)
                        }
                        (None, _) => PlannedAction::FastForward { to: target },
                    },
                }
            } else {
                PlannedAction::Report(S::UpstreamGone)
            };
            Some(PlannedBranch {
                branch: f.branch.clone(),
                upstream: up.name.clone(),
                from: f.tip,
                ahead: up.ahead,
                behind: up.behind,
                worktree: f.other_worktree.clone(),
                is_head: f.is_head,
                action,
            })
        })
        .collect();
    planned.sort_by(|a, b| {
        b.is_head
            .cmp(&a.is_head)
            .then_with(|| a.branch.cmp(&b.branch))
    });
    planned
}

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

    fn oid(n: u8) -> Oid {
        Oid::from_hex(&format!("{n:02x}").repeat(20)).unwrap()
    }

    fn facts(name: &str, ahead: u32, behind: u32) -> BranchFacts {
        BranchFacts {
            branch: name.into(),
            tip: oid(1),
            is_head: false,
            other_worktree: None,
            upstream: Some(UpstreamFacts {
                name: format!("origin/{name}"),
                kind: UpstreamKind::Remote,
                target: Some(oid(2)),
                ahead,
                behind,
                fetch_failed: false,
            }),
        }
    }

    fn action_of(f: BranchFacts, w: WorktreePolicy) -> PlannedAction {
        plan(&[f], w).remove(0).action
    }

    #[test]
    fn a_branch_without_an_upstream_is_not_part_of_the_run() {
        let mut f = facts("a", 0, 0);
        f.upstream = None;
        assert!(plan(&[f], WorktreePolicy::Skip).is_empty());
    }

    #[test]
    fn equal_and_ahead_branches_are_reported_not_touched() {
        use BranchSyncStatus as S;
        assert_eq!(
            action_of(facts("a", 0, 0), WorktreePolicy::Skip),
            PlannedAction::Report(S::UpToDate)
        );
        assert_eq!(
            action_of(facts("a", 3, 0), WorktreePolicy::Skip),
            PlannedAction::Report(S::Ahead)
        );
    }

    #[test]
    fn a_behind_branch_fast_forwards_to_its_upstream_tip() {
        assert_eq!(
            action_of(facts("a", 0, 2), WorktreePolicy::Skip),
            PlannedAction::FastForward { to: oid(2) }
        );
    }

    #[test]
    fn a_diverged_branch_that_is_not_checked_out_is_skipped() {
        assert_eq!(
            action_of(facts("a", 1, 2), WorktreePolicy::Update),
            PlannedAction::Report(BranchSyncStatus::Diverged)
        );
    }

    #[test]
    fn the_checked_out_branch_is_integrated_when_behind_even_if_diverged() {
        let mut f = facts("a", 0, 2);
        f.is_head = true;
        assert_eq!(
            action_of(f.clone(), WorktreePolicy::Skip),
            PlannedAction::IntegrateHead
        );
        f.upstream.as_mut().unwrap().ahead = 4;
        assert_eq!(
            action_of(f, WorktreePolicy::Skip),
            PlannedAction::IntegrateHead
        );
    }

    #[test]
    fn a_branch_in_another_worktree_follows_the_policy() {
        let mut f = facts("a", 0, 1);
        f.other_worktree = Some(PathBuf::from("/w/a"));
        assert_eq!(
            action_of(f.clone(), WorktreePolicy::Skip),
            PlannedAction::Report(BranchSyncStatus::CheckedOutElsewhere)
        );
        assert_eq!(
            action_of(f.clone(), WorktreePolicy::Update),
            PlannedAction::FastForwardInWorktree {
                to: oid(2),
                path: PathBuf::from("/w/a")
            }
        );
        // Diverged there: never touched, whatever the policy.
        f.upstream.as_mut().unwrap().ahead = 1;
        assert_eq!(
            action_of(f, WorktreePolicy::Update),
            PlannedAction::Report(BranchSyncStatus::Diverged)
        );
    }

    #[test]
    fn a_gone_local_or_unfetched_upstream_is_reported() {
        use BranchSyncStatus as S;
        let mut gone = facts("a", 0, 0);
        gone.upstream.as_mut().unwrap().target = None;
        assert_eq!(
            action_of(gone, WorktreePolicy::Skip),
            PlannedAction::Report(S::UpstreamGone)
        );
        let mut local = facts("a", 0, 3);
        local.upstream.as_mut().unwrap().kind = UpstreamKind::Local;
        assert_eq!(
            action_of(local, WorktreePolicy::Skip),
            PlannedAction::Report(S::LocalUpstream)
        );
        let mut failed = facts("a", 0, 3);
        failed.upstream.as_mut().unwrap().fetch_failed = true;
        assert_eq!(
            action_of(failed, WorktreePolicy::Skip),
            PlannedAction::Report(S::FetchFailed)
        );
    }

    #[test]
    fn the_checked_out_branch_comes_first_then_by_name() {
        let mut head = facts("zeta", 0, 0);
        head.is_head = true;
        let planned = plan(
            &[facts("b", 0, 0), head, facts("a", 0, 0)],
            WorktreePolicy::Skip,
        );
        let names: Vec<_> = planned.iter().map(|p| p.branch.as_str()).collect();
        assert_eq!(names, ["zeta", "a", "b"]);
    }

    #[test]
    fn the_request_defaults_to_fetching_and_skipping_worktrees() {
        let req: PullAllBranchesRequest = serde_json::from_str("{}").unwrap();
        assert!(req.fetch);
        assert_eq!(req.head_mode, None);
        assert_eq!(req.worktrees, WorktreePolicy::Skip);
        let req: PullAllBranchesRequest =
            serde_json::from_str(r#"{"fetch":false,"head_mode":"rebase","worktrees":"update"}"#)
                .unwrap();
        assert!(!req.fetch);
        assert_eq!(req.head_mode, Some(PullMode::Rebase));
        assert_eq!(req.worktrees, WorktreePolicy::Update);
    }

    #[test]
    fn the_outcome_counts_moves_and_failures() {
        let item = |status| BranchSyncItem {
            branch: "a".into(),
            upstream: None,
            status,
            from: None,
            to: None,
            ahead: 0,
            behind: 0,
            worktree: None,
            detail: None,
            is_head: false,
        };
        let mut out = PullAllBranchesOutcome {
            branches: vec![
                item(BranchSyncStatus::Updated),
                item(BranchSyncStatus::WorktreeUpdated),
                item(BranchSyncStatus::Diverged),
            ],
            ..PullAllBranchesOutcome::default()
        };
        assert_eq!(out.updated(), 2);
        assert!(!out.has_failures());
        assert!(!out.conflicted());
        out.branches.push(item(BranchSyncStatus::Failed));
        assert!(out.has_failures());
        out.branches.push(item(BranchSyncStatus::Conflicted));
        assert!(out.conflicted());
    }
}