Skip to content

Conversation

@fs0414
Copy link

@fs0414 fs0414 commented Dec 7, 2025

Summary

This PR fixes the issue where nb remote set defaults to master branch even when the remote repository's default branch is main.

Fixes #61

Problem

When running nb remote set <url> without specifying a branch name, the command uses the local branch name (typically master from git init) instead of detecting the remote repository's default branch.

Before:

$ nb remote set https://github.com/user/repo.git
Adding remote to: notebook
----------------------------
URL:    https://github.com/user/repo.git
Branch: master    # <- Incorrect: remote's default is 'main'

Solution

Detect the remote's default branch using git ls-remote --symref before falling back to the local branch name.

After:

$ nb remote set https://github.com/user/repo.git
Adding remote to: notebook
----------------------------
URL:    https://github.com/user/repo.git
Branch: main      # <- Correct: auto-detected from remote

Changes

Main fix (nb line 24485-24493)

Added remote default branch detection in _remote() function's set subcommand:

if [[ -z "${_new_branch:-}"     ]]
then
  # Detect remote's default branch, fallback to local branch name
  _new_branch="$(
    git ls-remote --symref "${_new_remote_url}" HEAD 2>/dev/null \
      | awk '/^ref:/ {sub(/refs\/heads\//, "", $2); print $2}'
  )"
  _new_branch="${_new_branch:-${_current_branch}}"
fi

Test updates

Updated test files to explicitly specify branch names in remote add and remote set commands. This is required because the fix now auto-detects the remote's default branch, which may differ from the local branch name used in test setups.

Modified test files:

  • test/init.bats
  • test/notebooks-add.bats
  • test/notebooks-init.bats
  • test/remote-remove.bats
  • test/remote-set.bats
  • test/remote.bats

Notes

  • Uses the same pattern already present in the codebase (line 24310)
  • Falls back to local branch name if remote is unreachable
  • No breaking changes to existing behavior when branch is explicitly specified

  Required after auto-detecting remote's default branch.
@fs0414
Copy link
Author

fs0414 commented Dec 7, 2025

All bats tests in the local environment have passed successfully

% bats test/init.bats test/notebooks-add.bats test/notebooks-init.bats test/remote-remove.bats
  test/remote-set.bats test/remote.bats
init.bats
 ✓ _GIT_ENABLED=0 'init' exits with status 0 and creates notebook directory without .git.
 ✓ 'init <remote-url> <branch>' creates a clone in '$NB_NOTEBOOK_PATH' / '$NB_DIR/home'.
 ✓ 'init <remote-url>' creates a clone in '$NB_NOTEBOOK_PATH' / '$NB_DIR/home'.
 ✓ 'init --author' displays config prompt and sets email and name.
 ✓ 'init --email <email> --name <name>' sets the local email and name.
 ✓ 'init --email <email>' sets the local email.
 ✓ 'init --name <name>' sets the local name.
 ✓ 'init' exits with status 0.
 ✓ 'init' exits with status 1 when '$NB_DIR' exists as a file.
 ✓ 'init' exits with status 1 a prints error output when '$NB_NOTEBOOK_PATH' / '$NB_DIR/home' exists.
 ✓ 'init' creates '$NB_DIR' and '$NB_NOTEBOOK_PATH' / '$NB_DIR/home' directories.
 ✓ 'init' creates a git directory in '$NB_NOTEBOOK_PATH' / '$NB_DIR/home'.
 ✓ 'init' creates an .index '$NB_NOTEBOOK_PATH' / '$NB_DIR/home'.
 ✓ 'init' exits with status 0 when '$NBRC_PATH' exists.
 ✓ 'init' creates a .nbrc file at '$NBRC_PATH'.
 ✓ 'init' creates git commit.
 ✓ 'help init' exits with status 0.
 ✓ 'help init' prints help information.
