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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# patchtestrepo: PatchTestRepo class used mainly to control a git repo from patchtest
#
# Copyright (C) 2016 Intel Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#
import os
import utils
import logging
from patch import PatchTestPatch
logger = logging.getLogger('patchtest')
info=logger.info
class PatchTestRepo(object):
# prefixes used for temporal branches/stashes
prefix = 'patchtest'
def __init__(self, patch, repodir, commit=None, branch=None):
self._repodir = repodir
self._patch = PatchTestPatch(patch)
self._current_branch = self._get_current_branch()
# targeted branch defined on the patch may be invalid, so make sure there
# is a corresponding remote branch
valid_patch_branch = None
if self._patch.branch in self.upstream_branches():
valid_patch_branch = self._patch.branch
# Target Branch
# Priority (top has highest priority):
# 1. branch given at cmd line
# 2. branch given at the patch
# 3. current branch
self._branch = branch or valid_patch_branch or self._current_branch
# Target Commit
# Priority (top has highest priority):
# 1. commit given at cmd line
# 2. branch given at cmd line
# 3. branch given at the patch
# 3. current HEAD
self._commit = self._get_commitid(commit) or \
self._get_commitid(branch) or \
self._get_commitid(valid_patch_branch) or \
self._get_commitid('HEAD')
self._workingbranch = "%s_%s" % (PatchTestRepo.prefix, os.getpid())
# create working branch
self._exec({'cmd': ['git', 'checkout', '-b', self._workingbranch, self._commit]})
self._patchmerged = False
# Check if patch can be merged using git-am
self._patchcanbemerged = True
try:
self._exec({'cmd': ['git', 'am', '--keep-cr'], 'input': self._patch.contents})
except utils.CmdException as ce:
self._exec({'cmd': ['git', 'am', '--abort']})
self._patchcanbemerged = False
finally:
# if patch was applied, remove it
if self._patchcanbemerged:
self._exec({'cmd':['git', 'reset', '--hard', self._commit]})
# for debugging purposes, print all repo parameters
logger.debug("Parameters")
logger.debug("\tRepository : %s" % self._repodir)
logger.debug("\tTarget Commit : %s" % self._commit)
logger.debug("\tTarget Branch : %s" % self._branch)
logger.debug("\tWorking branch : %s" % self._workingbranch)
logger.debug("\tPatch : %s" % self._patch)
@property
def patch(self):
return self._patch.path
@property
def branch(self):
return self._branch
@property
def commit(self):
return self._commit
@property
def ismerged(self):
return self._patchmerged
@property
def canbemerged(self):
return self._patchcanbemerged
def _exec(self, cmds):
_cmds = []
if isinstance(cmds, dict):
_cmds.append(cmds)
elif isinstance(cmds, list):
_cmds = cmds
else:
raise utils.CmdException({'cmd':str(cmds)})
results = []
cmdfailure = False
try:
results = utils.exec_cmds(_cmds, self._repodir)
except utils.CmdException as ce:
cmdfailure = True
raise ce
finally:
if cmdfailure:
for cmd in _cmds:
logger.debug("CMD: %s" % ' '.join(cmd['cmd']))
else:
for result in results:
cmd, rc, stdout, stderr = ' '.join(result['cmd']), result['returncode'], result['stdout'], result['stderr']
logger.debug("CMD: %s RCODE: %s STDOUT: %s STDERR: %s" % (cmd, rc, stdout, stderr))
return results
def _get_current_branch(self, commit='HEAD'):
cmd = {'cmd':['git', 'rev-parse', '--abbrev-ref', commit]}
cb = self._exec(cmd)[0]['stdout']
if cb == commit:
logger.warning('You may be detached so patchtest will checkout to master after execution')
cb = 'master'
return cb
def _get_commitid(self, commit):
if not commit:
return None
try:
cmd = {'cmd':['git', 'rev-parse', '--short', commit]}
return self._exec(cmd)[0]['stdout']
except utils.CmdException as ce:
# try getting the commit under any remotes
cmd = {'cmd':['git', 'remote']}
remotes = self._exec(cmd)[0]['stdout']
for remote in remotes.splitlines():
cmd = {'cmd':['git', 'rev-parse', '--short', '%s/%s' % (remote, commit)]}
try:
return self._exec(cmd)[0]['stdout']
except utils.CmdException:
pass
return None
def upstream_branches(self):
cmd = {'cmd':['git', 'branch', '--remotes']}
remote_branches = self._exec(cmd)[0]['stdout']
# just get the names, without the remote name
branches = set(branch.split('/')[-1] for branch in remote_branches.splitlines())
return branches
def merge(self):
if self._patchcanbemerged:
self._exec({'cmd': ['git', 'am', '--keep-cr'],
'input': self._patch.contents,
'updateenv': {'PTRESOURCE':self._patch.path}})
self._patchmerged = True
def clean(self):
self._exec({'cmd':['git', 'checkout', '%s' % self._current_branch]})
self._exec({'cmd':['git', 'branch', '-D', self._workingbranch]})
self._patchmerged = False
|