Skip to content

Refactor #441

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

Merged
merged 4 commits into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
check if commit exists through message name
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Aug 9, 2020
commit 7f663ccd2b2f5743719b4416eb6f5342da1e037f
32 changes: 32 additions & 0 deletions src/services/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,35 @@ export async function loadCommitHistory(): Promise<string[]> {
export function getShortHash(hash: string): string {
return hash.slice(0, 7)
}

export async function getCommitMessage(hash: string): Promise<string | null> {
try {
// returns an list of commit hashes
const { stdout, stderr } = await exec({ command: `git log -n 1 --pretty=format:%s ${hash}` })
if (stderr) {
return null
}
// string match on remote output
return stdout
} catch (error) {
logger('error', error)
// likely no git commit message found
return null
}
}

export async function commitsExistsByMessage(message: string): Promise<boolean> {
try {
// returns an list of commit hashes
// note: may not work with quotes in message
const { stdout, stderr } = await exec({ command: `git log -g --grep='${message}'` })
if (stderr) {
return false
}
return !!stdout.length
} catch (error) {
logger('error', error)
// likely no commit found
return false
}
}
2 changes: 1 addition & 1 deletion src/services/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as TT from 'typings/tutorial'
import * as git from '../git'
import loadCommits from './utils/loadCommits'
import { loadCommits } from './utils/commits'
import { loadWatchers, resetWatchers } from './utils/watchers'
import openFiles from './utils/openFiles'
import runCommands from './utils/runCommands'
Expand Down
23 changes: 23 additions & 0 deletions src/services/hooks/utils/commits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as git from '../../git'

// avoid duplicate commits
const verifyCommitUnique = async (hash: string): Promise<boolean> => {
const message: string | null = await git.getCommitMessage(hash)
if (!message) {
return false
}
const exists: boolean = await git.commitsExistsByMessage(message)
return exists
}

export const loadCommits = async (commits: string[] = []): Promise<void> => {
if (commits && commits.length) {
// load the current list of commits for validation
for (const commit of commits) {
const commitExists = await verifyCommitUnique(commit)
if (!commitExists) {
await git.loadCommit(commit)
}
}
}
}
12 changes: 0 additions & 12 deletions src/services/hooks/utils/loadCommits.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/services/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LOG } from '../../environment'

export type Log = string | number | object | null | undefined // eslint-disable-line
export type Log = any

const logger = (...messages: Log[]): void => {
if (!LOG) {
Expand Down