Class: Hillary::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/hillary/repo.rb,
lib/hillary/repo/version.rb

Defined Under Namespace

Classes: Version

Constant Summary collapse

DirtyProjectError =
Class.new(StandardError)
MissingHeadError =
Class.new(StandardError)
PRODUCTION_TAG_FORMAT =
"%Y_%m_%d_%H_%M_%S"
RC_TAG_FORMAT =
"RC#{PRODUCTION_TAG_FORMAT}"
PRODUCTION_TAG_REGEX =
/^\d{4}(_\d{2}){5}$/
RC_TAG_REGEX =
/^RC\d{4}(_\d{2}){5}$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, logger = Logger.new($stdout)) ⇒ Repo

Returns a new instance of Repo.



27
28
29
30
31
32
33
34
# File 'lib/hillary/repo.rb', line 27

def initialize(dir, logger = Logger.new($stdout))
  Grit.debug = true if ENV['DEBUG']

  @name = Pathname.new(File.expand_path(dir)).basename
  @repo = Grit::Repo.new(dir)
  @logger = logger
  logger.formatter = proc{|_, _, _, msg| msg + "\n"}
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/hillary/repo.rb', line 15

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/hillary/repo.rb', line 15

def name
  @name
end

Class Method Details

.production_tag_nameObject



22
23
24
# File 'lib/hillary/repo.rb', line 22

def production_tag_name
  DateTime.now.strftime(PRODUCTION_TAG_FORMAT)
end

.rc_tag_nameObject



18
19
20
# File 'lib/hillary/repo.rb', line 18

def rc_tag_name
  DateTime.now.strftime(RC_TAG_FORMAT)
end

Instance Method Details

#checkout(ref) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hillary/repo.rb', line 40

def checkout(ref)
  raise DirtyProjectError, "Cannot checkout '#{ref}', #{name} is dirty." if dirty?

  logger.info "Checking out #{ref} and updating from origin"
  if !repo.objects([ref]).empty?
    git.checkout({}, ref)
  else
    raise MissingHeadError, "Cannot checkout '#{ref}', because it doesn't exist."
  end

  git.pull({}, 'origin', ref)
end

#commit_and_push(files, message) ⇒ Object



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

def commit_and_push(files, message)
  logger.info "Adding files:", *files.map{|f| "  #{f}"}
  git.add({}, files)
  logger.info "Committing with '#{message}'"
  git.commit({m: message})
  logger.info "Pushing changes to origin/head"
  push('head')
end

#create_production_tag(tag_name = self.class.production_tag_name) ⇒ Object



58
59
60
61
# File 'lib/hillary/repo.rb', line 58

def create_production_tag(tag_name = self.class.production_tag_name)
  rc_tag = last_rc_tag
  tag_and_push(tag_name, rc_tag)
end

#create_rc_tag(branch_name = 'master', tag_name = self.class.rc_tag_name) ⇒ Object



53
54
55
56
# File 'lib/hillary/repo.rb', line 53

def create_rc_tag(branch_name = 'master', tag_name = self.class.rc_tag_name)
  branch = repo.remotes.find{|branch| branch.name == "origin/#{branch_name}"}
  tag_and_push(tag_name, branch)
end

#delete_last_production_tagObject



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

def delete_last_production_tag
  delete_tag_and_push(last_production_tag.name)
end

#delete_last_rc_tagObject



74
75
76
# File 'lib/hillary/repo.rb', line 74

def delete_last_rc_tag
  delete_tag_and_push(last_rc_tag.name)
end

#delete_tag_and_push(tag_name) ⇒ Object



82
83
84
85
86
87
# File 'lib/hillary/repo.rb', line 82

def delete_tag_and_push(tag_name)
  git.fetch
  logger.info "Deleting #{tag_name} locally"
  git.tag({d: tag_name})
  push(":refs/tags/#{tag_name}")
end

#headObject



36
37
38
# File 'lib/hillary/repo.rb', line 36

def head
  repo.head
end

#last_production_tagObject



102
103
104
# File 'lib/hillary/repo.rb', line 102

def last_production_tag
  repo.tags.select{|tag| tag.name =~ PRODUCTION_TAG_REGEX}.sort_by(&:name).last
end

#last_rc_tagObject



98
99
100
# File 'lib/hillary/repo.rb', line 98

def last_rc_tag
  repo.tags.select{|tag| tag.name =~ RC_TAG_REGEX}.sort_by(&:name).last
end

#tag_and_push(tag_name, ref, message = tag_name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/hillary/repo.rb', line 63

def tag_and_push(tag_name, ref, message = tag_name)
  ref_name = ref.respond_to?(:name) ? ref.name : ref
  commit = ref.respond_to?(:commit) ? ref.commit : ref
  sha = commit.respond_to?(:sha) ? commit.sha : ref

  git.fetch
  logger.info "Tagging #{ref_name} (#{sha}) as #{tag_name}"
  git.tag({a: tag_name, m: message}, sha)
  push(tag_name)
end