fix case when no files were commited in remote push trigger

The issue appears together with --intent-to-add flag for adding new
files. Original testing has been performed by having already added new
files, thus it passed all checks.

This commit also adds `commit_author` option which will allow to
overwrite the author.
This commit is contained in:
2022-11-14 00:59:43 +02:00
parent b2ed383de0
commit cdd66ee780
6 changed files with 68 additions and 29 deletions

View File

@ -161,12 +161,12 @@ class Sources(LazyLogging):
str: patch as plain text
"""
instance = Sources()
instance.add(sources_dir, *pattern)
instance.add(sources_dir, *pattern, intent_to_add=True)
diff = instance.diff(sources_dir)
return f"{diff}\n" # otherwise, patch will be broken
@staticmethod
def push(sources_dir: Path, remote: RemoteSource, *pattern: str) -> None:
def push(sources_dir: Path, remote: RemoteSource, *pattern: str, commit_author: Optional[str] = None) -> None:
"""
commit selected changes and push files to the remote repository
@ -174,19 +174,21 @@ class Sources(LazyLogging):
sources_dir(Path): local path to git repository
remote(RemoteSource): remote target, branch and url
*pattern(str): glob patterns
commit_author(Optional[str]): commit author in form of git config (i.e. ``user <user@host>``)
"""
instance = Sources()
instance.add(sources_dir, *pattern)
instance.commit(sources_dir)
instance.commit(sources_dir, author=commit_author)
Sources._check_output("git", "push", remote.git_url, remote.branch, cwd=sources_dir, logger=instance.logger)
def add(self, sources_dir: Path, *pattern: str) -> None:
def add(self, sources_dir: Path, *pattern: str, intent_to_add: bool = False) -> None:
"""
track found files via git
Args:
sources_dir(Path): local path to git repository
*pattern(str): glob patterns
intent_to_add(bool): record only the fact that it will be added later, acts as --intent-to-add git flag
"""
# glob directory to find files which match the specified patterns
found_files: List[Path] = []
@ -196,23 +198,26 @@ class Sources(LazyLogging):
return # no additional files found
self.logger.info("found matching files %s", found_files)
# add them to index
Sources._check_output("git", "add", "--intent-to-add",
*[str(fn.relative_to(sources_dir)) for fn in found_files],
args = ["--intent-to-add"] if intent_to_add else []
Sources._check_output("git", "add", *args, *[str(fn.relative_to(sources_dir)) for fn in found_files],
cwd=sources_dir, logger=self.logger)
def commit(self, sources_dir: Path, commit_message: Optional[str] = None) -> None:
def commit(self, sources_dir: Path, message: Optional[str] = None, author: Optional[str] = None) -> None:
"""
commit changes
Args:
sources_dir(Path): local path to git repository
commit_message(Optional[str]): optional commit message if any. If none set, message will be generated
according to the current timestamp
message(Optional[str]): optional commit message if any. If none set, message will be generated according to
the current timestamp
author(Optional[str]): optional commit author if any
"""
if commit_message is None:
commit_message = f"Autogenerated commit at {datetime.datetime.utcnow()}"
Sources._check_output("git", "commit", "--allow-empty", "--message", commit_message,
cwd=sources_dir, logger=self.logger)
if message is None:
message = f"Autogenerated commit at {datetime.datetime.utcnow()}"
args = ["--allow-empty", "--message", message]
if author is not None:
args.extend(["--author", author])
Sources._check_output("git", "commit", *args, cwd=sources_dir, logger=self.logger)
def diff(self, sources_dir: Path) -> str:
"""

View File

@ -38,6 +38,7 @@ class RemotePush(LazyLogging):
sync PKGBUILDs to remote repository after actions
Attributes:
commit_author(Optional[str]): optional commit author in form of git config (i.e. ``user <user@host>``)
remote_source(RemoteSource): repository remote source (remote pull url and branch)
"""
@ -49,6 +50,7 @@ class RemotePush(LazyLogging):
configuration(Configuration): configuration instance
remote_push_trigger.py
"""
self.commit_author = configuration.get(section, "commit_author", fallback=None)
self.remote_source = RemoteSource(
git_url=configuration.get(section, "push_url"),
web_url="",
@ -73,10 +75,8 @@ class RemotePush(LazyLogging):
# firstly, we need to remove old data to make sure that removed files are not tracked anymore...
package_target_dir = target_dir / package.base
shutil.rmtree(package_target_dir, ignore_errors=True)
# ...secondly, we copy whole tree...
with TemporaryDirectory(ignore_cleanup_errors=True) as dir_name, (clone_dir := Path(dir_name)):
Sources.fetch(clone_dir, package.remote)
shutil.copytree(clone_dir, package_target_dir)
# ...secondly, we clone whole tree...
Sources.fetch(package_target_dir, package.remote)
# ...and last, but not least, we remove the dot-git directory...
shutil.rmtree(package_target_dir / ".git", ignore_errors=True)
# ...and finally return path to the copied directory
@ -107,7 +107,8 @@ class RemotePush(LazyLogging):
try:
with TemporaryDirectory(ignore_cleanup_errors=True) as dir_name, (clone_dir := Path(dir_name)):
Sources.fetch(clone_dir, self.remote_source)
Sources.push(clone_dir, self.remote_source, *RemotePush.packages_update(result, clone_dir))
Sources.push(clone_dir, self.remote_source, *RemotePush.packages_update(result, clone_dir),
commit_author=self.commit_author)
except Exception:
self.logger.exception("git push failed")
raise GitRemoteError()