gnostr_asyncgit/sync/branch/
rename.rs1use scopetime::scope_time;
4
5use crate::{
6 error::Result,
7 sync::{RepoPath, repository::repo},
8};
9
10pub fn rename_branch(
12 repo_path: &RepoPath,
13 branch_ref: &str,
14 new_name: &str,
15) -> Result<()> {
16 scope_time!("rename_branch");
17
18 let repo = repo(repo_path)?;
19 let branch_as_ref = repo.find_reference(branch_ref)?;
20 let mut branch = git2::Branch::wrap(branch_as_ref);
21 branch.rename(new_name, true)?;
22
23 Ok(())
24}
25
26#[cfg(test)]
27mod test {
28 use super::{
29 super::{RepoPath, checkout_branch, create_branch},
30 rename_branch,
31 };
32 use crate::sync::tests::repo_init;
33
34 #[test]
35 fn test_rename_branch() {
36 let (_td, repo) = repo_init().unwrap();
37 let root = repo.path().parent().unwrap();
38 let repo_path: &RepoPath =
39 &root.as_os_str().to_str().unwrap().into();
40
41 create_branch(repo_path, "branch1").unwrap();
42
43 checkout_branch(repo_path, "branch1").unwrap();
44
45 assert_eq!(
46 repo.branches(None)
47 .unwrap()
48 .next()
49 .unwrap()
50 .unwrap()
51 .0
52 .name()
53 .unwrap()
54 .unwrap(),
55 "branch1"
56 );
57
58 rename_branch(repo_path, "refs/heads/branch1", "AnotherName")
59 .unwrap();
60
61 assert_eq!(
62 repo.branches(None)
63 .unwrap()
64 .next()
65 .unwrap()
66 .unwrap()
67 .0
68 .name()
69 .unwrap()
70 .unwrap(),
71 "AnotherName"
72 );
73 }
74}