From 07b7ad0a09b829357d7e1bf4c24f726090a7c747 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Sat, 5 Mar 2022 19:36:02 -0500 Subject: [PATCH] Allow ignoring certain files from being managed in the git repository. (#3) --- README.md | 3 +++ rsrc/example.yml | 3 +++ rsrc/template.yml | 3 +++ src/config.rs | 12 +++++++++++- src/copy.rs | 9 ++++++++- src/git.rs | 25 ++----------------------- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 8b19d5e..2b3b1b2 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,9 @@ repos: name: origin branch: master url: "https://github.com/owner/repo.git" +unmanaged: + - LICENSE + - README.md packages: homesync: - $HOME/.homesync.yml diff --git a/rsrc/example.yml b/rsrc/example.yml index 01a8b0a..72ece24 100644 --- a/rsrc/example.yml +++ b/rsrc/example.yml @@ -10,6 +10,9 @@ repos: name: origin branch: master url: "git@github.com:jrpotter/home-config.git" +unmanaged: + - LICENSE + - README.md packages: alacritty: - $HOME/.alacritty.yml diff --git a/rsrc/template.yml b/rsrc/template.yml index f3c9b2f..f3927aa 100644 --- a/rsrc/template.yml +++ b/rsrc/template.yml @@ -11,6 +11,9 @@ repos: name: origin branch: master url: "https://github.com/owner/repo.git" +unmanaged: + - LICENSE + - README.md packages: homesync: - $HOME/.homesync.yml diff --git a/src/config.rs b/src/config.rs index 114fd2c..d76de60 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,9 @@ //! name: origin //! branch: master //! url: "https://github.com/owner/repo.git" +//! unmanaged: +//! - LICENSE +//! - README.md //! packages: //! homesync: //! - $HOME/.homesync.yml @@ -28,7 +31,13 @@ use super::{path, path::ResPathBuf}; use paris::formatter::colorize_string; use serde_derive::{Deserialize, Serialize}; use simplelog::{info, paris}; -use std::{collections::BTreeMap, env::VarError, error, fmt, fs, io, io::Write, path::PathBuf}; +use std::{ + collections::{BTreeMap, HashSet}, + env::VarError, + error, fmt, fs, io, + io::Write, + path::PathBuf, +}; // ======================================== // Error @@ -124,6 +133,7 @@ pub struct Config { pub user: User, pub ssh: SSH, pub repos: Repos, + pub unmanaged: Option>, pub packages: BTreeMap>, } diff --git a/src/copy.rs b/src/copy.rs index 5eee22a..328c5d0 100644 --- a/src/copy.rs +++ b/src/copy.rs @@ -153,7 +153,14 @@ pub fn stage(pc: &PathConfig) -> Result<()> { // Find all files in our repository that are no longer being referenced in // our primary config file. They should be removed from the repository. for repo_file in &repo_files { - if !package_lookup.contains_key(repo_file.unresolved()) { + let unresolved = repo_file.unresolved(); + if !package_lookup.contains_key(unresolved) + && !pc + .config + .unmanaged + .as_ref() + .map_or(false, |m| m.contains(unresolved)) + { fs::remove_file(repo_file.resolved())?; } if let Some(p) = repo_file.resolved().parent() { diff --git a/src/git.rs b/src/git.rs index 9dd6f68..f7ffb00 100644 --- a/src/git.rs +++ b/src/git.rs @@ -9,9 +9,7 @@ use git2::{ StashFlags, }; use simplelog::{info, paris, warn}; -use std::{ - collections::HashSet, env::VarError, error, fmt, fs::File, io, io::Write, path::Path, result, -}; +use std::{collections::HashSet, env::VarError, error, fmt, io, path::Path, result}; // ======================================== // Error @@ -140,24 +138,6 @@ pub fn init(pc: &PathConfig) -> Result { // Syncing // ======================================== -fn create_readme(repo: &Repository) -> Result<()> { - let workdir = repo.workdir().ok_or(Error::InvalidBareRepo)?; - - let mut readme = workdir.to_path_buf(); - readme.push("README.md"); - - let mut file = File::create(&readme)?; - // TODO(jrpotter): Get the name of the remote repository. - file.write_all( - "# home-config\n\nThis repository is maintained by \ - [homesync](https://github.com/jrpotter/homesync)." - .as_ref(), - )?; - info!("Generated new README.md file."); - - Ok(()) -} - /// Take the current state of the local repository and push changes to the /// remote. /// @@ -171,7 +151,6 @@ pub fn push(pc: &PathConfig, repo: &mut Repository) -> Result<()> { let refspec = format!("refs/heads/{}", &pc.config.repos.remote.branch); repo.set_head(&refspec)?; - create_readme(repo)?; // The index corresponds to our staging area. We add all files and write out // to a tree. The resulting tree can be found using `git ls-tree `. @@ -498,7 +477,7 @@ fn get_push_options(pc: &PathConfig) -> Result { } // ======================================== -// Miscellaneous +// Utility // ======================================== fn now_signature(pc: &PathConfig) -> Result {