Top Level Namespace

Includes:
Amp::KernelMethods

Defined Under Namespace

Modules: Amp, Archive, BZ2, BugFix, Docable, Enumerable, IOExtras, Kernel, Net, Platform, PythonConfig, Sinatra, Trollop, Zip, Zlib Classes: AbortError, Array, AuthorizationError, Bignum, CPriorityQueue, Dir, File, Fixnum, Generator, Hash, Integer, LockError, LockHeld, LockUnavailable, Module, OSError, Object, PoorPriorityQueue, Proc, Range, RubyPriorityQueue, String, Symbol, Time, ZipList

Constant Summary collapse

Boolean =
:bool
Threesome =
Amp::Merges::ThreeWayMerger
Tempfile =
BugFix::Tempfile
PriorityQueue =
RubyPriorityQueue

Instance Method Summary collapse

Methods included from Amp::KernelMethods

#command, #cut!, #namespace, #template

Instance Method Details

#addremoveObject

This command will go through your repository’s working directory, and if it discovers any files that it was tracking, but have mysteriously disappeared, the command will assume you meant to remove it (stop tracking it), and remove it for you. Similarly, if you have any files that are untracked and have appeared in your repository, and they aren’t ignored by your .hgignore file, then we assume you wanted to add them, and add them for you.

The only significant difference between this command and Mercurial’s implementation is the addition of the interactive mode. This mode asks the user before each removal and addition. Personally, I added it because I don’t like blanket actions taken across my entire repository. It’s off by default.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/amp/commands/commands/workflows/hg/addremove.rb', line 13

command :addremove do |c|
  c.workflow :hg
  
  c.desc "Add all new files, delete all missing files"
  c.opt :interactive, "Asks about each file before adding/removing", :default => false, :short => "-a"
  c.opt :include, "include names matching the given patterns", :type => :string, :short => "-I"
  c.opt :exclude, "exclude names matching the given patterns", :type => :string, :short => "-E"
  c.opt :"dry-run", "Don't perform actions, just print output", :short => "-n"
  c.help <<-EOS
amp addremove [options]+

  New files are ignored if they match any of the patterns in .hgignore. As
  with add, these changes take effect at the next commit.

  Use the -s option to detect renamed files. With a parameter > 0,
  this compares every removed file with every added file and records
  those similar enough as renames. This option takes a percentage
  between 0 (disabled) and 100 (files must be identical) as its
  parameter. Detecting renamed files this way can be expensive.
  
  Where options are:
EOS

  c.on_run do |opts, args|
    repo = opts[:repository]
    
    # Standard preparation for mercurial-style matching. We need a match object.
    # If a file is missed by our matcher, assume we want it included (unknown files match
    # this situation)
    matcher = Amp::Match.create(:includer => opts[:include], :excluder => opts[:exclude]) { true }
    # Get only the deleted and unknown files. We'll remove the former and add the latter.
    results = repo.status(:match => matcher, :deleted => true, :added => false, :unknown => true,
                          :ignored => false, :modified => false, :clean => false)
    
    Amp::UI.say
    # Prettified, add check later if user disables colors
    Amp::UI.say "ADDING FILES".blue if results[:unknown].any?
    # Handle adding the files now
    if opts[:interactive]
      # Interactive means we ask the user if they want to add the file or not.
      # Build a list based on what they agree upon, then add that list
      to_add = []
      results[:unknown].each do |file|
        add = Amp::UI.yes_or_no("Add #{file.relative_path(repo.root).blue}? [y/n] ")
        to_add << file if add
      end
    else
      # Otherwise, just let the user know what we're about to add
      results[:unknown].each {|file| Amp::UI.say "Adding #{file.relative_path repo.root}" }
      to_add = results[:unknown]
    end
    repo.add(to_add) unless opts[:"dry-run"]
    
    Amp::UI.say
    # Prettified, add check later if user disables colors
    Amp::UI.say "REMOVING FILES".red if results[:deleted].any?
    # Now we remove any files that mysteriously disappeared on us
    if opts[:interactive]
      # Interactive means ask the user if they want to remove a file from the repo or not.
      # Build a list.
      to_del = []
      results[:deleted].each do |file|
        del = Amp::UI.yes_or_no("Remove #{file.relative_path(repo.root).red}?")
        to_del << file if del
      end
    else
      # Otherwise, just inform the user of the damage (yes I'm biased against this command)
      results[:deleted].each {|file| Amp::UI.say "Removing #{file.relative_path repo.root}" }
      to_del = results[:deleted]
    end
    repo.remove to_del unless opts[:"dry-run"]
    
  end
