Skip to content

fix: update integration tests to work with modern Git default branch - #1

Merged
michaldarda merged 2 commits into
michaldarda:masterfrom
pawelma:improve-hook-management
Sep 11, 2025
Merged

michaldarda merged 2 commits into
michaldarda:masterfrom
pawelma:improve-hook-management

Conversation

@pawelma

@pawelma pawelma commented Sep 11, 2025

Copy link
Copy Markdown
Contributor

Git Hook Management Improvements

Overview

The git-pair tool has been enhanced with improved git hook management that preserves existing hooks while seamlessly integrating git-pair functionality.

Key Improvements

1. Hook Preservation

  • Before: Completely overwrote existing prepare-commit-msg hooks
  • After: Preserves existing hook content and only manages the git-pair section

2. Sectioned Management

  • Uses clear markers (# BEGIN git-pair and # END git-pair) to delimit git-pair functionality
  • Only modifies the git-pair section, leaving other hook logic intact
  • Safe installation and removal without affecting existing workflows

3. Smart Hook Removal

  • Selective removal: Only removes git-pair sections when clearing co-authors
  • Complete removal: Removes entire hook file only if it contains only git-pair content
  • Content preservation: Keeps existing hook logic when removing git-pair sections

Implementation Details

Hook Installation Process

  1. Read existing hook: Checks if prepare-commit-msg already exists
  2. Merge or append:
    • If git-pair section exists: replaces it with updated content
    • If no git-pair section: appends new section with proper markers
    • If no hook exists: creates new hook with shebang and git-pair section

Hook Removal Process

  1. Locate git-pair section: Finds content between # BEGIN git-pair and # END git-pair
  2. Remove section: Extracts only the git-pair portion
  3. Clean up spacing: Properly handles newlines and spacing
  4. Smart file management:
    • Removes entire file if only shebang/comments remain
    • Preserves file with remaining hook logic

Example Scenarios

Scenario 1: No Existing Hook

$ git-pair add John Doe john@example.com

Result: Creates new hook file with shebang and git-pair section

Scenario 2: Existing Hook Present

# Existing hook content
#!/bin/sh
echo "Custom hook logic"

After git-pair add John Doe john@example.com:

#!/bin/sh
echo "Custom hook logic"

# BEGIN git-pair
# git-pair hook to automatically add co-authors
# ... git-pair logic ...
# END git-pair

Scenario 3: Clearing Co-authors

When running git-pair clear:

  • If other hook logic exists: Removes only git-pair section, preserves rest
  • If only git-pair content: Removes entire hook file

Benefits

  1. Backward Compatibility: Existing hooks continue to work without modification
  2. Team Integration: Multiple team members can use git-pair without conflicts
  3. Tool Interoperability: Works alongside other tools that use prepare-commit-msg hooks
  4. Safe Operations: All operations are reversible and non-destructive

Testing

The implementation includes comprehensive tests covering:

  • Hook preservation workflows
  • Section merging and replacement
  • Complete hook removal scenarios
  • Integration with existing hook content
  • Edge cases (empty hooks, comment-only hooks, etc.)

All tests pass and the functionality has been validated through both unit tests and integration tests.

- Replace hardcoded 'config-master' with dynamic branch detection
- Use git branch --show-current to get actual branch name
- Add better error messages when config file not found
- Ensures tests work with both 'main' and 'master' default branches
This commit enhances the git hook management system to work alongside
existing prepare-commit-msg hooks instead of overwriting them.

Key improvements:
- Use delimited sections (# BEGIN/END git-pair) to isolate git-pair logic
- Preserve existing hook content when installing/updating hooks
- Smart removal that only removes git-pair sections
- Complete hook removal only when file contains only git-pair content
- Proper spacing and formatting handling

New functions:
- merge_git_pair_section(): Safely merges git-pair content into existing hooks
- remove_git_pair_section(): Removes only git-pair sections from hooks
- is_effectively_empty(): Determines if hook content is meaningful

Benefits:
- Backward compatibility with existing hooks
- Team collaboration without hook conflicts
- Tool interoperability with other prepare-commit-msg tools
- Safe, reversible operations

Testing:
- Added 9 comprehensive unit tests covering all scenarios
- Tests for hook preservation, merging, removal, and edge cases
- All existing tests continue to pass
@michaldarda
michaldarda merged commit 1a56ed5 into michaldarda:master Sep 11, 2025
7 checks passed
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