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

filter author/committer with an array of strings #56

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Show commits older than a specific date.
### author/committer

Limit the commits output to ones with author/committer header lines that match the specified pattern.
Can also be an array of patterns/strings.

### nameStatus

Expand Down
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export interface GitlogOptions<Fields extends string = DefaultField> {
/** File filter for the git log command */
file?: string;
/** Limit the commits output to ones with author header lines that match the specified pattern. */
author?: string;
author?: string | string[];
/** Limit the commits output to ones with committer header lines that match the specified pattern. */
committer?: string;
committer?: string | string[];
/** Show commits more recent than a specific date. */
since?: string;
/** Show commits more recent than a specific date. */
Expand Down Expand Up @@ -124,9 +124,21 @@ function addOptional<Field extends string = DefaultField>(
"committer",
] as const;

const addOption = (option: string) => (value: string) => {
commandWithOptions += ` --${option}="${value}"`;
};

for (let i = cmdOptional.length; i--; ) {
if (options[cmdOptional[i]]) {
commandWithOptions += ` --${cmdOptional[i]}="${options[cmdOptional[i]]}"`;
const commandOption = cmdOptional[i];
const flagOptions = options[commandOption];

if (flagOptions) {
if (Array.isArray(flagOptions)) {
const addMultiOption = addOption(commandOption);
flagOptions.forEach(addMultiOption);
} else {
addOption(commandOption)(flagOptions);
}
}
}

Expand Down
70 changes: 62 additions & 8 deletions test/gitlog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,51 @@ describe("gitlog", () => {
});
});

it("returns commits only by multiple authors", (done) => {
const command =
`cd ${testRepoLocation}` +
"&& touch new-file" +
"&& git add new-file" +
'&& git commit -m "New commit" --author="A U Thor <[email protected]>"' +
"&& touch loki-file" +
"&& git add loki-file" +
'&& git commit -m "loki commit" --author="A U Loki <[email protected]>"';
const authors = ["A U Thor", "A U Loki"];

// Adding a new commit by different author
exec(command, () => {
const commits = gitlog({
repo: testRepoLocation,
author: authors,
fields: ["authorName"],
});

expect.assertions(2);
commits.forEach((commit) => {
expect(authors.includes(commit.authorName)).toBe(true);
});

done();
});
});

it("returns commits only by committer", (done) => {
const defaults = ["committerName"] as const;
const command =
`cd ${testRepoLocation} ` +
`&& touch new-file ` +
`&& git add new-file ` +
`&& git commit -m "New commit" ` +
`--committer="A U Thor <[email protected]>"`;
const committer = "Your Name";
"&& touch new-file " +
"&& git add new-file " +
'&& git -c "user.name=A U Thor" -c "[email protected]" commit -m "New commit"';
const committer = "A U Thor";

// Adding a new commit by different author
exec(command, () => {
const commits = gitlog({
repo: testRepoLocation,
committer,
fields: defaults,
fields: ["committerName"],
});

expect.assertions(10);
expect.assertions(1);
commits.forEach((commit) => {
expect(commit.committerName).toBe(committer);
});
Expand All @@ -237,6 +263,34 @@ describe("gitlog", () => {
});
});

it("returns commits only by multiple committers", (done) => {
const command =
`cd ${testRepoLocation} ` +
"&& touch new-file " +
"&& git add new-file " +
'&& git -c "user.name=A U Thor" -c "[email protected]" commit -m "New commit"' +
"&& touch loki-file " +
"&& git add loki-file " +
'&& git -c "user.name=A U Loki" -c "[email protected]" commit -m "loki commit"';
const committer = ["A U Thor", "A U Loki"];

// Adding a new commit by different author
exec(command, () => {
const commits = gitlog({
repo: testRepoLocation,
committer,
fields: ["committerName", "subject"],
});

expect.assertions(2);
commits.forEach((commit) => {
expect(committer.includes(commit.committerName)).toBe(true);
});

done();
});
});

it("returns A status for files that are added", () => {
const commits = gitlog({ repo: testRepoLocation });
expect(commits[1].status[0]).toBe("A");
Expand Down