1- import {
2- CommitResult ,
3- DefaultLogFields ,
4- GitResponseError ,
5- LogResult ,
6- SimpleGit
7- } from 'simple-git'
1+ import { CommitResult , DefaultLogFields , LogResult , SimpleGit } from 'simple-git'
82import { CommitMessage } from './commit-message'
93import { CommitOptions } from './commit-options'
4+ import { GitDirNotInitializedError } from './errors'
105import { GitRepoDir } from './git-repo-dir'
6+ import { execSync } from 'child_process'
7+ import { existsSync } from 'fs'
118
129export class GitRepo {
1310 private readonly dir : GitRepoDir
@@ -18,8 +15,17 @@ export class GitRepo {
1815 this . git = git
1916 }
2017
21- async isInitialized ( ) : Promise < boolean > {
22- return await this . git . checkIsRepo ( )
18+ isInitialized ( ) : boolean {
19+ try {
20+ // Make sure the string we will pass to to the shell is an actual dir
21+ if ( ! existsSync ( this . dir . getDirPath ( ) ) ) {
22+ throw new Error ( )
23+ }
24+ execSync ( `git -C ${ this . getDirPath ( ) } status` )
25+ } catch {
26+ return false
27+ }
28+ return true
2329 }
2430
2531 getDir ( ) : GitRepoDir {
@@ -48,21 +54,18 @@ export class GitRepo {
4854 }
4955
5056 async hasCommits ( ) : Promise < boolean > {
51- // TODO: find a better way to check if the repo has commits
52- const currentBranch = await this . getCurrentBranch ( )
57+ if ( ! this . isInitialized ( ) ) {
58+ throw new GitDirNotInitializedError ( this . dir . getDirPath ( ) )
59+ }
5360 try {
54- await this . log ( )
55- } catch ( err ) {
56- if (
57- ( err as GitResponseError ) . message . includes (
58- `fatal: your current branch '${ currentBranch } ' does not have any commits yet`
59- )
60- ) {
61- // No commits yet
62- return false
63- } else {
64- throw err
61+ // Make sure the string we will pass to to the shell is an actual dir
62+ if ( ! existsSync ( this . dir . getDirPath ( ) ) ) {
63+ throw new Error ( )
6564 }
65+ execSync ( `git -C ${ this . dir . getDirPath ( ) } log -n 0` )
66+ } catch ( err ) {
67+ // No commits yet
68+ return false
6669 }
6770 return true
6871 }
0 commit comments