Skip to content

Commit a46fdef

Browse files
committed
index: Fix a bug introduced from making GitFileIndex immutable
1 parent b90e320 commit a46fdef

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lib/index.dart

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import 'package:collection/collection.dart' show IterableExtension;
21
import 'package:file/file.dart';
2+
import 'package:path/path.dart' as p;
33

44
import 'package:dart_git/dart_git.dart';
55
import 'package:dart_git/exceptions.dart';
@@ -13,7 +13,8 @@ extension Index on GitRepository {
1313

1414
var index = indexStorage.readIndex();
1515

16-
var stat = fs.statSync(pathSpec);
16+
var filePath = p.join(workTree, pathSpec);
17+
var stat = fs.statSync(filePath);
1718
if (stat.type == FileSystemEntityType.file) {
1819
addFileToIndex(index, pathSpec);
1920
} else if (stat.type == FileSystemEntityType.directory) {
@@ -42,34 +43,38 @@ extension Index on GitRepository {
4243
}
4344
// LB: Wait is this a linear search over all files??
4445
// Maybe... but omitting it fully does not speed things up.
45-
var entry = index.entries.firstWhereOrNull((e) => e.path == pathSpec);
46+
var ei = index.entries.indexWhere((e) => e.path == pathSpec);
4647
var stat = FileStat.statSync(filePath);
47-
if (entry != null &&
48-
entry.cTime.isAtSameMomentAs(stat.changed) &&
49-
entry.mTime.isAtSameMomentAs(stat.modified) &&
50-
entry.fileSize == stat.size) {
51-
// We assume it is the same file.
52-
return entry;
48+
if (ei != -1) {
49+
var entry = index.entries[ei];
50+
if (entry.cTime.isAtSameMomentAs(stat.changed) &&
51+
entry.mTime.isAtSameMomentAs(stat.modified) &&
52+
entry.fileSize == stat.size) {
53+
// We assume it is the same file.
54+
return entry;
55+
}
5356
}
5457

5558
var data = file.readAsBytesSync();
5659
var blob = GitBlob(data, null); // Hash the file (takes time!)
5760
var hash = objStorage.writeObject(blob);
5861

5962
// Existing file
60-
if (entry != null) {
63+
if (ei != -1) {
6164
assert(data.length == stat.size);
6265

63-
return entry.copyWith(
66+
var newEntry = index.entries[ei].copyWith(
6467
hash: hash,
6568
fileSize: data.length,
6669
cTime: stat.changed,
6770
mTime: stat.modified,
6871
);
72+
index.entries[ei] = newEntry;
73+
return newEntry;
6974
}
7075

7176
// New file
72-
entry = GitIndexEntry.fromFS(pathSpec, stat, hash);
77+
var entry = GitIndexEntry.fromFS(pathSpec, stat, hash);
7378
index.entries.add(entry);
7479
return entry;
7580
}

0 commit comments

Comments
 (0)