rakelib/hglib.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 20 Nov 2016 23:28:58 +0000
changeset 76 df28d45f7f5a
parent 75 9b57c88b2ab3
child 78 2d09a485772f
permissions -rwxr-xr-x
On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH On Windows, most of users tend to use `plink.exe` as SSH client. Moreover, TortoiseHG comes with its own version if plink and it's pre-configured to use it. This results in a bad performance over high-speed LAN since plink uses 16k channel input buffer (!) leading to a pretty slow transfers (a lot of iowaits...) OTOH, OpenSSH client has 2MB input buffer which is much better. Sadly, does not help as much on Windows as TCP window size is fixed to 65k. Windows TCP window autotuning (CTCP) does not help as it's only enabled on connections with RTT > 1ms which is clearly not the case of high-speed low-latencly LAN. So, unless you hack OpenSSH sources to manually increase TCP window size to match 2MB channel buffer, you're doomed to slow transfers. How nice! Still, 65k is 4 times more than 16k, so still worth the hassle. As a workaround, look if MSYS2's OpenSSH client is installed and if so, use that one - but only if `ui.ssh` config option has the default value. This is soo ugly, isn't it? But it actually makes checkout over high-speed LAN ~8times faster on Windows, believe it or not! (at least on my setup). Sigh, I need a double scotch.
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
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    22
# Cross-platform way of finding an executable in the $PATH.
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    23
#
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    24
#   which('ruby') #=> /usr/bin/ruby
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    25
def which(cmd)
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    26
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    27
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    28
    exts.each { |ext|
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    29
      exe = File.join(path, "#{cmd}#{ext}")
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    30
      return exe if File.executable?(exe) && !File.directory?(exe)
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    31
    }
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    32
  end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    33
  return nil
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    34
end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    35
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    36
if not $LOGGER then
69
23cdc822cfc5 Initial support for Jenkins pipelines.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
    37
  if STDOUT.tty? or win32? then
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    38
    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
    39
    $LOGGER = Logger.new(STDOUT)
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    40
    
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    41
	if (VERBOSE != nil) then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    42
	    $LOGGER.level = Logger::DEBUG
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    43
	else 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    44
	   $LOGGER.level = Logger::INFO	
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
    45
	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
    46
  else 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    47
    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
    48
    $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
    49
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    50
end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    51
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    52
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
    53
  @@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
    54
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    55
  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
    56
       :'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
    57
       :'hidden' ]
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    58
  # 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
    59
  # 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
    60
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    61
  #     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
    62
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    63
  # 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
    64
  # 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    65
  #     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
    66
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    67
  # 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
    68
  # `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
    69
  # 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
    70
  # 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
    71
  # 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
    72
  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
    73
    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
    74
    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
    75
    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
    76
      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
    77
        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
    78
        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
    79
          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
    80
            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
    81
              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
    82
            end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    83
          else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    84
            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
    85
          end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    86
        else
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 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
    88
            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
    89
              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
    90
            end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    91
          else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    92
            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
    93
          end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    94
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    95
      end
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
    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
    98
    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
    99
    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
   100
                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
   101
                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
   102
    $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
   103
    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
   104
      puts cmd_info
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   105
    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
   106
    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
   107
      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
   108
      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
   109
      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
   110
        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
   111
      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
   112
        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
   113
      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
   114
        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
   115
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   116
        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
   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
    else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   119
      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
   120
        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
   121
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   122
    end    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   123
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   124
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   125
  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
   126
    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
   127
      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
   128
          [ '/etc/mercurial/hgrc' ,
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   129
          hgrc() ]
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   130
	  if Gem.win_platform? then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   131
	     hg_exe = which("hg")
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   132
		 hgrc_d = File.join(File.dirname(hg_exe), "hgrc.d")
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   133
		 if File.directory? (hgrc_d) then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   134
			files += Dir.glob("#{hgrc_d}\\*.rc".gsub('\\', '/'))
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   135
		 end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   136
	  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
   137
      @@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
   138
      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
   139
        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
   140
          $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
   141
          @@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
   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
      end  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   144
    end
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 @@config
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 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
   149
  	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
   150
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   151
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   152
  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
   153
    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
   154
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   155
    # 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
   156
    # 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
   157
    # clone. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   158
    # 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
   159
    # 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
   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 self.clone(uri, directory, noupdate: false)
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   162
	  uri_obj = URI(uri)
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   163
      host = uri_obj.host
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   164
	  scheme = uri_obj.scheme
