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



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/big_keeper/service/git_service.rb', line 93

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

#pull(path, branch_name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/big_keeper/service/git_service.rb', line 105

def pull(path, branch_name)
  git = GitOperator.new
  current_branch_name = git.current_branch(path)
  if current_branch_name == branch_name
    git.pull(path)
  else
    git.checkout(path, branch_name)
    git.pull(path)
    git.checkout(path, current_branch_name)
  end
end

#start(path, name, type) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/big_keeper/service/git_service.rb', line 9

def start(path, name, type)
  git = GitOperator.new

  branch_name = "#{GitflowType.name(type)}/#{name}"
  if !git.has_remote_branch(path, branch_name) && !git.has_local_branch(path, branch_name)

    verify_special_branch(path, 'master')
    verify_special_branch(path, 'develop')

    GitflowOperator.new.start(path, name, type)
    git.push_to_remote(path, branch_name)
  else
    verify_checkout(path, branch_name)

    if !git.has_remote_branch(path, branch_name)
      git.push_to_remote(path, branch_name)
    end
  end
end

#verify_checkout(path, branch_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/big_keeper/service/git_service.rb', line 29

def verify_checkout(path, branch_name)
  Dir.chdir(path) do
    cmd = "git checkout -b #{branch_name}"
    if GitOperator.new.has_branch(path, branch_name)
      cmd = "git checkout #{branch_name}"
    end
    IO.popen(cmd) do |io|
      io.each do |line|
        Logger.error("Checkout #{branch_name} failed.") if line.include? 'error'
      end
    end
  end
end

#verify_del(path, branch_name, name, type) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/big_keeper/service/git_service.rb', line 117

def verify_del(path, branch_name, name, type)
  git = GitOperator.new

  if git.has_local_branch(path, branch_name)
    Logger.highlight("Delete local branch '#{branch_name}' for '#{name}'...")

    if git.current_branch(path) == branch_name
      git.dicard(path)
      git.checkout(path, GitflowType.base_branch(type))
    end
    git.del_local(path, branch_name)
  end

  if git.has_remote_branch(path, branch_name)
    Logger.highlight("Delete remote branch '#{branch_name}' for '#{name}'...")
    git.del_remote(path, branch_name)
  end
end

#verify_home_branch(path, branch_name, type) ⇒ Object



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

def verify_home_branch(path, branch_name, type)
  Logger.highlight('Sync local branchs from remote, waiting...')
  git = GitOperator.new

  git.fetch(path)

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

#verify_push(path, comment, branch_name, name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/big_keeper/service/git_service.rb', line 136

def verify_push(path, comment, branch_name, name)
  git = GitOperator.new
  if git.has_changes(path)
    git.commit(path, comment)
    if git.has_remote_branch(path, branch_name)
      Dir.chdir(path) do
        `git push`
      end
    else
      git.push_to_remote(path, branch_name)
    end
  else
    Logger.default("Nothing to push for '#{name}'.")
  end
end

#verify_rebase(path, branch_name, name) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/big_keeper/service/git_service.rb', line 152

def verify_rebase(path, branch_name, name)

  # pull rebased branch
  pull(path, branch_name)

  Dir.chdir(path) do
    IO.popen("git rebase #{branch_name} --ignore-whitespace") do |io|
      unless io.gets
        Logger.error("#{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'
        Logger.error("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
    if GitOperator.new.current_branch(path) != 'develop' && GitOperator.new.current_branch(path) != 'master'
      `git push -f`
      GitOperator.new.checkout(path, branch_name)
    end
  end
end

#verify_special_branch(path, name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/big_keeper/service/git_service.rb', line 43

def verify_special_branch(path, name)
  git = GitOperator.new

  if git.has_remote_branch(path, name)
    if git.has_local_branch(path, name)
      if git.has_commits(path, name)
        Logger.error(%Q('#{name}' has unpushed commits, you should fix it manually...))
      end
      pull(path, name)
    else
      git.checkout(path, name)
    end
  else
    verify_checkout(path, name)
    git.push_to_remote(path, name)
  end
end