Skip to content

Commit

Permalink
Merge pull request #11 from omarahm3/feat/improve-git-stash
Browse files Browse the repository at this point in the history
Feat/improve git stash
  • Loading branch information
omarahm3 authored Jan 3, 2022
2 parents 367492a + d5ebf76 commit bdc92c0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repository = "https://github.com/omarahm3/switcher"
readme = "README.md"
license = "MIT"
keywords = ["microservices", "git", "multi-projects"]
version = "0.0.6-alpha"
version = "0.0.7-alpha"
authors = ["Omar Ahmed <[email protected]>"]
edition = "2021"
rust-version ="1.57"
Expand Down
2 changes: 1 addition & 1 deletion src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn print(program: ProgramInfo) {
print!("\t\t\t\t{}", name);

if detail {
let current_branch = git_current_branch(repository.to_path_buf());
let current_branch = git_current_branch(repository);
// TODO properly handle the perfect alignment of the tabs
println!(" ({})", current_branch.yellow());
} else {
Expand Down
57 changes: 41 additions & 16 deletions src/core/git.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use colored::*;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -28,7 +29,7 @@ pub fn get_repositories(path: &Path) -> Vec<PathBuf> {
repositories
}

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

println!("\t!> Running git fetch");
git_fetch(repository.to_path_buf());
git_fetch(&repository);

println!("\t!> Running git stash");
git_stash(repository.to_path_buf());
let should_stash = git_has_changes(&repository);

if should_stash {
println!("\t!> Running git stash");
git_stash(&repository);
}

println!("\t!> Running git checkout {}", &branch);
git_checkout(repository.to_path_buf(), branch.to_string());
let (err, _) = git_checkout(&repository, branch.to_string());

if !err.is_empty() && err.contains("did not match any file(s) known to git") {
println!("\t{}", "!!> Branch does not exist on this repository".red());
}

println!("\t!> Running git pull");
git_pull(repository.to_path_buf());
let (err, _) = git_pull(&repository);

if !err.is_empty() && err.contains("but no such ref was fetched") {
println!(
"\t{}",
"!!> Branch does not exist on the remote, most probably remote branch was removed"
.red()
);
}

println!("\n");
}

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

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

fn git_checkout(repository: PathBuf, branch: String) {
fn git_checkout(repository: &Path, branch: String) -> (String, String) {
let command_string = format!("git checkout {}", branch);
let (command, args) = build_command(&command_string);
run_command(command, args, repository);
run_command(command, args, repository)
}

fn git_pull(repository: PathBuf) {
fn git_pull(repository: &Path) -> (String, String) {
let (command, args) = build_command("git pull");
run_command(command, args, repository);
run_command(command, args, repository)
}

fn git_fetch(repository: PathBuf) {
fn git_fetch(repository: &Path) -> (String, String) {
let command_string = "git fetch";
let (command, args) = build_command(command_string);
run_command(command, args, repository);
run_command(command, args, repository)
}

fn git_stash(repository: PathBuf) {
// TODO save unique stash message
fn git_stash(repository: &Path) -> (String, String) {
let command_string = "git stash save 'switcher:: changes'";
let (command, args) = build_command(command_string);
run_command(command, args, repository);
run_command(command, args, repository)
}

fn git_has_changes(repository: &Path) -> bool {
let command_string = "git status -s";
let (command, args) = build_command(command_string);
let (_, output) = run_command(command, args, repository);

!output.is_empty()
}

0 comments on commit bdc92c0

Please sign in to comment.