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



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

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



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

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')
        Logger.error(%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')
        Logger.error(%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.first_push(path, 'develop') if !GitOperator.new.has_remote_branch(path, 'develop')
    GitOperator.new.first_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.first_push(path, branch_name)
  end
end

#verify_branch(path, branch_name, type) ⇒ Object



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

def verify_branch(path, branch_name, type)
  Logger.highlight('Sync local branchs from remote, waiting...')
  GitOperator.new.git_fetch(path)

  if OperateType::START == type
    if GitOperator.new.current_branch(path) == branch_name
      Logger.error(%(Current branch is '#{branch_name}' already. Use 'update' please))
    end
    if GitOperator.new.has_branch(path, branch_name)
      Logger.error(%(Branch '#{branch_name}' already exists. Use 'switch' please))
    end
  elsif OperateType::SWITCH == type
    if !GitOperator.new.has_branch(path, branch_name)
      Logger.error(%(Can't find a branch named '#{branch_name}'. Use 'start' please))
    end
    if GitOperator.new.current_branch(path) == branch_name
      Logger.error(%(Current branch is '#{branch_name}' already. Use 'update' please))
    end
  elsif OperateType::UPDATE == type
    if !GitOperator.new.has_branch(path, branch_name)
      Logger.error(%(Can't find a branch named '#{branch_name}'. Use 'start' please))
    end
    if GitOperator.new.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



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

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)
      git.push(path)
    else
      git.first_push(path, branch_name)
    end
  else
    Logger.default("Nothing to push for '#{name}'.")
  end
end

#verify_rebase(path, branch_name, name) ⇒ Object



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

def verify_rebase(path, branch_name, 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
    `git push -f origin #{branch_name}`
    GitOperator.new.git_checkout(path, 'develop')
  end
end