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

Adjust git dir when checking for merge in worktree #662

Merged
merged 1 commit into from
Oct 15, 2021

Conversation

errsyn
Copy link
Contributor

@errsyn errsyn commented Oct 4, 2021

Fixes #638

Right now ongoing merges aren't detected in linked working trees, as it is assumed that .git is a directory. However, this is not the case for linked working trees. Working trees instead have a .git file, which references the path to the git directory like this:

$ cat .git
gitdir: /path/to/main-working-tree/.git/worktrees/<worktree-name>

So when we are dealing with a linked working tree, we just have to readjust the git directory to the one referenced in the .git file.

@errsyn errsyn force-pushed the worktree_adjust_gitdir branch 2 times, most recently from 04102c2 to c747973 Compare October 4, 2021 21:43
@@ -10,15 +10,25 @@
b'=======\n',
b'>>>>>>> ',
]
GITDIR_PREFIX_LEN = len('gitdir: ')
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to use git's mechanisms for discovering this directory -- using implementation detail like reading the file is going to be fragile

Comment on lines 12 to 13
@pytest.fixture(scope='function', params=['repo', 'worktree'])
def f1_is_a_conflict_file(tmpdir, request):
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer not to use parametrized fixtures -- they're really difficult to understand and maintain

what you can actually do here instead is write a separate test which consumes this fixture and the makes a worktree from that and call the original test -- it's going to be a lot easier to follow than this action-at-a-distance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call! Initially I thought the parameterized fixtures could be a nice approach to consolidate the variants for all dependent tests, but I can definitely see how they can negatively impact maintainability in the long run.

@errsyn errsyn force-pushed the worktree_adjust_gitdir branch 2 times, most recently from 214e217 to 751ab24 Compare October 5, 2021 20:49
Comment on lines 69 to 80
worktree = f1_is_a_conflict_file.dirpath().join('worktree')
cmd_output('git', 'worktree', 'add', str(worktree))
with worktree.as_cwd():
cmd_output(
'git', 'pull', '--no-rebase', 'origin', 'master', retcode=None,
)
assert os.path.exists(
f1_is_a_conflict_file.join(
'.git', 'worktrees', 'worktree', 'MERGE_MSG',
),
)
yield worktree
Copy link
Member

Choose a reason for hiding this comment

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

I meant that you would inline this directly in your test above the call to the other test

no need to make a fixture if only one thing is using it

Comment on lines 117 to 123
def test_not_in_a_repository(tmpdir):
with tmpdir.as_cwd():
f1 = tmpdir.join('f1')
f1.write('foo\n')
assert main(['f1']) == 0


Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is a realistic test -- I expect these to always run inside a git repo

@@ -13,12 +16,17 @@


def is_in_merge() -> int:
Copy link
Member

Choose a reason for hiding this comment

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

this is probably my fault -- but can you change this to -> bool

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

@asottile asottile merged commit cb02c5b into pre-commit:master Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

check_merge_conflict.py does not work in worktrees
2 participants