Class: Plus2GitTagger::Tags

Inherits:
Struct
  • Object
show all
Defined in:
lib/plus2_git_tagger/tags.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#git_helperObject

Returns the value of attribute git_helper

Returns:

  • (Object)

    the current value of git_helper



2
3
4
# File 'lib/plus2_git_tagger/tags.rb', line 2

def git_helper
  @git_helper
end

#tag_prefixObject

Returns the value of attribute tag_prefix

Returns:

  • (Object)

    the current value of tag_prefix



2
3
4
# File 'lib/plus2_git_tagger/tags.rb', line 2

def tag_prefix
  @tag_prefix
end

Instance Method Details



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/plus2_git_tagger/tags.rb', line 25

def banner(*msgs)
  width = msgs.map {|m| m.size}.max

  banner = "*" * (width + 6)

  puts
  puts banner

  msgs.each do |m|
    print "*  #{m}"
    print " " * (width - m.size + 2)
    puts  "*"
  end

  puts banner
  puts
end

#base_tagObject

Returns the base tag for today’s date.

Example:

di/rel-2011-07-07


88
89
90
# File 'lib/plus2_git_tagger/tags.rb', line 88

def base_tag
  Time.now.strftime("#{ tag_prefix }-%Y-%m-%d")
end

#extensionsObject

Returns a sorted list of possible extensions.



112
113
114
# File 'lib/plus2_git_tagger/tags.rb', line 112

def extensions
  ('1'..'9').to_a + ('a'..'z').to_a
end

#git(cmd) ⇒ Object

delegate to git helper



132
133
134
# File 'lib/plus2_git_tagger/tags.rb', line 132

def git(cmd)
  git_helper.git( cmd )
end

#git_s(cmd) ⇒ Object



137
138
139
# File 'lib/plus2_git_tagger/tags.rb', line 137

def git_s(cmd)
  git_helper.git_s( cmd )
end

#listObject

Lists all tags matching the patten



68
69
70
71
72
# File 'lib/plus2_git_tagger/tags.rb', line 68

def list
  tags_with_pattern.sort.each do |tag|
    puts tag
  end
end

#make!Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/plus2_git_tagger/tags.rb', line 5

def make!
  if last_tag = todays_tags.sort.last
    tag = next_tag(last_tag)
  else
    tag = base_tag
  end

  luser = git_s("config user.name").chomp

  git "tag -m'auto-tagged by #{luser}' #{tag}"


  banner "Behold a tag! #{ tag }"

  git "push origin --tags"

  banner "Your tag has been pushed automatically.", "Please don't forget to push your current branch too!"
end

#month_listObject

List all tags made this month



76
77
78
79
80
# File 'lib/plus2_git_tagger/tags.rb', line 76

def month_list
  this_months_tags.sort.each do |tag|
    puts tag
  end
end

#month_patternObject

Returns a pattern we can use to determine all tags for the month



100
101
102
# File 'lib/plus2_git_tagger/tags.rb', line 100

def month_pattern
  Time.now.strftime("#{ tag_prefix }-%Y-%m")
end

#next_tag(tag) ⇒ Object

Given a tag, using all the rules, find the next tag.



45
46
47
48
49
50
# File 'lib/plus2_git_tagger/tags.rb', line 45

def next_tag(tag)
  last_index = extensions.index( tag_ext(tag) ) || -1
  next_ext = extensions[last_index+1]

  "#{base_tag}-#{next_ext}"
end

#tag_ext(tag) ⇒ Object

Extracts the extension from a tag

Returns either the extension, or a blank string.



120
121
122
123
124
125
126
127
# File 'lib/plus2_git_tagger/tags.rb', line 120

def tag_ext(tag)
  ext = tag.sub(tag_regex, '')
  if ext[0] == ?-
    ext[1].chr
  else
    ''
  end
end

#tag_regexObject

Returns a regex which matches tags in the required format, and starting with ‘tag_prefix`.



56
57
58
# File 'lib/plus2_git_tagger/tags.rb', line 56

def tag_regex
  @tag_regex ||= %r{^#{ Regexp.quote tag_prefix }-\d{4}-\d{2}-\d{2}}
end

#tags_with_patternObject

Returns tags matching ‘tag_regex` for this repo.



62
63
64
# File 'lib/plus2_git_tagger/tags.rb', line 62

def tags_with_pattern
  git_helper.tags.grep(tag_regex)
end

#this_months_tagsObject

Returns all tags for the month



106
107
108
# File 'lib/plus2_git_tagger/tags.rb', line 106

def this_months_tags
  tags_with_pattern.grep(%r{^#{ month_pattern }})
end

#todays_tagsObject

Returns a list matching the ‘tag_prefix`, on today’s date.



94
95
96
# File 'lib/plus2_git_tagger/tags.rb', line 94

def todays_tags
  tags_with_pattern.grep(%r{^#{ base_tag }})
end