rakelib/hglib.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 03 Nov 2016 22:27:02 +0000
changeset 71 68c8cccbdec5
parent 68 61d8bee7c4d4
child 72 3e832d54a4af
permissions -rwxr-xr-x
Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions. Also use Ruby's **kwargs notation to access keyword arguments rather than messing with *varargs notation. Man, this is an ancient code! I was pretty new to practical Ruby back then...
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 |       
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
    57
      if v != false and v != nil
66
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      
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
    80
    cmd_info = cmd.shelljoin.
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
    81
                gsub(/username\\=\S+/, "username\\=***").
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
    82
                gsub(/password\\=\S+/, "password\\=***")
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
    83
    $LOGGER.debug("executing: #{cmd_info}")
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
    84
    if defined? RakeFileUtils then
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
    85
      puts cmd_info
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
    86
    end
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    87
    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
    88
      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
    89
      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
    90
      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
    91
        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
    92
      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
    93
        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
    94
      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
    95
        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
    96
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    97
        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
    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
    else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   100
      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
   101
        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
   102
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   103
    end    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   104
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   105
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   106
  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
   107
    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
   108
      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
   109
          [ '/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
   110
          hgrc() ]  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   111
      @@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
   112
      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
   113
        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
   114
          $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
   115
          @@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
   116
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   117
      end  
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
    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
   120
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   121
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   122
  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
   123
  	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
   124
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   125
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   126
  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
   127
    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
   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
    # 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
   130
    # 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
   131
    # clone. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   132
    # 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
   133
    # 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
   134
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   135
    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
   136
      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
   137
        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
   138
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   139
        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
   140
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   141
      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
   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
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   144
    # Initializes an empty repository in given directory. Returns an 
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   145
    # `HG::Repository` instance representing the created (empty) repository.
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   146
    def self.init(directory)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   147
      HG::hg("init", directory)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   148
      return HG::Repository.new(directory)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   149
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   150
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   151
    # 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
   152
    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
   153
      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
   154
      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
   155
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   156
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   157
    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
   158
      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
   159
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   160
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   161
    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
   162
      @path = directory
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   163
      config_file = hgrc()      
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   164
      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
   165
        $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
   166
        @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
   167
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   168
        @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
   169
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   170
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   171
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   172
    # Return a hashmap with defined paths (alias => uri)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   173
    def paths() 
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   174
      return @config['paths'].clone
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   175
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   176
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   177
    # Set paths for given repository
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   178
    def paths=(paths)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   179
      config = IniFile.new(:filename => self.hgrc())
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   180
      config['paths'] = paths
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   181
      config.write()
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   182
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   183
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   184
    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
   185
      log = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   186
      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
   187
        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
   188
          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
   189
          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
   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
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   192
      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
   193
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   194
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   195
    # 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
   196
    # 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
   197
    # branch.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   198
    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
   199
      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
   200
        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
   201
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   202
        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
   203
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   204
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   205
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   206
    # 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
   207
    # bookmarks. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   208
    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
   209
      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
   210
      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
   211
      bookmarks = {}
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   212
      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
   213
        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
   214
        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
   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
      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
   217
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   218
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   219
    def pull(remote = 'default', user: nil, pass: nil, rev: nil)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   220
      authconf = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   221
      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
   222
        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
   223
          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
   224
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   225
        # 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
   226
        # username in remote URI. Otherwise Mercurial won't use 
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   227
        # password from config!        
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   228
        uri = URI.parse(self.paths[remote] || remote)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   229
        uri.user = nil
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   230
        uri = uri.to_s
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   231
        uri_alias = if self.paths.has_key? remote then remote else 'xxx' end
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   232
        authconf << "auth.#{uri_alias}.prefix=#{uri}"
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   233
        authconf << "auth.#{uri_alias}.username=#{user}"        
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   234
        authconf << "auth.#{uri_alias}.password=#{pass}"        
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   235
      end
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   236
      hg("pull", remote, config: authconf, rev: nil) do | status |
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   237
        if not status.success? then
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   238
          raise Exception.new("Failed to pull from #{remote} (exit code #{status.exitstatus})")
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   239
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   240
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   241
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   242
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   243
    def push(remote = 'default', user: nil, pass: nil, rev: nil)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   244
      authconf = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   245
      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
   246
        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
   247
          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
   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 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
   250
        # username in remote URI. Otherwise Mercurial won't use 
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   251
        # password from config!        
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   252
        uri = URI.parse(self.paths[remote] || remote)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   253
        uri.user = nil
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   254
        uri = uri.to_s
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   255
        uri_alias = if self.paths.has_key? remote then remote else 'xxx' end
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   256
        authconf << "auth.#{uri_alias}.prefix=#{uri}"
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   257
        authconf << "auth.#{uri_alias}.username=#{user}"        
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   258
        authconf << "auth.#{uri_alias}.password=#{pass}"        
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   259
      end      
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   260
      hg("push", remote, config: authconf, rev: rev) do | status |
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   261
        if status.exitstatus != 0 and status.exitstatus != 1 then
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   262
          raise Exception.new("Failed to push to #{remote} (exit code #{status.exitstatus})")
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   263
        end
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   264
      end            
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   265
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   266
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   267
    # 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
   268
    # 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
   269
    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
   270
      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
   271
        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
   272
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   273
      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
   274
        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
   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
      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
   277
        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
   278
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   279
      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
   280
      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
   281
      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
   282
      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
   283
      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
   284
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   285
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   286
    # 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
   287
    # 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
   288
    # 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
   289
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   290
    #   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
   291
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   292
    # 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
   293
    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
   294
      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
   295
        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
   296
          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
   297
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   298
        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
   299
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   300
        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
   301
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   302
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   303
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   304
    # 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
   305
    # 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
   306
    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
   307
      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
   308
        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
   309
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   310
      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
   311
        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
   312
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   313
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   314
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   315
    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
   316
      user = ''
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   317
      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
   318
        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
   319
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   320
      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
   321
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   322
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   323
    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
   324
    	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
   325
    	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
   326
    end
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
    # 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
   329
    # 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
   330
    # `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
   331
    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
   332
      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
   333
      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
   334
      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
   335
        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
   336
          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
   337
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   338
        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
   339
        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
   340
          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
   341
        else 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   342
          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
   343
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   344
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   345
    end    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   346
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   347
    # 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
   348
    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
   349
      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
   350
      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
   351
      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
   352
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   353
  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
   354
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   355
  # 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
   356
  # 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
   357
  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
   358
    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
   359
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   360
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   361
  # 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
   362
  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
   363
    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
   364
      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
   365
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   366
    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
   367
      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
   368
      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
   369
        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
   370
          next    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   371
        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
   372
          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
   373
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   374
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   375
    end  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   376
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   377
end # module HG