Browse files

branch: use libgit2 git_branch_name for .name

  • Loading branch information...
1 parent 693c112 commit 2330c49ba845754423e1b8638652f2ff7abf0331 @nvloff nvloff committed May 6, 2013
Showing with 22 additions and 7 deletions.
  1. +18 −0 ext/rugged/rugged_branch.c
  2. +2 −7 lib/rugged/branch.rb
  3. +1 −0 lib/rugged/reference.rb
  4. +1 −0 test/reference_test.rb
View
18 ext/rugged/rugged_branch.c
@@ -304,6 +304,23 @@ static VALUE rb_git_branch_head_p(VALUE self)
return git_branch_is_head(branch) ? Qtrue : Qfalse;
}
+/* call-seq:
+ * name -> string
+ *
+ * The name of the branch, without a fully-qualified reference path
+ *
+ * E.g. 'master' instead of 'refs/heads/master'
+ */
+static VALUE rb_git_branch_name(VALUE self)
+{
+ git_reference *branch;
+ const char *branch_name;
+ Data_Get_Struct(self, git_reference, branch);
+
+ git_branch_name(&branch_name, branch);
+ return rb_str_new_utf8(branch_name);
+}
+
static VALUE rb_git_branch__remote_name(VALUE rb_repo, const char *canonical_name)
{
git_repository *repo;
@@ -371,5 +388,6 @@ void Init_rugged_branch(void)
rb_define_method(rb_cRuggedBranch, "rename", rb_git_branch_move, -1);
rb_define_method(rb_cRuggedBranch, "move", rb_git_branch_move, -1);
rb_define_method(rb_cRuggedBranch, "head?", rb_git_branch_head_p, 0);
+ rb_define_method(rb_cRuggedBranch, "name", rb_git_branch_name, 0);
rb_define_method(rb_cRuggedBranch, "remote_name", rb_git_branch_remote_name, 0);
}
View
9 lib/rugged/branch.rb
@@ -16,13 +16,8 @@ def ==(other)
#
# This is the same as calling Reference#name for the reference behind
# the path
- alias_method 'canonical_name', 'name'
-
- # The name of the branch, without a fully-qualified reference path
- #
- # E.g. 'master' instead of 'refs/heads/master'
- def name
- super.gsub(%r{^(refs/heads/|refs/remotes/)}, '')
+ def cannonical_name
+ super
end
# Get the remote the branch belongs to.
View
1 lib/rugged/reference.rb
@@ -1,5 +1,6 @@
module Rugged
class Reference
+ alias_method :canonical_name, :name
def inspect
"#<Rugged::Reference:#{object_id} {name: #{name.inspect}, target: #{target.inspect}}>"
View
1 test/reference_test.rb
@@ -42,6 +42,7 @@ def test_can_open_reference
assert_equal "36060c58702ed4c2a40832c51758d5344201d89a", ref.target
assert_equal :direct, ref.type
assert_equal "refs/heads/master", ref.name
+ assert_equal "refs/heads/master", ref.canonical_name
assert_nil ref.peel
end

0 comments on commit 2330c49

Please sign in to comment.