rakelib/hglib.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 29 Oct 2016 23:54:12 +0000
changeset 66 8d2d5dfe94d0
child 67 75b6eb7b781c
permissions -rwxr-xr-x
Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks ...rather than issuing hg commands by hand. `hglib.rb` provides higher-level API for repositories and thus would allow more complex logic (such as using mirrors to fetch base or smarter handling of bookmarks) being written in a more concise way.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     1
# This file is not a standalone script. It is a kind
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     2
# of lightweight Mercurial library used by other scripts.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     3
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     4
require 'uri'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     5
require 'open3'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     6
require 'shellwords'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     7
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     8
# Following hack is to make hglib.rb working wit both jv:scripts and
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     9
# Smalltalk/X rakefiles. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    10
begin
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    11
  require 'rakelib/inifile'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    12
rescue
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    13
  begin
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    14
    require 'inifile'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    15
  rescue LoadError => ex
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    16
    $LOGGER.error("Cannot load package 'inifile'")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    17
    $LOGGER.error("Run 'gem install inifile' to install it")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    18
    exit 1
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    19
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    20
end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    21
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    22
if not $LOGGER then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    23
  if STDOUT.tty? then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    24
    require 'logger'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    25
    $LOGGER = Logger.new(STDOUT)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    26
    $LOGGER.level = Logger::INFO
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    27
  else 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    28
    require 'syslog/logger'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    29
    $LOGGER = Syslog::Logger.new($0)    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    30
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    31
end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    32
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    33
module HG
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    34
  @@config = nil
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    35
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    36
  GLOBAL_OPTIONS= [:'cwd', :'repository', :'noninteractive', :'config', :'debug', :'debugger',
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    37
       :'encoding',:'encodingmode',:'traceback',:'time', :'profile',:'version', :'help',
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    38
       :'hidden' ]
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    39
  # Execute `hg` command with given positional arguments and
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    40
  # keyword arguments turned into command options. For example, 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    41
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    42
  #     HG::hg("heads", "default", cwd: '/tmp/testrepo')
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    43
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    44
  # will result in executing
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    45
  # 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    46
  #     hg --cwd '/tmp/testrepo' heads default
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    47
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    48
  # In addition if block is passed, then the block is evaluate with
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    49
  # `hg` command exit status (as Process::Status) and (optionally)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    50
  # with contents of `hg` command stdout and stderr. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    51
  # If no block is given, an exception is raised when `hg` command 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    52
  # exit status IS NOT zero.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    53
  def self.hg(command, *args, **options, &block)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    54
    g_opts = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    55
    c_opts = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    56
    options.each do | k , v |       
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    57
      if v != false                
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    58
        o = k.size == 1 ? "-#{k}" : "--#{k}"                
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    59
        if GLOBAL_OPTIONS.include? k then                  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    60
          if v.kind_of?(Array)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    61
            v.each do | e |
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    62
              g_opts << o << (e == true ? '' : e)  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    63
            end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    64
          else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    65
            g_opts << o << (v == true ? '' : v)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    66
          end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    67
        else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    68
          if v.kind_of?(Array)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    69
            v.each do | e |
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    70
              c_opts << o << (e == true ? '' : e)  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    71
            end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    72
          else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    73
            c_opts << o << (v == true ? '' : v)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    74
          end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    75
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    76
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    77
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    78
    c_opts.reject! { | e | e.size == 0 }
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    79
    cmd = ['hg'] + g_opts + [command] + c_opts + args      
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    80
    $LOGGER.debug("executing: #{cmd.shelljoin}")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    81
    if block_given? then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    82
      stdout, stderr, status = Open3.capture3(*cmd)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    83
      case block.arity
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    84
      when 1        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    85
        yield status
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    86
      when 2        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    87
        yield status, stdout
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    88
      when 3        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    89
        yield status, stdout, stderr
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    90
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    91
        raise Exception.new("invalid arity of given block")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    92
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    93
    else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    94
      if not system(*cmd) then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    95
        raise Exception.new("command failed: #{cmd.join(' ')}")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    96
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    97
    end    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    98
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    99
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   100
  def self.config()
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   101
    if @@config == nil
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   102
      files = Dir.glob('/etc/mercurial/hgrc.d/*.rc') + 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   103
          [ '/etc/mercurial/hgrc' ,
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   104
          hgrc() ]  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   105
      @@config = IniFile.new()
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   106
      files.each do | file |
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   107
        if File.exist?(file)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   108
          $LOGGER.debug("Loading global config from \"#{file}\"")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   109
          @@config.merge!(IniFile.new(:filename => file))
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   110
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   111
      end  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   112
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   113
    return @@config
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   114
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   115
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   116
  def self.hgrc()
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   117
  	return File.expand_path('~/.hgrc')
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   118
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   119
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   120
  class Repository
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   121
    attr_accessor :path, :config
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   122
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   123
    # Clone a repository from given `uri` to given `directory`. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   124
    # Returns an `HG::Repository` instance representing the repository
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   125
    # clone. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   126
    # If `noupdate` is true, working copy is not updated, i.e., will be
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   127
    # empty. Use this when you're going to issue `update(rev)` shortly after.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   128
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   129
    def self.clone(uri, directory, noupdate: false)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   130
      if noupdate then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   131
        HG::hg("clone", uri, directory, noupdate: true)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   132
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   133
        HG::hg("clone", uri, directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   134
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   135
      return HG::Repository.new(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   136
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   137
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   138
    # Like HG::hg, but passes --cwd @path
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   139
    def hg(command, *args, **options, &block)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   140
      options[:cwd] = @path
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   141
      HG::hg(command, *args, **options, &block)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   142
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   143
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   144
    def hgrc() 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   145
      return File.join(@path, '.hg', 'hgrc')
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   146
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   147
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   148
    def initialize(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   149
      @path = directory
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   150
      config_file = hgrc()
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   151
      if File.exist? ( config_file ) 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   152
        $LOGGER.debug("Loading repository config from \"#{config_file}\"")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   153
        @config = HG::config().merge(IniFile.new(:filename => config_file))
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   154
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   155
        @config = HG::config()
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   156
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   157
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   158
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   159
    def log(revset, template = "{node|short}\n")      
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   160
      log = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   161
      hg("log", rev: revset, template: template) do | status, out |     
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   162
        if status.success?
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   163
          puts out
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   164
          log = out.split("\n")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   165
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   166
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   167
      return log
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   168
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   169
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   170
    # Return changeset IDs of all head revisions. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   171
    # If `branch` is given, return only heads in given
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   172
    # branch.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   173
    def heads(branch = nil) 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   174
      if branch then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   175
        return log("head() and branch('#{branch}')")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   176
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   177
        return log("head()")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   178
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   179
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   180
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   181
    # Return a hash "bookmark => revision" of all 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   182
    # bookmarks. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   183
    def bookmarks(branch = nil)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   184
      revset  = "bookmark()"
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   185
      revset += " and branch('#{branch}')" if branch
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   186
      bookmarks = {}
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   187
      self.log(revset, "{bookmarks}|{node|short}\n").each do | line |
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   188
        bookmark, changesetid = line.split("|")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   189
        bookmarks[bookmark] = changesetid
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   190
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   191
      return bookmarks
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   192
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   193
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   194
    def pull(remote = 'default', user: nil, pass: nil)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   195
      authconf = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   196
      if pass != nil then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   197
        if user == nil then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   198
          raise Exception.new("Password given but not username! Use user: named param to specify username.")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   199
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   200
        # If user/password is provided, make sure we don't have
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   201
        # username in remote URI. Otherwise Mercurial won't use 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   202
        # password from config!
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   203
        uri = URI.parse(remote)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   204
        uri.user = nil
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   205
        remote = uri.to_s        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   206
        authconf << "auth.bb.prefix=#{remote}"
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   207
        authconf << "auth.bb.username=#{user}"        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   208
        authconf << "auth.bb.password=#{pass}"        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   209
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   210
      hg("pull", remote, config: authconf) do | status |
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   211
        if not status.success? then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   212
          raise Exception.new("Failed to pull from #{remote}")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   213
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   214
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   215
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   216
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   217
    def push(remote = 'default', user: nil, pass: nil)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   218
      authconf = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   219
      if pass != nil then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   220
        if user == nil then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   221
          raise Exception.new("Password given but not username! Use user: named param to specify username.")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   222
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   223
        # If user/password is provided, make sure we don't have
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   224
        # username in remote URI. Otherwise Mercurial won't use 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   225
        # password from config!
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   226
        uri = URI.parse(remote)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   227
        uri.user = nil
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   228
        remote = uri.to_s                
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   229
        authconf << "bb.prefix = #{remote}"
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   230
        authconf << "bb.username = #{user}"        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   231
        authconf << "bb.password = #{pass}"        
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   232
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   233
      hg("pull", remote, config: authconf) do | status |
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   234
        if status.exitstatus != 0 and status.exitstatus != 1 then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   235
          raise Exception.new("Failed to pull from #{remote}")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   236
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   237
      end      
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   238
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   239
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   240
    # Create a shared clone in given directory, Return a new
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   241
    # HG::Repository object on the shared clone
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   242
    def share(dst, rev = nil)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   243
      if File.exist? dst then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   244
        raise Exception.new("Destination file exists: #{dst}")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   245
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   246
      if rev == nil then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   247
        rev = log('.')[0]
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   248
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   249
      if not has_revision?(rev) 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   250
        raise Exception.new("Revision #{rev} does not exist")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   251
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   252
      mkdir_p File.dirname(dst);
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   253
      HG::hg("share", path, dst, config: 'extensions.share=', noupdate: true, bookmarks: false)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   254
      share = Repository.new(dst)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   255
      share.update(rev);
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   256
      return share
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   257
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   258
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   259
    # Updates the repository's working copy to given 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   260
    # revision if given. If not, update to most-recent
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   261
    # head, as plain
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   262
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   263
    #   hg update
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   264
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   265
    # would do. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   266
    def update(rev = nil)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   267
      if rev 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   268
        if not has_revision? rev then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   269
          raise Exception.new("Revision #{rev} does not exist")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   270
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   271
        hg("update", rev: rev)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   272
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   273
        hg("update")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   274
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   275
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   276
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   277
    # Merge given revision. Return true, if the merge was
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   278
    # successful, false otherwise
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   279
    def merge(rev)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   280
      if not has_revision? rev then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   281
        raise Exception.new("Revision #{rev} does not exist")
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   282
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   283
      hg("merge", rev) do | status |
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   284
        return status.success?
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   285
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   286
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   287
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   288
    def commit(message)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   289
      user = ''
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   290
      if not @config['ui'].has_key? 'username' then
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   291
        user = @config['ui']['username']
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   292
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   293
      hg("commit", message: message, user: user)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   294
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   295
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   296
    def has_revision?(rev)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   297
    	revs = log(rev)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   298
    	return revs.size > 0      
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   299
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   300
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   301
    # Lookup a repository in given `directory`. If found,
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   302
    # return it as instance of HG::Repository. If not,
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   303
    # `nil` is returned.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   304
    def self.lookup(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   305
      return nil if not File.exist?(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   306
      repo_dir = directory
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   307
      while repo_dir != nil
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   308
        if HG::repository? repo_dir
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   309
          return Repository.new(repo_dir)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   310
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   311
        repo_dir_parent = File.dirname(repo_dir)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   312
        if repo_dir_parent == repo_dir
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   313
          repo_dir = nil
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   314
        else 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   315
          repo_dir = repo_dir_parent
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   316
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   317
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   318
    end    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   319
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   320
    # Initializes and empty Mercurial repository in given `directory`
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   321
    def self.init(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   322
      FileUtils.mkdir_p File.dirname(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   323
      HG::hg("init", directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   324
      return Repository.new(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   325
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   326
  end # class Repository 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   327
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   328
  # Return `true` if given `directory` is a root of mercurial
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   329
  # repository, `false` otherwise.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   330
  def self.repository?(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   331
    return File.directory? File.join(directory, '.hg')
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   332
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   333
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   334
  # Enumerate all repositories in given `directory`
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   335
  def self.forest(directory, &block)      
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   336
    if repository? directory  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   337
      yield Repository.new(directory)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   338
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   339
    Dir.foreach(directory) do |x|
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   340
      path = File.join(directory, x)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   341
      if File.directory? path       
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   342
        if x == "." or x == ".." or x == ".svn" or x == '.git'
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   343
          next    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   344
        elsif File.directory?(path)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   345
          forest(path, &block)
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   346
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   347
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   348
    end  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   349
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   350
end # module HG