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

Detect if this is a merge or rebase in rebase_migration command #260

Closed
wants to merge 4 commits into from
Closed

Detect if this is a merge or rebase in rebase_migration command #260

wants to merge 4 commits into from

Conversation

dmitrysleptsov
Copy link
Contributor

@dmitrysleptsov dmitrysleptsov commented May 23, 2023

Resolve #80. Adds logic to check if there is a merge request in progress. If so, handle it by swapping the migration files.

@adamchainz
Copy link
Owner

I don't think this is the right approach. It would be better to fix django-linear-migrations to handle both cases. It's possible to detect if the current directory is a Git repository and whether there's a rebase or merge in progress with a few Git commands.

@dmitrysleptsov
Copy link
Contributor Author

dmitrysleptsov commented May 26, 2023

Agree, it's not the fix for the command, it's just the less risky change that allows us to modify the command. I thought it would be simpler to add changes that don't change the logic. But I can make the existing command work with both rebase and merge. I think we can check the existence of the MERGE_HEAD file in the .git directory (in the same way as the git client does it.

The question for me is how far the library should go to find out the path to the .git directory?

  1. We could ask the user to do it via some setting. Because I'm not sure that setting.BASE_DIR is reliable enough to use it.
  2. We could execute something like git rev-parse --git-dir directly in the script to find out the path to the .git directory. But in this case we rely on the existence of git command.

And in this case, there is also a question about what we should do if there is no Git repository? I think command should just fail with an error

@adamchainz
Copy link
Owner

Option 2: use git rev-parse --git-dir. Ensure it does not crash if git is not available. Don't error if there is no Git repository, carry on with the existing presume-rebase behaviour.

@dmitrysleptsov dmitrysleptsov changed the title Move find_migration_names to Command class Detect if this is a merge or rebase in rebase_migration command May 26, 2023
@dmitrysleptsov dmitrysleptsov marked this pull request as draft May 26, 2023 16:03
@dmitrysleptsov dmitrysleptsov marked this pull request as ready for review May 26, 2023 19:40
text=True,
)
except (FileNotFoundError, subprocess.SubprocessError):
# Either `git` is not available or there is no git repository
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If some error should be shown to user, this function can be moved under Command class

@dmitrysleptsov
Copy link
Contributor Author

It's ready for review when you have time @adamchainz . I tested it, works as expected for both rebase and merge

@adamchainz
Copy link
Owner

Great, thanks 👍. Can you update the docs to mention the automatic detection , and add a changelog note? I’ll properly review a bit later, if you can do those tasks it would help.

Copy link
Owner

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

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

a few changes I'm going to make

2.8.0 (2023-05-28)
------------------

* Improve `rebase_migration` command to handle both rebasing and merging of the base branch
Copy link
Owner

Choose a reason for hiding this comment

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

reStructuredText uses two backticks for code

Comment on lines +5 to +6
2.8.0 (2023-05-28)
------------------
Copy link
Owner

Choose a reason for hiding this comment

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

headers are inserted at release time... I should add a comment to this file to clarify that.

Comment on lines +500 to +507
def setUp(self) -> None:
subprocess_run_patch = mock.patch(
"django_linear_migrations.management.commands.rebase_migration"
".subprocess.run"
)
self.mock_subprocess_run = subprocess_run_patch.start()

self.addCleanup(subprocess_run_patch.stop)
Copy link
Owner

Choose a reason for hiding this comment

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

I'm gonna rewrite these tests to not use mocking

Comment on lines +194 to +208
def is_merge_in_progress() -> bool:
try:
result = subprocess.run(
["git", "rev-parse", "--git-dir"],
capture_output=True,
check=True,
text=True,
)
except (FileNotFoundError, subprocess.SubprocessError):
# Either `git` is not available or there is no git repository, fall back to
# default behaviour
return False

git_dir = result.stdout.strip()
return Path(git_dir).joinpath("MERGE_HEAD").exists()
Copy link
Owner

Choose a reason for hiding this comment

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

we can do better with git rev-parse --verify MERGE_HEAD, skipping getting the git dir and reading the internal files

@adamchainz
Copy link
Owner

I think your pull request did not allow me to push changes, so I have had to create a new one in #261.

For future ref, please ensure the box is checked: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork

@adamchainz adamchainz closed this May 29, 2023
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

Successfully merging this pull request may close these issues.

Rebases the wrong migration when merging branches
3 participants