end

#amp_c_extension(path_to_c, path_to_alt) ⇒ Object

Loads a C extension, or an alternate file if the C cannot be loaded for any reason (such as the user not compiling it).

Parameters:

  • path_to_c (String)

    the path to the C library. Will be loaded relative to the caller’s file.

  • path_to_alt (String)

    the path to the pure ruby version of the C library. Will be loaded relative to the caller’s file



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amp/support/loaders.rb', line 78

def amp_c_extension(path_to_c, path_to_alt)
  
  if $USE_RUBY
    Amp::UI.debug "Loading alternative ruby: #{path_to_alt}"
    require File.join(File.dirname(caller_file(1)), path_to_alt)
    return
  end
  
  begin
    offset = RUBY_VERSION < "1.9" ? 1 : 0
    require File.expand_path(File.join(File.dirname(caller_file(offset)), path_to_c))
  rescue LoadError # C Version could not be found, try ruby version
    Amp::UI.debug "Loading alternative ruby: #{path_to_alt}"
    require File.join(File.dirname(caller_file(1)), path_to_alt)
  end
end

#need(file = nil, &block) ⇒ Object

Loads a given file, relative to the directory of the file executing the need statement.

Examples:

need { “silly.rb” }

need(“silly.rb”)

Parameters:

  • file (String) (defaults to: nil)

    the file to load



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/amp/support/loaders.rb', line 12

def need(file=nil, &block)
  # do some timing if we're still benchmarking
  s = Time.now if ENV["TESTING"] == "true"
  
  if block_given?
    if RUBY_VERSION < "1.9" && (!defined?(RUBY_ENGINE) || RUBY_ENGINE != 'rbx')
      require File.expand_path(File.join(File.dirname(eval("__FILE__", block.binding)),block.call)) # 1.9 hack
    else
      require File.expand_path(File.join(File.dirname(caller_file(0)),block.call))
    end
  elsif file
    require File.expand_path(File.join(File.dirname(__FILE__),file))
  end
  # do some timing if we're still benchmarking
  $times << [Time.now - s, block ? block.call : file]  if ENV["TESTING"] == "true"
end

#require_dir(dir = nil) { ... } ⇒ Object

Loads an entire directory, relative to the caller’s file

Examples:

require_dir { “commands/*/.rb” }

Parameters:

  • dir (String) (defaults to: nil)

    the directory, in glob format, to load

Yields:

  • returns the string of the directory



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/amp/support/loaders.rb', line 35

def require_dir(dir=nil, &block)
  # do some timing if we're still benchmarking
  s = Time.now if ENV["TESTING"] == "true"
  
  if block_given?
    Dir[File.join(Amp::CODE_ROOT, block.call)].each do |f|
      unless File.directory? f
        f = f[Amp::CODE_ROOT.size+1..-1]
        require f
      end
    end
  else
    Dir[dir].each {|f| require f unless File.directory? f }
  end
  
  # do some timing if we're still benchmarking
  $times << [Time.now - s, block ? block.call : file]  if ENV["TESTING"] == "true"
end

#show_caller_for(meth, lines, new_meth = "__#{meth}__") ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/amp/profiling_hacks.rb', line 14

def show_caller_for(meth, lines, new_meth="__#{meth}__")
  lines = [*lines]
  alias_method "#{new_meth}".to_sym, meth
  self.class_eval(<<-HELP)
def #{meth}(*args, &block)
  #{lines.join("\n")}
  #{new_meth}(*args, &block)
end
HELP
end