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
mod model;

pub use model::*;

#[allow(unused_imports)] // Not bothering matching the right features
use eyre::WrapErr;

impl TodoList {
    pub fn load(path: &std::path::Path) -> eyre::Result<Self> {
        match path.extension().and_then(std::ffi::OsStr::to_str) {
            #[cfg(feature = "yaml")]
            Some("yaml") | Some("yml") => {
                let data = std::fs::read_to_string(path)
                    .wrap_err_with(|| format!("Could not read {}", path.display()))?;

                Self::parse_yaml(&data)
                    .wrap_err_with(|| format!("Could not parse {}", path.display()))
            }
            #[cfg(feature = "json")]
            Some("json") => {
                let data = std::fs::read_to_string(path)
                    .wrap_err_with(|| format!("Could not read {}", path.display()))?;

                Self::parse_json(&data)
                    .wrap_err_with(|| format!("Could not parse {}", path.display()))
            }
            #[cfg(feature = "toml")]
            Some("toml") => {
                let data = std::fs::read_to_string(path)
                    .wrap_err_with(|| format!("Could not read {}", path.display()))?;

                Self::parse_toml(&data)
                    .wrap_err_with(|| format!("Could not parse {}", path.display()))
            }
            Some(other) => Err(eyre::eyre!("Unknown extension: {:?}", other)),
            None => Err(eyre::eyre!("No extension for {}", path.display())),
        }
    }

    pub fn save(&self, path: &std::path::Path) -> eyre::Result<()> {
        match path.extension().and_then(std::ffi::OsStr::to_str) {
            #[cfg(feature = "yaml")]
            Some("yaml") | Some("yml") => {
                let raw = self
                    .to_yaml()
                    .wrap_err_with(|| format!("Could not parse {}", path.display()))?;
                std::fs::write(path, &raw)
                    .wrap_err_with(|| format!("Could not write {}", path.display()))
            }
            #[cfg(feature = "json")]
            Some("json") => {
                let raw = self
                    .to_json()
                    .wrap_err_with(|| format!("Could not parse {}", path.display()))?;
                std::fs::write(path, &raw)
                    .wrap_err_with(|| format!("Could not write {}", path.display()))
            }
            #[cfg(feature = "toml")]
            Some("toml") => {
                let raw = self
                    .to_toml()
                    .wrap_err_with(|| format!("Could not parse {}", path.display()))?;
                std::fs::write(path, &raw)
                    .wrap_err_with(|| format!("Could not write {}", path.display()))
            }
            Some(other) => Err(eyre::eyre!("Unknown extension: {:?}", other)),
            None => Err(eyre::eyre!("No extension for {}", path.display())),
        }
    }

    #[cfg(feature = "yaml")]
    pub fn parse_yaml(data: &str) -> eyre::Result<Self> {
        serde_yaml::from_str(data).map_err(|err| err.into())
    }

    #[cfg(feature = "json")]
    pub fn parse_json(data: &str) -> eyre::Result<Self> {
        serde_json::from_str(data).map_err(|err| err.into())
    }

    #[cfg(feature = "toml")]
    pub fn parse_toml(data: &str) -> eyre::Result<Self> {
        toml::from_str(data).map_err(|err| err.into())
    }

    #[cfg(feature = "yaml")]
    pub fn to_yaml(&self) -> eyre::Result<String> {
        serde_yaml::to_string(self).map_err(|err| err.into())
    }

    #[cfg(feature = "json")]
    pub fn to_json(&self) -> eyre::Result<String> {
        serde_json::to_string(self).map_err(|err| err.into())
    }

    #[cfg(feature = "toml")]
    pub fn to_toml(&self) -> eyre::Result<String> {
        toml::to_string(self).map_err(|err| err.into())
    }
}

