Class: Gerrit::Cli::Command::Clone

Inherits:
Base
  • Object
show all
Defined in:
lib/gerrit/cli/command/clone.rb

Overview

A thin wrapper around git-clone that attempts to install commit-msg hooks that automatically insert the ChangeID lines used by Gerrit.

Instance Attribute Summary

Attributes inherited from Base

#option_parser

Instance Method Summary collapse

Methods inherited from Base

#name, #show_usage, #summary

Constructor Details

#initialize(logger, runner) ⇒ Clone

Returns a new instance of Clone.



10
11
12
13
14
# File 'lib/gerrit/cli/command/clone.rb', line 10

def initialize(logger, runner)
  super(logger)

  @runner = runner
end

Instance Method Details

#install_commit_hooks(parsed_repo_uri, repo_dir) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gerrit/cli/command/clone.rb', line 59

def install_commit_hooks(parsed_repo_uri, repo_dir)
  hook_src = "#{parsed_repo_uri.host}:hooks/commit-msg"
  if parsed_repo_uri.user
    hook_src = "#{parsed_repo_uri.user}@#{hook_src}"
  end

  hook_dst = "#{repo_dir}/.git/hooks"

  gerrit_port = parsed_repo_uri.port || Gerrit::Cli::DEFAULT_GERRIT_PORT

  @logger.info("\nInstalling commit-msg hooks into '#{hook_dst}'.")
  @runner.system!("scp -p -P #{gerrit_port} #{hook_src} #{hook_dst}")
end

#install_tracked_hooks(repo_dir) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/gerrit/cli/command/clone.rb', line 73

def install_tracked_hooks(repo_dir)
  Dir.chdir(repo_dir) do
    if File.executable?("git/install-hook-symlinks")
      @logger.info("\nInstalling tracked git hooks: ")
      @runner.system!("git/install-hook-symlinks")
    end
  end
end

#run(argv) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gerrit/cli/command/clone.rb', line 27

def run(argv)
  args = @option_parser.parse(argv)

  repo_uri, repo_dir = nil, nil
  case args.length
  when 2
    repo_uri, repo_dir = args
  when 1
    repo_uri = args[0]
  else
    raise Gerrit::Cli::UsageError.new("Incorrect number of arguments")
  end

  @runner.system!("git clone #{repo_uri} #{repo_dir}")

  # At this point the uri must be valid, otherwise the clone would have
  # failed
  parsed_repo_uri = URI.parse(repo_uri)

  unless repo_dir
    if parsed_repo_uri.path =~ /\/([^\/]+?)(.git)?$/
      repo_dir = $1
    else
      emsg = "Failed to determine the directory the repo was cloned into."
      raise Gerrit::Cli::Error.new(emsg)
    end
  end

  install_commit_hooks(parsed_repo_uri, repo_dir)
  install_tracked_hooks(repo_dir)
end

#setup_option_parserObject



16
17
18
19
20
21
# File 'lib/gerrit/cli/command/clone.rb', line 16

def setup_option_parser
  super

  @option_parser.banner =
    "Clone a Gerrit hosted repo and install commit-msg hooks."
end

#usageObject



23
24
25
# File 'lib/gerrit/cli/command/clone.rb', line 23

def usage
  "Usage: gerrit clone [options] <repo> [<dir>]\n\n" + @option_parser.help
end