From f53538907c477754f511d96e5cd07fc4db4a698c Mon Sep 17 00:00:00 2001 From: mbrav Date: Sun, 9 Apr 2023 14:45:12 +0300 Subject: [PATCH] Commit checks --- CHANGELOG.md | 16 +++++++++------ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 24 ++++++++++++++++++++++- latest | 2 +- src/func.rs | 19 +----------------- src/git.rs | 2 +- src/main.rs | 27 +++++++++++++------------- src/raider.rs | 54 ++++++++++++++++++++++++++++++++++++--------------- 9 files changed, 90 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 238847e..966f860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,16 +5,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.1.5] - 2023-04-09 + +### Fixed + +- Check for changed to avoid creating empty commits on every run + ## [0.1.4] - 2023-04-08 -## Added +### Added - Checkout and commit error checks; - Stage only files that were matched rather than staging all files before creating a commit. ## [0.1.3] - 2023-04-07 -## Changed +### Changed - Rename binary from `git-raider` to `gitraider` @@ -58,12 +64,10 @@ This release marks the first full realization of project's description. ### Removed -- 'None - -### Fixed +- None -- 'None +[0.1.5]: https://github.com/mbrav/git_raider/compare/0.1.4...0.1.5 [0.1.4]: https://github.com/mbrav/git_raider/compare/0.1.3...0.1.4 [0.1.3]: https://github.com/mbrav/git_raider/compare/0.1.2...0.1.3 [0.1.2]: https://github.com/mbrav/git_raider/compare/0.1.1...0.1.2 diff --git a/Cargo.lock b/Cargo.lock index 6ac780a..abb8576 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,7 +176,7 @@ dependencies = [ [[package]] name = "git_raider" -version = "0.1.4" +version = "0.1.5" dependencies = [ "clap", "git2", diff --git a/Cargo.toml b/Cargo.toml index 5d34680..1598875 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "git_raider" -version = "0.1.4" +version = "0.1.5" descripion = "Mass git repository search, replace and commit tool" authors = ["mbrav "] edition = "2021" diff --git a/README.md b/README.md index e974d18..54ad4bd 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,26 @@ Mass git repository search, replace and commit tool written in Rust **⚠️WARNING⚠️** This tool is designed to make changes to hundreds of git repositories, as well as pushing these changes to remote if `--push` flag is used. This project is still WIP so please test on repositories that you have a safe copy. -## Run +## Install binary on Linux + +To install the latest version of the binary to `/usr/local/bin/` copy the following into your terminal: + +```bash +latest_ver=$(curl https://raw.githubusercontent.com/mbrav/git_raider/main/latest) +file_name=gitraider_$latest_ver-stable-x86_64-unknown-linux-gnu.tar.gz +curl -L -o /tmp/$file_name https://github.com/mbrav/git_raider/releases/download/$latest_ver/$file_name +tar -xvf /tmp/$file_name -C /tmp/ +sudo cp /tmp/target/release/gitraider /usr/local/bin/ +gitraider -V +``` + +If successful, you will get the following after the end: + +```text +git_raider 0.1.4 +``` + +## Run from source To run, first install Rust's tool chain. Then build: @@ -82,5 +101,8 @@ Elapsed: 39.170ms For base functionality to be completed, the following must still be finished: - [x] ~~Create new commit with specified message~~; +- [x] ~~Add more elaborate commit changes checks to avoid making duplicate changes and commits~~; - [ ] Push changes to remote after successful commit; +- [ ] Add undo mechanics based on already done changes to avoid deleting and recreating all repositories after each unsuccessful run; +- [ ] Add optional pull from remote before a commit to branch - [ ] Make `dry-run` mode more functional. diff --git a/latest b/latest index 446ba66..def9a01 100644 --- a/latest +++ b/latest @@ -1 +1 @@ -0.1.4 \ No newline at end of file +0.1.5 \ No newline at end of file diff --git a/src/func.rs b/src/func.rs index 7e9a794..0740cb7 100644 --- a/src/func.rs +++ b/src/func.rs @@ -2,24 +2,6 @@ use regex::Regex; use std::fs; use std::path::PathBuf; -#[cfg(target_os = "linux")] -pub fn are_you_on_linux() { - println!("You are running linux!"); - if cfg!(target_os = "linux") { - println!("Yes. It's definitely linux!"); - } else { - println!("Yes. It's definitely *not* linux!"); - } -} - -// And this function only gets compiled if the target OS is *not* linux -#[cfg(not(target_os = "linux"))] -pub fn are_you_on_linux() { - println!("You are not using Linux. Do you seriously call yourself a developer?"); - println!("Do yourself a favor and install Linux"); - println!("I use arch btw"); -} - /// Recursively find directories #[must_use] pub fn find_dirs(dir: &PathBuf, name: &str, parent: &bool) -> Vec { @@ -79,6 +61,7 @@ pub fn find_files(dir: &str, pattern: &str) -> Vec { found_files } +/// Prints info about paths pub fn paths_info_print(list: &Vec, msg: &str, elements: usize) { println!("First {} ({}) {}:", elements, list.len(), msg); for f in 0..elements { diff --git a/src/git.rs b/src/git.rs index 15909d7..3044f69 100644 --- a/src/git.rs +++ b/src/git.rs @@ -47,7 +47,7 @@ pub fn checkout_branch(repo: &Repository, branch: &Branch) -> Result { - println!(" Staged '{}'", file_repo_path.display()); - } - Err(_) => { - println!(" Error staging '{}'", file_repo_path.display()); + files.into_iter().for_each(|p| { + // Check if file has at least on replace pattern + if p.matches.into_iter().any(|m| m.replace.is_some()) { + println!( + " File {} has replace patterns", + p.relative_path.display() + ); + // Get file path relative to repository root + let file_repo_path = p + .relative_path + .strip_prefix(&dir.relative_path) + .expect("Error stripping Path prefix"); + match git::stage_file(repo, file_repo_path) { + Ok(_) => { + println!(" Staged '{}'", file_repo_path.display()); + } + Err(_) => { + println!(" Error staging '{}'", file_repo_path.display()); + } } } }); @@ -240,8 +252,18 @@ impl RepoRaider { pub fn commit(&mut self, msg: &str) { self.dirs.iter_mut().for_each(|dir| { if let Some(repo) = &mut dir.repo { - // Commit all staged files - git::commit(repo, msg).expect("Error committing changes"); + // Check if there are is at least one Match to commit + let do_commit = dir + .pages + .iter() + .flat_map(|p| p.matches.iter()) + .any(|m| m.replace.is_some()); + // If page has at least on Match that was has Some replace string, commit file + if do_commit { + git::commit(repo, msg).expect("Error committing changes"); + } else { + println!(" No changes to commit") + } } else { println!( "Skipping {} not a git repository",