-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathgetMissingData.ts
More file actions
76 lines (64 loc) · 2.4 KB
/
getMissingData.ts
File metadata and controls
76 lines (64 loc) · 2.4 KB
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
import { Action, Step } from '../../actions';
import { validateUser } from './checkUserPushPermission';
import simpleGit from 'simple-git';
import { EMPTY_COMMIT_HASH } from '../constants';
const isEmptyBranch = async (action: Action) => {
const git = simpleGit(`${action.proxyGitPath}/${action.repoName}`);
if (action.commitFrom === EMPTY_COMMIT_HASH) {
try {
const type = await git.raw(['cat-file', '-t', action.commitTo || '']);
const known = type.trim() === 'commit';
if (known) {
return true;
}
} catch (err) {
console.log(`Commit ${action.commitTo} not found: ${err}`);
}
}
return false;
};
const exec = async (req: any, action: Action): Promise<Action> => {
const step = new Step('getMissingData');
if (action.commitData && action.commitData.length > 0) {
console.log('getMissingData', action);
return action;
}
if (await isEmptyBranch(action)) {
step.setError('Push blocked: Empty branch. Please make a commit before pushing a new branch.');
action.addStep(step);
step.error = true;
return action;
}
console.log(`commitData not found, fetching missing commits from git...`);
try {
const path = `${action.proxyGitPath}/${action.repoName}`;
const git = simpleGit(path);
const log = await git.log({ from: action.commitFrom, to: action.commitTo });
action.commitData = [...log.all].reverse().map((entry, i, array) => {
const parent = i === 0 ? action.commitFrom : array[i - 1].hash;
const timestamp = Math.floor(new Date(entry.date).getTime() / 1000).toString();
return {
message: entry.message || '',
committer: entry.author_name || '',
tree: entry.hash || '',
parent: parent || EMPTY_COMMIT_HASH,
author: entry.author_name || '',
authorEmail: entry.author_email || '',
commitTimestamp: timestamp,
}
});
console.log(`Updated commitData:`, { commitData: action.commitData });
if (action.commitFrom === EMPTY_COMMIT_HASH) {
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
}
const user = action.commitData[action.commitData.length - 1].committer;
action.user = user;
} catch (e: any) {
step.setError(e.toString('utf-8'));
} finally {
action.addStep(step);
}
return await validateUser(action.user || '', action, step);
};
exec.displayName = 'getMissingData.exec';
export { exec };