Skip to content

Commit 50d6792

Browse files
committed
fix(github): add current branch info to github status command
Fixes bounty issue #1232 The 'cortex github status' command now displays the current git branch in both text and JSON output.
1 parent 7a104aa commit 50d6792

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

cortex-cli/src/github_cmd.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ async fn run_status(args: StatusArgs) -> Result<()> {
570570
// Check if we're in a git repo
571571
status.is_git_repo = repo_path.join(".git").exists();
572572

573+
// Get current branch if in a git repo
574+
if status.is_git_repo {
575+
status.current_branch = get_current_branch(&repo_path);
576+
}
577+
573578
if args.json {
574579
let json = serde_json::to_string_pretty(&status)?;
575580
println!("{}", json);
@@ -586,6 +591,12 @@ async fn run_status(args: StatusArgs) -> Result<()> {
586591
return Ok(());
587592
}
588593

594+
// Display current branch
595+
if let Some(ref branch) = status.current_branch {
596+
println!("Branch: {}", branch);
597+
println!();
598+
}
599+
589600
if !status.github_dir_exists {
590601
println!("❌ .github directory not found");
591602
println!(" Run `Cortex github install` to set up GitHub Actions.");
@@ -614,12 +625,30 @@ async fn run_status(args: StatusArgs) -> Result<()> {
614625
#[derive(Debug, Default, serde::Serialize)]
615626
struct InstallationStatus {
616627
is_git_repo: bool,
628+
current_branch: Option<String>,
617629
github_dir_exists: bool,
618630
workflow_installed: bool,
619631
workflow_path: Option<PathBuf>,
620632
features: Vec<String>,
621633
}
622634

635+
/// Get the current git branch name from the repository.
636+
fn get_current_branch(repo_path: &std::path::Path) -> Option<String> {
637+
let head_path = repo_path.join(".git").join("HEAD");
638+
if let Ok(content) = std::fs::read_to_string(&head_path) {
639+
let content = content.trim();
640+
// HEAD file contains "ref: refs/heads/<branch>" for normal branches
641+
if let Some(branch) = content.strip_prefix("ref: refs/heads/") {
642+
return Some(branch.to_string());
643+
}
644+
// If HEAD is detached, it contains a commit hash
645+
if content.len() == 40 && content.chars().all(|c| c.is_ascii_hexdigit()) {
646+
return Some(format!("(detached at {})", &content[..7]));
647+
}
648+
}
649+
None
650+
}
651+
623652
#[cfg(test)]
624653
mod tests {
625654
use super::*;
@@ -639,4 +668,19 @@ mod tests {
639668
assert!(help.contains("/Cortex help"));
640669
assert!(help.contains("/Cortex review"));
641670
}
671+
672+
#[test]
673+
fn test_get_current_branch() {
674+
// Test with current repo - should return Some branch name
675+
let current_dir = std::path::PathBuf::from(".");
676+
if current_dir.join(".git").exists() {
677+
let branch = get_current_branch(&current_dir);
678+
assert!(branch.is_some(), "Should detect branch in git repo");
679+
}
680+
681+
// Test with non-existent path - should return None
682+
let non_existent = std::path::PathBuf::from("/non/existent/path");
683+
let branch = get_current_branch(&non_existent);
684+
assert!(branch.is_none(), "Should return None for non-existent path");
685+
}
642686
}

0 commit comments

Comments
 (0)