resolveRemote method

  1. @override
Future resolveRemote()
override

By default, a generic PubEntry does not fetch a resolved URL.

Implementation

@override
Future resolveRemote() async {
  final repoUrl = description?.url;
  if (repoUrl == null) {
    print('Error: Git repository URL is not available for package $name.');
    return;
  }

  try {
    // Note: Examine in local git cache because ls-remote cannot
    // reliably determine the latest revision for repositories
    final home = Platform.environment['HOME'] ?? '';
    final pubCache = Platform.environment['PUB_CACHE'] ?? '$home/.pub-cache';

    final pkgName = packageName();
    final targetPath =
        '$pubCache/git/cache/$pkgName-${urlSha1 ?? checksum()}';

    if (!await Directory(targetPath).exists()) {
      print('Error: Git cache path does not exist: $targetPath');
      return;
    }

    final result = await Process.run('git', [
      '-C',
      targetPath,
      'for-each-ref',
      '--sort=-committerdate',
      '--format=%(objectname)',
      '--count=1',
      'refs/heads/',
    ]);

    if (result.exitCode == 0) {
      final output = result.stdout.toString().trim();
      if (output.isNotEmpty) {
        _gitRevision = output;
        return;
      } else {
        print('Error: No ref found in $targetPath');
        print('  Make sure to run \'pub get\' before running this');
      }
    } else {
      print('Error running git for-each-ref in $targetPath:');
      print('(exit code ${result.exitCode}): ${result.stderr}');
    }
  } catch (e) {
    print(
      'Exception while trying to get remote latest revision for $repoUrl: $e',
    );
    return;
  }
}