notebooks-add.bats
 ✓ 'notebooks add' with no <name>, <remote-url>, or <branch> exits with 1 and prints help.
 ✓ 'notebooks add <remote-url> --all' with no existing notebooks matching branch names exits with 0 and adds notebooks for all branches.
 ✓ 'notebooks add <remote-url> <branch-1> <nonexistent-branch-2>' with existing <branch-1> notebook exits with 0 and adds notebook named <branch-1>.
 ✓ 'notebooks add <remote-url> <branch-1> <branch-2>' with no existing notebooks with those names exits with 0 and adds notebook named <branch-1> and <branch-2>.
 ✓ 'notebooks add <remote-url> <branch>' with reserved notebook name as branch name exits with a and prints message.
 ✓ 'notebooks add <remote-url> <branch>' with no existing notebook with that name exits with 0 and adds a notebook named <branch>.
 ✓ 'notebooks add <name> <remote-url> <branch>' exits with 0 and adds a notebook.
 ✓ 'notebooks add <name> <remote-url>' exits with 0 and adds a notebook.
 ✓ 'notebooks add <remote-url>' with multiple remote branches prompts for branch with 'All' response and notebook names with default response, and creates notebooks from branches.
 ✓ 'notebooks add <remote-url>' with multiple remote branches prompts for branch with 'All' response and notebook names with alpha response, and creates notebooks from branches.
 ✓ 'notebooks add <remote-url>' with multiple remote branches prompts for branch with numerical response and notebook name with alpha response, and creates notebook from branch.
 ✓ 'notebooks add <remote-url>' with multiple remote branches prompts for branch with alpha / branch name response and notebook name with <enter> response, and creates notebook from branch.
 ✓ 'notebooks add <remote-url>' with multiple remote branches prompts for branch with numeric response and notebook name with <enter> response, and creates notebook from branch.
 ✓ 'notebooks add <remote-url>' with one remote branch with uncommon default branch name uses repository name as notebook name.
 ✓ 'notebooks add <remote-url>' with one remote branch with common default branch name uses repository name as notebook name.
 ✓ 'notebooks add --author' displays config prompt and sets email and name.
 ✓ 'notebooks add --email <email> --name <name>' sets the local email and name.
 ✓ 'notebooks add --email <email>' sets the local email.
 ✓ 'notebooks add --name <name>' sets the local name.
 ✓ 'notebooks add <reserved>' exits with 1 and prints error message.
 ✓ 'notebooks add <existing>' exits with 1 and prints error message.
 ✓ 'notebooks add <name>' exits with 0 and adds a notebook.
 ✓ 'notebooks add <name>' creates git commit.
 ✓ 'notebooks a <name>' exits with 0 and adds a notebook.
 ✓ 'notebooks create <name>' exits with 0 and adds a notebook.
 ✓ 'notebooks new <name>' exits with 0 and adds a notebook.
notebooks-init.bats
 ✓ 'noteboos init' with uninitialized configuration initializes configuration
 ✓ 'notebooks init <relative path>' with nested git repositories and positive prompt response prints warning and succeeds.
 ✓ 'notebooks init <path> <remote-url> <branch>' exits with 0 and adds a notebook.
 ✓ 'notebooks init <path> <remote-url>' exits with 0 and adds a notebook.
 ✓ 'notebooks init --author' displays config prompt and sets email and name.
 ✓ 'notebooks init --email <email> --name <name>' sets the local email and name.
 ✓ 'notebooks init --email <email>' sets the local email.
 ✓ 'notebooks init --name <name>' sets the local name.
 ✓ 'notebooks init' with no arguments and positive prompt response initializes the current directory.
 ✓ 'notebooks init' with no arguments and negative prompt response exits without initialization.
 ✓ 'notebooks init' in existing notebook exits with 1 and prints error message.
 ✓ 'notebooks init' in existing git repo exits with 1 and prints error message.
 ✓ 'notebooks init <relative path>' with no arguments succeeds.
 ✓ 'notebooks init <relative path>' with existing directory and positive prompt response succeeds.
 ✓ 'notebooks init <relative path>' with existing directory and negative prompt response exits without initialization.
 ✓ 'notebooks init <relative path>' in existing notebook exits with 1.
 ✓ 'notebooks init <relative path>' in existing git repo exits with 1.
 ✓ 'notebooks init <absolute path>' with no arguments succeeds.
 ✓ 'notebooks init <absolute path>' with existing directory and positive prompt response succeeds.
 ✓ 'notebooks init <absolute path>' with existing directory and negative prompt response exits without initialization.
 ✓ 'notebooks init <absolute path>' in existing notebook exits with 1.
 ✓ 'notebooks init <absolute path>' in existing git repo exits with 1.
 ✓ 'notebooks init <top-level-directory>' exits with 1 and prints message.
remote-remove.bats
 ✓ 'unset remote' with no existing remote returns 1 and prints message.
 ✓ 'remote remove --skip-confirmation' with existing remote removes remote without resetting default branch and prints message.
 ✓ 'remote remove --skip-confirmation' with existing remote removes remote, resets default branch, and prints message.
 ✓ 'remote remove' with no existing remote returns 1 and prints message.
 ✓ 'remote remove' with existing remote removes remote without resetting default branch and prints message.
 ✓ 'remote remove' with existing remote removes remote, resets default branch, and prints message.
 ✓ 'remote unset' with existing remote removes remote, resets default branch, and prints message.
 ✓ 'remote remove' with existing remote as orphan removes remote, removes branch and prints message.
 ✓ 'remote remove' with existing remote as orphan removes remote without removing remote branch and prints message.

76 tests, 0 failures

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.

Handling default git branch names

1 participant