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.

Constant Summary collapse

COMMAND =
"git"
STATUS =

git arguments

"status"
FETCH_WITH_TAGS =
"fetch --tags"
VERSION =
"--version"
TAGS_ASC =
"tag -l --sort=v:refname"
TAGS_DESC =
"tag -l --sort=-v:refname"
LATEST_TAG_COMMIT =
"#{COMMAND} rev-list --tags --max-count=1".freeze
LATEST_TAG_NAME =
format("describe --tags $(%<commit>s)", commit: LATEST_TAG_COMMIT)
LATEST_TAG_BRANCHES =
format("branch --contains $(%<commit>s)", commit: 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\""
PUSH_HEAD =
"push origin head"
PULL =
"-C #{Dir.home}/%s pull".freeze
REMOTE_URL =
"-C #{Dir.home}/%s config --get remote.origin.url".freeze
ORIGIN_URL =
"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.



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

def initialize(git)
  @git = git
end

Instance Attribute Details

#gitObject (readonly)

Returns the value of attribute git.



28
29
30
# File 'lib/groundskeeper/git.rb', line 28

def git
  @git
end

Class Method Details

.buildObject



30
31
32
# File 'lib/groundskeeper/git.rb', line 30

def self.build
  new Executable.new(COMMAND)
end

Instance Method Details

#add_allObject



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

def add_all
  git.execute(ADD_ALL)
end

#add_updateObject



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

def add_update
  git.execute(ADD_UPDATE)
end

#branches_containing_latest_tagObject



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

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

#commit(message) ⇒ Object



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

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

#create_and_checkout_branch(name) ⇒ Object



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

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

#fetchObject



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

def fetch
  git.execute(FETCH_WITH_TAGS)
end

#fetch_and_list_tagsObject



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

def fetch_and_list_tags
  git.execute(FETCH_WITH_TAGS)
  git.execute(TAGS_DESC).split("\n")
end

#latest_tag_name_across_branchesObject



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

def latest_tag_name_across_branches
  git.execute(LATEST_TAG_NAME).chop
end

#list_tagsObject



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

def list_tags
  git.execute(TAGS_ASC)
end

#origin_urlObject



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

def origin_url
  git.execute(ORIGIN_URL).strip
end

#pull(name) ⇒ Object



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

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

#pushObject



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

def push
  git.execute(PUSH_HEAD)
end

#raw_changes_since_latest_tagObject



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

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



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

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

#statusObject



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

def status
  git.execute(STATUS)
end

#versionObject



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

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

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