75
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   165
      # When cloning over LAN, use --uncompressed option
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   166
      # as it tends to be faster if bandwidth is good (1GB norm
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   167
      # these days) amd saves some CPU cycles.
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   168
	  local = false
75
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   169
      if host
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   170
        require 'resolv'
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   171
        addr = Resolv.getaddress(host)
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   172
        # Really poor detection of LAN, but since this is an 
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   173
        # optimization, getting this wrong does not hurt.         
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   174
        local = (addr.start_with? '192.168.') or (addr.start_with? '10.10.')
75
9b57c88b2ab3 Use `hg clone` instead of `hg init` followed by `hg pull` to checkout a Mercurial repository
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 73
diff changeset
   175
      end
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   176
	  # On Windows, most of users tend to use TortoiseHG and hg.exe comming with
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   177
	  # it. THG makes hg.exe to use (shipped) plink.exe which is bad for performance 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   178
	  # since it uses 16k channel input buffer (!) leading to a pretty slow transfers 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   179
	  # (a lot of iowaits...)
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   180
	  # OpenSSH OTOH has 2MB input buffer which is good though on Windows bit 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   181
	  # oversized as Windows TCP window size is fixed to 65k for all connections with 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   182
	  # RTT less than 1ms. Still, 65k better then 16k. 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   183
	  # As a workaround, look if MSYS2's OpenSSH client is installed and if so, use that 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   184
	  # one - but only if `ui.ssh` config option has the default value. 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   185
	  # Ugly, isn't it? 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   186
	  ssh = nil
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   187
	  puts "1 #{HG::config['ui'].has_key?('ssh')}"
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   188
	  if (scheme == 'ssh') and (Gem.win_platform?) and (HG::config['ui'].has_key?('ssh')) then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   189
	    # For THG, `ui.ssh` is configured as: 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   190
		# 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   191
		#     "C:\Program Files\TortoiseHg\lib\TortoisePlink.exe" -ssh -2
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   192
		#
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   193
		# Be more relaxed and conver all "standard" plink.exe usages...
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   194
		if File.exist? "c:\\msys64\\usr\\bin\\ssh.exe" then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   195
			# Rename. only if user did not override the setting! 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   196
			if /^.*[pP]link.exe"?\s*(-ssh)?\s*(-2)?$/ =~ HG::config['ui']['ssh'] then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   197
				ssh = "\"c:\\msys64\\usr\\bin\\ssh.exe\""
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   198
			end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   199
			# Since we're messing wth ssh config anyway, add -C if we're cloning "over LAN"
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   200
			# to save some CPU cycles. Same reasosing as for --uncompressed above. 
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   201
			if local then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   202
				ssh += " -C"
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   203
			end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   204
		end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   205
	  end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   206
	  
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   207
	  # A downside of messing with ssh configuration is that OpenSSH client does not know how
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   208
	  # to connect to pageant. So issue a warning...
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   209
	  if ssh then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   210
		$LOGGER.warn("Passing --ssh \"#{ssh}\" option to 'hg clone' for better performance.")
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   211
		if not ENV['SSH_AUTH_SOCK'] then
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   212
			$LOGGER.warn("Clone may fail since MSYS2 `ssh.exe` dont know how to talk to pageant. ")
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   213
			$LOGGER.warn("Consider using ssh-pageant")
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   214
		end
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   215
	  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
   216
      if noupdate then
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   217
        HG::hg("clone", uri, directory, ssh: ssh, uncompressed: local, noupdate: true)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   218
      else
