-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
git.ts
191 lines (180 loc) · 4.41 KB
/
git.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as gitRawCommits from 'git-raw-commits';
import { EMPTY, Observable, throwError } from 'rxjs';
import { catchError, last, map, scan, startWith } from 'rxjs/operators';
import { exec } from '../../common/exec';
import { logStep, _logStep } from './logger';
/**
* Return the list of commit bodies since `since` commit.
*/
export function getCommits({
projectRoot,
since,
}: {
projectRoot: string;
since?: string;
}): Observable<string[]> {
return getFormattedCommits({
since,
projectRoot,
ignoreMergeCommits: true,
format: '%B',
});
}
/**
* Return hash of last commit of a project
*/
export function getLastCommitHash({
projectRoot,
}: {
projectRoot: string;
}): Observable<string> {
return getFormattedCommits({
projectRoot,
ignoreMergeCommits: false,
format: '%H',
}).pipe(map(([commit]) => commit.trim()));
}
function getFormattedCommits({
projectRoot,
format,
ignoreMergeCommits,
since = '',
}: {
projectRoot: string;
format: string;
ignoreMergeCommits: boolean;
since?: string;
}): Observable<string[]> {
return new Observable<string>((observer) => {
const params: any = {
from: since,
format,
path: projectRoot,
'full-history': true,
};
if (ignoreMergeCommits) {
params['no-merges'] = ignoreMergeCommits;
}
gitRawCommits(params)
.on('data', (data: string) => observer.next(data))
.on('error', (error: Error) => observer.error(error))
.on('close', () => observer.complete())
.on('finish', () => observer.complete());
}).pipe(
scan((commits, commit) => [...commits, commit.toString()], [] as string[]),
startWith([]),
last(),
);
}
export function tryPush({
remote,
branch,
noVerify,
projectName,
tag,
}: {
tag: string;
remote: string;
branch: string;
noVerify: boolean;
projectName: string;
}): Observable<string> {
if (remote == null || branch == null) {
return throwError(
() =>
new Error(
'Missing option --remote or --branch, see: https://github.com/jscutlery/semver#configure.',
),
);
}
const gitPushOptions = [...(noVerify ? ['--no-verify'] : [])];
return exec('git', [
'push',
...gitPushOptions,
'--atomic',
remote,
branch,
tag,
])
.pipe(
catchError((error) => {
if (/atomic/.test(error)) {
_logStep({
step: 'warning',
level: 'warn',
message: 'Git push --atomic failed, attempting non-atomic push.',
projectName,
});
return exec('git', ['push', ...gitPushOptions, remote, branch, tag]);
}
return throwError(() => error);
}),
)
.pipe(
logStep({
step: 'push_success',
message: `Pushed to "${remote}" "${branch}".`,
projectName,
}),
);
}
export function addToStage({
paths,
dryRun,
}: {
paths: string[];
dryRun: boolean;
}): Observable<void> {
if (paths.length === 0) {
return EMPTY;
}
const gitAddOptions = [...(dryRun ? ['--dry-run'] : []), ...paths];
return exec('git', ['add', ...gitAddOptions]).pipe(map(() => undefined));
}
export function getFirstCommitRef(): Observable<string> {
return exec('git', ['rev-list', '--max-parents=0', 'HEAD']).pipe(
map((output) => {
return output
.split('\n')
.map((c) => c.trim())
.filter(Boolean)
.pop()!;
}),
);
}
export function createTag({
dryRun,
tag,
commitHash,
commitMessage,
projectName,
}: {
dryRun: boolean;
tag: string;
commitHash: string;
commitMessage: string;
projectName: string;
}): Observable<string> {
if (dryRun) {
return EMPTY;
}
return exec('git', ['tag', '-a', tag, commitHash, '-m', commitMessage]).pipe(
catchError((error) => {
if (/already exists/.test(error)) {
return throwError(
() =>
new Error(`Failed to tag "${tag}", this tag already exists.
This error occurs because the same version was previously created but the tag does not point to a commit referenced in your base branch.
Please delete the tag by running "git tag -d ${tag}", make sure the tag has been removed from the remote repository as well and run this command again.`),
);
}
return throwError(() => error);
}),
map(() => tag),
logStep({
step: 'tag_success',
message: `Tagged "${tag}".`,
projectName,
}),
);
}