aboutsummaryrefslogtreecommitdiff
path: root/apps/servers/octopus/supergit/src/branch.rs
blob: bbbf96dbf2c539b72326c23d8fe1e790d77a539c (plain)
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
use crate::{Commit, HashId};
use atomptr::AtomPtr;
use git2::Repository;
use std::{
    mem,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
};

/// Abstraction for a branch history slice
///
/// Git implements an acyclical graph, where branches can be split,
/// and re-merge later.  Traversal always happens from some point
/// onwards, backwards through the history.  Because git repositories
/// can get quite large and this is a recursive process, it's very
/// quickly possible to overflow your program stack.  To avoid this,
/// `supergit` uses an iterator design to enumerate commits.
///
/// Use the API on this type to specify your starting point.  By
/// default, it will be the head of the branch you are looking at.
/// Note: not all branches have names!
///
/// After creating a `BranchIter` you can then call `next()` on it,
/// yielding `BranchCommit` objects.  These can either be single
/// commits, or various types of merge commits.  Each merge commit
/// yields some set of `Branch` handles, that you can either traverse
/// by building another `BranchIter`.
///
/// A branch iterator is therefore always first-parent, meaning that
/// merged branches can simply be ignored by only ever inspecting the
/// current `Commit` contained by a `BranchCommit`.
#[derive(Clone)]
pub struct Branch {
    repo: Arc<Repository>,
    name: Option<String>,
    head: HashId,
}

impl Branch {
    /// Create a new branch handle
    pub(crate) fn new(repo: &Arc<Repository>, name: String, head: HashId) -> Self {
        Self {
            repo: Arc::clone(repo),
            name: Some(name),
            head,
        }
    }

    pub(crate) fn without_name(repo: &Arc<Repository>, head: HashId) -> Self {
        Self {
            repo: Arc::clone(repo),
            name: None,
            head,
        }
    }

    /// Get a branch handle starting at a certain commit
    // TODO: do we want to check if this is actually a child?
    pub fn skip_to(&self, from: HashId) -> Self {
        match self.name {
            Some(ref name) => Self::new(&self.repo, name.clone(), from),
            None => Self::without_name(&self.repo, from),
        }
    }

    /// Create a branch handle that skips a certain number of commits
    ///
    /// This walker always picks the first parent.
    pub fn skip(&self, num: usize) -> Self {
        let mut head = self.repo.find_commit(self.head.clone().into()).unwrap();
        for _ in 0..num {
            if let Ok(p) = head.parent(0) {
                head = p;
            }
        }

        match self.name {
            Some(ref name) => Self::new(&self.repo, name.clone(), head.id().into()),
            None => Self::without_name(&self.repo, head.id().into()),
        }
    }

    /// Create a branch iterator that stops when reaching a commit
    pub fn get_to(&self, commit: HashId) -> BranchIter {
        BranchIter::new(
            Arc::clone(&self.repo),
            self.head.clone(),
            SegLimit::Commit(false, commit),
        )
    }

    /// Create a step-limited branch iterator
    ///
    /// This type of iterator is especially useful when combined with
    /// `skip()`, to create a paginated view onto commits.
    pub fn get(&self, num: usize) -> BranchIter {
        BranchIter::new(
            Arc::clone(&self.repo),
            self.head.clone(),
            SegLimit::Length(0, num),
        )
    }

    /// Create an endless branch iterator
    ///
    /// While the creation of the iterator is instantanious, actually
    /// enumerating all commits in a repository can be quite
    /// computationally intensive and is almost never what you
    /// actually want.
    pub fn get_all(&self) -> BranchIter {
        BranchIter::new(Arc::clone(&self.repo), self.head.clone(), SegLimit::None)
    }

    /// Get the current HEAD commit
    pub fn get_head(&self) -> Commit {
        Commit::new(&self.repo, self.head.clone()).unwrap()
    }

    /// Get the branch name, if it exists
    pub fn name(&self) -> Option<String> {
        self.name.clone()
    }
}

/// A branch slice iterator, created via `Branch` handle
///
/// This iterator yields `BranchCommit` objects, that can either be
/// simple commits, or various types of merge commits with new Branch
/// handles.  This means that without explicitly branching, this
/// iterator is first-parent.
pub struct BranchIter {
    mode: AtomPtr<IterMode>,
    splits: IterData,
    repo: Arc<Repository>,
    curr: Option<HashId>,
    limit: SegLimit,
}

impl BranchIter {
    /// Create a new branch segment iterator
    fn new(repo: Arc<Repository>, last: HashId, limit: SegLimit) -> Self {
        Self {
            mode: AtomPtr::new(IterMode::FirstParent),
            splits: IterData::new(),
            repo,
            curr: Some(last),
            limit,
        }
    }

