upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/git.rs
blob: 281f00ce782e39c5c11cfd4ecd10ade261d73eef (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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
use std::env::current_dir;
#[cfg(test)]
use std::path::PathBuf;

use anyhow::{bail, Context, Result};
use git2::{Oid, Revwalk};
use nostr::prelude::{sha1::Hash as Sha1Hash, Hash};

pub struct Repo {
    git_repo: git2::Repository,
}

impl Repo {
    pub fn discover() -> Result<Self> {
        Ok(Self {
            git_repo: git2::Repository::discover(current_dir()?)?,
        })
    }
    #[cfg(test)]
    pub fn from_path(path: &PathBuf) -> Result<Self> {
        Ok(Self {
            git_repo: git2::Repository::open(path)?,
        })
    }
}

// pub type CommitId = [u8; 7];
// pub type Sha1 = [u8; 20];

pub trait RepoActions {
    fn get_local_branch_names(&self) -> Result<Vec<String>>;
    fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>;
    fn get_checked_out_branch_name(&self) -> Result<String>;
    fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash>;
    fn get_root_commit(&self, branch_name: &str) -> Result<Sha1Hash>;
    fn does_commit_exist(&self, commit: &str) -> Result<bool>;
    fn get_head_commit(&self) -> Result<Sha1Hash>;
    fn get_commit_parent(&self, commit: &Sha1Hash) -> Result<Sha1Hash>;
    fn get_commits_ahead_behind(
        &self,
        base_commit: &Sha1Hash,
        latest_commit: &Sha1Hash,
    ) -> Result<(Vec<Sha1Hash>, Vec<Sha1Hash>)>;
    fn make_patch_from_commit(&self, commit: &Sha1Hash) -> Result<String>;
    fn checkout(&self, ref_name: &str) -> Result<()>;
    fn create_branch_at_commit(&self, branch_name: &str, commit: &str) -> Result<()>;
}

impl RepoActions for Repo {
    fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)> {
        let main_branch_name = {
            let local_branches = self
                .get_local_branch_names()
                .context("cannot find any local branches")?;
            if local_branches.contains(&"main".to_string()) {
                "main"
            } else if local_branches.contains(&"master".to_string()) {
                "master"
            } else {
                bail!("no main or master branch locally in this git repository to initiate from",)
            }
        };

        let tip = self
            .get_tip_of_local_branch(main_branch_name)
            .context(format!(
                "branch {main_branch_name} was listed as a local branch but cannot get its tip commit id",
            ))?;

        Ok((main_branch_name, tip))
    }

    fn get_local_branch_names(&self) -> Result<Vec<String>> {
        let local_branches = self
            .git_repo
            .branches(Some(git2::BranchType::Local))
            .context("getting GitRepo branches should not error even for a blank repository")?;

        let mut branch_names = vec![];

        for iter in local_branches {
            let branch = iter?.0;
            if let Some(name) = branch.name()? {
                branch_names.push(name.to_string());
            }
        }
        Ok(branch_names)
    }

    fn get_checked_out_branch_name(&self) -> Result<String> {
        Ok(self
            .git_repo
            .head()?
            .shorthand()
            .context("an object without a shorthand is checked out")?
            .to_string())
    }

    fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash> {
        let branch = self
            .git_repo
            .find_branch(branch_name, git2::BranchType::Local)
            .context(format!("cannot find branch {branch_name}"))?;
        Ok(oid_to_sha1(&branch.into_reference().peel_to_commit()?.id()))
    }

    fn get_root_commit(&self, branch_name: &str) -> Result<Sha1Hash> {
        let tip = self.get_tip_of_local_branch(branch_name)?;
        let mut revwalk = self
            .git_repo
            .revwalk()
            .context("revwalk should be created from git repo")?;
        revwalk
            .push(sha1_to_oid(&tip)?)
            .context("revwalk should accept tip oid")?;
        Ok(oid_to_sha1(
            &revwalk
                .last()
                .context("revwalk from tip should be at least contain the tip oid")?
                .context("revwalk iter from branch tip should not result in an error")?,
        ))
    }

    fn does_commit_exist(&self, commit: &str) -> Result<bool> {
        if let Ok(c) = self.git_repo.find_commit(Oid::from_str(commit)?) {
            Ok(true)
        } else {
            Ok(false)
        }
    }

    fn get_head_commit(&self) -> Result<Sha1Hash> {
        let head = self
            .git_repo
            .head()
            .context("failed to get git repo head")?;
        let oid = head.peel_to_commit()?.id();
        Ok(oid_to_sha1(&oid))
    }

    fn get_commit_parent(&self, commit: &Sha1Hash) -> Result<Sha1Hash> {
        let parent_oid = self
            .git_repo
            .find_commit(sha1_to_oid(commit)?)
            .context(format!("could not find commit {commit}"))?
            .parent_id(0)
            .context(format!("could not find parent of commit {commit}"))?;
        Ok(oid_to_sha1(&parent_oid))
    }

    fn make_patch_from_commit(&self, commit: &Sha1Hash) -> Result<String> {
        let c = self
            .git_repo
            .find_commit(Oid::from_bytes(commit.as_byte_array()).context(format!(
                "failed to convert commit_id format for {}",
                &commit
            ))?)
            .context(format!("failed to find commit {}", &commit))?;
        let patch = git2::Email::from_commit(&c, &mut git2::EmailCreateOptions::default())
            .context(format!("failed to create patch from commit {}", &commit))?;

        Ok(std::str::from_utf8(patch.as_slice())
            .context("patch content could not be converted to a utf8 string")?
            .to_owned())
    }

    fn get_commits_ahead_behind(
        &self,
        base_commit: &Sha1Hash,
        latest_commit: &Sha1Hash,
    ) -> Result<(Vec<Sha1Hash>, Vec<Sha1Hash>)> {
        let mut ahead: Vec<Sha1Hash> = vec![];
        let mut behind: Vec<Sha1Hash> = vec![];

        let get_revwalk = |commit: &Sha1Hash| -> Result<Revwalk> {
            let mut revwalk = self
                .git_repo
                .revwalk()
                .context("revwalk should be created from git repo")?;
            revwalk
                .push(sha1_to_oid(commit)?)
                .context("revwalk should accept commit oid")?;
            Ok(revwalk)
        };

        // scan through the base commit ancestory until a common ancestor is found
        let most_recent_shared_commit = match get_revwalk(base_commit)
            .context("failed to get revwalk for base_commit")?
            .find(|base_res| {
                let base_oid = base_res.as_ref().unwrap();

                if get_revwalk(latest_commit)
                    .unwrap()
                    .any(|latest_res| base_oid.eq(latest_res.as_ref().unwrap()))
                {
                    true
                } else {
                    // add commits not found in latest ancestory to 'behind' vector
                    behind.push(oid_to_sha1(base_oid));
                    false
                }
            }) {
            None => {
                bail!(format!(
                    "{} is not an ancestor of {}",
                    latest_commit, base_commit
                ));
            }
            Some(res) => res.context("revwalk failed to reveal commit")?,
        };

        // scan through the latest commits until shared commit is reached
        get_revwalk(latest_commit)
            .context("failed to get revwalk for latest_commit")?
            .any(|latest_res| {
                let latest_oid = latest_res.as_ref().unwrap();
                if latest_oid.eq(&most_recent_shared_commit) {
                    true
                } else {
                    // add commits not found in base to 'ahead' vector
                    ahead.push(oid_to_sha1(latest_oid));
                    false
                }
            });
        Ok((ahead, behind))
    }

    fn checkout(&self, ref_name: &str) -> Result<()> {
        let (object, reference) = self.git_repo.revparse_ext(ref_name)?;

        self.git_repo.checkout_tree(&object, None)?;

        match reference {
            // gref is an actual reference like branches or tags
            Some(gref) => self.git_repo.set_head(gref.name().unwrap()),
            // this is a commit, not a reference
            None => self.git_repo.set_head_detached(object.id()),
        }?;
        Ok(())
    }

    fn create_branch_at_commit(&self, branch_name: &str, commit: &str) -> Result<()> {
        self.git_repo
            .branch(
                branch_name,
                &self.git_repo.find_commit(Oid::from_str(commit)?)?,
                false,
            )
            .context("branch could not be created")?;
        Ok(())
    }
}

