]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Allow combining --force and --rerun-build-scripts
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 28 Mar 2025 09:19:29 +0000 (10:19 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 28 Mar 2025 10:42:02 +0000 (11:42 +0100)
Until now we didn't allow this, but it turns out there's actually
a use case for this, build the image if it doesn't exist yet, reuse
the existing image otherwise.

mkosi/__init__.py
mkosi/config.py

index 49e208ac6356c6d5866f31d4ff40a5e452b9ea74..dc01dbd0650f14ac2bf7ef488afbee71c0222a62 100644 (file)
@@ -3943,7 +3943,10 @@ def build_image(context: Context) -> None:
         check_root_populated(context)
         run_build_scripts(context)
 
-        if context.config.output_format == OutputFormat.none or context.args.rerun_build_scripts:
+        if context.config.output_format == OutputFormat.none or (
+            context.args.rerun_build_scripts
+            and (context.config.output_dir_or_cwd() / context.config.output).exists()
+        ):
             return
 
         if wantrepo:
@@ -4668,7 +4671,7 @@ def validate_certificates_and_keys(config: Config) -> None:
 
 def needs_build(args: Args, config: Config, force: int = 1) -> bool:
     return (
-        args.force >= force
+        (args.force >= force and not args.rerun_build_scripts)
         or not (config.output_dir_or_cwd() / config.output_with_compression).exists()
         # When the output is a directory, its name is the same as the symlink we create that points to the
         # actual output when not building a directory. So if the full output path exists, we have to check
@@ -5121,7 +5124,7 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
         logging.info(f"Output path {output} exists already. (Use --force to rebuild.)")
         return
 
-    if args.rerun_build_scripts and not output.exists():
+    if args.rerun_build_scripts and not args.force and not output.exists():
         die(
             f"Image '{last.image}' must be built once before --rerun-build-scripts can be used",
             hint="Build the image once with 'mkosi build'",
@@ -5149,7 +5152,9 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
                     hint="Add the &I specifier to the cache key to avoid this issue",
                 )
 
-    if last.is_incremental() and (last.incremental == Incremental.strict or args.rerun_build_scripts):
+    if last.is_incremental() and (
+        last.incremental == Incremental.strict or (args.rerun_build_scripts and not args.force)
+    ):
         if args.force > 1:
             die(
                 "Cannot remove incremental caches when building with Incremental=strict",
@@ -5197,6 +5202,10 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
         or last.output_format == OutputFormat.none
         or not (last.output_dir_or_cwd() / last.output).exists()
     ):
+        history = (
+            last.output_format == OutputFormat.none or not (last.output_dir_or_cwd() / last.output).exists()
+        )
+
         for config in images:
             if any(
                 source.type != KeySourceType.file
@@ -5279,7 +5288,7 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
         if args.auto_bump:
             bump_image_version()
 
-        if last.history and not args.rerun_build_scripts:
+        if last.history and history:
             Path(".mkosi-private/history").mkdir(parents=True, exist_ok=True)
             Path(".mkosi-private/history/latest.json").write_text(
                 json.dumps(
index 7fce3ce5d1b36d5d54c837e83a84a07881677018..660ecf74b959d8acdc4c7edd4da6c6fe8cfe7956 100644 (file)
@@ -4824,8 +4824,7 @@ def have_history(args: Args) -> bool:
     return (
         args.directory is not None
         and args.verb.needs_build()
-        and (args.verb != Verb.build or args.rerun_build_scripts)
-        and not args.force
+        and ((args.verb != Verb.build and not args.force) or args.rerun_build_scripts)
         and Path(".mkosi-private/history/latest.json").exists()
     )
 
@@ -4860,9 +4859,6 @@ def parse_config(
     if args.cmdline and not args.verb.supports_cmdline():
         die(f"Arguments after verb are not supported for {args.verb}.")
 
-    if args.rerun_build_scripts and args.force:
-        die("--force cannot be used together with --rerun-build-scripts")
-
     # If --debug was passed, apply it as soon as possible.
     if ARG_DEBUG.get():
         logging.getLogger().setLevel(logging.DEBUG)