Method: TYCiCore::Git.git_checkout_branch

Defined in:
lib/tuya/ci/core/git.rb

.git_checkout_branch(target_branch) ⇒ Object



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
138
# File 'lib/tuya/ci/core/git.rb', line 93

def self.git_checkout_branch(target_branch)
  puts "Git checkout branch: #{target_branch}".green
  commands = [
    %W(add -u),
    %W(reset --hard),
    %W(remote prune origin),
    %W(fetch)
  ]
  EXE.multi_exe('git', commands, true )
  commands = []
  if git_detached?
    puts "Current status is HEAD detached".green
    in_target_branch = false
  else
    current_branch = git_current_branch
    puts "Current branch is #{current_branch}".green
    in_target_branch = current_branch == target_branch
    commands << %W(rebase)
  end

  unless in_target_branch
    remote_exist = git_branch_exist_remote? target_branch
    local_exist = git_branch_exist_local? target_branch
    puts "Git local branch: #{target_branch} is exist?: #{local_exist}".green
    puts "Git remote branch: #{target_branch} is exist?: #{remote_exist}".green
    if local_exist
      commands << %W(checkout #{target_branch})
      unless remote_exist
        commands << %W(git push --set-upstream origin #{target_branch})
      end
    else
      if remote_exist
        commands << %W(checkout #{target_branch})
      else
        commands << %W(checkout -b #{target_branch})
        commands << %W(push --set-upstream origin #{target_branch})
      end
    end
  end

  commands << %W(remote prune origin)
  commands << %W(fetch)
  commands << %W(rebase)

  EXE.multi_exe('git', commands, true )
end