forked from dart-lang/pub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.dart
923 lines (826 loc) · 28.2 KB
/
git.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:pool/pool.dart';
import 'package:pub_semver/pub_semver.dart';
import '../exceptions.dart';
import '../git.dart' as git;
import '../io.dart';
import '../language_version.dart';
import '../log.dart' as log;
import '../package.dart';
import '../package_name.dart';
import '../pubspec.dart';
import '../source.dart';
import '../system_cache.dart';
import '../utils.dart';
import 'cached.dart';
import 'path.dart';
import 'root.dart';
/// A package source that gets packages from Git repos.
class GitSource extends CachedSource {
static GitSource instance = GitSource._();
GitSource._();
@override
final name = 'git';
@override
PackageRef parseRef(
String name,
Object? description, {
Description? containingDescription,
required LanguageVersion languageVersion,
}) {
String url;
String? ref;
String? path;
if (description is String) {
url = description;
} else if (description is! Map) {
throw const FormatException('The description must be a Git URL or a map '
"with a 'url' key.");
} else {
final descriptionUrl = description['url'];
if (descriptionUrl is! String) {
throw const FormatException(
"The 'url' field of a description must be a string.",
);
}
url = descriptionUrl;
final descriptionRef = description['ref'];
if (descriptionRef is! String?) {
throw const FormatException(
"The 'ref' field of the description must be a "
'string.');
}
ref = descriptionRef;
final descriptionPath = description['path'];
if (descriptionPath is! String?) {
throw const FormatException(
"The 'path' field of the description must be a "
'string.');
}
path = descriptionPath;
if (languageVersion.forbidsUnknownDescriptionKeys) {
for (final key in description.keys) {
if (!['url', 'ref', 'path'].contains(key)) {
throw FormatException(
'Unknown key "$key" in description.',
);
}
}
}
}
final containingDir = switch (containingDescription) {
RootDescription(path: final path) => path,
PathDescription(path: final path) => path,
_ => null,
};
return PackageRef(
name,
GitDescription(
url: url,
containingDir: containingDir,
ref: ref,
path: _validatedPath(path),
),
);
}
@override
PackageId parseId(
String name,
Version version,
Object? description, {
String? containingDir,
}) {
if (description is! Map) {
throw const FormatException("The description must be a map with a 'url' "
'key.');
}
final ref = description['ref'];
if (ref is! String?) {
throw const FormatException(
"The 'ref' field of the description must be a "
'string.');
}
final resolvedRef = description['resolved-ref'];
if (resolvedRef is! String) {
throw const FormatException("The 'resolved-ref' field of the description "
'must be a string.');
}
final url = description['url'];
if (url is! String) {
throw const FormatException("The 'url' field of the description "
'must be a string.');
}
return PackageId(
name,
version,
ResolvedGitDescription(
GitDescription(
url: url,
ref: ref,
path: _validatedPath(
description['path'],
),
containingDir: containingDir,
),
resolvedRef,
),
);
}
/// Throws a [FormatException] if [url] isn't a valid Git URL.
static _ValidatedUrl _validatedUrl(String url, String? containingDir) {
var relative = false;
// If the URL contains an @, it's probably an SSH hostname, which we don't
// know how to validate.
if (!url.contains('@')) {
// Otherwise, we use Dart's URL parser to validate the URL.
final parsed = Uri.parse(url);
if (!parsed.hasAbsolutePath) {
// Relative paths coming from pubspecs that are not on the local file
// system aren't allowed. This can happen if a hosted or git dependency
// has a git dependency.
if (containingDir == null) {
throw FormatException('"$url" is a relative path, but this '
'isn\'t a local pubspec.');
}
// A relative path is stored internally as absolute resolved relative to
// [containingPath].
relative = true;
url = p.url.normalize(
p.url.join(
p.toUri(p.absolute(containingDir)).toString(),
parsed.toString(),
),
);
}
}
return _ValidatedUrl(url, relative);
}
/// Normalizes [path].
///
/// Throws a [FormatException] if [path] isn't a [String] parsing as a
/// relative URL or `null`.
///
/// A relative url here has:
/// - non-absolute path
/// - no scheme
/// - no authority
String _validatedPath(dynamic path) {
path ??= '.';
if (path is! String) {
throw const FormatException(
"The 'path' field of the description must be a "
'string.');
}
// Use Dart's URL parser to validate the URL.
final parsed = Uri.parse(path);
if (parsed.hasAbsolutePath ||
parsed.hasScheme ||
parsed.hasAuthority ||
parsed.hasFragment ||
parsed.hasQuery) {
throw const FormatException(
"The 'path' field of the description must be a relative path URL.",
);
}
if (!p.url.isWithin('.', path) && !p.url.equals('.', path)) {
throw const FormatException(
"The 'path' field of the description must not reach outside the "
'repository.');
}
return p.url.normalize(parsed.toString());
}
/// Limit the number of concurrent git operations to 1.
// TODO(sigurdm): Use RateLimitedScheduler.
final Pool _pool = Pool(1);
/// A map from revision cache locations to futures that will complete once
/// they're finished being cloned.
///
/// This lets us avoid race conditions when getting multiple different
/// packages from the same repository.
final _revisionCacheClones = <String, Future<void>>{};
/// The paths to the canonical clones of repositories for which "git fetch"
/// has already been run during this run of pub.
final _updatedRepos = <String>{};
/// Given a Git repo that contains a pub package, gets the name of the pub
/// package.
///
/// Will download the repo to the system cache under the assumption that the
/// package will be downloaded afterwards.
Future<String> getPackageNameFromRepo(
String url,
String? ref,
String? path,
SystemCache cache, {
required String relativeTo,
}) async {
final description = GitDescription(
url: url,
ref: ref,
path: path,
containingDir: relativeTo,
);
return await _pool.withResource(() async {
await _ensureRepoCache(description, cache);
final path = _repoCachePath(description, cache);
final revision = await _firstRevision(path, description.ref);
final resolvedDescription = ResolvedGitDescription(description, revision);
return Pubspec.parse(
await _showFileAtRevision(resolvedDescription, 'pubspec.yaml', cache),
cache.sources,
containingDescription: description,
).name;
});
}
/// Lists the file as it is represented at the revision of
/// [resolvedDescription].
///
/// Assumes that revision is present in the cache already (can be done with
/// [_ensureRevision]).
Future<String> _showFileAtRevision(
ResolvedGitDescription resolvedDescription,
String pathInProject,
SystemCache cache,
) async {
final description = resolvedDescription.description;
// Normalize the path because Git treats "./" at the beginning of a path
// specially.
var pathInCache =
p.normalize(p.join(p.fromUri(description.path), pathInProject));
// Git doesn't recognize backslashes in paths, even on Windows.
if (Platform.isWindows) pathInCache = pathInCache.replaceAll('\\', '/');
final repoPath = _repoCachePath(description, cache);
final revision = resolvedDescription.resolvedRef;
try {
return await git.run(
[_gitDirArg(repoPath), 'show', '$revision:$pathInCache'],
workingDir: repoPath,
);
} on git.GitException catch (_) {
fail('Could not find a file named "$pathInCache" in '
'${GitDescription.prettyUri(description.url)} $revision.');
}
}
@override
Future<List<PackageId>> doGetVersions(
PackageRef ref,
Duration? maxAge,
SystemCache cache,
) async {
final description = ref.description;
if (description is! GitDescription) {
throw StateError('Called with wrong ref');
}
return await _pool.withResource(() async {
await _ensureRepoCache(description, cache);
final path = _repoCachePath(description, cache);
final revision = await _firstRevision(path, description.ref);
final pubspec = await _describeUncached(ref, revision, cache);
return [
PackageId(
ref.name,
pubspec.version,
ResolvedGitDescription(description, revision),
),
];
});
}
/// Since we don't have an easy way to read from a remote Git repo, this
/// just installs [id] into the system cache, then describes it from there.
@override
Future<Pubspec> describeUncached(PackageId id, SystemCache cache) {
final description = id.description;
if (description is! ResolvedGitDescription) {
throw StateError('Called with wrong ref');
}
return _pool.withResource(
() => _describeUncached(
id.toRef(),
description.resolvedRef,
cache,
),
);
}
/// Like [describeUncached], but takes a separate [ref] and Git [revision]
/// rather than a single ID.
Future<Pubspec> _describeUncached(
PackageRef ref,
String revision,
SystemCache cache,
) async {
final description = ref.description;
if (description is! GitDescription) {
throw ArgumentError('Wrong source');
}
await _ensureRevision(description, revision, cache);
return Pubspec.parse(
await _showFileAtRevision(
ResolvedGitDescription(description, revision),
'pubspec.yaml',
cache,
),
cache.sources,
expectedName: ref.name,
containingDescription: ref.description,
);
}
/// Clones a Git repo to the local filesystem.
///
/// The Git cache directory is a little idiosyncratic. At the top level, it
/// contains a directory for each commit of each repository, named
/// `<package name>-<commit hash>`. These are the canonical package
/// directories that are linked to from the `.dart_tool/package_config.json`
/// file.
///
/// In addition, the Git system cache contains a subdirectory named `cache/`
/// which contains a directory for each separate repository URL, named
/// `<package name>-<url hash>`. These are used to check out the repository
/// itself; each of the commit-specific directories are clones of a directory
/// in `cache/`.
@override
Future<DownloadPackageResult> downloadToSystemCache(
PackageId id,
SystemCache cache,
) async {
return await _pool.withResource(() async {
var didUpdate = false;
final ref = id.toRef();
final description = ref.description;
if (description is! GitDescription) {
throw ArgumentError('Wrong source');
}
if (!git.isInstalled) {
fail('Cannot get ${id.name} from Git (${description.url}).\n'
'Please ensure Git is correctly installed.');
}
ensureDir(p.join(cache.rootDirForSource(this), 'cache'));
final resolvedRef =
(id.description as ResolvedGitDescription).resolvedRef;
didUpdate |= await _ensureRevision(description, resolvedRef, cache);
final revisionCachePath = _revisionCachePath(id, cache);
final path = description.path;
await _revisionCacheClones.putIfAbsent(revisionCachePath, () async {
if (!entryExists(revisionCachePath)) {
await _cloneViaTemp(
_repoCachePath(description, cache),
revisionCachePath,
cache,
);
await _checkOut(revisionCachePath, resolvedRef);
_writePackageList(revisionCachePath, [path]);
didUpdate = true;
} else {
didUpdate |= _updatePackageList(revisionCachePath, path);
}
});
return DownloadPackageResult(id, didUpdate: didUpdate);
});
}
/// Returns the path to the revision-specific cache of [id].
@override
String getDirectoryInCache(PackageId id, SystemCache cache) {
final description = id.toRef().description;
if (description is! GitDescription) {
throw ArgumentError('Wrong source');
}
return p.join(_revisionCachePath(id, cache), description.path);
}
@override
List<Package> getCachedPackages(SystemCache cache) {
// TODO(keertip): Implement getCachedPackages().
throw UnimplementedError(
"The git source doesn't support listing its cached packages yet.",
);
}
/// Resets all cached packages back to the pristine state of the Git
/// repository at the revision they are pinned to.
@override
Future<Iterable<RepairResult>> repairCachedPackages(SystemCache cache) async {
final rootDir = cache.rootDirForSource(this);
if (!dirExists(rootDir)) return [];
final result = <RepairResult>[];
final packages = listDir(rootDir)
.where((entry) => dirExists(p.join(entry, '.git')))
.expand((revisionCachePath) {
return _readPackageList(revisionCachePath).map((relative) {
// If we've already failed to load another package from this
// repository, ignore it.
if (!dirExists(revisionCachePath)) return null;
final packageDir = p.join(revisionCachePath, relative);
try {
return Package.load(
packageDir,
loadPubspec: Pubspec.loadRootWithSources(cache.sources),
);
} catch (error, stackTrace) {
log.error('Failed to load package', error, stackTrace);
final name = p.basename(revisionCachePath).split('-').first;
result.add(
RepairResult(name, Version.none, this, success: false),
);
tryDeleteEntry(revisionCachePath);
return null;
}
});
})
.nonNulls
.toList();
// Note that there may be multiple packages with the same name and version
// (pinned to different commits). The sort order of those is unspecified.
packages.sort(Package.orderByNameAndVersion);
for (var package in packages) {
// If we've already failed to repair another package in this repository,
// ignore it.
if (!dirExists(package.dir)) continue;
log.message('Resetting Git repository for '
'${log.bold(package.name)} ${package.version}...');
try {
// Remove all untracked files.
await git
.run(['clean', '-d', '--force', '-x'], workingDir: package.dir);
// Discard all changes to tracked files.
await git.run(['reset', '--hard', 'HEAD'], workingDir: package.dir);
result.add(
RepairResult(package.name, package.version, this, success: true),
);
} on git.GitException catch (error, stackTrace) {
log.error('Failed to reset ${log.bold(package.name)} '
'${package.version}. Error:\n$error');
log.fine(stackTrace.toString());
result.add(
RepairResult(package.name, package.version, this, success: false),
);
// Delete the revision cache path, not the subdirectory that contains
// the package.
final repoRoot = git.repoRoot(package.dir);
if (repoRoot != null) tryDeleteEntry(repoRoot);
}
}
return result;
}
/// Ensures that the canonical clone of the repository referred to by
/// [description] contains the given Git [revision].
Future<bool> _ensureRevision(
GitDescription description,
String revision,
SystemCache cache,
) async {
final path = _repoCachePath(description, cache);
if (_updatedRepos.contains(path)) return false;
await _deleteGitRepoIfInvalid(path);
if (!entryExists(path)) await _createRepoCache(description, cache);
// Try to list the revision. If it doesn't exist, git will fail and we'll
// know we have to update the repository.
try {
await _firstRevision(path, revision);
} on git.GitException catch (_) {
await _updateRepoCache(description, cache);
return true;
}
return false;
}
/// Ensures that the canonical clone of the repository referred to by
/// [description] exists and is up-to-date.
///
/// Returns `true` if it had to update anything.
Future<bool> _ensureRepoCache(
GitDescription description,
SystemCache cache,
) async {
final path = _repoCachePath(description, cache);
if (_updatedRepos.contains(path)) return false;
await _deleteGitRepoIfInvalid(path);
if (!entryExists(path)) {
await _createRepoCache(description, cache);
return true;
} else {
return await _updateRepoCache(description, cache);
}
}
/// Creates the canonical clone of the repository referred to by
/// [description].
///
/// This assumes that the canonical clone doesn't yet exist.
Future<void> _createRepoCache(
GitDescription description,
SystemCache cache,
) async {
final path = _repoCachePath(description, cache);
assert(!_updatedRepos.contains(path));
try {
await _cloneViaTemp(description.url, path, cache, mirror: true);
} catch (_) {
await _deleteGitRepoIfInvalid(path);
rethrow;
}
_updatedRepos.add(path);
}
/// Runs "git fetch" in the canonical clone of the repository referred to by
/// [description].
///
/// This assumes that the canonical clone already exists.
///
/// Returns `true` if it had to update anything.
Future<bool> _updateRepoCache(
GitDescription description,
SystemCache cache,
) async {
final path = _repoCachePath(description, cache);
if (_updatedRepos.contains(path)) return false;
await git.run([_gitDirArg(path), 'fetch'], workingDir: path);
_updatedRepos.add(path);
return true;
}
/// Clean-up [dirPath] if it's an invalid git repository.
///
/// The git clones in the `PUB_CACHE` folder should never be invalid. But this
/// can happen if the clone operation failed in some way, and the program did
/// not exit gracefully, leaving the cache git clone in a dirty state.
Future<void> _deleteGitRepoIfInvalid(String dirPath) async {
if (!dirExists(dirPath)) {
return;
}
var isValid = true;
try {
final result = await git.run(
[_gitDirArg(dirPath), 'rev-parse', '--is-inside-git-dir'],
workingDir: dirPath,
);
if (result.trim() != 'true') {
isValid = false;
}
} on git.GitException {
isValid = false;
}
// If [dirPath] is not a valid git repository we remove it.
if (!isValid) {
deleteEntry(dirPath);
}
}
/// Updates the package list file in [revisionCachePath] to include [path], if
/// necessary.
///
/// Returns `true` if it had to update anything.
bool _updatePackageList(String revisionCachePath, String path) {
final packages = _readPackageList(revisionCachePath);
if (packages.contains(path)) return false;
_writePackageList(revisionCachePath, packages..add(path));
return true;
}
/// Returns the list of packages in [revisionCachePath].
List<String> _readPackageList(String revisionCachePath) {
final path = _packageListPath(revisionCachePath);
// If there's no package list file, this cache was created by an older
// version of pub where pubspecs were only allowed at the root of the
// repository.
if (!fileExists(path)) return ['.'];
return readTextFile(path).split('\n');
}
/// Writes a package list indicating that [packages] exist in
/// [revisionCachePath].
void _writePackageList(String revisionCachePath, List<String> packages) {
writeTextFile(_packageListPath(revisionCachePath), packages.join('\n'));
}
/// The path in a revision cache repository in which we keep a list of the
/// packages in the repository.
String _packageListPath(String revisionCachePath) =>
p.join(revisionCachePath, '.git/pub-packages');
/// Runs "git rev-list" on [reference] in [path] and returns the first result.
///
/// This assumes that the canonical clone already exists.
Future<String> _firstRevision(String path, String reference) async {
final String output;
try {
output = (await git.run(
[_gitDirArg(path), 'rev-list', '--max-count=1', reference],
workingDir: path,
))
.trim();
} on git.GitException catch (e) {
throw PackageNotFoundException(
"Could not find git ref '$reference' (${e.stderr})",
);
}
if (output.isEmpty) {
throw PackageNotFoundException("Could not find git ref '$reference'.");
}
return output;
}
/// Clones the repo at the URI [from] to the path [to] on the local
/// filesystem.
///
/// If [mirror] is true, creates a bare, mirrored clone. This doesn't check
/// out the working tree, but instead makes the repository a local mirror of
/// the remote repository. See the manpage for `git clone` for more
/// information.
Future<void> _clone(
String from,
String to, {
bool mirror = false,
}) async {
// Git on Windows does not seem to automatically create the destination
// directory.
ensureDir(to);
final args = ['clone', if (mirror) '--mirror', from, to];
await git.run(args);
}
/// Like [_clone], but clones to a temporary directory (inside the [cache])
/// and moves
Future<void> _cloneViaTemp(
String from,
String to,
SystemCache cache, {
bool mirror = false,
}) async {
final tempDir = cache.createTempDir();
try {
await _clone(from, tempDir, mirror: mirror);
} catch (_) {
deleteEntry(tempDir);
rethrow;
}
// Now that the clone has succeeded, move it to the real location in the
// cache.
//
// If this fails with a "directory not empty" exception we assume that
// another pub process has installed the same package version while we
// cloned. In that case [tryRenameDir] will delete the folder for us.
tryRenameDir(tempDir, to);
}
/// Checks out the reference [ref] in [repoPath].
Future<void> _checkOut(String repoPath, String ref) {
return git
.run(['checkout', ref], workingDir: repoPath).then((result) => null);
}
String _revisionCachePath(PackageId id, SystemCache cache) => p.join(
cache.rootDirForSource(this),
'${_repoName(id.description.description as GitDescription)}-'
'${(id.description as ResolvedGitDescription).resolvedRef}',
);
/// Returns the path to the canonical clone of the repository referred to by
/// [description] (the one in `<system cache>/git/cache`).
String _repoCachePath(GitDescription description, SystemCache cache) {
final repoCacheName = '${_repoName(description)}-${sha1(description.url)}';
return p.join(cache.rootDirForSource(this), 'cache', repoCacheName);
}
/// Returns a short, human-readable name for the repository URL in
/// [description].
///
/// This name is not guaranteed to be unique.
String _repoName(GitDescription description) {
var name = p.url.basename(description.url);
if (name.endsWith('.git')) {
name = name.substring(0, name.length - '.git'.length);
}
name = name.replaceAll(RegExp('[^a-zA-Z0-9._-]'), '_');
// Shorten name to 50 chars for sanity.
if (name.length > 50) {
name = name.substring(0, 50);
}
return name;
}
}
class GitDescription extends Description {
/// The url of the repo of this package.
///
/// If the url was relative in the pubspec.yaml it will be resolved relative
/// to the pubspec location, and stored here as an absolute file url, and
/// [relative] will be true.
///
/// This will not always parse as a [Uri] due the fact that `Uri.parse` does
/// not allow strings of the form: '[email protected]:dart-lang/pub.git'.
final String url;
/// `true` if [url] was parsed from a relative url.
final bool relative;
/// The git ref to resolve for finding the commit.
final String ref;
/// Relative path of the package inside the git repo.
///
/// Represented as a relative url.
final String path;
GitDescription.raw({
required this.url,
required this.relative,
required String? ref,
required String? path,
}) : ref = ref ?? 'HEAD',
path = path ?? '.';
factory GitDescription({
required String url,
required String? ref,
required String? path,
required String? containingDir,
}) {
final validatedUrl = GitSource._validatedUrl(url, containingDir);
return GitDescription.raw(
url: validatedUrl.url,
relative: validatedUrl.wasRelative,
ref: ref,
path: path,
);
}
@override
String format() {
var result = '${prettyUri(url)} at '
'$ref';
if (path != '.') result += ' in $path';
return result;
}
@override
Object? serializeForPubspec({
required String? containingDir,
required LanguageVersion languageVersion,
}) {
final relativeUrl = containingDir != null && relative
? p.url.relative(
url,
from: p.toUri(p.normalize(p.absolute(containingDir))).toString(),
)
: url;
if (ref == 'HEAD' && path == '.') return relativeUrl;
return {
'url': relativeUrl,
if (ref != 'HEAD') 'ref': ref,
if (path != '.') 'path': path,
};
}
@override
GitSource get source => GitSource.instance;
@override
bool operator ==(Object other) {
return other is GitDescription &&
other.url == url &&
other.ref == ref &&
other.path == path;
}
GitDescription withRef(String newRef) => GitDescription.raw(
url: url,
relative: relative,
ref: newRef,
path: path,
);
@override
int get hashCode => Object.hash(url, ref, path);
// Similar in intend to [p.prettyUri] but does not fail if the input doesn't
// parse with [Uri.parse].
static String prettyUri(String url) {
// HACK: Working around the fact that `Uri.parse` does not allow strings of
// the form: '[email protected]:dart-lang/pub.git'.
final parsedAsUri = Uri.tryParse(url);
if (parsedAsUri == null) {
return url;
}
return p.prettyUri(url);
}
}
class ResolvedGitDescription extends ResolvedDescription {
@override
GitDescription get description => super.description as GitDescription;
final String resolvedRef;
ResolvedGitDescription(GitDescription super.description, this.resolvedRef);
@override
String format() {
var result = '${GitDescription.prettyUri(description.url)} at '
'${resolvedRef.substring(0, 6)}';
if (description.path != '.') result += ' in ${description.path}';
return result;
}
@override
Object? serializeForLockfile({required String? containingDir}) {
final url = description.relative && containingDir != null
? p.url.relative(
description.url,
from: Uri.file(p.absolute(containingDir)).toString(),
)
: description.url;
return {
'url': url,
'ref': description.ref,
'resolved-ref': resolvedRef,
'path': description.path,
};
}
@override
bool operator ==(Object other) {
return other is ResolvedGitDescription &&
other.description == description &&
other.resolvedRef == resolvedRef;
}
@override
int get hashCode => Object.hash(description, resolvedRef);
}
class _ValidatedUrl {
final String url;
final bool wasRelative;
_ValidatedUrl(this.url, this.wasRelative);
}
String _gitDirArg(String path) {
path = p.absolute(path);
final forwardSlashPath =
Platform.isWindows ? path.replaceAll('\\', '/') : path;
return '--git-dir=$forwardSlashPath';
}