From 4864d96dbce59546b84014aae830bf5d47b92323 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Mon, 27 Apr 2020 09:50:55 -0700 Subject: [PATCH 1/2] 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..5ce2add 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 commandOption = cmdOptional[i]; + const flagOptions = options[commandOption]; + + if (flagOptions) { + 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"); From de78a28d3eccdc72244cdd8f330238f8e3a8c87e Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Mon, 27 Apr 2020 10:28:31 -0700 Subject: [PATCH 2/2] fix lint --- src/index.ts | 11 +++++++---- test/gitlog.test.ts | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5ce2add..30c6c3b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -124,17 +124,20 @@ function addOptional( "committer", ] as const; + const addOption = (option: string) => (value: string) => { + commandWithOptions += ` --${option}="${value}"`; + }; + for (let i = cmdOptional.length; i--; ) { const commandOption = cmdOptional[i]; const flagOptions = options[commandOption]; if (flagOptions) { if (Array.isArray(flagOptions)) { - flagOptions.map(flagOption => { - commandWithOptions += ` --${commandOption}="${flagOption}"`; - }) + const addMultiOption = addOption(commandOption); + flagOptions.forEach(addMultiOption); } else { - commandWithOptions += ` --${commandOption}="${flagOptions}"`; + addOption(commandOption)(flagOptions); } } } diff --git a/test/gitlog.test.ts b/test/gitlog.test.ts index dbb0a84..d819789 100644 --- a/test/gitlog.test.ts +++ b/test/gitlog.test.ts @@ -213,11 +213,11 @@ describe("gitlog", () => { it("returns commits only by multiple authors", (done) => { const command = `cd ${testRepoLocation}` + - '&& touch new-file' + - '&& git add new-file' + + "&& touch new-file" + + "&& git add new-file" + '&& git commit -m "New commit" --author="A U Thor "' + - '&& touch loki-file' + - '&& git add loki-file' + + "&& touch loki-file" + + "&& git add loki-file" + '&& git commit -m "loki commit" --author="A U Loki "'; const authors = ["A U Thor", "A U Loki"]; @@ -241,8 +241,8 @@ describe("gitlog", () => { it("returns commits only by committer", (done) => { const command = `cd ${testRepoLocation} ` + - '&& touch new-file ' + - '&& git add new-file ' + + "&& 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"; @@ -251,7 +251,7 @@ describe("gitlog", () => { const commits = gitlog({ repo: testRepoLocation, committer, - fields: ["committerName"], + fields: ["committerName"], }); expect.assertions(1); @@ -266,11 +266,11 @@ describe("gitlog", () => { it("returns commits only by multiple committers", (done) => { const command = `cd ${testRepoLocation} ` + - '&& touch new-file ' + - '&& git add new-file ' + + "&& 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 ' + + "&& 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"]; @@ -285,7 +285,7 @@ describe("gitlog", () => { expect.assertions(2); commits.forEach((commit) => { expect(committer.includes(commit.committerName)).toBe(true); - }); + }); done(); });