From 1bff307f5e4beea9e5b210f8589ff571d74c3e5a Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sat, 10 Apr 2021 21:44:39 +0200 Subject: [PATCH] Implement changing the branch --- lib/repository.dart | 70 +++++++++++++++++++++++++++- lib/screens/settings_git_remote.dart | 17 +++++-- lib/widgets/katex_widget.dart | 1 - 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/lib/repository.dart b/lib/repository.dart index 208bbaef..f19aef7d 100644 --- a/lib/repository.dart +++ b/lib/repository.dart @@ -43,6 +43,8 @@ class GitJournalRepo with ChangeNotifier { final String cacheDir; final String id; + String _currentBranch; + GitNoteRepository _gitRepo; NotesCache _notesCache; @@ -123,6 +125,7 @@ class GitJournalRepo with ChangeNotifier { remoteGitRepoConfigured: remoteConfigured, settings: settings, id: id, + currentBranch: await repo.currentBranch(), ); } @@ -133,9 +136,13 @@ class GitJournalRepo with ChangeNotifier { @required this.cacheDir, @required this.settings, @required this.remoteGitRepoConfigured, + @required String currentBranch, }) { _gitRepo = GitNoteRepository(gitDirPath: repoPath, settings: settings); notesFolder = NotesFolderFS(null, _gitRepo.gitDirPath, settings); + _currentBranch = currentBranch; + + Log.i("Branch $_currentBranch"); // Makes it easier to filter the analytics getAnalytics().setUserProperty( @@ -484,7 +491,68 @@ class GitJournalRepo with ChangeNotifier { Future> branches() async { var repo = await GitRepository.load(repoPath); - return repo.branches(); + var branches = Set.from(await repo.branches()); + if (repo.config.remotes.isNotEmpty) { + var remoteName = repo.config.remotes.first.name; + var remoteBranches = await repo.remoteBranches(remoteName); + branches.addAll(remoteBranches.map((e) { + return e.name.branchName(); + })); + } + return branches.toList()..sort(); + } + + String get currentBranch => _currentBranch; + + Future checkoutBranch(String branchName) async { + Log.i("Changing branch to $branchName"); + var repo = await GitRepository.load(repoPath); + + var created = await createBranchIfRequired(repo, branchName); + if (created.isEmpty) { + return ""; + } + + try { + await repo.checkoutBranch(branchName); + _currentBranch = branchName; + print("Done checking out $branchName"); + + await _notesCache.clear(); + notesFolder.reset(repoPath); + notifyListeners(); + + _loadNotes(); + } catch (e, st) { + print('maya hooo'); + print(e); + print(st); + } + return branchName; + } + + Future createBranchIfRequired(GitRepository repo, String name) async { + var localBranches = await repo.branches(); + if (localBranches.contains(name)) { + return name; + } + + if (repo.config.remotes.isEmpty) { + return ""; + } + var remoteConfig = repo.config.remotes.first; + var remoteBranches = await repo.remoteBranches(remoteConfig.name); + var remoteBranchRef = remoteBranches + .firstWhere((ref) => ref.name.branchName() == name, orElse: null); + if (remoteBranchRef == null) { + return ""; + } + + await repo.createBranch(name, hash: remoteBranchRef.hash); + await repo.setBranchUpstreamTo(name, remoteConfig, name); + + Log.i("Created branch $name"); + return name; } } diff --git a/lib/screens/settings_git_remote.dart b/lib/screens/settings_git_remote.dart index 8c986a87..e1961a95 100644 --- a/lib/screens/settings_git_remote.dart +++ b/lib/screens/settings_git_remote.dart @@ -30,6 +30,7 @@ class GitRemoteSettingsScreen extends StatefulWidget { class _GitRemoteSettingsScreenState extends State { String remoteHost; var branches = []; + var currentBranch = ""; @override Widget build(BuildContext context) { @@ -48,6 +49,7 @@ class _GitRemoteSettingsScreenState extends State { } if (branches.isEmpty) { + currentBranch = repo.currentBranch; repo.branches().then((list) { setState(() { if (!mounted) return; @@ -67,12 +69,17 @@ class _GitRemoteSettingsScreenState extends State { if (remoteHost != null && remoteHost.isNotEmpty) ListTile(title: Text(remoteHost)), if (branches.isNotEmpty) - Text( - tr('settings.gitRemote.branch'), - style: textTheme.bodyText1, - textAlign: TextAlign.left, + ListPreference( + title: tr('settings.gitRemote.branch'), + currentOption: currentBranch, // FIXME + options: branches, + onChange: (String branch) { + repo.checkoutBranch(branch); + setState(() { + currentBranch = branch; + }); + }, ), - if (branches.isNotEmpty) ListTile(title: Text(branches.first)), const SizedBox(height: 8.0), Text( tr('setup.sshKeyUserProvided.public'), diff --git a/lib/widgets/katex_widget.dart b/lib/widgets/katex_widget.dart index 95713e06..474cb8d3 100644 --- a/lib/widgets/katex_widget.dart +++ b/lib/widgets/katex_widget.dart @@ -7,7 +7,6 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:crypto/crypto.dart'; -import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; import 'package:mutex/mutex.dart'; import 'package:path/path.dart' as p;