Class: Garlic::Repo

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

Overview

This class represents a local git repo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Repo

Returns a new instance of Repo.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/garlic/repo.rb', line 6

def initialize(options = {})
  if @url = options[:url]
    @url = File.expand_path(@url) unless options[:url] =~ /^\w+(:|@)/
  end
  
  @path = options[:path] or raise ArgumentError, "Repo requires a :path"
  @path = File.expand_path(@path)
  
  @local = options[:local]
  @local = File.expand_path(@local) if @local
  
  @name = options[:name] || File.basename(@path)
end

Instance Attribute Details

#localObject (readonly)

Returns the value of attribute local.



4
5
6
# File 'lib/garlic/repo.rb', line 4

def local
  @local
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/garlic/repo.rb', line 4

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/garlic/repo.rb', line 4

def path
  @path
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/garlic/repo.rb', line 4

def url
  @url
end

Class Method Details

.head_sha(path) ⇒ Object



32
33
34
# File 'lib/garlic/repo.rb', line 32

def head_sha(path)
  `cd #{path}; git log HEAD -1 --pretty=format:\"%H\"`
end

.path?(path) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/garlic/repo.rb', line 21

def path?(path)
  File.directory?(File.join(path, '.git'))
end

.tree_ish(options) ⇒ Object



25
26
27
28
29
30
# File 'lib/garlic/repo.rb', line 25

def tree_ish(options)
  [:tree_ish, :tree, :branch, :tag, :commit, :sha].each do |key|
    return options[key] if options[key]
  end
  nil
end

Instance Method Details

#checkObject



70
71
72
73
74
75
76
# File 'lib/garlic/repo.rb', line 70

def check
  if !Repo.path?(path)
    raise "#{name} is missing from #{path}, or is not a git repo"
  elsif url && !same_url?(origin_url)
    raise "#{name}'s url has changed from #{url} to #{origin_url}"
  end
end

#checkout(tree_ish) ⇒ Object



83
84
85
# File 'lib/garlic/repo.rb', line 83

def checkout(tree_ish)
  cd(path) { sh "git checkout -q #{tree_ish}", :verbose => false }
end

#clone_to(clone_path) ⇒ Object



98
99
100
101
102
103
# File 'lib/garlic/repo.rb', line 98

def clone_to(clone_path)
  mkdir_p File.dirname(clone_path)
  cd (File.dirname(clone_path)) do
    sh "git clone #{path} #{clone_path}"
  end
end

#export_to(export_path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/garlic/repo.rb', line 87

def export_to(export_path)
  rm_rf export_path; mkdir_p export_path
  cd(path) do
    sh "git archive --format=tar HEAD > #{File.join(export_path, "#{name}.tar")}", :verbose => false
  end
  cd(export_path) do
    `tar -x -f #{name}.tar`
    rm "#{name}.tar"
  end
end

#head_shaObject



117
118
119
# File 'lib/garlic/repo.rb', line 117

def head_sha
  Repo.head_sha(path)
end

#installObject



37
38
39
40
41
42
43
44
# File 'lib/garlic/repo.rb', line 37

def install
  if File.exists?(path)
    puts "\nSkipping #{name}, as it exists"
  else
    puts "\nInstalling #{name}"
    sh "git clone #{url}#{" --reference #{local}" if local} #{path}"
  end
end

#origin_urlObject



105
106
107
108
109
110
111
# File 'lib/garlic/repo.rb', line 105

def origin_url
  unless @origin_url
    match = `cd #{path}; git remote show -n origin`.match(/URL: (.*)\n/)
    @origin_url = (match && match[1])
  end
  @origin_url
end

#resetObject



78
79
80
81
# File 'lib/garlic/repo.rb', line 78

def reset
  cd(path) { sh "git reset --hard master" }
  checkout('master')
end

#same_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/garlic/repo.rb', line 113

def same_url?(url)
  self.url.sub(/\/?(\.git)?$/, '') == url.sub(/\/?(\.git)?$/, '')
end

#updateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/garlic/repo.rb', line 46

def update
  puts "\nUpdating #{name}..."
  if Repo.path?(path)
    if url
      begin
        checkout 'master'
        cd(path) do
          sh "git fetch origin", :verbose => false
          sh "git pull", :verbose => false
        end
      rescue Exception => e
        puts "\n\nIt seems there was a problem.\nTry running rake garlic:reset_repos\n\n"
        raise e
      end
    else
      puts "No url for #{name} given, so not updating"
    end
  elsif File.exists?(path)
    raise "\nRepo #{name} (#{path}) is not a git repo.\nRemove it and run rake garlic:install_repos\n\n"
  else
    raise "\nRepo #{name} missing from #{path}.\nPlease run rake garlic:install_repos\n\n"
  end
end