Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 18 additions & 2 deletions __tests__/unit/git-repo-dir.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import {isAbsolute, resolve} from 'path'
import {GitDirNotFoundError} from '../../src/errors'
import {GitRepoDir} from '../../src/git-repo-dir'
import {createInexistentTempDir} from '../../src/__tests__/helpers'

describe('GitRepoDir', () => {
it('should contain dir path for the Git repo', () => {
const gitRepoDir = new GitRepoDir('./')
const currentDir = resolve('./')
const gitRepoDir = new GitRepoDir(currentDir)

expect(gitRepoDir.getDirPath()).toBe('./')
expect(gitRepoDir.getDirPath()).toBe(currentDir)
})

it('should fail when the dir does not exist', async () => {
const inexistentDir = await createInexistentTempDir()
const failingRepoDirTest = (): GitRepoDir => new GitRepoDir(inexistentDir)

expect(failingRepoDirTest).toThrow(GitDirNotFoundError)
})

it('should compare two git repo dirs', () => {
Expand All @@ -14,4 +25,9 @@ describe('GitRepoDir', () => {
expect(gitRepoDir1.equalsTo(gitRepoDir1)).toBe(true)
expect(gitRepoDir1.equalsTo(gitRepoDir2)).toBe(false)
})

it('should convert a relative path to an absolute one', () => {
const gitRepoDir = new GitRepoDir('../')
expect(isAbsolute(gitRepoDir.getDirPath())).toBe(true)
})
})
6 changes: 3 additions & 3 deletions __tests__/unit/git-repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ describe('GitRepo', () => {

await gitRepo.init()

expect(await gitRepo.isInitialized()).toBe(true)
expect(gitRepo.isInitialized()).toBe(true)
})

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

expect(await gitRepo.isInitialized()).toBe(false)
expect(gitRepo.isInitialized()).toBe(false)

await gitRepo.init()

expect(await gitRepo.isInitialized()).toBe(true)
expect(gitRepo.isInitialized()).toBe(true)
})
})
67 changes: 48 additions & 19 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/__tests__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface GpgSigningKeyConfig {

export function testConfiguration(): TestConfig {
const gpgPrivateKeyBody = fs.readFileSync(
'__tests__/fixtures/test-key-committer.pgp',
`${__dirname}/../../__tests__/fixtures/test-key-committer.pgp`,
{
encoding: 'utf8',
flag: 'r'
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import {GitRepo} from '../git-repo'
import {GitRepoDir} from '../git-repo-dir'

import {createTempDir} from 'jest-fixtures'
import {join} from 'path'
import {testConfiguration} from './config'

export async function createTempEmptyDir(): Promise<string> {
const tempGitDirPath = await createTempDir()
return tempGitDirPath
}

export async function createInexistentTempDir(): Promise<string> {
return join(await createTempEmptyDir(), `inexistent`)
}

export async function createInitializedTempGnuPGHomeDir(): Promise<string> {
const tempGnuPGHomeDir = await createTempDir()
const keygrip = testConfiguration().gpg_signing_key.keygrip
Expand Down
7 changes: 7 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export class GitDirNotInitializedError extends Error {
}
}

export class GitDirNotFoundError extends Error {
constructor(dir: string) {
super(`Git dir: ${dir} does not exist or is not reachable`)
Object.setPrototypeOf(this, GitDirNotFoundError.prototype)
}
}

export class PendingJobsLimitReachedError extends Error {
constructor(committedMessage: CommittedMessage) {
super(
Expand Down
18 changes: 16 additions & 2 deletions src/git-repo-dir.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import {isAbsolute, resolve} from 'path'
import {GitDirNotFoundError} from './errors'
import {existsSync} from 'fs'

export class GitRepoDir {
private readonly dirPath: string

constructor(dirPath: string) {
// TODO: validate dir path
this.dirPath = dirPath
this.validatePath(dirPath)
this.dirPath = this.normalizePath(dirPath)
}

validatePath(dirPath): void {
if (!existsSync(dirPath)) {
throw new GitDirNotFoundError(dirPath)
}
}

normalizePath(dirPath): string {
return isAbsolute(dirPath) ? dirPath : resolve(dirPath)
}

getDirPath(): string {
Expand Down
47 changes: 25 additions & 22 deletions src/git-repo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
CommitResult,
DefaultLogFields,
GitResponseError,
LogResult,
SimpleGit
} from 'simple-git'
import {CommitResult, DefaultLogFields, LogResult, SimpleGit} from 'simple-git'
import {CommitMessage} from './commit-message'
import {CommitOptions} from './commit-options'
import {GitDirNotInitializedError} from './errors'
import {GitRepoDir} from './git-repo-dir'
import {execSync} from 'child_process'
import {existsSync} from 'fs'

export class GitRepo {
private readonly dir: GitRepoDir
Expand All @@ -18,8 +15,17 @@ export class GitRepo {
this.git = git
}

async isInitialized(): Promise<boolean> {
return await this.git.checkIsRepo()
isInitialized(): boolean {
try {
// Make sure the string we will pass to to the shell is an actual dir
if (!existsSync(this.dir.getDirPath())) {
throw new Error()
}
execSync(`git -C ${this.getDirPath()} status`)
} catch {
return false
}
return true
}

getDir(): GitRepoDir {
Expand Down Expand Up @@ -48,21 +54,18 @@ export class GitRepo {
}

async hasCommits(): Promise<boolean> {
// TODO: find a better way to check if the repo has commits
const currentBranch = await this.getCurrentBranch()
if (!this.isInitialized()) {
throw new GitDirNotInitializedError(this.dir.getDirPath())
}
try {
await this.log()
} catch (err) {
if (
(err as GitResponseError).message.includes(
`fatal: your current branch '${currentBranch}' does not have any commits yet`
)
) {
// No commits yet
return false
} else {
throw err
// Make sure the string we will pass to to the shell is an actual dir
if (!existsSync(this.dir.getDirPath())) {
throw new Error()
}
execSync(`git -C ${this.dir.getDirPath()} log -n 0`)
} catch (err) {
// No commits yet
return false
}
return true
}
Expand Down
2 changes: 1 addition & 1 deletion src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Queue {
}

private async guardThatGitRepoHasBeenInitialized(): Promise<void> {
const isInitialized = await this.gitRepo.isInitialized()
const isInitialized = this.gitRepo.isInitialized()
if (!isInitialized) {
throw new GitDirNotInitializedError(this.gitRepo.getDirPath())
}
Expand Down