Skip to content

Commit

Permalink
Fix mure refresh bug
Browse files Browse the repository at this point in the history
mure refresh has the feature of removing merged branches.
However, in some cases it did not work well.
This pull request fixes it.

Specifically, it means avoiding early returns after judging the repository clean.
This will allow deletion of previously skipped branches to work properly.
  • Loading branch information
kitsuyui committed Nov 4, 2022
1 parent b31bb6f commit f938230
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/app/clone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn clone(config: &Config, repo_url: &str) -> Result<(), Error> {
let result = Command::new("git")
.current_dir(tobe_clone.parent().unwrap())
.arg("clone")
.arg(&repo_url)
.arg(repo_url)
.output()?;

if !result.status.success() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/issues/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn show_issues(query: &str) -> Result<(), Error> {
"{}\t{}\t{}\t{}",
result.number_of_issues,
result.number_of_pull_requests,
result.default_branch_name.unwrap_or_else(|| "".to_string()),
result.default_branch_name.unwrap_or_default(),
result.url
);
}
Expand Down
19 changes: 14 additions & 5 deletions src/app/refresh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Reason {
}

pub fn refresh(repo_path: &str) -> Result<RefreshStatus, Error> {
let mut messages = vec![];
if !PathBuf::from(repo_path).join(".git").exists() {
return Ok(RefreshStatus::DoNothing(Reason::NotGitRepository));
}
Expand All @@ -39,15 +40,22 @@ pub fn refresh(repo_path: &str) -> Result<RefreshStatus, Error> {

// TODO: origin is hardcoded. If you have multiple remotes, you need to specify which one to use.
repo.pull_fast_forwarded("origin", &default_branch)?;
messages.push(format!(
"Pulled from origin/{} into {}",
default_branch, default_branch
));

// switch to default branch if current branch is clean
if repo.is_clean()? {
// git switch $default_branch
let result = repo.switch(&default_branch)?;
return Ok(RefreshStatus::Update {
switch_to_default: true,
message: String::from_utf8(result.stdout).unwrap(),
});
if result.status.success() {
messages.push(format!("Switched to {}", default_branch));
} else {
let message =
String::from_utf8(result.stdout).map_err(|e| Error::from_str(&e.to_string()))?;
messages.push(message);
}
}

let merged_branches = repo.merged_branches()?;
Expand All @@ -58,11 +66,12 @@ pub fn refresh(repo_path: &str) -> Result<RefreshStatus, Error> {

for branch in delete_branches {
repo.delete_branch(branch)?;
messages.push(format!("Deleted branch {}", branch));
}

Ok(RefreshStatus::Update {
switch_to_default: false,
message: String::from(""),
message: messages.join("\n"),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/gh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::process::Command;

pub fn get_default_branch() -> Result<String, Error> {
let result = Command::new("gh")
.args(&[
.args([
"repo",
"view",
"--json",
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ fn main() {
None => current_dir.to_str().unwrap().to_string(),
};
match app::refresh::refresh(&repo_path) {
Ok(_) => (),
Ok(r) => {
if let app::refresh::RefreshStatus::Update { message, .. } = r {
println!("{}", message);
}
}
Err(e) => println!("{}", e),
}
}
Expand Down

0 comments on commit f938230

Please sign in to comment.