| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library git_source; | 5 library git_source; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'git.dart' as git; | 8 import 'git.dart' as git; |
| 9 import 'io.dart'; | 9 import 'io.dart'; |
| 10 import 'package.dart'; | 10 import 'package.dart'; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 Future<Package> installToSystemCache(PackageId id) { | 35 Future<Package> installToSystemCache(PackageId id) { |
| 36 var revisionCachePath; | 36 var revisionCachePath; |
| 37 | 37 |
| 38 return git.isInstalled.then((installed) { | 38 return git.isInstalled.then((installed) { |
| 39 if (!installed) { | 39 if (!installed) { |
| 40 throw new Exception( | 40 throw new Exception( |
| 41 "Cannot install '${id.name}' from Git (${_getUrl(id)}).\n" | 41 "Cannot install '${id.name}' from Git (${_getUrl(id)}).\n" |
| 42 "Please ensure Git is correctly installed."); | 42 "Please ensure Git is correctly installed."); |
| 43 } | 43 } |
| 44 | 44 |
| 45 return ensureDir(join(systemCacheRoot, 'cache')); | 45 ensureDir(join(systemCacheRoot, 'cache')); |
| 46 }).then((_) => _ensureRepoCache(id)) | 46 return _ensureRepoCache(id); |
| 47 .then((_) => _revisionCachePath(id)) | 47 }).then((_) => _revisionCachePath(id)) |
| 48 .then((path) { | 48 .then((path) { |
| 49 revisionCachePath = path; | 49 revisionCachePath = path; |
| 50 return exists(revisionCachePath); | 50 if (entryExists(revisionCachePath)) return; |
| 51 }).then((exists) { | |
| 52 if (exists) return; | |
| 53 return _clone(_repoCachePath(id), revisionCachePath, mirror: false); | 51 return _clone(_repoCachePath(id), revisionCachePath, mirror: false); |
| 54 }).then((_) { | 52 }).then((_) { |
| 55 var ref = _getEffectiveRef(id); | 53 var ref = _getEffectiveRef(id); |
| 56 if (ref == 'HEAD') return; | 54 if (ref == 'HEAD') return; |
| 57 return _checkOut(revisionCachePath, ref); | 55 return _checkOut(revisionCachePath, ref); |
| 58 }).then((_) { | 56 }).then((_) { |
| 59 return Package.load(id.name, revisionCachePath, systemCache.sources); | 57 return new Package(id.name, revisionCachePath, systemCache.sources); |
| 60 }); | 58 }); |
| 61 } | 59 } |
| 62 | 60 |
| 63 Future<String> systemCacheDirectory(PackageId id) => _revisionCachePath(id); | 61 Future<String> systemCacheDirectory(PackageId id) => _revisionCachePath(id); |
| 64 | 62 |
| 65 /// Ensures [description] is a Git URL. | 63 /// Ensures [description] is a Git URL. |
| 66 void validateDescription(description, {bool fromLockFile: false}) { | 64 void validateDescription(description, {bool fromLockFile: false}) { |
| 67 // A single string is assumed to be a Git URL. | 65 // A single string is assumed to be a Git URL. |
| 68 if (description is String) return; | 66 if (description is String) return; |
| 69 if (description is! Map || !description.containsKey('url')) { | 67 if (description is! Map || !description.containsKey('url')) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 99 description['resolved-ref'] = revision; | 97 description['resolved-ref'] = revision; |
| 100 return new PackageId(id.name, this, id.version, description); | 98 return new PackageId(id.name, this, id.version, description); |
| 101 }); | 99 }); |
| 102 } | 100 } |
| 103 | 101 |
| 104 /// Ensure that the canonical clone of the repository referred to by [id] (the | 102 /// Ensure that the canonical clone of the repository referred to by [id] (the |
| 105 /// one in `<system cache>/git/cache`) exists and is up-to-date. Returns a | 103 /// one in `<system cache>/git/cache`) exists and is up-to-date. Returns a |
| 106 /// future that completes once this is finished and throws an exception if it | 104 /// future that completes once this is finished and throws an exception if it |
| 107 /// fails. | 105 /// fails. |
| 108 Future _ensureRepoCache(PackageId id) { | 106 Future _ensureRepoCache(PackageId id) { |
| 109 var path = _repoCachePath(id); | 107 return defer(() { |
| 110 return exists(path).then((exists) { | 108 var path = _repoCachePath(id); |
| 111 if (!exists) return _clone(_getUrl(id), path, mirror: true); | 109 if (!entryExists(path)) return _clone(_getUrl(id), path, mirror: true); |
| 112 | |
| 113 return git.run(["fetch"], workingDir: path).then((result) => null); | 110 return git.run(["fetch"], workingDir: path).then((result) => null); |
| 114 }); | 111 }); |
| 115 } | 112 } |
| 116 | 113 |
| 117 /// Returns a future that completes to the revision hash of [id]. | 114 /// Returns a future that completes to the revision hash of [id]. |
| 118 Future<String> _revisionAt(PackageId id) { | 115 Future<String> _revisionAt(PackageId id) { |
| 119 return git.run(["rev-parse", _getEffectiveRef(id)], | 116 return git.run(["rev-parse", _getEffectiveRef(id)], |
| 120 workingDir: _repoCachePath(id)).then((result) => result[0]); | 117 workingDir: _repoCachePath(id)).then((result) => result[0]); |
| 121 } | 118 } |
| 122 | 119 |
| 123 /// Returns the path to the revision-specific cache of [id]. | 120 /// Returns the path to the revision-specific cache of [id]. |
| 124 Future<String> _revisionCachePath(PackageId id) { | 121 Future<String> _revisionCachePath(PackageId id) { |
| 125 return _revisionAt(id).then((rev) { | 122 return _revisionAt(id).then((rev) { |
| 126 var revisionCacheName = '${id.name}-$rev'; | 123 var revisionCacheName = '${id.name}-$rev'; |
| 127 return join(systemCacheRoot, revisionCacheName); | 124 return join(systemCacheRoot, revisionCacheName); |
| 128 }); | 125 }); |
| 129 } | 126 } |
| 130 | 127 |
| 131 /// Clones the repo at the URI [from] to the path [to] on the local | 128 /// Clones the repo at the URI [from] to the path [to] on the local |
| 132 /// filesystem. | 129 /// filesystem. |
| 133 /// | 130 /// |
| 134 /// If [mirror] is true, create a bare, mirrored clone. This doesn't check out | 131 /// If [mirror] is true, create a bare, mirrored clone. This doesn't check out |
| 135 /// the working tree, but instead makes the repository a local mirror of the | 132 /// the working tree, but instead makes the repository a local mirror of the |
| 136 /// remote repository. See the manpage for `git clone` for more information. | 133 /// remote repository. See the manpage for `git clone` for more information. |
| 137 Future _clone(String from, String to, {bool mirror: false}) { | 134 Future _clone(String from, String to, {bool mirror: false}) { |
| 138 // Git on Windows does not seem to automatically create the destination | 135 return defer(() { |
| 139 // directory. | 136 // Git on Windows does not seem to automatically create the destination |
| 140 return ensureDir(to).then((_) { | 137 // directory. |
| 138 ensureDir(to); |
| 141 var args = ["clone", from, to]; | 139 var args = ["clone", from, to]; |
| 142 if (mirror) args.insertRange(1, 1, "--mirror"); | 140 if (mirror) args.insertRange(1, 1, "--mirror"); |
| 143 return git.run(args); | 141 return git.run(args); |
| 144 }).then((result) => null); | 142 }).then((result) => null); |
| 145 } | 143 } |
| 146 | 144 |
| 147 /// Checks out the reference [ref] in [repoPath]. | 145 /// Checks out the reference [ref] in [repoPath]. |
| 148 Future _checkOut(String repoPath, String ref) { | 146 Future _checkOut(String repoPath, String ref) { |
| 149 return git.run(["checkout", ref], workingDir: repoPath).then( | 147 return git.run(["checkout", ref], workingDir: repoPath).then( |
| 150 (result) => null); | 148 (result) => null); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 return description['ref']; | 191 return description['ref']; |
| 194 } | 192 } |
| 195 | 193 |
| 196 /// Returns [description] if it's a description, or [PackageId.description] if | 194 /// Returns [description] if it's a description, or [PackageId.description] if |
| 197 /// it's a [PackageId]. | 195 /// it's a [PackageId]. |
| 198 _getDescription(description) { | 196 _getDescription(description) { |
| 199 if (description is PackageId) return description.description; | 197 if (description is PackageId) return description.description; |
| 200 return description; | 198 return description; |
| 201 } | 199 } |
| 202 } | 200 } |
| OLD | NEW |