Module: Amp::Core::Repositories::CommonChangesetMethods

Included in:
AbstractChangeset
Defined in:
lib/amp-core/repository/abstract/common_methods/changeset.rb

Overview

CommonVersionedFileMethods

These methods are common to all repositories, and this module is mixed into the AbstractLocalRepository class. This guarantees that all repositories will have these methods.

No methods should be placed into this module unless it relies on methods in the general API for repositories.

Instance Method Summary collapse

Instance Method Details

#changed_filesHash{String => String}

A hash of the files that have been changed and their most recent diffs. The diffs are lazily made upon access. To just get the files, use #altered_files or #changed_files.keys Checks whether this changeset included a given file or not.

Returns:

  • (Hash{String => String})

    a hash of => the diff



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/amp-core/repository/abstract/common_methods/changeset.rb', line 37

def changed_files
  h = {}
  class << h
    def [](*args)
      super(*args).call # we expect a proc
    end
  end

  altered_files.inject(h) do |s, k|
    s[k] = proc do
      other = parents.first[k]
      other.unified_diff_with self[k]
    end
    s
  end
end

#changed_lines_statisticsHash{Symbol => Integer}

Gets the number of lines added and removed in this changeset.

Returns:

  • (Hash{Symbol => Integer})

    a hash returning line-changed statistics. Keys are :added, :deleted



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/amp-core/repository/abstract/common_methods/changeset.rb', line 59

def changed_lines_statistics
  result = {:added => 0, :removed => 0}
  changed_files.each do |_, diffproc|
    diff = diffproc.call
    diff.split("\n").each do |line|
      if line.start_with?("+") && !line.start_with?("+++")
        result[:added] += 1
      elsif line.start_with?("-") && !line.start_with?("---")
        result[:removed] += 1
      end
    end
  end
  result
end

#include?(file) ⇒ Boolean

Is file being tracked at this point in time?

Parameters:

  • file (String)

    the file to lookup

Returns:

  • (Boolean)

    whether the file is in this changeset’s manifest



79
80
81
# File 'lib/amp-core/repository/abstract/common_methods/changeset.rb', line 79

def include?(file)
  all_files.include? file
end

#template_for_options(opts = {}) ⇒ Template

Returns template to handle templating based on the options provided.

Parameters:

  • options (Hash)

    a list of options to configure template usage

Returns:

  • (Template)

    a template to use to print the changeset to a nice string.



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/amp-core/repository/abstract/common_methods/changeset.rb', line 143

def template_for_options(opts = {})
  type = opts[:template_type] || 'log'
  if opts[:"template-raw"]
    Support::RawERbTemplate.new(opts[:"template-raw"])
  elsif opts[:template].is_a?(Support::Template)
    opts[:template]
  else
    template = opts[:template]
    template = "default-#{type}" if template.nil? || template.to_s == "default"
    Support::Template['mercurial', template]
  end
end

#to_templated_s(opts = {}) ⇒ String

Renders the changeset with a given template. Takes a few different options to control output.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) the options to control rendering to text

Options Hash (opts):

  • :no_output (Boolean) — default: false

    Don’t actually return any output

  • :"template-raw" (String)

    a bare string to render as a template. Nice option for letting users drop a teensy bit of template code into their commands.

  • :template (String, Template) — default: "default-log"

    The name of the template to use (if a String is provided), or the actual template to use (if a Template object is provided).

Returns:

  • (String)

    the changeset pumped through the template.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/amp-core/repository/abstract/common_methods/changeset.rb', line 114

def to_templated_s(opts={})
  locals = {:change_node => node,
            :revision    => self.revision,
            :username    => self.user,
            :date        => self.easy_date,
            :files       => self.altered_files,
            :description => self.description,
            :branch      => self.branch,
            :cs_tags     => self.tags,

            :added       => opts[:added]   || [],
            :removed     => opts[:removed] || [],
            :updated     => opts[:updated] || [],
            :config      => opts,

            :parents     => useful_parents.map {|p| [p.revision, p.node.short_hex] }
          }

  return "" if opts[:no_output]

  template_for_options(opts).render(locals, binding)
end

#useful_parentsArray<Changeset>

Gets the list of parents useful for printing in a template - no point in saying that revision 127’s only parent is 126, for example.

Returns:

  • (Array<Changeset>)

    a list of changesets that are the parent of this node and happen to be worth noting to the user.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/amp-core/repository/abstract/common_methods/changeset.rb', line 89

def useful_parents
  ret_parents = self.parents
  if ret_parents[1].nil?
    if ret_parents[0].revision >= self.revision - 1
      ret_parents = []
    else
      ret_parents = [ret_parents[0]]
    end
  end
  ret_parents
end

#walk(match) ⇒ Object

recursively walk

Parameters:

  • match (Amp::Matcher)

    this is a custom object that knows files magically. Not your grampa’s proc!



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/amp-core/repository/abstract/common_methods/changeset.rb', line 161

def walk(match)
  # just make it so the keys are there
  results = []

  hash = Hash.with_keys match.files
  hash.delete '.'

  each do |file|
    hash.each {|f, val| (hash.delete file and break) if f == file }
  
    results << file if match.call file # yield file if match.call file
  end

  hash.keys.sort.each do |file|
    if match.bad file, "No such file in revision #{revision}" and match[file]
      results << file # yield file
    end
  end
  results
end