MCPcopy Create free account
hub / github.com/Noumena-Network/code / isCurrentDirectoryBareGitRepo

Function isCurrentDirectoryBareGitRepo

src/utils/git.ts:891–940  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

889 */
890/* eslint-disable custom-rules/no-sync-fs -- sync permission-eval check */
891export function isCurrentDirectoryBareGitRepo(): boolean {
892 const fs = getFsImplementation()
893 const cwd = getCwd()
894
895 const gitPath = join(cwd, '.git')
896 try {
897 const stats = fs.statSync(gitPath)
898 if (stats.isFile()) {
899 // worktree/submodule — Git follows the gitdir reference
900 return false
901 }
902 if (stats.isDirectory()) {
903 const gitHeadPath = join(gitPath, 'HEAD')
904 try {
905 // SECURITY: check isFile(). An attacker creating .git/HEAD as a
906 // DIRECTORY would pass a bare statSync but Git's setup_git_directory
907 // rejects it (not a valid HEAD) and falls back to cwd discovery.
908 if (fs.statSync(gitHeadPath).isFile()) {
909 // normal repo — .git/HEAD valid, Git won't fall back to cwd
910 return false
911 }
912 // .git/HEAD exists but is not a regular file — fall through
913 } catch {
914 // .git exists but no HEAD — fall through to bare-repo check
915 }
916 }
917 } catch {
918 // no .git — fall through to bare-repo indicator check
919 }
920
921 // No valid .git/HEAD found. Check if cwd has bare git repo indicators.
922 // Be cautious — flag if ANY of these exist without a valid .git reference.
923 // Per-indicator try/catch so an error on one doesn't mask another.
924 try {
925 if (fs.statSync(join(cwd, 'HEAD')).isFile()) return true
926 } catch {
927 // no HEAD
928 }
929 try {
930 if (fs.statSync(join(cwd, 'objects')).isDirectory()) return true
931 } catch {
932 // no objects/
933 }
934 try {
935 if (fs.statSync(join(cwd, 'refs')).isDirectory()) return true
936 } catch {
937 // no refs/
938 }
939 return false
940}
941/* eslint-enable custom-rules/no-sync-fs */

Callers 2

checkReadOnlyConstraintsFunction · 0.85

Calls 2

getFsImplementationFunction · 0.85
getCwdFunction · 0.85

Tested by

no test coverage detected