add git subsystem — daemon-side status, diff, operations + IPC
Shell-outs to git for status (porcelain v1/v2), unified-diff parsing, and operations (stage, unstage, hunk-apply, discard, commit, stash, log, pull, push). IPC verbs git.* registered on the daemon dispatcher with git.changed event emission on mutations. 42 new core tests. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/clide.dart';
|
||||
import 'package:clide/src/daemon/git_commands.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
late Directory sandbox;
|
||||
late DaemonDispatcher dispatcher;
|
||||
late RecordingEventSink sink;
|
||||
|
||||
setUp(() async {
|
||||
sandbox = await Directory.systemTemp.createTemp('clide-git-cmd-test-');
|
||||
await Process.run('git', ['init'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.email', 'test@test.com'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.name', 'Test'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await File('${sandbox.path}/file.txt').writeAsString('hello\n');
|
||||
await Process.run('git', ['add', '.'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['commit', '-m', 'init'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
|
||||
sink = RecordingEventSink();
|
||||
dispatcher = DaemonDispatcher();
|
||||
registerGitCommands(dispatcher, sandbox, sink);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
Future<IpcResponse> call(String cmd,
|
||||
[Map<String, Object?> args = const {}]) {
|
||||
return dispatcher.dispatch(IpcRequest(id: '1', cmd: cmd, args: args));
|
||||
}
|
||||
|
||||
test('git.status returns clean status', () async {
|
||||
final r = await call('git.status');
|
||||
expect(r.ok, isTrue);
|
||||
expect(r.data['clean'], isTrue);
|
||||
expect(r.data['branch'], isNotNull);
|
||||
});
|
||||
|
||||
test('git.status shows untracked files', () async {
|
||||
await File('${sandbox.path}/new.txt').writeAsString('x');
|
||||
final r = await call('git.status');
|
||||
expect(r.ok, isTrue);
|
||||
final untracked = r.data['untracked'] as List;
|
||||
expect(untracked, hasLength(1));
|
||||
});
|
||||
|
||||
test('git.stage + git.status shows staged file', () async {
|
||||
await File('${sandbox.path}/new.txt').writeAsString('x');
|
||||
final stage = await call('git.stage', {'paths': ['new.txt']});
|
||||
expect(stage.ok, isTrue);
|
||||
|
||||
final r = await call('git.status');
|
||||
final staged = r.data['staged'] as List;
|
||||
expect(staged, hasLength(1));
|
||||
});
|
||||
|
||||
test('git.stage without paths returns error', () async {
|
||||
final r = await call('git.stage');
|
||||
expect(r.ok, isFalse);
|
||||
expect(r.error!.kind, 'user_error');
|
||||
});
|
||||
|
||||
test('git.unstage removes from staging', () async {
|
||||
await File('${sandbox.path}/new.txt').writeAsString('x');
|
||||
await call('git.stage', {'paths': ['new.txt']});
|
||||
final unstage = await call('git.unstage', {'paths': ['new.txt']});
|
||||
expect(unstage.ok, isTrue);
|
||||
|
||||
final r = await call('git.status');
|
||||
final staged = r.data['staged'] as List;
|
||||
expect(staged, isEmpty);
|
||||
});
|
||||
|
||||
test('git.commit creates a commit', () async {
|
||||
await File('${sandbox.path}/c.txt').writeAsString('x');
|
||||
await call('git.stage', {'paths': ['c.txt']});
|
||||
final r = await call('git.commit', {'message': 'test commit'});
|
||||
expect(r.ok, isTrue);
|
||||
expect(r.data['hash'], hasLength(40));
|
||||
});
|
||||
|
||||
test('git.commit without message returns error', () async {
|
||||
final r = await call('git.commit');
|
||||
expect(r.ok, isFalse);
|
||||
expect(r.error!.kind, 'user_error');
|
||||
});
|
||||
|
||||
test('git.diff returns diffs for modified files', () async {
|
||||
await File('${sandbox.path}/file.txt').writeAsString('modified\n');
|
||||
final r = await call('git.diff');
|
||||
expect(r.ok, isTrue);
|
||||
final diffs = r.data['diffs'] as List;
|
||||
expect(diffs, hasLength(1));
|
||||
});
|
||||
|
||||
test('git.diff --staged returns staged diffs', () async {
|
||||
await File('${sandbox.path}/file.txt').writeAsString('modified\n');
|
||||
await call('git.stage', {'paths': ['file.txt']});
|
||||
final r = await call('git.diff', {'staged': true});
|
||||
expect(r.ok, isTrue);
|
||||
final diffs = r.data['diffs'] as List;
|
||||
expect(diffs, hasLength(1));
|
||||
});
|
||||
|
||||
test('git.log returns entries', () async {
|
||||
final r = await call('git.log');
|
||||
expect(r.ok, isTrue);
|
||||
final entries = r.data['entries'] as List;
|
||||
expect(entries, isNotEmpty);
|
||||
});
|
||||
|
||||
test('git.discard restores a file', () async {
|
||||
await File('${sandbox.path}/file.txt').writeAsString('changed');
|
||||
final r = await call('git.discard', {'paths': ['file.txt']});
|
||||
expect(r.ok, isTrue);
|
||||
final content = await File('${sandbox.path}/file.txt').readAsString();
|
||||
expect(content, 'hello\n');
|
||||
});
|
||||
|
||||
test('git.discard without paths returns error', () async {
|
||||
final r = await call('git.discard');
|
||||
expect(r.ok, isFalse);
|
||||
expect(r.error!.kind, 'user_error');
|
||||
});
|
||||
|
||||
test('mutations emit git.changed events', () async {
|
||||
await File('${sandbox.path}/e.txt').writeAsString('x');
|
||||
await call('git.stage', {'paths': ['e.txt']});
|
||||
expect(
|
||||
sink.events,
|
||||
contains(predicate<IpcEvent>((e) => e.kind == 'git.changed')),
|
||||
);
|
||||
});
|
||||
|
||||
test('git.stage-all stages everything', () async {
|
||||
await File('${sandbox.path}/a.txt').writeAsString('a');
|
||||
await File('${sandbox.path}/b.txt').writeAsString('b');
|
||||
final r = await call('git.stage-all');
|
||||
expect(r.ok, isTrue);
|
||||
|
||||
final status = await call('git.status');
|
||||
final staged = status.data['staged'] as List;
|
||||
expect(staged.length, greaterThanOrEqualTo(2));
|
||||
});
|
||||
|
||||
test('git.stash and git.stash-pop round-trip', () async {
|
||||
await File('${sandbox.path}/file.txt').writeAsString('stash-me');
|
||||
final stash = await call('git.stash');
|
||||
expect(stash.ok, isTrue);
|
||||
|
||||
var content = await File('${sandbox.path}/file.txt').readAsString();
|
||||
expect(content, 'hello\n');
|
||||
|
||||
final pop = await call('git.stash-pop');
|
||||
expect(pop.ok, isTrue);
|
||||
content = await File('${sandbox.path}/file.txt').readAsString();
|
||||
expect(content, 'stash-me');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/src/git/diff.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('parseDiffOutput', () {
|
||||
test('parses a simple modification', () {
|
||||
const output = '''diff --git a/file.txt b/file.txt
|
||||
index abc1234..def5678 100644
|
||||
--- a/file.txt
|
||||
+++ b/file.txt
|
||||
@@ -1,3 +1,4 @@
|
||||
line1
|
||||
-line2
|
||||
+line2-modified
|
||||
+line3-new
|
||||
line4
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
expect(diffs, hasLength(1));
|
||||
expect(diffs.first.path, 'file.txt');
|
||||
expect(diffs.first.hunks, hasLength(1));
|
||||
expect(diffs.first.additions, 2);
|
||||
expect(diffs.first.removals, 1);
|
||||
});
|
||||
|
||||
test('parses a new file', () {
|
||||
const output = '''diff --git a/new.txt b/new.txt
|
||||
new file mode 100644
|
||||
index 0000000..abc1234
|
||||
--- /dev/null
|
||||
+++ b/new.txt
|
||||
@@ -0,0 +1,2 @@
|
||||
+hello
|
||||
+world
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
expect(diffs, hasLength(1));
|
||||
expect(diffs.first.isNew, isTrue);
|
||||
expect(diffs.first.additions, 2);
|
||||
});
|
||||
|
||||
test('parses a deleted file', () {
|
||||
const output = '''diff --git a/old.txt b/old.txt
|
||||
deleted file mode 100644
|
||||
index abc1234..0000000
|
||||
--- a/old.txt
|
||||
+++ /dev/null
|
||||
@@ -1,2 +0,0 @@
|
||||
-hello
|
||||
-world
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
expect(diffs, hasLength(1));
|
||||
expect(diffs.first.isDeleted, isTrue);
|
||||
expect(diffs.first.removals, 2);
|
||||
});
|
||||
|
||||
test('parses a rename', () {
|
||||
const output = '''diff --git a/old.txt b/new.txt
|
||||
similarity index 100%
|
||||
rename from old.txt
|
||||
rename to new.txt
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
expect(diffs, hasLength(1));
|
||||
expect(diffs.first.isRenamed, isTrue);
|
||||
expect(diffs.first.path, 'new.txt');
|
||||
expect(diffs.first.oldPath, 'old.txt');
|
||||
});
|
||||
|
||||
test('parses multiple hunks', () {
|
||||
const output = '''diff --git a/multi.txt b/multi.txt
|
||||
index abc..def 100644
|
||||
--- a/multi.txt
|
||||
+++ b/multi.txt
|
||||
@@ -1,3 +1,3 @@
|
||||
line1
|
||||
-old2
|
||||
+new2
|
||||
line3
|
||||
@@ -10,3 +10,3 @@
|
||||
line10
|
||||
-old11
|
||||
+new11
|
||||
line12
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
expect(diffs, hasLength(1));
|
||||
expect(diffs.first.hunks, hasLength(2));
|
||||
expect(diffs.first.hunks[0].oldStart, 1);
|
||||
expect(diffs.first.hunks[1].oldStart, 10);
|
||||
});
|
||||
|
||||
test('parses binary file', () {
|
||||
const output = '''diff --git a/image.png b/image.png
|
||||
Binary files a/image.png and b/image.png differ
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
expect(diffs, hasLength(1));
|
||||
expect(diffs.first.isBinary, isTrue);
|
||||
expect(diffs.first.hunks, isEmpty);
|
||||
});
|
||||
|
||||
test('empty output returns empty list', () {
|
||||
expect(parseDiffOutput(''), isEmpty);
|
||||
});
|
||||
|
||||
test('hunk line numbers are correct', () {
|
||||
const output = '''diff --git a/f.txt b/f.txt
|
||||
index abc..def 100644
|
||||
--- a/f.txt
|
||||
+++ b/f.txt
|
||||
@@ -5,4 +5,5 @@
|
||||
ctx
|
||||
-removed
|
||||
+added1
|
||||
+added2
|
||||
ctx2
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
final hunk = diffs.first.hunks.first;
|
||||
expect(hunk.oldStart, 5);
|
||||
expect(hunk.newStart, 5);
|
||||
|
||||
final additions = hunk.lines.where((l) => l.kind == DiffLineKind.addition).toList();
|
||||
expect(additions[0].newLineNo, 6);
|
||||
expect(additions[1].newLineNo, 7);
|
||||
|
||||
final removals = hunk.lines.where((l) => l.kind == DiffLineKind.removal).toList();
|
||||
expect(removals[0].oldLineNo, 6);
|
||||
});
|
||||
|
||||
test('multiple diffs in one output', () {
|
||||
const output = '''diff --git a/a.txt b/a.txt
|
||||
index abc..def 100644
|
||||
--- a/a.txt
|
||||
+++ b/a.txt
|
||||
@@ -1 +1 @@
|
||||
-old
|
||||
+new
|
||||
diff --git a/b.txt b/b.txt
|
||||
index abc..def 100644
|
||||
--- a/b.txt
|
||||
+++ b/b.txt
|
||||
@@ -1 +1 @@
|
||||
-old
|
||||
+new
|
||||
''';
|
||||
final diffs = parseDiffOutput(output);
|
||||
expect(diffs, hasLength(2));
|
||||
expect(diffs[0].path, 'a.txt');
|
||||
expect(diffs[1].path, 'b.txt');
|
||||
});
|
||||
});
|
||||
|
||||
group('gitDiff (live)', () {
|
||||
late Directory sandbox;
|
||||
|
||||
setUp(() async {
|
||||
sandbox = await Directory.systemTemp.createTemp('clide-git-diff-test-');
|
||||
await Process.run('git', ['init'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.email', 'test@test.com'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.name', 'Test'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await File('${sandbox.path}/file.txt').writeAsString('line1\nline2\n');
|
||||
await Process.run('git', ['add', '.'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['commit', '-m', 'init'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
test('returns unstaged diff after modification', () async {
|
||||
await File('${sandbox.path}/file.txt')
|
||||
.writeAsString('line1\nmodified\n');
|
||||
final diffs = await gitDiff(sandbox);
|
||||
expect(diffs, hasLength(1));
|
||||
expect(diffs.first.path, 'file.txt');
|
||||
expect(diffs.first.additions, greaterThan(0));
|
||||
});
|
||||
|
||||
test('returns staged diff with staged: true', () async {
|
||||
await File('${sandbox.path}/file.txt')
|
||||
.writeAsString('line1\nmodified\n');
|
||||
await Process.run(
|
||||
'git',
|
||||
['add', 'file.txt'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
final diffs = await gitDiff(sandbox, staged: true);
|
||||
expect(diffs, hasLength(1));
|
||||
});
|
||||
|
||||
test('returns empty for clean repo', () async {
|
||||
final diffs = await gitDiff(sandbox);
|
||||
expect(diffs, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/src/git/operations.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
late Directory sandbox;
|
||||
|
||||
setUp(() async {
|
||||
sandbox = await Directory.systemTemp.createTemp('clide-git-ops-test-');
|
||||
await Process.run('git', ['init'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.email', 'test@test.com'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.name', 'Test'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await File('${sandbox.path}/file.txt').writeAsString('hello\n');
|
||||
await Process.run('git', ['add', '.'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['commit', '-m', 'init'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
test('gitStage stages a file', () async {
|
||||
await File('${sandbox.path}/new.txt').writeAsString('x');
|
||||
await gitStage(sandbox, ['new.txt']);
|
||||
final r = await Process.run(
|
||||
'git',
|
||||
['diff', '--cached', '--name-only'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
expect((r.stdout as String).trim(), 'new.txt');
|
||||
});
|
||||
|
||||
test('gitUnstage unstages a file', () async {
|
||||
await File('${sandbox.path}/new.txt').writeAsString('x');
|
||||
await gitStage(sandbox, ['new.txt']);
|
||||
await gitUnstage(sandbox, ['new.txt']);
|
||||
final r = await Process.run(
|
||||
'git',
|
||||
['diff', '--cached', '--name-only'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
expect((r.stdout as String).trim(), isEmpty);
|
||||
});
|
||||
|
||||
test('gitCommit creates a commit', () async {
|
||||
await File('${sandbox.path}/c.txt').writeAsString('commit me');
|
||||
await gitStage(sandbox, ['c.txt']);
|
||||
final hash = await gitCommit(sandbox, 'test commit');
|
||||
expect(hash, hasLength(40));
|
||||
final r = await Process.run(
|
||||
'git',
|
||||
['log', '-1', '--format=%s'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
expect((r.stdout as String).trim(), 'test commit');
|
||||
});
|
||||
|
||||
test('gitCommit with nothing staged throws', () async {
|
||||
expect(
|
||||
() => gitCommit(sandbox, 'empty'),
|
||||
throwsA(isA<GitException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('gitLog returns entries', () async {
|
||||
final entries = await gitLog(sandbox);
|
||||
expect(entries, hasLength(1));
|
||||
expect(entries.first.subject, 'init');
|
||||
expect(entries.first.hash, hasLength(40));
|
||||
});
|
||||
|
||||
test('gitDiscard restores a file', () async {
|
||||
await File('${sandbox.path}/file.txt').writeAsString('changed');
|
||||
await gitDiscard(sandbox, ['file.txt']);
|
||||
final content = await File('${sandbox.path}/file.txt').readAsString();
|
||||
expect(content, 'hello\n');
|
||||
});
|
||||
|
||||
test('gitStash and gitStashPop round-trip', () async {
|
||||
await File('${sandbox.path}/file.txt').writeAsString('stashed');
|
||||
await gitStash(sandbox);
|
||||
var content = await File('${sandbox.path}/file.txt').readAsString();
|
||||
expect(content, 'hello\n');
|
||||
|
||||
await gitStashPop(sandbox);
|
||||
content = await File('${sandbox.path}/file.txt').readAsString();
|
||||
expect(content, 'stashed');
|
||||
});
|
||||
|
||||
test('gitCurrentBranch returns branch name', () async {
|
||||
final branch = await gitCurrentBranch(sandbox);
|
||||
expect(branch, isNotNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/src/git/status.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
late Directory sandbox;
|
||||
|
||||
setUp(() async {
|
||||
sandbox = await Directory.systemTemp.createTemp('clide-git-status-test-');
|
||||
await Process.run('git', ['init'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.email', 'test@test.com'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await Process.run(
|
||||
'git',
|
||||
['config', 'user.name', 'Test'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
// Initial commit so HEAD exists.
|
||||
await File('${sandbox.path}/.gitkeep').writeAsString('');
|
||||
await Process.run('git', ['add', '.'], workingDirectory: sandbox.path);
|
||||
await Process.run(
|
||||
'git',
|
||||
['commit', '-m', 'init'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
test('clean repo returns empty entries', () async {
|
||||
final status = await gitStatus(sandbox);
|
||||
expect(status.isClean, isTrue);
|
||||
expect(status.branch, isNotNull);
|
||||
});
|
||||
|
||||
test('untracked file appears in untracked', () async {
|
||||
await File('${sandbox.path}/new.txt').writeAsString('hello');
|
||||
final status = await gitStatus(sandbox);
|
||||
expect(status.untracked, hasLength(1));
|
||||
expect(status.untracked.first.path, 'new.txt');
|
||||
});
|
||||
|
||||
test('staged file appears in staged', () async {
|
||||
await File('${sandbox.path}/staged.txt').writeAsString('x');
|
||||
await Process.run(
|
||||
'git',
|
||||
['add', 'staged.txt'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
final status = await gitStatus(sandbox);
|
||||
expect(status.staged, hasLength(1));
|
||||
expect(status.staged.first.path, 'staged.txt');
|
||||
expect(status.staged.first.indexState, GitFileState.added);
|
||||
});
|
||||
|
||||
test('modified tracked file appears in unstaged', () async {
|
||||
await File('${sandbox.path}/.gitkeep').writeAsString('changed');
|
||||
final status = await gitStatus(sandbox);
|
||||
expect(status.unstaged, hasLength(1));
|
||||
expect(status.unstaged.first.workTreeState, GitFileState.modified);
|
||||
});
|
||||
|
||||
test('deleted file appears in unstaged', () async {
|
||||
await File('${sandbox.path}/.gitkeep').delete();
|
||||
final status = await gitStatus(sandbox);
|
||||
expect(status.unstaged, hasLength(1));
|
||||
expect(status.unstaged.first.workTreeState, GitFileState.deleted);
|
||||
});
|
||||
|
||||
test('file staged and then modified appears in both', () async {
|
||||
await File('${sandbox.path}/both.txt').writeAsString('v1');
|
||||
await Process.run(
|
||||
'git',
|
||||
['add', 'both.txt'],
|
||||
workingDirectory: sandbox.path,
|
||||
);
|
||||
await File('${sandbox.path}/both.txt').writeAsString('v2');
|
||||
final status = await gitStatus(sandbox);
|
||||
expect(status.staged.any((e) => e.path == 'both.txt'), isTrue);
|
||||
expect(status.unstaged.any((e) => e.path == 'both.txt'), isTrue);
|
||||
});
|
||||
|
||||
test('branch info is populated', () async {
|
||||
final status = await gitStatus(sandbox);
|
||||
expect(status.branch, isNotNull);
|
||||
expect(status.ahead, isZero);
|
||||
expect(status.behind, isZero);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user