Skip to content

Commit

Permalink
feat: set current branch to source branch (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
djego authored Sep 29, 2024
1 parent 7c0da3d commit 561ea77
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cd prt
Then, create a `.env` file with the following content:
````bash
GITHUB_TOKEN=your_github_token
GITHUB_DEFAULT_BRANCH=your_github_default_branch # if not set, it will be 'main'
GITHUB_DEFAULT_TARGET_BRANCH=your_github_default_target_branch # if not set, it will be 'main'
````
Finally, use the following commands:
````rust
Expand Down
9 changes: 5 additions & 4 deletions src/core/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::core::errors::PullRequestError;
use crate::core::git::get_repo_info;
use crate::core::git::{get_current_branch, get_repo_info};
use crate::core::input_mode::InputMode;
use crate::core::pull_request::PullRequest;
use octocrab::models::pulls::PullRequest as OctocrabPullRequest;
Expand All @@ -23,16 +23,17 @@ impl App {
Some((owner, repo)) => (owner, repo),
None => ("-".to_string(), "-".to_string()),
};
let current_branch = get_current_branch().unwrap_or_else(|| "-".to_string());
App {
pull_request: PullRequest::new(),
pull_request: PullRequest::new(current_branch.clone()),
input_mode: InputMode::Normal,
current_field: 0,
show_popup: false,
error_message: None,
success_message: None,
repo_owner,
repo_name,
default_branch: std::env::var("GITHUB_DEFAULT_BRANCH")
default_branch: std::env::var("GITHUB_DEFAULT_TARGET_BRANCH")
.unwrap_or_else(|_| "main".to_string()),
}
}
Expand Down Expand Up @@ -107,7 +108,7 @@ impl App {
}

pub fn reset(&mut self) {
self.pull_request = PullRequest::new();
self.pull_request = PullRequest::new(self.pull_request.source_branch.clone());
self.input_mode = InputMode::Normal;
self.current_field = 0;
self.show_popup = false;
Expand Down
2 changes: 1 addition & 1 deletion src/core/app_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
#[test]
fn test_app_initialization() {
//let (repo_owner, repo_name) = mock_get_repo_info().unwrap();
env::set_var("GITHUB_DEFAULT_BRANCH", "main");
env::set_var("GITHUB_DEFAULT_TARGET_BRANCH", "main");
let app = App::new();

//assert_eq!(app.repo_owner, repo_owner);
Expand Down
16 changes: 16 additions & 0 deletions src/core/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ fn parse_git_url(url: &str) -> Option<(&str, &str)> {
}
None
}

pub fn get_current_branch() -> Option<String> {
let output = Command::new("git")
.arg("rev-parse")
.arg("--abbrev-ref")
.arg("HEAD")
.output()
.expect("Failed to execute git command");

if output.status.success() {
let branch = str::from_utf8(&output.stdout).unwrap().trim();
return Some(branch.to_string());
}

None
}
6 changes: 3 additions & 3 deletions src/core/pull_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ pub struct PullRequest {
}

impl PullRequest {
pub fn new() -> PullRequest {
pub fn new(current_branch: String) -> PullRequest {
PullRequest {
title: String::new(),
description: String::new(),
source_branch: String::new(),
target_branch: std::env::var("GITHUB_DEFAULT_BRANCH")
source_branch: current_branch,
target_branch: std::env::var("GITHUB_DEFAULT_TARGET_BRANCH")
.unwrap_or_else(|_| "main".to_string()),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ fn main() -> Result<(), io::Error> {
}
}
}
KeyCode::Char('e') => {
KeyCode::Char('e') | KeyCode::Char('n') => {
app.input_mode = InputMode::Editing;
app.show_popup = false;
}
KeyCode::Char('q') | KeyCode::Char('n') => {
KeyCode::Char('q') => {
app.input_mode = InputMode::Normal;
app.show_popup = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn ui(f: &mut Frame, app: &App) {
.borders(Borders::ALL)
.style(Style::default().bg(Color::Blue));

let area = centered_rect(30, 12, f.area());
let area = centered_rect(35, 12, f.area());
f.render_widget(Clear, area);
f.render_widget(popup_block, area);

Expand Down

0 comments on commit 561ea77

Please sign in to comment.