fn oid_to_u8_20_bytes(oid: &Oid) -> [u8; 20] {
    let b = oid.as_bytes();
    [
        b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13],
        b[14], b[15], b[16], b[17], b[18], b[19],
    ]
}

// fn oid_to_shorthand_string(oid: Oid) -> Result<String> {
//     let binding = oid.to_string();
//     let b = binding.as_bytes();
//     String::from_utf8(vec![b[0], b[1], b[2], b[3], b[4], b[5], b[6]])
//         .context("oid should always start with 7 u8 btyes of utf8")
// }

// fn oid_to_sha1_string(oid: Oid) -> Result<String> {
//     let b = oid.as_bytes();
//     String::from_utf8(vec![
//         b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10],
// b[11], b[12], b[13],         b[14], b[15], b[16], b[17], b[18], b[19],
//     ])
//     .context("oid should contain 20 u8 btyes of utf8")
// }

// git2 Oid object to Sha1Hash
pub fn oid_to_sha1(oid: &Oid) -> Sha1Hash {
    Sha1Hash::from_byte_array(oid_to_u8_20_bytes(oid))
}

/// `Sha1Hash` to git2 `Oid` object
fn sha1_to_oid(hash: &Sha1Hash) -> Result<Oid> {
    Oid::from_bytes(hash.as_byte_array()).context("Sha1Hash bytes failed to produce a valid Oid")
}

