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