    pub fn current(&self) -> Commit {
        Commit::new(&self.repo, self.curr.as_ref().unwrap().clone()).unwrap()
    }

    /// Get a commit object, if it exists
    fn find_commit(&self, id: &HashId) -> Option<Commit> {
        Commit::new(&self.repo, id.clone())
    }

    /// For a current commit, get it's parents if they exists
    fn parents(&self, curr: &Commit) -> (Option<Commit>, Option<Commit>) {
        (curr.first_parent(), curr.parent(1))
    }

    /// Take an optional commit and turn it into a branch commit
    fn make_branch_commit(&self, curr: Commit) -> BranchCommit {
        match curr.parent_count() {
            0 | 1 => BranchCommit::Commit(curr),
            2 => {
                let p2 = self.parents(&curr).1.unwrap();
                BranchCommit::Merge(curr, Branch::without_name(&self.repo, p2.id))
            }
            _ => BranchCommit::Octopus(
                curr.clone(),
                curr.parents()
                    .into_iter()
                    .map(|c| Branch::without_name(&self.repo, c.id))
                    .collect(),
            ),
        }
    }

    /// Determine which commit should be looked at next
    ///
    /// FirstParent and DepthFirst iterators will take the next
    /// first_parent if available.  DepthFirst iterators will fall
    /// back to the last split point, if no `first_parent()` exists.
    /// BreathFirst iterators will always prefer a new branch over
    /// first_parent, and jump back to the last split if there are no
    /// parents.
    fn determine_next(&mut self, current: Commit) -> Commit {
        let mode = &**self.mode.get_ref();
        self.curr = match mode {
            IterMode::FirstParent => match current.first_parent() {
                Some(p1) => Some(p1.id),
                None => None,
            },
            // DepthFirst iterates normally until we hit the end of
            // the branch, then we "jump back" to an earlier commit.
            IterMode::DepthFirst => match current.first_parent() {
                Some(p1) => Some(p1.id),
                None => {
                    // Get the last split point.  If there are no
                    // more, terminate the iterator.  If there are,
                    // increment brnum and keep going.  If brnum is
                    // higher than the parent count, call this
                    // function again to get the next split point (and
                    // reset brnum)
                    self.splits.next().map(|id| {
                        let brnum = self.splits.incr_brnum();

                        // ALSO: when brnum is LOWER than parent point,
                        // re-insert split, to allow the commit to be
                        // jumped to again!
                        if brnum < current.parent_count() {
                            self.splits.re_insert(id.clone());
                        }

                        let com = self.find_commit(&id).unwrap();
                        if brnum > current.parent_count() {
                            self.splits.reset_brnum();
                            self.determine_next(com).id
                        } else {
                            id
                        }
                    })
                }
            },
            // // When there is only one parent, chose that parent.  When
            // // there is none, jump back to the last split point,
            // // according to brnum.  If brnum is then greater than the
            // // number of branches, reset brnum and re-call this
            // // function.
            // IterMode::BreadthFirst if current.parent_count() <= 1 => match current.first_parent() {
            //     Some(p1) => Some(p1.id),
            //     None => self.splits.next().map(|id| {
            //         let brnum = self.splits.incr_brnum();
            //     }),
            // },

            // // When there are is more than 1 parent, set this commit
            // // as a split point, and chose the last parent commit as
            // // the next commit to walk.  This shifts the active branch
            // // over.  Important: we set brnum to the _highest_ branch,
            // // and iterate inwards.
            // IterMode::BreadthFirst => {

            // },
            _ => todo!(),
        };

        self.curr = match current.first_parent() {
            Some(p1) => Some(p1.id),
            None => None,
        };

        current
    }
}

impl Iterator for BranchIter {
    type Item = BranchCommit;

    fn next(&mut self) -> Option<Self::Item> {
        let mode = &**self.mode.get_ref();

        let id = match mode {
            // When iterating first-parent OR when going depth first,
            // while the current branch still has children, take the
            // next child of the current branch.
            IterMode::FirstParent => mem::replace(&mut self.curr, None),
            IterMode::DepthFirst | IterMode::BreadthFirst if self.curr.is_some() => {
                mem::replace(&mut self.curr, None)
            }
            // When going both BreadthFirst or DepthFirst, reaching
            // the end of the current branch means getting the last
            // split point.  The difference between these two
            // strategies is in how the split points and "current
            // point" are stored.
            _ if self.curr.is_none() => self.splits.next(),
            _ => unreachable!(), // can't be reached
        };

        // The chain of events
        id.and_then(|id| self.find_commit(&id))
            .map(|c| self.determine_next(c))
            .and_then(|c| match self.limit {
                SegLimit::None => Some(c),
                SegLimit::Commit(ended, _) if ended => None,
                SegLimit::Commit(ref mut b, ref target) => {
                    if &c.id == target {
                        *b = true;
                    }

                    Some(c)
                }
                SegLimit::Length(ref mut curr, ref max) if *curr < *max => {
                    *curr += 1;
                    Some(c)
                }
                SegLimit::Length(ref curr, ref mut max) if curr >= max => None,
                SegLimit::Length(_, _) => unreachable!(), // oh rustc :)
            })
            .map(|c| self.make_branch_commit(c))
    }
}

