Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
fe11160c
Commit
fe11160c
authored
Sep 08, 2020
by
Sven Strickroth
Committed by
Edward Thomson
Oct 25, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add git_branch_name_is_valid
Signed-off-by: Sven Strickroth <email@cs-ware.de>
parent
c7143d7c
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
0 deletions
+58
-0
include/git2/branch.h
+12
-0
src/branch.c
+29
-0
tests/refs/branches/name.c
+17
-0
No files found.
include/git2/branch.h
View file @
fe11160c
...
@@ -304,6 +304,18 @@ GIT_EXTERN(int) git_branch_remote_name(
...
@@ -304,6 +304,18 @@ GIT_EXTERN(int) git_branch_remote_name(
*/
*/
GIT_EXTERN
(
int
)
git_branch_upstream_remote
(
git_buf
*
buf
,
git_repository
*
repo
,
const
char
*
refname
);
GIT_EXTERN
(
int
)
git_branch_upstream_remote
(
git_buf
*
buf
,
git_repository
*
repo
,
const
char
*
refname
);
/**
* Determine whether a branch name is valid, meaning that (when prefixed
* with `refs/heads/`) that it is a valid reference name, and that any
* additional branch name restrictions are imposed (eg, it cannot start
* with a `-`).
*
* @param valid output pointer to set with validity of given branch name
* @param name a branch name to test
* @return 0 on success or an error code
*/
GIT_EXTERN
(
int
)
git_branch_name_is_valid
(
int
*
valid
,
const
char
*
name
);
/** @} */
/** @} */
GIT_END_DECL
GIT_END_DECL
#endif
#endif
src/branch.c
View file @
fe11160c
...
@@ -723,3 +723,32 @@ int git_branch_is_head(
...
@@ -723,3 +723,32 @@ int git_branch_is_head(
return
is_same
;
return
is_same
;
}
}
int
git_branch_name_is_valid
(
int
*
valid
,
const
char
*
name
)
{
git_buf
ref_name
=
GIT_BUF_INIT
;
int
error
=
0
;
GIT_ASSERT
(
valid
);
*
valid
=
0
;
/*
* Discourage branch name starting with dash,
* https://github.com/git/git/commit/6348624010888b
* and discourage HEAD as branch name,
* https://github.com/git/git/commit/a625b092cc5994
*/
if
(
!
name
||
name
[
0
]
==
'-'
||
!
git__strcmp
(
name
,
"HEAD"
))
goto
done
;
if
((
error
=
git_buf_puts
(
&
ref_name
,
GIT_REFS_HEADS_DIR
))
<
0
||
(
error
=
git_buf_puts
(
&
ref_name
,
name
))
<
0
)
goto
done
;
error
=
git_reference_name_is_valid
(
valid
,
ref_name
.
ptr
);
done:
git_buf_dispose
(
&
ref_name
);
return
error
;
}
tests/refs/branches/name.c
View file @
fe11160c
...
@@ -43,3 +43,20 @@ void test_refs_branches_name__error_when_ref_is_no_branch(void)
...
@@ -43,3 +43,20 @@ void test_refs_branches_name__error_when_ref_is_no_branch(void)
cl_git_pass
(
git_reference_lookup
(
&
ref
,
repo
,
"refs/notes/fanout"
));
cl_git_pass
(
git_reference_lookup
(
&
ref
,
repo
,
"refs/notes/fanout"
));
cl_git_fail
(
git_branch_name
(
&
name
,
ref
));
cl_git_fail
(
git_branch_name
(
&
name
,
ref
));
}
}
static
int
name_is_valid
(
const
char
*
name
)
{
int
valid
;
cl_git_pass
(
git_branch_name_is_valid
(
&
valid
,
name
));
return
valid
;
}
void
test_refs_branches_is_name_valid
(
void
)
{
cl_assert_equal_i
(
true
,
name_is_valid
(
"master"
));
cl_assert_equal_i
(
true
,
name_is_valid
(
"test/master"
));
cl_assert_equal_i
(
false
,
name_is_valid
(
""
));
cl_assert_equal_i
(
false
,
name_is_valid
(
"HEAD"
));
cl_assert_equal_i
(
false
,
name_is_valid
(
"-dash"
));
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment