Skip to content

Commit 6cfc66a

Browse files
committed
Merge remote-tracking branch 'origin/bugfix-new-branch-from-unapproved-commit' into bugfix-prevent-empty-branch-push
2 parents 0028743 + 3ecd5fb commit 6cfc66a

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

src/proxy/processors/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const BRANCH_PREFIX = 'refs/heads/';
2+
export const EMPTY_COMMIT_HASH = '0000000000000000000000000000000000000000';
3+
export const FLUSH_PACKET = '0000';
4+
export const PACK_SIGNATURE = 'PACK';
5+
export const PACKET_SIZE = 4;

src/proxy/processors/push-action/checkUserPushPermission.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const exec = async (req: any, action: Action): Promise<Action> => {
77
const user = action.user;
88

99
if (!user) {
10-
step.log('Action has no user set. This may be due to a fast-forward ref update. Deferring to getMissingData action.');
11-
action.addStep(step);
10+
console.log('Action has no user set. This may be due to a fast-forward ref update. Deferring to getMissingData action.');
1211
return action;
1312
}
1413

src/proxy/processors/push-action/getDiff.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Action, Step } from '../../actions';
22
import simpleGit from 'simple-git';
33

4+
import { EMPTY_COMMIT_HASH } from '../constants';
5+
46
const exec = async (req: any, action: Action): Promise<Action> => {
57
const step = new Step('diff');
68

@@ -18,8 +20,8 @@ const exec = async (req: any, action: Action): Promise<Action> => {
1820
return action;
1921
}
2022

21-
if (action.commitFrom === '0000000000000000000000000000000000000000') {
22-
if (action.commitData[0].parent !== '0000000000000000000000000000000000000000') {
23+
if (action.commitFrom === EMPTY_COMMIT_HASH) {
24+
if (action.commitData[0].parent !== EMPTY_COMMIT_HASH) {
2325
commitFrom = `${action.commitData[action.commitData.length - 1].parent}`;
2426
}
2527
} else {

src/proxy/processors/push-action/getMissingData.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Action, Step } from '../../actions';
22
import { validateUser } from './checkUserPushPermission';
33
import simpleGit from 'simple-git';
4+
import { EMPTY_COMMIT_HASH } from '../constants';
45

56
const isEmptyBranch = async (action: Action) => {
67
const git = simpleGit(`${action.proxyGitPath}/${action.repoName}`);
78

8-
if (action.commitFrom === '0'.repeat(40)) {
9+
if (action.commitFrom === EMPTY_COMMIT_HASH) {
910
try {
1011
const type = await git.raw(['cat-file', '-t', action.commitTo || '']);
1112
const known = type.trim() === 'commit';
@@ -23,46 +24,47 @@ const isEmptyBranch = async (action: Action) => {
2324
const exec = async (req: any, action: Action): Promise<Action> => {
2425
const step = new Step('getMissingData');
2526

26-
try {
27-
if (!action.commitData) {
28-
action.commitData = [];
29-
}
27+
if (action.commitData && action.commitData.length > 0) {
28+
console.log('getMissingData', action);
29+
return action;
30+
}
3031

31-
if (action.commitData.length === 0) {
32-
if (await isEmptyBranch(action)) {
33-
step.setError('Push blocked: Empty branch. Please make a commit before pushing a new branch.');
34-
action.addStep(step);
35-
step.error = true;
36-
return action;
37-
}
38-
console.log(`commitData not found, fetching missing commits from git...`);
39-
const path = `${action.proxyGitPath}/${action.repoName}`;
40-
const git = simpleGit(path);
41-
const log = await git.log({ from: action.commitFrom, to: action.commitTo });
32+
if (await isEmptyBranch(action)) {
33+
step.setError('Push blocked: Empty branch. Please make a commit before pushing a new branch.');
34+
action.addStep(step);
35+
step.error = true;
36+
return action;
37+
}
38+
console.log(`commitData not found, fetching missing commits from git...`);
4239

43-
action.commitData = log.all.toReversed().map((entry, i, array) => {
44-
const parent = i === 0 ? action.commitFrom : array[i - 1].hash;
45-
const timestamp = Math.floor(new Date(entry.date).getTime() / 1000).toString();
46-
return {
47-
message: entry.message || '',
48-
committer: entry.author_name || '',
49-
tree: entry.hash || '',
50-
parent: parent || '0'.repeat(40),
51-
author: entry.author_name || '',
52-
authorEmail: entry.author_email || '',
53-
commitTimestamp: timestamp,
54-
}
55-
});
56-
console.log(`Updated commitData:`, { commitData: action.commitData });
40+
try {
41+
const path = `${action.proxyGitPath}/${action.repoName}`;
42+
const git = simpleGit(path);
43+
const log = await git.log({ from: action.commitFrom, to: action.commitTo });
5744

58-
if (action.commitFrom === '0000000000000000000000000000000000000000') {
59-
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
45+
action.commitData = log.all.toReversed().map((entry, i, array) => {
46+
const parent = i === 0 ? action.commitFrom : array[i - 1].hash;
47+
const timestamp = Math.floor(new Date(entry.date).getTime() / 1000).toString();
48+
return {
49+
message: entry.message || '',
50+
committer: entry.author_name || '',
51+
tree: entry.hash || '',
52+
parent: parent || EMPTY_COMMIT_HASH,
53+
author: entry.author_name || '',
54+
authorEmail: entry.author_email || '',
55+
commitTimestamp: timestamp,
6056
}
61-
const user = action.commitData[action.commitData.length - 1].committer;
62-
action.user = user;
57+
});
58+
console.log(`Updated commitData:`, { commitData: action.commitData });
6359

64-
return await validateUser(user, action, step);
60+
if (action.commitFrom === EMPTY_COMMIT_HASH) {
61+
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
6562
}
63+
64+
const user = action.commitData[action.commitData.length - 1].committer;
65+
action.user = user;
66+
67+
return await validateUser(user, action, step);
6668
} catch (e: any) {
6769
step.setError(e.toString('utf-8'));
6870
} finally {

0 commit comments

Comments
 (0)