Skip to content

Commit 0079b6e

Browse files
Merge pull request #85 from Nautilus-Cyberneering/issue-72-fix-git-language-bug
Issue 72 fix git language bug
2 parents f9eb2a4 + 8b1561d commit 0079b6e

File tree

10 files changed

+125
-51
lines changed

10 files changed

+125
-51
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
import {isAbsolute, resolve} from 'path'
2+
import {GitDirNotFoundError} from '../../src/errors'
13
import {GitRepoDir} from '../../src/git-repo-dir'
4+
import {createInexistentTempDir} from '../../src/__tests__/helpers'
25

36
describe('GitRepoDir', () => {
47
it('should contain dir path for the Git repo', () => {
5-
const gitRepoDir = new GitRepoDir('./')
8+
const currentDir = resolve('./')
9+
const gitRepoDir = new GitRepoDir(currentDir)
610

7-
expect(gitRepoDir.getDirPath()).toBe('./')
11+
expect(gitRepoDir.getDirPath()).toBe(currentDir)
12+
})
13+
14+
it('should fail when the dir does not exist', async () => {
15+
const inexistentDir = await createInexistentTempDir()
16+
const failingRepoDirTest = (): GitRepoDir => new GitRepoDir(inexistentDir)
17+
18+
expect(failingRepoDirTest).toThrow(GitDirNotFoundError)
819
})
920

1021
it('should compare two git repo dirs', () => {
@@ -14,4 +25,9 @@ describe('GitRepoDir', () => {
1425
expect(gitRepoDir1.equalsTo(gitRepoDir1)).toBe(true)
1526
expect(gitRepoDir1.equalsTo(gitRepoDir2)).toBe(false)
1627
})
28+
29+
it('should convert a relative path to an absolute one', () => {
30+
const gitRepoDir = new GitRepoDir('../')
31+
expect(isAbsolute(gitRepoDir.getDirPath())).toBe(true)
32+
})
1733
})

__tests__/unit/git-repo.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ describe('GitRepo', () => {
2323

2424
await gitRepo.init()
2525

26-
expect(await gitRepo.isInitialized()).toBe(true)
26+
expect(gitRepo.isInitialized()).toBe(true)
2727
})
2828

2929
it('should check if a repo has been initialized', async () => {
3030
const gitRepoDir = new GitRepoDir(await createTempEmptyDir())
3131
const git = await newSimpleGitWithCommitterIdentity(gitRepoDir)
3232
const gitRepo = new GitRepo(gitRepoDir, git)
3333

34-
expect(await gitRepo.isInitialized()).toBe(false)
34+
expect(gitRepo.isInitialized()).toBe(false)
3535

3636
await gitRepo.init()
3737

38-
expect(await gitRepo.isInitialized()).toBe(true)
38+
expect(gitRepo.isInitialized()).toBe(true)
3939
})
4040
})

dist/index.js

Lines changed: 48 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface GpgSigningKeyConfig {
2424

2525
export function testConfiguration(): TestConfig {
2626
const gpgPrivateKeyBody = fs.readFileSync(
27-
'__tests__/fixtures/test-key-committer.pgp',
27+
`${__dirname}/../../__tests__/fixtures/test-key-committer.pgp`,
2828
{
2929
encoding: 'utf8',
3030
flag: 'r'

src/__tests__/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import {GitRepo} from '../git-repo'
99
import {GitRepoDir} from '../git-repo-dir'
1010

1111
import {createTempDir} from 'jest-fixtures'
12+
import {join} from 'path'
1213
import {testConfiguration} from './config'
1314

1415
export async function createTempEmptyDir(): Promise<string> {
1516
const tempGitDirPath = await createTempDir()
1617
return tempGitDirPath
1718
}
1819

20+
export async function createInexistentTempDir(): Promise<string> {
21+
return join(await createTempEmptyDir(), `inexistent`)
22+
}
23+
1924
export async function createInitializedTempGnuPGHomeDir(): Promise<string> {
2025
const tempGnuPGHomeDir = await createTempDir()
2126
const keygrip = testConfiguration().gpg_signing_key.keygrip

src/errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ export class GitDirNotInitializedError extends Error {
3737
}
3838
}
3939

40+
export class GitDirNotFoundError extends Error {
41+
constructor(dir: string) {
42+
super(`Git dir: ${dir} does not exist or is not reachable`)
43+
Object.setPrototypeOf(this, GitDirNotFoundError.prototype)
44+
}
45+
}
46+
4047
export class PendingJobsLimitReachedError extends Error {
4148
constructor(committedMessage: CommittedMessage) {
4249
super(

src/git-repo-dir.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
import {isAbsolute, resolve} from 'path'
2+
import {GitDirNotFoundError} from './errors'
3+
import {existsSync} from 'fs'
4+
15
export class GitRepoDir {
26
private readonly dirPath: string
37

48
constructor(dirPath: string) {
5-
// TODO: validate dir path
6-
this.dirPath = dirPath
9+
this.validatePath(dirPath)
10+
this.dirPath = this.normalizePath(dirPath)
11+
}
12+
13+
validatePath(dirPath): void {
14+
if (!existsSync(dirPath)) {
15+
throw new GitDirNotFoundError(dirPath)
16+
}
17+
}
18+
19+
normalizePath(dirPath): string {
20+
return isAbsolute(dirPath) ? dirPath : resolve(dirPath)
721
}
822

923
getDirPath(): string {

src/git-repo.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
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'
82
import {CommitMessage} from './commit-message'
93
import {CommitOptions} from './commit-options'
4+
import {GitDirNotInitializedError} from './errors'
105
import {GitRepoDir} from './git-repo-dir'
6+
import {execSync} from 'child_process'
7+
import {existsSync} from 'fs'
118

129
export 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
}

src/queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Queue {
5757
}
5858

5959
private async guardThatGitRepoHasBeenInitialized(): Promise<void> {
60-
const isInitialized = await this.gitRepo.isInitialized()
60+
const isInitialized = this.gitRepo.isInitialized()
6161
if (!isInitialized) {
6262
throw new GitDirNotInitializedError(this.gitRepo.getDirPath())
6363
}

0 commit comments

Comments
 (0)