76
df28d45f7f5a On Windows use MSYS2 `ssh.exe` rather than `plink.exe` to clone/checkout repositories over SSH
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 75
diff changeset
   219
        HG::hg("clone", uri, directory, ssh: ssh, uncompressed: local)
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
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   221
      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
   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
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   224
    # 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
   225
    # `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
   226
    def self.init(directory)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   227
      HG::hg("init", directory)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   228
      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
   229
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   230
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   231
    # 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
   232
    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
   233
      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
   234
      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
   235
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   236
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   237
    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
   238
      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
   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
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   241
    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
   242
      @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
   243
      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
   244
      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
   245
        $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
   246
        @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
   247
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   248
        @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
   249
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   250
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   251
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   252
    # 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
   253
    def paths() 
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   254
      return @config['paths'].clone
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   255
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   256
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   257
    # 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
   258
    def paths=(paths)
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   259
      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
   260
      config['paths'] = paths
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   261
      config.write()
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   262
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   263
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   264
    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
   265
      log = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   266
      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
   267
        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
   268
          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
   269
          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
   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
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   272
      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
   273
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   274
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   275
    # 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
   276
    # 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
   277
    # branch.
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   278
    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
   279
      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
   280
        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
   281
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   282
        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
   283
      end
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
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   286
    # Return name of an active bookmark or nil if no bookmark
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   287
    # is active
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   288
    def bookmark() 
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   289
      filename = File.join(@path, '.hg', 'bookmarks.current')
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   290
      if File.exist? filename then
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   291
        file = File.open(filename, "r")
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   292
        begin
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   293
          bookmark = file.read.chomp
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   294
        ensure
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   295
          file.close()
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   296
        end
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   297
        return bookmark
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   298
      else
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   299
        return nil
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   300
      end
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   301
    end
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   302
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   303
    # 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
   304
    # bookmarks. 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   305
    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
   306
      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
   307
      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
   308
      bookmarks = {}
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   309
      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
   310
        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
   311
        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
   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
      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
   314
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   315
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   316
    def pull(remote = 'default', user: nil, pass: nil, rev: nil, bookmarks: [])
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   317
      authconf = []
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   318
      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
   319
        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
   320
          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
   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
        # 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
   323
        # 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
   324
        # 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
   325
        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
   326
        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
   327
        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
   328
        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
   329
        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
   330
        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
   331
        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
   332
      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
   333
      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
   334
        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
   335
          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
   336
        end
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
    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
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   340
    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
   341
      authconf = []
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 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
   343
        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
   344
          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
   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
        # 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
   347
        # 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
   348
        # 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
   349
        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
   350
        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
   351
        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
   352
        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
   353
        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
   354
        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
   355
        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
   356
      end      
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   357
      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
   358
        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
   359
          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
   360
        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
   361
      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
   362
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   363
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   364
    # 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
   365
    # 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
   366
    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
   367
      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
   368
        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
   369
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   370
      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
   371
        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
   372
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   373
      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
   374
        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
   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
      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
   377
      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
   378
      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
   379
      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
   380
      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
   381
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   382
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   383
    # 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
   384
    # 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
   385
    # 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
   386
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   387
    #   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
   388
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   389
    # 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
   390
    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
   391
      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
   392
        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
   393
          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
   394
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   395
        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
   396
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   397
        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
   398
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   399
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   400
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   401
    # 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
   402
    # 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
   403
    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
   404
      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
   405
        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
   406
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   407
      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
   408
        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
   409
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   410
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   411
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   412
    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
   413
      user = ''
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   414
      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
   415
        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
   416
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   417
      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
   418
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   419
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   420
    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
   421
    	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
   422
    	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
   423
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   424
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   425
    # 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
   426
    # 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
   427
    # `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
   428
    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
   429
      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
   430
      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
   431
      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
   432
        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
   433
          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
   434
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   435
        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
   436
        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
   437
          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
   438
        else 
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   439
          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
   440
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   441
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   442
    end    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   443
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   444
    # 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
   445
    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
   446
      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
   447
      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
   448
      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
   449
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   450
  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
   451
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   452
  # 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
   453
  # 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
   454
  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
   455
    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
   456
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   457
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   458
  # 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
   459
  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
   460
    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
   461
      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
   462
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   463
    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
   464
      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
   465
      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
   466
        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
   467
          next    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   468
        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
   469
          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
   470
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   471
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   472
    end  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   473
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   474
end # module HG