impl TodoList {
    pub fn run(self, cwd: &std::path::Path) -> eyre::Result<()> {
        let repo = if self.init {
            git2::Repository::init(cwd)?
        } else {
            git2::Repository::open(cwd)?
        };

        let mut head = None;
        let mut last_oid = repo
            .head()
            .and_then(|h| h.resolve())
            .ok()
            .and_then(|r| r.target());
        let mut labels: std::collections::HashMap<Label, git2::Oid> = Default::default();
        for (i, event) in self.commands.iter().enumerate() {
            match event {
                Command::Label(label) => {
                    let current_oid = last_oid.ok_or_else(|| eyre::eyre!("no commits yet"))?;
                    log::trace!("label {}  # {}", label, current_oid);
                    labels.insert(label.clone(), current_oid);
                }
                Command::Reset(label) => {
                    let current_oid = *labels
                        .get(label.as_str())
                        .ok_or_else(|| eyre::eyre!("Label doesn't exist: {:?}", label))?;
                    log::trace!("reset {}  # {}", label, current_oid);
                    last_oid = Some(current_oid);
                }
                Command::Tree(tree) => {
                    let mut builder = repo.treebuilder(None)?;
                    for (relpath, content) in tree.files.iter() {
                        let relpath = path2bytes(relpath);
                        let blob_id = repo.blob(content.as_bytes())?;
                        let mode = 0o100755;
                        builder.insert(relpath, blob_id, mode)?;
                    }
                    let new_tree_oid = builder.write()?;
                    let new_tree = repo.find_tree(new_tree_oid)?;

                    let sig =
                        if let Some(author) = tree.author.as_deref().or(self.author.as_deref()) {
                            git2::Signature::now(author, "")?
                        } else {
                            repo.signature()?
                        };
                    let message = tree
                        .message
                        .clone()
                        .unwrap_or_else(|| format!("Commit (command {i})"));
                    let mut parents = Vec::new();
                    if let Some(last_oid) = last_oid {
                        parents.push(repo.find_commit(last_oid)?);
                    }
                    let parents = parents.iter().collect::<Vec<_>>();
                    let current_oid =
                        repo.commit(None, &sig, &sig, &message, &new_tree, &parents)?;
                    last_oid = Some(current_oid);

                    if let Some(sleep) = self.sleep {
                        std::thread::sleep(sleep);
                    }
                }
                Command::Merge(merge) => {
                    let ours_oid = last_oid.ok_or_else(|| eyre::eyre!("no commits yet"))?;
                    log::trace!(
                        "merge {}  # {}",
                        merge
                            .base
                            .iter()
                            .map(|s| s.as_str())
                            .collect::<Vec<_>>()
                            .join(" "),
                        ours_oid
                    );
                    let mut parents = Vec::new();

                    let ours_commit = repo.find_commit(ours_oid)?;
                    let mut ours_tree_oid = ours_commit.tree_id();
                    parents.push(ours_commit);
                    for label in &merge.base {
                        let ours_tree = repo.find_tree(ours_tree_oid)?;

                        let their_oid = *labels
                            .get(label.as_str())
                            .ok_or_else(|| eyre::eyre!("Label doesn't exist: {:?}", label))?;
                        let their_commit = repo.find_commit(their_oid)?;
                        let their_tree = their_commit.tree()?;
                        parents.push(their_commit);

                        let base_oid = repo.merge_base(ours_oid, their_oid)?;
                        let base_commit = repo.find_commit(base_oid)?;
                        let base_tree = base_commit.tree()?;

                        let mut options = git2::MergeOptions::new();
                        options.find_renames(true);
                        options.fail_on_conflict(true);
                        let mut index =
                            repo.merge_trees(&base_tree, &ours_tree, &their_tree, Some(&options))?;
                        ours_tree_oid = index.write_tree()?;
                    }

                    let sig =
                        if let Some(author) = merge.author.as_deref().or(self.author.as_deref()) {
                            git2::Signature::now(author, "")?
                        } else {
                            repo.signature()?
                        };
                    let message = merge.message.clone().unwrap_or_else(|| {
                        format!(
                            "Merged {} (command {i})",
                            merge
                                .base
                                .iter()
                                .map(|s| s.as_str())
                                .collect::<Vec<_>>()
                                .join(" "),
                        )
                    });
                    let ours_tree = repo.find_tree(ours_tree_oid)?;
                    let parents = parents.iter().collect::<Vec<_>>();
                    let current_oid =
                        repo.commit(None, &sig, &sig, &message, &ours_tree, &parents)?;
                    last_oid = Some(current_oid);

                    if let Some(sleep) = self.sleep {
                        std::thread::sleep(sleep);
                    }
                }
                Command::Branch(branch) => {
                    let current_oid = last_oid.ok_or_else(|| eyre::eyre!("no commits yet"))?;
                    log::trace!("exec git branch --force {}  # {}", branch, current_oid);
                    let commit = repo.find_commit(current_oid)?;
                    repo.branch(branch.as_str(), &commit, true)?;
                }
                Command::Tag(tag) => {
                    let current_oid = last_oid.ok_or_else(|| eyre::eyre!("no commits yet"))?;
                    log::trace!("exec git tag --force -a {}  # {}", tag, current_oid);
                    let commit = repo.find_commit(current_oid)?;
                    let sig = if let Some(author) = self.author.as_deref() {
                        git2::Signature::now(author, "")?
                    } else {
                        repo.signature()?
                    };
                    let message = format!("Tag (command {i})");
                    repo.tag(tag.as_str(), commit.as_object(), &sig, &message, true)?;
                }
                Command::Head => {
                    let new_head = if let Some(branch) = self.last_branch(i) {
                        AnnotatedOid::Branch(branch)
                    } else {
                        let current_oid = last_oid.ok_or_else(|| eyre::eyre!("no commits yet"))?;
                        AnnotatedOid::Commit(current_oid)
                    };
                    log::trace!("exec git checkout {}", new_head);
                    head = Some(new_head);
                }
            }
        }

        let head = if let Some(head) = head {
            head
        } else if let Some(branch) = self.last_branch(self.commands.len()) {
            AnnotatedOid::Branch(branch)
        } else {
            let current_oid = last_oid.ok_or_else(|| eyre::eyre!("no commits yet"))?;
            AnnotatedOid::Commit(current_oid)
        };
        match head {
            AnnotatedOid::Commit(head) => {
                repo.set_head_detached(head)?;
            }
            AnnotatedOid::Branch(head) => {
                let branch = repo.find_branch(&head, git2::BranchType::Local)?;
                repo.set_head(branch.get().name().unwrap())?;
            }
        }
        repo.checkout_head(None)?;

        Ok(())
    }

    fn last_branch(&self, current_index: usize) -> Option<String> {
        if let Some(Command::Branch(prev)) = self.commands.get(current_index.saturating_sub(1)) {
            Some(prev.as_str().to_owned())
        } else {
            None
        }
    }
}

enum AnnotatedOid {
    Commit(git2::Oid),
    Branch(String),
}

impl std::fmt::Display for AnnotatedOid {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Commit(ann) => ann.fmt(f),
            Self::Branch(ann) => ann.fmt(f),
        }
    }
}

#[cfg(unix)]
fn path2bytes(p: &std::path::Path) -> Vec<u8> {
    use std::os::unix::prelude::*;
    p.as_os_str().as_bytes().to_vec()
}

#[cfg(not(unix))]
fn path2bytes(p: &std::path::Path) -> Vec<u8> {
    _path2bytes_utf8(p)
}

fn _path2bytes_utf8(p: &std::path::Path) -> Vec<u8> {
    let mut v = p.as_os_str().to_str().unwrap().as_bytes().to_vec();
    for c in &mut v {
        if *c == b'\\' {
            *c = b'/'
        }
    }
    v
}