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
4615f0f7
Commit
4615f0f7
authored
Apr 09, 2012
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
branch: add git_branch_move()
parent
555aa453
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
111 additions
and
0 deletions
+111
-0
include/git2/branch.h
+22
-0
src/branch.c
+18
-0
src/refs.c
+9
-0
tests-clar/refs/branches/move.c
+62
-0
No files found.
include/git2/branch.h
View file @
4615f0f7
...
@@ -95,6 +95,28 @@ GIT_EXTERN(int) git_branch_list(
...
@@ -95,6 +95,28 @@ GIT_EXTERN(int) git_branch_list(
git_repository
*
repo
,
git_repository
*
repo
,
unsigned
int
list_flags
);
unsigned
int
list_flags
);
/**
* Move/rename an existing branch reference.
*
* @param repo Repository where lives the branch.
*
* @param old_branch_name Current name of the branch to be moved;
* this name is validated for consistency.
*
* @param new_branch_name Target name of the branch once the move
* is performed; this name is validated for consistency.
*
* @param force Overwrite existing branch.
*
* @return GIT_SUCCESS on success, GIT_ENOTFOUND if the branch
* doesn't exist or an error code.
*/
GIT_EXTERN
(
int
)
git_branch_move
(
git_repository
*
repo
,
const
char
*
old_branch_name
,
const
char
*
new_branch_name
,
int
force
);
/** @} */
/** @} */
GIT_END_DECL
GIT_END_DECL
#endif
#endif
src/branch.c
View file @
4615f0f7
...
@@ -178,3 +178,21 @@ int git_branch_list(git_strarray *branch_names, git_repository *repo, unsigned i
...
@@ -178,3 +178,21 @@ int git_branch_list(git_strarray *branch_names, git_repository *repo, unsigned i
branch_names
->
count
=
branchlist
.
length
;
branch_names
->
count
=
branchlist
.
length
;
return
0
;
return
0
;
}
}
int
git_branch_move
(
git_repository
*
repo
,
const
char
*
old_branch_name
,
const
char
*
new_branch_name
,
int
force
)
{
git_reference
*
reference
;
git_buf
old_reference_name
=
GIT_BUF_INIT
,
new_reference_name
=
GIT_BUF_INIT
;
int
error
;
if
(
git_buf_joinpath
(
&
old_reference_name
,
GIT_REFS_HEADS_DIR
,
old_branch_name
)
<
0
)
return
-
1
;
if
(
git_buf_joinpath
(
&
new_reference_name
,
GIT_REFS_HEADS_DIR
,
new_branch_name
)
<
0
)
return
-
1
;
if
((
error
=
git_reference_lookup
(
&
reference
,
repo
,
git_buf_cstr
(
&
old_reference_name
)))
<
0
)
return
error
;
return
git_reference_rename
(
reference
,
git_buf_cstr
(
&
new_reference_name
),
force
);
}
src/refs.c
View file @
4615f0f7
...
@@ -287,6 +287,15 @@ static int loose_write(git_reference *ref)
...
@@ -287,6 +287,15 @@ static int loose_write(git_reference *ref)
if
(
git_buf_joinpath
(
&
ref_path
,
ref
->
owner
->
path_repository
,
ref
->
name
)
<
0
)
if
(
git_buf_joinpath
(
&
ref_path
,
ref
->
owner
->
path_repository
,
ref
->
name
)
<
0
)
return
-
1
;
return
-
1
;
/* Remove a possibly existing empty directory hierarchy
* which name would collide with the reference name
*/
if
(
git_path_isdir
(
git_buf_cstr
(
&
ref_path
))
&&
(
git_futils_rmdir_r
(
git_buf_cstr
(
&
ref_path
),
GIT_DIRREMOVAL_ONLY_EMPTY_DIRS
)
<
0
))
{
git_buf_free
(
&
ref_path
);
return
-
1
;
}
if
(
git_filebuf_open
(
&
file
,
ref_path
.
ptr
,
GIT_FILEBUF_FORCE
)
<
0
)
{
if
(
git_filebuf_open
(
&
file
,
ref_path
.
ptr
,
GIT_FILEBUF_FORCE
)
<
0
)
{
git_buf_free
(
&
ref_path
);
git_buf_free
(
&
ref_path
);
return
-
1
;
return
-
1
;
...
...
tests-clar/refs/branches/move.c
0 → 100644
View file @
4615f0f7
#include "clar_libgit2.h"
#include "branch.h"
static
git_repository
*
repo
;
void
test_refs_branches_move__initialize
(
void
)
{
cl_fixture_sandbox
(
"testrepo.git"
);
cl_git_pass
(
git_repository_open
(
&
repo
,
"testrepo.git"
));
}
void
test_refs_branches_move__cleanup
(
void
)
{
git_repository_free
(
repo
);
cl_fixture_cleanup
(
"testrepo.git"
);
}
#define NEW_BRANCH_NAME "new-branch-on-the-block"
void
test_refs_branches_move__can_move_a_local_branch
(
void
)
{
cl_git_pass
(
git_branch_move
(
repo
,
"br2"
,
NEW_BRANCH_NAME
,
0
));
}
void
test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace
(
void
)
{
/* Downward */
cl_git_pass
(
git_branch_move
(
repo
,
"br2"
,
"somewhere/"
NEW_BRANCH_NAME
,
0
));
/* Upward */
cl_git_pass
(
git_branch_move
(
repo
,
"somewhere/"
NEW_BRANCH_NAME
,
"br2"
,
0
));
}
void
test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace
(
void
)
{
/* Downward */
cl_git_pass
(
git_branch_move
(
repo
,
"br2"
,
"br2/"
NEW_BRANCH_NAME
,
0
));
/* Upward */
cl_git_pass
(
git_branch_move
(
repo
,
"br2/"
NEW_BRANCH_NAME
,
"br2"
,
0
));
}
void
test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one
(
void
)
{
cl_git_fail
(
git_branch_move
(
repo
,
"br2"
,
"master"
,
0
));
}
void
test_refs_branches_move__can_not_move_a_non_existing_branch
(
void
)
{
cl_git_fail
(
git_branch_move
(
repo
,
"i-am-no-branch"
,
NEW_BRANCH_NAME
,
0
));
}
void
test_refs_branches_move__can_force_move_over_an_existing_branch
(
void
)
{
cl_git_pass
(
git_branch_move
(
repo
,
"br2"
,
"master"
,
1
));
}
void
test_refs_branches_move__can_not_move_a_branch_through_its_canonical_name
(
void
)
{
cl_git_fail
(
git_branch_move
(
repo
,
"refs/heads/br2"
,
NEW_BRANCH_NAME
,
1
));
}
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