Module: Amp::CommandSupport

Included in:
Command
Defined in:
lib/amp/commands/command_support.rb

Constant Summary collapse

REV_SEP =

When a user specifies a range, REV_SEP is between the given revisions.

":"

Instance Method Summary collapse

Instance Method Details

#expand_path(*arr) ⇒ Object

Return repository location relative to cwd or from [paths]



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/amp/commands/command_support.rb', line 105

def expand_path(*arr)
  loc  = arr.shift
  dflt = arr.shift # dflt = default
  cnfg = arr.pop   # always take the last

  return loc if loc =~ /:\/\// or File.directory?(File.join(loc, '.hg'))

  path = cnfg['paths'][loc]

  if !path && dflt
    path = cnfg['paths'][dflt]
  end
  path || loc
end

#local_path(path) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/amp/commands/command_support.rb', line 91

def local_path(path)
  case path
  when /file:\/\/localhost\//
    path[17..-1]
  when /file:\/\//
    path[8..-1]
  when /file:/
    path[6..-1]
  else
    path
  end
end

#log_message(message, logfile) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/amp/commands/command_support.rb', line 120

def log_message(message, logfile)
  if message && logfile
    raise abort('options --message and --logfile are mutually exclusive')
  end
  
  if !message && logfile
    begin
      message = logfile == '-' ? $stdin.read : File.read(logfile)
    rescue IOError => e
      raise abort("can't read commit message '#{logfile}': #{e}")
    end
  end
  
  message
end

#parse_url(*arr) ⇒ Object

returns [String, Array || nil, String]



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/amp/commands/command_support.rb', line 77

def parse_url(*arr)
  url  = arr.shift
  revs = arr.shift || []
  
  unless url =~ /#/
    hds = revs.any? ? revs : []
    return url, hds, revs[-1]
  end
  
  url, branch = url.split('#')[0..1]
  checkout = revs[-1] || branch
  [url, revs + [branch], checkout]
end

Prints the statistics returned from an update or merge. These are given in the form of a hash, such as => 0, :unresolved => 3, …, and should be printed in a nice manner.

Parameters:

  • stats (Hash<Symbol => Fixnum>)

    the statistics resulting from an update, merge or clean command.



31
32
33
# File 'lib/amp/commands/command_support.rb', line 31

def print_update_stats(stats)
  Amp::UI.status stats.map {|note, files| "#{files.size} files #{note}" }.join(", ")
end

#revision_lookup(repo, value, default = nil) ⇒ String

Looks up the node ID of a given revision in the repository. Since this method uses Repository#lookup, the choices for “value” are very flexible. You can choose a revision number, a partial node ID, “tip”, and so on. See LocalRepository#lookup for more details.

Parameters:

  • repo (Repository)

    the repository in which to lookup the node

  • value (String, Integer)

    the search term for the node - could be a revision index #, a partial node ID, and so on

  • default (String, Integer) (defaults to: nil)

    the default search term, in case value is nil.

Returns:

  • (String)

    the full node ID of the node that is found, or nil if none is found.

See Also:

  • Amp::CommandSupport.{LocalRepository{LocalRepository#lookup}


19
20
21
22
# File 'lib/amp/commands/command_support.rb', line 19

def revision_lookup(repo, value, default = nil)
  value ||= default
  repo.lookup(value)
end

#revision_pair(repo, revisions) ⇒ Array<String>

Parses strings that represent a range of

Examples:

revision_pair(repo, ["10:tip"])     #=> [repo.lookup(10), repo.lookup("tip")]
revision_pair(repo, ["10", "tip"])  #=> [repo.lookup(10), repo.lookup("tip")]
revision_pair(repo, ":tip")         #=> [repo.lookup(0),  repo.lookup("tip")]

Parameters:

  • repo (Repository)

    the repository to use when looking up revisions

  • revisions (Array, String)

    the revisions to parse. Could be a set of strings, passed directly in from the command line, or could just be 1 string.

Returns:

  • (Array<String>)

    the node IDs that correspond to the start and end of the specified range of changesets



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
# File 'lib/amp/commands/command_support.rb', line 47

def revision_pair(repo, revisions)
  #revisions = [revisions] unless revisions.is_a?(Array)
  if !revisions || revisions.empty?
    return repo.dirstate.parents.first, nil
  end
  stop = nil
  
  if revisions.size == 1
    #old revision compared with working dir?
    if revisions[0].include? REV_SEP
      start, stop = revisions.first.split REV_SEP, 2
      start = revision_lookup repo, start, 0
      stop  = revision_lookup repo, stop, repo.size - 1
    else
      start = revision_lookup repo, revisions.first
    end
  elsif revisions.size == 2
    if revisions.first.include?(REV_SEP) ||
       revisions.second.include?(REV_SEP)
       raise ArgumentError.new("too many revisions specified")
    end
    start = revision_lookup(repo, revisions.first)
    stop  = revision_lookup(repo, revisions.second)
  else
    raise ArgumentError.new("too many revisions specified")
  end
  [start, stop]
end