Skip to content

Commit

Permalink
filter author/committer with an array of string
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Apr 27, 2020
1 parent 7cbef29 commit dadb3da
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
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
17 changes: 13 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 @@ -125,8 +125,17 @@ function addOptional<Field extends string = DefaultField>(
] as const;

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

if (flagOptions && commandOption) {
if (Array.isArray(flagOptions)) {
flagOptions.map(flagOption => {
commandWithOptions += ` --${commandOption}="${flagOption}"`;
})
} else {
commandWithOptions += ` --${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

0 comments on commit dadb3da

Please sign in to comment.