Skip to content

Commit

Permalink
feat(core): source control commands for retrieving commits
Browse files Browse the repository at this point in the history
These two commands will be used to retrieve commits from the source
control provider. One retrieves the latest, current commit; and the
other returns a list of commits. The commit list can accept from and to
arguments that filter the list of commits that will be returned.

Ref: #75
  • Loading branch information
GeorgeCadwallader authored and AdeAttwood committed Oct 18, 2024
1 parent 4661c12 commit 604b046
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/lib/source-control/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {run} from '../exec';
import * as fs from 'fs';
import type {SourceControlProvider} from '.';

const git: SourceControlProvider = {
Expand All @@ -17,6 +18,37 @@ const git: SourceControlProvider = {
const {stdout} = await run(`git rev-parse --show-toplevel`);
return stdout.trim();
},
getCurrentCommitMessage: async () => {
const root = await git.root();

const commitEditMessagePath = `${root}/.git/COMMIT_EDITMSG`;

if (fs.existsSync(commitEditMessagePath)) {
const commitEditMessage = fs
.readFileSync(commitEditMessagePath)
.toString();

const scissors = commitEditMessage.indexOf(
'------------------------ >8 ------------------------',
);

const end = scissors > -1 ? scissors : commitEditMessage.length;
const editMessage = commitEditMessage.substring(0, end).replace(/^#.*$/gm, '').trim();

if (editMessage.length) {
return editMessage;
}
}

return null;
},
getCommits: async (from, to) => {
const range = from && to ? `${from}..${to}` : '';
const {stdout: commitsString} = await run(
`git log ${range} --pretty=format:"%B%x00"`,
);
return commitsString.split('\0');
},
};

export default git;
13 changes: 13 additions & 0 deletions src/lib/source-control/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ export interface SourceControlProvider {
* Get the current root directory of for the repository.
*/
root(): Promise<string>;
/**
* Get the current commit message from the source provider.
*/
getCurrentCommitMessage(): Promise<string | null>;
/**
* Get a list of commits from the source provider to lint.
*
* @param from A string representing the starting commit hash or reference to
* begin retrieving commits from.
* @param to A string representing the ending commit hash or reference to
* stop retrieving commits from.
*/
getCommits(from?: string, to?: string): Promise<string[]>;
}

const PROVIDERS = [git, sapling];
Expand Down

0 comments on commit 604b046

Please sign in to comment.