-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlib.dart
337 lines (277 loc) · 9.83 KB
/
lib.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// ignore_for_file: avoid_print
import 'dart:async';
import 'dart:io';
import 'package:archive/archive_io.dart';
import 'package:async/async.dart' show NullStreamSink;
import 'package:path/path.dart' as p;
import 'package:process_run/process_run.dart';
import 'package:process_run/shell.dart' as shell;
import 'package:test/test.dart';
import 'package:dart_git/config.dart';
import 'package:dart_git/plumbing/git_hash.dart';
import 'package:dart_git/plumbing/objects/commit.dart';
import '../bin/main.dart' as git;
var inCI = Platform.environment["CI"] != null;
var silenceShellOutput = !inCI;
Future<String> runGitCommand(
String command,
String dir, {
Map<String, String> env = const {},
bool shouldReturnError = false,
bool throwOnError = false,
}) async {
var sink = NullStreamSink<List<int>>();
var results = await shell.run(
'git $command',
workingDirectory: dir,
includeParentEnvironment: false,
environment: env,
throwOnError: throwOnError,
// silence
stdout: silenceShellOutput ? sink : null,
stderr: silenceShellOutput ? sink : null,
);
expect(results.length, 1);
var r = results.first;
if (!shouldReturnError) {
expect(r.exitCode, 0);
} else {
expect(r.exitCode, isNot(0));
}
var stdout = results.map((e) => e.stdout).join('\n').trim();
var stderr = results.map((e) => e.stderr).join('\n').trim();
return '$stdout\n$stderr'.trim();
}
void createFile(String basePath, String path, String contents) {
var fullPath = p.join(basePath, path);
Directory(p.dirname(fullPath)).createSync(recursive: true);
File(fullPath).writeAsStringSync(contents);
}
Future<void> testRepoEquals(String repo1, String repo2) async {
if (!repo1.endsWith(p.separator)) {
repo1 += p.separator;
}
if (!repo2.endsWith(p.separator)) {
repo2 += p.separator;
}
// Test if all the objects are the same
var listObjScript = r'''#!/bin/bash
set -e
shopt -s nullglob extglob
cd "`git rev-parse --git-path objects`"
# packed objects
for p in pack/pack-*([0-9a-f]).idx ; do
git show-index < $p | cut -f 2 -d " "
done
# loose objects
for o in [0-9a-f][0-9a-f]/*([0-9a-f]) ; do
echo ${o/\/}
done''';
var script = p.join(Directory.systemTemp.path, 'list-objects');
File(script).writeAsStringSync(listObjScript);
var repo1Result =
await runExecutableArguments('bash', [script], workingDirectory: repo1);
var repo2Result =
await runExecutableArguments('bash', [script], workingDirectory: repo2);
var repo1Objects =
repo1Result.stdout.split('\n').where((String e) => e.isNotEmpty).toSet();
var repo2Objects =
repo2Result.stdout.split('\n').where((String e) => e.isNotEmpty).toSet();
expect(repo1Objects, repo2Objects, reason: 'Objects are different');
// Test if all the references are the same
var listRefScript = 'git show-ref --head';
script = p.join(Directory.systemTemp.path, 'list-refs');
File(script).writeAsStringSync(listRefScript);
repo1Result =
await runExecutableArguments('bash', [script], workingDirectory: repo1);
repo2Result =
await runExecutableArguments('bash', [script], workingDirectory: repo2);
var repo1Refs =
repo1Result.stdout.split('\n').where((String e) => e.isNotEmpty).toSet();
var repo2Refs =
repo2Result.stdout.split('\n').where((String e) => e.isNotEmpty).toSet();
expect(repo1Refs, repo2Refs, reason: 'Refs are different');
// Test if the index is the same
var listIndexScript = 'git ls-files --stage';
script = p.join(Directory.systemTemp.path, 'list-index');
File(script).writeAsStringSync(listIndexScript);
repo1Result =
await runExecutableArguments('bash', [script], workingDirectory: repo1);
repo2Result =
await runExecutableArguments('bash', [script], workingDirectory: repo2);
var repo1Index = repo1Result.stdout
.split('\n')
.where((String e) => e.isNotEmpty)
.toSet() as Set<String>?;
var repo2Index = repo2Result.stdout
.split('\n')
.where((String e) => e.isNotEmpty)
.toSet() as Set<String>?;
expect(repo1Index, repo2Index, reason: 'Index is different');
// Test if the config is the same
var config1Data = await File(p.join(repo1, '.git', 'config')).readAsString();
var config2Data = await File(p.join(repo2, '.git', 'config')).readAsString();
var config1 = ConfigFile.parse(config1Data);
var config2 = ConfigFile.parse(config2Data);
var c1 = config1.sections.where((s) => s.name != 'core' && s.name != 'user');
var c2 = config2.sections.where((s) => s.name != 'core' && s.name != 'user');
expect(c1, c2);
// Test if the working dir is the same
var repo1FsEntities = Directory(repo1).listSync(recursive: true).toList();
repo1FsEntities = repo1FsEntities
.where((e) => !e.path.startsWith(p.join(repo1, '.git/')))
.toList();
var repo2FsEntities = Directory(repo2).listSync(recursive: true).toList();
repo2FsEntities = repo2FsEntities
.where((e) => !e.path.startsWith(p.join(repo2, '.git/')))
.toList();
var repo1Files =
repo1FsEntities.map((f) => f.path.substring(repo1.length)).toSet();
var repo2Files =
repo2FsEntities.map((f) => f.path.substring(repo2.length)).toSet();
expect(repo1Files, repo2Files);
for (var ent in repo1FsEntities) {
var st = ent.statSync();
if (st.type != FileSystemEntityType.file) {
continue;
}
var path = ent.path.substring(repo1.length);
var repo1FilePath = p.join(repo1, path);
var repo2FilePath = p.join(repo2, path);
try {
var repo1File = File(repo1FilePath).readAsStringSync();
var repo2File = File(repo2FilePath).readAsStringSync();
expect(repo1File, repo2File, reason: '$path is different');
} catch (e) {
var repo1File = File(repo1FilePath).readAsBytesSync();
var repo2File = File(repo2FilePath).readAsBytesSync();
expect(repo1File, repo2File, reason: '$path is different');
}
}
// FIXME:
// Test if file/folder permissions are the same
}
Future<List<String>> runDartGitCommand(
String command,
String workingDir, {
Map<String, String> env = const {},
bool shouldReturnError = false,
}) async {
var printLog = <String>[];
if (!silenceShellOutput) {
print('dartgit>\$ git $command');
}
// Spawn an actual process as we can't set the env variables for a zone or isolate
if (env.isNotEmpty) {
var sink = NullStreamSink<List<int>>();
var results = await shell.run(
'${Directory.current.path}/bin/main.dart $command',
workingDirectory: workingDir,
includeParentEnvironment: true,
environment: env,
throwOnError: true,
// silence
stdout: silenceShellOutput ? sink : null,
stderr: silenceShellOutput ? sink : null,
);
var stdout = results.map((e) => e.stdout).join('\n').trim();
var stderr = results.map((e) => e.stderr).join('\n').trim();
return '$stdout\n$stderr'.trim().split('\n');
}
var spec = ZoneSpecification(print: (_, __, ___, String msg) {
printLog.add(msg);
});
var ret = await Zone.current.fork(specification: spec).run(() async {
assert(!command.contains('"') && !command.contains("'"));
int returnCode = 5000;
try {
returnCode = await git.mainWithExitCode(command.split(' '), workingDir);
} catch (e) {
printLog = ['$e'];
}
return returnCode;
});
expect(
ret,
isNot(5000),
reason: "Command ran with an exception. This shouldn't happen",
);
if (!shouldReturnError) {
expect(ret, 0, reason: 'Dart command `$command` failed in $workingDir');
} else {
expect(ret, isNot(0));
}
if (!silenceShellOutput) {
for (var log in printLog) {
print('dartgit> $log');
}
}
return printLog;
}
Future<void> copyDirectory(String source, String destination) async {
await Directory(destination).create(recursive: true);
await for (var entity in Directory(source).list(recursive: false)) {
if (entity is Directory) {
var newDirectory = Directory(p.join(
Directory(destination).absolute.path, p.basename(entity.path)));
await newDirectory.create();
await copyDirectory(entity.absolute.path, newDirectory.path);
} else if (entity is File) {
await entity.copy(p.join(destination, p.basename(entity.path)));
}
}
}
Future<String> openFixture(String filePath) async {
final bytes = await File(filePath).readAsBytes();
final gzipBytes = GZipDecoder().decodeBytes(bytes);
final archive = TarDecoder().decodeBytes(gzipBytes);
var gitDir = (await Directory.systemTemp.createTemp()).path;
var gitDotDir = p.join(gitDir, '.git');
for (var file in archive) {
var filename = file.name;
if (file.isFile) {
var data = file.content as List<int>;
File(p.join(gitDotDir, filename))
..createSync(recursive: true)
..writeAsBytesSync(data);
} else {
await Directory(p.join(gitDotDir, filename)).create(recursive: true);
}
}
return gitDir;
}
Future<String> cloneGittedFixture(String fixtureName, String newDirPath,
[GitHash? hash]) async {
var fixtureDirPath = 'test/data/$fixtureName';
assert(Directory(fixtureDirPath).existsSync());
assert(Directory('$fixtureDirPath/.gitted').existsSync());
await copyDirectory(fixtureDirPath, newDirPath);
assert(Directory('$newDirPath/.gitted').existsSync());
await Directory('$newDirPath/.gitted').rename('$newDirPath/.git');
await shell.run(
'git reset HEAD .',
workingDirectory: newDirPath,
includeParentEnvironment: false,
verbose: false,
);
if (hash != null) {
await shell.run(
'git checkout $hash',
workingDirectory: newDirPath,
includeParentEnvironment: false,
verbose: false,
);
}
return newDirPath;
}
extension GitIterable on Iterable<GitCommit> {
List<String> asHashStrings() {
var list = <String>[];
for (var commitR in this) {
var commit = commitR;
var hash = commit.hash.toString();
list.add(hash);
}
return list;
}
}