rakelib/hglib.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 13 Nov 2019 19:08:42 +0000
changeset 280 c012c276634e
parent 272 13ce86e80c30
child 301 0aba3d00b91f
permissions -rw-r--r--
Merge
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
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    16
    $LOGGER.error(%q{Cannot load package 'inifile'})
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    17
    $LOGGER.error(%q{Run 'gem install inifile' to install it})
66
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
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    22
unless $LOGGER
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    23
  if STDOUT.tty? or win32?
66
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)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    26
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    27
    if !VERBOSE.nil?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    28
      $LOGGER.level = Logger::DEBUG
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    29
    else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    30
      $LOGGER.level = Logger::INFO
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
    31
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    32
  else
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    33
    require 'syslog/logger'
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    34
    $LOGGER = Syslog::Logger.new($0)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    35
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    36
end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    37
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    38
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
    39
  @@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
    40
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
    41
  class Exception < ::Exception
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
    42
  end
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
    43
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
    44
  class RepositoryNotFoundException < Exception
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
    45
  end
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
    46
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    47
  # Cross-platform way of finding an executable in the $PATH.
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    48
  #
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    49
  #   which('ruby') #=> /usr/bin/ruby
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    50
  module_function
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    51
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    52
  def which(cmd)
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    53
    exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    54
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    55
      exts.each {|ext|
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    56
        exe = File.join(path, "#{cmd}#{ext}")
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    57
        return exe if File.executable?(exe) && !File.directory?(exe)
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    58
      }
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    59
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    60
    nil
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    61
  end
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
    62
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    63
  GLOBAL_OPTIONS = %i(cwd repository noninteractive config debug debugger encoding
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    64
                      encodingmode traceback time profile version help hidden
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    65
                      )
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    66
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
  # Execute `hg` command with given positional arguments and
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    68
  # keyword arguments turned into command options. For example,
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    69
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    70
  #     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
    71
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    72
  # will result in executing
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    73
  #
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    74
  #     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
    75
  #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    76
  # 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
    77
  # `hg` command exit status (as Process::Status) and (optionally)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    78
  # with contents of `hg` command stdout and stderr.
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    79
  # If no block is given, an exception is raised when `hg` command
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    80
  # 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
    81
  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
    82
    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
    83
    c_opts = []
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    84
    options.each do |k, v|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    85
      if v != false and !v.nil?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    86
        o = k.size == 1 ? "-#{k}" : "--#{k}"
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    87
        if GLOBAL_OPTIONS.include? k
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    88
          if v.is_a?(Array)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    89
            v.each do |e|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    90
              g_opts << o << (e == true ? '' : e) # bug ruby: e ? '' : e does not work correctly
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    91
            end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    92
          else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    93
            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
    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
        else
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    96
          if v.is_a?(Array)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    97
            v.each do |e|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
    98
              c_opts << o << (e == true ? '' : e)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    99
            end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   100
          else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   101
            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
   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
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   106
    c_opts.reject! {|e| e.size == 0}
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   107
    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
   108
    cmd_info = cmd.shelljoin.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   109
        gsub(/username\\=\S+/, "username\\=***").
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   110
        gsub(/password\\=\S+/, "password\\=***")
68
61d8bee7c4d4 Added new tasks - `workflow:push-upstream` and `workflow:push-staging`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 67
diff changeset
   111
    $LOGGER.debug("executing: #{cmd_info}")
138
Patrik Svestka <patrik.svestka@gmail.com>
parents: 137 127
diff changeset
   112
    if defined? RakeFileUtils and RakeFileUtils.verbose; puts cmd_info; end
Patrik Svestka <patrik.svestka@gmail.com>
parents: 137 127
diff changeset
   113
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   114
    if block_given?
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   115
      stdout, stderr, status = Open3.capture3(*cmd)
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   116
      # For command that deal with remotes, handle check whether the failure
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   117
      # is because of missing remote repository. If so, raise exception. 
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   118
      if status.exitstatus == 255
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   119
        if stdout =~ /remote:\ *Repository.*not found/ or stderr =~ /abort:.*HTTP.*404/
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   120
          raise RepositoryNotFoundException.new("Remote repository not found!")
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   121
        end
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   122
      end
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   123
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   124
      case block.arity
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   125
      when 1
