Skip to content

Commit 224b6e2

Browse files
dkattanTestCopilot
authored
fix: Split-Commit piece grouping, Move-Commit conflict handling, and GIT_EDITOR (#17)
- Fix Split-Commit creating one piece per hunk instead of grouping by PieceNumber across multiple files in a single commit - Add -AutoResolveConflicts to Move-Commit for modify/delete conflicts during source rebase (auto-removes files created by the moved commit) - Set GIT_EDITOR/GIT_SEQUENCE_EDITOR during Move-Commit rebase to prevent interactive editor hangs in non-interactive environments Co-authored-by: Test <test@test.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c880f24 commit 224b6e2

3 files changed

Lines changed: 410 additions & 78 deletions

File tree

‎.github/workflows/codeql.yml‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CodeQL
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths-ignore:
7+
- 'GitSplit.psd1'
8+
pull_request:
9+
branches: [main]
10+
11+
jobs:
12+
analyze:
13+
name: Analyze
14+
runs-on: ubuntu-latest
15+
permissions:
16+
actions: read
17+
contents: read
18+
security-events: write
19+
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
language: [powershell]
24+
25+
steps:
26+
- name: Checkout
27+
uses: actions/checkout@v4
28+
29+
- name: Initialize CodeQL
30+
uses: github/codeql-action/init@v3
31+
with:
32+
languages: ${{ matrix.language }}
33+
34+
- name: Autobuild
35+
uses: github/codeql-action/autobuild@v3
36+
37+
- name: Perform CodeQL Analysis
38+
uses: github/codeql-action/analyze@v3
39+
with:
40+
category: "/language:${{ matrix.language }}"

‎GitSplit.Tests.ps1‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,76 @@ Import-Module '$escapedManifestPath' -Force
13911391
Pop-Location
13921392
}
13931393
}
1394+
1395+
It "groups hunks by PieceNumber across multiple files in a single commit" {
1396+
Push-Location $script:TempRepoPath
1397+
try {
1398+
# Create three files with 30 lines each for well-separated hunks.
1399+
$lines = 1..30 | ForEach-Object { "line-$_" }
1400+
$lines | Set-Content -Path 'fileA.txt'
1401+
$lines | Set-Content -Path 'fileB.txt'
1402+
$lines | Set-Content -Path 'fileC.txt'
1403+
git add fileA.txt fileB.txt fileC.txt | Out-Null
1404+
git commit -m 'Add fileA, fileB, fileC' | Out-Null
1405+
1406+
# Modify all three files with 3 well-separated hunks per file (lines 2, 15, 28).
1407+
$modLines = 1..30 | ForEach-Object {
1408+
if ($_ -in @(2, 15, 28)) { "line-$_ changed" } else { "line-$_" }
1409+
}
1410+
$modLines | Set-Content -Path 'fileA.txt'
1411+
$modLines | Set-Content -Path 'fileB.txt'
1412+
$modLines | Set-Content -Path 'fileC.txt'
1413+
git add fileA.txt fileB.txt fileC.txt | Out-Null
1414+
git commit -m 'Modify all three files with 3 hunks each' | Out-Null
1415+
1416+
# Assign fileB's last hunk to piece 2; everything else defaults to piece 1.
1417+
$targetHunk = @(Get-GitSplitHunks -Ref 'HEAD' | Where-Object { $_.Path -eq 'fileB.txt' } | Select-Object -Last 1)
1418+
$targetHunk | Should -HaveCount 1
1419+
1420+
$beforeCount = [int](git rev-list --count HEAD)
1421+
$created = @(Split-Commit -Ref 'HEAD' -NewCommitRanges @(
1422+
[pscustomobject]@{ HunkId = $targetHunk[0].HunkId; PieceNumber = 2 }
1423+
))
1424+
1425+
# Should create exactly 2 split pieces, not 3 (one per hunk in fileB).
1426+
$created | Should -HaveCount 2
1427+
([int](git rev-list --count HEAD)) | Should -Be ($beforeCount + 1)
1428+
1429+
# Piece 1 should contain changes to all 3 files (fileA + fileC all hunks + fileB first 2 hunks).
1430+
git checkout --detach -q $created[0] 2>$null | Out-Null
1431+
if ($LASTEXITCODE -ne 0) {
1432+
throw "Expected to be able to checkout first split commit $($created[0])"
1433+
}
1434+
$firstA = @(Get-Content -Path 'fileA.txt')
1435+
$firstB = @(Get-Content -Path 'fileB.txt')
1436+
$firstC = @(Get-Content -Path 'fileC.txt')
1437+
# fileA: all 3 hunks applied
1438+
$firstA[1] | Should -Be 'line-2 changed'
1439+
$firstA[14] | Should -Be 'line-15 changed'
1440+
$firstA[27] | Should -Be 'line-28 changed'
1441+
# fileB: first 2 hunks applied, last hunk NOT applied
1442+
$firstB[1] | Should -Be 'line-2 changed'
1443+
$firstB[14] | Should -Be 'line-15 changed'
1444+
$firstB[27] | Should -Be 'line-28'
1445+
# fileC: all 3 hunks applied
1446+
$firstC[1] | Should -Be 'line-2 changed'
1447+
$firstC[14] | Should -Be 'line-15 changed'
1448+
$firstC[27] | Should -Be 'line-28 changed'
1449+
1450+
# Piece 2 should contain only fileB's last hunk.
1451+
git checkout --detach -q $created[1] 2>$null | Out-Null
1452+
if ($LASTEXITCODE -ne 0) {
1453+
throw "Expected to be able to checkout second split commit $($created[1])"
1454+
}
1455+
$finalB = @(Get-Content -Path 'fileB.txt')
1456+
$finalB[1] | Should -Be 'line-2 changed'
1457+
$finalB[14] | Should -Be 'line-15 changed'
1458+
$finalB[27] | Should -Be 'line-28 changed'
1459+
}
1460+
finally {
1461+
Pop-Location
1462+
}
1463+
}
13941464
}
13951465

