Skip to content

fix: handle repos without git gracefully #985

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

Merged
merged 2 commits into from
Apr 9, 2025
Merged
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
19 changes: 13 additions & 6 deletions packages/utils/src/lib/git/git.commits-and-tags.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { type LogOptions as SimpleGitLogOptions, simpleGit } from 'simple-git';
import { type Commit, commitSchema } from '@code-pushup/models';
import { stringifyError } from '../errors.js';
import { ui } from '../logging.js';
import { isSemver } from '../semver.js';

export async function getLatestCommit(
git = simpleGit(),
): Promise<Commit | null> {
const log = await git.log({
maxCount: 1,
// git log -1 --pretty=format:"%H %s %an %aI" - See: https://git-scm.com/docs/pretty-formats
format: { hash: '%H', message: '%s', author: '%an', date: '%aI' },
});
return commitSchema.parse(log.latest);
try {
const log = await git.log({
maxCount: 1,
// git log -1 --pretty=format:"%H %s %an %aI" - See: https://git-scm.com/docs/pretty-formats
format: { hash: '%H', message: '%s', author: '%an', date: '%aI' },
});
return commitSchema.parse(log.latest);
} catch (error) {

Check failure on line 17 in packages/utils/src/lib/git/git.commits-and-tags.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
ui().logger.error(stringifyError(error));
return null;
}

Check warning on line 20 in packages/utils/src/lib/git/git.commits-and-tags.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 18-20 are not covered in any test case.
}

export async function getCurrentBranchOrTag(
Expand Down Expand Up @@ -59,7 +66,7 @@
const fromIndex = finIndex(from, 0);
const toIndex = finIndex(to, undefined);
return allTags
.slice(fromIndex, toIndex ? toIndex + 1 : toIndex)

Check failure on line 69 in packages/utils/src/lib/git/git.commits-and-tags.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
.slice(0, maxCount ?? undefined);
}

Expand Down
Loading