#[cfg(test)]
mod tests {
    use test_utils::git::GitTestRepo;

    use super::*;

    #[test]
    fn get_commit_parent() -> Result<()> {
        let test_repo = GitTestRepo::default();
        let parent_oid = test_repo.populate()?;
        std::fs::write(test_repo.dir.join("t100.md"), "some content")?;
        let child_oid = test_repo.stage_and_commit("add t100.md")?;

        let git_repo = Repo::from_path(&test_repo.dir)?;

        assert_eq!(
            // Sha1Hash::from_byte_array("bla".to_string().as_bytes()),
            oid_to_sha1(&parent_oid),
            git_repo.get_commit_parent(&oid_to_sha1(&child_oid))?,
        );
        Ok(())
    }

    mod does_commit_exist {
        use super::*;

        #[test]
        fn existing_commits_results_in_true() -> Result<()> {
            let test_repo = GitTestRepo::default();
            let oid = test_repo.populate()?;
            let git_repo = Repo::from_path(&test_repo.dir)?;

            assert!(git_repo.does_commit_exist(&"431b84edc0d2fa118d63faa3c2db9c73d630a5ae")?);
            Ok(())
        }

        #[test]
        fn correctly_formatted_hash_that_doesnt_correspond_to_an_existing_commit_results_in_false()
        -> Result<()> {
            let test_repo = GitTestRepo::default();
            let oid = test_repo.populate()?;
            let git_repo = Repo::from_path(&test_repo.dir)?;

            assert!(!git_repo.does_commit_exist(&"000004edc0d2fa118d63faa3c2db9c73d630a5ae")?);
            Ok(())
        }

        #[test]
        fn incorrectly_formatted_hash_that_doesnt_correspond_to_an_existing_commit_results_in_error()
        -> Result<()> {
            let test_repo = GitTestRepo::default();
            let oid = test_repo.populate()?;
            let git_repo = Repo::from_path(&test_repo.dir)?;

            assert!(!git_repo.does_commit_exist(&"00").is_err());
            Ok(())
        }
    }

    mod make_patch_from_commit {
        use super::*;
        #[test]
        fn simple_patch_matches_string() -> Result<()> {
            let test_repo = GitTestRepo::default();
            let oid = test_repo.populate()?;

            let git_repo = Repo::from_path(&test_repo.dir)?;

            assert_eq!(
                "\
                From 431b84edc0d2fa118d63faa3c2db9c73d630a5ae Mon Sep 17 00:00:00 2001\n\
                From: Joe Bloggs <joe.bloggs@pm.me>\n\
                Date: Thu, 1 Jan 1970 00:00:00 +0000\n\
                Subject: [PATCH] add t2.md\n\
                \n\
                ---\n \
                t2.md | 1 +\n \
                1 file changed, 1 insertion(+)\n \
                create mode 100644 t2.md\n\
                \n\
                diff --git a/t2.md b/t2.md\n\
                new file mode 100644\n\
                index 0000000..a66525d\n\
                --- /dev/null\n\
                +++ b/t2.md\n\
                @@ -0,0 +1 @@\n\
                +some content1\n\\ \
                No newline at end of file\n\
                --\n\
                libgit2 1.7.1\n\
                \n\
                ",
                git_repo.make_patch_from_commit(&oid_to_sha1(&oid))?,
            );
            Ok(())
        }
    }

    mod get_main_or_master_branch {

        use super::*;
        mod returns_main {
            use super::*;
            #[test]
            fn when_it_exists() -> Result<()> {
                let test_repo = GitTestRepo::new("main")?;
                let main_oid = test_repo.populate()?;
                let git_repo = Repo::from_path(&test_repo.dir)?;
                let (name, commit_hash) = git_repo.get_main_or_master_branch()?;
                assert_eq!(name, "main");
                assert_eq!(commit_hash, oid_to_sha1(&main_oid));
                Ok(())
            }

