Skip to content

Commit

Permalink
chore: refactor tests, get everything working, remove cb() form
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 17, 2024
1 parent f6d218f commit 8e2d0a1
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 216 deletions.
87 changes: 22 additions & 65 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
execFile,
execFileSync,
ExecFileSyncOptions,
ExecException,
} from "child_process";
ExecFileException,
} from "node:child_process";
import { promisify } from "node:util";
import { existsSync } from "fs";
import createDebugger from "debug";

const debug = createDebugger("gitlog");
const execFilePromise = promisify(execFile);

const delimiter = "\t";
const fieldMap = {
Expand All @@ -33,7 +34,7 @@ const fieldMap = {
export type CommitField = keyof typeof fieldMap;

const notOptFields = ["status", "files"] as const;
type NotOptField = typeof notOptFields[number];
type NotOptField = (typeof notOptFields)[number];

export interface FileLineRange {
/** Will be pass as -L <startLine>,<endLine>:<file> */
Expand All @@ -56,7 +57,7 @@ const defaultFields = [
"authorName",
"authorDate",
] as const;
type DefaultField = typeof defaultFields[number];
type DefaultField = (typeof defaultFields)[number];

export interface GitlogOptions<Fields extends string = DefaultField> {
/** The location of the repo */
Expand Down Expand Up @@ -306,7 +307,7 @@ function createCommandArguments<
return command;
}

type GitlogError = ExecException | null;
type GitlogError = ExecFileException | null;

type CommitBase<Field extends string> = Record<Field, string>;
type CommitBaseWithFiles<Field extends string> = Record<
Expand All @@ -326,18 +327,15 @@ function gitlog<Field extends CommitField = DefaultField>(

function gitlog<Field extends CommitField = DefaultField>(
userOptions: GitlogOptions<Field> & { nameStatus: false }
): CommitBase<Field>[];
): Promise<CommitBase<Field>[]>;

function gitlog<Field extends CommitField = DefaultField>(
userOptions: GitlogOptions<Field>
): CommitBaseWithFiles<Field>[];
): Promise<CommitBaseWithFiles<Field>[]>;

function gitlog<Field extends CommitField = DefaultField>(
userOptions: GitlogOptions<Field>,
cb?:
| ((err: GitlogError, commits: CommitBase<Field>[]) => void)
| ((err: GitlogError, commits: CommitBaseWithFiles<Field>[]) => void)
): CommitBase<Field>[] | CommitBaseWithFiles<Field>[] | void {
async function gitlog<Field extends CommitField = DefaultField>(
userOptions: GitlogOptions<Field>
): Promise<CommitBase<Field>[] | CommitBaseWithFiles<Field>[] | void> {
if (!userOptions.repo) {
throw new Error("Repo required!");
}
Expand All @@ -354,60 +352,19 @@ function gitlog<Field extends CommitField = DefaultField>(
const execOptions = { cwd: userOptions.repo, ...userOptions.execOptions };
const commandArguments = createCommandArguments(options);

if (!cb) {
const stdout = execFileSync(
"git",
commandArguments,
execOptions
).toString();
const commits = stdout.split("@begin@");

if (commits[0] === "") {
commits.shift();
}
const { stdout } = await execFilePromise(
"git",
commandArguments,
execOptions
);
const commits = stdout.split("@begin@");

debug("commits", commits);
return parseCommits(commits, options.fields, options.nameStatus);
if (commits[0] === "") {
commits.shift();
}

execFile("git", commandArguments, execOptions, (err, stdout, stderr) => {
debug("stdout", stdout);
const commits = stdout.split("@begin@");

if (commits[0] === "") {
commits.shift();
}

debug("commits", commits);

if (stderr) {
err = new Error(stderr);
}

cb(err, parseCommits(commits, options.fields, options.nameStatus));
});
}

export function gitlogPromise<Field extends CommitField = DefaultField>(
options: GitlogOptions<Field> & { nameStatus: false }
): Promise<CommitBase<Field>[]>;

export function gitlogPromise<Field extends CommitField = DefaultField>(
options: GitlogOptions<Field>
): Promise<CommitBaseWithFiles<Field>[]>;

export function gitlogPromise<Field extends CommitField = DefaultField>(
options: GitlogOptions<Field>
): Promise<CommitBase<Field>[]> {
return new Promise((resolve, reject) => {
gitlog(options, (err, commits) => {
if (err) {
reject(err);
} else {
resolve(commits);
}
});
});
debug("commits", commits);
return parseCommits(commits, options.fields, options.nameStatus);
}

export default gitlog;
5 changes: 3 additions & 2 deletions test/create-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ git clone -l $REPO test-repo-clone
cd test-repo-clone
git config --local user.email "[email protected]"
git config --local user.name "Your Name"
git config --local init.defaultbranch main

# Add
for i in {1..16}
Expand Down Expand Up @@ -47,14 +48,14 @@ git checkout -b new-branch
touch new-file
git add new-file
git commit -m "Added new file on new branch"
git checkout master
git checkout main

# Merge commit
git checkout -b new-merge-branch
touch foo
git add foo
git commit -m "Commit to be merged"
git checkout master
git checkout main
git merge --no-edit --no-ff new-merge-branch
git branch -d new-merge-branch

Expand Down
Loading

0 comments on commit 8e2d0a1

Please sign in to comment.