Skip to content

Commit

Permalink
Find git dir by searching upwards
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Sep 14, 2023
1 parent 50f8e37 commit 1525023
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions sidekick_test/lib/src/local_testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import 'package:test/test.dart';
/// True when dependencies should be linked to local sidekick dependencies
final bool shouldUseLocalDeps = env['SIDEKICK_PUB_DEPS'] != 'true';

final String _gitRoot =
start('git rev-parse --show-toplevel', progress: Progress.capture())
.firstLine!;
final String _gitRoot = Directory.current
.findParent((dir) => dir.directory('.git').existsSync())!
.path;

/// Changes the sidekick_core dependency to a local override
void overrideSidekickCoreWithLocalPath(Directory package) {
Expand Down Expand Up @@ -245,3 +245,23 @@ String? _systemDartSdkPath() => _systemDartSdk()?.path;
String? _systemDartExecutable() =>
// /opt/homebrew/bin/dart
start('which dart', progress: Progress.capture(), nothrow: true).firstLine;

extension DirectoryExt on Directory {
/// Recursively goes up and tries to find a [Directory] matching [predicate]
///
/// Returns `null` when reaching root (/) without a match
Directory? findParent(bool Function(Directory dir) predicate) {
var dir = this;
while (true) {
if (predicate(dir)) {
return dir;
}
final parent = dir.parent;
if (canonicalize(dir.path) == canonicalize(parent.path)) {
// not found
return null;
}
dir = dir.parent;
}
}
}

0 comments on commit 1525023

Please sign in to comment.