File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -2740,6 +2740,7 @@ extern "C" {
2740
2740
force : c_int ,
2741
2741
) -> c_int ;
2742
2742
pub fn git_branch_name ( out : * mut * const c_char , branch : * const git_reference ) -> c_int ;
2743
+ pub fn git_branch_name_is_valid ( valid : * mut c_int , name : * const c_char ) -> c_int ;
2743
2744
pub fn git_branch_remote_name (
2744
2745
out : * mut git_buf ,
2745
2746
repo : * mut git_repository ,
Original file line number Diff line number Diff line change @@ -28,6 +28,16 @@ impl<'repo> Branch<'repo> {
28
28
Branch { inner : reference }
29
29
}
30
30
31
+ /// Ensure the branch name is well-formed.
32
+ pub fn name_is_valid ( name : & str ) -> Result < bool , Error > {
33
+ let name = CString :: new ( name) ?;
34
+ let mut valid: libc:: c_int = 0 ;
35
+ unsafe {
36
+ try_call ! ( raw:: git_branch_name_is_valid( & mut valid, name. as_ptr( ) ) ) ;
37
+ }
38
+ Ok ( valid == 1 )
39
+ }
40
+
31
41
/// Gain access to the reference that is this branch
32
42
pub fn get ( & self ) -> & Reference < ' repo > {
33
43
& self . inner
@@ -151,7 +161,7 @@ impl<'repo> Drop for Branches<'repo> {
151
161
152
162
#[ cfg( test) ]
153
163
mod tests {
154
- use crate :: BranchType ;
164
+ use crate :: { Branch , BranchType } ;
155
165
156
166
#[ test]
157
167
fn smoke ( ) {
@@ -175,4 +185,12 @@ mod tests {
175
185
176
186
b1. delete ( ) . unwrap ( ) ;
177
187
}
188
+
189
+ #[ test]
190
+ fn name_is_valid ( ) {
191
+ assert ! ( Branch :: name_is_valid( "foo" ) . unwrap( ) ) ;
192
+ assert ! ( !Branch :: name_is_valid( "" ) . unwrap( ) ) ;
193
+ assert ! ( !Branch :: name_is_valid( "with spaces" ) . unwrap( ) ) ;
194
+ assert ! ( !Branch :: name_is_valid( "~tilde" ) . unwrap( ) ) ;
195
+ }
178
196
}
You can’t perform that action at this time.
0 commit comments