-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
coauthors.ts
169 lines (140 loc) · 4.38 KB
/
coauthors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { CoreCommands } from '../../constants';
import { Container } from '../../container';
import { GitContributor, Repository } from '../../git/models';
import { normalizePath } from '../../system/path';
import { ViewsWithRepositoryFolders } from '../../views/viewBase';
import { executeCoreCommand } from '../common';
import {
PartialStepState,
pickContributorsStep,
pickRepositoryStep,
QuickCommand,
StepGenerator,
StepResult,
StepState,
} from '../quickCommand';
interface Context {
repos: Repository[];
activeRepo: Repository | undefined;
associatedView: ViewsWithRepositoryFolders;
title: string;
}
interface State {
repo: string | Repository;
contributors: GitContributor | GitContributor[];
}
export interface CoAuthorsGitCommandArgs {
readonly command: 'co-authors';
state?: Partial<State>;
}
type CoAuthorStepState<T extends State = State> = ExcludeSome<StepState<T>, 'repo', string>;
export class CoAuthorsGitCommand extends QuickCommand<State> {
constructor(container: Container, args?: CoAuthorsGitCommandArgs) {
super(container, 'co-authors', 'co-authors', 'Add Co-Authors', {
description: 'adds co-authors to a commit message',
});
let counter = 0;
if (args?.state?.repo != null) {
counter++;
}
if (
args?.state?.contributors != null &&
(!Array.isArray(args.state.contributors) || args.state.contributors.length !== 0)
) {
counter++;
}
this.initialState = {
counter: counter,
confirm: false,
...args?.state,
};
}
override get canConfirm() {
return false;
}
async execute(state: CoAuthorStepState) {
const repo = await this.container.git.getOrOpenScmRepository(state.repo.path);
if (repo == null) return;
let message = repo.inputBox.value;
const index = message.indexOf('Co-authored-by: ');
if (index !== -1) {
message = message.substring(0, index - 1).trimRight();
}
if (state.contributors != null && !Array.isArray(state.contributors)) {
state.contributors = [state.contributors];
}
for (const c of state.contributors) {
let newlines;
if (message.includes('Co-authored-by: ')) {
newlines = '\n';
} else if (message.length !== 0 && message.endsWith('\n')) {
newlines = '\n\n';
} else {
newlines = '\n\n\n';
}
message += `${newlines}Co-authored-by: ${c.getCoauthor()}`;
}
repo.inputBox.value = message;
void (await executeCoreCommand(CoreCommands.ShowSCM));
}
protected async *steps(state: PartialStepState<State>): StepGenerator {
const context: Context = {
repos: this.container.git.openRepositories,
activeRepo: undefined,
associatedView: this.container.contributorsView,
title: this.title,
};
const scmRepositories = await this.container.git.getOpenScmRepositories();
if (scmRepositories.length) {
// Filter out any repo's that are not known to the built-in git
context.repos = context.repos.filter(repo =>
scmRepositories.find(r => normalizePath(r.rootUri.fsPath) === repo.path),
);
// Ensure that the active repo is known to the built-in git
context.activeRepo = await this.container.git.getOrOpenRepositoryForEditor();
if (
context.activeRepo != null &&
!scmRepositories.some(r => r.rootUri.fsPath === context.activeRepo!.path)
) {
context.activeRepo = undefined;
}
}
let skippedStepOne = false;
while (this.canStepsContinue(state)) {
context.title = this.title;
if (state.counter < 1 || state.repo == null || typeof state.repo === 'string') {
skippedStepOne = false;
if (context.repos.length === 1) {
skippedStepOne = true;
if (state.repo == null) {
state.counter++;
}
state.repo = context.repos[0];
} else {
const result = yield* pickRepositoryStep(state, context);
// Always break on the first step (so we will go back)
if (result === StepResult.Break) break;
state.repo = result;
}
}
if (state.counter < 2 || state.contributors == null) {
const result = yield* pickContributorsStep(
state as CoAuthorStepState,
context,
'Choose contributors to add as co-authors',
);
if (result === StepResult.Break) {
// If we skipped the previous step, make sure we back up past it
if (skippedStepOne) {
state.counter--;
}
continue;
}
state.contributors = result;
}
QuickCommand.endSteps(state);
void this.execute(state as CoAuthorStepState);
}
return state.counter < 0 ? StepResult.Break : undefined;
}
}