            #[test]
            fn when_it_exists_and_other_branch_checkedout() -> Result<()> {
                let test_repo = GitTestRepo::new("main")?;
                let main_oid = test_repo.populate()?;
                test_repo.create_branch("feature")?;
                test_repo.checkout("feature")?;
                std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
                let feature_oid = test_repo.stage_and_commit("add t3.md")?;

                let git_repo = Repo::from_path(&test_repo.dir)?;
                let (name, commit_hash) = git_repo.get_main_or_master_branch()?;
                assert_eq!(name, "main");
                assert_eq!(commit_hash, oid_to_sha1(&main_oid));
                assert_ne!(commit_hash, oid_to_sha1(&feature_oid));
                Ok(())
            }

            #[test]
            fn when_exists_even_if_master_is_checkedout() -> Result<()> {
                let test_repo = GitTestRepo::new("main")?;
                let main_oid = test_repo.populate()?;
                test_repo.create_branch("master")?;
                test_repo.checkout("master")?;
                std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
                let master_oid = test_repo.stage_and_commit("add t3.md")?;

                let git_repo = Repo::from_path(&test_repo.dir)?;
                let (name, commit_hash) = git_repo.get_main_or_master_branch()?;
                assert_eq!(name, "main");
                assert_eq!(commit_hash, oid_to_sha1(&main_oid));
                assert_ne!(commit_hash, oid_to_sha1(&master_oid));
                Ok(())
            }
        }

        #[test]
        fn returns_master_if_exists_and_main_doesnt() -> Result<()> {
            let test_repo = GitTestRepo::new("master")?;
            let master_oid = test_repo.populate()?;
            test_repo.create_branch("feature")?;
            test_repo.checkout("feature")?;
            std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
            let feature_oid = test_repo.stage_and_commit("add t3.md")?;

            let git_repo = Repo::from_path(&test_repo.dir)?;
            let (name, commit_hash) = git_repo.get_main_or_master_branch()?;
            assert_eq!(name, "master");
            assert_eq!(commit_hash, oid_to_sha1(&master_oid));
            assert_ne!(commit_hash, oid_to_sha1(&feature_oid));
            Ok(())
        }
        #[test]
        fn returns_error_if_no_main_or_master() -> Result<()> {
            let test_repo = GitTestRepo::new("feature")?;
            test_repo.populate()?;
            let git_repo = Repo::from_path(&test_repo.dir)?;
            assert!(git_repo.get_main_or_master_branch().is_err());
            Ok(())
        }
    }

    mod get_checked_out_branch_name {
        use super::*;

        #[test]
        fn returns_checked_out_branch_name() -> Result<()> {
            let test_repo = GitTestRepo::default();
            let _ = test_repo.populate()?;
            // create feature branch
            test_repo.create_branch("example-feature")?;
            test_repo.checkout("example-feature")?;

            let git_repo = Repo::from_path(&test_repo.dir)?;

            assert_eq!(
                git_repo.get_checked_out_branch_name()?,
                "example-feature".to_string()
            );
            Ok(())
        }
    }

    mod get_commits_ahead_behind {
        use super::*;
        mod returns_main {
            use super::*;

            #[test]
            fn when_on_same_commit_return_empty() -> Result<()> {
                let test_repo = GitTestRepo::default();
                let oid = test_repo.populate()?;
                // create feature branch
                test_repo.create_branch("feature")?;
                test_repo.checkout("feature")?;

                let git_repo = Repo::from_path(&test_repo.dir)?;

                let (ahead, behind) =
                    git_repo.get_commits_ahead_behind(&oid_to_sha1(&oid), &oid_to_sha1(&oid))?;
                assert_eq!(ahead, vec![]);
                assert_eq!(behind, vec![]);
                Ok(())
            }

            #[test]
            fn when_2_commit_behind() -> Result<()> {
                let test_repo = GitTestRepo::default();
                test_repo.populate()?;
                // create feature branch
                test_repo.create_branch("feature")?;
                let feature_oid = test_repo.checkout("feature")?;
                // checkout main and add 2 commits
                test_repo.checkout("main")?;
                std::fs::write(test_repo.dir.join("t5.md"), "some content")?;
                let behind_1_oid = test_repo.stage_and_commit("add t5.md")?;
                std::fs::write(test_repo.dir.join("t6.md"), "some content")?;
                let behind_2_oid = test_repo.stage_and_commit("add t6.md")?;

                let git_repo = Repo::from_path(&test_repo.dir)?;

                let (ahead, behind) = git_repo.get_commits_ahead_behind(
                    &oid_to_sha1(&behind_2_oid),
                    &oid_to_sha1(&feature_oid),
                )?;
                assert_eq!(ahead, vec![]);
                assert_eq!(
                    behind,
                    vec![oid_to_sha1(&behind_2_oid), oid_to_sha1(&behind_1_oid),],
                );
                Ok(())
            }