13961466
Describe "Split-Commit guardrails" {
@@ -2314,6 +2384,99 @@ Import-Module '$escapedManifestPath' -Force
23142384
Pop-Location
23152385
}
23162386
}
2387+
2388+
It "auto-removes files created by moved commit from source during modify/delete conflicts" {
2389+
Push-Location $script:TempRepoPath
2390+
try {
2391+
$sourceBranch = (git rev-parse --abbrev-ref HEAD).Trim()
2392+
2393+
git branch auto-resolve-dest | Out-Null
2394+
$LASTEXITCODE | Should -Be 0
2395+
2396+
# Commit that CREATES a new file — this is the commit to MOVE.
2397+
'new-file-content' | Set-Content -Path 'newFile.txt'
2398+
git add newFile.txt | Out-Null
2399+
$msgToMove = "Add newFile to MOVE $(New-Guid)"
2400+
git commit -m $msgToMove | Out-Null
2401+
$LASTEXITCODE | Should -Be 0
2402+
2403+
# Later commit that MODIFIES the new file AND another file — this stays in the source branch.
2404+
# When the move commit is removed via rebase --onto, this commit will hit a
2405+
# modify/delete conflict because newFile.txt no longer exists in the rebased history.
2406+
@(
2407+
'new-file-content'
2408+
'modified-line'
2409+
) | Set-Content -Path 'newFile.txt'
2410+
'extra-a-line-for-keep' | Add-Content -Path 'a.txt'
2411+
git add newFile.txt a.txt | Out-Null
2412+
$msgToKeep = "Modify newFile to KEEP $(New-Guid)"
2413+
git commit -m $msgToKeep | Out-Null
2414+
$LASTEXITCODE | Should -Be 0
2415+
2416+
# Move the commit that created newFile.txt, removing it from source.
2417+
# This triggers a modify/delete conflict during rebase because newFile.txt was
2418+
# created by the moved commit but is modified by the kept commit. The auto-resolve
2419+
# logic removes newFile.txt from the source (it now lives on the destination branch).
2420+
Move-Commit -CommitRef HEAD~1 -DestinationBranch 'auto-resolve-dest' -RemoveFromSource -AutoResolveConflicts | Out-Null
2421+
2422+
(git rev-parse --abbrev-ref HEAD).Trim() | Should -Be $sourceBranch
2423+
2424+
# Source branch should no longer contain newFile.txt (it was auto-removed).
2425+
Test-Path 'newFile.txt' | Should -BeFalse
2426+
2427+
# Source branch should still contain the later commit.
2428+
$sourceSubjects = (git log -n 50 --pretty=format:%s) -join "`n"
2429+
$sourceSubjects | Should -Not -Match ([regex]::Escape($msgToMove))
2430+
$sourceSubjects | Should -Match ([regex]::Escape($msgToKeep))
2431+
2432+
# Destination should contain the moved commit and the file.
2433+
$destSubjects = (git log auto-resolve-dest -n 50 --pretty=format:%s) -join "`n"
2434+
$destSubjects | Should -Match ([regex]::Escape($msgToMove))
2435+
git -C (Join-Path $script:TempRepoPath '.gitsplit-worktrees') rev-parse HEAD 2>$null | Out-Null
2436+
# Verify the file exists on the destination branch.
2437+
$destFileContent = @(git show "auto-resolve-dest:newFile.txt" 2>$null)
2438+
$destFileContent | Should -Contain 'new-file-content'
2439+
}
2440+
finally {
2441+
Pop-Location
2442+
}
2443+
}
2444+
2445+
It "sets GIT_EDITOR and GIT_SEQUENCE_EDITOR in Move-Commit rebase scripts" {
2446+
$scriptPath = $null
2447+
Push-Location $script:TempRepoPath
2448+
try {
2449+
git branch editor-dest | Out-Null
2450+
$LASTEXITCODE | Should -Be 0
2451+
2452+
'editor-line-1' | Add-Content -Path 'b.txt'
2453+
git add b.txt | Out-Null
2454+
git commit -m "Editor move $(New-Guid)" | Out-Null
2455+
$LASTEXITCODE | Should -Be 0
2456+
2457+
'editor-line-2' | Add-Content -Path 'a.txt'
2458+
git add a.txt | Out-Null
2459+
git commit -m "Editor keep $(New-Guid)" | Out-Null
2460+
$LASTEXITCODE | Should -Be 0
2461+
2462+
$scriptPath = Join-Path ([System.IO.Path]::GetTempPath()) ("move-commit-editor-" + (New-Guid) + ".ps1")
2463+
if (Test-Path $scriptPath) {
2464+
Remove-Item -Path $scriptPath -Force
2465+
}
2466+
2467+
Move-Commit -CommitRef HEAD~1 -DestinationBranch 'editor-dest' -RemoveFromSource -AutoResolveConflicts -OutputScriptPath $scriptPath | Out-Null
2468+
2469+
$scriptText = Get-Content -Path $scriptPath -Raw
2470+
$scriptText | Should -Match ([regex]::Escape("`$env:GIT_EDITOR = ':'"))
2471+
$scriptText | Should -Match ([regex]::Escape("`$env:GIT_SEQUENCE_EDITOR = ':'"))
2472+
}
2473+
finally {
2474+
if ($scriptPath -and (Test-Path $scriptPath)) {
2475+
Remove-Item -Path $scriptPath -Force
2476+
}
2477+
Pop-Location
2478+
}
2479+
}
23172480
}
23182481

23192482
Describe "Deterministic test hooks" {
@@ -2429,6 +2592,7 @@ $destinationBranch = 'snapshot-dest'
24292592
$destinationRef = 'refs/heads/snapshot-dest'
24302593
$useRemoteTrackingBranch = $false
24312594
$autoStash = $true
2595+
$autoResolveConflicts = $false
24322596
$pushDestination = $false
24332597
$plannedStashName = 'fixed-move-commit-stash'
24342598
$destWorktreePath = '__WORKTREE_PATH__'

0 commit comments

Comments
 (0)