From dadb3dad99d32b07ed57085754103c6745932f99 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Mon, 27 Apr 2020 09:50:55 -0700 Subject: [PATCH] filter author/committer with an array of string --- README.md | 1 + src/index.ts | 17 ++++++++--- test/gitlog.test.ts | 70 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9e1040a..f31939f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.ts b/src/index.ts index 6345e62..00c2add 100644 --- a/src/index.ts +++ b/src/index.ts @@ -85,9 +85,9 @@ export interface GitlogOptions { /** 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. */ @@ -125,8 +125,17 @@ function addOptional( ] 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}"`; + } } } diff --git a/test/gitlog.test.ts b/test/gitlog.test.ts index d78156c..dbb0a84 100644 --- a/test/gitlog.test.ts +++ b/test/gitlog.test.ts @@ -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 "' + + '&& touch loki-file' + + '&& git add loki-file' + + '&& git commit -m "loki commit" --author="A U Loki "'; + 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 "`; - const committer = "Your Name"; + '&& touch new-file ' + + '&& git add new-file ' + + '&& git -c "user.name=A U Thor" -c "user.email=author@example.com" 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); }); @@ -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 "user.email=author@example.com" commit -m "New commit"' + + '&& touch loki-file ' + + '&& git add loki-file ' + + '&& git -c "user.name=A U Loki" -c "user.email=loki@example.com" 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");