require 'build-tool/vcs/base' require 'build-tool/errors' module BuildTool; module VCS class MercurialError < BuildTool::Error; end class MercurialConfiguration < BaseConfiguration def name "mercurial" end attr_accessor :remote def initialize( mod = nil ) super( mod ) @remote = {} end def vcs( mod ) @module = mod Mercurial.new( self ) end def branch self.module.remote_path end end # # Implementation for the hg version control system. # class Mercurial < Base def initialize( config ) super( config ) @remote = {} @vcs = nil end # ### ATTRIBUTES # def name "mercurial" end def fetching_supported? true end # ### METHODS # def checkedout? return false if !local_path_exist? if !Pathname.new( local_path ).join( ".hg" ).exist? raise Base::VcsError, "Checkout path #{local_path} is not a mercurial repo!" end return true end # Initialize the local repository def clone # Check if path exists if local_path_exist? raise MercurialError, "Failed to create repository at '#{local_path}': Path exists" end FileUtils.mkdir_p Pathname.new( local_path ).dirname if ! $noop # Initialize the repository if hg( "clone #{repository.url} #{local_path}", Pathname.new( local_path ).dirname) != 0 raise MercurialError, "Error while initializing the repo `hg init #{local_path}'`: #{$?}" end fetch() rebase() end # Fetch from +repository+ # # Initializes the local clone if it does not exist. def fetch() if !checkedout? and !$noop clone end cmd = "pull #{repository.url}" if ( rc = hg( cmd ) ) != 0 raise MercurialError, "Error while fetching: #{rc}" end end def hg( command, wd = local_path, &block ) rc = self.class.execute "hg #{command}", wd, &block if rc != 0 raise MercurialError, "hg #{command || "" } failed with error code #{rc}"; end rc end def rebase if 0 != ( hg "update #{config.branch}" ) raise MercurialSvnError, "Error while rebasing the repo with `hg update: #{$?}" end end end # class Mercurial end; end # module BuildTool::VCS