Class: GitCommands::Computer

Inherits:
Object
  • Object
show all
Includes:
Prompt
Defined in:
lib/git_commands/computer.rb

Defined Under Namespace

Classes: GitError

Constant Summary

Constants included from Prompt

Prompt::VALID_ANSWERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Prompt

#confirm, #error, #success, #warning

Constructor Details

#initialize(repo:, branches:, origin: Branch::ORIGIN, default: Branch::DEFAULT, repo_klass: Repository, branch_klass: Branch, out: STDOUT) ⇒ Computer

Returns a new instance of Computer.



14
15
16
17
18
19
20
21
22
23
# File 'lib/git_commands/computer.rb', line 14

def initialize(repo:, branches:, origin: Branch::ORIGIN, default: Branch::DEFAULT, repo_klass: Repository, branch_klass: Branch, out: STDOUT)
  @out = out
  @repo = repo_klass.new(repo)
  @origin = origin
  @default = default
  Dir.chdir(@repo) do
    @branches = branch_klass.factory(branches)
    print_branches
  end
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



12
13
14
# File 'lib/git_commands/computer.rb', line 12

def out
  @out
end

Instance Method Details

#aggregate(aggregator = Aggregator.new) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/git_commands/computer.rb', line 53

def aggregate(aggregator = Aggregator.new)
  temp = "temp/#{aggregator.timestamp}"
  aggregate_name = aggregator.call 
  confirm("Aggregate branches into #{aggregate_name}") do
    enter_repo do
      `git branch #{aggregate_name}`
      @branches.each do |branch|
        warning("Merging branch: #{branch}")
        `git checkout -b #{temp} origin/#{branch} --no-track`
        clean_and_exit([temp, aggregate_name]) unless rebase_with
        clean_and_exit([temp]) unless rebase_with(aggregate_name)
        `git checkout #{aggregate_name}`
        `git merge #{temp}`
        `git branch -D #{temp}`
      end      
    end
    success("#{aggregate_name} branch created")
  end
end

#rebaseObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/git_commands/computer.rb', line 37

def rebase
  confirm("Proceed rebasing these branches with: #{local_def}") do
    enter_repo do
      @branches.each do |branch|
        warning("Rebasing branch: #{branch}")
        `git checkout #{branch}`
        `git pull -r origin #{branch}`
        next unless rebase_with
        `git push --force-with-lease origin #{branch}`
        success("Rebased successfully!")
      end
      remove_locals
    end
  end
end

#removeObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git_commands/computer.rb', line 25

def remove
  enter_repo do
    confirm("Proceed removing these branches") do
      @branches.each do |branch|
        warning("Removing branch: #{branch}")
        `git branch -D #{branch}` if branch.exists?(false)
        `git push origin :#{branch}`
      end
    end
  end
end