Skip to content

Skip writing files if they haven't' changed#2491

Merged
CLHatch merged 1 commit into
mainfrom
Updates
May 7, 2026
Merged

Skip writing files if they haven't' changed#2491
CLHatch merged 1 commit into
mainfrom
Updates

Conversation

@CLHatch
Copy link
Copy Markdown
Contributor

@CLHatch CLHatch commented May 7, 2026

Pull request

Purpose
Describe the problem or feature in addition to a link to the issues.

Approach
How does this change address the problem?

Open Questions and Pre-Merge TODOs
Check all boxes as they are completed

  • Use github checklists. When solved, check the box and explain the answer.

Learning
Describe the research stage
Links to blog posts, patterns, libraries or addons used to solve this problem

Requirements
Check all boxes as they are completed

Summary by Sourcery

Avoid rewriting docker-compose and env files when their contents have not changed.

Bug Fixes:

  • Prevent unnecessary rewrites of docker-compose.yml by generating output in a temporary file and only copying when contents differ.
  • Prevent unnecessary rewrites of .env and app-specific .env files by comparing temporary updates to existing files before copying.

@CLHatch CLHatch requested a review from a team as a code owner May 7, 2026 08:32
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 7, 2026

Reviewer's Guide

This PR updates the YAML merge and env update scripts to write composed/updated output to temporary files first and only overwrite the target files when the content has actually changed, avoiding unnecessary writes while preserving existing logging and error handling behavior.

Sequence diagram for updated docker_compose_yml_merge_behavior

sequenceDiagram
    participant yml_merge_script
    participant docker_compose
    participant filesystem

    yml_merge_script->>filesystem: mktemp MKTEMP_COMPOSE_YML
    Note over yml_merge_script,filesystem: If mktemp fails, fatal and exit

    yml_merge_script->>docker_compose: run config with COMPOSE_FILE
    docker_compose-->>yml_merge_script: write output to MKTEMP_COMPOSE_YML (or error)

    alt docker_compose_config_failed
        yml_merge_script->>filesystem: rm -f MKTEMP_COMPOSE_YML
        yml_merge_script-->>yml_merge_script: error and return nonzero
    else docker_compose_config_succeeded
        yml_merge_script->>filesystem: check if docker-compose.yml exists
        yml_merge_script->>filesystem: cmp -s MKTEMP_COMPOSE_YML docker-compose.yml
        alt files_differ_or_target_missing
            yml_merge_script->>filesystem: cp -f MKTEMP_COMPOSE_YML docker-compose.yml
            Note over yml_merge_script,filesystem: If cp fails, fatal and exit
        else files_identical
            Note over yml_merge_script,filesystem: Skip overwriting docker-compose.yml
        end
        yml_merge_script->>filesystem: rm -f MKTEMP_COMPOSE_YML
        yml_merge_script-->>yml_merge_script: log merge complete and unset_needs_yml_merge
    end
Loading

Sequence diagram for updated_env_update_file_write_behavior

sequenceDiagram
    participant env_update_script
    participant filesystem

    Note over env_update_script: Main .env update
    env_update_script->>filesystem: mktemp MKTEMP_ENV_UPDATED
    env_update_script->>filesystem: write UPDATED_ENV_LINES to MKTEMP_ENV_UPDATED
    Note over env_update_script,filesystem: If write fails, fatal and exit

    env_update_script->>filesystem: check if COMPOSE_ENV exists
    env_update_script->>filesystem: cmp -s MKTEMP_ENV_UPDATED COMPOSE_ENV
    alt compose_env_missing_or_differs
        env_update_script->>filesystem: RunAndLog cp -f MKTEMP_ENV_UPDATED COMPOSE_ENV
        Note over env_update_script,filesystem: If cp fails, fatal and exit
    else compose_env_identical
        Note over env_update_script,filesystem: Skip overwriting COMPOSE_ENV
    end
    env_update_script->>filesystem: RunAndLog rm -f MKTEMP_ENV_UPDATED

    Note over env_update_script: Per_app_env_update
    env_update_script->>filesystem: mktemp MKTEMP_APP_ENV_UPDATED
    env_update_script->>filesystem: write UPDATED_APP_ENV_LINES to MKTEMP_APP_ENV_UPDATED
    Note over env_update_script,filesystem: If write fails, fatal and exit

    env_update_script->>filesystem: check if APP_ENV_FILE exists
    env_update_script->>filesystem: cmp -s MKTEMP_APP_ENV_UPDATED APP_ENV_FILE
    alt app_env_missing_or_differs
        env_update_script->>filesystem: RunAndLog cp -f MKTEMP_APP_ENV_UPDATED APP_ENV_FILE
        Note over env_update_script,filesystem: If cp fails, fatal and exit
    else app_env_identical
        Note over env_update_script,filesystem: Skip overwriting APP_ENV_FILE
    end
    env_update_script->>filesystem: RunAndLog rm -f MKTEMP_APP_ENV_UPDATED
Loading

File-Level Changes

Change Details Files
Generate docker-compose.yml via a temporary file and copy it over only when content differs from the existing file.
  • Create a temporary compose output file using mktemp before running docker compose config
  • Write docker compose config output to the temporary file instead of directly to docker-compose.yml
  • On failure, remove the temporary file and report the failing command using the temporary path
  • If docker-compose.yml is missing or differs from the temporary file, copy the temporary file to docker-compose.yml
  • Always remove the temporary compose file after processing
scripts/yml_merge.sh
Avoid rewriting .env and per-app .env files when there are no content changes by comparing temporary updates to existing files.
  • After writing updated .env content to a temporary file, compare it to the existing .env and only cp -f when the files differ or the target does not exist
  • Apply the same compare-before-copy logic for per-app .env.app. files
  • Suppress cmp errors by redirecting stderr to /dev/null during comparison
  • Retain existing RunAndLog wrappers and fatal error handling around cp operations
scripts/env_update.sh

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions Bot added the core Automatic label label May 7, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Several cmp and cp invocations use unquoted paths (e.g., ${COMPOSE_FOLDER}/docker-compose.yml, ${COMPOSE_ENV}, ${APP_ENV_FILE}); consider quoting these variables to avoid issues with spaces or special characters in paths.
  • The new temporary file handling (mktemp/rm -f) is duplicated and manually cleaned up in multiple places; consider wrapping this pattern in a small helper or using a trap to ensure consistent cleanup on all exit paths.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Several `cmp` and `cp` invocations use unquoted paths (e.g., `${COMPOSE_FOLDER}/docker-compose.yml`, `${COMPOSE_ENV}`, `${APP_ENV_FILE}`); consider quoting these variables to avoid issues with spaces or special characters in paths.
- The new temporary file handling (`mktemp`/`rm -f`) is duplicated and manually cleaned up in multiple places; consider wrapping this pattern in a small helper or using a `trap` to ensure consistent cleanup on all exit paths.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@CLHatch CLHatch merged commit 3aa7912 into main May 7, 2026
16 checks passed
@CLHatch CLHatch deleted the Updates branch May 7, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Automatic label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant