Skip to content

Commit

Permalink
Commit checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrav committed Apr 9, 2023
1 parent 765e238 commit f535389
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 58 deletions.
16 changes: 10 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
edition = "2021"
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion latest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.4
0.1.5
19 changes: 1 addition & 18 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
Expand Down Expand Up @@ -79,6 +61,7 @@ pub fn find_files(dir: &str, pattern: &str) -> Vec<PathBuf> {
found_files
}

/// Prints info about paths
pub fn paths_info_print(list: &Vec<PathBuf>, msg: &str, elements: usize) {
println!("First {} ({}) {}:", elements, list.len(), msg);
for f in 0..elements {
Expand Down
2 changes: 1 addition & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn checkout_branch(repo: &Repository, branch: &Branch) -> Result<Oid, git2::
.expect("Error unwrapping repo head")
.target()
.expect("Error head target");
println!(" Success checking out branch '{}' {}", refname, head);
println!(" Success branch checkout '{}' {}", refname, head);

Ok(head)
}
Expand Down
27 changes: 14 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use std::time::Instant;

use clap::Parser;
use git_raider::config::Config;
// use git_raider::func;
use git_raider::raider::RepoRaider;

fn main() {
// func::are_you_on_linux();
let conf = Config::parse();
let start: Instant = Instant::now();

Expand Down Expand Up @@ -62,23 +60,26 @@ fn main() {
println!("Elapsed: {:.3?}", start.elapsed());
}

/// Print results
/// Print results
fn results(raider: &RepoRaider) {
// func::paths_info_print(&raider.get_dirs(), "found directories (repos)", 5);
// git_raider::func::paths_info_print(&raider.get_dirs(), "found directories (repos)", 5);
println!("REPORT");
println!("M n - Matched files, number of matched lines");
println!(" O n - Original line, line number");
println!(" R n - Replaced line, line number");
println!("Files:");
println!("FILES:");
raider.get_pages().iter().for_each(|p| {
println!("M {}: {}", p.matches.len(), p.relative_path.display());
p.matches.iter().for_each(|m| {
println!(" O {:<3} {}", m.line, m.content);
if let Some(r) = m.replace.as_ref() {
println!(" R {:<3} {}", m.line, r);
} else {
println!(" R None");
}
});
p.matches
.iter()
.filter(|m| m.replace.is_some())
.for_each(|m| {
println!(" O {:<3} {}", m.line, m.content);
if let Some(r) = m.replace.as_ref() {
println!(" R {:<3} {}", m.line, r);
} else {
println!(" R None");
}
});
});
}
54 changes: 38 additions & 16 deletions src/raider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ impl RepoRaider {
dir.pages.iter_mut().for_each(|page| {
page.matches.iter_mut().for_each(|mat| {
let res = re.replace(mat.content.as_str(), replace);
mat.replace = Some(res.to_string());
let replace_string = res.to_string();

// Create a replace string only if replaced string does not match line content
if replace_string != mat.content {
mat.replace = Some(replace_string);
println!(" Replaced '{}'", mat.replace.as_ref().unwrap())
}
});
});
});
Expand Down Expand Up @@ -211,19 +217,25 @@ impl RepoRaider {
.filter(|p| !p.matches.is_empty())
.collect();

files.into_iter().for_each(|f| {
// Get file path relative to repository root
let file_repo_path = f
.relative_path
.strip_prefix(&dir.relative_path)
.expect("Error stripping Path prefix");
let staged = git::stage_file(repo, file_repo_path);
match staged {
Ok(_) => {
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());
}
}
}
});
Expand All @@ -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",
Expand Down

0 comments on commit f535389

Please sign in to comment.