Skip to content

Commit

Permalink
Merge pull request nautilus-cyberneering#259 from josecelano/issue-19…
Browse files Browse the repository at this point in the history
…8-fix-code-scanning-alert

Refactor after implementing issue 198
  • Loading branch information
josecelano committed Jun 8, 2022
2 parents 53ac70e + c832485 commit 93576e2
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 252 deletions.
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

0 comments on commit 93576e2

Please sign in to comment.