mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
git log enhancements
This commit is contained in:
2 files changed
+358
-48
No files matched your search
@@ -0,0 +1,197 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { MockFS } from '@/test/MockFS';
|
||||
import { Git } from '@/lib/git';
|
||||
import { GitLogCommand } from './log';
|
||||
import { NPool } from '@nostrify/nostrify';
|
||||
|
||||
describe('GitLogCommand', () => {
|
||||
let fs: MockFS;
|
||||
let git: Git;
|
||||
let logCommand: GitLogCommand;
|
||||
|
||||
beforeEach(async () => {
|
||||
fs = new MockFS();
|
||||
git = new Git({
|
||||
fs,
|
||||
nostr: new NPool(),
|
||||
});
|
||||
logCommand = new GitLogCommand({ git, fs });
|
||||
|
||||
// Initialize a git repository
|
||||
await git.init({ dir: '/test-repo' });
|
||||
|
||||
// Configure git
|
||||
await git.setConfig({ dir: '/test-repo', path: 'user.name', value: 'Test User' });
|
||||
await git.setConfig({ dir: '/test-repo', path: 'user.email', value: '[email protected]' });
|
||||
|
||||
// Create some commits
|
||||
await fs.writeFile('/test-repo/file1.txt', 'content 1');
|
||||
await git.add({ dir: '/test-repo', filepath: 'file1.txt' });
|
||||
await git.commit({
|
||||
dir: '/test-repo',
|
||||
message: 'First commit',
|
||||
});
|
||||
|
||||
await fs.writeFile('/test-repo/file2.txt', 'content 2');
|
||||
await git.add({ dir: '/test-repo', filepath: 'file2.txt' });
|
||||
await git.commit({
|
||||
dir: '/test-repo',
|
||||
message: 'Second commit',
|
||||
});
|
||||
|
||||
await fs.writeFile('/test-repo/file3.txt', 'content 3');
|
||||
await git.add({ dir: '/test-repo', filepath: 'file3.txt' });
|
||||
await git.commit({
|
||||
dir: '/test-repo',
|
||||
message: 'Third commit',
|
||||
});
|
||||
});
|
||||
|
||||
it('should show all commits by default (no limit)', async () => {
|
||||
const result = await logCommand.execute([], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('First commit');
|
||||
expect(result.stdout).toContain('Second commit');
|
||||
expect(result.stdout).toContain('Third commit');
|
||||
});
|
||||
|
||||
it('should limit commits with -n flag', async () => {
|
||||
const result = await logCommand.execute(['-n', '1'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Third commit');
|
||||
expect(result.stdout).not.toContain('Second commit');
|
||||
expect(result.stdout).not.toContain('First commit');
|
||||
});
|
||||
|
||||
it('should limit commits with -n= syntax', async () => {
|
||||
const result = await logCommand.execute(['-n=2'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Third commit');
|
||||
expect(result.stdout).toContain('Second commit');
|
||||
expect(result.stdout).not.toContain('First commit');
|
||||
});
|
||||
|
||||
it('should limit commits with --max-count flag', async () => {
|
||||
const result = await logCommand.execute(['--max-count', '2'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Third commit');
|
||||
expect(result.stdout).toContain('Second commit');
|
||||
expect(result.stdout).not.toContain('First commit');
|
||||
});
|
||||
|
||||
it('should limit commits with -<number> syntax', async () => {
|
||||
const result = await logCommand.execute(['-2'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Third commit');
|
||||
expect(result.stdout).toContain('Second commit');
|
||||
expect(result.stdout).not.toContain('First commit');
|
||||
});
|
||||
|
||||
it('should format with --oneline', async () => {
|
||||
const result = await logCommand.execute(['--oneline'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
// Oneline format should have short hash and message on same line
|
||||
const lines = result.stdout.trim().split('\n');
|
||||
expect(lines.length).toBe(3);
|
||||
expect(lines[0]).toMatch(/^[0-9a-f]{7} Third commit$/);
|
||||
expect(lines[1]).toMatch(/^[0-9a-f]{7} Second commit$/);
|
||||
expect(lines[2]).toMatch(/^[0-9a-f]{7} First commit$/);
|
||||
});
|
||||
|
||||
it('should format with --format=oneline', async () => {
|
||||
const result = await logCommand.execute(['--format=oneline'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
const lines = result.stdout.trim().split('\n');
|
||||
expect(lines.length).toBe(3);
|
||||
expect(lines[0]).toMatch(/^[0-9a-f]{7} Third commit$/);
|
||||
});
|
||||
|
||||
it('should format with --format=short', async () => {
|
||||
const result = await logCommand.execute(['--format=short'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('commit');
|
||||
expect(result.stdout).toContain('Author:');
|
||||
expect(result.stdout).not.toContain('Date:'); // Short format doesn't include date
|
||||
});
|
||||
|
||||
it('should format with --format=full', async () => {
|
||||
const result = await logCommand.execute(['--format=full'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('commit');
|
||||
expect(result.stdout).toContain('Author:');
|
||||
expect(result.stdout).toContain('Date:');
|
||||
});
|
||||
|
||||
it('should format with --format=fuller', async () => {
|
||||
const result = await logCommand.execute(['--format=fuller'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('commit');
|
||||
expect(result.stdout).toContain('Author:');
|
||||
expect(result.stdout).toContain('AuthorDate:');
|
||||
});
|
||||
|
||||
it('should format with --format=raw', async () => {
|
||||
const result = await logCommand.execute(['--format=raw'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('commit');
|
||||
expect(result.stdout).toContain('author');
|
||||
expect(result.stdout).toMatch(/author .+ <.+> \d+ \+0000/);
|
||||
});
|
||||
|
||||
it('should show graph with --graph', async () => {
|
||||
const result = await logCommand.execute(['--graph', '--oneline'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
const lines = result.stdout.trim().split('\n');
|
||||
expect(lines[0]).toMatch(/^\* [0-9a-f]{7} Third commit$/);
|
||||
});
|
||||
|
||||
it('should combine --graph with full format', async () => {
|
||||
const result = await logCommand.execute(['--graph'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('* commit');
|
||||
});
|
||||
|
||||
it('should error when not in a git repository', async () => {
|
||||
const result = await logCommand.execute([], '/not-a-repo');
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('not a git repository');
|
||||
});
|
||||
|
||||
it('should show more than 50 commits when no limit is specified', async () => {
|
||||
// Create 60 commits
|
||||
for (let i = 4; i <= 60; i++) {
|
||||
await fs.writeFile(`/test-repo/file${i}.txt`, `content ${i}`);
|
||||
await git.add({ dir: '/test-repo', filepath: `file${i}.txt` });
|
||||
await git.commit({
|
||||
dir: '/test-repo',
|
||||
message: `Commit ${i}`,
|
||||
});
|
||||
}
|
||||
|
||||
const result = await logCommand.execute(['--oneline'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
const lines = result.stdout.trim().split('\n');
|
||||
expect(lines.length).toBe(60); // Should show all 60 commits, not just 50
|
||||
expect(result.stdout).toContain('Commit 60');
|
||||
expect(result.stdout).toContain('Commit 1');
|
||||
});
|
||||
|
||||
it('should respect limit even with many commits', async () => {
|
||||
// Create 60 commits
|
||||
for (let i = 4; i <= 60; i++) {
|
||||
await fs.writeFile(`/test-repo/file${i}.txt`, `content ${i}`);
|
||||
await git.add({ dir: '/test-repo', filepath: `file${i}.txt` });
|
||||
await git.commit({
|
||||
dir: '/test-repo',
|
||||
message: `Commit ${i}`,
|
||||
});
|
||||
}
|
||||
|
||||
const result = await logCommand.execute(['-10', '--oneline'], '/test-repo');
|
||||
expect(result.exitCode).toBe(0);
|
||||
const lines = result.stdout.trim().split('\n');
|
||||
expect(lines.length).toBe(10); // Should show only 10 commits
|
||||
expect(result.stdout).toContain('Commit 60');
|
||||
expect(result.stdout).not.toContain('Commit 1');
|
||||
});
|
||||
});
|
||||
+161
-48
@@ -7,7 +7,7 @@ import type { Git } from "../../git";
|
||||
export class GitLogCommand implements GitSubcommand {
|
||||
name = 'log';
|
||||
description = 'Show commit logs';
|
||||
usage = 'git log [--oneline] [--graph] [-n <number>] [<since>..<until>]';
|
||||
usage = 'git log [--oneline] [--graph] [--format=<format>] [-n <number>] [--all] [<ref>]';
|
||||
|
||||
private git: Git;
|
||||
private fs: JSRuntimeFS;
|
||||
@@ -26,23 +26,27 @@ export class GitLogCommand implements GitSubcommand {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
|
||||
const { options, limit } = this.parseArgs(args);
|
||||
const { options, limit, ref } = this.parseArgs(args);
|
||||
|
||||
try {
|
||||
const commits = await this.git.log({
|
||||
dir: cwd,
|
||||
depth: limit,
|
||||
});
|
||||
const logOptions: { dir: string; depth?: number; ref?: string } = { dir: cwd };
|
||||
if (limit !== undefined) {
|
||||
logOptions.depth = limit;
|
||||
}
|
||||
if (ref !== undefined) {
|
||||
logOptions.ref = ref;
|
||||
}
|
||||
|
||||
const commits = await this.git.log(logOptions);
|
||||
|
||||
if (commits.length === 0) {
|
||||
return createErrorResult('fatal: your current branch does not have any commits yet');
|
||||
}
|
||||
|
||||
if (options.oneline) {
|
||||
return this.formatOnelineLog(commits, cwd);
|
||||
} else {
|
||||
return this.formatFullLog(commits, options.graph, cwd);
|
||||
}
|
||||
// Determine format based on options
|
||||
const format = options.format || (options.oneline ? 'oneline' : 'full');
|
||||
|
||||
return this.formatLog(commits, format, options.graph, cwd);
|
||||
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('does not exist')) {
|
||||
@@ -57,11 +61,16 @@ export class GitLogCommand implements GitSubcommand {
|
||||
}
|
||||
|
||||
private parseArgs(args: string[]): {
|
||||
options: { oneline: boolean; graph: boolean };
|
||||
limit: number
|
||||
options: { oneline: boolean; graph: boolean; format?: string };
|
||||
limit: number | undefined;
|
||||
ref?: string;
|
||||
} {
|
||||
const options = { oneline: false, graph: false };
|
||||
let limit = 50; // Default limit
|
||||
const options: { oneline: boolean; graph: boolean; format?: string } = {
|
||||
oneline: false,
|
||||
graph: false
|
||||
};
|
||||
let limit: number | undefined = undefined; // No default limit - show all commits
|
||||
let ref: string | undefined = undefined;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
@@ -70,6 +79,18 @@ export class GitLogCommand implements GitSubcommand {
|
||||
options.oneline = true;
|
||||
} else if (arg === '--graph') {
|
||||
options.graph = true;
|
||||
} else if (arg === '--all') {
|
||||
// --all is not directly supported by isomorphic-git log
|
||||
// We'll just ignore it for now
|
||||
} else if (arg === '--format' || arg === '--pretty') {
|
||||
if (i + 1 < args.length) {
|
||||
options.format = args[i + 1];
|
||||
i++; // Skip next argument
|
||||
}
|
||||
} else if (arg.startsWith('--format=')) {
|
||||
options.format = arg.substring(9);
|
||||
} else if (arg.startsWith('--pretty=')) {
|
||||
options.format = arg.substring(9);
|
||||
} else if (arg === '-n' || arg === '--max-count') {
|
||||
if (i + 1 < args.length) {
|
||||
const num = parseInt(args[i + 1], 10);
|
||||
@@ -94,58 +115,150 @@ export class GitLogCommand implements GitSubcommand {
|
||||
if (!isNaN(num) && num > 0) {
|
||||
limit = num;
|
||||
}
|
||||
} else if (!arg.startsWith('-')) {
|
||||
// Treat as a ref (branch name, commit hash, etc.)
|
||||
ref = arg;
|
||||
}
|
||||
}
|
||||
|
||||
return { options, limit };
|
||||
return { options, limit, ref };
|
||||
}
|
||||
|
||||
private formatOnelineLog(commits: Array<{ oid: string; commit: { message: string } }>, _cwd: string): ShellCommandResult {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const commit of commits) {
|
||||
const shortHash = commit.oid.substring(0, 7);
|
||||
const message = commit.commit.message.split('\n')[0]; // First line only
|
||||
lines.push(`${shortHash} ${message}`);
|
||||
}
|
||||
|
||||
return createSuccessResult(lines.join('\n') + '\n');
|
||||
}
|
||||
|
||||
private formatFullLog(commits: Array<{ oid: string; commit: { message: string; author: { name: string; email: string; timestamp: number } } }>, showGraph: boolean, _cwd: string): ShellCommandResult {
|
||||
private formatLog(
|
||||
commits: Array<{ oid: string; commit: { message: string; author: { name: string; email: string; timestamp: number }; committer?: { name: string; email: string; timestamp: number } } }>,
|
||||
format: string,
|
||||
showGraph: boolean,
|
||||
_cwd: string
|
||||
): ShellCommandResult {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (let i = 0; i < commits.length; i++) {
|
||||
const commit = commits[i];
|
||||
const isLast = i === commits.length - 1;
|
||||
const graphPrefix = showGraph ? '* ' : '';
|
||||
|
||||
if (showGraph) {
|
||||
lines.push('* commit ' + commit.oid);
|
||||
} else {
|
||||
lines.push('commit ' + commit.oid);
|
||||
}
|
||||
// Format based on the specified format
|
||||
switch (format) {
|
||||
case 'oneline':
|
||||
lines.push(this.formatOneline(commit, graphPrefix));
|
||||
break;
|
||||
|
||||
// Author information
|
||||
const author = commit.commit.author;
|
||||
const authorDate = new Date(author.timestamp * 1000);
|
||||
const authorDateStr = authorDate.toUTCString();
|
||||
case 'short':
|
||||
lines.push(this.formatShort(commit, graphPrefix));
|
||||
if (!isLast) lines.push('');
|
||||
break;
|
||||
|
||||
lines.push(`Author: ${author.name} <${author.email}>`);
|
||||
lines.push(`Date: ${authorDateStr}`);
|
||||
lines.push('');
|
||||
case 'medium':
|
||||
case 'full':
|
||||
default:
|
||||
lines.push(this.formatFull(commit, graphPrefix));
|
||||
if (!isLast) lines.push('');
|
||||
break;
|
||||
|
||||
// Commit message (indented)
|
||||
const messageLines = commit.commit.message.split('\n');
|
||||
for (const messageLine of messageLines) {
|
||||
lines.push(` ${messageLine}`);
|
||||
}
|
||||
case 'fuller':
|
||||
lines.push(this.formatFuller(commit, graphPrefix));
|
||||
if (!isLast) lines.push('');
|
||||
break;
|
||||
|
||||
if (!isLast) {
|
||||
lines.push('');
|
||||
case 'raw':
|
||||
lines.push(this.formatRaw(commit));
|
||||
if (!isLast) lines.push('');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return createSuccessResult(lines.join('\n') + '\n');
|
||||
}
|
||||
|
||||
private formatOneline(commit: { oid: string; commit: { message: string } }, graphPrefix: string): string {
|
||||
const shortHash = commit.oid.substring(0, 7);
|
||||
const message = commit.commit.message.split('\n')[0]; // First line only
|
||||
return `${graphPrefix}${shortHash} ${message}`;
|
||||
}
|
||||
|
||||
private formatShort(commit: { oid: string; commit: { message: string; author: { name: string; email: string } } }, graphPrefix: string): string {
|
||||
const lines: string[] = [];
|
||||
const shortHash = commit.oid.substring(0, 7);
|
||||
|
||||
lines.push(`${graphPrefix}commit ${shortHash}`);
|
||||
lines.push(`Author: ${commit.commit.author.name} <${commit.commit.author.email}>`);
|
||||
lines.push('');
|
||||
|
||||
const messageLines = commit.commit.message.split('\n');
|
||||
for (const messageLine of messageLines) {
|
||||
lines.push(` ${messageLine}`);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
private formatFull(commit: { oid: string; commit: { message: string; author: { name: string; email: string; timestamp: number } } }, graphPrefix: string): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
lines.push(`${graphPrefix}commit ${commit.oid}`);
|
||||
|
||||
const author = commit.commit.author;
|
||||
const authorDate = new Date(author.timestamp * 1000);
|
||||
const authorDateStr = authorDate.toUTCString();
|
||||
|
||||
lines.push(`Author: ${author.name} <${author.email}>`);
|
||||
lines.push(`Date: ${authorDateStr}`);
|
||||
lines.push('');
|
||||
|
||||
const messageLines = commit.commit.message.split('\n');
|
||||
for (const messageLine of messageLines) {
|
||||
lines.push(` ${messageLine}`);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
private formatFuller(commit: { oid: string; commit: { message: string; author: { name: string; email: string; timestamp: number }; committer?: { name: string; email: string; timestamp: number } } }, graphPrefix: string): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
lines.push(`${graphPrefix}commit ${commit.oid}`);
|
||||
|
||||
const author = commit.commit.author;
|
||||
const authorDate = new Date(author.timestamp * 1000);
|
||||
|
||||
lines.push(`Author: ${author.name} <${author.email}>`);
|
||||
lines.push(`AuthorDate: ${authorDate.toUTCString()}`);
|
||||
|
||||
// Include committer info if available
|
||||
if (commit.commit.committer) {
|
||||
const committer = commit.commit.committer;
|
||||
const commitDate = new Date(committer.timestamp * 1000);
|
||||
lines.push(`Commit: ${committer.name} <${committer.email}>`);
|
||||
lines.push(`CommitDate: ${commitDate.toUTCString()}`);
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
|
||||
const messageLines = commit.commit.message.split('\n');
|
||||
for (const messageLine of messageLines) {
|
||||
lines.push(` ${messageLine}`);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
private formatRaw(commit: { oid: string; commit: { message: string; author: { name: string; email: string; timestamp: number }; committer?: { name: string; email: string; timestamp: number } } }): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
lines.push(`commit ${commit.oid}`);
|
||||
|
||||
const author = commit.commit.author;
|
||||
lines.push(`author ${author.name} <${author.email}> ${author.timestamp} +0000`);
|
||||
|
||||
if (commit.commit.committer) {
|
||||
const committer = commit.commit.committer;
|
||||
lines.push(`committer ${committer.name} <${committer.email}> ${committer.timestamp} +0000`);
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
lines.push(commit.commit.message);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user