aboutsummaryrefslogtreecommitdiff
path: root/src/sync/mod.rs
blob: 1d2c2cabca7ae05af265985897b326f78e4e245f (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
use std::fmt::{Debug, Display};

use crate::{
    batch::batch,
    repo::{LocalRepoState, Repo, RepoError, Repos},
};

impl crate::GTree {
    pub fn sync(&self, repos: Repos) {
        batch(repos, |mut repo| {
            match repo.sync() {
                Ok(u) => println!("{}", u),
                Err(u) => println!("{}", u),
            };
        });
    }
}

impl Repo {
    /// Clone repos from forge and push new repos to forge
    #[tracing::instrument(level = "trace")]
    pub fn sync(&mut self) -> Result<SyncResult, SyncResult> {
        let repo_name = self.name.clone();

        // let repo_state = self
        //     .is_clean()
        //     .map_err(|err| SyncResult::err(repo_name.clone(), err))?;
        // if self.repo.is_some() && repo_state != LocalRepoState::Clean {
        //     return Ok(SyncResult::dirty(repo_name, repo_state));
        // };

        if self.repo.is_some() && self.forge.is_some() {
            Ok(SyncResult::no_changes(repo_name))
        } else if self.repo.is_some() {
            // TODO do push stuff
            Ok(SyncResult::pushed(repo_name))
        } else if self.forge.is_some() {
            let url = self
                .forge
                .as_ref()
                .unwrap()
                .ssh_clone_url
                .as_ref()
                .ok_or_else(|| SyncResult::err(self.name.clone(), RepoError::NoRemoteFound))?;

            self.clone(url)
                .map_err(|err| SyncResult::err(repo_name.clone(), err))?;

            // TODO detect moved repos based on first commit
            // ???? how to detect and not move forks?

            Ok(SyncResult::cloned(repo_name))
        } else {
            Ok(SyncResult::no_changes(repo_name))
        }
    }
}

#[derive(Debug)]
pub enum SyncResult {
    NoChanges { name: String },
    Dirty { name: String, state: LocalRepoState },
    Cloned { name: String },
    Pushed { name: String },
    Error { name: String, error: RepoError },
}

impl SyncResult {
    pub fn err(name: String, error: RepoError) -> SyncResult {
        SyncResult::Error { name, error }
    }

    pub fn cloned(name: String) -> SyncResult {
        SyncResult::Cloned { name }
    }

    pub fn pushed(name: String) -> SyncResult {
        SyncResult::Pushed { name }
    }

    pub fn dirty(name: String, state: LocalRepoState) -> SyncResult {
        SyncResult::Dirty { name, state }
    }

    pub fn no_changes(name: String) -> SyncResult {
        SyncResult::NoChanges { name }
    }
}

impl Display for SyncResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use ansi_term::Colour::{Blue, Green, Red, Yellow};

        match self {
            SyncResult::NoChanges { name } => {
                f.write_fmt(format_args!("{} {}", Blue.paint("NOCHANGE"), name))
            }
            SyncResult::Dirty { name, state } => f.write_fmt(format_args!(
                "{} {} [{}]",
                Yellow.paint("DIRTY   "),
                name,
                state
            )),
            SyncResult::Cloned { name } => {
                f.write_fmt(format_args!("{} {}", Green.paint("CLONED  "), name))
            }
            SyncResult::Pushed { name } => {
                f.write_fmt(format_args!("{} {}", Green.paint("PUSHED  "), name))
            }
            SyncResult::Error { name, error } => f.write_fmt(format_args!(
                "{} {} [{}]",
                Red.paint("ERROR  "),
                name,
                error
            )),
        }
    }
}