Sha256: 15d74068a104dc75fbeded1752d403bf0b4dfb1e5df1fac4e5ff58b1f19f9df6

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module HGTK
  
  # A Mercurial repository.
  class Repo
    # TODO: Swap out for an existing Hg API?
    
    attr_reader :dir
    
    # @param kwargs [Hash]
    # @option kwargs [String?] :dir Path to the repository. Current working
    #   directory by default.
    def initialize **kwargs
      @dir = kwargs.fetch :dir, Dir.pwd
    end
    
    def is_repo?
      Dir.exists? "#{@dir}/.hg"
    end
    
    # Check repo status (uncommitted changes).
    def status
      repo_check
      command 'st'
    end
    
    # Check for incoming changesets.
    def incoming
      repo_check
      command 'incoming'
    end
    
    # Check for outgoing changesets.
    def outgoing
      repo_check
      command 'outgoing'
    end
    
    # Pull incoming changesets.
    def pull update: false
      repo_check
      cmd = 'incoming'
      cmd += ' -u' if update
      command cmd
    end
    
    # Push outgoing changesets.
    def push force: false
      repo_check
      cmd = 'outgoing'
      cmd += ' -f' if force
      command cmd
    end
    
    # Synchronize repository by pulling, then pushing.
    def sync
      repo_check
      pull
      push
    end
    
    
    private
    
    # Execute an hg command at this repo's path.
    def command cmd
      system "hg #{cmd} -R #{dir}"
    end
    
    def repo_check
      raise "Not an Hg repository: #{@dir}" unless is_repo?
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hgtk-0.1.0 lib/hgtk/repo.rb