Skip to content

Add dynamic user switching based on git config gh.user (POC for #326)#11388

Draft
MSch wants to merge 2 commits intocli:trunkfrom
MSch:msch/dynamic-user-switching
Draft

Add dynamic user switching based on git config gh.user (POC for #326)#11388
MSch wants to merge 2 commits intocli:trunkfrom
MSch:msch/dynamic-user-switching

Conversation

@MSch
Copy link
Copy Markdown

@MSch MSch commented Jul 26, 2025

Summary

Proof of concept implementation for #326. Adds automatic user switching based on repository-specific gh.user git config setting.

Implementation

When any authenticated command runs, the CLI automatically:

  1. Reads gh.user from git config
  2. Switches to that user if different from current active user
  3. Continues with original command using switched user

Usage

As described in #326, configure per-repository users using git config include:

# In ~/.gitconfig
[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal
# In ~/.gitconfig-work
[gh]
    user = work-username

# In ~/.gitconfig-personal  
[gh]
    user = personal-username

Now all gh commands automatically use the appropriate user based on repository location:

cd ~/work/some-repo
gh pr create  # Uses work-username

cd ~/personal/my-project  
gh pr create  # Uses personal-username

Files Changed

  • internal/config/git_user_switch.go - Core switching logic
  • pkg/cmd/factory/default.go - Integration point in HTTP client factory
  • internal/config/git_user_switch_test.go - Test suite (9 scenarios)

Testing

All test scenarios pass:

  • Not in git repo (skip)
  • No gh.user configured (skip)
  • User already active (no-op)
  • Invalid user (error with warning)
  • Successful switch (core functionality)
  • Switch failure (error handling)

Proof of Concept Status

This is a functional POC demonstrating the core concept. Implementation works as described in #326 but may need refinement for production:

  • Uses first authenticated host (github.com)
  • Basic error handling with warnings
  • No configuration validation commands
  • No integration with existing gh auth switch UX

Manual Verification

Tested with two authenticated users. Switching works correctly:

$ git config gh.user user1
$ gh auth status  # Shows user1 as active

$ git config gh.user user2  
$ gh auth status  # Shows user2 as active

Invalid users show warning but don't break commands.


Note: This POC was generated with Claude Code as a vibecoded exploration of #326.

MSch and others added 2 commits July 26, 2025 17:24
This feature allows the GitHub CLI to automatically switch users based on a
repository-specific git config setting 'gh.user'. This enables seamless work
with multiple GitHub accounts across different repositories.

Features:
- Reads gh.user from git config when in a repository
- Automatically switches to the configured user before authentication
- Graceful error handling with warnings for invalid users
- Non-breaking: commands continue to work if user switching fails
- Works with all authenticated commands (pr, repo, auth, etc.)

Usage:
  git config gh.user <username>  # Set user for current repository
  gh pr create                   # Uses the configured user automatically

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Added proper unit tests for the git config based user switching functionality:

Test Coverage:
- ✅ Not in git repository (graceful skip)
- ✅ Git repository errors (graceful skip)
- ✅ No gh.user configured (graceful skip)
- ✅ No authenticated hosts (graceful skip)
- ✅ No active user (graceful skip)
- ✅ User already matches current (no switch needed)
- ✅ Configured user not authenticated (proper error)
- ✅ Successful user switch (core functionality)
- ✅ User switch fails (error handling)

Implementation improvements:
- Extracted GitClientInterface for better testability
- Added internal maybeAutomaticUserSwitchWithGitClient for dependency injection
- Comprehensive mocks for gh.Config, gh.AuthConfig, and git client
- Tests verify both success and failure scenarios
- Tests check that SwitchUser is called with correct parameters

All tests pass: 9/9 scenarios covered

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
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.

1 participant