Files
pipelines/app/lib/features/changes/domain/file_change.dart
T

118 lines
3.9 KiB
Dart

import '../../../core/api_gateway.dart';
import 'change_kind.dart';
import 'unified_patch.dart';
/// One recorded file change, as the server's `file_changes.files[]` entry
/// describes it. A null/blank [patch] is a recorded fact (binary, truncated or
/// no text diff), never an invented empty diff.
class FileChange {
FileChange({
required this.path,
required this.kind,
String? patch,
this.binary = false,
this.truncated = false,
String? beforeMode,
String? afterMode,
this.steps = const [],
ParsedPatch? parsedPatch,
}) : patch = _nonBlank(patch),
beforeMode = _nonBlank(beforeMode),
afterMode = _nonBlank(afterMode),
// Aggregate copies pass their already-parsed patch through instead of
// re-parsing the same recorded bytes for every stage that touched a path.
parsedPatch = parsedPatch ?? parseUnifiedPatch(patch);
final String path;
final ChangeKind kind;
/// The server's unified patch verbatim, or null when it recorded none. This
/// exact string is what Copy patch puts on the clipboard, including any
/// trailing newline or truncated final line.
final String? patch;
final bool binary;
final bool truncated;
final String? beforeMode;
final String? afterMode;
/// Stage names that recorded a change to this path, in run order and deduped.
/// Empty for a single recorded entry; filled by [runChanges] aggregation.
final List<String> steps;
final ParsedPatch parsedPatch;
int get added => parsedPatch.added;
int get removed => parsedPatch.removed;
/// True when a patch the operator can read is present.
bool get hasPatch => !binary && patch != null;
/// True when the recorded permissions differ.
bool get modeChanged =>
beforeMode != null && afterMode != null && beforeMode != afterMode;
/// True when the recorded permissions differ and no content change explains
/// it. The server records no text diff for a mode-only change.
bool get otherModeChange =>
modeChanged && !binary && added == 0 && removed == 0;
List<String> get tags => [
if (binary) 'binary',
if (truncated) 'truncated',
if (otherModeChange) 'mode change',
];
FileChange withSteps(List<String> value) => FileChange(
path: path,
kind: kind,
patch: patch,
binary: binary,
truncated: truncated,
beforeMode: beforeMode,
afterMode: afterMode,
steps: List.unmodifiable(value),
parsedPatch: parsedPatch,
);
/// Parses one server file entry; null when it has no usable path. The
/// capture owns the decision to skip such an entry, so it is never rendered
/// as an anonymous row.
static FileChange? tryParse(dynamic value) {
if (value is! Map) return null;
final json = Map<String, dynamic>.from(value);
final path = _text(json['path']);
if (path.trim().isEmpty) return null;
return FileChange(
path: path,
kind: changeKind(json['kind']),
patch: json['patch'] is String ? json['patch'] as String : null,
binary: json['binary'] == true,
truncated: json['truncated'] == true,
beforeMode: _textOrNull(json['before_mode']),
afterMode: _textOrNull(json['after_mode']),
);
}
/// Parses a required file entry. Integration code normally receives files
/// from [ChangeCapture] or [runChanges]; this constructor is for a viewer
/// opened directly with one recorded entry.
factory FileChange.fromJson(JsonMap json) {
final file = tryParse(json);
if (file == null) {
throw const FormatException('A recorded file change needs a path.');
}
return file;
}
}
/// A value kept as itself when present, else null (`?:` over nullable fields).
String? _textOrNull(dynamic value) => value == null ? null : '$value';
String _text(dynamic value) => '${value ?? ''}';
/// A string kept only when it has non-whitespace content.
String? _nonBlank(String? value) {
if (value == null) return null;
return value.trim().isEmpty ? null : value;
}