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
c9e96403
Commit
c9e96403
authored
Jan 24, 2013
by
Vicent Martí
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1277 from sba1/branch-name
Add git_branch_name()
parents
5425097f
c253056d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
0 deletions
+84
-0
include/git2/branch.h
+18
-0
src/branch.c
+21
-0
tests-clar/refs/branches/name.c
+45
-0
No files found.
include/git2/branch.h
View file @
c9e96403
...
...
@@ -142,6 +142,24 @@ GIT_EXTERN(int) git_branch_lookup(
git_branch_t
branch_type
);
/**
* Return the name of the given local or remote branch.
*
* The name of the branch matches the definition of the name
* for git_branch_lookup. That is, if the returned name is given
* to git_branch_lookup() then the reference is returned that
* was given to this function.
*
* @param out where the pointer of branch name is stored;
* this is valid as long as the ref is not freed.
* @param ref the reference ideally pointing to a branch
*
* @return 0 on success; otherwise an error code (e.g., if the
* ref is no local or remote branch).
*/
GIT_EXTERN
(
int
)
git_branch_name
(
const
char
**
out
,
git_reference
*
ref
);
/**
* Return the reference supporting the remote tracking branch,
* given a local branch reference.
*
...
...
src/branch.c
View file @
c9e96403
...
...
@@ -221,6 +221,27 @@ int git_branch_lookup(
return
retrieve_branch_reference
(
ref_out
,
repo
,
branch_name
,
branch_type
==
GIT_BRANCH_REMOTE
);
}
int
git_branch_name
(
const
char
**
out
,
git_reference
*
ref
)
{
const
char
*
branch_name
;
assert
(
out
&&
ref
);
branch_name
=
ref
->
name
;
if
(
git_reference_is_branch
(
ref
))
{
branch_name
+=
strlen
(
GIT_REFS_HEADS_DIR
);
}
else
if
(
git_reference_is_remote
(
ref
))
{
branch_name
+=
strlen
(
GIT_REFS_REMOTES_DIR
);
}
else
{
giterr_set
(
GITERR_INVALID
,
"Reference '%s' is neither a local nor a remote branch."
,
ref
->
name
);
return
-
1
;
}
*
out
=
branch_name
;
return
0
;
}
static
int
retrieve_tracking_configuration
(
const
char
**
out
,
git_repository
*
repo
,
...
...
tests-clar/refs/branches/name.c
0 → 100644
View file @
c9e96403
#include "clar_libgit2.h"
#include "branch.h"
static
git_repository
*
repo
;
static
git_reference
*
ref
;
void
test_refs_branches_name__initialize
(
void
)
{
cl_git_pass
(
git_repository_open
(
&
repo
,
cl_fixture
(
"testrepo.git"
)));
}
void
test_refs_branches_name__cleanup
(
void
)
{
git_reference_free
(
ref
);
ref
=
NULL
;
git_repository_free
(
repo
);
repo
=
NULL
;
}
void
test_refs_branches_name__can_get_local_branch_name
(
void
)
{
const
char
*
name
;
cl_git_pass
(
git_branch_lookup
(
&
ref
,
repo
,
"master"
,
GIT_BRANCH_LOCAL
));
cl_git_pass
(
git_branch_name
(
&
name
,
ref
));
cl_assert_equal_s
(
"master"
,
name
);
}
void
test_refs_branches_name__can_get_remote_branch_name
(
void
)
{
const
char
*
name
;
cl_git_pass
(
git_branch_lookup
(
&
ref
,
repo
,
"test/master"
,
GIT_BRANCH_REMOTE
));
cl_git_pass
(
git_branch_name
(
&
name
,
ref
));
cl_assert_equal_s
(
"test/master"
,
name
);
}
void
test_refs_branches_name__error_when_ref_is_no_branch
(
void
)
{
const
char
*
name
;
cl_git_pass
(
git_reference_lookup
(
&
ref
,
repo
,
"refs/notes/fanout"
));
cl_git_fail
(
git_branch_name
(
&
name
,
ref
));
}
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