Class: BigKeeper::GitService

Inherits:
Object
  • Object
show all
Defined in:
lib/big_keeper/service/git_service.rb

Overview

Operator for got

Instance Method Summary collapse

Instance Method Details

#branchs_with_type(path, type) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/big_keeper/service/git_service.rb', line 79

def branchs_with_type(path, type)
  branchs = []
  Dir.chdir(path) do
    IO.popen('git branch -a') do |io|
      io.each do |line|
        branchs << line.rstrip if line =~ /(\* |  )#{GitflowType.name(type)}*/
      end
    end
  end
  branchs
end

#start(path, name, type) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/big_keeper/service/git_service.rb', line 8

def start(path, name, type)
  if GitOperator.new.has_remote_branch(path, 'master')
    if GitOperator.new.has_local_branch(path, 'master')
      if GitOperator.new.has_commits(path, 'master')
        raise %Q('master' has unpushed commits, you should fix it)
      else
        GitOperator.new.git_checkout(path, 'master')
        GitOperator.new.pull(path)
      end
    else
      GitOperator.new.git_checkout(path, 'master')
    end
  end

  if GitOperator.new.has_remote_branch(path, 'develop')
    if GitOperator.new.has_local_branch(path, 'develop')
      if GitOperator.new.has_commits(path, 'develop')
        raise %Q('develop' has unpushed commits, you should fix it)
      else
        GitOperator.new.git_checkout(path, 'develop')
        GitOperator.new.pull(path)
      end
    else
      GitOperator.new.git_checkout(path, 'develop')
    end
  end

  if !GitflowOperator.new.verify_git_flow(path)
    GitOperator.new.push(path, 'develop') if !GitOperator.new.has_remote_branch(path, 'develop')
    GitOperator.new.push(path, 'master') if !GitOperator.new.has_remote_branch(path, 'master')
  end

  branch_name = "#{GitflowType.name(type)}/#{name}"
  if GitOperator.new.has_branch(path, branch_name)
    GitOperator.new.git_checkout(path, branch_name)
    GitOperator.new.pull(path)
  else
    GitflowOperator.new.start(path, name, type)
    GitOperator.new.push(path, branch_name)
  end
end

#verify_branch(path, branch_name, type) ⇒ Object



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
# File 'lib/big_keeper/service/git_service.rb', line 50

def verify_branch(path, branch_name, type)
  GitOperator.new.git_fetch(path)

  if OperateType::START == type
    if GitOperator.new.current_branch(path) == branch_name
      raise %(Current branch is '#{branch_name}' already. Use 'update' please)
    end
    if GitOperator.new.has_branch(path, branch_name)
      raise %(Branch '#{branch_name}' already exists. Use 'switch' please)
    end
  elsif OperateType::SWITCH == type
    if !GitOperator.new.has_branch(path, branch_name)
      raise %(Can't find a branch named '#{branch_name}'. Use 'start' please)
    end
    if GitOperator.new.current_branch(path) == branch_name
      raise %(Current branch is '#{branch_name}' already. Use 'update' please)
    end
  elsif OperateType::UPDATE == type
    if !GitOperator.new.has_branch(path, branch_name)
      raise %(Can't find a branch named '#{branch_name}'. Use 'start' please)
    end
    if GitOperator.new.current_branch(path) != branch_name
      raise %(Current branch is not '#{branch_name}'. Use 'switch' please)
    end
  else
    raise %(Not a valid command for '#{branch_name}'.)
  end
end

#verify_rebase(path, branch_name, name) ⇒ Object



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
# File 'lib/big_keeper/service/git_service.rb', line 91

def verify_rebase(path, branch_name, name)
  Dir.chdir(path) do
    IO.popen("git rebase #{branch_name} --ignore-whitespace") do |io|
      unless io.gets
        raise "#{name} is already in a rebase-apply, Please:\n\
              1.Resolve it;\n\
              2.Commit the changes;\n\
              3.Push to remote;\n\
              4.Create a MR;\n\
              5.Run 'finish' again."
      end
      io.each do |line|
        next unless line.include? 'Merge conflict'
        raise "Merge conflict in #{name}, Please:\n\
              1.Resolve it;\n\
              2.Commit the changes;\n\
              3.Push to remote;\n\
              4.Create a MR;\n\
              5.Run 'finish' again."
      end
    end
    `git push -f origin #{branch_name}`
    GitOperator.new.git_checkout(path, 'develop')
  end
end