Skip to content

Commit 0028743

Browse files
committed
fix: check and prevent empty branch pushes
1 parent 95d1d90 commit 0028743

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ import { Action, Step } from '../../actions';
22
import { validateUser } from './checkUserPushPermission';
33
import simpleGit from 'simple-git';
44

5+
const isEmptyBranch = async (action: Action) => {
6+
const git = simpleGit(`${action.proxyGitPath}/${action.repoName}`);
7+
8+
if (action.commitFrom === '0'.repeat(40)) {
9+
try {
10+
const type = await git.raw(['cat-file', '-t', action.commitTo || '']);
11+
const known = type.trim() === 'commit';
12+
if (known) {
13+
return true;
14+
}
15+
} catch (err) {
16+
console.log(`Commit ${action.commitTo} not found: ${err}`);
17+
}
18+
}
19+
20+
return false;
21+
};
22+
523
const exec = async (req: any, action: Action): Promise<Action> => {
624
const step = new Step('getMissingData');
725

@@ -11,6 +29,12 @@ const exec = async (req: any, action: Action): Promise<Action> => {
1129
}
1230

1331
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+
}
1438
console.log(`commitData not found, fetching missing commits from git...`);
1539
const path = `${action.proxyGitPath}/${action.repoName}`;
1640
const git = simpleGit(path);

0 commit comments

Comments
 (0)