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]]
name = "gg"
version = "0.0.4"
version = "0.0.5"
dependencies = [
"clap",
"clap_mangen",

View file

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

View file

@ -21,6 +21,15 @@ use std::os::unix::fs::symlink;
use std::path::Path;
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.
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Config {
@ -42,8 +51,7 @@ pub struct GitRepo {
pub name: String,
pub path: String,
pub url: String,
pub clone: bool,
pub push: bool,
pub flags: Vec<RepoFlags>,
}
fn handle_file_exists(selff: &Links, tx_path: &Path, rx_path: &Path) {
@ -92,7 +100,7 @@ impl Links {
impl GitRepo {
/// Clones the repository to its specified folder.
fn clone(&self) {
if self.clone {
if self.flags.contains(&RepoFlags::Clone) {
// TODO: check if &self.name already exists in dir
let out = Command::new("git")
.current_dir(&self.path)
@ -117,7 +125,7 @@ impl GitRepo {
}
/// Adds all files in the repository.
fn add_all(&self) {
if self.push {
if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name))
.arg("add")
@ -132,7 +140,7 @@ impl GitRepo {
/// Tries to commit changes in the repository.
#[allow(dead_code)]
fn commit(&self) {
if self.push {
if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name))
.arg("commit")
@ -145,7 +153,7 @@ impl GitRepo {
}
/// Tries to commit changes with a message argument.
fn commit_with_msg(&self, msg: &String) {
if self.push {
if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name))
.arg("commit")
@ -160,7 +168,7 @@ impl GitRepo {
}
/// Attempts to push the repository.
fn push(&self) {
if self.push {
if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name))
.arg("push")

View file

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

View file

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

View file

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