Class: RightDevelop::Commands::Git

Inherits:
Object
  • Object
show all
Includes:
RightSupport::Log::Mixin
Defined in:
lib/right_develop/commands/git.rb

Constant Summary collapse

NAME_SPLIT_CHARS =
/-|_|\//
YES =
/(ye?s?)/i
NO =
/(no?)/i
TASKS =
%w(prune tickets)
MERGE_COMMENT =
/^Merge (?:remote[- ])?(?:tracking )?(?:branch|pull request #[0-9]+ from) ['"]?(.*)['"]?$/i
WORD_BOUNDARY =
%r{[_ /-]+}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, task, options) ⇒ Git

Returns a new instance of Git.

Parameters:

  • repo (RightGit::Git::Repository)

    the Git repository to operate on

  • task (Symbol)

    one of :prune or :tickets

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :age (String)

    Ignore branches newer than this time-ago-in-words e.g. “3 months”; default unit is months

  • :except (String)

    Ignore branches matching this regular expression

  • :only (String)

    Consider only branches matching this regular expression

  • :local (Boolean)

    Consider only local branches

  • :remote (Boolean)

    Consider only remote branches

  • :merged (String)

    Consider only branches that are fully merged into this branch (e.g. master)

  • :since (String)

    the name of a “base branch” representing the previous release

  • :link (String)

    word prefix connoting a link to an external ticketing system

  • :link_to (String)

    URL prefix to use when generating ticket links



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/right_develop/commands/git.rb', line 108

def initialize(repo, task, options)
  logger                                  = Logger.new(STDERR)
  logger.level                            = options[:debug] ? Logger::DEBUG : Logger::WARN
  RightSupport::Log::Mixin.default_logger = logger

  # Post-process "age" option; transform from natural-language expression into a timestamp.
  if (age = options.delete(:age))
    age           = parse_age(age)
    options[:age] = age
  end

  # Post-process "except" option; transform into a Regexp.
  if (except = options.delete(:except))
    except           = Regexp.new("^(origin/)?(#{except})")
    options[:except] = except
  end

  # Post-process "only" option; transform into a Regexp.
  if (only = options.delete(:only))
    only           = Regexp.new("^(origin/)?(#{only})")
    options[:only] = only
  end

  # Post-process "since" option; transform into a Regexp.
  if (link = options.delete(:link))
    link           = Regexp.new("^#{link}")
    options[:link] = link
  end

  @git     = repo
  @task    = task
  @options = options
end

Class Method Details

.createObject

Parse command-line options and create a Command object



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
89
90
91
92
93
94
95
# File 'lib/right_develop/commands/git.rb', line 43

def self.create
  task_list = TASKS.map { |c| "       * #{c}" }.join("\n")

  options = Trollop.options do
    banner <<-EOS
The 'git' command automates various repository management tasks. All tasks
accept the same options, although not every option applies to every command.

Usage:
   right_develop git <task> [options]

Where <task> is one of:
#{task_list}

And [options] are selected from:
    EOS
    opt :age, "Minimum age to consider",
        :default => "3.months"
    opt :only, "Limit to branches matching this prefix",
        :type => :string
    opt :except, "Ignore branches matching this prefix",
        :type    => :string,
        :default => "(release|ve?r?)?[0-9.]+"
    opt :local, "Limit to local branches"
    opt :remote, "Limit to remote branches"
    opt :merged, "Limit to branches that are merged into this branch",
        :type    => :string,
        :default => "master"
    opt :since, "Base branch or tag to compare against for determining 'new' commits",
        :default => "origin/master"
    opt :link, "Word prefix indicating a link to an external ticketing system",
        :default => "(?:[#A-Za-z]+)([0-9]+)$"
    opt :link_to, "URL pattern to generate ticket links, don't forget trailing slash!",
        :type => :string
    opt :debug, "Enable verbose debug output",
        :default => false
  end

  task = ARGV.shift

  repo = ::RightGit::Git::Repository.new(
    ::Dir.pwd,
    ::RightDevelop::Utility::Git::DEFAULT_REPO_OPTIONS)

  case task
  when "prune"
    self.new(repo, :prune, options)
  when "tickets"
    self.new(repo, :tickets, options)
  else
    Trollop.die "unknown task #{task}"
  end
end

Instance Method Details

#runObject

Run the task that was specified when this object was instantiated. This method does no work; it just delegates to a task method.



144
145
146
147
148
149
150
151
152
153
# File 'lib/right_develop/commands/git.rb', line 144

def run
  case @task
  when :prune
    prune(@options)
  when :tickets
    tickets(@options)
  else
    raise StateError, "Invalid @task; check Git.create!"
  end
end