Fix setup git host failing

Outlined every case properly and tested it thoroughly. If this still
fails I am a terrible programmer, and should learn to isolate components
and write through tests.

Fixes   
This commit is contained in:
Vishesh Handa
2021-01-22 10:50:20 +01:00
parent 2199cd2a3a
commit a60e2bbf2a
4 changed files with 66 additions and 30 deletions

@ -11,11 +11,11 @@ Future<void> cloneRemote({
@required String sshPublicKey,
@required String sshPrivateKey,
@required String sshPassword,
@required String authorName,
@required String authorEmail,
}) async {
var repo = await GitRepository.load(repoPath);
// FIXME: In the case of no remotes and no commits, revert to clone?
var remote = await repo.addOrUpdateRemote(remoteName, cloneUrl);
var _gitRepo = git_bindings.GitRepo(folderPath: repoPath);
@ -26,28 +26,21 @@ Future<void> cloneRemote({
password: sshPassword,
);
var remoteBranchName = "master";
try {
var branch = await _gitRepo.defaultBranch(
remote: remoteName,
publicKey: sshPublicKey,
privateKey: sshPrivateKey,
password: sshPassword,
);
Log.i("Got default branch: $branch");
if (branch != null && branch.isNotEmpty) {
remoteBranchName = branch;
}
} catch (ex) {
Log.w("Could not get git main branch - assuming master", ex: ex);
}
var remoteBranch = await repo.remoteBranch(remoteName, remoteBranchName);
var remoteBranchName = await _remoteDefaultBranch(
repo: repo,
libGit2Repo: _gitRepo,
remoteName: remoteName,
sshPublicKey: sshPublicKey,
sshPrivateKey: sshPrivateKey,
sshPassword: sshPassword,
);
Log.i("Using remote branch: $remoteBranchName");
var branches = await repo.branches();
if (branches.isEmpty) {
Log.i("Completing - no local branch");
var remoteBranch = await repo.remoteBranch(remoteName, remoteBranchName);
if (remoteBranchName != null &&
remoteBranchName.isNotEmpty &&
remoteBranch != null) {
@ -61,7 +54,14 @@ Future<void> cloneRemote({
Log.i("Completing - localBranch: $branch");
await repo.setUpstreamTo(remote, remoteBranchName);
await _gitRepo.merge();
var remoteBranch = await repo.remoteBranch(remoteName, remoteBranchName);
if (remoteBranch != null) {
await _gitRepo.merge(
branch: '$remoteName/$remoteBranchName',
authorName: authorName,
authorEmail: authorEmail,
);
}
} else {
Log.i("Completing - localBranch diff remote: $branch $remoteBranchName");
@ -69,14 +69,48 @@ Future<void> cloneRemote({
await repo.checkoutBranch(remoteBranchName, headRef.hash);
await repo.deleteBranch(branch);
await repo.setUpstreamTo(remote, remoteBranchName);
await _gitRepo.merge();
await _gitRepo.merge(
branch: '$remoteName/$remoteBranchName',
authorName: authorName,
authorEmail: authorEmail,
);
}
// if more than one branch
// TODO: Check if one of the branches matches the remote branch name
// and use that
// if not, then just create a new branch with the remoteBranchName
// and merge ..
}
}
Future<String> _remoteDefaultBranch({
@required GitRepository repo,
@required git_bindings.GitRepo libGit2Repo,
@required String remoteName,
@required String sshPublicKey,
@required String sshPrivateKey,
@required String sshPassword,
}) async {
try {
var branch = await libGit2Repo.defaultBranch(
remote: remoteName,
publicKey: sshPublicKey,
privateKey: sshPrivateKey,
password: sshPassword,
);
Log.i("Got default branch: $branch");
if (branch != null && branch.isNotEmpty) {
return branch;
}
} catch (ex) {
Log.w("Could not fetch git main branch", ex: ex);
}
var remoteBranch = await repo.guessRemoteHead(remoteName);
if (remoteBranch == null) {
return 'master';
}
return remoteBranch.target.branchName();
}
// Test Cases -
// * New Repo, No Local Changes
// * New Repo, Existing Changes
// * Existing Repo (master default), No Local Changes
// * Existing Repo (master default), Local changes in 'master' branch
// * Existing Repo (main default), Local changes in 'master' branch

@ -540,6 +540,8 @@ class GitHostSetupScreenState extends State<GitHostSetupScreen> {
sshPassword: settings.sshPassword,
sshPrivateKey: settings.sshPrivateKey,
sshPublicKey: settings.sshPublicKey,
authorEmail: settings.gitAuthorEmail,
authorName: settings.gitAuthor,
);
} on Exception catch (e, stacktrace) {
Log.e("Failed to clone", ex: e, stacktrace: stacktrace);

@ -458,7 +458,7 @@ packages:
description:
path: "."
ref: HEAD
resolved-ref: b795b657863ea152618b58b73b9af1922c96de74
resolved-ref: abd0c78e4de7cfb3d3300ede4ca811d98252117c
url: "https://github.com/GitJournal/git_bindings.git"
source: git
version: "0.0.18"

@ -10,7 +10,7 @@ dependencies:
dart_git:
git: https://github.com/GitJournal/dart-git.git
#path: /Users/vishesh/src/gitjournal/dart-git
git_bindings: #^0.0.18
git_bindings:
#path: /Users/vishesh/src/gitjournal/git_bindings
git: https://github.com/GitJournal/git_bindings.git
gotrue: