gittwo 0.0.2

A command-line like wrapper around git2.
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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
use crate::{GitRepository, helpers::channel::ChannelHelper};
use git2::{
    AutotagOption, CertificateCheckStatus, Error, FetchOptions, Remote, RemoteCallbacks,
    build::RepoBuilder,
};

use std::{
    path::{Path, PathBuf},
    time::SystemTime,
};

#[cfg(not(feature = "tokio-channels"))]
use std::sync::mpsc as std_mpsc;

#[cfg(feature = "tokio-channels")]
use tokio::sync::mpsc as tokio_mpsc;

#[derive(Clone)]
/// Specifies details for a `git clone` operation.
pub struct CloneConfig {
    pub(crate) clone_dir_name: String,
    pub(crate) parent_path: PathBuf,
    pub(crate) url: String,
    pub(crate) flags: CloneFlagsInternal,
    pub(crate) sender: Option<ChannelHelper<(usize, String)>>,
}

impl CloneConfig {
    /// Creates a new `CloneConfig` with the repository URL and the parent directory for the clone.
    ///
    /// A new directory, named after the repository (e.g., "gittwo" from
    /// "https://github.com/psomani16k/gittwo.git"), will be created inside `parent_dir`. The repository will be cloned into this new directory.
    pub fn new(url: String, parent_dir: &Path) -> Self {
        let target_dir: String = url;
        let url = target_dir.clone();
        let target_dir = target_dir.split("/").last().unwrap();
        let target_dir = match target_dir.strip_suffix(".git") {
            Some(t) => t,
            None => target_dir,
        };

        CloneConfig {
            clone_dir_name: target_dir.to_string(),
            parent_path: parent_dir.to_path_buf(),
            url,
            flags: CloneFlagsInternal::default(),
            sender: None,
        }
    }

    // getters

    /// Returns the URL of the repository to be cloned.
    pub fn get_url(&self) -> &str {
        &self.url
    }

    #[cfg(not(feature = "tokio-channels"))]
    /// Returns the receiver end of a multi-producer, single-consumer (mpsc) channel.
    /// This channel is used to receive progress updates during the clone operation,
    /// similar to Git CLI output, with an associated index of each line.
    ///
    /// NOTE: messages are send at the sender only if a receiver is initilized (by calling this
    /// function), messages will be sent regardless of whether they are received.
    /// sent regardless
    pub fn get_update_channel(&mut self) -> std_mpsc::Receiver<(usize, String)> {
        let (sender, receiver) = std_mpsc::channel();
        let sender = ChannelHelper::StdChannel(sender);
        self.sender = Some(sender);
        receiver
    }

    #[cfg(feature = "tokio-channels")]
    /// Returns the receiver end of an unbounded multi-producer, single-consumer (mpsc) channel from the tokio
    /// crate.
    /// This channel is used to receive progress updates during the clone operation,
    /// similar to Git CLI output, with an associated index of each line.
    ///
    /// NOTE: messages are send at the sender only if a receiver is initilized (by calling this
    /// function), messages will be sent regardless of whether they are received.
    /// sent regardless
    pub fn get_update_channel(&mut self) -> tokio_mpsc::UnboundedReceiver<(usize, String)> {
        let (sender, receiver) = tokio_mpsc::unbounded_channel();
        let sender = ChannelHelper::TokioChannel(sender);
        self.sender = Some(sender);
        receiver
    }

    /// Returns the parent directory where the repository will be cloned.
    pub fn get_parent_path(&self) -> &Path {
        &self.parent_path
    }

    /// Returns the name of the directory for the cloned repository.
    /// The full path will be `parent_path/clone_dir_name/`.
    pub fn get_clone_dir_name(&self) -> String {
        if self.flags.bare {
            let mut dir = self.clone_dir_name.clone();
            dir += ".git";
            return dir;
        }
        self.clone_dir_name.clone()
    }

    // setters

    /// Sets a custom name for the directory where the repository will be cloned.
    /// This overrides the default name derived from the repository URL.
    pub fn custom_clone_directory(&mut self, dir: impl Into<String>) {
        self.clone_dir_name = dir.into();
    }

    /// Configures a specific flag for the `git clone` operation.
    pub fn add_flag(&mut self, flag: CloneFlags) -> &Self {
        match flag {
            CloneFlags::Branch(branch) => self.flags.branch = branch,
            CloneFlags::Depth(depth) => self.flags.depth = depth,
            CloneFlags::SingleBranch(single) => self.flags.single_branch = single,
            CloneFlags::Bare(bare) => self.flags.bare = bare,
            CloneFlags::Recursive(rec) => self.flags.recursive = rec,
        }
        self
    }
}

#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
/// Internal representation of clone flags, not intended for direct public use.
pub(crate) struct CloneFlagsInternal {
    pub(crate) branch: Option<String>,
    pub(crate) depth: Option<usize>,
    pub(crate) single_branch: bool,
    pub(crate) bare: bool,
    pub(crate) recursive: Option<Vec<String>>,
}

/// Represents flags that can be applied to a `git clone` command.
/// See [git clone documentation](https://git-scm.com/docs/git-clone) for more details on each flag.
pub enum CloneFlags {
    /// Corresponds to the [`--branch`](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt-code--branchcodeemltnamegtem)
    /// or `-b` flag.
    /// `Some(branch_name)` specifies the branch to checkout. `None` uses the remote's default branch.
    ///
    /// Defaults to `None`.
    Branch(Option<String>),

    /// Corresponds to the [`--depth <depth>`](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---depthltdepthgt)
    /// flag.
    /// `Some(n)` creates a shallow clone with a history truncated to `n` commits. `None` implies a full clone.
    ///
    /// Defaults to `None`.
    Depth(Option<usize>),

    /// Corresponds to the [`--single-branch`](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---single-branch)
    /// flag.
    /// `true` clones only the history leading to the tip of a single branch (either the one specified by `--branch` or the remote's default).
    /// `false` clones all branches.
    ///
    /// Defaults to `false`.
    SingleBranch(bool),

    /// Corresponds to the [`--bare`](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---bare)
    /// flag.
    /// `true` creates a bare Git repository (no working directory). `false` creates a standard repository.
    ///
    /// Defaults to `false`.
    Bare(bool),

    /// Corresponds to the [`--recursive`](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---recursive)
    /// or [`--recurse-submodules[=<pathspec>]`](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---recurse-submodulesltpathspecgt) flag.
    /// `Some(pathspecs)` initializes submodules matching the pathspecs. An empty vector initializes all submodules.
    /// `None` does not initialize submodules.
    ///
    /// Defaults to `None`.
    Recursive(Option<Vec<String>>),
}

impl GitRepository {
    /// Clones a Git repository based on the provided `CloneConfig`.
    ///
    /// If GitRepository was created using `GitRepository::new()` this will allow you to clone a
    /// remote repository to the provided directory. If GitRepository was created using
    /// `GitRepository::open()` calling this function will return an error.
    pub fn git_clone(&mut self, config: CloneConfig) -> Result<(), Error> {
        if self.repository.is_some() {
            return Err(git2::Error::from_str(
                "git_clone() called on a pre-existing repository.",
            ));
        }

        let mut remote_update_index = 1;
        let mut transfer_update_index = 100;
        let mut progress_helper = ProgressCallbackHelper::default();
        let mut fetch_options = FetchOptions::new();
        let mut repo_builder = RepoBuilder::new();
        let mut callbacks = RemoteCallbacks::new();
        let mut callbacks2 = RemoteCallbacks::new();
        let mut remote = Remote::create_detached(config.url.clone())?;

        // skip user verification if configured so
        if self.skip_owner_validation {
            unsafe {
                git2::opts::set_verify_owner_validation(false)?;
            };
        }

        // continue even if cert checks fail, if configured so
        if self.bypass_certificate_check {
            callbacks.certificate_check(|_, _| Ok(CertificateCheckStatus::CertificateOk));
        }

        // setting up credentials
        let cred = self.cred.clone();
        let cred2 = self.cred.clone();
        callbacks.credentials(move |_a: &str, _b, _c| cred.get_cred());
        callbacks2.credentials(move |_a: &str, _b, _c| cred2.get_cred());

        let remote = remote.connect_auth(git2::Direction::Fetch, Some(callbacks2), None)?;
        let mut def_branch: Vec<u8> = vec![];
        remote.default_branch()?.clone_into(&mut def_branch);
        let def_branch = String::from_utf8(def_branch);
        let mut def_branch = def_branch.unwrap_or("main".to_string());
        def_branch = def_branch.split("/").last().unwrap_or("main").to_string();

        // getting the name of the repository
        let repo_path = config.get_parent_path().join(config.get_clone_dir_name());

        // +----------------------------+
        // | SETTING UP UPDATES CHANNEL |
        // +----------------------------+

        #[cfg(not(feature = "tokio-channels"))]
        if config.sender.is_some() {
            let sender = config.sender.clone().unwrap();
            let initial_msg = format!("Cloning into '{}'...", config.get_clone_dir_name());
            let _ = sender.send((0, initial_msg));
            callbacks.sideband_progress(move |stats| {
                remote_update_index =
                    ProgressCallbackHelper::update_remote(remote_update_index, stats, &sender);
                true
            });

            let sender = config.sender.clone().unwrap();

            callbacks.transfer_progress(move |stats| {
                if transfer_update_index == 100 {
                    let total_objects = stats.total_objects();
                    let received_objects = stats.received_objects();
                    let received_bytes = stats.received_bytes();
                    transfer_update_index = progress_helper.update_receiving(
                        received_objects,
                        total_objects,
                        received_bytes,
                        &sender,
                        transfer_update_index,
                    );
                } else if transfer_update_index == 101 {
                    let indexed_deltas = stats.indexed_deltas();
                    let total_deltas = stats.total_deltas();
                    transfer_update_index = progress_helper.update_resolving(
                        indexed_deltas,
                        total_deltas,
                        &sender,
                        transfer_update_index,
                    );
                }
                true
            });
        }

        #[cfg(feature = "tokio-channels")]
        if config.sender.is_some() {
            let sender = config.sender.clone().unwrap();
            let initial_msg = format!("Cloning into '{}'...", config.get_clone_dir_name());
            let _ = sender.send((0, initial_msg));
            callbacks.sideband_progress(move |stats| {
                remote_update_index =
                    ProgressCallbackHelper::update_remote(remote_update_index, stats, &sender);
                true
            });

            let sender = config.sender.clone().unwrap();

            callbacks.transfer_progress(move |stats| {
                if transfer_update_index == 100 {
                    let total_objects = stats.total_objects();
                    let received_objects = stats.received_objects();
                    let received_bytes = stats.received_bytes();
                    transfer_update_index = progress_helper.update_receiving(
                        received_objects,
                        total_objects,
                        received_bytes,
                        &sender,
                        transfer_update_index,
                    );
                } else if transfer_update_index == 101 {
                    let indexed_deltas = stats.indexed_deltas();
                    let total_deltas = stats.total_deltas();
                    transfer_update_index = progress_helper.update_resolving(
                        indexed_deltas,
                        total_deltas,
                        &sender,
                        transfer_update_index,
                    );
                }
                true
            });
        }

        // +---------------+
        // | SETTING FLAGS |
        // +---------------+

        // branch
        if let Some(branch) = &config.flags.branch {
            def_branch = branch.to_string();
        }

        // depth
        if let Some(depth) = config.flags.depth {
            let depth: i32 = depth as i32;
            fetch_options.depth(depth);
            fetch_options.download_tags(AutotagOption::None);
            let branch = def_branch.clone();
            repo_builder.remote_create(move |repo, name, url| {
                let refspec = format!("+refs/heads/{0:}:refs/remotes/origin/{0:}", branch);
                repo.remote_with_fetch(name, url, &refspec)
            });
        }

        // single-branch
        if config.flags.single_branch {
            fetch_options.download_tags(AutotagOption::None);
            let branch = def_branch.clone();
            repo_builder.remote_create(move |repo, name, url| {
                let refspec = format!("+refs/heads/{0:}:refs/remotes/origin/{0:}", branch);
                repo.remote_with_fetch(name, url, &refspec)
            });
        }

        // bare
        let repo_builder = repo_builder.bare(config.flags.bare);

        // +--------------+
        // | CLONING REPO |
        // +--------------+

        let repo_builder = repo_builder.branch(&def_branch);

        fetch_options.remote_callbacks(callbacks);

        // setting fetch options and cloning
        repo_builder.fetch_options(fetch_options);
        let repository = repo_builder.clone(config.get_url(), &repo_path)?;

        if let Some(pathspecs) = config.flags.recursive {
            if pathspecs.is_empty() {
                let submodule = repository.submodules()?;
                for mut sub in submodule {
                    // git seems to ignore errors in cloning submodule
                    // TODO: investigate this
                    let _ = sub.clone(None);
                }
            }
            for pathspec in pathspecs {
                if let Ok(mut submodule) = repository.find_submodule(&pathspec) {
                    // git seems to ignore errors in cloning submodule, hence doing the same
                    // here...
                    // TODO: investigate this
                    let _ = submodule.clone(None);
                }
            }
        }
        self.repository = Some(repository);

        Ok(())
    }
}

struct ProgressCallbackHelper {
    last_update_time: SystemTime,
    last_throughput_update_time: SystemTime,
    last_transfered_bytes: usize,
    previous_throughut: u128,
}

impl Default for ProgressCallbackHelper {
    fn default() -> Self {
        Self {
            last_update_time: SystemTime::now(),
            last_throughput_update_time: SystemTime::now(),
            last_transfered_bytes: 0,
            previous_throughut: 0,
        }
    }
}

impl ProgressCallbackHelper {
    fn update_remote(index: usize, msg: &[u8], sender: &ChannelHelper<(usize, String)>) -> usize {
        let mut index = index;
        let msg = String::from_utf8_lossy(msg);
        let msgs = msg.split('\n');
        for msg in msgs {
            let messages = msg.split('\r');
            for message in messages {
                if !message.is_empty() {
                    let message = format!("remote: {}", message);
                    let _ = sender.send((index, message));
                }
                if message.ends_with("done.") {
                    index += 1;
                }
            }
        }
        index
    }

    fn update_resolving(
        &mut self,
        indexed_deltas: usize,
        total_deltas: usize,
        sender: &ChannelHelper<(usize, String)>,
        index: usize,
    ) -> usize {
        let now = SystemTime::now();
        let time_since_last_update = now
            .duration_since(self.last_update_time)
            .unwrap()
            .as_millis();
        if total_deltas == 0 {
            return index;
        }
        if time_since_last_update >= 100 && indexed_deltas < total_deltas {
            self.last_update_time = now;
            let percent = indexed_deltas * 100 / total_deltas;
            let msg = format!("Resolving deltas: {percent}% ({indexed_deltas}/{total_deltas})");
            let _ = sender.send((index, msg));
        } else if indexed_deltas == total_deltas {
            // making sure the last msg is sent regardless of rate limiting.
            self.last_update_time = now;
            let msg = format!("Resolving deltas: 100% ({indexed_deltas}/{total_deltas}), done.");
            let _ = sender.send((index, msg));
            return index + 1;
        }
        index
    }

