Class: RightDevelop::Commands::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/right_develop/commands/git.rb

Constant Summary collapse

NAME_SPLIT_CHARS =
/-|_|\//
YES =
/(ye?s?)/i
NO =
/(no?)/i
TASKS =
%w(prune)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, task, options) ⇒ Git

Returns a new instance of Git.

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 (true|false)

    Consider local branches

  • :remote (true|false)

    Consider remote branches

  • :merged (String)

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/right_develop/commands/git.rb', line 86

def initialize(repo, task, options)
  # Post-process "age" option; transform from natural-language expression into a timestamp.
  if (age = options.delete(:age))
    require 'ruby-debug'
    debugger
    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

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

Class Method Details

.createObject

Parse command-line options and create a Command object



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
# File 'lib/right_develop/commands/git.rb', line 36

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

  options = Trollop.options do
    banner "The 'git' command automates various repository management tasks. All tasks\naccept the same options, although not every option applies to every command.\n\nUsage:\n   right_develop git <task> [options]\n\nWhere <task> is one of:\n\#{task_list}\n\nAnd [options] are selected from:\n"
    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|v?[0-9.]+|)"
    opt :local, "Limit to local branches"
    opt :remote, "Limit to remote branches"
    opt :merged, "Limit to branches that are fully merged into the named branch",
          :type=>:string,
          :default => "master"
    stop_on TASKS
  end

  task = ARGV.shift

  case task
  when "prune"
    repo = ::RightGit::Git::Repository.new(
      ::Dir.pwd,
      ::RightDevelop::Utility::Git::DEFAULT_REPO_OPTIONS)
    self.new(repo, :prune, 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.



114
115
116
117
118
119
120
121
# File 'lib/right_develop/commands/git.rb', line 114

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