Class: Sunshine::GitRepo

Inherits:
Repo
  • Object
show all
Defined in:
lib/sunshine/repos/git_repo.rb

Overview

Simple wrapper for git repos. Constructor supports :tree option to specify either a branch or tree-ish to checkout:

git = GitRepo.new "git://mygitrepo.git", :tree => "tags/release001"

Constant Summary collapse

LOG_FORMAT =
[
  ":revision: %H",
  ":committer: %cn",
  ":date: %cd",
  ":message: %s",
  ":refs: '%d'",
  ":tree: %t"
].join("%n")
NAME_MATCH =
/\/([^\/]+)(\/?)\.git/

Instance Attribute Summary collapse

Attributes inherited from Repo

#flags, #scm, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Repo

#checkout_to, detect, #get_repo_info, inherited, new_of_type, #scm_flags

Constructor Details

#initialize(url, options = {}) ⇒ GitRepo

Returns a new instance of GitRepo.



90
91
92
93
# File 'lib/sunshine/repos/git_repo.rb', line 90

def initialize url, options={}
  super
  @tree = options[:branch] || options[:tree] || "master"
end

Instance Attribute Details

#treeObject

Returns the value of attribute tree.



88
89
90
# File 'lib/sunshine/repos/git_repo.rb', line 88

def tree
  @tree
end

Class Method Details

.get_info(path = ".", shell = nil) ⇒ Object

Get the repo info from the path to a checked out git repo



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sunshine/repos/git_repo.rb', line 31

def self.get_info path=".", shell=nil
  shell ||= Sunshine.shell

  info = YAML.load git_log(path, shell)

  info[:date]   = Time.parse info[:date]
  info[:branch] = parse_branch info
  info[:url]    = git_origin path, shell

  info
rescue => e
  raise RepoError, e
end

.git_log(path, shell) ⇒ Object

Returns the git logs for a path, formatted as yaml.



49
50
51
52
# File 'lib/sunshine/repos/git_repo.rb', line 49

def self.git_log path, shell
  git_options = "-1 --no-color --format=\"#{LOG_FORMAT}\""
  shell.call "cd #{path} && git log #{git_options}"
end

.git_origin(path, shell, public_url = true) ⇒ Object

Returns the fetch origin of the current git repo. Returns the path to a public git repo by default:

GitRepo.git_origin "/some/path", Sunshine.shell
  #=> "git://myrepo/path/to/repo.git"
GitRepo.git_origin "/some/path", Sunshine.shell, false
  #=> "user@myrepo:path/to/repo.git"


63
64
65
66
67
68
69
70
71
72
# File 'lib/sunshine/repos/git_repo.rb', line 63

def self.git_origin path, shell, public_url=true
  get_origin_cmd = "cd #{path} && git remote -v | grep \\(fetch\\)"

  origin = shell.call get_origin_cmd
  origin = origin.split(/\t|\s/)[1]

  origin = make_public_url origin if public_url

  origin
end

.make_public_url(git_url) ⇒ Object

Returns the git url for a public checkout



78
79
80
81
82
83
84
85
# File 'lib/sunshine/repos/git_repo.rb', line 78

def self.make_public_url git_url
  url, protocol = git_url.split("://").reverse
  url, user     = url.split("@").reverse

  url.gsub!(":", "/") if !protocol

  "git://#{url}"
end

.valid?(path = ".") ⇒ Boolean

Check if this is an svn repo

Returns:

  • (Boolean)


23
24
25
# File 'lib/sunshine/repos/git_repo.rb', line 23

def self.valid? path="."
   File.exist? File.join(path, ".git")
end

Instance Method Details

#do_checkout(path, shell) ⇒ Object



96
97
98
99
100
# File 'lib/sunshine/repos/git_repo.rb', line 96

def do_checkout path, shell
  cmd = "cd #{path} && git clone #{@url} #{scm_flags} . && "+
    "git checkout #{@tree}"
  shell.call cmd
end

#nameObject



105
106
107
108
109
# File 'lib/sunshine/repos/git_repo.rb', line 105

def name
  @url.match(NAME_MATCH)[1]
rescue
  raise RepoError, "Git url must match #{NAME_MATCH.inspect}"
end