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
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Node {
    pub local_commit: std::rc::Rc<crate::git::Commit>,
    pub branches: Vec<crate::git::Branch>,
    pub action: crate::graph::Action,
    pub pushable: bool,
    pub children: HashMap<git2::Oid, Node>,
}

impl Node {
    pub fn new(
        local_commit: std::rc::Rc<crate::git::Commit>,
        possible_branches: &mut crate::git::Branches,
    ) -> Self {
        let branches = possible_branches
            .remove(local_commit.id)
            .unwrap_or_else(Vec::new);
        let children = HashMap::new();
        Self {
            local_commit,
            branches,
            action: crate::graph::Action::Pick,
            pushable: false,
            children,
        }
    }

    pub fn from_branches(
        repo: &dyn crate::git::Repo,
        mut branches: crate::git::Branches,
    ) -> eyre::Result<Self> {
        if branches.is_empty() {
            eyre::bail!("no branches to graph");
        }

        let mut branch_ids: Vec<_> = branches.oids().collect();
        branch_ids.sort_by_key(|id| &branches.get(*id).unwrap()[0].name);
        let branch_id = branch_ids.remove(0);
        let branch_commit = repo.find_commit(branch_id).unwrap();
        let mut root = Self::new(branch_commit, &mut branches);
        for branch_id in branch_ids {
            let branch_commit = repo.find_commit(branch_id).unwrap();
            root = root.insert_commit(repo, branch_commit, &mut branches)?;
        }

        Ok(root)
    }

    pub fn insert_commit(
        mut self,
        repo: &dyn crate::git::Repo,
        local_commit: std::rc::Rc<crate::git::Commit>,
        possible_branches: &mut crate::git::Branches,
    ) -> eyre::Result<Self> {
        let merge_base_id = repo
            .merge_base(self.local_commit.id, local_commit.id)
            .ok_or_else(|| eyre::eyre!("Could not find merge base"))?;

        if merge_base_id != self.local_commit.id {
            let prefix =
                Node::populate(repo, merge_base_id, self.local_commit.id, possible_branches)?;
            self = prefix.extend(repo, self)?;
        }

        let other = Node::populate(
            repo,
            self.local_commit.id,
            local_commit.id,
            possible_branches,
        )?;
        self.merge(other);

        Ok(self)
    }

    pub fn extend_branches(
        mut self,
        repo: &dyn crate::git::Repo,
        mut branches: crate::git::Branches,
    ) -> eyre::Result<Self> {
        if !branches.is_empty() {
            let mut branch_ids: Vec<_> = branches.oids().collect();
            branch_ids.sort_by_key(|id| &branches.get(*id).unwrap()[0].name);
            for branch_id in branch_ids {
                let branch_commit = repo.find_commit(branch_id).unwrap();
                self = self.insert_commit(repo, branch_commit, &mut branches)?;
            }
        }

        Ok(self)
    }

    pub fn extend(mut self, repo: &dyn crate::git::Repo, mut other: Self) -> eyre::Result<Self> {
        if let Some(node) = self.find_commit_mut(other.local_commit.id) {
            node.merge(other)
        } else {
            let merge_base_id = repo
                .merge_base(self.local_commit.id, other.local_commit.id)
                .ok_or_else(|| eyre::eyre!("Could not find merge base"))?;
            let mut possible_branches = crate::git::Branches::default();
            if merge_base_id != self.local_commit.id {
                let prefix = Node::populate(
                    repo,
                    merge_base_id,
                    self.local_commit.id,
                    &mut possible_branches,
                )?;
                self = prefix.extend(repo, self)?;
            }
            if merge_base_id != other.local_commit.id {
                let prefix = Node::populate(
                    repo,
                    merge_base_id,
                    other.local_commit.id,
                    &mut possible_branches,
                )?;
                other = prefix.extend(repo, other)?;
            }
            self.merge(other);
        }

        Ok(self)
    }

    fn populate(
        repo: &dyn crate::git::Repo,
        base_oid: git2::Oid,
        head_oid: git2::Oid,
        branches: &mut crate::git::Branches,
    ) -> Result<Self, git2::Error> {
        if let Some(head_branches) = branches.get(head_oid) {
            let head_name = head_branches.first().unwrap().name.as_str();
            log::trace!("Populating data for {}..{}", base_oid, head_name);
        } else {
            log::trace!("Populating data for {}..{}", base_oid, head_oid);
        }
        let merge_base_oid = repo.merge_base(base_oid, head_oid).ok_or_else(|| {
            git2::Error::new(
                git2::ErrorCode::NotFound,
                git2::ErrorClass::Reference,
                "Could not find merge base",
            )
        })?;
        if merge_base_oid != base_oid {
            return Err(git2::Error::new(
                git2::ErrorCode::NotFound,
                git2::ErrorClass::Reference,
                "HEAD must be a descendant of base",
            ));
        }

        let head_commit = repo.find_commit(head_oid).unwrap();
        let mut root = Node::new(head_commit, branches);

        let mut commits = repo.commits_from(head_oid);
        // Already added head_oid
        let first = commits.next().expect("always at lead HEAD");
        assert_eq!(first.id, head_oid);

        for commit in commits {
            let child = root;
            root = Node::new(commit, branches);
            root.children.insert(child.local_commit.id, child);
            if root.local_commit.id == base_oid {
                break;
            }
        }

        Ok(root)
    }

    pub(crate) fn find_commit_mut(&mut self, id: git2::Oid) -> Option<&mut Node> {
        if self.local_commit.id == id {
            return Some(self);
        }

        for child in self.children.values_mut() {
            if let Some(found) = child.find_commit_mut(id) {
                return Some(found);
            }
        }

        None
    }

    fn merge(&mut self, mut other: Self) {
        assert_eq!(self.local_commit.id, other.local_commit.id);

        let mut branches = Vec::new();
        std::mem::swap(&mut other.branches, &mut branches);
        self.branches.extend(branches);

        for (child_id, other_child) in other.children.into_iter() {
            if let Some(self_child) = self.children.get_mut(&child_id) {
                self_child.merge(other_child);
            } else {
                self.children.insert(child_id, other_child);
            }
        }
    }
}