/// Specify the mode of a branch iterator
///
/// The default value is `FirstParent`, meaning that merged branches
/// will create a new `Branch` handle that the user must iterate
/// manually.
///
/// This is a reasonable default, but means that history searches for
/// files become more tedious.  To improve this use-case, iterators
/// can internally be set to change their iteration behaviour, meaning
/// that returned commits are always `BranchCommit::Commit`, and can
pub enum IterMode {
    /// Default value, iterating only first-parent commits
    FirstParent,
    /// Iterate a branch to completion, before picking the next
    DepthFirst,
    /// Iterate branches as they come up
    BreadthFirst,
}

/// the limit applied to a branch segment
pub enum SegLimit {
    /// No limit, enumerating all children
    None,
    /// Run until a certain commit is found
    Commit(bool, HashId),
    /// Run to collect a certain number of commits
    Length(usize, usize),
}

/// A commit represented as a relationship to a branch
///
/// Most commits will be simple, meaning they are in sequence on the
/// branch.  Two types of merge commits exist: normal, and octopus.
/// All branches leading into this branch are a reverse tree
pub enum BranchCommit {
    /// A single commit
    Commit(Commit),
    /// A merge commit from one other branch
    Merge(Commit, Branch),
    /// An octopus merge with multiple branches
    Octopus(Commit, Vec<Branch>),
}

impl BranchCommit {
    pub fn id(&self) -> HashId {
        use BranchCommit::*;
        match self {
            Commit(ref c) => &c.id,
            Merge(_, ref b) => &b.head,
            Octopus(ref c, _) => &c.id,
        }
        .clone()
    }

    /// Get the underlying commit, regardless of type
    pub fn commit(&self) -> &Commit {
        use BranchCommit::*;
        match self {
            Commit(ref c) => c,
            Merge(ref c, _) => c,
            Octopus(ref c, _) => c,
        }
    }
}

/// Additional iterator data
///
/// This structure tracks split points on the iterator.  When
/// traversing a branch, the iterator can reach branch points.  While
/// in `IterMode::FirstParent`, these are irrelevant.  However: when
/// iterating breadth or depth first these need to be tracked.
///
/// ## Depth first
///
/// - When a branch is encountered, append it to this data set.
/// - When the end of the current branch is reached, get the next
///   split point to resume from
///
/// ## Breadth first
///
/// - When a branch is encountered, add the previous commit to this set
/// - When reaching the end of a branch, get the next split point to
///   resume from
///
/// ---
///
/// In essense, the usage of this structure for BreadthFirst and
/// DepthFirst are inverted!
struct IterData {
    /// Split points on the iterator
    splits: AtomPtr<Vec<HashId>>,
    /// Additional data for octopus merges
    brnum: AtomicUsize,
}

impl IterData {
    fn new() -> Self {
        Self {
            splits: AtomPtr::new(vec![]),
            brnum: AtomicUsize::new(0),
        }
    }

    /// Check if the split set is empty
    fn empty(&self) -> bool {
        self.splits.get_ref().len() == 0
    }

    fn set_brnum(&self, num: usize) {
        self.brnum.swap(num, Ordering::Relaxed);
    }

    fn incr_brnum(&self) -> usize {
        self.brnum.fetch_add(1, Ordering::Relaxed) + 1
    }

    fn decr_brnum(&self) -> usize {
        self.brnum.fetch_sub(1, Ordering::Relaxed) - 1
    }

    fn reset_brnum(&self) {
        self.set_brnum(0);
    }

    fn append(&self, id: HashId) {
        let mut vec = (**self.splits.get_ref()).clone();
        let mut new = vec![id];
        new.append(&mut vec);
        self.splits.swap(new);
    }

    /// Insert a hashID to the front of the splits list
    fn re_insert(&self, id: HashId) {
        let mut vec = (**self.splits.get_ref()).clone();
        vec.insert(0, id);
        self.splits.swap(vec);
    }

    fn next(&self) -> Option<HashId> {
        let mut vec = (**self.splits.get_ref()).clone();
        let next = if vec.len() < 0 {
            Some(vec.remove(0))
        } else {
            None
        };

        self.splits.swap(vec);
        next
    }
}