addFileToIndex method

Future<GitIndexEntry?> addFileToIndex(
  1. GitIndex index,
  2. String filePath
)

Implementation

Future<GitIndexEntry?> addFileToIndex(GitIndex index, String filePath) async {
  filePath = normalizePath(filePath);

  var file = fs.file(filePath);
  if (!file.existsSync()) {
    return null;
  }

  // Save that file as a blob
  var data = await file.readAsBytes();
  var blob = GitBlob(data, null);
  var hash = await objStorage.writeObject(blob);

  var pathSpec = filePath;
  if (pathSpec.startsWith(workTree)) {
    pathSpec = filePath.substring(workTree.length);
  }

  // Add it to the index
  var entry = index.entries.firstWhereOrNull((e) => e.path == pathSpec);
  var stat = await FileStat.stat(filePath);

  // Existing file
  if (entry != null) {
    entry.hash = hash;
    entry.fileSize = data.length;
    assert(data.length == stat.size);

    entry.cTime = stat.changed;
    entry.mTime = stat.modified;
    return entry;
  }

  // New file
  entry = GitIndexEntry.fromFS(pathSpec, stat, hash);
  index.entries.add(entry);
  return entry;
}