Skip to content

Commit eb3cdcc

Browse files
authored
feat: Support Git v2.25 and earlier (#11)
The rebase functionality was refactored in [Git v2.26](https://github.com/git/git/blob/master/Documentation/RelNotes/2.26.0.txt). Therefore the reflog messages were slightly different before that version. Modified `getLatestFixedCommit()` to have alternative regexp so that the reflog entries are also found correctly in Git v2.25 and earlier.
1 parent f642d47 commit eb3cdcc

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ on:
99
jobs:
1010
validate:
1111
runs-on: ubuntu-latest
12-
1312
steps:
1413
- name: Checkout code
1514
uses: actions/checkout@v4
@@ -25,3 +24,22 @@ jobs:
2524
run: npm run test
2625
- name: Run packaging smoke test
2726
run: npx @vscode/vsce package
27+
28+
old-version-support:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Install Git 2.25.1
32+
run: |
33+
echo "deb http://archive.ubuntu.com/ubuntu focal-updates main universe" | sudo tee /etc/apt/sources.list.d/focal.list
34+
sudo apt-get update
35+
sudo apt-get install --allow-downgrades -y git=1:2.25.1-1ubuntu3.13 git-man=1:2.25.1-1ubuntu3.13
36+
- name: Checkout code
37+
uses: actions/checkout@v4
38+
- name: Set up Node.js
39+
uses: actions/setup-node@v4
40+
with:
41+
node-version: '20'
42+
- name: Install dependencies
43+
run: npm ci
44+
- name: Run unit tests
45+
run: npm run test

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
- Support Git v2.25 and earlier
11+
812
## 1.2.0 (2025-01-19)
913

1014
- Highlight commits that match staged files

src/GitFacade.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export class GitFacade {
6464
}
6565

6666
async getLatestFixedCommit(): Promise<Commit> {
67-
return (await this.queryCommits('--grep-reflog=rebase (fixup)', '--walk-reflogs', '-1'))[0]
67+
// old Git versions (earlier than v2.26) include "-i" in the message
68+
return (await this.queryCommits('--grep-reflog=rebase (fixup)', '--grep-reflog=rebase -i (fixup)', '--walk-reflogs', '-1'))[0]
6869
}
6970

7071
private async queryCommits(...args: string[]): Promise<Commit[]> {

test/GitFacade.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('GitFacade', () => {
99
let facade: GitFacade
1010
let git: SimpleGit
1111
let repoDirectory: string
12+
let mainBranch: string
1213

1314
const configureGit = async () => {
1415
const configs = {
@@ -34,25 +35,26 @@ describe('GitFacade', () => {
3435
beforeEach(async () => {
3536
repoDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'temp-repo-'))
3637
git = simpleGit(repoDirectory)
37-
await git.init(['-b', 'main'])
38+
await git.init()
3839
await configureGit()
3940
await createCommit('lorem\n\n', 'subject 1')
4041
await createCommit('lorem\n\n\n\nipsum\n\n', 'subject 2')
4142
await createCommit('lorem\n\n\n\nipsum\n\n\n\ndolor', 'subject 3\n\nbody test')
4243
facade = new GitFacade()
4344
facade.updateWorkingDirectory(repoDirectory)
45+
mainBranch = await facade.getMainBranch()
4446
})
4547

4648
it('happy path', async () => {
4749
const FIXABLE_COMMIT_INDEX = 1
48-
const commitsBefore = await facade.getMainBranchCommits('main')
50+
const commitsBefore = await facade.getMainBranchCommits(mainBranch)
4951
const fixableCommit = commitsBefore[FIXABLE_COMMIT_INDEX]
5052
await modifyFileAndStageChanges('lorem\n\n\n\nfoobar\n\n')
5153
await facade.commitFixup(fixableCommit.hash)
5254
const isMergeConflict = await facade.rebaseFixupCommit(fixableCommit.hash)
5355
expect(isMergeConflict).toBe(false)
5456
const fixedCommit = await facade.getLatestFixedCommit()
55-
const commitsAfter = await facade.getMainBranchCommits('main')
57+
const commitsAfter = await facade.getMainBranchCommits(mainBranch)
5658
expect(commitsAfter[FIXABLE_COMMIT_INDEX].hash).toEqual(fixedCommit.hash)
5759
const fixedCommitDiff = await git.raw(['diff', '-U0', `${fixedCommit.hash}~`, `${fixedCommit.hash}`])
5860
expect(fixedCommitDiff).toContain('+\n+\n+foobar\n+\n')
@@ -61,19 +63,19 @@ describe('GitFacade', () => {
6163
})
6264

6365
it('getMainBranchCommits()', async () => {
64-
const commits = await facade.getMainBranchCommits('main')
66+
const commits = await facade.getMainBranchCommits(mainBranch)
6567
expect(commits).toHaveLength(3)
6668
const expectedLength = (await git.raw(['rev-parse', '--short', 'HEAD'])).trim().length
6769
expect(commits.map((c) => c.hash.length)).toEqual([expectedLength, expectedLength, expectedLength])
6870
expect(commits.map((c) => c.subject)).toEqual(['subject 3', 'subject 2', 'subject 1'])
6971
})
7072

7173
it('getFeatureBranchCommits()', async () => {
72-
const branchName = `feature-${Date.now()}`
73-
await git.checkoutLocalBranch(branchName)
74-
expect(await facade.getFeatureBranchCommits(branchName, 'main')).toHaveLength(0)
74+
const featureBranch = `feature-${Date.now()}`
75+
await git.checkoutLocalBranch(featureBranch)
76+
expect(await facade.getFeatureBranchCommits(featureBranch, mainBranch)).toHaveLength(0)
7577
await createCommit('foo', 'bar')
76-
expect(await facade.getFeatureBranchCommits(branchName, 'main')).toHaveLength(1)
78+
expect(await facade.getFeatureBranchCommits(featureBranch, mainBranch)).toHaveLength(1)
7779
})
7880

7981
it('getStagedFiles()', async () => {

0 commit comments

Comments
 (0)