1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
""" |
3
|
|
|
Git fixtures helpers |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
import os |
7
|
|
|
import subprocess |
|
|
|
|
8
|
|
|
|
9
|
|
|
from git import Repo |
|
|
|
|
10
|
|
|
|
11
|
|
|
def commit(repo, message, author=None, date=None): |
|
|
|
|
12
|
|
|
cwd = os.getcwd() |
13
|
|
|
|
14
|
|
|
os.chdir(repo.working_dir) |
15
|
|
|
|
16
|
|
|
# need to do a manual commit to allow empty message and set committer date |
17
|
|
|
cmd = 'git commit --allow-empty -m "{}"'.format(message) |
18
|
|
|
if author: |
19
|
|
|
cmd = cmd+' --author="{}"'.format(author) |
20
|
|
|
|
21
|
|
|
if date: |
22
|
|
|
cmd = 'GIT_COMMITTER_DATE="{}" '.format(date)+cmd+' --date="{}"'.format(date) |
23
|
|
|
|
24
|
|
|
os.system(cmd) |
25
|
|
|
os.chdir(cwd) |
26
|
|
|
|
27
|
|
|
return repo.commit('HEAD') |
28
|
|
|
|
29
|
|
|
def tag(repo, version, author=None, date=None): |
|
|
|
|
30
|
|
|
commit_obj = commit(repo=repo, message="release: {}".format(version), author=author, date=date) |
31
|
|
|
repo.create_tag(path=version, message=version) |
32
|
|
|
|
33
|
|
|
return commit_obj |
34
|
|
|
|
35
|
|
|
def branch(repo, branch, start='HEAD'): |
|
|
|
|
36
|
|
|
return repo.create_head(branch, commit=start) |
37
|
|
|
|
38
|
|
|
def clone(remote_repo, path): |
|
|
|
|
39
|
|
|
return remote_repo.clone(path) |
40
|
|
|
|
41
|
|
|
def init(email='[email protected]', username='User Test', repo_dir=None): |
|
|
|
|
42
|
|
|
cwd = os.getcwd() |
43
|
|
|
if not repo_dir or not os.path.exists(repo_dir): |
44
|
|
|
repo_dir = cwd |
45
|
|
|
|
46
|
|
|
os.chdir(repo_dir) |
47
|
|
|
|
48
|
|
|
repo = Repo.init(repo_dir) |
49
|
|
|
|
50
|
|
|
# git config |
51
|
|
|
with repo.config_writer() as cfg: |
52
|
|
|
cfg.add_section('user') |
53
|
|
|
cfg.set('user', 'email', email) |
54
|
|
|
cfg.set('user', 'name', username) |
55
|
|
|
|
56
|
|
|
cfg.release() |
57
|
|
|
|
58
|
|
|
os.chdir(cwd) |
59
|
|
|
|
60
|
|
|
return repo |
61
|
|
|
|
62
|
|
|
def default_init(version='0.1.2', email='[email protected]', username='User Test', |
|
|
|
|
63
|
|
|
author='User Test <[email protected]>', date='2016-11-20T12:41:30+0000', |
|
|
|
|
64
|
|
|
tag_date='2016-11-20T12:42:30+0000', repo_dir=None): |
|
|
|
|
65
|
|
|
repo = init(email=email, username=username, repo_dir=repo_dir) |
66
|
|
|
commit(repo=repo, message='initial commit', author=author, date=date) |
67
|
|
|
tag(repo=repo, version=version, author=author, date=tag_date) |
68
|
|
|
|
69
|
|
|
return repo |
70
|
|
|
|