feat!: add repo flags

Flags indicate what operations should be masked on a repo.

This is done in a way that is relatively more pleasurable to configure,
and should reduce the amount of breaking changes long term.

Overally, a very nice commit :D

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2023-06-22 15:54:45 +02:00
parent 720b9b0de1
commit 56244f9d4f
Signed by: cafkafk
GPG key ID: CDDC792F655251ED
6 changed files with 41 additions and 35 deletions

2
Cargo.lock generated
View file

@ -190,7 +190,7 @@ dependencies = [
[[package]] [[package]]
name = "gg" name = "gg"
version = "0.0.4" version = "0.0.5"
dependencies = [ dependencies = [
"clap", "clap",
"clap_mangen", "clap_mangen",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "gg" name = "gg"
version = "0.0.4" version = "0.0.5"
edition = "2021" edition = "2021"
authors = ["Christina Sørensen <christina@cafkafk.com>"] authors = ["Christina Sørensen <christina@cafkafk.com>"]
repository = "https://github.com/cafkafk/gg" repository = "https://github.com/cafkafk/gg"

View file

@ -21,6 +21,15 @@ use std::os::unix::fs::symlink;
use std::path::Path; use std::path::Path;
use std::{fs, process::Command}; use std::{fs, process::Command};
// why not make it O(log n) instead of a vec that's /only/ O(n)
// ...because premature optimization is the root of all evil!
#[derive(PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum RepoFlags {
Push,
Clone,
// Pull, FIXME: could be interesting to implement
}
/// Represents the config.toml file. /// Represents the config.toml file.
#[derive(PartialEq, Debug, Serialize, Deserialize)] #[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Config { pub struct Config {
@ -42,8 +51,7 @@ pub struct GitRepo {
pub name: String, pub name: String,
pub path: String, pub path: String,
pub url: String, pub url: String,
pub clone: bool, pub flags: Vec<RepoFlags>,
pub push: bool,
} }
fn handle_file_exists(selff: &Links, tx_path: &Path, rx_path: &Path) { fn handle_file_exists(selff: &Links, tx_path: &Path, rx_path: &Path) {
@ -92,7 +100,7 @@ impl Links {
impl GitRepo { impl GitRepo {
/// Clones the repository to its specified folder. /// Clones the repository to its specified folder.
fn clone(&self) { fn clone(&self) {
if self.clone { if self.flags.contains(&RepoFlags::Clone) {
// TODO: check if &self.name already exists in dir // TODO: check if &self.name already exists in dir
let out = Command::new("git") let out = Command::new("git")
.current_dir(&self.path) .current_dir(&self.path)
@ -117,7 +125,7 @@ impl GitRepo {
} }
/// Adds all files in the repository. /// Adds all files in the repository.
fn add_all(&self) { fn add_all(&self) {
if self.push { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("add") .arg("add")
@ -132,7 +140,7 @@ impl GitRepo {
/// Tries to commit changes in the repository. /// Tries to commit changes in the repository.
#[allow(dead_code)] #[allow(dead_code)]
fn commit(&self) { fn commit(&self) {
if self.push { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("commit") .arg("commit")
@ -145,7 +153,7 @@ impl GitRepo {
} }
/// Tries to commit changes with a message argument. /// Tries to commit changes with a message argument.
fn commit_with_msg(&self, msg: &String) { fn commit_with_msg(&self, msg: &String) {
if self.push { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("commit") .arg("commit")
@ -160,7 +168,7 @@ impl GitRepo {
} }
/// Attempts to push the repository. /// Attempts to push the repository.
fn push(&self) { fn push(&self) {
if self.push { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("push") .arg("push")

View file

@ -72,6 +72,7 @@ fn main() {
mod config { mod config {
use crate::*; use crate::*;
use git::GitRepo; use git::GitRepo;
use git::RepoFlags::{Clone, Push};
use relative_path::RelativePath; use relative_path::RelativePath;
use std::env::current_dir; use std::env::current_dir;
use std::fs::File; use std::fs::File;
@ -94,8 +95,7 @@ mod config {
name: "test repo".to_string(), name: "test repo".to_string(),
path: "/tmp".to_string(), path: "/tmp".to_string(),
url: "https://github.com/cafkafk/gg".to_string(), url: "https://github.com/cafkafk/gg".to_string(),
clone: false, flags: vec![Clone, Push],
push: false,
}; };
config.repos.push(repo); config.repos.push(repo);
} }
@ -141,35 +141,33 @@ mod config {
.into_string() .into_string()
.unwrap(), .unwrap(),
); );
let flags = vec![Clone, Push];
// FIXME This is unnecessarily terse // FIXME This is unnecessarily terse
#[allow(clippy::bool_assert_comparison)] #[allow(clippy::bool_assert_comparison)]
{ {
assert_eq!(config.repos[0].name, "gg"); assert_eq!(config.repos[0].name, "gg");
assert_eq!(config.repos[0].path, "/home/ces/.dots/"); assert_eq!(config.repos[0].path, "/home/ces/.dots/");
assert_eq!(config.repos[0].url, "git@github.com:cafkafk/gg.git"); assert_eq!(config.repos[0].url, "git@github.com:cafkafk/gg.git");
assert_eq!(config.repos[0].clone, true); assert_eq!(config.repos[0].flags, flags);
assert_eq!(config.repos[0].push, true);
assert_eq!(config.repos[1].name, "li"); assert_eq!(config.repos[1].name, "li");
assert_eq!(config.repos[1].path, "/home/ces/org/src/git/"); assert_eq!(config.repos[1].path, "/home/ces/org/src/git/");
assert_eq!(config.repos[1].url, "git@github.com:cafkafk/li.git"); assert_eq!(config.repos[1].url, "git@github.com:cafkafk/li.git");
assert_eq!(config.repos[1].clone, true); assert_eq!(config.repos[1].flags, flags);
assert_eq!(config.repos[1].push, true);
assert_eq!(config.repos[2].name, "qmk_firmware"); assert_eq!(config.repos[2].name, "qmk_firmware");
assert_eq!(config.repos[2].path, "/home/ces/org/src/git/"); assert_eq!(config.repos[2].path, "/home/ces/org/src/git/");
assert_eq!( assert_eq!(
config.repos[2].url, config.repos[2].url,
"git@github.com:cafkafk/qmk_firmware.git" "git@github.com:cafkafk/qmk_firmware.git"
); );
assert_eq!(config.repos[2].clone, true); assert_eq!(config.repos[2].flags, flags);
assert_eq!(config.repos[2].push, true);
assert_eq!(config.repos[3].name, "starship"); assert_eq!(config.repos[3].name, "starship");
assert_eq!(config.repos[3].path, "/home/ces/org/src/git/"); assert_eq!(config.repos[3].path, "/home/ces/org/src/git/");
assert_eq!( assert_eq!(
config.repos[3].url, config.repos[3].url,
"https://github.com/starship/starship.git" "https://github.com/starship/starship.git"
); );
assert_eq!(config.repos[3].clone, true); assert_eq!(config.repos[3].flags, flags);
assert_eq!(config.repos[3].push, true);
} }
{ {
assert_eq!(config.links[0].name, "gg"); assert_eq!(config.links[0].name, "gg");

View file

@ -2,23 +2,19 @@ repos:
- name: gg - name: gg
path: /home/ces/.dots/ path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git url: git@github.com:cafkafk/gg.git
clone: true flags: [Clone, Push]
push: true
- name: li - name: li
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git url: git@github.com:cafkafk/li.git
clone: true flags: [Clone, Push]
push: true
- name: qmk_firmware - name: qmk_firmware
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git url: git@github.com:cafkafk/qmk_firmware.git
clone: true flags: [Clone, Push]
push: true
- name: starship - name: starship
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git url: https://github.com/starship/starship.git
clone: true flags: [Clone, Push]
push: true
links: links:
- name: gg - name: gg
rx: /home/ces/.config/gg rx: /home/ces/.config/gg

View file

@ -2,23 +2,27 @@ repos:
- name: gg - name: gg
path: /home/ces/.dots/ path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git url: git@github.com:cafkafk/gg.git
clone: true flags:
push: true - Clone
- Push
- name: li - name: li
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git url: git@github.com:cafkafk/li.git
clone: true flags:
push: true - Clone
- Push
- name: qmk_firmware - name: qmk_firmware
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git url: git@github.com:cafkafk/qmk_firmware.git
clone: true flags:
push: true - Clone
- Push
- name: starship - name: starship
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git url: https://github.com/starship/starship.git
clone: true flags:
push: true - Clone
- Push
links: links:
- name: gg - name: gg
rx: /home/ces/.config/gg rx: /home/ces/.config/gg