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('io.dart'); | 7 #import('io.dart'); |
8 #import('package.dart'); | 8 #import('package.dart'); |
9 #import('source.dart'); | 9 #import('source.dart'); |
10 #import('source_registry.dart'); | 10 #import('source_registry.dart'); |
(...skipping 22 matching lines...) Expand all Loading... | |
33 * `<package name>-<url hash>`. These are used to check out the repository | 33 * `<package name>-<url hash>`. These are used to check out the repository |
34 * itself; each of the commit-specific directories are clones of a directory | 34 * itself; each of the commit-specific directories are clones of a directory |
35 * in `cache/`. | 35 * in `cache/`. |
36 */ | 36 */ |
37 Future<Package> installToSystemCache(PackageId id) { | 37 Future<Package> installToSystemCache(PackageId id) { |
38 var revisionCachePath; | 38 var revisionCachePath; |
39 | 39 |
40 return isGitInstalled.chain((installed) { | 40 return isGitInstalled.chain((installed) { |
41 if (!installed) { | 41 if (!installed) { |
42 throw new Exception( | 42 throw new Exception( |
43 "Cannot install '${id.name}' from Git (${id.description}).\n" | 43 "Cannot install '${id.name}' from Git (${_getUrl(id)}).\n" |
44 "Please ensure Git is correctly installed."); | 44 "Please ensure Git is correctly installed."); |
45 } | 45 } |
46 | 46 |
47 return ensureDir(join(systemCacheRoot, 'cache')); | 47 return ensureDir(join(systemCacheRoot, 'cache')); |
48 }).chain((_) => _ensureRepoCache(id)) | 48 }).chain((_) => _ensureRepoCache(id)) |
49 .chain((_) => _revisionCachePath(id, "HEAD")) | 49 .chain((_) => _revisionCachePath(id)) |
50 .chain((path) { | 50 .chain((path) { |
51 revisionCachePath = path; | 51 revisionCachePath = path; |
52 return exists(revisionCachePath); | 52 return exists(revisionCachePath); |
53 }).chain((exists) { | 53 }).chain((exists) { |
54 if (exists) return new Future.immediate(null); | 54 if (exists) return new Future.immediate(null); |
55 return _clone(_repoCachePath(id), revisionCachePath); | 55 return _clone(_repoCachePath(id), revisionCachePath); |
56 }).chain((_) { | |
57 var ref = _getRef(id); | |
58 if (ref == null) return new Future.immediate(null); | |
59 return _checkout(revisionCachePath, ref); | |
56 }).chain((_) => Package.load(revisionCachePath, systemCache.sources)); | 60 }).chain((_) => Package.load(revisionCachePath, systemCache.sources)); |
57 } | 61 } |
58 | 62 |
59 /** | 63 /** |
60 * The package name of a Git repo is the name of the directory into which | 64 * The package name of a Git repo is the name of the directory into which |
61 * it'll be cloned. | 65 * it'll be cloned. |
62 */ | 66 */ |
63 String packageName(description) => | 67 String packageName(description) => |
64 basename(description).replaceFirst(const RegExp("\.git\$"), ""); | 68 basename(_getUrl(description)).replaceFirst(const RegExp("\.git\$"), ""); |
65 | 69 |
66 /** | 70 /** |
67 * Ensures [description] is a Git URL. | 71 * Ensures [description] is a Git URL. |
68 */ | 72 */ |
69 void validateDescription(description) { | 73 void validateDescription(description) { |
70 if (description is! String) { | 74 // A single string is assumed to be a Git URL. |
71 throw new FormatException("The description must be a git URL."); | 75 if (description is String) return; |
76 if (description is! Map || !description.containsKey('url')) { | |
77 throw new FormatException("The description must be a Git URL or a map " | |
78 "with a 'url' key."); | |
79 } | |
80 description = new Map.from(description); | |
81 description.remove('url'); | |
82 description.remove('ref'); | |
83 | |
84 if (!description.isEmpty()) { | |
85 var plural = description.length > 1; | |
86 var keys = Strings.join(description.keys, ', '); | |
87 throw new FormatException("Invalid key${plural ? 's' : ''}: $keys."); | |
72 } | 88 } |
73 } | 89 } |
74 | 90 |
75 /** | 91 /** |
76 * Ensure that the canonical clone of the repository referred to by [id] (the | 92 * Ensure that the canonical clone of the repository referred to by [id] (the |
77 * one in `<system cache>/git/cache`) exists and is up-to-date. Returns a | 93 * one in `<system cache>/git/cache`) exists and is up-to-date. Returns a |
78 * future that completes once this is finished and throws an exception if it | 94 * future that completes once this is finished and throws an exception if it |
79 * fails. | 95 * fails. |
80 */ | 96 */ |
81 Future _ensureRepoCache(PackageId id) { | 97 Future _ensureRepoCache(PackageId id) { |
82 var path = _repoCachePath(id); | 98 var path = _repoCachePath(id); |
83 return exists(path).chain((exists) { | 99 return exists(path).chain((exists) { |
84 if (!exists) return _clone(id.description, path); | 100 if (!exists) return _clone(_getUrl(id), path); |
85 | 101 |
86 return runProcess("git", ["pull", "--force"], workingDir: path, | 102 return runProcess("git", ["pull", "--force"], workingDir: path, |
87 pipeStdout: true, pipeStderr: true).transform((result) { | 103 pipeStdout: true, pipeStderr: true).transform((result) { |
88 if (!result.success) throw 'Git failed.'; | 104 if (!result.success) throw 'Git failed.'; |
89 return null; | 105 return null; |
90 }); | 106 }); |
91 }); | 107 }); |
92 } | 108 } |
93 | 109 |
94 /** | 110 /** |
95 * Returns a future that completes to the revision hash of the repository for | 111 * Returns a future that completes to the revision hash of the repository for |
96 * [id] at [ref], which can be any Git ref. | 112 * [id] at [ref], which can be any Git ref. |
97 */ | 113 */ |
98 Future<String> _revisionAt(PackageId id, String ref) { | 114 Future<String> _revisionAt(PackageId id, String ref) { |
99 return runProcess("git", ["rev-parse", ref], | 115 return runProcess("git", ["rev-parse", ref], |
100 workingDir: _repoCachePath(id), pipeStderr: true).transform((result) { | 116 workingDir: _repoCachePath(id), pipeStderr: true).transform((result) { |
101 if (!result.success) throw 'Git failed.'; | 117 if (!result.success) throw 'Git failed.'; |
102 return result.stdout[0]; | 118 return result.stdout[0]; |
103 }); | 119 }); |
104 } | 120 } |
105 | 121 |
106 /** | 122 /** |
107 * Returns the path to the revision-specific cache of [id] at [ref], which can | 123 * Returns the path to the revision-specific cache of [id] at [ref], which can |
108 * be any Git ref. | 124 * be any Git ref. |
109 */ | 125 */ |
110 Future<String> _revisionCachePath(PackageId id, String ref) { | 126 Future<String> _revisionCachePath(PackageId id) { |
127 var ref = _getRef(id); | |
128 if (ref == null) ref = 'HEAD'; | |
111 return _revisionAt(id, ref).transform((rev) { | 129 return _revisionAt(id, ref).transform((rev) { |
112 var revisionCacheName = '${id.name}-$rev'; | 130 var revisionCacheName = '${id.name}-$rev'; |
113 return join(systemCacheRoot, revisionCacheName); | 131 return join(systemCacheRoot, revisionCacheName); |
114 }); | 132 }); |
115 } | 133 } |
116 | 134 |
117 /** | 135 /** |
118 * Clones the repo at the URI [from] to the path [to] on the local filesystem. | 136 * Clones the repo at the URI [from] to the path [to] on the local filesystem. |
119 */ | 137 */ |
120 Future _clone(String from, String to) { | 138 Future _clone(String from, String to) { |
121 return runProcess("git", ["clone", from, to], pipeStdout: true, | 139 return runProcess("git", ["clone", from, to], pipeStdout: true, |
122 pipeStderr: true).transform((result) { | 140 pipeStderr: true).transform((result) { |
123 if (!result.success) throw 'Git failed.'; | 141 if (!result.success) throw 'Git failed.'; |
124 return null; | 142 return null; |
125 }); | 143 }); |
126 } | 144 } |
127 | 145 |
128 /** | 146 /** |
147 * Checks out the reference [ref] in [repoPath]. | |
148 */ | |
149 Future _checkout(String repoPath, String ref) { | |
Bob Nystrom
2012/07/17 00:10:31
"_checkOut". I know the command is all lowercase i
nweiz
2012/07/17 01:06:55
Done.
| |
150 return runProcess("git", ["checkout", ref], pipeStdout: true, | |
151 pipeStderr: true, workingDir: repoPath).transform((result) { | |
152 if (!result.success) throw 'Git failed.'; | |
Bob Nystrom
2012/07/17 00:10:31
Should handle this more gracefully, or at least ha
nweiz
2012/07/17 01:06:55
I'm not sure I agree. Since we're piping stderr, s
| |
153 return null; | |
154 }); | |
155 } | |
156 | |
157 /** | |
129 * Returns the path to the canonical clone of the repository referred to by | 158 * Returns the path to the canonical clone of the repository referred to by |
130 * [id] (the one in `<system cache>/git/cache`). | 159 * [id] (the one in `<system cache>/git/cache`). |
131 */ | 160 */ |
132 String _repoCachePath(PackageId id) { | 161 String _repoCachePath(PackageId id) { |
133 var repoCacheName = '${id.name}-${sha1(id.description)}'; | 162 var repoCacheName = '${id.name}-${sha1(_getUrl(id))}'; |
134 return join(systemCacheRoot, 'cache', repoCacheName); | 163 return join(systemCacheRoot, 'cache', repoCacheName); |
135 } | 164 } |
165 | |
166 /** | |
167 * Returns the repository URL for [id]. | |
168 * | |
169 * [description] may be a description or a [PackageId]. | |
170 */ | |
171 String _getUrl(description) { | |
172 description = _getDescription(description); | |
173 if (description is String) return description; | |
174 return description['url']; | |
175 } | |
176 | |
177 /** | |
178 * Returns the commit ref for [id], or null if none is given. | |
179 * | |
180 * [description] may be a description or a [PackageId]. | |
181 */ | |
182 String _getRef(description) { | |
183 description = _getDescription(description); | |
184 if (description is String) return null; | |
185 return description['ref']; | |
186 } | |
187 | |
188 /** | |
189 * Returns [description] if it's a description, or [PackageId.description] if | |
190 * it's a [PackageId]. | |
191 */ | |
192 _getDescription(description) { | |
193 if (description is PackageId) return description.description; | |
194 return description; | |
195 } | |
136 } | 196 } |
OLD | NEW |