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
|
import os
import shlex
from ansible.errors import AnsibleActionFail
from ansible.module_utils.common.text.converters import to_text
def is_work_tree(action, repo_dir, task_vars):
cmd_result = action._execute_module(
module_name='ansible.builtin.command',
module_args=dict(
argv=['git', 'rev-parse', '--is-inside-work-tree'],
chdir=repo_dir,
),
task_vars=task_vars,
)
return not cmd_result.get('failed')
def get_status(action, repo_dir, task_vars):
cmd_result = action._execute_module(
module_name='ansible.builtin.command',
module_args=dict(
argv=['git', 'status', '--porcelain'],
chdir=repo_dir,
),
task_vars=task_vars,
)
if cmd_result.get('failed'):
raise AnsibleActionFail(
f"Couldn't get git status for {repo_dir}", result=cmd_result
)
return to_text(cmd_result['stdout'])
def get_dirty_file_list(action, repo_dir, task_vars):
status = get_status(action, repo_dir, task_vars)
status = status.splitlines()
status = [line[3:] for line in status]
return status
def get_remote_url(action, repo_dir, task_vars, remote='origin'):
cmd_result = action._execute_module(
module_name='ansible.builtin.command',
module_args=dict(
argv=['git', 'config', '--local', f'remote.{remote}.url'],
chdir=repo_dir,
),
task_vars=task_vars,
)
if cmd_result.get('failed'):
raise AnsibleActionFail(
f"Couldn't read the URL of remote '{remote}' in {repo_dir}",
result=cmd_result,
)
return cmd_result['stdout']
def update_remote(action, repo_dir, remote, task_vars, accept_hostkey=False):
ssh = os.environ.get('GIT_SSH', 'ssh')
ssh_opts = ''
if accept_hostkey:
ssh_opts += ' -o StrictHostKeyChecking=no'
ssh_command = shlex.quote(ssh) + ssh_opts
cmd_result = action._execute_module(
module_name='ansible.builtin.command',
module_args=dict(
argv=[
'env',
f'GIT_SSH_COMMAND={ssh_command}',
'git',
'remote',
'update',
remote,
],
chdir=repo_dir,
),
task_vars=task_vars,
)
if cmd_result.get('failed'):
raise AnsibleActionFail(
f"Couldn't update remote '{remote}' for {repo_dir}", result=cmd_result
)
def is_ancestor(action, repo_dir, remote, version, task_vars):
looks_like_hash = all(
((c >= '0' and c <= '9') or (c >= 'a' and c <= 'f') for c in version)
)
remote_versions = [f'{remote}/{version}']
if looks_like_hash:
remote_versions += [version]
for _version in remote_versions:
cmd_result = action._execute_module(
module_name='ansible.builtin.command',
module_args=dict(
argv=['git', 'merge-base', '--is-ancestor', 'HEAD', _version],
chdir=repo_dir,
),
task_vars=task_vars,
)
if cmd_result.get('failed'):
if cmd_result.get('stderr'):
continue
else:
return True
raise AnsibleActionFail(
f"Couldn't deduce how the HEAD of {repo_dir} relates to remote version {remote}/{version}"
)
|