From c34b329852748f2a6c83dcd74f0766e572c34b2a Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 20 Oct 2020 14:07:27 +0200 Subject: [PATCH] Handle setting up remote with a blank repo This isn't perfect as we're guessing the remote's main branch, but it'll do. We need this as I'm no longer ever calling 'git clone'. It's always a fetch. The good part is that this uses dart_git a lot :) --- lib/state_container.dart | 30 +++++++++++++++++++++++++++--- pubspec.lock | 2 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/state_container.dart b/lib/state_container.dart index a936fc4c..541584f3 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -329,11 +329,35 @@ class StateContainer with ChangeNotifier { () async { var repo = await GitRepository.load(_gitRepo.gitDirPath); var remote = repo.config.remote(remoteName); - var remoteBranchName = await repo.guessRemoteHead(remoteName); + var remoteBranch = await repo.guessRemoteHead(remoteName); + var remoteBranchName = remoteBranch.name.branchName(); - await repo.setUpstreamTo(remote, remoteBranchName); + var branches = await repo.branches(); + if (branches.isEmpty) { + assert(remoteBranch.isHash); + await repo.checkoutBranch(remoteBranchName, remoteBranch.hash); + await repo.setUpstreamTo(remote, remoteBranchName); + } else { + var branch = branches[0]; - await _gitRepo.merge(); + if (branch == remoteBranchName) { + await repo.setUpstreamTo(remote, remoteBranchName); + await _gitRepo.merge(); + } else { + var headRef = await repo.resolveReference(await repo.head()); + await repo.checkoutBranch(remoteBranchName, headRef.hash); + await repo.deleteBranch(branch); + await repo.setUpstreamTo(remote, remoteBranchName); + await _gitRepo.merge(); + } + + // 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 .. + + } await _persistConfig(); _loadNotes(); diff --git a/pubspec.lock b/pubspec.lock index f90c4303..967a130d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -181,7 +181,7 @@ packages: description: path: "." ref: HEAD - resolved-ref: "09bc129de57f8c179305a99ca1b9a885a4807c3f" + resolved-ref: "156ac4e589cf8a67c126e5c7c18f6ca76c6c232e" url: "https://github.com/GitJournal/dart_git.git" source: git version: "0.0.1"