setup desktop: Get the remote's default branch

Instead of guessing
This commit is contained in:
Vishesh Handa
2021-07-13 17:55:43 +02:00
parent 4e1df9bc31
commit cd754e52b5
2 changed files with 67 additions and 1 deletions

View File

@ -50,6 +50,17 @@ Future<Result<void>> cloneRemote({
if (r.isFailure) { if (r.isFailure) {
return fail(r); return fail(r);
} }
var branchR = await gitDefaultBranchViaExecutable(
repoPath: repoPath,
privateKey: sshPrivateKey,
privateKeyPassword: sshPassword,
remoteName: remoteName,
);
if (r.isFailure) {
return fail(r);
}
remoteBranchName = branchR.getOrThrow();
} }
Log.i("Using remote branch: $remoteBranchName"); Log.i("Using remote branch: $remoteBranchName");

View File

@ -1,6 +1,7 @@
// GIT_SSH_COMMAND='ssh -i private_key_file -o IdentitiesOnly=yes' git clone user@host:repo.git // GIT_SSH_COMMAND='ssh -i private_key_file -o IdentitiesOnly=yes' git clone user@host:repo.git
import 'dart:io' show Directory, File, Process, ProcessStartMode; import 'dart:convert';
import 'dart:io';
import 'package:dart_git/utils/result.dart'; import 'package:dart_git/utils/result.dart';
@ -72,3 +73,57 @@ Future<Result<void>> _gitCommandViaExecutable({
return Result(null); return Result(null);
} }
// Default branch - git remote show origin | grep 'HEAD branch'
Future<Result<String>> gitDefaultBranchViaExecutable({
required String repoPath,
required String privateKey,
required String privateKeyPassword,
required String remoteName,
}) async {
assert(repoPath.startsWith('/'));
if (privateKeyPassword.isNotEmpty) {
var ex = Exception("SSH Keys with passwords are not supported");
return Result.fail(ex);
}
var dir = Directory.systemTemp.createTempSync();
var temp = File("${dir.path}/key");
temp.writeAsString(privateKey);
var process = await Process.start(
'git',
[
'remote',
'show',
remoteName,
],
workingDirectory: repoPath,
environment: {
'GIT_SSH_COMMAND': 'ssh -i ${temp.path} -o IdentitiesOnly=yes',
},
);
var exitCode = await process.exitCode;
await dir.delete(recursive: true);
if (exitCode != 0) {
var ex = Exception("Failed to fetch, exitCode: $exitCode");
return Result.fail(ex);
}
var stdoutB = <int>[];
await for (var d in process.stdout) {
stdoutB.addAll(d);
}
var stdout = utf8.decode(stdoutB);
for (var line in LineSplitter.split(stdout)) {
if (line.contains('HEAD branch:')) {
var branch = line.split(':')[1].trim();
return Result(branch);
}
}
var ex = Exception('Default Branch not found');
return Result.fail(ex);
}