    fn update_receiving(
        &mut self,
        recieved_obj: usize,
        total_obj: usize,
        recieved_bytes: usize,
        sender: &ChannelHelper<(usize, String)>,
        index: usize,
    ) -> usize {
        // calculating throughput
        let now = SystemTime::now();
        let time_since_last_update = now
            .duration_since(self.last_update_time)
            .unwrap()
            .as_millis();
        let time_since_last_throughput_update = now
            .duration_since(self.last_throughput_update_time)
            .unwrap()
            .as_millis();

        if time_since_last_throughput_update >= 500 {
            let throughput = ((recieved_bytes - self.last_transfered_bytes) * 1_000) as u128
                / time_since_last_throughput_update;
            self.previous_throughut = throughput;
            self.last_throughput_update_time = now;
        }
        if time_since_last_update >= 100 && recieved_obj < total_obj {
            // making stuff pretty
            let (speed_num, speed_unit) = Self::give_speed(self.previous_throughut);
            let (transfer_num, transfer_unit) = Self::give_data_transfer(recieved_bytes);
            let complete_percent = 100 * recieved_obj / total_obj;

            self.last_update_time = now;
            self.last_transfered_bytes = recieved_bytes;

            let msg = format!(
                "Receiving objects: {complete_percent}% ({recieved_obj}/{total_obj}), {:.2} {transfer_unit} | {:.2} {speed_unit}",
                transfer_num, speed_num
            );
            let _ = sender.send((index, msg));
            return index;
        } else if recieved_obj == total_obj {
            // making sure the last msg is sent regardless of rate limiting.
            let (speed_num, speed_unit) = Self::give_speed(self.previous_throughut);
            let (transfer_num, transfer_unit) = Self::give_data_transfer(recieved_bytes);
            let complete_percent = 100 * recieved_obj / total_obj;
            let msg = format!(
                "Receiving objects: {complete_percent}% ({recieved_obj}/{total_obj}), {:.2} {transfer_unit} | {:.2} {speed_unit}, done.",
                transfer_num, speed_num
            );
            let _ = sender.send((index, msg));
            return index + 1;
        }
        index
    }

    fn give_speed(bytes_per_sec: u128) -> (f32, String) {
        // GiB/s
        if bytes_per_sec > 1_073_741_824 {
            let bytes_per_sec: f32 = bytes_per_sec as f32;
            let speed: f32 = bytes_per_sec / 1_073_741_824.0;
            return (speed, "GiB/s".to_string());
        // MiB/s
        } else if bytes_per_sec > 1_048_576 {
            let bytes_per_sec: f32 = bytes_per_sec as f32;
            let speed: f32 = bytes_per_sec / 1_048_576.0;
            return (speed, "MiB/s".to_string());
        // KiB/s
        } else if bytes_per_sec > 1_024 {
            let bytes_per_sec: f32 = bytes_per_sec as f32;
            let speed: f32 = bytes_per_sec / 1_024.0;
            return (speed, "KiB/s".to_string());
        }
        (bytes_per_sec as f32, "B/s".to_string())
    }

    fn give_data_transfer(bytes_transfered: usize) -> (f32, String) {
        // gib/s
        if bytes_transfered > 1_073_741_824 {
            let bytes_transfered: f32 = bytes_transfered as f32;
            let data: f32 = bytes_transfered / 1_073_741_824.0;
            return (data, "Gib".to_string());
        // mib/s
        } else if bytes_transfered > 1_048_576 {
            let bytes_transfered: f32 = bytes_transfered as f32;
            let data: f32 = bytes_transfered / 1_048_576.0;
            return (data, "Mib".to_string());
        // kib/s
        } else if bytes_transfered > 1_024 {
            let bytes_transfered: f32 = bytes_transfered as f32;
            let data: f32 = bytes_transfered / 1_024.0;
            return (data, "Kib".to_string());
        }
        (bytes_transfered as f32, "B".to_string())
    }
}

#[cfg(test)]
mod clone_test {
    use super::{CloneConfig, CloneFlags};
    use crate::GitRepository;
    use std::{io::BufRead, path::Path, process::Command};

