class Roger::Release::Finalizers::GitBranch

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

Public Instance Methods

default_options() click to toggle source

@param Hash options The options

@option options String :remote The remote repository (default is the

origin of the current repository)

@option options String :branch The remote branch (default is “gh-pages”) @option options Boolean :cleanup Cleanup temp dir afterwards (default is

true)

@option options Boolean :push Push to remote (default is true)

# File lib/roger/release/finalizers/git_branch.rb, line 16
def default_options
  {
    remote: nil,
    branch: "gh-pages",
    cleanup: true,
    push: true
  }
end
perform() click to toggle source
# File lib/roger/release/finalizers/git_branch.rb, line 25
def perform
  remote = find_git_remote(release.project.path)
  branch = @options[:branch]

  tmp_dir = Pathname.new(::Dir.mktmpdir)
  clone_dir = tmp_dir + "clone"

  # Check if remote already has branch
  if remote_has_branch?(remote, branch)
    release.log(self, "Cloning existing repo")
    clone_branch(clone_dir, remote, branch)
  else
    release.log(self, "Creating empty branch")
    create_empty_branch(clone_dir, remote, branch)
  end

  @release.log(self, "Working git magic in #{clone_dir}")

  commit_and_push_release(clone_dir, branch)

  if @options[:cleanup]
    FileUtils.rm_rf(tmp_dir)
  else
    tmp_dir
  end
end

Protected Instance Methods

clone_branch(clone_dir, remote, branch) click to toggle source
# File lib/roger/release/finalizers/git_branch.rb, line 102
def clone_branch(clone_dir, remote, branch)
  command = Shellwords.join([
                              "git",
                              "clone",
                              remote,
                              "--branch",
                              branch,
                              "--single-branch",
                              clone_dir
                            ])
  `#{command}`
end
commit_and_push_release(clone_dir, branch) click to toggle source
# File lib/roger/release/finalizers/git_branch.rb, line 54
def commit_and_push_release(clone_dir, branch)
  ::Dir.chdir(clone_dir) do
    # 3. Copy changes
    FileUtils.rm_rf("*")
    FileUtils.cp_r @release.build_path.to_s + "/.", clone_dir.to_s

    commands = [
      "git add .", # 4. Add all files
      %(git commit -q -m "Release #{Shellwords.escape(release.scm.version)}") # 5. Commit
    ]

    # 6. Git push if in options
    commands << ("git push origin" << Shellwords.escape(branch)) if @options[:push]

    commands.each do |command|
      `#{command}`
    end
  end
end
create_empty_branch(clone_dir, remote, branch) click to toggle source
# File lib/roger/release/finalizers/git_branch.rb, line 86
def create_empty_branch(clone_dir, remote, branch)
  commands = [
    %w(git init -q),
    %w(git remote add origin) << remote,
    %w(git checkout -q -b) << branch
  ]

  # Branch does not exist yet
  FileUtils.mkdir(clone_dir)
  ::Dir.chdir(clone_dir) do
    commands.each do |command|
      `#{Shellwords.join(command)}`
    end
  end
end
find_git_remote(path) click to toggle source
# File lib/roger/release/finalizers/git_branch.rb, line 115
def find_git_remote(path)
  if @options[:remote]
    remote = @options[:remote]
  else
    git_dir = Roger::Release::Scm::Git.find_git_dir(path)
    remote = `git --git-dir=#{Shellwords.escape(git_dir.to_s)} config --get remote.origin.url`
  end

  remote.strip!

  raise "No remote found for origin" if remote.nil? || remote.empty?

  remote
end
remote_has_branch?(remote, branch) click to toggle source

Check if remote already has branch

# File lib/roger/release/finalizers/git_branch.rb, line 75
def remote_has_branch?(remote, branch)
  command = Shellwords.join([
                              "git",
                              "ls-remote",
                              "--heads",
                              remote,
                              "refs/heads/#{branch}"
                            ])
  `#{command}` != ""
end