Class: Commands::Pull

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/pull.rb

Instance Method Summary collapse

Instance Method Details

#optionsObject

holds the options that were passed you can set any initial defaults here



13
14
15
16
# File 'lib/commands/pull.rb', line 13

def options
  @options ||= {
  }
end

#register(opts, global_options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/commands/pull.rb', line 24

def register(opts, global_options)
  opts.banner = "Usage: pull [options]"
  opts.description = "Pull code from remote repo."

  opts.on('-t', "--tag name", "Name of the remote tag that you want to checkout. Warning this will overwrite any local changes.") do |v|
    options[:tag] = v
  end

  opts.on("--remote-version", "Pull from remote version branch into your current branch.") do |v|
    options[:remote_version] = true
  end

  opts.on("--base-version", "Pull from base remote version branch into your current branch.") do |v|
    options[:base_version] = true
  end

  opts.on('-n', "--dry-run", "Perform a dry run.") do |v|
    options[:dry_run] = true
  end
end

#required_optionsObject

required options



19
20
21
22
# File 'lib/commands/pull.rb', line 19

def required_options
  @required_options ||= Set.new [
  ]
end

#run(global_options) ⇒ Object

Parameters:

  • global_options (Object)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/commands/pull.rb', line 46

def run(global_options)

  # see if we can open the config file - we append the .config suffix
  # the file is expected to be in JSON format
  tag = options[:tag]
  remote_version = !!options[:remote_version]
  base_version = !!options[:base_version]
  dry_run = !!options[:dry_run] ? "--dry-run" : ""

  if ARGV.length > 0
    raise "You must specify all arguments with their options."
  end

  if (!tag.nil? && (remote_version || base_version))
    raise "You can't specify both a pull from a tag and a pull from a remote version branch together."
  end

  if (remote_version && base_version)
    raise "You can only specify base-version or remote-version, not both."
  end

  # get config based on name of current dir
  info = EbmSharedLib.get_config_from_top_dir

  # Back up to version parent dir.  This directory contains the top level repos.
  top_dir = Dir.pwd

  merge_failed = false
  repos = info[:repos]
  repos.each do |repo|
    repo_name = EbmSharedLib.get_repo_name(repo[:git_path])
    repo_path = "#{top_dir}/#{repo_name}"
    branch = repo[:branch]
    base_branch = repo[:base_branch]

    puts("\n#{repo_name} pull:\n");

    cmd = "git fetch #{dry_run} --all"
    if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
      raise "Git fetch failed for #{repo_name}."
    end

    if tag
      # use git checkout to force changes from either tag or branch
      cmd = "git checkout -f refs/tags/#{tag}"
      if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
        raise "Git checkout failed for #{repo_name}."
      end
    elsif repo[:create_dev_branch]
      if remote_version
        # pulling from remote version branch
        cmd = "git pull #{dry_run} --no-ff --no-edit --recurse-submodules=yes origin #{branch}"
      elsif base_version
        # pulling from base remote version branch
        if base_branch.nil?
          raise "You specified base-version but the config has no base_branch."
        end
        cmd = "git pull #{dry_run} --no-ff --no-edit --recurse-submodules=yes origin #{base_branch}"
      else
        # pull from the remote branch of the current branch
        cmd = "git pull #{dry_run} --no-ff --no-edit --recurse-submodules=yes"
      end

      exit_code = EbmSharedLib::CL.do_cmd_ignore_str(cmd, "Automatic merge failed", repo_path)
      if exit_code != 0 && exit_code != 999
        raise "Git pull failed for #{repo_name}."
      end
      if exit_code == 999
        merge_failed = true
      end
    end

    cmd = "git submodule sync"
    if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
      raise "Submodule sync for #{repo_name} failed."
    end
    if dry_run.empty?
      cmd = "git submodule update"
    else
      cmd = "git submodule update --no-fetch"
    end
    if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
      raise "Updating submodules for #{repo_name} failed."
    end

  end

  if merge_failed
    raise "Automatic merge failed, you must hand merge the conflicts"
  end

end