Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bendzae committed Nov 25, 2024
1 parent 80977fc commit a8e0ed9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
20 changes: 17 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gstack"
version = "0.0.3"
version = "0.0.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -11,7 +11,7 @@ clap = { version = "4.5.11", features = ["derive"] }
console = "0.15.8"
dialoguer = "0.11.0"
indicatif = "0.17.8"
octocrab = "0.41.2"
octocrab = "0.42.1"
ron = "0.8.1"
toml = "0.8.16"
rustygit = "0.5.0"
Expand Down
16 changes: 15 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Stacked branches and github PR's

> [!CAUTION]
> Heavy work in progress
> Work in progress
G-stack is a CLI util to simplify creating stacked branches and pull requests on github.

Expand Down Expand Up @@ -70,3 +70,17 @@ gs change
#or
gs c
```

### Working with github pull requests

Create github pr's for all stack branches that don't have a pr yet:

```bash
gs pr new
```

Merge all stack pr's in sequence to the stacks base branch (takes care of rebases in between merges):

```bash
gs pr merge
```
1 change: 1 addition & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ pub enum PrCommands {
#[clap(alias = "ls")]
/// List open PR's for all stack branches
List {},
/// Merge all stack pr's in sequence to the stacks base branch
Merge {},
}
49 changes: 32 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::panic;
use std::{path::PathBuf, str::FromStr, sync::Arc, thread::current};
use std::{path::PathBuf, str::FromStr, sync::Arc, thread::current, time::Duration};

use clap::Parser;
use console::{pad_str, style};
Expand All @@ -11,6 +11,7 @@ use octocrab::{
Octocrab,
};
use rustygit::types::BranchName;
use tokio::time::sleep;

use crate::{
command::{Cli, Commands},
Expand Down Expand Up @@ -320,6 +321,7 @@ impl GsContext {
}

async fn create_pull_requests(&self) -> Result<()> {
self.sync(false).await?;
let stack = &self.current_stack().unwrap();
let branches = &stack.branches;
let remote = self.repo.remote_repo_info()?;
Expand Down Expand Up @@ -356,7 +358,11 @@ impl GsContext {
.body("---")
.send()
.await?;
println!("#{}: {}", pr.number, pr.html_url.clone().unwrap());
println!(
"#{}: {}",
pr.number,
style(pr.html_url.clone().unwrap()).blue()
);
created_pulls.push(pr);
}

Expand Down Expand Up @@ -444,30 +450,36 @@ impl GsContext {
let github = self.github.clone();
let pulls = github.pulls(remote.owner, remote.name);
let open_pulls = self.get_pull_requests().await?;

let merge_method = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Merge method")
.default(0)
.items(&["Squash", "Merge", "Rebase"])
.interact()
.unwrap();

let merge_method = match merge_method {
0 => MergeMethod::Squash,
1 => MergeMethod::Merge,
2 => MergeMethod::Rebase,
_ => panic!("Unknown merge method"),
};
let base = self.current_stack().unwrap().base_branch.clone();

// TODO: fix for Squash and Rebase
// let merge_method = Select::with_theme(&ColorfulTheme::default())
// .with_prompt("Merge method")
// .default(0)
// .items(&["Squash", "Merge", "Rebase"])
// .interact()
// .unwrap();
//
// let merge_method = match merge_method {
// 0 => MergeMethod::Squash,
// 1 => MergeMethod::Merge,
// 2 => MergeMethod::Rebase,
// _ => panic!("Unknown merge method"),
// };
let merge_method = MergeMethod::Merge;

let mut orginal_branches = vec![];
for pr in &open_pulls {
pulls.update(pr.number).base(base.clone()).send().await?;
self.sync(false).await?;
println!("Merging PR #{}...", pr.number);
let branch = self.get_pr_branch(pr);
pulls.merge(pr.number).method(merge_method).send().await?;
// Not the best way to do this should try to listen to the completed merge somehow
sleep(Duration::from_secs(3)).await;
if let Some(branch) = branch {
orginal_branches.push(branch.clone());
self.remove_branch_from_stack(&branch)?;
self.sync(false).await?;
}
}

Expand All @@ -488,6 +500,7 @@ impl GsContext {
}

fn remove_branch_from_stack(&mut self, branch: &String) -> Result<()> {
println!("Removing branch: {}", branch);
if !self.current_stack().unwrap().branches.contains(branch) {
println!("Unknown stack branch, not removing");
return Ok(());
Expand Down Expand Up @@ -525,7 +538,9 @@ impl GsContext {

if self.state.stacks[stack_idx].branches.is_empty() {
self.state.stacks.remove(stack_idx);
self.state.write(self.base_path.clone())?;
}
println!("Removed branch {}", branch);
Ok(())
}

Expand Down

0 comments on commit a8e0ed9

Please sign in to comment.