Skip to content
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

Refactor after implementing issue 198 #259

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
2 changes: 1 addition & 1 deletion .github/workflows/deploy-documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
name: Deploy documentation
runs-on: ubuntu-latest
permissions:
contents: write
contents: write
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mega-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

permissions:
pull-requests: write
statuses: write
statuses: write

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
Expand Down
49 changes: 44 additions & 5 deletions __tests__/unit/git-repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {

import {GitRepo} from '../../src/git-repo'
import {GitRepoDir} from '../../src/git-repo-dir'
import {GitDirNotInitializedError} from '../../src/errors'

describe('GitRepo', () => {
it('should be bound to a file system dir', async () => {
Expand All @@ -16,25 +17,63 @@ describe('GitRepo', () => {
expect(gitRepo.getDir().equalsTo(gitRepoDir)).toBe(true)
})

it('could init a git repo', async () => {
it('should be able to init a git repo', async () => {
const gitRepoDir = new GitRepoDir(await createTempEmptyDir())
const git = await newSimpleGitWithCommitterIdentity(gitRepoDir)
const gitRepo = new GitRepo(gitRepoDir, git)

const gitRepo = new GitRepo(gitRepoDir, git)
await gitRepo.init()

expect(gitRepo.isInitialized()).toBe(true)
expect(await 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(gitRepo.isInitialized()).toBe(false)
expect(await gitRepo.isInitialized()).toBe(false)

await gitRepo.init()

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

it('should check if an initialized repo has commits', async () => {
const gitRepoDir = new GitRepoDir(await createTempEmptyDir())
const git = await newSimpleGitWithCommitterIdentity(gitRepoDir)

const gitRepo = new GitRepo(gitRepoDir, git)
await gitRepo.init()

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

await git.raw(
'commit',
'--no-gpg-sign',
'--allow-empty',
'--message',
'Initial commit'
)

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

it('should fail while checking if an uninitialized repo has commits', async () => {
const gitRepoDir = new GitRepoDir(await createTempEmptyDir())
const git = await newSimpleGitWithCommitterIdentity(gitRepoDir)

const gitRepo = new GitRepo(gitRepoDir, git)

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

const checkIfItHasCommits = async (): Promise<boolean> => {
return await gitRepo.hasCommits()
}

await expect(checkIfItHasCommits).rejects.toThrow(
new GitDirNotInitializedError(gitRepoDir.getDirPath())
)
})
})
69 changes: 0 additions & 69 deletions __tests__/unit/git.test.ts

This file was deleted.

107 changes: 19 additions & 88 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.

24 changes: 10 additions & 14 deletions src/git-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {CommitMessage} from './commit-message'
import {CommitOptions} from './commit-options'
import {GitDirNotInitializedError} from './errors'
import {GitRepoDir} from './git-repo-dir'
import {Git} from './git'

export class GitRepo {
private readonly dir: GitRepoDir
Expand All @@ -14,10 +13,9 @@ export class GitRepo {
this.git = git
}

isInitialized(): boolean {
async isInitialized(): Promise<boolean> {
try {
const git = new Git(this.dir)
git.status()
await this.git.raw('status')
} catch {
return false
}
Expand All @@ -40,29 +38,27 @@ export class GitRepo {
this.git.env(name, value)
}

async getCurrentBranch(): Promise<string | null> {
const status = await this.git.status()
return status.current
}

async log(): Promise<LogResult<DefaultLogFields>> {
return await this.git.log()
}

async hasCommits(): Promise<boolean> {
if (!this.isInitialized()) {
throw new GitDirNotInitializedError(this.dir.getDirPath())
}
await this.guardThatRepoIsInitialized()
try {
const git = new Git(this.dir)
git.log()
await this.git.raw('log', '-n', '0')
} catch (err) {
// No commits yet
return false
}
return true
}

async guardThatRepoIsInitialized(): Promise<void> {
if (!(await this.isInitialized())) {
throw new GitDirNotInitializedError(this.dir.getDirPath())
}
}

async commit(
commitMessage: CommitMessage,
commitOptions: CommitOptions
Expand Down
Loading