Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing test: No Base image rename in Git commit after renaming Gold image #122

Open
josecelano opened this issue Aug 23, 2023 · 1 comment

Comments

@josecelano
Copy link
Member

josecelano commented Aug 23, 2023

The test
given_a_dvc_diff_object_with_a_renamed_gold_image_it_should_commit_the_renamed_base_image_to_git is failing:

When you rename a Gold image, the final Git commit should include a rename for the Base image, but it includes a deletion and insertion.

The test passes with the following change, meaning the Base image commit does not rename the file. It deletes the old one and inserts the new one.

Git Diff (32 = Gold Image; 54 = Base Image):

     expected_commit_stats_files = {
-        "data/000001/52/.gitignore": {"insertions": 0, "deletions": 1, "lines": 1},
-        "data/000002/52/.gitignore": {"insertions": 3, "deletions": 0, "lines": 3},
-        "data/{000001/52/000001-52.600.2.tif.dvc => 000002/52/000002-52.600.2.tif.dvc}": {
-            "insertions": 1,
-            "deletions": 1,
-            "lines": 2,
-        },
+        "data/000001/52/.gitignore": {"deletions": 1, "insertions": 0, "lines": 1},
+        "data/000001/52/000001-52.600.2.tif.dvc": {"deletions": 4, "insertions": 0, 'lines': 4},
+        "data/000002/52/.gitignore": {"deletions": 0, "insertions": 3, "lines": 3},
+        "data/000002/52/000002-52.600.2.tif.dvc": {"deletions": 0, "insertions": 4, "lines": 4}
     }

More info: https://dvc.org/doc/command-reference/diff#example-renamed-files

Originally posted by @josecelano in #118 (comment)

josecelano added a commit to josecelano/nautilus-librarian that referenced this issue Aug 23, 2023
@josecelano
Copy link
Member Author

It seems that it's the expected behaviour:

We commit with a rename case:

    return repo.commit(
        {
            "renamed": {
                "old": dvc_services.get_files_to_commit(old_base_img_relative_path),
                "new": dvc_services.get_files_to_commit(new_base_img_relative_path),
            }
        },
        commit_message=(
            f"feat: renamed base image: {os.path.basename(old_base_img_relative_path)}"
            f" -> {os.path.basename(new_base_img_relative_path)}"
        ),
        env={
            "GIT_COMMITTER_NAME": git_user.name,
            "GIT_COMMITTER_EMAIL": git_user.email,
        },
    )

But internally, the rename is committed as an add and remove git commands. But I do not understand why the test is failing now, and it was not failing before. If I'm not wrong, we agree @da2ce7 @yeraydavidrodriguez to implement it this way. I mean a "rename" in DVC is mapped to a add/remove git commands, but I do not see the issue/comment where we were discussing that topic. I will continue researching.

class GitRepo:
    """A wrapper for GitPython Repo"""

    def __init__(self, git_repo_dir, git_user, gnupghome="~/.gnupg"):
        self.repo = Repo(git_repo_dir)
        self.git_user = git_user
        self.gnupghome = gnupghome

    def commit(self, filepaths, commit_message: str, env: dict[str, str] = {}):
        """
        It creates a commit.
        Filepath is an object with four optional file lists:
        {
            "added": [],
            "deleted": [],
            "modified": [],
            "renamed": [],
        }
        """
        if "added" in filepaths:
            self.repo.index.add(filepaths["added"])

        if "deleted" in filepaths:
            self.repo.index.remove(filepaths["deleted"])

        if "renamed" in filepaths:
            self.repo.index.add(filepaths["renamed"]["new"])
            self.repo.index.remove(filepaths["renamed"]["old"])

        # Write index. Needed for commit with signature:
        # https://github.com/gitpython-developers/GitPython/issues/580#issuecomment-282474086
        self.repo.index.write()

        # Unsigned commit
        if self.git_user.signingkey is None:
            with self.repo.git.custom_environment(**env):
                return self.repo.git.commit("-m", f"{commit_message}")

        # Signed commit
        with self.repo.git.custom_environment(GNUPGHOME=self.gnupghome, **env):
            return self.repo.git.commit(
                "-S",
                f"--gpg-sign={self.git_user.signingkey}",
                "-m",
                f"{commit_message}",
            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant