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

feat: support symbolic link git dir #1353

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/resolveGitRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
const stats = await fs.lstat(defaultDir)
// If .git is a directory, use it
if (stats.isDirectory()) return defaultDir
// If .git is a symbolic link, find realpath and use it,
// thought realpath is a directory, avoid infinite recursion.
if (stats.isSymbolicLink()) {
return fs.realpath(defaultDir);

Check failure on line 25 in lib/resolveGitRepo.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `;`
Copy link
Member

Choose a reason for hiding this comment

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

This probably needs to be wrapped in normalizePath:

Suggested change
return fs.realpath(defaultDir);
return normalizePath(await fs.realpath(defaultDir))

}
// Otherwise .git is a file containing path to real location
const file = (await readFile(defaultDir)).toString()
return path.resolve(gitDir, file.replace(/^gitdir: /, '')).trim()
Expand Down
16 changes: 16 additions & 0 deletions test/unit/resolveGitRepo.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import fs from 'node:fs/promises'

import { normalizePath } from '../../lib/normalizePath.js'
import { determineGitDir, resolveGitRepo } from '../../lib/resolveGitRepo.js'
Expand Down Expand Up @@ -48,6 +49,21 @@
expect(gitDir).toEqual(null)
})

it('should resolve when git config dir is symbolic link', async () => {
const link = path.join(__dirname, '.git')
await fs.symlink(expected, link)

const processCwdBkp = process.cwd
process.cwd = () => __dirname

const { gitDir, gitConfigDir } = await resolveGitRepo()
expect(gitDir).toEqual(expected)
expect(gitConfigDir).toEqual(path.join(expected, '.git'));

Check failure on line 61 in test/unit/resolveGitRepo.spec.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `;`

await fs.unlink(link);

Check failure on line 63 in test/unit/resolveGitRepo.spec.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `;`
process.cwd = processCwdBkp
})

describe('determineGitDir', () => {
it('should resolve to current working dir when relative dir is empty', () => {
const cwd = process.cwd()
Expand Down
Loading