Class: GitProject

Inherits:
Object
  • Object
show all
Includes:
GitProjectConfig, GitProjectRemote
Defined in:
lib/helpers/git_project.rb

Overview

GitProject contains helpers for Git commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GitProjectRemote

included

Methods included from GitProjectConfig

included

Constructor Details

#initialize(config) ⇒ GitProject

Returns a new instance of GitProject.



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

def initialize(config)
  @project = Project.new(config)
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



10
11
12
# File 'lib/helpers/git_project.rb', line 10

def project
  @project
end

Instance Method Details

#add_remotesObject

Add missing remotes



54
55
56
57
58
59
60
61
# File 'lib/helpers/git_project.rb', line 54

def add_remotes
  @project.all.each do |k, v|
    working_dir = "#{v['root_dir']}/#{k}"
    g = Git.open(working_dir) || Git.init(working_dir)
    GitProject.add_remote(g, v)
    puts "Checking new remotes for #{k}".green
  end
end

#create_project_and_remotes(k, v) ⇒ Object

  1. Clone all repositories based on the origin key

  2. Add all other remotes unless it is origin



65
66
67
68
69
70
71
72
# File 'lib/helpers/git_project.rb', line 65

def create_project_and_remotes(k, v)
  puts "root_dir isn't defined for #{k}" unless v['root_dir']
  GitProject.dir_or_symlink_exist?(v['root_dir'])
  root_dir = GitProject.real_root_dir(v['root_dir'])
  GitProject.create_root_dir(root_dir)
  g =  GitProject.clone(v.values[0], k, root_dir)
  GitProject.add_remote(g, v) if g
end

#fetch_all(group = nil) ⇒ Object

By default, fetch from all



75
76
77
78
79
80
81
82
83
84
# File 'lib/helpers/git_project.rb', line 75

def fetch_all(group = nil)
  @project.all(group).each do |k, v|
    puts "Fetching changes for #{k}".green
    root_dir = GitProject.real_root_dir(v['root_dir'])
    GitProject.create_root_dir(root_dir)
    working_dir = "#{root_dir}/#{k}"
    g = Git.open(working_dir) || Git.init(working_dir)
    GitProject.fetch(g)
  end
end

#initObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/helpers/git_project.rb', line 19

def init
  @project.all.each do |k, v|
    begin
      create_project_and_remotes(k, v)
    rescue => e
      initialize_and_add_remotes(k, v)
      puts "Please check paths and permissions for #{k}. Error: #{e}".red
      puts "Failed to clone #{v.values[0]}. Initialized &
            fetched updates from remotes instead.".yellow
    end
  end
end

#initialize_and_add_remotes(k, v) ⇒ Object



32
33
34
35
36
37
# File 'lib/helpers/git_project.rb', line 32

def initialize_and_add_remotes(k, v)
  g = Git.init("#{v['root_dir']}/#{k}")
  return nil unless g
  GitProject.add_remote(g, v)
  GitProject.fetch(g)
end

#new_group(project, name) ⇒ Object

Change group



49
50
51
# File 'lib/helpers/git_project.rb', line 49

def new_group(project, name)
  @project.new_group(project, name)
end

#new_remote(project, name, url) ⇒ Object

Add a new remote



44
45
46
# File 'lib/helpers/git_project.rb', line 44

def new_remote(project, name, url)
  @project.new_remote(project, name, url)
end

#projectsObject



39
40
41
# File 'lib/helpers/git_project.rb', line 39

def projects
  @project.all
end