Skip to content

Commit f535389

Browse files
committed
Commit checks
1 parent 765e238 commit f535389

File tree

9 files changed

+90
-58
lines changed

9 files changed

+90
-58
lines changed

CHANGELOG.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.1.5] - 2023-04-09
9+
10+
### Fixed
11+
12+
- Check for changed to avoid creating empty commits on every run
13+
814
## [0.1.4] - 2023-04-08
915

10-
## Added
16+
### Added
1117

1218
- Checkout and commit error checks;
1319
- Stage only files that were matched rather than staging all files before creating a commit.
1420

1521
## [0.1.3] - 2023-04-07
1622

17-
## Changed
23+
### Changed
1824

1925
- Rename binary from `git-raider` to `gitraider`
2026

@@ -58,12 +64,10 @@ This release marks the first full realization of project's description.
5864

5965
### Removed
6066

61-
- 'None
62-
63-
### Fixed
67+
- None
6468

65-
- 'None
6669

70+
[0.1.5]: https://github.com/mbrav/git_raider/compare/0.1.4...0.1.5
6771
[0.1.4]: https://github.com/mbrav/git_raider/compare/0.1.3...0.1.4
6872
[0.1.3]: https://github.com/mbrav/git_raider/compare/0.1.2...0.1.3
6973
[0.1.2]: https://github.com/mbrav/git_raider/compare/0.1.1...0.1.2

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "git_raider"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
descripion = "Mass git repository search, replace and commit tool"
55
authors = ["mbrav <mbrav@protonmail.com>"]
66
edition = "2021"

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,26 @@ Mass git repository search, replace and commit tool written in Rust
88

99
**⚠️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.
1010

11-
## Run
11+
## Install binary on Linux
12+
13+
To install the latest version of the binary to `/usr/local/bin/` copy the following into your terminal:
14+
15+
```bash
16+
latest_ver=$(curl https://raw.githubusercontent.com/mbrav/git_raider/main/latest)
17+
file_name=gitraider_$latest_ver-stable-x86_64-unknown-linux-gnu.tar.gz
18+
curl -L -o /tmp/$file_name https://github.com/mbrav/git_raider/releases/download/$latest_ver/$file_name
19+
tar -xvf /tmp/$file_name -C /tmp/
20+
sudo cp /tmp/target/release/gitraider /usr/local/bin/
21+
gitraider -V
22+
```
23+
24+
If successful, you will get the following after the end:
25+
26+
```text
27+
git_raider 0.1.4
28+
```
29+
30+
## Run from source
1231

1332
To run, first install Rust's tool chain. Then build:
1433

@@ -82,5 +101,8 @@ Elapsed: 39.170ms
82101
For base functionality to be completed, the following must still be finished:
83102

84103
- [x] ~~Create new commit with specified message~~;
104+
- [x] ~~Add more elaborate commit changes checks to avoid making duplicate changes and commits~~;
85105
- [ ] Push changes to remote after successful commit;
106+
- [ ] Add undo mechanics based on already done changes to avoid deleting and recreating all repositories after each unsuccessful run;
107+
- [ ] Add optional pull from remote before a commit to branch
86108
- [ ] Make `dry-run` mode more functional.

latest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.4
1+
0.1.5

src/func.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,6 @@ use regex::Regex;
22
use std::fs;
33
use std::path::PathBuf;
44

