Fix migrations where .git/objects is a symlink#6042
Conversation
Android's `repo` tool creates `.git/objects` as a symlink to a place outside the `.git` directory for unknown reasons. When that happens, Git operations where we set the working directory to that directory fail. This is because when Git performs discovery on the working directory, it first canonicalizes the path, which takes it outside of the `.git` directory and it then fails to find anything that looks like a Git repository. In our codebase, we set the path to the object directory root in a couple of places which cause this problem. To avoid this problem, simply join the repository root with `..`, which is always applied before canonicalization and will result in the path being properly set to the `.git` directory, which results in `git lfs migrate import` working correctly in this configuration.
chrisd8088
left a comment
There was a problem hiding this comment.
Thanks very much for jumping in with a fix for these issues!
I was puzzled for a few minutes as to how the existing code could have functioned at all, if adding a .. path segment at the end of a so-called "root" path also works without problems (and solves some others), until I realized what is meant by the term "root" in this context.
Thanks again very much for fixing these problems!
Reference Notes
For my own future reference, the "root" referred to here is not the top of the repository's working tree or the top of Git's internal directory structure (i.e., .git), but the top of Git's internal object database directory structure (i.e., .git/objects). So appending a .. path segment always takes us back up to the top of Git's internal directory structure.
We construct the path to the .git/objects directory by first calling the GitCommonDir() method of our git package, which usually runs git rev-parse --git-common-dir, and then passing that path to the ObjectDatabase() method, which appends the objects path segment, unless GIT_OBJECT_DIRECTORY is specified directly. The resultant path is what is then used in the call to create the ObjectDatabase using our git-lfs/gitobj package.
Android's
repotool creates.git/objectsas a symlink to a place outside the.gitdirectory for unknown reasons. When that happens, Git operations where we set the working directory to that directory fail. This is because when Git performs discovery on the working directory, it first canonicalizes the path, which takes it outside of the.gitdirectory and it then fails to find anything that looks like a Git repository.In our codebase, we set the path to the object directory root in a couple of places which cause this problem. To avoid this problem, simply join the repository root with
.., which is always applied before canonicalization and will result in the path being properly set to the.gitdirectory, which results ingit lfs migrate importworking correctly in this configuration.Fixes #5426
Fixes #6030