-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.py
94 lines (74 loc) · 2.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import subprocess
from config import PROJECTS_PATH, GIT_REPOS_PATH, GIT_PATH
post_receive_content = 'git --work-tree=${PROJECT_DIR} checkout --force'
def create_repo(repo_name):
# print(repo_name)
project_path = os.path.join(PROJECTS_PATH, repo_name)
git_repo_path = os.path.join(GIT_REPOS_PATH, repo_name + '.git')
cmds = [
{
'cwd': GIT_REPOS_PATH,
'cmd': [GIT_PATH, 'init', '--bare', repo_name + '.git']
},
# git config --global core.autocrlf true
{
'cwd': git_repo_path,
'cmd': [GIT_PATH, 'config', 'core.autocrlf', 'true']
},
# git config --global core.safecrlf false
{
'cwd': git_repo_path,
'cmd': [GIT_PATH, 'config', 'core.safecrlf', 'false']
},
# git config receive.denyCurrentBranch ignore
{
'cwd': git_repo_path,
'cmd': [GIT_PATH, 'config', 'receive.denyCurrentBranch', 'ignore']
},
{
'cwd': git_repo_path,
'cmd': [GIT_PATH, '--work-tree=%s' % project_path, 'add', '.']
},
{
'cwd': git_repo_path,
'cmd': [GIT_PATH, '--work-tree=%s' % project_path, 'commit', '-m', '\'init project\'']
}
]
for item in cmds:
p = subprocess.Popen(' '.join(item.get('cmd')), cwd=item.get('cwd'), shell=True,
stderr=subprocess.PIPE)
for line in p.stderr:
yield line.decode()
post_receive_path = os.path.join(git_repo_path, 'hooks/post-receive')
with open(post_receive_path, 'w') as f:
f.write(post_receive_content)
os.chmod(post_receive_path, 0o755)
yield 'ok!'
def git_command(repo_name, version, *args):
dir_name = os.path.join(GIT_REPOS_PATH, repo_name)
project_dir = os.path.join(PROJECTS_PATH, repo_name[:-4])
if not os.path.isdir(dir_name):
for i in create_repo(repo_name[:-4]):
pass
cmd = [GIT_PATH, *args]
env = os.environ.copy()
env['PROJECT_DIR'] = project_dir
p = subprocess.Popen(' '.join(cmd), cwd=dir_name, env=env, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p.wait()
out = p.stdout.read()
return out
def git_command_with_input(repo_name, version, input_data, *args):
dir_name = os.path.join(GIT_REPOS_PATH, repo_name)
if not os.path.isdir(dir_name):
create_repo(repo_name[:-4])
env = os.environ.copy()
cmd = [GIT_PATH, *args]
env['PROJECT_DIR'] = os.path.join(PROJECTS_PATH, repo_name[:-4])
p = subprocess.Popen(' '.join(cmd), cwd=dir_name, env=env, shell=True, stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
p.stdin.write(input_data)
p.stdin.flush()
for line in p.stdout:
yield line