5-
#[cfg(target_os = "linux")]
6-
pub fn are_you_on_linux() {
7-
println!("You are running linux!");
8-
if cfg!(target_os = "linux") {
9-
println!("Yes. It's definitely linux!");
10-
} else {
11-
println!("Yes. It's definitely *not* linux!");
12-
}
13-
}
14-
15-
// And this function only gets compiled if the target OS is *not* linux
16-
#[cfg(not(target_os = "linux"))]
17-
pub fn are_you_on_linux() {
18-
println!("You are not using Linux. Do you seriously call yourself a developer?");
19-
println!("Do yourself a favor and install Linux");
20-
println!("I use arch btw");
21-
}
22-
235
/// Recursively find directories
246
#[must_use]
257
pub fn find_dirs(dir: &PathBuf, name: &str, parent: &bool) -> Vec<PathBuf> {
@@ -79,6 +61,7 @@ pub fn find_files(dir: &str, pattern: &str) -> Vec<PathBuf> {
7961
found_files
8062
}
8163

64+
/// Prints info about paths
8265
pub fn paths_info_print(list: &Vec<PathBuf>, msg: &str, elements: usize) {
8366
println!("First {} ({}) {}:", elements, list.len(), msg);
8467
for f in 0..elements {

src/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn checkout_branch(repo: &Repository, branch: &Branch) -> Result<Oid, git2::
4747
.expect("Error unwrapping repo head")
4848
.target()
4949
.expect("Error head target");
50-
println!(" Success checking out branch '{}' {}", refname, head);
50+
println!(" Success branch checkout '{}' {}", refname, head);
5151

5252
Ok(head)
5353
}

src/main.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use std::time::Instant;
22

33
use clap::Parser;
44
use git_raider::config::Config;
5-
// use git_raider::func;
65
use git_raider::raider::RepoRaider;
76

87
fn main() {
9-
// func::are_you_on_linux();
108
let conf = Config::parse();
119
let start: Instant = Instant::now();
1210

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

65-
/// Print results
63+
/// Print results
6664
fn results(raider: &RepoRaider) {
67-
// func::paths_info_print(&raider.get_dirs(), "found directories (repos)", 5);
65+
// git_raider::func::paths_info_print(&raider.get_dirs(), "found directories (repos)", 5);
6866
println!("REPORT");
6967
println!("M n - Matched files, number of matched lines");
7068
println!(" O n - Original line, line number");
7169
println!(" R n - Replaced line, line number");
72-
println!("Files:");
70+
println!("FILES:");
7371
raider.get_pages().iter().for_each(|p| {
7472
println!("M {}: {}", p.matches.len(), p.relative_path.display());
75-
p.matches.iter().for_each(|m| {
76-
println!(" O {:<3} {}", m.line, m.content);
77-
if let Some(r) = m.replace.as_ref() {
78-
println!(" R {:<3} {}", m.line, r);
79-
} else {
80-
println!(" R None");
81-
}
82-
});
73+
p.matches
74+
.iter()
75+
.filter(|m| m.replace.is_some())
76+
.for_each(|m| {
77+
println!(" O {:<3} {}", m.line, m.content);
78+
if let Some(r) = m.replace.as_ref() {
79+
println!(" R {:<3} {}", m.line, r);
80+
} else {
81+
println!(" R None");
82+
}
83+
});
8384
});
8485
}

src/raider.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ impl RepoRaider {
154154
dir.pages.iter_mut().for_each(|page| {
155155
page.matches.iter_mut().for_each(|mat| {
156156
let res = re.replace(mat.content.as_str(), replace);
157-
mat.replace = Some(res.to_string());
157+
let replace_string = res.to_string();
158+
159+
// Create a replace string only if replaced string does not match line content
160+
if replace_string != mat.content {
161+
mat.replace = Some(replace_string);
162+
println!(" Replaced '{}'", mat.replace.as_ref().unwrap())
163+
}
158164
});
159165
});
160166
});
@@ -211,19 +217,25 @@ impl RepoRaider {
211217
.filter(|p| !p.matches.is_empty())
212218
.collect();
213219

214-
files.into_iter().for_each(|f| {
215-
// Get file path relative to repository root
216-
let file_repo_path = f
217-
.relative_path
218-
.strip_prefix(&dir.relative_path)
219-
.expect("Error stripping Path prefix");
220-
let staged = git::stage_file(repo, file_repo_path);
221-
match staged {
222-
Ok(_) => {
223-
println!(" Staged '{}'", file_repo_path.display());
224-
}
225-
Err(_) => {
226-
println!(" Error staging '{}'", file_repo_path.display());
220+
files.into_iter().for_each(|p| {
221+
// Check if file has at least on replace pattern
222+
if p.matches.into_iter().any(|m| m.replace.is_some()) {
223+
println!(
224+
" File {} has replace patterns",
225+
p.relative_path.display()
226+
);
227+
// Get file path relative to repository root
228+
let file_repo_path = p
229+
.relative_path
230+
.strip_prefix(&dir.relative_path)
231+
.expect("Error stripping Path prefix");
232+
match git::stage_file(repo, file_repo_path) {
233+
Ok(_) => {
234+
println!(" Staged '{}'", file_repo_path.display());
235+
}
236+
Err(_) => {
237+
println!(" Error staging '{}'", file_repo_path.display());
238+
}
227239
}
228240
}
229241
});
@@ -240,8 +252,18 @@ impl RepoRaider {
240252
pub fn commit(&mut self, msg: &str) {
241253
self.dirs.iter_mut().for_each(|dir| {
242254
if let Some(repo) = &mut dir.repo {
243-
// Commit all staged files
244-
git::commit(repo, msg).expect("Error committing changes");
255+
// Check if there are is at least one Match to commit
256+
let do_commit = dir
257+
.pages
258+
.iter()
259+
.flat_map(|p| p.matches.iter())
260+
.any(|m| m.replace.is_some());
261+
// If page has at least on Match that was has Some replace string, commit file
262+
if do_commit {
263+
git::commit(repo, msg).expect("Error committing changes");
264+
} else {
265+
println!(" No changes to commit")
266+
}
245267
} else {
246268
println!(
247269
"Skipping {} not a git repository",

0 commit comments

Comments
 (0)