Class: GitCommands::Command

Inherits:
Object
  • Object
show all
Includes:
Prompt
Defined in:
lib/git_commands/command.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:, repo_klass: Repository, branch_klass: Branch, out: STDOUT) ⇒ Command

Returns a new instance of Command.



16
17
18
19
20
21
22
23
24
25
# File 'lib/git_commands/command.rb', line 16

def initialize(repo:, branches:, repo_klass: Repository, branch_klass: Branch, out: STDOUT)
  @out = out
  @repo = repo_klass.new(repo)
  @conflictual = []
  Dir.chdir(@repo) do
    @branches = branch_klass.factory(branches)
    @timestamp = Time.new.strftime("%Y-%m-%d")
    print_branches
  end
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



14
15
16
# File 'lib/git_commands/command.rb', line 14

def out
  @out
end

Instance Method Details

#aggregateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/git_commands/command.rb', line 59

def aggregate
  temp = "temp/#{@timestamp}"
  aggregate = "release/#{@timestamp}"
  confirm("Aggregate branches into #{aggregate}") do
    enter_repo do
      `git branch #{aggregate}`
      @branches.each do |branch|
        warning("Merging branch: #{branch}")
        `git checkout -b #{temp} origin/#{branch} --no-track`
        @conflictual << branch && next unless rebase_with_master
        `git rebase #{aggregate}`
        `git checkout #{aggregate}`
        `git merge #{temp}`
        `git branch -d #{temp}`
      end      
      `git checkout #{Branch::MASTER}`
    end
    delete_conflictual
    success "#{aggregate} branch created"
  end
end

#purgeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/git_commands/command.rb', line 27

def purge
  enter_repo do
    @branches.each do |branch|
      warning("Removing branch: #{branch}")
      confirm("Remove local branch") do
        `git branch -D #{branch}`
      end
      confirm("Remove remote branch") do
        `git push origin :#{branch}`
      end
    end
  end
end

#rebaseObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/git_commands/command.rb', line 41

def rebase
  confirm("Proceed rebasing these branches with master") do
    enter_repo do
      @branches.each do |branch|
        warning("Rebasing branch: #{branch}")
        `git checkout #{branch}`
        `git pull origin #{branch}`
        @conflictual << branch && next unless rebase_with_master
        `git push -f origin #{branch}`
        `git checkout #{Branch::MASTER}`
        `git branch -D #{branch}`
        success "Rebased successfully!"
      end
      delete_conflictual
    end
  end
end