    #[test]
    fn git_clone_depth_test() {
        // create temp directories
        Command::new("mkdir")
            .args(["-p", "./temp_test/clone_depth"])
            .output()
            .unwrap();

        // clone git2 using gittwo
        let mut repo = GitRepository::new();
        let mut config = CloneConfig::new(
            "https://github.com/rust-lang/git2-rs.git".to_string(),
            Path::new("./temp_test/clone_depth"),
        );
        config.add_flag(CloneFlags::Depth(Some(1)));
        repo.git_clone(config).unwrap();

        // verify that a single commit is cloned in
        let out = Command::new("git")
            .args([
                "-C",
                "./temp_test/clone_depth/git2-rs/",
                "rev-list",
                "--count",
                "--all",
            ])
            .output()
            .unwrap();

        Command::new("rm")
            .args(["-rf", "./temp_test/clone_depth/"])
            .output()
            .unwrap();

        assert_eq!(String::from_utf8_lossy(&out.stdout), "1\n");
    }

    #[test]
    fn git_clone_bare_test() {
        // create temp directories
        Command::new("mkdir")
            .args(["-p", "./temp_test/clone_bare"])
            .output()
            .unwrap();

        // clone git2 using gittwo
        let mut repo = GitRepository::new();
        let mut config = CloneConfig::new(
            "https://github.com/rust-lang/git2-rs.git".to_string(),
            Path::new("./temp_test/clone_bare"),
        );
        config.add_flag(CloneFlags::Bare(true));
        repo.git_clone(config).unwrap();

        // verify that repository is bare
        let out = Command::new("git")
            .args([
                "-C",
                "./temp_test/clone_bare/git2-rs.git/",
                "rev-parse",
                "--is-bare-repository",
            ])
            .output()
            .unwrap();

        // delete the repository
        Command::new("rm")
            .args(["-rf", "./temp_test/clone_bare/"])
            .output()
            .unwrap();

        assert_eq!(String::from_utf8_lossy(&out.stdout), "true\n");
    }

    #[test]
    fn git_clone_branch_test() {
        // create temp directories
        Command::new("mkdir")
            .args(["-p", "./temp_test/clone_branch"])
            .output()
            .unwrap();

        // clone git2 using gittwo
        let mut repo = GitRepository::new();
        let mut config = CloneConfig::new(
            "https://github.com/rust-lang/git2-rs.git".to_string(),
            Path::new("./temp_test/clone_branch/"),
        );
        config.add_flag(CloneFlags::Branch(Some(String::from("curl"))));
        repo.git_clone(config).unwrap();

        // verify that a single commit is cloned in
        let out = Command::new("git")
            .args(["-C", "./temp_test/clone_branch/git2-rs/", "branch"])
            .output()
            .unwrap();

        Command::new("rm")
            .args(["-rf", "./temp_test/clone_branch/"])
            .output()
            .unwrap();

        assert_eq!(String::from_utf8_lossy(&out.stdout), "* curl\n");
    }

    #[test]
    fn git_clone_single_branch_test() {
        // create temp directories
        Command::new("mkdir")
            .args(["-p", "./temp_test/clone_single_branch"])
            .output()
            .unwrap();

        // clone git2 using gittwo
        let mut repo = GitRepository::new();
        let mut config = CloneConfig::new(
            "https://github.com/rust-lang/git2-rs.git".to_string(),
            Path::new("./temp_test/clone_single_branch/"),
        );
        config.add_flag(CloneFlags::SingleBranch(true));
        repo.git_clone(config).unwrap();

        // verify that a single commit is cloned in
        let out = Command::new("git")
            .args([
                "-C",
                "./temp_test/clone_single_branch/git2-rs/",
                "branch",
                "--remotes",
            ])
            .output()
            .unwrap();

        let out = out.stdout.lines().count();
        Command::new("rm")
            .args(["-rf", "./temp_test/clone_single_branch/"])
            .output()
            .unwrap();

        assert_eq!(out, 2);
    }
}