File: git_utils.rb

package info (click to toggle)
r10k 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,228 kB
  • sloc: ruby: 18,180; makefile: 10; sh: 1
file content (205 lines) | stat: -rwxr-xr-x 5,920 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Execute a git command on a host.
#
# ==== Attributes
#
# * +host+ - One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.
# * +git_sub_command+ - The git sub-command to execute including arguments. (The 'git' command is assumed.)
# * +git_repo_path+ - The path to the git repository on the target host.
# * +opts+ - Options to alter execution.
#
# ==== Returns
#
# +nil+
#
# ==== Examples
#
# git_on(master, 'add file.txt', '~/git_repo')
def git_on(host, git_sub_command, git_repo_path, opts = {})
  git_command = "git --git-dir=#{git_repo_path}/.git --work-tree=#{git_repo_path} #{git_sub_command}"

  on(host, git_command, opts)
end

# Add all uncommitted files located in a repository.
#
# ==== Attributes
#
# * +host+ - One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.
# * +git_repo_path+ - The path to the git repository on the target host.
#
# ==== Returns
#
# +nil+
#
# ==== Examples
#
# git_add_everything(master, '~/git_repo')
def git_add_everything(host, git_repo_path)
  git_on(host, "add #{git_repo_path}/*", git_repo_path)
end

# Push branch to origin remote.
#
# ==== Attributes
#
# * +host+ - One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.
# * +branch+ - The branch to push.
# * +git_repo_path+ - The path to the git repository on the target host.
#
# ==== Returns
#
# +nil+
#
# ==== Examples
#
# git_push(master, 'production', '~/git_repo')
def git_push(host, branch, git_repo_path)
  git_on(host, "push origin #{branch}", git_repo_path)
end

# Commit changes and push branch to origin remote.
#
# ==== Attributes
#
# * +host+ - One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.
# * +branch+ - The branch to push.
# * +message+ - A single-line commit message. (Don't quote message!)
# * +git_repo_path+ - The path to the git repository on the target host.
#
# ==== Returns
#
# +nil+
#
# ==== Examples
#
# git_commit_push(master, 'production', 'Awesome stuff!', '~/git_repo')
def git_commit_push(host, branch, message, git_repo_path)
  git_on(host, "commit -m \"#{message}\"", git_repo_path)
  git_push(host, branch, git_repo_path)
end

# Add all uncommitted files located in a repository, commit changes and push branch to origin remote.
#
# ==== Attributes
#
# * +host+ - One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.
# * +branch+ - The branch to push.
# * +message+ - A single-line commit message. (Don't quote message!)
# * +git_repo_path+ - The path to the git repository on the target host.
#
# ==== Returns
#
# +nil+
#
# ==== Examples
#
# git_add_commit_push(master, 'production', 'Awesome stuff!', '~/git_repo')
def git_add_commit_push(host, branch, message, git_repo_path)
  git_add_everything(host, git_repo_path)
  git_commit_push(host, branch, message, git_repo_path)
end

# Get the last commit SHA.
#
# ==== Attributes
#
# * +host+ - One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.
# * +git_repo_path+ - The path to the git repository on the target host.
#
# ==== Returns
#
# +string+ - The SHA of the last commit.
#
# ==== Examples
#
# last_commit = git_last_commit(master, '~/git_repo')
def git_last_commit(host, git_repo_path)
  sha_regex = /commit (\w{40})/

  return sha_regex.match(git_on(host, 'log', git_repo_path).stdout)[1]
end

# Hard reset the git repository to a specific commit.
#
# ==== Attributes
#
# * +host+ - One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.
# * +commit_sha+ - The reset HEAD to this commit SHA.
# * +git_repo_path+ - The path to the git repository on the target host.
#
# ==== Returns
#
# +nil+
#
# ==== Examples
#
# git_reset_hard(master, 'ff81c01c5', '~/git_repo')
def git_reset_hard(host, commit_sha, git_repo_path)
  git_on(host, "reset --hard #{commit_sha}", git_repo_path)
end

# Create a bare Git repository.
#
# ==== Attributes
#
# * +host+ - The Puppet host on which to create a bare git repo.
# * +git_repo_parent_path+ - The parent path that contains the desired Git repository.
# * +git_repo_name+ - The name of the repository.
#
# ==== Returns
#
# +string+ - The path to the newly created Git repository.
#
# ==== Examples
#
# git_init_bare_repo(master, '/git/repos', 'environments')
def git_init_bare_repo(host, git_repo_parent_path, git_repo_name)
  #Init
  git_repo_path = File.join(git_repo_parent_path, "#{git_repo_name}.git")

  #Initialize bare Git repository
  on(host, "mkdir -p #{git_repo_path}")
  on(host, "git init --bare #{git_repo_path}")

  return git_repo_path
end

# Clone a Git repository.
#
# ==== Attributes
#
# * +host+ - The Puppet host on which to create a bare git repo.
# * +git_clone_path+ - The destination path for the git clone.
# * +git_source+ - The origin from which to clone.
#
# ==== Returns
#
# +nil+
#
# ==== Examples
#
# git_clone_repo(master, '~/repos/r10k', '/git/repos/environments.git')
def git_clone_repo(host, git_clone_path, git_source)
  on(host, "git clone #{git_source} #{git_clone_path}")
end

# Create a bare Git repository and then clone the repository.
#
# ==== Attributes
#
# * +host+ - The Puppet host on which to create a bare git repo.
# * +git_repo_parent_path+ - The parent path that contains the desired Git repository.
# * +git_repo_name+ - The name of the repository.
# * +git_clone_path+ - The destination path for the git clone.
#
# ==== Returns
#
# +string+ - The path to the newly created Git repository.
#
# ==== Examples
#
# git_init_bare_repo_and_clone(master, '/git/repos', 'environments', '~/repos/r10k')
def git_init_bare_repo_and_clone(host, git_repo_parent_path, git_repo_name, git_clone_path)
  origin_git_repo_path = git_init_bare_repo(host, git_repo_parent_path, git_repo_name)
  git_clone_repo(host, git_clone_path, origin_git_repo_path)
end