Class: Gitl::Start

Inherits:
SubCommand show all
Defined in:
lib/commands/start.rb

Instance Attribute Summary

Attributes inherited from SubCommand

#gitl_config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SubCommand

#check_uncommit, #run_in_workspace, #save_workspace_config, #workspace_config

Methods inherited from Command

#error, handle_exception, #info, report_error, run

Constructor Details

#initialize(argv) ⇒ Start

Returns a new instance of Start.



23
24
25
26
27
28
# File 'lib/commands/start.rb', line 23

def initialize(argv)
  @working_branch = argv.shift_argument
  @remote_branch = argv.shift_argument
  @force = argv.flag?('force')
  super
end

Class Method Details

.optionsObject



17
18
19
20
21
# File 'lib/commands/start.rb', line 17

def self.options
  [
      ["--force", "忽略工作分支是否存在,强制执行"],
  ].concat(super)
end

Instance Method Details

#runObject



40
41
42
43
44
45
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
# File 'lib/commands/start.rb', line 40

def run
  remote = 'origin'
  workspace_config = WorkSpaceConfig.new(@remote_branch, @working_branch)

  self.gitl_config.projects.each do |project|
    project_path = File.expand_path(project.name, './')
    if File.exist?(project_path)
      g = Git.open(project_path)
    else
      g = Git.clone(project.git, project.name, :path => './')
    end

    if self.verbose?
      # g.setLogger(Logger.new(STDOUT))
    end

    check_uncommit(g, project.name)

    # 更新本地代码
    g.fetch(remote, :p => true, :t => true)
    # g.pull_opts(remote, g.current_branch, :p => true)

    if !g.is_remote_branch?(@remote_branch)
      raise Error.new("remote branch '#{@remote_branch}' does not exist for project '#{project.name}'.")
    end

    if g.is_remote_branch?(@working_branch) && !@force
      raise Error.new("branch '#{@working_branch}' exist in remote '#{remote}' for project '#{project.name}'.")
    end

    if g.is_local_branch?(@working_branch) && !@force
      raise Error.new("branch '#{@working_branch}' exist in local for project '#{project.name}'.")
    end

    # g.remote(remote).branch(@remote_branch).checkout()
    # g.branch(@remote_branch).checkout

    # git_cmd = "git remote set-branches #{remote} '#{@remote_branch}'"
    # puts `#{git_cmd}`.chomp
    #
    # git_cmd = "git fetch --depth 1 #{remote} '#{@remote_branch}'"
    # puts `#{git_cmd}`.chomp
    #
    # $ git remote set-branches origin 'remote_branch_name'
    # $ git fetch --depth 1 origin remote_branch_name
    # $ git checkout remote_branch_name

    g.checkout(@remote_branch)

    g.pull(remote, @remote_branch)

    if g.is_local_branch?(@working_branch)
      g.checkout(@working_branch)
      g.pull(remote, @working_branch)
    else
      puts "create branch '#{@working_branch}' for project '#{project.name}'.".green
      # 创建本地工作分支
      g.checkout(@working_branch, :new_branch => true)
    end

    # 跟踪远程分支
    g.track(remote, @remote_branch)

    puts "push branch '#{@working_branch}' to remote for project '#{project.name}'.".green
    # push到origin
    g.push(remote, @working_branch)

    puts
  end

  #保存新的workspace配置
  self.save_workspace_config(workspace_config)

  puts "create work branch '#{@working_branch}' from #{@remote_branch} and push to '#{remote}' success.".green
end

#validate!Object



30
31
32
33
34
35
36
37
38
# File 'lib/commands/start.rb', line 30

def validate!
  super
  if @working_branch.nil?
    help! 'working_branch is required.'
  end
  if @remote_branch.nil?
    help! 'remote_branch is required.'
  end
end