Class: Branchtree::Commands::Update

Inherits:
Common
  • Object
show all
Defined in:
lib/branchtree/commands/update.rb

Instance Method Summary collapse

Methods inherited from Common

#load_situation, #load_tree, #pluralize

Methods included from Branchtree::Context

logger

Instance Method Details

#executeObject



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/branchtree/commands/update.rb', line 25

def execute
  super

  situation = load_situation
  tree = load_tree

  to_push = []

  tree.breadth_first do |level, branch|
    next if branch.root? && !params[:root]
    branch.info.populate
    next if branch.info.behind_parent.zero?

    if branch.info.behind_upstream > 0 && branch.info.ahead_upstream > 0
      logger.error "#{branch.name} has diverged from its upstream."
      logger.error "Please resolve this with a force push or reset --hard, then run again."
      exit 1
    end

    before = branch.info.behind_upstream

    logger.info "#{branch.name} is #{pluralize(branch.info.behind_parent, "commit")} behind its parent branch."
    logger.info "Checking out #{branch.name}."
    branch.checkout

    success = false
    if branch.rebase?
      logger.info "Rebasing #{branch.name} on #{branch.parent_branch_name}."
      success = branch.rebase_parent.success?
    else
      logger.info "Merging #{branch.parent_branch_name} into #{branch.name}."
      success = branch.merge_parent.success?
    end

    unless success
      logger.error "Please resolve these problems and run again."
      exit 1
    end

    logger.success "#{branch.name} is now up to date."

    branch.info.repopulate

    after = branch.info.behind_upstream
    to_push << branch if before != after
  end

  logger.success "All branches are now up to date."

  if params[:push] && to_push.size > 0
    chosen = prompt.multi_select("Push changed branches?") do |menu|
      to_push.each do |branch|
        option_name = branch.name.dup
        option_name << " (force)" if branch.rebase?

        menu.choice option_name, branch
      end

      menu.default *(1..to_push.size)
    end

    forced, unforced = chosen.partition(&:rebase?)
    unless forced.empty?
      logger.info "Force pushing #{pluralize(forced.size, "branch", plural: "branches")}."
      qcmd.run("git", "push", "--force-with-lease", "origin", *forced.map(&:name))
    end
    unless unforced.empty?
      logger.info "Pushing #{pluralize(unforced.size, "branch", plural: "branches")}."
      qcmd.run("git", "push", "origin", *unforced.map(&:name))
    end
    logger.success "Goodbye."
  end
end