Class: GitCommands::Command

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

Defined Under Namespace

Classes: GitError, NoBranchesError, NoentRepositoryError

Constant Summary collapse

GITHUB_HOST =
"github.com"
UNFINISHED_REBASE_FILES =
%w(rebase-merge rebase-apply)

Constants included from Prompt

Prompt::VALID_ANSWERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Prompt

#confirm, #error, #success, #warning

Constructor Details

#initialize(repo:, branches:, out: STDOUT) ⇒ Command

Returns a new instance of Command.



33
34
35
36
37
38
39
40
# File 'lib/git_commands/command.rb', line 33

def initialize(repo:, branches:, out: STDOUT)
  self.class.check_connection
  @out = out
  @repo = fetch_repo(repo)
  @branches = fetch_branches(String(branches))
  @timestamp = Time.new.strftime("%Y-%m-%d")
  print_branches
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



31
32
33
# File 'lib/git_commands/command.rb', line 31

def out
  @out
end

Class Method Details

.check_connectionObject



17
18
19
20
21
# File 'lib/git_commands/command.rb', line 17

def self.check_connection
  !!Net::HTTP.new(GITHUB_HOST).head("/")
rescue Errno::ENETUNREACH => e
  raise e, "There is no connection!"
end

.git_repo?(repo) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/git_commands/command.rb', line 23

def self.git_repo?(repo)
  `git rev-parse --is-inside-work-tree 2> /dev/null`.strip == "true"
end

.valid_branch?(branch) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/git_commands/command.rb', line 27

def self.valid_branch?(branch)
  `git rev-parse --verify origin/#{branch} 2> /dev/null`.match(/^[0-9a-z]+/)
end

Instance Method Details

#aggregateObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/git_commands/command.rb', line 73

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`
        rebase_with_master
        `git rebase #{aggregate}`
        `git checkout #{aggregate}`
        `git merge #{temp}`
        `git branch -d #{temp}`
        `git checkout master`
      end      
    end
    success "#{aggregate} branch created"
  end
end

#purgeObject



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

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



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

def rebase
  confirm("Proceed rebasing these branches") do
    enter_repo do
      @branches.each do |branch|
        warning("Rebasing branch: #{branch}")
        `git checkout #{branch}`
        `git pull origin #{branch}`
        rebase_with_master
        `git push origin #{branch}`
        `git checkout master`
        `git branch -D #{branch}`
        success "Rebased successfully!"
      end
    end
  end
end