1
0
Fork 0

applying diff works

This commit is contained in:
Christian Kühnel 2019-12-09 12:33:17 +01:00
parent 9f4d4b674f
commit 1d4551b510

View file

@ -61,70 +61,54 @@ class ApplyPatch:
try: try:
print('Checking out master...') print('Checking out master...')
dependencies = self._get_dependencies()
self.repo.git.checkout('master') self.repo.git.checkout('master')
revision_id, dependencies = self._get_dependencies()
print('Analyzing {}'.format(diff_to_str(revision_id)))
if len(dependencies) > 0: if len(dependencies) > 0:
print('This diff depends on: {}'.format(diff_list_to_str(dependencies))) print('This diff depends on: {}'.format(diff_list_to_str(dependencies)))
missing, landed = self._get_missing_landed_dependencies(dependencies) missing, landed = self._get_missing_landed_dependencies(dependencies)
print(' These have already landed: {}'.format(diff_list_to_str(landed))) print(' These have already landed: {}'.format(diff_list_to_str(landed)))
print(' These are missing on master: {}'.format(diff_list_to_str(missing))) print(' These are missing on master: {}'.format(diff_list_to_str(missing)))
# TODO for revision in missing:
for revision in landed:
self._apply_revision(revision)
# self._get_parent_hash() self._apply_diff(self.diff_id)
# self._git_checkout()
# self._apply_patch()
finally: finally:
self._write_error_message() self._write_error_message()
def _get_parent_hash(self) -> str:
diff = self._get_diff(self.diff_id)
# Keep a copy of the Phabricator answer for later usage in a json file
try:
with open(self.diff_json_path,'w') as json_file:
json.dump(diff.response, json_file, sort_keys=True, indent=4)
print('Wrote diff details to "{}".'.format(self.diff_json_path))
except Exception:
print('WARNING: could not write build/diff.json log file')
self.git_hash = diff['sourceControlBaseRevision']
def _get_diff(self, diff_id: str): def _get_diff(self, diff_id: str):
return self.phab.differential.getdiff(diff_id=diff_id) return self.phab.differential.getdiff(diff_id=diff_id)
def _get_revision(self, revision_id: int): def _get_revision(self, revision_id: int):
return self.phab.differential.query(ids=[revision_id])[0] return self.phab.differential.query(ids=[revision_id])[0]
def _get_revisions(self, phids): def _get_revisions(self, phids: str = None, ids: int = None):
if phids is not None:
return self.phab.differential.query(phids=phids) return self.phab.differential.query(phids=phids)
def _get_dependencies(self) -> List[int]: def _get_dependencies(self) -> List[int]:
revision_id = int(self._get_diff(self.diff_id).revisionID) revision_id = int(self._get_diff(self.diff_id).revisionID)
print('Analyzing {}'.format(diff_to_str(revision_id)))
revision = self._get_revision(revision_id) revision = self._get_revision(revision_id)
dependency_ids = revision['auxiliary']['phabricator:depends-on'] dependency_ids = revision['auxiliary']['phabricator:depends-on']
revisions = self._get_revisions(dependency_ids) revisions = self._get_revisions(dependency_ids)
diff_ids = [int(rev['id']) for rev in revisions] diff_ids = [int(rev['id']) for rev in revisions]
return diff_ids # It seems Phabricator lists the dependencies in the opposite order,
# so we reverse the order before returning the list, so that they
# can be applied in this order
diff_ids.reverse()
return revision_id, diff_ids
def _git_checkout(self): def _apply_diff(self, diff_id: int):
try: print('Applying diff {}...'.format(diff_id))
print('Checking out git hash {}'.format(self.git_hash)) diff = self.phab.differential.getrawdiff(diffID=diff_id).response
subprocess.check_call('git reset --hard {}'.format(self.git_hash), proc = subprocess.run('git apply', input=diff, shell=True, text=True,
stdout=sys.stdout, stderr=sys.stderr, shell=True) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except subprocess.CalledProcessError: if proc.returncode != 0:
print('WARNING: checkout of hash failed, using master branch instead.') raise Exception('Applying patch failed:\n{}'.format(proc.stdout + proc.stderr))
self.msg += [
'Could not check out parent git hash "{}". It was not found in '
'the repository. Did you configure the "Parent Revision" in '
'Phabricator properly? Trying to apply the patch to the '
'master branch instead...'.format(self.git_hash)]
subprocess.check_call('git checkout master', stdout=sys.stdout,
stderr=sys.stderr, shell=True)
print('git checkout completed.')
def _apply_patch(self): def _apply_revision(self, revision_id: int):
# TODO revision = self._get_revisions([revision_id])
pass print(revision.response)
def _write_error_message(self): def _write_error_message(self):
"""Write the log message to a file.""" """Write the log message to a file."""