aboutsummaryrefslogtreecommitdiffstats
path: root/src/git.rs
diff options
context:
space:
mode:
authorTeddy Wing2021-05-30 17:14:25 +0200
committerTeddy Wing2021-05-30 17:14:25 +0200
commitf8971cea4f8f916bbb528169f06c513c2a18802e (patch)
treede07b47deee291182020dfe5e68fbc75fb9bcac3 /src/git.rs
parent7fc7462bdb6565fa4e49a9f15099f8faed5385b5 (diff)
downloadreflectub-f8971cea4f8f916bbb528169f06c513c2a18802e.tar.bz2
main::update(): Update repository description on fetch update
If the repository was updated, write the description into the `description` file. Add a `github::Repo.description()` method to get an empty string if the description is `None`. This facilitates writing to the `description` file.
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs35
1 files changed, 28 insertions, 7 deletions
diff --git a/src/git.rs b/src/git.rs
index 479a3c5..a96adf5 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -1,5 +1,7 @@
use thiserror;
+use std::fs;
+use std::io::Write;
use std::path::Path;
@@ -7,6 +9,9 @@ use std::path::Path;
pub enum Error {
#[error("git error")]
Git(#[from] git2::Error),
+
+ #[error(transparent)]
+ Io(#[from] std::io::Error),
}
@@ -20,18 +25,13 @@ pub enum Error {
pub fn mirror<P: AsRef<Path>>(
url: &str,
path: P,
- description: Option<&str>,
+ description: &str,
) -> Result<(), Error> {
- let description_str = match description {
- Some(d) => d,
- None => "",
- };
-
let repo = git2::Repository::init_opts(
path,
&git2::RepositoryInitOptions::new()
.bare(true)
- .description(description_str),
+ .description(description),
)?;
let remote_name = "origin";
@@ -82,3 +82,24 @@ pub fn update<P: AsRef<Path>>(
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(())
+}