aboutsummaryrefslogtreecommitdiff
path: root/src/repo/mod.rs
blob: 4eb904325d23eff34ba4a1d00f39f3281d55acd4 (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
use std::{collections::HashMap, fmt::Debug, path::PathBuf, sync::RwLock};

use anyhow::Context;
use thiserror::Error;

use gix::{
    bstr::BString,
    clone::checkout::main_worktree::ProgressId,
    refs::{
        transaction::{LogChange, PreviousValue, RefEdit},
        FullName,
    },
    remote, Id, ObjectId, Progress, Remote, Repository,
};
use tracing::{debug, error};

use crate::forge::Project;

mod aggregate;
mod repostate;

pub use aggregate::*;
pub use repostate::*;

// pub type Repos = Vec<Repo>;
pub type Repos = HashMap<String, RwLock<Repo>>;

pub struct Repo {
    pub name: String,
    pub path: PathBuf,
    pub repo: Option<Repository>,
    pub forge: Option<Project>,
    pub default_branch: String,
}

impl Repo {
    pub fn repo(&self) -> Result<&Repository, RepoError> {
        match &self.repo {
            Some(repo) => Ok(repo),
            None => Err(RepoError::NoLocalRepo),
        }
    }

    pub fn repo_mut(&mut self) -> Result<&mut Repository, RepoError> {
        match &mut self.repo {
            Some(repo) => Ok(repo),
            None => Err(RepoError::NoLocalRepo),
        }
    }

    #[tracing::instrument(level = "debug")]
    pub fn is_clean(&self) -> Result<LocalRepoState, RepoError> {
        let repo = self.repo()?;

        if let Some(state) = repo.state() {
            Ok(LocalRepoState::InProgress(state))
        } else {
            let head = repo.head().unwrap();

            if head.is_detached() {
                return Ok(LocalRepoState::DetachedHead);
            }

            if head.is_unborn() {
                return Ok(LocalRepoState::UnbornHead);
            }

            Ok(LocalRepoState::Clean)
        }
    }

    pub fn default_remote(&self) -> Result<Remote, RepoError> {
        Ok(self
            .repo()?
            .find_default_remote(gix::remote::Direction::Fetch)
            .ok_or(RepoError::NoRemoteFound)?
            .context("fetch: failed to find default remote")?)
    }

    pub fn default_branch(&self) -> Result<BString, RepoError> {
        let repo = self.repo()?;
        let remote = self.default_remote()?;
        let remote_name = remote.name().context("remote does not have name")?;

        let origin_ref = repo
            .find_reference(&format!("remotes/{}/HEAD", remote_name.as_bstr()))
            .context("the remotes HEAD references does not exist")?;

        if let Some(origin_ref) = origin_ref.target().try_name() {
            Ok(origin_ref.shorten().to_owned())
        } else {
            Err(RepoError::NoDefaultBranch)
        }
    }

    #[tracing::instrument(level = "trace")]
    pub fn clone(&self, url: &str) -> Result<(), RepoError> {
        std::fs::create_dir_all(&self.path).unwrap();

        let mut prepare_clone = gix::prepare_clone(url, &self.path).unwrap();

        let (mut prepare_checkout, _) = prepare_clone
            .fetch_then_checkout(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
            .unwrap();

        let (_repo, _) = prepare_checkout
            .main_worktree(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
            .unwrap();

        Ok(())
    }

    #[tracing::instrument(level = "trace")]
    pub fn fetch<'a>(&mut self) -> Result<bool, RepoError> {
        let remote = self.default_remote()?;
        let conn = remote.connect(gix::remote::Direction::Fetch).unwrap();
        let outcome = conn
            .prepare_fetch(
                gix::progress::Discard,
                gix::remote::ref_map::Options::default(),
            )
            .context("fetch: failed to prepare patch")?
            .receive(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
            .context("fetch: failed to receive")?;

        match outcome.status {
            gix::remote::fetch::Status::NoPackReceived {
                dry_run: _,
                negotiate: _,
                update_refs: _,
            } => Ok(false),
            gix::remote::fetch::Status::Change {
                negotiate: _,
                write_pack_bundle: _,
                update_refs: _,
            } => Ok(true),
        }
    }

    pub fn refedit(target: ObjectId, name: &str, message: &str) -> RefEdit {
        RefEdit {
            change: gix::refs::transaction::Change::Update {
                log: LogChange {
                    mode: gix::refs::transaction::RefLog::AndReference,
                    force_create_reflog: false,
                    message: message.into(),
                },
                expected: PreviousValue::Any,
                new: gix::refs::Target::Peeled(target),
            },
            name: FullName::try_from(name).unwrap(),
            deref: true,
        }
    }

    pub fn update_default_branch_ref(
        &self,
        remote: remote::Name,
        head: Id,
    ) -> Result<(), RepoError> {
        let default_branch = self.default_branch()?;
        let repo = self.repo()?;

        repo.edit_reference(Repo::refedit(
            head.into(),
            &format!("heads/{}", default_branch),
            &format!("checkout: {}/HEAD with gtree", remote.as_bstr()),
        ))
        .context("checkout: failed to edit ref")?;

        Ok(())
    }

    pub fn default_remote_head(&self) -> Result<(remote::Name, Id), RepoError> {
        let repo = self.repo()?;

        let remote = repo
            .find_fetch_remote(None)
            .context("could not find remote to fetch")?;
        let remote = remote.name().context("remote does not have name")?;

        let head_ref = repo
            .find_reference(&format!("remotes/{}/HEAD", remote.as_bstr()))
            .context("the remotes HEAD references does not exist")?;
        let head = head_ref
            .into_fully_peeled_id()
            .context("failed to peel ref")?;

        Ok((remote.to_owned(), head.to_owned()))
    }

    #[tracing::instrument(level = "trace", skip(progress))]
    pub fn checkout(
        &self,
        remote: remote::Name,
        head: Id,
        progress: &mut dyn gix::progress::DynNestedProgress,
    ) -> Result<(), RepoError> {
        let repo = self.repo()?;

        let workdir = repo.work_dir().ok_or(RepoError::NoWorktree)?;
        let head_tree = head
            .object()
            .context("could not find object HEAD points to")?
            .peel_to_tree()
            .context("failed to peel HEAD object")?
            .id();

        let index =
            gix_index::State::from_tree(&head_tree, &repo.objects).context("index from tree")?;
        let mut index = gix_index::File::from_state(index, repo.index_path());

        let mut files =
            progress.add_child_with_id("checkout".to_string(), ProgressId::CheckoutFiles.into());
        let mut bytes =
            progress.add_child_with_id("writing".to_string(), ProgressId::BytesWritten.into());

        files.init(Some(index.entries().len()), gix::progress::count("files"));
        bytes.init(None, gix::progress::bytes());

        let start = std::time::Instant::now();

        debug!("workdir: {:?}", workdir);
        let opts = gix_worktree_state::checkout::Options::default();
        let outcome = gix_worktree_state::checkout(
            &mut index,
            workdir,
            repo.objects.clone().into_arc().unwrap(),
            &files,
            &bytes,
            &gix::interrupt::IS_INTERRUPTED,
            opts,
        )
        .context("checkout: failed");

        files.show_throughput(start);
        bytes.show_throughput(start);

        debug!("outcome: {:?}", outcome);
        debug!("is interrupted: {:?}", &gix::interrupt::IS_INTERRUPTED);

        index
            .write(Default::default())
            .context("checkout: write index")?;

        Ok(())
    }
}

#[derive(Error, Debug)]
pub enum RepoError {
    #[error("repo is not cloned locally")]
    NoLocalRepo,
    #[error("local git repo does not have a remote")]
    NoRemoteFound,
    #[error("could not determine default branch based on remote HEAD")]
    NoDefaultBranch,
    #[error("repo is not checked out")]
    NoWorktree,
    #[error("repository is dirty: {0}")]
    Dirty(LocalRepoState),
    #[error("fast-forward merge was not possible")]
    NoFF,
    #[error("error: {0}")]
    Anyhow(#[from] anyhow::Error),
    #[error("unknown repo error")]
    Unknown,
}

#[derive(Error, Debug, PartialEq)]
pub enum LocalRepoState {
    #[error("operation in progress: {0:?}")]
    InProgress(gix::state::InProgress),
    #[error("head is detached")]
    DetachedHead,
    #[error("head is unborn")]
    UnbornHead,
    #[error("repo is clean")]
    Clean,
}

impl Ord for Repo {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.name.cmp(&other.name)
    }
}

impl Eq for Repo {}

impl PartialOrd for Repo {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.name.partial_cmp(&other.name)
    }
}

impl PartialEq for Repo {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl From<Project> for Repo {
    fn from(project: Project) -> Self {
        Self {
            name: project.path.clone(),
            forge: Some(project),
            ..Repo::default()
        }
    }
}

impl From<&Project> for Repo {
    fn from(project: &Project) -> Self {
        Self {
            name: project.path.clone(),
            forge: Some(project.to_owned()),
            ..Repo::default()
        }
    }
}

impl std::fmt::Display for Repo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{} {}", RepoState::from(self), self.name))
    }
}

impl Debug for Repo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Repo").field("path", &self.name).finish()
    }
}

impl Default for Repo {
    fn default() -> Self {
        Self {
            name: Default::default(),
            path: Default::default(),
            repo: Default::default(),
            forge: Default::default(),
            default_branch: "main".to_string(),
        }
    }
}