poky/meta/lib/patchtest/repo.py
Gyorgy Sarvari a690cfb9c0 patchtest: apply patch in repo root
Patchtest tries to apply the received patch on the repository, without
specifying the directory, which means that the CWD is used. In case the
patch modifies a content in a different folder (e.g. the script is running
in ./meta, but the patch modifies ./meta-selftest), the patch will be skipped,
but git still returns 0, instead of complaining.

To avoid such false positives, specify the working directory for applying the
patch - the top of the repodir.

(From OE-Core rev: 6c7bb23b05ab613d5efe8e1378d7e1b1cc8cfc45)

Signed-off-by: Gyorgy Sarvari <skandigraun@gmail.com>
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2025-09-18 11:16:35 +01:00

86 lines
2.8 KiB
Python

# 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 git
import os
import mbox
class PatchTestRepo(object):
# prefixes used for temporal branches/stashes
prefix = 'patchtest'
def __init__(self, patch, repodir, commit=None, branch=None):
self.repodir = repodir
self.repo = git.Repo.init(repodir)
self.patch = mbox.PatchSeries(patch)
self.current_branch = self.repo.active_branch.name
# 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.repo.branches:
valid_patch_branch = self.patch.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. Use the '-B' flag so that we just
# check out the existing one if it's there
self.repo.git.execute(['git', 'checkout', '-B', self._workingbranch, self._commit])
self._patchmerged = False
# Check if patch can be merged using git-am
self._patchcanbemerged = True
try:
# Make sure to get the absolute path of the file
self.repo.git.execute(['git', '-C', self.repodir, 'apply', '--check', os.path.abspath(self.patch.path)], with_exceptions=True)
except git.exc.GitCommandError as ce:
self._patchcanbemerged = False
def ismerged(self):
return self._patchmerged
def canbemerged(self):
return self._patchcanbemerged
def _get_commitid(self, commit):
if not commit:
return None
try:
return self.repo.rev_parse(commit).hexsha
except Exception as e:
print(f"Couldn't find commit {commit} in repo")
return None
def merge(self):
if self._patchcanbemerged:
self.repo.git.execute(['git', '-C', self.repodir, 'am', '--keep-cr', os.path.abspath(self.patch.path)])
self._patchmerged = True
def clean(self):
self.repo.git.execute(['git', 'checkout', self.current_branch])
self.repo.git.execute(['git', 'branch', '-D', self._workingbranch])
self._patchmerged = False