f02ff56dfe
show_html documents that declare dark support follow the app theme; others stay light. Tool description and skill docs say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
3.9 KiB
Dart
115 lines
3.9 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
import '../../changes/domain/change_capture.dart';
|
|
import '../../changes/domain/unified_patch.dart';
|
|
|
|
/// A `show_diff` item's comparison, as the server records it
|
|
/// (`internal/protocol/chat_shown.go` ChatShownDiffBody): a repository's work
|
|
/// tree, or HEAD when committed only, against HEAD or the merge base with a
|
|
/// base branch, with the same per-file changes a run records.
|
|
class ChatShownDiff {
|
|
const ChatShownDiff({
|
|
required this.repo,
|
|
required this.base,
|
|
required this.baseCommit,
|
|
required this.head,
|
|
this.headRevision = '',
|
|
this.paths = const [],
|
|
required this.committedOnly,
|
|
required this.files,
|
|
required this.additions,
|
|
required this.deletions,
|
|
required this.capture,
|
|
});
|
|
|
|
/// The diff of a `diff` item, or null for any other item or a malformed one.
|
|
static ChatShownDiff? of(JsonMap item) {
|
|
final body = item['diff'];
|
|
if (item['kind'] != 'diff' || body is! Map) return null;
|
|
final changes = body['changes'];
|
|
return ChatShownDiff(
|
|
repo: _text(body['repo']),
|
|
base: _text(body['base']),
|
|
baseCommit: _text(body['base_commit']),
|
|
head: _text(body['head']),
|
|
headRevision: _text(body['head_revision']),
|
|
paths: [
|
|
if (body['paths'] case final List paths)
|
|
for (final path in paths)
|
|
if (_text(path).isNotEmpty) _text(path),
|
|
],
|
|
committedOnly: body['committed_only'] == true,
|
|
files: _count(body['files']),
|
|
additions: _count(body['additions']),
|
|
deletions: _count(body['deletions']),
|
|
capture: item['expired'] == true || changes is! Map
|
|
? null
|
|
: ChangeCapture.fromJson(Map<String, dynamic>.from(changes)),
|
|
);
|
|
}
|
|
|
|
final String repo;
|
|
final String base;
|
|
final String baseCommit;
|
|
final String head;
|
|
|
|
/// The revision compared instead of the work tree (`show_diff` head), or
|
|
/// empty.
|
|
final String headRevision;
|
|
|
|
/// The paths or globs the comparison is limited to; empty for all.
|
|
final List<String> paths;
|
|
final bool committedOnly;
|
|
final int files;
|
|
final int additions;
|
|
final int deletions;
|
|
|
|
/// The per-file changes, or null once the server no longer retains them.
|
|
final ChangeCapture? capture;
|
|
|
|
/// The repository's folder name.
|
|
String get repoName {
|
|
final parts = repo.split('/').where((part) => part.isNotEmpty);
|
|
return parts.isEmpty ? repo : parts.last;
|
|
}
|
|
|
|
/// What was compared, in words: the branch (or the given revision) against
|
|
/// its base, whether uncommitted work is included, and the paths it is
|
|
/// limited to.
|
|
String get comparison {
|
|
final limited = paths.isEmpty
|
|
? ''
|
|
: ' · ${paths.length <= 3 ? paths.join(', ') : '${paths.take(3).join(', ')} +${paths.length - 3}'}';
|
|
return '${_compared()}$limited';
|
|
}
|
|
|
|
String _compared() {
|
|
final where = repoName.isEmpty ? '' : '$repoName · ';
|
|
if (headRevision.isNotEmpty) {
|
|
return '$where$headRevision vs ${base.isEmpty ? 'HEAD' : base}';
|
|
}
|
|
final head = this.head.isEmpty ? 'HEAD' : this.head;
|
|
final against = base.isEmpty
|
|
? (baseCommit.isEmpty ? 'no commits yet' : 'uncommitted changes')
|
|
: '$head vs $base${committedOnly ? ' · committed only' : ' · with uncommitted work'}';
|
|
return base.isEmpty ? '$where$head · $against' : '$where$against';
|
|
}
|
|
|
|
/// "3 files changed +12 -4", or "No changes".
|
|
String get heading {
|
|
if (files == 0) return 'No changes';
|
|
final stat = changeStat(additions, deletions);
|
|
return '${files == 1 ? '1 file' : '$files files'} changed'
|
|
'${stat.isEmpty ? '' : ' $stat'}';
|
|
}
|
|
|
|
/// True when files or patches were left out by the size limits.
|
|
bool get partial {
|
|
final capture = this.capture;
|
|
return capture != null && (!capture.complete || capture.omitted > 0);
|
|
}
|
|
}
|
|
|
|
String _text(Object? value) => '${value ?? ''}'.trim();
|
|
|
|
int _count(Object? value) => value is num && value > 0 ? value.toInt() : 0;
|