Class: Bruw::Git

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

Class Method Summary collapse

Class Method Details

.branch(remote, branch) ⇒ Object



33
34
35
36
# File 'lib/bruw/git.rb', line 33

def self.branch(remote, branch)
  `git branch -D #{branch}` if branch_exists?(branch)
  `git checkout #{remote}/#{branch} && git switch -c #{branch}`
end

.branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/bruw/git.rb', line 60

def self.branch_exists?(branch)
  !`git branch --list #{branch}`.empty?
end

.cmd_exists?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/bruw/git.rb', line 52

def self.cmd_exists?(cmd)
  !`which #{cmd}`.empty?
end

.fetch(remote) ⇒ Object



38
39
40
# File 'lib/bruw/git.rb', line 38

def self.fetch(remote)
  `git fetch #{remote}`
end

.find_path(remote) ⇒ Object



28
29
30
31
# File 'lib/bruw/git.rb', line 28

def self.find_path(remote)
  path = `git remote -v | grep #{remote} | cut -d' ' -f1 | head -n1`.split
  path[1].match?("https") ? path[1].split(":")[1].split("com/")[1].split(".")[0] : path[1].split(":")[1].split(".")[0]
end

.git?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/bruw/git.rb', line 56

def self.git?
  Dir.exist?(".git")
end

.local_changes?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/bruw/git.rb', line 64

def self.local_changes?
  !`git status`.match(/Changes not staged for commit:/).nil?
end

.prune_remotes(remotes) ⇒ Object

Raises:

  • (StandardError)


42
43
44
45
46
47
48
49
50
# File 'lib/bruw/git.rb', line 42

def self.prune_remotes(remotes)
  raise StandardError, "An error occured, please check git remotes" if remotes.nil? || !remotes.respond_to?(:each)
  raise StandardError, "No git remotes to prune" if remotes.empty?

  remotes.each do |remote|
    puts "Removing #{remote.colorize(:green)}..."
    `git remote remove #{remote}`
  end
end

.remotes(pattern = "") ⇒ Object



22
23
24
25
26
# File 'lib/bruw/git.rb', line 22

def self.remotes(pattern = "")
  return `git remote` if pattern.empty?

  `git remote | grep ^#{pattern}`.split
end

.repos(owner, pattern = "", without = "") ⇒ Object

Returns a list of repos that match specified patterns pattern



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bruw/git.rb', line 6

def self.repos(owner, pattern = "", without = "")
  cmd = "gh repo list #{owner}"
  cmd = "#{cmd} | grep #{pattern}" unless pattern.nil? || pattern.empty?
  cmd = "#{cmd} | grep -v #{without}" unless without.nil? || without.empty?

  repos = `#{cmd}`.gsub!(/\s+/, " ").split

  repos = repos.select do |repo|
    repo.start_with?(owner)
  end

  repos.map do |repo|
    repo.split("/")[1]
  end
end