Allow ignoring certain files from being managed in the git repository. (#3)

main
Joshua Potter 2022-03-05 19:36:02 -05:00 committed by GitHub
parent 09674036bd
commit 07b7ad0a09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 25 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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<HashSet<PathBuf>>,
pub packages: BTreeMap<String, Vec<PathBuf>>,
}

View File

@ -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() {

View File

@ -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<Repository> {
// 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 <cyan>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 <oid>`.
@ -498,7 +477,7 @@ fn get_push_options(pc: &PathConfig) -> Result<PushOptions> {
}
// ========================================
// Miscellaneous
// Utility
// ========================================
fn now_signature(pc: &PathConfig) -> Result<Signature> {