/
git_desktop.dart
134 lines (116 loc) · 3.43 KB
/
git_desktop.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// GIT_SSH_COMMAND='ssh -i private_key_file -o IdentitiesOnly=yes' git clone user@host:repo.git
import 'dart:convert';
import 'dart:io';
import 'package:dart_git/utils/file_extensions.dart';
import 'package:dart_git/utils/result.dart';
import 'package:gitjournal/utils/logger.dart';
Future<Result<void>> gitFetchViaExecutable({
required String repoPath,
required String privateKey,
required String privateKeyPassword,
required String remoteName,
}) =>
_gitCommandViaExecutable(
repoPath: repoPath,
privateKey: privateKey,
privateKeyPassword: privateKeyPassword,
remoteName: remoteName,
command: 'fetch',
);
Future<Result<void>> gitPushViaExecutable({
required String repoPath,
required String privateKey,
required String privateKeyPassword,
required String remoteName,
}) =>
_gitCommandViaExecutable(
repoPath: repoPath,
privateKey: privateKey,
privateKeyPassword: privateKeyPassword,
remoteName: remoteName,
command: 'push',
);
Future<Result<void>> _gitCommandViaExecutable({
required String repoPath,
required String privateKey,
required String privateKeyPassword,
required String remoteName,
required String command,
}) 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");
await temp.writeAsString(privateKey);
await temp.chmod(int.parse('0600', radix: 8));
Log.i("Running git $command $remoteName");
var process = await Process.start(
'git',
[
command,
remoteName,
],
workingDirectory: repoPath,
environment: {
'GIT_SSH_COMMAND': 'ssh -i ${temp.path} -o IdentitiesOnly=yes',
},
mode: ProcessStartMode.inheritStdio,
);
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);
}
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);
}