228
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   126
        STDOUT.print stdout if defined? RakeFileUtils and RakeFileUtils.verbose
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   127
        STDERR.print stderr if defined? RakeFileUtils and RakeFileUtils.verbose
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   128
        yield status
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   129
      when 2
228
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   130
        STDERR.print stderr if defined? RakeFileUtils and RakeFileUtils.verbose
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   131
        yield status, stdout
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   132
      when 3
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   133
        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
   134
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   135
        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
   136
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   137
    else
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   138
      raise Exception.new("command failed: #{cmd.join(' ')}") unless system(*cmd)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   139
    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
   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
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   142
  def self.config
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   143
    if @@config.nil?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   144
      files = Dir.glob('/etc/mercurial/hgrc.d/*.rc') +
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   145
          ['/etc/mercurial/hgrc',
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   146
           hgrc]
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   147
      if Gem.win_platform?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   148
        hg_exe = HG::which("hg")
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   149
        hgrc_d = File.join(File.dirname(hg_exe), "hgrc.d")
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   150
        if File.directory? (hgrc_d)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   151
          files += Dir.glob("#{hgrc_d}\\*.rc".gsub('\\', '/'))
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   152
        end
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   153
      end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   154
      @@config = IniFile.new
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   155
      files.each do |file|
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   156
        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
   157
          $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
   158
          @@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
   159
        end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   160
      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
   161
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   162
    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
   163
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   164
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   165
  def self.hgrc
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   166
    return File.expand_path('~/.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
   167
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   168
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   169
  class Repository
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   170
    @@HOSTS_ON_LAN = {}
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   171
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   172
    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
   173
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   174
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   175
    private
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   176
    # Return --ssh config string for use with passed remote url or nil
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   177
    # if no special --ssh config is needed.
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   178
    #
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   179
    # Rationale:
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   180
    #
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   181
    # On Windows, most of users tend to use TortoiseHG and hg.exe comming with
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   182
    # it. THG makes hg.exe to use (shipped) plink.exe which is bad for performance
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   183
    # since it uses 16k channel input buffer (!) leading to a pretty slow transfers
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   184
    # (a lot of iowaits...)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   185
    # OpenSSH OTOH has 2MB input buffer which is good though on Windows bit
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   186
    # oversized as Windows TCP window size is fixed to 65k for all connections with
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   187
    # RTT less than 1ms. Still, 65k better then 16k.
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   188
    # As a workaround, look if MSYS2's OpenSSH client is installed and if so, use that
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   189
    # one - but only if `ui.ssh` config option has the default value.
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   190
    # Ugly, isn't it?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   191
    def self.sshconf(uri_string)
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   192
      uri = URI(uri_string)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   193
      ssh = nil
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   194
      if uri.scheme == 'ssh'
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 85
diff changeset
   195
        ssh_in_path = HG::which('ssh') ? true : false
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   196
        if Gem.win_platform?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   197
          # Running on Windows
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   198
          #
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   199
          # Mercurial uses `ssh` by default, so to use `plink.exe`, `ui.ssh`
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   200
          # config option has to be explicitly set.
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   201
          #
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   202
          # It it's set to `plink.exe`, check whether MSYS's `ssh.exe` is available
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   203
          # and if so, change it to `ssh.exe`...
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   204
          ssh_configured = HG::config['ui']['ssh']
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   205
          if /^.*[pP]link.exe"?\s*(-ssh)?\s*(-2)?$/ =~ ssh_configured
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   206
            if ssh_in_path
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   207
              ssh = 'ssh'
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   208
            else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   209
              ssh = %q{"c:\msys64\usr\bin\ssh.exe"} if File.exist? %q{c:\msys64\usr\bin\ssh.exe}
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   210
            end
85
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   211
            # Sigh, we should not tamper with SSH configuration wildly. User may have
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   212
            # her ssh and mercurial properly configured to use `plink.exe` and `pageant`.
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   213
            # If we just start using `ssh.exe` clone/pull might not work beause
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   214
            # `ssh.exe` cannot talk to `pageant`. So, if we don't find OpenSSH's
85
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   215
            # style of agent, don't use `ssh.exe` event if available.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   216
            if ssh
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   217
              if ENV['SSH_AUTH_SOCK']
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   218
                # Good, OpenSSH agent running but still, be nice and  tell the
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   219
                # user SSH configuration has been tampered wirh.
85
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   220
                $LOGGER.info("Passing --ssh \"#{ssh}\" option to 'hg' command for faster transfers")
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   221
              else
85
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   222
                # No agent, no fun. Be nice and give user a hit
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   223
                $LOGGER.warn("Not using -ssh \"#{ssh}\" option because SSH agent is not running")
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   224
                $LOGGER.warn("For faster clone/pull consider using ssh-agent or ssh-pageant (if you want to use PuTTY's pageant)")
6d918f722075 Do not tamper with SSH configuration if SSH agent is not running.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 83
diff changeset
   225
                ssh = nil
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   226
              end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   227
            end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   228
            # Turn off SSH compression - data transferred by Mercurial are either
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   229
            # already compressed or --uncompressed was given to reduce CPU load
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   230
            # in which case passing -C would reduce it further.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   231
            ssh += ' -C' if ssh and HG::Repository::host_on_lan?(uri.host)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   232
          end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   233
        else
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   234
          # Turn off SSH compression - data transferred by Mercurial are either
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   235
          # already compressed or --uncompressed was given to reduce CPU load
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   236
          # in which case passing -C would reduce it further.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   237
          ssh = 'ssh -C' if ssh_in_path and HG::Repository::host_on_lan?(uri.host)
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   238
        end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   239
      end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   240
      return ssh
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   241
    end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   242
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   243
    # Same as class method, but allows for remote aliases
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   244
    def sshconf(remote)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   245
      HG::Repository::sshconf(self.paths[remote] || remote)
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   246
    end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   247
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   248
    # Given a hostname, return true if the host is on local LAN, false
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   249
    # otherwise.
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   250
    #
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   251
    # Results are cached to reduce resolver queries (maybe not worth it,
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   252
    # system may cache answers anyways)
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   253
    #
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   254
    # The detection is rather simplistic - it only checks if host's address
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   255
    # is from private IP range - and only for IPv4.
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   256
    # This may cause problems when using over VPN that assigns private address
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   257
    # (the usuall case). In that case this code will treat is a local
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   258
    # which may result in transfer of uncompressed data over WAN. Not nice,
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   259
    # This should be fixed, somehow. 
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   260
    def self.host_on_lan?(hostname)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   261
      unless @@HOSTS_ON_LAN.has_key? hostname
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   262
        require 'resolv'
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   263
        addr = Resolv.getaddress(hostname)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   264
        # Really poor detection of LAN, but since this is an
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   265
        # optimization, getting this wrong does not hurt.
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   266
        local = (addr.start_with? '192.168.') or (addr.start_with? '10.10.')
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   267
        @@HOSTS_ON_LAN[hostname] = local
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   268
      end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   269
      return @@HOSTS_ON_LAN[hostname]
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   270
    end
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   271
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   272
    public
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   273
    # Clone a repository from given `uri` to given `directory`.
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   274
    # Returns an `HG::Repository` instance representing the repository
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   275
    # clone.
66
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 `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
   277
    # 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
   278
    #
101
32f9287b419a Cleanup: Nuked leftover code for using BCC 5.5.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 89
diff changeset
   279
    def self.clone(uri, directory, noupdate: false, publishing: false)
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   280
      uri_obj = URI(uri)
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
   281
      host = uri_obj.host
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   282
      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
   283
      # 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
   284
      # 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
   285
      # these days) amd saves some CPU cycles.
83
8405c22a8ffd Use customized ssh command for all commands involving data transfer (in addition to `clone`)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 79
diff changeset
   286
      local = HG::Repository::host_on_lan?(URI(uri).host)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   287
      ssh = HG::Repository::sshconf(uri)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   288
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   289
      HG::hg('clone', uri, directory, ssh: ssh, uncompressed: local, noupdate: noupdate, config: "phases.publish=#{publishing}")
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   290
      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
   291
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   292
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   293
    # Initializes an empty repository in given directory. Returns an
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   294
    # `HG::Repository` instance representing the created (empty) repository.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   295
    # TODO: unreachable code!!!
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   296
    def self.init(directory)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   297
      HG::hg('init', directory)
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   298
      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
   299
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   300
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   301
    # 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
   302
    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
   303
      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
   304
      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
   305
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   306
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   307
    def hgrc
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   308
      File.join(@path, '.hg', '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
   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
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   311
    def initialize(directory)
234
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 228
diff changeset
   312
      dir = File.expand_path(directory)
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 228
diff changeset
   313
      if not File.exist?(File.join(dir, '.hg'))
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 228
diff changeset
   314
        raise Exception.new("Not a Mercurial repository (missing .hg directory): #{dir}")
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 228
diff changeset
   315
      end
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 228
diff changeset
   316
      @path = dir
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   317
      initialize_config
120
2c4e52c4c930 Do not use `hg clone ...` to create and pull changes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 101
diff changeset
   318
    end
2c4e52c4c930 Do not use `hg clone ...` to create and pull changes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 101
diff changeset
   319
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   320
    def initialize_config
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   321
      config_file = hgrc
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   322
      if File.exist? (config_file)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   323
        $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
   324
        @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
   325
      else
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   326
        @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
   327
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   328
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   329
120
2c4e52c4c930 Do not use `hg clone ...` to create and pull changes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 101
diff changeset
   330
    # Add / replace given config options and write back
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   331
    # .hg/hgrc
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   332
    def config_set(**options)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   333
      config = IniFile.new(:filename => self.hgrc)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   334
      options.each do |section, keys_and_values|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   335
        keys_and_values.each {|key, value| config[section][key] = value.to_s}
120
2c4e52c4c930 Do not use `hg clone ...` to create and pull changes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 101
diff changeset
   336
      end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   337
      config.write
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   338
      initialize_config
120
2c4e52c4c930 Do not use `hg clone ...` to create and pull changes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 101
diff changeset
   339
    end
2c4e52c4c930 Do not use `hg clone ...` to create and pull changes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 101
diff changeset
   340
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   341
    # Return a hashmap with defined paths (alias => uri)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   342
    def paths
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   343
      @config['paths'].clone
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   344
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   345
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   346
    # 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
   347
    def paths=(paths)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   348
      config_set(paths: paths)
67
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   349
    end
75b6eb7b781c Added support for canonical, upstream and staging repositores.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 66
diff changeset
   350
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   351
    def log(revset, template = "{node|short}\n")
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   352
      log = []
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   353
      hg('log', rev: revset, template: template) do |status, out|
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   354
        if status.success?
228
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   355
          puts out if defined? RakeFileUtils and RakeFileUtils.verbose
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   356
          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
   357
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   358
      end
228
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   359
      return log
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   360
    end
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   361
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   362
    def locate(revset = '.', pattern: '*')
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   363
      files = []
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   364
      hg('locate', rev: revset, include: pattern) do |status, out|
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   365
        if status.success?          
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   366
          files = out.split("\n")
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   367
        end
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   368
      end
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   369
      return files
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   370
    end
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   371
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   372
    # Return current branch name
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   373
    def branch()      
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   374
      hg('branch') do | status, out |
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   375
        if status.success?          
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   376
          return out.strip()
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   377
        end
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   378
      end
8566593080a5 hglib: added `HG::Repository#branch()` and `HG::Repository#locate()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 220
diff changeset
   379
      raise Exception.new("Failed to retrieve current branch")
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   380
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   381
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   382
    # 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
   383
    # 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
   384
    # branch.
220
8faa459a7ee9 Rakefiles: do not consider secret changesets as heads when updating
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 148
diff changeset
   385
    # If `secret_secret` is false, then changesets in 
8faa459a7ee9 Rakefiles: do not consider secret changesets as heads when updating
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 148
diff changeset
   386
    # secret phase are ignored (even if thet're heads    
8faa459a7ee9 Rakefiles: do not consider secret changesets as heads when updating
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 148
diff changeset
   387
    def heads(branch = nil, include_secret: true)
8faa459a7ee9 Rakefiles: do not consider secret changesets as heads when updating
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 148
diff changeset
   388
      revset  = "head()"
8faa459a7ee9 Rakefiles: do not consider secret changesets as heads when updating
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 148
diff changeset
   389
      revset += " and branch('#{branch}')" if branch
8faa459a7ee9 Rakefiles: do not consider secret changesets as heads when updating
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 148
diff changeset
   390
      revset += " and !secret()" if not include_secret
8faa459a7ee9 Rakefiles: do not consider secret changesets as heads when updating
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 148
diff changeset
   391
      return log(revset)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   392
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   393
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   394
    # 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
   395
    # is active
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   396
    def bookmark
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   397
      filename = File.join(@path, '.hg', 'bookmarks.current')
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   398
      if File.exist?(filename)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   399
        file = File.open(filename, 'r')
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   400
        begin
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   401
          bookmark = file.read.chomp
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   402
        ensure
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   403
          file.close
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   404
        end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   405
        bookmark
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   406
      else
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   407
        nil
72
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   408
      end
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   409
    end
3e832d54a4af Added support for checking out a particular revision
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 71
diff changeset
   410
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   411
    # Return a hash "bookmark => revision" of all
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   412
    # 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
   413
    def bookmarks(branch = nil)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   414
      revset = 'bookmark()'
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   415
      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
   416
      bookmarks = {}
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   417
      self.log(revset, "{bookmarks}|{node|short}\n").each do |line|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   418
        bookmarks_per_changeset, changesetid = line.split('|')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   419
        bookmarks_per_changeset.split(' ').each {|bookmark| bookmarks[bookmark] = changesetid}
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   420
      end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   421
      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
   422
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   423
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   424
    # a private helper to generate auth --config options for
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   425
    # HTTP-served repositories
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   426
    protected
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   427
    def authconf(remote, user, 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
   428
      authconf = []
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   429
      unless pass.nil?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   430
        raise Exception.new('Password given but not username! Use user: named param to specify username.') if user.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
   431
        # If user/password is provided, make sure we don't have
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   432
        # username in remote URI. Otherwise Mercurial won't use
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   433
        # password from config!
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   434
        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
   435
        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
   436
        uri = uri.to_s
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   437
        uri_alias = self.paths.has_key? remote ? remote : 'xxx'
71
68c8cccbdec5 Cleanup in `scm.rb`: Unified API of `checkout` and `update` functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 68
diff changeset
   438
        authconf << "auth.#{uri_alias}.prefix=#{uri}"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   439
        authconf << "auth.#{uri_alias}.username=#{user}"
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   440
        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
   441
      end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   442
      authconf
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   443
    end
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   444
269
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   445
    # Return true, if given remote is known to be shaky, i.e., randomly failing.
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   446
    # BitBucket is known to be very, very shaky recently (since summer 2919().
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   447
    def shaky_remote?(remote)
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   448
      remote_url = @config['paths'][remote] || remote
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   449
      return remote_url =~ /bitbucket.org/
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   450
    end
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   451
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   452
    def pull1(remote = 'default', user: nil, pass: nil, rev: nil, bookmarks: nil)
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   453
      hg('pull', remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev, bookmark: bookmarks) do |status, stdout, stderr |
269
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   454
        STDOUT.print stdout
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   455
        case status.exitstatus
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   456
          when 0
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   457
            #nothing
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   458
          when 1
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   459
            raise Exception.new("Failed to 'pull' from #{remote} (update had unresolved conflicts)")
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   460
          else
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   461
            STDERR.print stderr
269
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   462
            raise Exception.new("Failed to 'pull' from #{remote} (exit code #{status.exitstatus})")
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   463
        end
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   464
      end
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   465
    end
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   466
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   467
    public
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   468
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   469
    def incoming(remote = 'default', user: nil, pass: nil, rev: nil)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   470
      hg('incoming', remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev) do |status, stdout|
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   471
        case status.exitstatus
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   472
          when 0
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   473
            STDOUT.print stdout
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   474
          when 1
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   475
            puts "No new changes coming from #{remote}"
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   476
          else
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   477
            STDERR.print stderr
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   478
            raise Exception.new("Failed to do 'incoming' 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
   479
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   480
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   481
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   482
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   483
    def pull(remote = 'default', user: nil, pass: nil, rev: nil, bookmarks: nil)
269
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   484
      if not shaky_remote?(remote)
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   485
        pull1(remote, user: user, pass: pass, rev: rev, bookmarks: bookmarks)
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   486
      else
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   487
        # Remote is shaky, try to pull, if pull fails, wait some time and retry
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   488
        # again. See #shaky_remote?()
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   489
        begin
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   490
          pull1(remote, user: user, pass: pass, rev: rev, bookmarks: bookmarks)
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   491
        rescue RepositoryNotFoundException => rnfe
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   492
          raise rnfe
269
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   493
        rescue Exception
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   494
          delay = 30 + rand * 100
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   495
          puts "Oops, remote is shaky, retrying after #{delay}"
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   496
          sleep(delay)
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   497
          begin
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   498
            pull1(remote, user: user, pass: pass, rev: rev, bookmarks: bookmarks)
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   499
          rescue Exception
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   500
            delay = 30 + rand * 100
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   501
            puts "Oops, remote is shaky, retrying after #{delay}"
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   502
            sleep(delay)
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   503
            pull1(remote, user: user, pass: pass, rev: rev, bookmarks: bookmarks)
e6ccd5be9bcb Rakefiles: move retrying code from `scm.rb` to `hglib.rb`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 234
diff changeset
   504
          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
   505
        end
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   506
      end
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   507
    end
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   508
127
664296ccdb4a Improved tasks `workflow:out-staging` and `workflow:out-upstream`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 124
diff changeset
   509
    def outgoing(remote = 'default', user: nil, pass: nil, rev: nil, &block)      
138
Patrik Svestka <patrik.svestka@gmail.com>
parents: 137 127
diff changeset
   510
      if !block_given?
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   511
      hg('outgoing', remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev) do |status, stdout, stderr|
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   512
        STDOUT.print stdout
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   513
        case status.exitstatus
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   514
          when 0
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   515
            # notning 
127
664296ccdb4a Improved tasks `workflow:out-staging` and `workflow:out-upstream`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 124
diff changeset
   516
          when 1
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   517
            puts "No new changes going to #{remote}"
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   518
          else
272
13ce86e80c30 Rakefiles: raise (new) `HG::RepositoryNotFoundException` when remote repository is missing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 269
diff changeset
   519
            STDERR.print stderr
127
664296ccdb4a Improved tasks `workflow:out-staging` and `workflow:out-upstream`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 124
diff changeset
   520
            raise Exception.new("Failed to do 'outgoing' 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
   521
        end
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   522
      end
127
664296ccdb4a Improved tasks `workflow:out-staging` and `workflow:out-upstream`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 124
diff changeset
   523
      else
138
Patrik Svestka <patrik.svestka@gmail.com>
parents: 137 127
diff changeset
   524
        hg('outgoing', remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev, &block)
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   525
      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
   526
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   527
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   528
    def push(remote = 'default', user: nil, pass: nil, rev: nil, bookmarks: [])
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   529
      hg('push', remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev, bookmark: bookmarks) do |status, stdout, stderr|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   530
        STDOUT.puts stdout
148
3abc438b6c59 Fixed slip in `hglib.rb` introduced in commit e665031cade7
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 138
diff changeset
   531
        unless status.exitstatus == 0 or status.exitstatus == 1
124
d89325244d7c hglib: show `hg` output when pushing to remote repositories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 120
diff changeset
   532
          STDERR.puts stderr
78
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   533
          raise Exception.new("Failed to 'push' from #{remote} (exit code #{status.exitstatus})")
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   534
        end
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   535
      end
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   536
    end
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   537
2d09a485772f Push active bookmark (if any) when running task `workflow:push-staging` or...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 76
diff changeset
   538
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   539
    # 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
   540
    # 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
   541
    def share(dst, rev = nil)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   542
      raise Exception.new("Destination file exists: #{dst}") if File.exist? dst
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   543
      rev = log('.')[0] if rev.nil?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   544
      raise Exception.new("Revision #{rev} does not exist") unless has_revision? rev
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   545
      mkdir_p File.dirname(dst)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   546
      HG::hg('share', path, dst, config: 'extensions.share=', noupdate: true, bookmarks: false)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   547
      share = Repository.new(dst)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   548
      share.update(rev)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   549
      share
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   550
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   551
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   552
    # Updates the repository's working copy to given
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   553
    # 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
   554
    # 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
   555
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   556
    #   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
   557
    #
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   558
    # 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
   559
    def update(rev = nil)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   560
      if rev
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   561
        raise Exception.new("Revision #{rev} does not exist") unless has_revision? rev
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   562
        hg('update', rev: rev)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   563
      else
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   564
        hg('update')
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   565
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   566
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   567
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   568
    # 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
   569
    # 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
   570
    def merge(rev)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   571
      raise Exception.new("Revision #{rev} does not exist") unless has_revision? rev
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   572
      hg('merge', rev) {|status| return status.success?}
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   573
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   574
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   575
    def commit(message, user='')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   576
      user = @config['ui']['username'] unless @config['ui'].has_key? 'username'
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   577
      hg('commit', message: message, user: user)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   578
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   579
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   580
    def has_revision?(rev)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   581
      revs = log(rev)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   582
      revs.size > 0
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   583
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   584
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   585
    # 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
   586
    # 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
   587
    # `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
   588
    def self.lookup(directory)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   589
      return nil unless File.exist?(directory)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   590
      repo_dir = directory
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   591
      until repo_dir.nil?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   592
        return Repository.new(repo_dir) if HG::repository? repo_dir
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   593
        repo_dir_parent = File.dirname(repo_dir)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   594
        repo_dir_parent == repo_dir ? (repo_dir = nil) : (repo_dir = repo_dir_parent)
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   595
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   596
    end    
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   597
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   598
    # 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
   599
    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
   600
      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
   601
      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
   602
      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
   603
    end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   604
  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
   605
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   606
  # 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
   607
  # 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
   608
  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
   609
    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
   610
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   611
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   612
  # Enumerate all repositories in given `directory`
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   613
  def self.forest(directory, &block)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   614
    yield Repository.new(directory) if repository? directory
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   615
    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
   616
      path = File.join(directory, x)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   617
      if File.directory? path
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   618
        if x == '.' or x == '..' or x == '.svn' or x == '.git'
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 124
diff changeset
   619
          next
66
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   620
        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
   621
          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
   622
        end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   623
      end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   624
    end  
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   625
  end
8d2d5dfe94d0 Refactored SCM support to use `hglib.rb` for performing Mercurial related tasks
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   626
end # module HG