Skip to content

Commit 72878f6

Browse files
Feat: pull changes for every git repo
1 parent 0de0233 commit 72878f6

9 files changed

+148
-10
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repos:
2424
# args: ['--verbose', '--']
2525
- id: cargo-check
2626
- repo: https://github.com/containerscrew/mtoc
27-
rev: v0.3.1
27+
rev: v0.5.0
2828
hooks:
2929
- id: mtoc
3030
args: [ "-e", "./target" ]

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->
2+
**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)*
3+
- [Changelog](#changelog)
4+
- [[2.2.1] - 2025-03-04](#221---2025-03-04)
5+
- [[2.2.0] - 2025-02-14](#220---2025-02-14)
6+
- [[2.1.1] - 2025-02-14](#211---2025-02-14)
7+
- [[2.1.0] - 2025-02-14](#210---2025-02-14)
8+
- [[2.0.0] - 2024-08-23](#200---2024-08-23)
9+
- [[1.1.0] - 2024-08-23](#110---2024-08-23)
10+
- [[1.0.0] - 2024-08-22](#100---2024-08-22)
11+
<!-- END OF TOC -->
112
# Changelog
213

314
All notable changes to this project will be documented in this file.

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
[package]
22
name = "gitrack"
3-
version = "2.2.1"
3+
version = "2.3.0"
44
edition = "2021"
55
authors = ["containerscrew [email protected]"]
66
repository = "https://github.com/containerscrew/gitrack"
77
rust-version = "1.80.1"
88
license-file = "LICENSE"
9-
description = "Scan local git repositories"
10-
9+
description = "Scan local git repositories and show their status (check untracked files)"
1110

1211
[dependencies]
1312
clap = { version = "4.5.37", features = ["derive"] }
@@ -16,6 +15,7 @@ git2 = { version = "0.20.2", default-features = false }
1615
walkdir = "2.5.0"
1716
home = "0.5.9"
1817
threadpool = "1.8.1"
18+
which = "7.0.3"
1919

2020
# Pending to add benchmark
2121
#[dev-dependencies]

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
- [Usage](#usage)
1515
- [Help](#help)
1616
- [Scan folders containing git repositories](#scan-folders-containing-git-repositories)
17-
- [Scanning for untracked changes (summarized)](#scanning-for-untracked-changes-(summarized))
18-
- [Scanning for untracked changes (verbose)](#scanning-for-untracked-changes-(verbose))
17+
- [Scanning for untracked changes (summarized)](#scanning-for-untracked-changes-summarized)
18+
- [Scanning for untracked changes (verbose)](#scanning-for-untracked-changes-verbose)
1919
- [Diff files for untracked changes](#diff-files-for-untracked-changes)
20-
- [Control number of workers/threads](#control-number-of-workers/threads)
20+
- [Control number of workers/threads](#control-number-of-workersthreads)
2121
- [Exclude directories](#exclude-directories)
22+
- [Pulling changes](#pulling-changes)
2223
- [About threads](#about-threads)
2324
- [Dev](#dev)
2425
- [pre-commit](#pre-commit)
@@ -158,7 +159,15 @@ gitrack -p /home/elliot -u -w 6
158159
## Exclude directories
159160

160161
```bash
161-
gitrack -p /home/elliot -e "/home/elliot/.cache" -e "/home/elliot/.local" -u -w 6
162+
gitrack -p /home/elliot -e .terragrunt-cache/ -e .terraform/ -u -w 6
163+
```
164+
165+
## Pulling changes
166+
167+
Run a `git pull` to update local code from remote repositories:
168+
169+
```shell
170+
gitrack -p /home/elliot/Documents/Code/ -e .terragrunt-cache/ -e .terraform/ --pull
162171
```
163172

164173
# About threads

src/cli.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@ pub struct Args {
6565
required = false
6666
)]
6767
pub verbose: bool,
68+
69+
#[arg(
70+
long = "pull",
71+
help = "Pull changes using git pull",
72+
default_value_t = false,
73+
required = false
74+
)]
75+
pub pull: bool,
6876
}

src/git_ops.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ use git2::{DiffOptions, Repository, StatusOptions};
33
use std::io;
44
use std::io::ErrorKind;
55
use std::path::{Path, PathBuf};
6+
use std::process::{Command, Stdio};
67
use std::sync::mpsc;
78
use std::sync::mpsc::Sender;
89
use threadpool::ThreadPool;
910
use walkdir::WalkDir;
1011

12+
use crate::utils::find_binary_path;
13+
1114
// Check if a directory should be excluded
1215
fn is_excluded_dir(entry: &walkdir::DirEntry, exclude_dirs: &[String]) -> bool {
1316
let path = entry.path(); // Get the full path of the directory
@@ -67,6 +70,27 @@ pub fn check_untracked_files(repo_path: &Path) -> Result<Vec<String>, git2::Erro
6770
Ok(untracked_files)
6871
}
6972

73+
// Pull changes in a Git repository
74+
// TODO: use git2 library instead of command process, like the rest of the code
75+
pub fn pull_changes(repo_path: &Path) -> Result<String, io::Error> {
76+
let git_path = find_binary_path("git").unwrap_or_else(|_| "/usr/bin/git".into());
77+
78+
// Ejecutar git pull en el directorio correcto
79+
let output = Command::new(git_path)
80+
.arg("pull")
81+
.current_dir(repo_path)
82+
.stdout(Stdio::piped())
83+
.stderr(Stdio::piped())
84+
.output()?;
85+
86+
if output.status.success() {
87+
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
88+
} else {
89+
let error_msg = String::from_utf8_lossy(&output.stderr);
90+
Err(io::Error::new(ErrorKind::Other, error_msg.to_string()))
91+
}
92+
}
93+
7094
// Show the diff for a specific file in a Git repository
7195
pub fn show_diff(repo_path: &Path, file: &str) -> io::Result<String> {
7296
let repo = Repository::open(repo_path).expect("Error opening repository");

src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::git_ops::{check_untracked_files, find_git_repos, show_diff};
22
use clap::Parser;
33
use cli::Args;
44
use colored::*;
5+
use git_ops::pull_changes;
56
use std::path::Path;
67

78
mod cli;
@@ -41,7 +42,20 @@ fn main() {
4142

4243
// Process each repository
4344
for repo in git_repos {
44-
process_repo(&repo, &args);
45+
// Git pull to update the repository
46+
if args.pull {
47+
println_orange!("Pulling latest changes in: {}", repo.display());
48+
match pull_changes(&repo) {
49+
Ok(result) => println_orange!(
50+
"Successfully pulled changes in: {}. {}",
51+
repo.display(),
52+
result
53+
),
54+
Err(e) => println_light_orange!("{}: {}", "Error pulling changes".red(), e),
55+
}
56+
} else {
57+
process_repo(&repo, &args);
58+
}
4559
}
4660
}
4761

src/utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use std::io;
2+
13
use colored::Colorize;
4+
use which::which;
25

36
pub fn default_home_dir() -> String {
47
match home::home_dir() {
@@ -9,3 +12,12 @@ pub fn default_home_dir() -> String {
912
}
1013
}
1114
}
15+
16+
pub fn find_binary_path(binary_name: &str) -> Result<std::path::PathBuf, io::Error> {
17+
which(binary_name).map_err(|e| {
18+
io::Error::new(
19+
io::ErrorKind::NotFound,
20+
format!("'{}' not found: {}", binary_name, e),
21+
)
22+
})
23+
}

0 commit comments

Comments
 (0)