aboutsummaryrefslogtreecommitdiffstats
path: root/src/git.rs
blob: 753137c1022eca301d7815e553760313e71b0f16 (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
// Copyright (c) 2021  Teddy Wing
//
// This file is part of Reflectub.
//
// Reflectub is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Reflectub is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Reflectub. If not, see <https://www.gnu.org/licenses/>.


use thiserror;

use std::fs;
use std::io::Write;
use std::path::Path;


#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("git error")]
    Git(#[from] git2::Error),

    #[error(transparent)]
    Io(#[from] std::io::Error),
}


/// Mirror a repository.
///
/// Works like:
///
/// ```shell
/// git clone --mirror URL
/// ```
pub fn mirror<P: AsRef<Path>>(
    url: &str,
    path: P,
    description: &str,
    default_branch: &str,
) -> Result<(), Error> {
    let repo = git2::Repository::init_opts(
        path,
        &git2::RepositoryInitOptions::new()
            .bare(true)

            // On Linux, using the external template prevents the custom
            // description from being added. It doesn't make a difference on
            // Mac OS.
            .external_template(false)
            .description(description),
    )?;

    let remote_name = "origin";

    let mut remote = repo.remote_with_fetch(
        remote_name,
        url,
        "+refs/*:refs/*",
    )?;

    let mut config = repo.config()?;
    config.set_bool(
        &format!("remote.{}.mirror", remote_name),
        true,
    )?;

    let refspecs: [&str; 0] = [];
    remote.fetch(&refspecs, None, None)?;

    if default_branch != "master" {
        repo.set_head(
            &format!("refs/heads/{}", default_branch),
        )?;
    }

    Ok(())
}

/// Update remotes.
///
/// Works like:
///
/// ```shell
/// git remote update
/// ```
pub fn update<P: AsRef<Path>>(
    path: P,
) -> Result<(), Error> {
    let repo = git2::Repository::open_bare(path)?;

    for remote_opt in &repo.remotes()? {
        if let Some(remote_name) = remote_opt {
            let mut remote = repo.find_remote(remote_name)?;

            let mut fetch_options = git2::FetchOptions::new();
            fetch_options
                .prune(git2::FetchPrune::On)
                .download_tags(git2::AutotagOption::All);

            let refspecs: [&str; 0] = [];
            remote.fetch(&refspecs, Some(&mut fetch_options), None)?;
        }
    }

    Ok(())
}

/// Update the repository's description file.
pub fn update_description<P: AsRef<Path>>(
    repo_path: P,
    description: &str,
) -> Result<(), Error> {
    let description_path = repo_path.as_ref().join("description");

    let mut file = fs::OpenOptions::new()
        .write(true)
        .truncate(true)
        .open(description_path)?;

    if description.is_empty() {
        file.set_len(0)?;
    } else {
        writeln!(file, "{}", description)?;
    }

    Ok(())
}