Class: Giblish::GitCheckoutManager

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/gitrepos/checkoutmanager.rb

Overview

acquires a handle to an existing git repo and provide the user with a iteration method ‘each_checkout’ where each matching branch and/or tag is checked out and presented to the user code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_opts) ⇒ GitCheckoutManager

cmd_opts

a CmdLine::Options instance

Required options:

srcdir

Pathname to the top dir of the local git repo to work with

local_only

if true, do not try to access any remote branches or merge with any upstream changes

Optional options:

abort_on_error

bail out at an error or continue. Default ‘true’

branch_regex

the regex for the branches to include during iteration (default: none)

tag_regex

the regex for the tags to include during iteration (default: none)

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/giblish/gitrepos/checkoutmanager.rb', line 25

def initialize(cmd_opts)
  @repo_root = GitItf.find_gitrepo_root(cmd_opts.srcdir)
  raise ArgumentError, "The path: #{cmd_opts.srcdir} is not within a git repo!" if @repo_root.nil?

  @local_only = cmd_opts.local_only
  @abort_on_error = cmd_opts.abort_on_error.nil? ? true : cmd_opts.abort_on_error

  @git_repo = init_git_repo(@repo_root, @local_only)
  @branches = select_user_branches(cmd_opts.branch_regex, @local_only)
  @tags = select_user_tags(cmd_opts.tag_regex)
  @summary_provider = GitSummaryDataProvider.new(@repo_root.basename)
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



13
14
15
# File 'lib/giblish/gitrepos/checkoutmanager.rb', line 13

def branches
  @branches
end

#git_repoObject (readonly)

Returns the value of attribute git_repo.



13
14
15
# File 'lib/giblish/gitrepos/checkoutmanager.rb', line 13

def git_repo
  @git_repo
end

#repo_rootObject (readonly)

Returns the value of attribute repo_root.



13
14
15
# File 'lib/giblish/gitrepos/checkoutmanager.rb', line 13

def repo_root
  @repo_root
end

#summary_providerObject (readonly)

Returns the value of attribute summary_provider.



13
14
15
# File 'lib/giblish/gitrepos/checkoutmanager.rb', line 13

def summary_provider
  @summary_provider
end

#tagsObject (readonly)

Returns the value of attribute tags.



13
14
15
# File 'lib/giblish/gitrepos/checkoutmanager.rb', line 13

def tags
  @tags
end

Instance Method Details

#each_checkoutObject

present each git checkout matching the init criteria to the user’s code.

Example

gcm = GitCheckoutManager.new(opts) gcm.each_checkout do |name| … do things with the currently checked out working tree … end



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/giblish/gitrepos/checkoutmanager.rb', line 46

def each_checkout
  current_branch = @git_repo.current_branch
  checkouts = (@branches + @tags)
  if checkouts.empty?
    Giblog.logger.info { "No matching branches or tags found." }
    return
  end

  checkouts.each do |treeish|
    sync_treeish(treeish)
    # cache branch/tag info for downstream content generation
    @summary_provider.cache_info(@git_repo, treeish)

    yield(treeish.name)
  rescue => e
    Giblog.logger.error { e.message }
    raise e if @abort_on_error
  end

  if @git_repo.current_branch != current_branch
    Giblog.logger.info { "Checking out '#{current_branch}'" }
    @git_repo.checkout current_branch
  end
end