Skip to content

chore(script): add safety check for identical files during unification and move files#2579

Closed
DevGeniusCode wants to merge 2 commits intoTheSuperHackers:mainfrom
DevGeniusCode:safety-script
Closed

chore(script): add safety check for identical files during unification and move files#2579
DevGeniusCode wants to merge 2 commits intoTheSuperHackers:mainfrom
DevGeniusCode:safety-script

Conversation

@DevGeniusCode
Copy link
Copy Markdown

Updated the file unification script to include a safety mechanism that prevents accidental data loss during the move/merge process.

The script now skips files and prints an error message if a mismatch is detected, allowing for manual inspection.

@DevGeniusCode DevGeniusCode added the ThisProject The issue was introduced by this project, or this task is specific to this project label Apr 11, 2026
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 11, 2026

Greptile Summary

This PR adds a safety check to unify_file in the file-unification script that (1) verifies both the source and opposite-game file exist before proceeding, and (2) aborts with an error message if the two copies aren't byte-for-byte identical. A follow-up commit (e5b601d8) was added to address a previously raised concern about filecmp.cmp crashing when the opposite-game file is absent — that concern is now resolved by the os.path.exists guard added at line 86.

Confidence Score: 5/5

Safe to merge — the previously reported FileNotFoundError is fixed and the safety check logic is correct.

The existence guard at line 86 fully addresses the prior P1 concern about filecmp.cmp crashing on a missing file. The only remaining finding is a P2 suggestion to improve error-message diagnostics; it does not affect correctness or safety.

No files require special attention.

Important Files Changed

Filename Overview
scripts/cpp/unify_move_files.py Adds existence + identity safety checks to unify_file; the previously reported FileNotFoundError from filecmp.cmp is fixed. One minor diagnostic gap: the error message on line 87 doesn't specify which of the two paths is missing.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["unify_file(fromGame, fromFile, toGame, toFile)"] --> B{fromFile exists in\nfromGame dir?}
    B -- No --> C["print ERROR: file missing\nreturn (skip)"]
    B -- Yes --> D{fromFile exists in\noppositeGame dir?}
    D -- No --> C
    D -- Yes --> E{filecmp.cmp\nbyte-for-byte match?}
    E -- No --> F["print ERROR: files differ\nreturn (skip)"]
    E -- Yes --> G[Modify CMakeLists:\ncomment out in both games]
    G --> H[Uncomment in Core CMakeLists]
    H --> I[delete_file from oppositeGame]
    I --> J[move_file from fromGame to Core]
Loading
Prompt To Fix All With AI
This is a comment left during a code review.
Path: scripts/cpp/unify_move_files.py
Line: 86-88

Comment:
**Ambiguous "missing file" error message**

The message says "One or both files do not exist!" but doesn't reveal *which* path is absent, making it harder to diagnose the root cause (e.g. was it already moved, does it have a different relative path in the other game's directory, etc.). Printing the missing path directly would make the output immediately actionable.

```suggestion
    if not os.path.exists(fullFromPath):
        print(f"ERROR: Source file does not exist! Skipping: {fullFromPath}")
        return
    if not os.path.exists(fullOppositePath):
        print(f"ERROR: Opposite-game file does not exist! Skipping: {fullOppositePath}")
        return
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (2): Last reviewed commit: "Fix FileNotFoundError" | Re-trigger Greptile

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

import os
import shutil
import filecmp
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sort by alphabet


if not os.path.exists(fullFromPath) or not os.path.exists(fullOppositePath):
print(f"ERROR: One or both files do not exist! Skipping: {fromFile}")
return
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Previously it errored when a file was not found, stopping the entire script. This was intentional, to make very clear that there is an invocation error that needs addressing before continuing.

I suspect this test was added to so that it is possible to rerun the script without resetting the invocation setup?

This should then also be done in the other unify functions.

Better make this a separate helper function similar to move_file

print(f"ERROR: One or both files do not exist! Skipping: {fromFile}")
return

if not filecmp.cmp(fullFromPath, fullOppositePath, shallow=False):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Better make this a separate helper function similar to move_file

print(f"ERROR: One or both files do not exist! Skipping: {fromFile}")
return

if not filecmp.cmp(fullFromPath, fullOppositePath, shallow=False):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does this really work correctly? Typically the first lines are different between Generals and Zero Hour source files, because of the Copyright header (intentional).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think the difference lies in how we merge the files. I first compare the files using the diff window and adjust any mismatches (including the first few lines) so that the Vanilla and MD files are completely identical. Only then do I transfer them

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Merging the Copyright header in the first line is incorrect (!)

Generals must keep its Generals header, otherwise git will have difficulties resolving the rename and will pick Generals or Zero Hour at random, but we want git to always assume that Generals file is deleted and Zero Hour is moved to Core. Reason: This makes merging conflicts easier, because all our WIP changes touch Zero Hour first.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Got it, that makes perfect sense. I was trying to manually resolve all differences (comments, whitespace, etc.) to make the files identical before moving them, but I see how this interferes with Git's tracking. Moving forward, I’ll make sure the Core files remain exact copies of the Zero Hour version to preserve the history. Thanks!

@DevGeniusCode DevGeniusCode deleted the safety-script branch April 11, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ThisProject The issue was introduced by this project, or this task is specific to this project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants