Change Git dart API

Use a class instead of a many functions. This way we avoid passing the
same arguments again and again.
This commit is contained in:
Vishesh Handa
2019-06-17 19:38:56 +02:00
parent b9cd8696c2
commit d653663a47
10 changed files with 158 additions and 176 deletions

View File

@ -17,11 +17,18 @@ Future<Directory> getGitBaseDirectory() async {
return Directory(path);
}
///
/// It will be clone in gitBaseDirectory/folderName
///
Future<void> gitClone(String cloneUrl, String folderName) async {
print("Going to git clone");
class GitRepo {
String folderName;
String authorName;
String authorEmail;
GitRepo({
@required this.folderName,
@required this.authorName,
@required this.authorEmail,
});
static Future<void> clone(String folderName, String cloneUrl) async {
try {
await _platform.invokeMethod('gitClone', {
'cloneUrl': cloneUrl,
@ -32,8 +39,93 @@ Future<void> gitClone(String cloneUrl, String folderName) async {
print("gitClone Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
return null;
static Future<void> init(String folderName) async {
try {
await _platform.invokeMethod('gitInit', {
'folderName': folderName,
});
} on PlatformException catch (e) {
print("gitInit Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
Future<void> pull() async {
try {
await _platform.invokeMethod('gitPull', {
'folderName': folderName,
'authorName': authorName,
'authorEmail': authorEmail,
});
print("Done");
} on PlatformException catch (e) {
print("gitPull Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
Future<void> add(String filePattern) async {
try {
await _platform.invokeMethod('gitAdd', {
'folderName': folderName,
'filePattern': filePattern,
});
} on PlatformException catch (e) {
print("gitAdd Failed: '${e.message}'.");
}
}
Future<void> rm(String filePattern) async {
try {
await _platform.invokeMethod('gitRm', {
'folderName': folderName,
'filePattern': filePattern,
});
} on PlatformException catch (e) {
print("gitRm Failed: '${e.message}'.");
}
}
Future<void> push() async {
try {
await _platform.invokeMethod('gitPush', {
'folderName': folderName,
});
} on PlatformException catch (e) {
print("gitPush Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
// FIXME: Change this method to just resetHard
Future<void> resetLast() async {
try {
await _platform.invokeMethod('gitResetLast', {
'folderName': folderName,
});
} on PlatformException catch (e) {
print("gitResetLast Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
// FIXME: Change the datetime
// FIXME: Actually implement the 'when'
Future<void> commit({@required String message, String when}) async {
try {
await _platform.invokeMethod('gitCommit', {
'folderName': folderName,
'authorName': authorName,
'authorEmail': authorEmail,
'message': message,
'when': when,
});
} on PlatformException catch (e) {
print("gitCommit Failed: '${e.message}'.");
}
}
}
Future<String> generateSSHKeys({@required String comment}) async {
@ -97,109 +189,3 @@ GitException createGitException(String msg) {
}
return GitException(msg);
}
Future gitPull({
String folderName,
String authorName,
String authorEmail,
}) async {
print("Going to git pull: $folderName");
try {
await _platform.invokeMethod('gitPull', {
'folderName': folderName,
'authorName': authorName,
'authorEmail': authorEmail,
});
print("Done");
} on PlatformException catch (e) {
print("gitPull Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
Future gitAdd(String gitFolder, String filePattern) async {
print("Going to git add: " + filePattern);
try {
await _platform.invokeMethod('gitAdd', {
'folderName': gitFolder,
'filePattern': filePattern,
});
print("Done");
} on PlatformException catch (e) {
print("gitAdd Failed: '${e.message}'.");
}
}
Future gitRm(String gitFolder, String filePattern) async {
print("Going to git rm");
try {
await _platform.invokeMethod('gitRm', {
'folderName': gitFolder,
'filePattern': filePattern,
});
print("Done");
} on PlatformException catch (e) {
print("gitRm Failed: '${e.message}'.");
}
}
Future gitPush(String folderName) async {
print("Going to git push");
try {
await _platform.invokeMethod('gitPush', {
'folderName': folderName,
});
print("Done");
} on PlatformException catch (e) {
print("gitPush Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
Future gitResetLast(String folderName) async {
print("Going to git reset last");
try {
await _platform.invokeMethod('gitResetLast', {
'folderName': folderName,
});
print("Done");
} on PlatformException catch (e) {
print("gitResetLast Failed: '${e.message}'.");
throw createGitException(e.message);
}
}
Future gitCommit({
@required String gitFolder,
@required String authorName,
@required String authorEmail,
@required String message,
String when,
}) async {
print("Going to git commit");
try {
await _platform.invokeMethod('gitCommit', {
'folderName': gitFolder,
'authorName': authorName,
'authorEmail': authorEmail,
'message': message,
'when': when,
});
print("Done");
} on PlatformException catch (e) {
print("gitCommit Failed: '${e.message}'.");
}
}
Future gitInit(String folderName) async {
print("Going to git init");
try {
await _platform.invokeMethod('gitInit', {
'folderName': folderName,
});
print("Done");
} on PlatformException catch (e) {
print("gitInit Failed: '${e.message}'.");
throw createGitException(e.message);
}
}

View File

@ -34,13 +34,14 @@ Future migrateGitRepo({
print("Migrating " + file.path + " --> " + toPath);
await file.copy(toPath);
await gitAdd(toGitBaseFolder, fileName);
await gitCommit(
gitFolder: toGitBaseFolder,
var gitRepo = GitRepo(
folderName: toGitBaseFolder,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
message: "Added Journal Entry",
);
await gitRepo.add(fileName);
await gitRepo.commit(message: "Added Journal Entry");
}
print("migrateGitRepo: Done");
}

View File

@ -9,9 +9,9 @@ abstract class GitHost {
Future launchOAuthScreen();
Future<UserInfo> getUserInfo();
Future<List<GitRepo>> listRepos();
Future<GitRepo> createRepo(String name);
Future<GitRepo> getRepo(String name);
Future<List<GitHostRepo>> listRepos();
Future<GitHostRepo> createRepo(String name);
Future<GitHostRepo> getRepo(String name);
Future addDeployKey(String sshPublicKey, String repo);
}
@ -27,11 +27,11 @@ class UserInfo {
});
}
class GitRepo {
class GitHostRepo {
String fullName;
String cloneUrl;
GitRepo({this.fullName, this.cloneUrl});
GitHostRepo({this.fullName, this.cloneUrl});
@override
String toString() {

View File

@ -75,7 +75,7 @@ class GitHub implements GitHost {
}
@override
Future<List<GitRepo>> listRepos() async {
Future<List<GitHostRepo>> listRepos() async {
if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode;
}
@ -93,7 +93,7 @@ class GitHub implements GitHost {
}
List<dynamic> list = jsonDecode(response.body);
var repos = <GitRepo>[];
var repos = <GitHostRepo>[];
list.forEach((dynamic d) {
var map = Map<String, dynamic>.from(d);
var repo = _repoFromJson(map);
@ -105,7 +105,7 @@ class GitHub implements GitHost {
}
@override
Future<GitRepo> createRepo(String name) async {
Future<GitHostRepo> createRepo(String name) async {
if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode;
}
@ -143,7 +143,7 @@ class GitHub implements GitHost {
}
@override
Future<GitRepo> getRepo(String name) async {
Future<GitHostRepo> getRepo(String name) async {
if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode;
}
@ -201,8 +201,8 @@ class GitHub implements GitHost {
return json.decode(response.body);
}
GitRepo _repoFromJson(Map<String, dynamic> parsedJson) {
return GitRepo(
GitHostRepo _repoFromJson(Map<String, dynamic> parsedJson) {
return GitHostRepo(
fullName: parsedJson['full_name'],
cloneUrl: parsedJson['ssh_url'],
);

View File

@ -63,7 +63,7 @@ class GitLab implements GitHost {
}
@override
Future<List<GitRepo>> listRepos() async {
Future<List<GitHostRepo>> listRepos() async {
if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode;
}
@ -82,7 +82,7 @@ class GitLab implements GitHost {
}
List<dynamic> list = jsonDecode(response.body);
var repos = <GitRepo>[];
var repos = <GitHostRepo>[];
list.forEach((dynamic d) {
var map = Map<String, dynamic>.from(d);
var repo = _repoFromJson(map);
@ -94,7 +94,7 @@ class GitLab implements GitHost {
}
@override
Future<GitRepo> createRepo(String name) async {
Future<GitHostRepo> createRepo(String name) async {
if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode;
}
@ -132,7 +132,7 @@ class GitLab implements GitHost {
}
@override
Future<GitRepo> getRepo(String name) async {
Future<GitHostRepo> getRepo(String name) async {
if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode;
}
@ -191,8 +191,8 @@ class GitLab implements GitHost {
return json.decode(response.body);
}
GitRepo _repoFromJson(Map<String, dynamic> parsedJson) {
return GitRepo(
GitHostRepo _repoFromJson(Map<String, dynamic> parsedJson) {
return GitHostRepo(
fullName: parsedJson['path_with_namespace'],
cloneUrl: parsedJson['ssh_url_to_repo'],
);

View File

@ -43,7 +43,7 @@ class JournalApp extends StatelessWidget {
if (localGitRepoConfigured == false) {
// FIXME: What about exceptions!
localGitRepoPath = "journal_local";
await gitInit(localGitRepoPath);
await GitRepo.init(localGitRepoPath);
localGitRepoConfigured = true;

View File

@ -40,6 +40,12 @@ class GitApp extends StatelessWidget {
}
List<Widget> _buildGitButtons() {
var gitRepo = GitRepo(
folderName: basePath,
authorName: "Vishesh Handa",
authorEmail: "noemail@example.com",
);
return <Widget>[
RaisedButton(
child: Text("Generate Keys"),
@ -50,7 +56,7 @@ class GitApp extends StatelessWidget {
child: Text("Git Clone"),
onPressed: () async {
try {
await gitClone(cloneUrl, basePath);
await GitRepo.clone(cloneUrl, basePath);
_sendSuccess();
} on GitException catch (ex) {
print(ex);
@ -61,32 +67,25 @@ class GitApp extends StatelessWidget {
RaisedButton(
child: Text("Git Pull"),
onPressed: () async {
gitPull(
folderName: basePath,
authorEmail: "noemail@example.com",
authorName: "Vishesh Handa",
);
gitRepo.pull();
},
),
RaisedButton(
child: Text("Git Add"),
onPressed: () async {
await gitAdd(basePath, ".");
gitRepo.add(".");
},
),
RaisedButton(
child: Text("Git Push"),
onPressed: () async {
gitPush(basePath);
gitRepo.push();
},
),
RaisedButton(
child: Text("Git Commit"),
onPressed: () async {
gitCommit(
gitFolder: basePath,
authorEmail: "noemail@example.com",
authorName: "Vishesh Handa",
gitRepo.commit(
message: "Default message from GitJournal",
when: "2017-10-20T01:21:10+02:00",
);
@ -95,7 +94,7 @@ class GitApp extends StatelessWidget {
RaisedButton(
child: Text("Git Reset Last"),
onPressed: () async {
gitResetLast(basePath);
gitRepo.resetLast();
},
),
RaisedButton(

View File

@ -49,7 +49,7 @@ class GitHostSetupAutoConfigureState extends State<GitHostSetupAutoConfigure> {
}
print("GitHost Initalized: " + widget.gitHostType.toString());
GitRepo repo;
GitHostRepo repo;
try {
setState(() {
_message = "Creating private repo";

View File

@ -380,7 +380,7 @@ class GitHostSetupScreenState extends State<GitHostSetupScreen> {
String error;
try {
await gitClone(_gitCloneUrl, "journal");
await GitRepo.clone(_gitCloneUrl, "journal");
} on GitException catch (e) {
error = e.cause;
}

View File

@ -13,6 +13,7 @@ class GitNoteRepository implements NoteRepository {
final FileStorage _fileStorage;
final String dirName;
final String subDirName;
final GitRepo _gitRepo;
bool cloned = false;
bool checkForCloned = false;
@ -24,6 +25,11 @@ class GitNoteRepository implements NoteRepository {
}) : _fileStorage = FileStorage(
noteSerializer: MarkdownYAMLSerializer(),
baseDirectory: p.join(baseDirectory, dirName, subDirName),
),
_gitRepo = GitRepo(
folderName: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
);
@override
@ -37,11 +43,8 @@ class GitNoteRepository implements NoteRepository {
return result;
}
await gitAdd(dirName, '.');
await gitCommit(
gitFolder: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
await _gitRepo.add(".");
await _gitRepo.commit(
message: commitMessage,
);
@ -59,11 +62,8 @@ class GitNoteRepository implements NoteRepository {
var baseDir = _fileStorage.baseDirectory;
var filePath = result.noteFilePath.replaceFirst(baseDir + "/", "");
await gitRm(dirName, filePath);
await gitCommit(
gitFolder: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
await _gitRepo.rm(filePath);
await _gitRepo.commit(
message: "Removed Journal entry",
);
@ -71,7 +71,7 @@ class GitNoteRepository implements NoteRepository {
}
Future<NoteRepoResult> resetLastCommit() async {
await gitResetLast(dirName);
await _gitRepo.resetLast();
return NoteRepoResult(error: false);
}
@ -88,17 +88,13 @@ class GitNoteRepository implements NoteRepository {
@override
Future<bool> sync() async {
try {
await gitPull(
folderName: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
);
await _gitRepo.pull();
} on GitException catch (ex) {
print(ex);
}
try {
await gitPush(dirName);
await _gitRepo.push();
} on GitException catch (ex) {
print(ex);
rethrow;