git_x/
rename_branch.rs

1use std::process::{Command, exit};
2
3pub fn run(new_name: &str) {
4    // Step 1: Get current branch name
5    let output = Command::new("git")
6        .args(get_current_branch_args())
7        .output()
8        .expect("Failed to execute git");
9
10    if !output.status.success() {
11        eprintln!("Error: Failed to get current branch name.");
12        exit(1);
13    }
14
15    let current_branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
16
17    if is_branch_already_named(&current_branch, new_name) {
18        println!("{}", format_already_named_message(new_name));
19        return;
20    }
21
22    println!("{}", format_rename_start_message(&current_branch, new_name));
23
24    // Step 2: Rename branch locally
25    let status = Command::new("git")
26        .args(get_local_rename_args(new_name))
27        .status()
28        .expect("Failed to rename branch");
29
30    if !status.success() {
31        eprintln!("Error: Failed to rename local branch.");
32        exit(1);
33    }
34
35    // Step 3: Push the new branch to origin
36    let status = Command::new("git")
37        .args(get_push_new_branch_args(new_name))
38        .status()
39        .expect("Failed to push new branch");
40
41    if !status.success() {
42        eprintln!("Error: Failed to push new branch to origin.");
43        exit(1);
44    }
45
46    // Step 4: Delete the old branch from origin
47    let status = Command::new("git")
48        .args(get_delete_old_branch_args(&current_branch))
49        .status()
50        .expect("Failed to delete old branch");
51
52    if !status.success() {
53        eprintln!("{}", format_delete_failed_message(&current_branch));
54    } else {
55        println!("{}", format_delete_success_message(&current_branch));
56    }
57
58    println!("{}", format_rename_success_message());
59}
60
61// Helper function to get current branch args
62pub fn get_current_branch_args() -> [&'static str; 3] {
63    ["rev-parse", "--abbrev-ref", "HEAD"]
64}
65
66// Helper function to check if branch is already named
67pub fn is_branch_already_named(current_branch: &str, new_name: &str) -> bool {
68    current_branch == new_name
69}
70
71// Helper function to get local rename args
72pub fn get_local_rename_args(new_name: &str) -> Vec<String> {
73    vec!["branch".to_string(), "-m".to_string(), new_name.to_string()]
74}
75
76// Helper function to get push new branch args
77pub fn get_push_new_branch_args(new_name: &str) -> Vec<String> {
78    vec![
79        "push".to_string(),
80        "-u".to_string(),
81        "origin".to_string(),
82        new_name.to_string(),
83    ]
84}
85
86// Helper function to get delete old branch args
87pub fn get_delete_old_branch_args(old_branch: &str) -> Vec<String> {
88    vec![
89        "push".to_string(),
90        "origin".to_string(),
91        "--delete".to_string(),
92        old_branch.to_string(),
93    ]
94}
95
96// Helper function to format already named message
97pub fn format_already_named_message(new_name: &str) -> String {
98    format!("Current branch is already named '{new_name}'. Nothing to do.")
99}
100
101// Helper function to format rename start message
102pub fn format_rename_start_message(current_branch: &str, new_name: &str) -> String {
103    format!("Renaming branch '{current_branch}' to '{new_name}'")
104}
105
106// Helper function to format delete failed message
107pub fn format_delete_failed_message(old_branch: &str) -> String {
108    format!("Warning: Failed to delete old branch '{old_branch}' from origin.")
109}
110
111// Helper function to format delete success message
112pub fn format_delete_success_message(old_branch: &str) -> String {
113    format!("Deleted old branch '{old_branch}' from origin.")
114}
115
116// Helper function to format rename success message
117pub fn format_rename_success_message() -> &'static str {
118    "Branch renamed successfully."
119}