Class: GitBranch

Inherits:
HtmlMockup::Release::Finalizers::Base show all
Defined in:
lib/html_mockup/release/finalizers/git_branch.rb

Overview

Finalizes the release into a specific branch of a repository and pushes it

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GitBranch

Returns a new instance of GitBranch.

Parameters:

  • Hash

    options The options

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • String (Object)

    :remote The remote repository (default is the origin of the current repository)

  • String (Object)

    :branch The remote branch (default is “gh-pages”)

  • Boolean (Object)

    :cleanup Cleanup temp dir afterwards (default is true)

  • Boolean (Object)

    :push Push to remote (default is true)



11
12
13
14
15
16
17
18
# File 'lib/html_mockup/release/finalizers/git_branch.rb', line 11

def initialize(options={})
  @options = {
    :remote => nil,
    :branch => "gh-pages",
    :cleanup => true,
    :push => true
  }
end

Instance Method Details

#call(release, options = {}) ⇒ Object



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
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
# File 'lib/html_mockup/release/finalizers/git_branch.rb', line 21

def call(release, options = {})
  options = @options.dup.update(options)  
  git_dir = find_git_dir(release.project.path)
  
  # 0. Get remote
  unless remote = (options[:remote] || `git --git-dir=#{git_dir} config --get remote.origin.url`).strip
    raise "No remote found for origin"
  end
  
  e_remote = Shellwords.escape(remote)
  e_branch = Shellwords.escape(options[:branch])

  tmp_dir = Pathname.new(Dir.mktmpdir)
  clone_dir = tmp_dir + "clone"
  
  # Check if remote already has branch
  if `git ls-remote --heads #{e_remote} refs/heads/#{e_branch}` == ""
    release.log(self, "Creating empty branch")
    # Branch does not exist yet
    FileUtils.mkdir(clone_dir)
    Dir.chdir(clone_dir) do
      `git init`
      `git remote add origin #{e_remote}`
      `git checkout -b #{e_branch}`
    end
  else
    release.log(self, "Cloning existing repo")
    # 1. Clone into different directory      
    `git clone #{e_remote} --branch #{e_branch} --single-branch #{clone_dir}`
  end
  
  release.log(self, "Working git magic in #{clone_dir}")
  Dir.chdir(clone_dir) do
    # 3. Copy changes
    FileUtils.rm_rf("*")
    FileUtils.cp_r release.build_path.to_s + "/.", clone_dir.to_s
    
    # 4. Add all files
    `git add .`
  
    # 5. Commit
    `git commit -a -m "Release #{release.scm.version}"`

    # 6. Git push
    if options[:push]
      `git push origin #{e_branch}`
    end
  end
  
  if options[:cleanup]
    FileUtils.rm_rf(tmp_dir)
  end
  
end