diff options
author | Sebastian Bauer <mail@sebastianbauer.info> | 2013-01-24 23:44:17 +0400 |
---|---|---|
committer | Sebastian Bauer <mail@sebastianbauer.info> | 2013-01-25 08:24:21 +0400 |
commit | c253056d2429ea0a6201be60921dbac69dbcc98a (patch) | |
tree | 937977952cb53a57ff773597e8b47dac9edcbd48 /src/branch.c | |
parent | 5425097f0368c43e72210c33b844cf7350843c37 (diff) |
Added git_branch_name().
This is a convenience function to get the branch name of a given
ref. The returned branch name is compatible with the name that can
be supplied e.g. to git_branch_lookup(). That is, the prefixes
"refs/heads" or "refs/remotes" are omitted.
Also added a new test for testing the new function.
Diffstat (limited to 'src/branch.c')
-rw-r--r-- | src/branch.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/branch.c b/src/branch.c index 65c02b8af..3959409c5 100644 --- a/src/branch.c +++ b/src/branch.c @@ -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, |