Class: Groundskeeper::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/groundskeeper/git.rb

Overview

Wraps git executable and adds a few convenience methods on top.

Defined Under Namespace

Classes: Executable

Constant Summary collapse

COMMAND =
"git"
STATUS =

git arguments

"status"
FETCH_WITH_TAGS =
"fetch --tags"
VERSION =
"--version"
TAGS_ASC =
"tag -l --sort=v:refname"
LATEST_TAG_COMMIT =
"#{COMMAND} rev-list --tags --max-count=1"
LATEST_TAG_NAME =
format("describe --tags $(%s)", LATEST_TAG_COMMIT)
LATEST_TAG_BRANCHES =
format("branch --contains $(%s)", LATEST_TAG_COMMIT)
LATEST_CHANGES =
"log %s..HEAD --oneline"
CREATE_AND_CHECKOUT_BRANCH =
"checkout -b %s"
ADD_UPDATE =
"add -u"
ADD_ALL =
"add ."
COMMIT =
"commit -m \"%s\""
ADD_TAG =
"tag %s"
PUSH_HEAD_WITH_TAGS =
"push origin head --tags"
PULL =
"-C #{Dir.home}/%s pull"
REMOTE_URL =
"-C #{Dir.home}/%s config --get remote.origin.url"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git) ⇒ Git

Returns a new instance of Git.



38
39
40
# File 'lib/groundskeeper/git.rb', line 38

def initialize(git)
  @git = git
end

Instance Attribute Details

#git ⇒ Object (readonly)

Returns the value of attribute git.



25
26
27
# File 'lib/groundskeeper/git.rb', line 25

def git
  @git
end

Class Method Details

.build ⇒ Object



34
35
36
# File 'lib/groundskeeper/git.rb', line 34

def self.build
  new Executable.new
end

Instance Method Details

#add_all ⇒ Object



82
83
84
# File 'lib/groundskeeper/git.rb', line 82

def add_all
  git.execute(ADD_ALL)
end

#add_tag(name) ⇒ Object



90
91
92
# File 'lib/groundskeeper/git.rb', line 90

def add_tag(name)
  git.execute(format(ADD_TAG, name))
end

#add_update ⇒ Object



78
79
80
# File 'lib/groundskeeper/git.rb', line 78

def add_update
  git.execute(ADD_UPDATE)
end

#branches_containing_latest_tag ⇒ Object



64
65
66
# File 'lib/groundskeeper/git.rb', line 64

def branches_containing_latest_tag
  git.execute(LATEST_TAG_BRANCHES).split("\n").map(&:strip)
end

#commit(message) ⇒ Object



86
87
88
# File 'lib/groundskeeper/git.rb', line 86

def commit(message)
  git.execute(format(COMMIT, message))
end

#create_and_checkout_branch(name) ⇒ Object



74
75
76
# File 'lib/groundskeeper/git.rb', line 74

def create_and_checkout_branch(name)
  git.execute(format(CREATE_AND_CHECKOUT_BRANCH, name))
end

#fetch ⇒ Object



46
47
48
# File 'lib/groundskeeper/git.rb', line 46

def fetch
  git.execute(FETCH_WITH_TAGS)
end

#latest_tag_name_across_branches ⇒ Object



60
61
62
# File 'lib/groundskeeper/git.rb', line 60

def latest_tag_name_across_branches
  git.execute(LATEST_TAG_NAME).chop
end

#list_tags ⇒ Object



56
57
58
# File 'lib/groundskeeper/git.rb', line 56

def list_tags
  git.execute(TAGS_ASC)
end

#pull(name) ⇒ Object



98
99
100
# File 'lib/groundskeeper/git.rb', line 98

def pull(name)
  git.execute(format(PULL, name))
end

#push_with_tags ⇒ Object



94
95
96
# File 'lib/groundskeeper/git.rb', line 94

def push_with_tags
  git.execute(PUSH_HEAD_WITH_TAGS)
end

#raw_changes_since_latest_tag ⇒ Object



68
69
70
71
72
# File 'lib/groundskeeper/git.rb', line 68

def raw_changes_since_latest_tag
  latest_changes = format(LATEST_CHANGES, latest_tag_name_across_branches)

  git.execute(latest_changes).split("\n")
end

#remote_url(name) ⇒ Object



102
103
104
# File 'lib/groundskeeper/git.rb', line 102

def remote_url(name)
  git.execute(format(REMOTE_URL, name))
end

#status ⇒ Object



42
43
44
# File 'lib/groundskeeper/git.rb', line 42

def status
  git.execute(STATUS)
end

#version ⇒ Object



50
51
52
53
54
# File 'lib/groundskeeper/git.rb', line 50

def version
  semantic_version = /\d+.\d+.\d+/

  git.execute(VERSION).match(semantic_version)[0]
end