Skip to content

Conversation

@donhardman
Copy link
Contributor

  • Prevent commits, discards, undo, and redo on default branch
  • Return 403 error to enforce branching workflow

- Prevent commits, discards, undo, and redo on default branch
- Return 403 error to enforce branching workflow
@sanikolaev sanikolaev requested a review from Copilot October 24, 2025 04:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements protection for the default branch by preventing direct modifications. It adds a new isOnDefaultBranch helper function and enforces a branching workflow by blocking commits, file operations (save, move, delete), directory creation, and git operations (discard, undo, redo) when users are on the default branch (main/master).

Key Changes:

  • Added isOnDefaultBranch helper function to check current branch status
  • Implemented 403 error responses across multiple routes when operations are attempted on default branch
  • Enhanced error messages to guide users toward creating new branches

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
ui/helpers.js Adds new isOnDefaultBranch function that checks if the current branch matches the default branch
ui/routes.js Adds default branch checks to file operations (save, move, delete) and directory creation endpoints
ui/gitRoutes.js Adds default branch checks to git operations (commit, discard, undo, redo) endpoints

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

return { isDefault: false, currentBranch: null, defaultBranch: null };
}

const currentBranch = await git.revparse(['--abbrev-ref', 'HEAD']);
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The git.revparse() call returns a string with a trailing newline character. This needs to be trimmed before comparison with defaultBranch to avoid false negatives when checking currentBranch === defaultBranch.

Suggested change
const currentBranch = await git.revparse(['--abbrev-ref', 'HEAD']);
const currentBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim();

Copilot uses AI. Check for mistakes.
Comment on lines +495 to +506
// Check if on default branch - block file modifications
const userRepoPath = getUserRepoPath(req, WORKDIR, ROOT_DIR, getAuthConfig);
const branchCheck = await isOnDefaultBranch(userRepoPath);

if (branchCheck.isDefault) {
return res.status(403).json({
error: `Cannot save files on default branch (${branchCheck.defaultBranch}). Please create a new branch before editing.`,
currentBranch: branchCheck.currentBranch,
defaultBranch: branchCheck.defaultBranch,
isDefaultBranch: true
});
}
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch check logic is duplicated across multiple endpoints (lines 495-506, 623-634, 669-680, 736-747 in routes.js and lines 628-637, 808-817, 1119-1128, 1261-1270 in gitRoutes.js). Consider extracting this into a reusable middleware function to reduce code duplication and improve maintainability.

Copilot uses AI. Check for mistakes.
@sanikolaev sanikolaev self-requested a review October 24, 2025 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants