Module: Rosette::Core::Commands::WithRef

Overview

Mixin that handles configuration and validation of a git ref or commit id. Meant to be mixed into the classes in Rosette::Core::Commands. Required to be used in combination with WithRepoName.

Examples:

class MyCommand < Rosette::Core::Commands::Command
  include WithRepoName
  include WithRef
end

cmd = MyCommand.new
  .set_repo_name('my_repo')
  .set_ref('master')

cmd.commit_str  # => "master"
cmd.commit_id   # => "67f0e9a60dfe39430b346086f965e6c94a8ddd24"
cmd.valid?      # => true

cmd.set_ref('non_existant_ref')
cmd.valid?    # => false
cmd.messages  # => { commit_str: ["Unable to find commit 'non_existent_ref'"] }

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commit_strString (readonly)

Returns the raw value as set by either #set_ref or #set_commit_id.

Returns:


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rosette/core/commands/git/with_ref.rb', line 36

module WithRef
  attr_reader :commit_str

  # Set the git ref (i.e. a branch name). Calling this method after {#set_commit_id}
  # will overwrite the commit id value. In other words, it's generally a good idea to
  # only call one of {#set_commit_id} or {#set_ref} but not both.
  #
  # @param [String] ref_str The git ref.
  # @return [self]
  def set_ref(ref_str)
    @commit_str = ref_str
    self
  end

  # Set the git commit id. Calling this method after {#set_ref} will overwrite the ref value.
  # In other words, it's generally a good idea to only call one of {#set_commit_id} or {#set_ref}
  # but not both.
  #
  # @param [String] commit_id The commit id.
  # @return [self]
  def set_commit_id(commit_id)
    @commit_str = commit_id
    self
  end

  # Resolves the given git ref or commit id and returns the corresponding commit id.
  # If {#set_ref} was used to set a git ref (i.e. branch name), this method looks up
  # and returns the corresponding commit id. If {#set_commit_id} was used to set a
  # commit id, then that commit id is validated and returned.
  #
  # @return [String] The commit id set via either {#set_ref} or {#set_commit_id}.
  # @raise [Java::OrgEclipseJgitErrors::MissingObjectException, Java::JavaLang::IllegalArgumentException]
  #   If either the commit id doesn't exist or the ref can't be found.
  def commit_id
    @commit_id ||= begin
      REV_COMMIT_MUTEX.synchronize do
        get_repo(repo_name)
          .repo.get_rev_commit(@commit_str)
          .getId.name
      end
    end
  end

  private

  REV_COMMIT_MUTEX = Mutex.new

  def self.included(base)
    if base.respond_to?(:validate)
      base.validate :commit_str, type: :commit
    end
  end
end

Instance Method Details

#commit_idString

Resolves the given git ref or commit id and returns the corresponding commit id. If #set_ref was used to set a git ref (i.e. branch name), this method looks up and returns the corresponding commit id. If #set_commit_id was used to set a commit id, then that commit id is validated and returned.

Returns:

Raises:

  • (Java::OrgEclipseJgitErrors::MissingObjectException, Java::JavaLang::IllegalArgumentException)

    If either the commit id doesn't exist or the ref can't be found.


69
70
71
72
73
74
75
76
77
# File 'lib/rosette/core/commands/git/with_ref.rb', line 69

def commit_id
  @commit_id ||= begin
    REV_COMMIT_MUTEX.synchronize do
      get_repo(repo_name)
        .repo.get_rev_commit(@commit_str)
        .getId.name
    end
  end
end

#set_commit_id(commit_id) ⇒ self

Set the git commit id. Calling this method after #set_ref will overwrite the ref value. In other words, it's generally a good idea to only call one of #set_commit_id or #set_ref but not both.

Parameters:

  • commit_id (String)

    The commit id.

Returns:

  • (self)

56
57
58
59
# File 'lib/rosette/core/commands/git/with_ref.rb', line 56

def set_commit_id(commit_id)
  @commit_str = commit_id
  self
end

#set_ref(ref_str) ⇒ self

Set the git ref (i.e. a branch name). Calling this method after #set_commit_id will overwrite the commit id value. In other words, it's generally a good idea to only call one of #set_commit_id or #set_ref but not both.

Parameters:

  • ref_str (String)

    The git ref.

Returns:

  • (self)

45
46
47
48
# File 'lib/rosette/core/commands/git/with_ref.rb', line 45

def set_ref(ref_str)
  @commit_str = ref_str
  self
end