Skip to content

Commit

Permalink
prevent git console error
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Apr 18, 2024
1 parent 1ba6b3d commit d3520da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-turtles-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mdxts": patch
---

Prevent fatal git error in console by checking for `.git` directory in `getGitMetadata`.
11 changes: 8 additions & 3 deletions packages/mdxts/src/utils/get-git-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { execSync } from 'node:child_process'
import { existsSync } from 'node:fs'

let isGitRepository: null | boolean = null
let hasCheckedIfShallow = false
let hadGitError = false

/** Returns aggregated metadata about multiple files from git history. */
export function getGitMetadata(filePaths: string[]) {
if (!hasCheckedIfShallow) {
if (isGitRepository === null) {
isGitRepository = existsSync('.git')
}

if (isGitRepository && !hasCheckedIfShallow) {
try {
const isShallow = execSync('git rev-parse --is-shallow-repository')
.toString()
Expand All @@ -31,8 +37,7 @@ export function getGitMetadata(filePaths: string[]) {
}
}

/** Bail early if the repository is shallow cloned or there was a git error (e.g. not a git repository). */
if (hadGitError) {
if (!isGitRepository || hadGitError) {
return {
authors: [],
createdAt: undefined,
Expand Down

0 comments on commit d3520da

Please sign in to comment.