Skip to content

Commit bdc92c0

Browse files
authored
Merge pull request #11 from omarahm3/feat/improve-git-stash
Feat/improve git stash
2 parents 367492a + d5ebf76 commit bdc92c0

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository = "https://github.com/omarahm3/switcher"
66
readme = "README.md"
77
license = "MIT"
88
keywords = ["microservices", "git", "multi-projects"]
9-
version = "0.0.6-alpha"
9+
version = "0.0.7-alpha"
1010
authors = ["Omar Ahmed <[email protected]>"]
1111
edition = "2021"
1212
rust-version ="1.57"

src/core/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub fn print(program: ProgramInfo) {
126126
print!("\t\t\t\t{}", name);
127127

128128
if detail {
129-
let current_branch = git_current_branch(repository.to_path_buf());
129+
let current_branch = git_current_branch(repository);
130130
// TODO properly handle the perfect alignment of the tabs
131131
println!(" ({})", current_branch.yellow());
132132
} else {

src/core/git.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use colored::*;
12
use std::fs;
23
use std::path::Path;
34
use std::path::PathBuf;
@@ -28,7 +29,7 @@ pub fn get_repositories(path: &Path) -> Vec<PathBuf> {
2829
repositories
2930
}
3031

31-
pub fn git_current_branch(repository: PathBuf) -> String {
32+
pub fn git_current_branch(repository: &Path) -> String {
3233
let command_string = "git rev-parse --abbrev-ref HEAD";
3334
let (command, args) = build_command(command_string);
3435
let (error, output) = run_command(command, args, repository);
@@ -46,16 +47,33 @@ pub fn sync_repository_to_branch(repository: PathBuf, branch: &str) {
4647
println!("!> Syncing repository: [{}]", repo_name);
4748

4849
println!("\t!> Running git fetch");
49-
git_fetch(repository.to_path_buf());
50+
git_fetch(&repository);
5051

51-
println!("\t!> Running git stash");
52-
git_stash(repository.to_path_buf());
52+
let should_stash = git_has_changes(&repository);
53+
54+
if should_stash {
55+
println!("\t!> Running git stash");
56+
git_stash(&repository);
57+
}
5358

5459
println!("\t!> Running git checkout {}", &branch);
55-
git_checkout(repository.to_path_buf(), branch.to_string());
60+
let (err, _) = git_checkout(&repository, branch.to_string());
61+
62+
if !err.is_empty() && err.contains("did not match any file(s) known to git") {
63+
println!("\t{}", "!!> Branch does not exist on this repository".red());
64+
}
5665

5766
println!("\t!> Running git pull");
58-
git_pull(repository.to_path_buf());
67+
let (err, _) = git_pull(&repository);
68+
69+
if !err.is_empty() && err.contains("but no such ref was fetched") {
70+
println!(
71+
"\t{}",
72+
"!!> Branch does not exist on the remote, most probably remote branch was removed"
73+
.red()
74+
);
75+
}
76+
5977
println!("\n");
6078
}
6179

@@ -72,7 +90,7 @@ fn build_command(command: &str) -> (String, Vec<&str>) {
7290
(command.to_string(), args.to_vec())
7391
}
7492

75-
fn run_command(command: String, args: Vec<&str>, cwd: PathBuf) -> (String, String) {
93+
fn run_command(command: String, args: Vec<&str>, cwd: &Path) -> (String, String) {
7694
let command = match Command::new(command).current_dir(cwd).args(args).output() {
7795
Err(err) => panic!("Error running command [{}]", err),
7896
Ok(cmd) => cmd,
@@ -86,26 +104,33 @@ fn run_command(command: String, args: Vec<&str>, cwd: PathBuf) -> (String, Strin
86104
(error, output)
87105
}
88106

89-
fn git_checkout(repository: PathBuf, branch: String) {
107+
fn git_checkout(repository: &Path, branch: String) -> (String, String) {
90108
let command_string = format!("git checkout {}", branch);
91109
let (command, args) = build_command(&command_string);
92-
run_command(command, args, repository);
110+
run_command(command, args, repository)
93111
}
94112

95-
fn git_pull(repository: PathBuf) {
113+
fn git_pull(repository: &Path) -> (String, String) {
96114
let (command, args) = build_command("git pull");
97-
run_command(command, args, repository);
115+
run_command(command, args, repository)
98116
}
99117

100-
fn git_fetch(repository: PathBuf) {
118+
fn git_fetch(repository: &Path) -> (String, String) {
101119
let command_string = "git fetch";
102120
let (command, args) = build_command(command_string);
103-
run_command(command, args, repository);
121+
run_command(command, args, repository)
104122
}
105123

106-
fn git_stash(repository: PathBuf) {
107-
// TODO save unique stash message
124+
fn git_stash(repository: &Path) -> (String, String) {
108125
let command_string = "git stash save 'switcher:: changes'";
109126
let (command, args) = build_command(command_string);
110-
run_command(command, args, repository);
127+
run_command(command, args, repository)
128+
}
129+
130+
fn git_has_changes(repository: &Path) -> bool {
131+
let command_string = "git status -s";
132+
let (command, args) = build_command(command_string);
133+
let (_, output) = run_command(command, args, repository);
134+
135+
!output.is_empty()
111136
}

0 commit comments

Comments
 (0)