Module: GitProjectRemote::ClassMethods

Defined in:
lib/helpers/git_project_remote.rb

Overview

Check/ add new remotes

Instance Method Summary collapse

Instance Method Details

#add_new_remote(g, name, remote) ⇒ Object



49
50
51
52
53
54
# File 'lib/helpers/git_project_remote.rb', line 49

def add_new_remote(g, name, remote)
  g.add_remote(name, remote)
  g.add_remote('all', remote) unless remote_exists?(g, name)
  `git remote set-url --add all #{remote}`
  puts "Added remote #{name}".green
end

#add_remote(g, v) ⇒ Object

Add remote



57
58
59
60
61
62
63
64
65
# File 'lib/helpers/git_project_remote.rb', line 57

def add_remote(g, v)
  g.add_remote('origin', v['origin']) unless remote_exists?(g, 'origin')
  g.add_remote('all', v['origin']) unless remote_exists?(g, 'all')
  v.each do |name, remote|
    next if  %w(root_dir all group).include?(name) ||
      g.remotes.map(&:name).include?(name)
    add_new_remote(g, name, remote)
  end
end

#clone(url, name, path) ⇒ Object

Clone unless dir exists



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

def clone(url, name, path)
  path = real_root_dir(path)
  r = "#{path}/#{name}"
  g = Git.open(r)
  if g
    puts 'Already cloned '.yellow + "#{url}".blue
  else
    Git.clone(url, name, path: path) || Git.init(r)
    puts "Cloning #{url} as #{name} into #{path}".green
  end
  g
end

Check if symbolic link or directory exists

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/helpers/git_project_remote.rb', line 10

def dir_or_symlink_exist?(path)
  symbolic_link_exists = (File.symlink?(path) && File.readlink(path))
  unless File.directory?(path) || symbolic_link_exists
    puts "The dir #{path} does not exist"
  end
end

#fetch(g) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/helpers/git_project_remote.rb', line 37

def fetch(g)
  g.remotes.each do |r|
    next if %w(root_dir all group).include?(r.name)
    r.fetch
    puts "Fetching updates from #{r.name}: #{r.url}".green
  end
end

#real_root_dir(path) ⇒ Object

Get real root_dir



18
19
20
21
# File 'lib/helpers/git_project_remote.rb', line 18

def real_root_dir(path)
  return File.readlink(path) if File.symlink?(path)
  path
end

#remote_exists?(g, name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/helpers/git_project_remote.rb', line 45

def remote_exists?(g, name)
  g.remotes.map(&:name).include?(name)
end