Class: Lgit::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/lgit.rb

Overview

Git related class

Instance Method Summary collapse

Instance Method Details

#create_branch(name, base) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/lgit.rb', line 27

def create_branch(name, base)
  return unless name

  base ||= get_default_branch

  refresh_base base
  `git checkout -b #{name}`
end

#delete_branchesObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lgit.rb', line 44

def delete_branches
  `git fetch -p`
  `git branch -vv`
    .split("\n")
    .reject { |branch| branch.start_with?('*') }
    .select { |branch| branch.include?(': gone]') }
    .map! { |branch| branch.match(/^\s+(.*?)\s/)[1] }
    .each do |branch|
      `git branch -D #{branch}`
      puts "#{branch} deleted"
    end
end

#get_branchObject



15
16
17
# File 'lib/lgit.rb', line 15

def get_branch
  `git branch --show-current`.strip
end

#get_default_branchObject



19
20
21
22
23
24
25
# File 'lib/lgit.rb', line 19

def get_default_branch
  stdout, _, status = Open3.capture3("git symbolic-ref refs/remotes/origin/HEAD")

  return `git config init.defaultBranch`.strip if status != 0

  stdout.strip[20..-1]
end

#rebase(base) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/lgit.rb', line 36

def rebase(base)
  base ||= get_default_branch

  refresh_base base
  `git checkout - `
  `git rebase #{base}`
end

#refresh_base(base) ⇒ Object



8
9
10
11
12
13
# File 'lib/lgit.rb', line 8

def refresh_base(base)
  base ||= get_default_branch

  `git checkout #{base}`
  `git pull`
end