Class: FlexmlsGems::GitHelper

Inherits:
Object
  • Object
show all
Includes:
Grit
Defined in:
lib/flexmls_gems/git_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, version) ⇒ GitHelper

Returns a new instance of GitHelper.



10
11
12
13
# File 'lib/flexmls_gems/git_helper.rb', line 10

def initialize(dir, version)
  @pwd = dir
  @version = version
end

Instance Attribute Details

#pwdObject (readonly)

Returns the value of attribute pwd.



8
9
10
# File 'lib/flexmls_gems/git_helper.rb', line 8

def pwd
  @pwd
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/flexmls_gems/git_helper.rb', line 8

def version
  @version
end

Instance Method Details

#check_remoteObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/flexmls_gems/git_helper.rb', line 20

def check_remote
  if cmd_remote_list.include? "origin"
    matches = cmd_ls_remote
    if matches != ""
      raise GitError, "Git tag #{tag} already exists on the origin remote repository"
    end
  else
    raise GitError, "Git origin is missing, have you setup a remote repository?"
  end
end

#check_statusObject

Raises:



15
16
17
18
# File 'lib/flexmls_gems/git_helper.rb', line 15

def check_status
  changes = cmd_changes
  raise GitError, "There are #{changes} outstanding changes in the repository.  Run 'git status' to address before tagging" if changes > 0
end

#check_tagsObject



35
36
37
38
39
# File 'lib/flexmls_gems/git_helper.rb', line 35

def check_tags
  if cmd_tags.include? tag
    raise GitError, "The tag #{tag} already exists locally"
  end
end

#cmd_add(file) ⇒ Object



61
62
63
# File 'lib/flexmls_gems/git_helper.rb', line 61

def cmd_add(file)
  git.add(file)
end

#cmd_add_tagObject



77
78
79
# File 'lib/flexmls_gems/git_helper.rb', line 77

def cmd_add_tag
  `git tag -a #{tag} -m "Adding new tag '#{tag}'for release"`
end

#cmd_changesObject



67
68
69
70
# File 'lib/flexmls_gems/git_helper.rb', line 67

def cmd_changes
  s = git.status
  changes = reject_ignores(s.changed).count + reject_ignores(s.added).count + s.deleted.count
end

#cmd_commit(message) ⇒ Object



64
65
66
# File 'lib/flexmls_gems/git_helper.rb', line 64

def cmd_commit(message)
  git.commit_index(message)
end

#cmd_ls_remoteObject



74
75
76
# File 'lib/flexmls_gems/git_helper.rb', line 74

def cmd_ls_remote
  `git ls-remote --tags origin #{tag}`
end

#cmd_push_masterObject

Raises:



80
81
82
83
# File 'lib/flexmls_gems/git_helper.rb', line 80

def cmd_push_master
  raise GitError, "HEAD is not on the master branch" unless git.head.name == "master" 
  `git push origin master`
end

#cmd_push_tagsObject



84
85
86
# File 'lib/flexmls_gems/git_helper.rb', line 84

def cmd_push_tags
  `git push origin --tags`
end

#cmd_remote_listObject



87
88
89
# File 'lib/flexmls_gems/git_helper.rb', line 87

def cmd_remote_list
  git.remote_list
end

#cmd_tagsObject



71
72
73
# File 'lib/flexmls_gems/git_helper.rb', line 71

def cmd_tags
  git.tags
end

#commit_versionObject

Raises:



50
51
52
53
54
# File 'lib/flexmls_gems/git_helper.rb', line 50

def commit_version
  cmd_add "VERSION"
  raise GitError, "Some uncommitted files are in the folder." unless cmd_changes == 1 
  cmd_commit "Bumped version #{tag}" 
end

#gitObject



56
57
58
59
# File 'lib/flexmls_gems/git_helper.rb', line 56

def git
  @git = Repo.new(pwd) if @git.nil?
  return @git
end

#releaseObject



41
42
43
44
45
46
47
48
# File 'lib/flexmls_gems/git_helper.rb', line 41

def release
  check_status 
  check_remote 
  check_tags
  cmd_add_tag
  cmd_push_master      
  cmd_push_tags
end

#tagObject



31
32
33
# File 'lib/flexmls_gems/git_helper.rb', line 31

def tag
  "v#{version.chomp}"
end