Skip to content

git-co: guard numeric validation before integer comparison on branch selection#6

Merged
mlutonsky merged 2 commits intomainfrom
copilot/validate-branch-selection-input
Mar 14, 2026
Merged

git-co: guard numeric validation before integer comparison on branch selection#6
mlutonsky merged 2 commits intomainfrom
copilot/validate-branch-selection-input

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 14, 2026

Non-numeric input at the interactive branch-selection prompt (e.g. q, abc) causes Bash to emit [: abc: integer expression expected because $selection is passed directly to [ "$selection" -gt 0 ].

Change

Replace the -n emptiness check with a digit-only regex guard:

# Before
if [ -n "$selection" ] && [ "$selection" -gt 0 ]; then

# After
if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -gt 0 ]; then

The =~ ^[0-9]+$ pattern ensures $selection contains only digits before the integer comparison is evaluated, silently discarding any non-numeric input rather than surfacing a confusing shell error.

Original prompt

git-co: validate branch selection input is numeric

File to modify: git-co

Problem

In the interactive menu (multi-match branch of co()), user input is used directly in an integer comparison:

if [ -n "$selection" ] && [ "$selection" -gt 0 ]; then

If the user types a non-numeric string (e.g. q, abc), Bash will print:

[: abc: integer expression expected

Fix

Add a numeric validation guard before the integer comparison. Replace:

          if [ -n "$selection" ] && [ "$selection" -gt 0 ]; then

with:

          if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -gt 0 ]; then

This ensures $selection is a non-empty string of digits before doing the integer comparison. No other changes.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: @copilot Accepted Confirmation: Are you sure?

Assistant: Since the Copilot coding agent isn't enabled on this repository, I'll create each PR manually by pushing branches and files directly. Let me work through them one by one.

Assistant: Good — latest main SHA is 1da8d9f06dcededbe51882b26df4da9b085dfab2. I'll now create all the branches and files in parallel where possible, then open the PRs. Let me start creating all the branches.

User: How to enable/allo agents so you can create pull requests?

Assistant: Here's what you need to enable the Copilot coding agent so I can create pull requests autonomously:


How to Enable the Copilot Coding Agent

The coding agent is what allows me to write code, create branches, and open PRs on your behalf. It requires a supported Copilot plan and then a one-time settings toggle.

Step 1 — Confirm your Copilot plan

The coding agent is available on:

  • GitHub Copilot Pro
  • GitHub Copilot Pro+
  • GitHub Copilot Business
  • GitHub Copilot Enterprise

If you're on the free tier, you'll need to upgrade. You can check or upgrade at github.com/features/copilot/plans.


Step 2 — Enable the agent (personal account)

Since mlutonsky/utils is a personal (user-owned) repository, the coding agent should be available to you automatically once you have a qualifying plan. It is enabled by default for all repositories unless explicitly blocked.

To verify or toggle it:

  1. Go to github.com/settings/copilot
  2. Look for the "Coding agent" section
  3. Make sure it is set to Enabled

Step 3 — Enable GitHub Actions (required for the agent to run)

The coding agent uses GitHub Actions runners to do its work. Make sure Actions are enabled for the repository:

  1. Go to https://github.com/mlutonsky/utils/settings/actions
  2. Under "Actions permissions", select "Allow all actions and reusable workflows" (or at minimum allow GitHub Actions to run)

After enabling

Once those are in place, come back here and ask me to create the pull requests again — I'll be able to autonomously create branches, make all the code changes, and open a PR for each improvement for you to review and merge. See the full docs at Asking GitHub Copilot to create a pull request.

User: I have allowed it, so now please re-create these pull requests automatically

Assistant: All 10 pull requests have been submitted to mlutonsky/utils. Here's a summary of what was created:

# PR Title What it does
1 Add MIT license Adds a LICENSE file so users know they can freely use and adapt the scripts
2 Add ShellCheck CI workflow Adds .github/workflows/shellcheck.yml to automatically lint all shell scripts on every push and PR
3 git-branch-clean: safer branch deletion and fetch error handling Switches git branch -Dgit branch -d and adds an exit guard on git fetch --prune
4 git-co: fix expensive per-branch python3 Levenshtein loop Batches all branch names into a single python3 call instead of spawning one process per branch
5 git-co: remove unnecessary git pull --all Removes the redundant git pull --all after git switch -c, which already sets up tracking
6 git-co: validate branch selection input is numeric Adds [[ "$selection" =~ ^[0-9]+$ ]] guard to prevent a cryptic Bash error on non-numeric input
7 idle-power-manager: add log rotation Adds a rotate_log function and MAX_LOG_LINES env var to cap the log file at 10,000 lines
8 idle-power-manager: fix fragile switch_profile success detection Checks the gdbus call exit code directly instead of grepping stdout for the string "true"
9 idle-power-manager: add DBUS_SESSION_BUS_ADDRESS fallback Exports a fallback socket path so the script works reliably as a systemd user service
10 Add install.sh Adds a one-shot install ...

This pull request was created from Copilot chat.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: mlutonsky <423848+mlutonsky@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix branch selection input validation to be numeric git-co: guard numeric validation before integer comparison on branch selection Mar 14, 2026
Copilot AI requested a review from mlutonsky March 14, 2026 18:22
@mlutonsky mlutonsky marked this pull request as ready for review March 14, 2026 18:29
Copilot AI review requested due to automatic review settings March 14, 2026 18:29
@mlutonsky mlutonsky merged commit cc4863b into main Mar 14, 2026
3 checks passed
@mlutonsky mlutonsky deleted the copilot/validate-branch-selection-input branch March 14, 2026 18:30
Copy link
Copy Markdown

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

Updates git-co’s interactive branch-selection prompt to avoid Bash “integer expression expected” errors by validating that user input is numeric before performing an integer comparison.

Changes:

  • Add a digit-only regex guard ([[ ... =~ ^[0-9]+$ ]]) before [ "$selection" -gt 0 ] in the branch selection menu.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

3 participants