Skip to content

Commit 272e1d1

Browse files
authored
Merge pull request #1993 from gitgitgadget/explicit-config
Always pass the `config` as an explicit constructor parameter. This is necessary so that the project config is reliably passed through, and the `defaultConfig` is not used by mistake. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2 parents 7cf23ab + 39c86ed commit 272e1d1

10 files changed

+119
-31
lines changed

lib/ci-helper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class CIHelper {
5959
this.workDir = workDir;
6060
this.notes = new GitNotes(workDir);
6161
this.gggNotesUpdated = !!skipUpdate;
62-
this.mail2commit = new MailCommitMapping(this.notes.workDir);
62+
this.mail2commit = new MailCommitMapping(this.config, this.notes.workDir);
6363
this.mail2CommitMapUpdated = !!skipUpdate;
6464
this.github = new GitHubGlue(workDir, this.config.repo.owner, this.config.repo.name);
6565
this.testing = false;
@@ -853,6 +853,7 @@ export class CIHelper {
853853

854854
try {
855855
const gitGitGadget = await GitGitGadget.get(
856+
this.config,
856857
this.gggConfigDir,
857858
this.workDir,
858859
this.urlRepo,
@@ -1070,6 +1071,7 @@ export class CIHelper {
10701071
};
10711072

10721073
const gitGitGadget = await GitGitGadget.get(
1074+
this.config,
10731075
this.gggConfigDir,
10741076
this.workDir,
10751077
this.urlRepo,
@@ -1107,6 +1109,7 @@ export class CIHelper {
11071109
};
11081110
await this.maybeUpdateGGGNotes();
11091111
const mailArchiveGit = await MailArchiveGitHelper.get(
1112+
this.config,
11101113
this.notes,
11111114
mailArchiveGitDir,
11121115
this.github,

lib/gitgitgadget.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IGitHubUser, IPullRequestInfo } from "./github-glue.js";
55
import { PatchSeries, SendFunction } from "./patch-series.js";
66
import { IPatchSeriesMetadata } from "./patch-series-metadata.js";
77
import { PatchSeriesOptions } from "./patch-series-options.js";
8-
import { IConfig, getConfig } from "./project-config.js";
8+
import { IConfig } from "./project-config.js";
99
import { ISMTPOptions, parseHeadersAndSendMail, parseMBox, sendMail } from "./send-mail.js";
1010

1111
export interface IGitGitGadgetOptions {
@@ -37,6 +37,7 @@ export class GitGitGadget {
3737
}
3838

3939
public static async get(
40+
config: IConfig,
4041
gitGitGadgetDir: string,
4142
workDir?: string,
4243
publishTagsAndNotesToRemote?: string,
@@ -90,7 +91,15 @@ export class GitGitGadget {
9091

9192
const [options, allowedUsers] = await GitGitGadget.readOptions(notes);
9293

93-
return new GitGitGadget(notes, options, allowedUsers, smtpOptions, publishTagsAndNotesToRemote, notesPushToken);
94+
return new GitGitGadget(
95+
config,
96+
notes,
97+
options,
98+
allowedUsers,
99+
smtpOptions,
100+
publishTagsAndNotesToRemote,
101+
notesPushToken,
102+
);
94103
}
95104

96105
protected static async readOptions(notes: GitNotes): Promise<[IGitGitGadgetOptions, Set<string>]> {
@@ -103,7 +112,7 @@ export class GitGitGadget {
103112
return [options, allowedUsers];
104113
}
105114

106-
public readonly config: IConfig = getConfig();
115+
public readonly config: IConfig;
107116
public readonly workDir: string;
108117
public readonly notes: GitNotes;
109118
protected options: IGitGitGadgetOptions;
@@ -115,6 +124,7 @@ export class GitGitGadget {
115124
private readonly publishToken: string | undefined;
116125

117126
protected constructor(
127+
config: IConfig,
118128
notes: GitNotes,
119129
options: IGitGitGadgetOptions,
120130
allowedUsers: Set<string>,
@@ -125,6 +135,7 @@ export class GitGitGadget {
125135
if (!notes.workDir) {
126136
throw new Error("Could not determine Git worktree");
127137
}
138+
this.config = config;
128139
this.workDir = notes.workDir;
129140
this.notes = notes;
130141
this.options = options;
@@ -291,6 +302,7 @@ export class GitGitGadget {
291302
options.rfc = pr.draft ?? false;
292303

293304
const series = await PatchSeries.getFromNotes(
305+
this.config,
294306
this.notes,
295307
pr.pullRequestURL,
296308
pr.title,

lib/mail-archive-helper.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IGitGitGadgetOptions } from "./gitgitgadget.js";
55
import { GitHubGlue } from "./github-glue.js";
66
import { IMailMetadata } from "./mail-metadata.js";
77
import { IPatchSeriesMetadata } from "./patch-series-metadata.js";
8-
import { IConfig, getConfig } from "./project-config.js";
8+
import { IConfig } from "./project-config.js";
99
import { getPullRequestKey } from "./pullRequestKey.js";
1010
import { IParsedMBox, parseMBox, parseMBoxMessageIDAndReferences } from "./send-mail.js";
1111
import { SousChef } from "./sous-chef.js";
@@ -19,13 +19,14 @@ export interface IGitMailingListMirrorState {
1919

2020
export class MailArchiveGitHelper {
2121
public static async get(
22+
config: IConfig,
2223
gggNotes: GitNotes,
2324
mailArchiveGitDir: string,
2425
githubGlue: GitHubGlue,
2526
branch: string,
2627
): Promise<MailArchiveGitHelper> {
2728
const state: IGitMailingListMirrorState = (await gggNotes.get<IGitMailingListMirrorState>(stateKey)) || {};
28-
return new MailArchiveGitHelper(gggNotes, mailArchiveGitDir, githubGlue, state, branch);
29+
return new MailArchiveGitHelper(config, gggNotes, mailArchiveGitDir, githubGlue, state, branch);
2930
}
3031

3132
/**
@@ -56,19 +57,21 @@ export class MailArchiveGitHelper {
5657
}
5758

5859
protected readonly branch: string;
59-
protected readonly config: IConfig = getConfig();
60+
protected readonly config: IConfig;
6061
protected readonly state: IGitMailingListMirrorState;
6162
protected readonly gggNotes: GitNotes;
6263
protected readonly mailArchiveGitDir: string;
6364
protected readonly githubGlue: GitHubGlue;
6465

6566
protected constructor(
67+
config: IConfig,
6668
gggNotes: GitNotes,
6769
mailArchiveGitDir: string,
6870
githubGlue: GitHubGlue,
6971
state: IGitMailingListMirrorState,
7072
branch: string,
7173
) {
74+
this.config = config;
7275
this.branch = branch;
7376
this.gggNotes = gggNotes;
7477
this.mailArchiveGitDir = mailArchiveGitDir;

lib/mail-commit-mapping.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { git } from "./git.js";
22
import { GitNotes } from "./git-notes.js";
3-
import { IConfig, getConfig } from "./project-config.js";
3+
import { IConfig } from "./project-config.js";
44

55
export class MailCommitMapping {
6-
public readonly config: IConfig = getConfig();
6+
public readonly config: IConfig;
77
public readonly workDir?: string;
88
public readonly mail2CommitNotes: GitNotes;
99

10-
public constructor(workDir?: string) {
10+
public constructor(config: IConfig, workDir?: string) {
11+
this.config = config;
1112
this.workDir = workDir;
1213
this.mail2CommitNotes = new GitNotes(workDir, "refs/notes/mail-to-commit");
1314
}

lib/patch-series.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { IMailMetadata } from "./mail-metadata.js";
88
import { md2text } from "./markdown-renderer.js";
99
import { IPatchSeriesMetadata } from "./patch-series-metadata.js";
1010
import { PatchSeriesOptions } from "./patch-series-options.js";
11-
import { IConfig, getConfig } from "./project-config.js";
11+
import { IConfig } from "./project-config.js";
1212
import { ProjectOptions } from "./project-options.js";
1313
import { getPullRequestKeyFromURL } from "./pullRequestKey.js";
1414

@@ -57,6 +57,7 @@ interface IRangeDiff {
5757

5858
export class PatchSeries {
5959
public static async getFromNotes(
60+
config: IConfig,
6061
notes: GitNotes,
6162
pullRequestURL: string,
6263
pullRequestTitle: string,
@@ -139,12 +140,22 @@ export class PatchSeries {
139140

140141
const publishToRemote: string | undefined = undefined;
141142

142-
const project = await ProjectOptions.get(workDir, headCommit, cc, basedOn, publishToRemote, baseCommit);
143+
const project = await ProjectOptions.get(config, workDir, headCommit, cc, basedOn, publishToRemote, baseCommit);
143144
if (rangeDiff) {
144145
options.rangeDiff = rangeDiff;
145146
}
146147

147-
return new PatchSeries(notes, options, project, metadata, rangeDiffRanges, patchCount, coverLetter, senderName);
148+
return new PatchSeries(
149+
config,
150+
notes,
151+
options,
152+
project,
153+
metadata,
154+
rangeDiffRanges,
155+
patchCount,
156+
coverLetter,
157+
senderName,
158+
);
148159
}
149160

150161
protected static async parsePullRequest(
@@ -513,7 +524,7 @@ export class PatchSeries {
513524
return results;
514525
}
515526

516-
public readonly config: IConfig = getConfig();
527+
public readonly config: IConfig;
517528
public readonly notes: GitNotes;
518529
public readonly options: PatchSeriesOptions;
519530
public readonly project: ProjectOptions;
@@ -524,6 +535,7 @@ export class PatchSeries {
524535
public readonly patchCount: number;
525536

526537
protected constructor(
538+
config: IConfig,
527539
notes: GitNotes,
528540
options: PatchSeriesOptions,
529541
project: ProjectOptions,
@@ -533,6 +545,7 @@ export class PatchSeries {
533545
coverLetter?: string,
534546
senderName?: string,
535547
) {
548+
this.config = config;
536549
this.notes = notes;
537550
this.options = options;
538551
this.project = project;

lib/project-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { commitExists, git, revParse } from "./git.js";
2-
import { IConfig, getConfig, projectInfo } from "./project-config.js";
2+
import { IConfig, projectInfo } from "./project-config.js";
33

44
// For now, only the Git, Cygwin and BusyBox projects are supported
55
export class ProjectOptions {
66
public static async get(
7+
config: IConfig,
78
workDir: string,
89
branchName: string,
910
cc: string[],
1011
basedOn?: string,
1112
publishToRemote?: string,
1213
baseCommit?: string,
1314
): Promise<ProjectOptions> {
14-
const config: IConfig = getConfig();
1515
let upstreamBranch: string;
1616
let to: string;
1717
let midUrlPrefix = " Message-ID: ";

tests/gitgitgadget.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getConfig } from "../lib/gitgitgadget-config.js";
77
import { PatchSeries } from "../lib/patch-series.js";
88
import { IPatchSeriesMetadata } from "../lib/patch-series-metadata.js";
99
import { testCreateRepo } from "./test-lib.js";
10+
import defaultConfig from "../lib/gitgitgadget-config.js";
1011

1112
// This test script might take quite a while to run
1213
jest.setTimeout(60000);
@@ -209,6 +210,7 @@ test("generate tag/notes from a Pull Request", async () => {
209210
await git(["config", "user.email", "gitgitgadget@example.com"], repo.options);
210211

211212
const patches = await PatchSeries.getFromNotes(
213+
defaultConfig,
212214
notes,
213215
pullRequestURL,
214216
pullRequestTitle,
@@ -251,6 +253,7 @@ to have included in git.git [https://github.com/git/git].`);
251253

252254
const headCommit2 = await repo.revParse("HEAD");
253255
const patches2 = await PatchSeries.getFromNotes(
256+
defaultConfig,
254257
notes,
255258
pullRequestURL,
256259
pullRequestTitle,
@@ -342,7 +345,7 @@ test("allow/disallow", async () => {
342345
const notes = new GitNotes(remote.workDir);
343346
await notes.set("", {} as IGitGitGadgetOptions);
344347

345-
const gitGitGadget = await GitGitGadget.get(workDir);
348+
const gitGitGadget = await GitGitGadget.get(defaultConfig, workDir);
346349

347350
// pretend that the notes ref had been changed in the meantime
348351
await notes.set("", { allowedUsers: ["first-one"] } as IGitGitGadgetOptions, true);
@@ -370,7 +373,7 @@ test("allow/disallow with env vars", async () => {
370373
const notes = new GitNotes(remote.workDir);
371374
await notes.set("", {} as IGitGitGadgetOptions);
372375

373-
const gitGitGadget = await GitGitGadget.get(workDir);
376+
const gitGitGadget = await GitGitGadget.get(defaultConfig, workDir);
374377

375378
// pretend that the notes ref had been changed in the meantime
376379
await notes.set("", { allowedUsers: ["first-one"] } as IGitGitGadgetOptions, true);

0 commit comments

Comments
 (0)