Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- Highlight commits that match staged files

## 1.1.0 (2025-01-05)

- Dynamic hash length
Expand Down
4 changes: 4 additions & 0 deletions images/commit-dark-highlighted.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions images/commit-dark-upstream-highlighted.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions images/commit-dark-upstream.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions images/commit-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions images/commit-light-highlighted.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions images/commit-light-upstream-highlighted.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions images/commit-light-upstream.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions images/commit-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 18 additions & 7 deletions src/GitFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { BranchName, Commit, ShortCommitHash } from './types'

export const NO_UPSTREAM = 'NO_UPSTREAM'

const toLines = (output: string): string[] => {
const trimmed = output.replace(/\n$/, '')
if (trimmed !== '') {
return trimmed.split('\n')
} else {
return []
}
}

export class GitFacade {

private readonly git: SimpleGit
Expand All @@ -15,11 +24,6 @@ export class GitFacade {
this.git.cwd(path)
}

async hasStagedFiles(): Promise<boolean> {
const diff = await this.git.diff(['--name-only', '--cached'])
return (diff.trim() !== '')
}

async getCurrentBranch(): Promise<BranchName> {
const branchSummary = await this.git.branch()
return branchSummary.current
Expand Down Expand Up @@ -64,7 +68,7 @@ export class GitFacade {
}

private async queryCommits(...args: string[]): Promise<Commit[]> {
const lines = (await this.git.raw(['log', ...args, '--format=%h %s'])).trim().split('\n')
const lines = toLines(await this.git.raw(['log', ...args, '--format=%h %s']))
return lines.map((line) => {
const separatorIndex = line.indexOf(' ')
const hash = line.slice(0, separatorIndex)
Expand All @@ -73,7 +77,6 @@ export class GitFacade {
})
}


async commitFixup(hash: ShortCommitHash): Promise<void> {
await this.git.commit('', undefined, { '--fixup': hash })
}
Expand All @@ -92,4 +95,12 @@ export class GitFacade {
throw e
}
}

async getStagedFiles(): Promise<string[]> {
return toLines(await this.git.diff(['--cached', '--name-only']))
}

async getModifiedFiles(hash: ShortCommitHash): Promise<string[]> {
return toLines(await this.git.raw(['show', '--name-only', '--pretty=format:', hash]))
}
}
35 changes: 28 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode'
import { GitFacade, NO_UPSTREAM } from './GitFacade'
import { Commit } from './types'

export const activate = (context: vscode.ExtensionContext): void => {

Expand All @@ -11,6 +10,25 @@ export const activate = (context: vscode.ExtensionContext): void => {
outputChannel.appendLine(`${new Date().toISOString()} ${message}`)
}

const getCommitIcon = (isInUpstream: boolean, isHighlighted: boolean): vscode.IconPath => {
const getThemeIconUri = (theme: 'light' | 'dark'): vscode.Uri => {
const fileNameParts: string[] = []
fileNameParts.push('commit')
fileNameParts.push(theme)
if (isInUpstream) {
fileNameParts.push('upstream')
}
if (isHighlighted) {
fileNameParts.push('highlighted')
}
return vscode.Uri.joinPath(context.extensionUri, 'images', `${fileNameParts.join('-')}.svg`)
}
return {
light: getThemeIconUri('light'),
dark: getThemeIconUri('dark')
}
}

const disposable = vscode.commands.registerCommand('git-fixup.amendStagedChanges', async () => {

try {
Expand All @@ -21,8 +39,8 @@ export const activate = (context: vscode.ExtensionContext): void => {
}
git.updateWorkingDirectory(workspaceFolder)

const hasStagedFiles = await git.hasStagedFiles()
if (!hasStagedFiles) {
const stagedFiles = await git.getStagedFiles()
if (stagedFiles.length === 0) {
vscode.window.showErrorMessage('No staged files')
return
}
Expand All @@ -39,16 +57,19 @@ export const activate = (context: vscode.ExtensionContext): void => {
}

const commitsNotInUpstream = await git.getCommitsNotInUpstream()
const commitChoices = selectableCommits.map((commit: Commit) => {
const commitChoices = await Promise.all((selectableCommits.map(async (commit) => {
const isInUpstream = (commitsNotInUpstream !== NO_UPSTREAM) && (commitsNotInUpstream.find((upstreamCommit) => upstreamCommit.hash === commit.hash) === undefined)
const modifiedFiles = await git.getModifiedFiles(commit.hash)
const isHighlighted = modifiedFiles.some((filePath) => stagedFiles.includes(filePath))
const messageSubject = commit.subject
return {
label: `${isInUpstream ? '$(cloud)' : '$(git-commit)'} ${messageSubject}`,
iconPath: getCommitIcon(isInUpstream, isHighlighted),
label: ` ${messageSubject}`,
hash: commit.hash,
messageSubject,
isInUpstream: isInUpstream
isInUpstream
}
})
})))
const selectedCommit = await vscode.window.showQuickPick(commitChoices, { placeHolder: 'Select a Git commit to fix up' })

if (selectedCommit !== undefined) {
Expand Down
21 changes: 17 additions & 4 deletions test/GitFacade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,24 @@ describe('GitFacade', () => {
expect(commits.map((c) => c.subject)).toEqual(['subject 3', 'subject 2', 'subject 1'])
})

it('hasStagedFiles()', async () => {
expect(await facade.hasStagedFiles()).toBe(false)
it('getFeatureBranchCommits()', async () => {
const branchName = `feature-${Date.now()}`
await git.checkoutLocalBranch(branchName)
expect(await facade.getFeatureBranchCommits(branchName, 'main')).toHaveLength(0)
await createCommit('foo', 'bar')
expect(await facade.getFeatureBranchCommits(branchName, 'main')).toHaveLength(1)
})

it('getStagedFiles()', async () => {
expect(await facade.getStagedFiles()).toEqual([])
await modifyFileAndStageChanges('foobar')
expect(await facade.hasStagedFiles()).toBe(true)
expect(await facade.getStagedFiles()).toEqual(['file.txt'])
await git.commit('-')
expect(await facade.hasStagedFiles()).toBe(false)
expect(await facade.getStagedFiles()).toEqual([])
})

it('getModifiedFiles()', async () => {
const hash = (await git.raw(['rev-parse', '--short', 'HEAD'])).trim()
expect(await facade.getModifiedFiles(hash)).toEqual(['file.txt'])
})
})
Loading