forked from Puppet-Finland/fabric
/
git.py
79 lines (69 loc) · 2.01 KB
/
git.py
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
from fabric.api import *
from fabric.contrib.files import exists
import re
@task
def install():
"""Install Git"""
import package, vars
vars = vars.Vars()
package.install(vars.os.git_package_name)
@task
def clone(dir, url, reponame=None, submodule=False):
"""Clone a Git repository as-is or as a submodule"""
if not reponame:
temp = re.split("/", url)[-1:][0]
name = re.split("\.", temp)[0]
else:
name = reponame
with cd(dir):
if not exists(name):
if submodule:
cmd = "git submodule add"
else:
cmd = "git clone"
sudo(cmd+" "+url+" "+name)
@task
def add_submodule(url, basedir, name=None):
"""Add a Git submodule to a directory"""
if not name:
# This can handle URL in this format:
#
# https://github.com/Puppet-Finland/postgresql.git
# https://github.com/Puppet-Finland/postgresql
#
stripped = re.sub("\.git$", "", url)
name = re.split("/", stripped)[-1:][0]
cmd = "git submodule add "+url
else:
cmd = "git submodule add "+url+" "+name
with cd(basedir):
if not exists(name):
sudo(cmd)
@task
def add_submodules(basedir, file="files/git-submodules"):
"""Add submodules from url / name pairs listed in a file"""
with open(file) as modulelist:
for line in modulelist:
columns=line.split(" ")
url=columns[0].rstrip("\n")
if len(columns) > 1:
name = columns[1].rstrip("\n")
else:
name = None
add_submodule(url, basedir, name)
@task
def init(dir):
"""Initialize a Git repository"""
with cd(dir):
if not exists("./.git"):
sudo("git init")
@task
def add_all(basedir):
"""Add all files to Git"""
with cd(basedir):
sudo("git add -A")
@task
def commit(basedir, message):
"""Commit changes to Git"""
with cd(basedir):
sudo("git commit -m \""+message+"\"")