Module: MSPRelease::Project::Git

Defined in:
lib/msp_release/project/git.rb

Instance Method Summary collapse

Instance Method Details

#clean_checkoutObject



66
67
68
69
70
71
72
# File 'lib/msp_release/project/git.rb', line 66

def clean_checkout
  Dir.chdir(@dir) do
    first_commit_hash, commit_message = find_first_release_commit

    exec "git reset --hard #{first_commit_hash}"
  end
end

#find_first_release_commitObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/msp_release/project/git.rb', line 48

def find_first_release_commit
  all_commits = exec(log_command +  " --pretty=oneline").
    split("\n")

  all_commits.map { |commit_line|
    match = oneline_pattern.match(commit_line)
    [match[1], match[2]]
  }.find {|hash, message|
    release_name_from_message(message)
  }
end

#latest_commit_hashObject



60
61
62
63
# File 'lib/msp_release/project/git.rb', line 60

def latest_commit_hash
  output = exec(log_command + " --pretty=oneline -1").split("\n").first
  oneline_pattern.match(output)[1]
end

#log_commandObject



38
39
40
41
42
# File 'lib/msp_release/project/git.rb', line 38

def log_command
  Dir.chdir(@dir) do
    "git --no-pager log --no-color --full-index"
  end
end

#move_to(pathspec) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/msp_release/project/git.rb', line 22

def move_to(pathspec)
  Dir.chdir(@dir) do
    begin
      exec("git show #{pathspec} --")
    rescue MSPRelease::Exec::UnexpectedExitStatus => e
      if /^fatal: bad revision/.match(e.stderr)
        raise MSPRelease::CLI::Exit, "Git pathspec '#{pathspec}' does not exist"
      else
        raise
      end
    end

    exec("git checkout #{pathspec}")
  end
end

#oneline_patternObject



44
45
46
# File 'lib/msp_release/project/git.rb', line 44

def oneline_pattern
  /^([a-z0-9]+) (.+)$/i
end

#prepare_for_build(branch_name, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/msp_release/project/git.rb', line 5

def prepare_for_build(branch_name, options={})
  branch_is_release_branch = !! /^release-.+$/.match(branch_name)
  shallow_output = options[:shallow_output]

  if branch_name && branch_is_release_branch
    LOG.debug("Checking out latest release commit from origin/#{branch_name}#{shallow_output}")
  else
    LOG.debug("Checking out latest commit from origin/#{branch_name}#{shallow_output}")
  end

  pathspec = "origin/#{branch_name}"
  if pathspec != "origin/master"
    move_to(pathspec)
  end
  super if defined?(super)
end