            #[test]
            fn when_2_commit_ahead() -> Result<()> {
                let test_repo = GitTestRepo::default();
                let main_oid = test_repo.populate()?;
                // create feature branch and add 2 commits
                test_repo.create_branch("feature")?;
                test_repo.checkout("feature")?;
                std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
                let ahead_1_oid = test_repo.stage_and_commit("add t3.md")?;
                std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
                let ahead_2_oid = test_repo.stage_and_commit("add t4.md")?;

                let git_repo = Repo::from_path(&test_repo.dir)?;

                let (ahead, behind) = git_repo.get_commits_ahead_behind(
                    &oid_to_sha1(&main_oid),
                    &oid_to_sha1(&ahead_2_oid),
                )?;
                assert_eq!(
                    ahead,
                    vec![oid_to_sha1(&ahead_2_oid), oid_to_sha1(&ahead_1_oid),],
                );
                assert_eq!(behind, vec![]);
                Ok(())
            }

            #[test]
            fn when_2_commit_ahead_and_2_commits_behind() -> Result<()> {
                let test_repo = GitTestRepo::default();
                test_repo.populate()?;
                // create feature branch and add 2 commits
                test_repo.create_branch("feature")?;
                test_repo.checkout("feature")?;
                std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
                let ahead_1_oid = test_repo.stage_and_commit("add t3.md")?;
                std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
                let ahead_2_oid = test_repo.stage_and_commit("add t4.md")?;
                // checkout main and add 2 commits
                test_repo.checkout("main")?;
                std::fs::write(test_repo.dir.join("t5.md"), "some content")?;
                let behind_1_oid = test_repo.stage_and_commit("add t5.md")?;
                std::fs::write(test_repo.dir.join("t6.md"), "some content")?;
                let behind_2_oid = test_repo.stage_and_commit("add t6.md")?;

                let git_repo = Repo::from_path(&test_repo.dir)?;

                let (ahead, behind) = git_repo.get_commits_ahead_behind(
                    &oid_to_sha1(&behind_2_oid),
                    &oid_to_sha1(&ahead_2_oid),
                )?;
                assert_eq!(
                    ahead,
                    vec![oid_to_sha1(&ahead_2_oid), oid_to_sha1(&ahead_1_oid)],
                );
                assert_eq!(
                    behind,
                    vec![oid_to_sha1(&behind_2_oid), oid_to_sha1(&behind_1_oid)],
                );
                Ok(())
            }
        }
    }

    mod create_branch_at_commit {
        use super::*;
        #[test]
        fn doesnt_error() -> Result<()> {
            let test_repo = GitTestRepo::default();
            test_repo.populate()?;
            // create feature branch and add 2 commits
            test_repo.create_branch("feature")?;
            test_repo.checkout("feature")?;
            std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
            let ahead_1_oid = test_repo.stage_and_commit("add t3.md")?;
            std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
            test_repo.stage_and_commit("add t4.md")?;

            let git_repo = Repo::from_path(&test_repo.dir)?;

            let branch_name = "test-name-1";
            git_repo.create_branch_at_commit(branch_name, &ahead_1_oid.to_string())?;

            Ok(())
        }

        #[test]
        fn branch_gets_created() -> Result<()> {
            let test_repo = GitTestRepo::default();
            test_repo.populate()?;
            // create feature branch and add 2 commits
            test_repo.create_branch("feature")?;
            test_repo.checkout("feature")?;
            std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
            let ahead_1_oid = test_repo.stage_and_commit("add t3.md")?;
            std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
            test_repo.stage_and_commit("add t4.md")?;

            let git_repo = Repo::from_path(&test_repo.dir)?;

            let branch_name = "test-name-1";
            git_repo.create_branch_at_commit(branch_name, &ahead_1_oid.to_string())?;

            assert!(test_repo.checkout(&branch_name).is_ok());
            Ok(())
        }

        #[test]
        fn branch_created_with_correct_commit() -> Result<()> {
            let test_repo = GitTestRepo::default();
            test_repo.populate()?;
            // create feature branch and add 2 commits
            test_repo.create_branch("feature")?;
            test_repo.checkout("feature")?;
            std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
            let ahead_1_oid = test_repo.stage_and_commit("add t3.md")?;
            std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
            test_repo.stage_and_commit("add t4.md")?;

            let git_repo = Repo::from_path(&test_repo.dir)?;

            let branch_name = "test-name-1";
            git_repo.create_branch_at_commit(branch_name, &ahead_1_oid.to_string())?;

            assert_eq!(test_repo.checkout(&branch_name)?, ahead_1_oid);
            Ok(())
        }
    }
}