Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: lib/src/package.dart

Issue 1233533002: Respect gitignore for packages in subdirectories. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: code review Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/package_list_files_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 pub.package; 5 library pub.package;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 8
9 import 'package:barback/barback.dart'; 9 import 'package:barback/barback.dart';
10 import 'package:path/path.dart' as p; 10 import 'package:path/path.dart' as p;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 101
102 return p.join(dir, readmes.reduce((readme1, readme2) { 102 return p.join(dir, readmes.reduce((readme1, readme2) {
103 var extensions1 = ".".allMatches(readme1).length; 103 var extensions1 = ".".allMatches(readme1).length;
104 var extensions2 = ".".allMatches(readme2).length; 104 var extensions2 = ".".allMatches(readme2).length;
105 var comparison = extensions1.compareTo(extensions2); 105 var comparison = extensions1.compareTo(extensions2);
106 if (comparison == 0) comparison = readme1.compareTo(readme2); 106 if (comparison == 0) comparison = readme1.compareTo(readme2);
107 return (comparison <= 0) ? readme1 : readme2; 107 return (comparison <= 0) ? readme1 : readme2;
108 })); 108 }));
109 } 109 }
110 110
111 /// Returns whether or not this package is in a Git repo.
112 bool get _inGitRepo {
113 if (_inGitRepoCache != null) return _inGitRepoCache;
114
115 if (dir == null || !git.isInstalled) {
116 _inGitRepoCache = false;
117 } else {
118 try {
119 git.runSync(['rev-parse'], workingDir: dir);
120 _inGitRepoCache = true;
121 } on git.GitException catch (_) {
122 _inGitRepoCache = false;
123 }
124 }
125
126 return _inGitRepoCache;
127 }
128 bool _inGitRepoCache;
129
111 /// Loads the package whose root directory is [packageDir]. 130 /// Loads the package whose root directory is [packageDir].
112 /// 131 ///
113 /// [name] is the expected name of that package (e.g. the name given in the 132 /// [name] is the expected name of that package (e.g. the name given in the
114 /// dependency), or `null` if the package being loaded is the entrypoint 133 /// dependency), or `null` if the package being loaded is the entrypoint
115 /// package. 134 /// package.
116 Package.load(String name, String packageDir, SourceRegistry sources) 135 Package.load(String name, String packageDir, SourceRegistry sources)
117 : dir = packageDir, 136 : dir = packageDir,
118 pubspec = new Pubspec.load(packageDir, sources, expectedName: name); 137 pubspec = new Pubspec.load(packageDir, sources, expectedName: name);
119 138
120 /// Constructs a package with the given pubspec. 139 /// Constructs a package with the given pubspec.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 /// 196 ///
178 /// If this is a Git repository, this will respect .gitignore; otherwise, it 197 /// If this is a Git repository, this will respect .gitignore; otherwise, it
179 /// will return all non-hidden, non-blacklisted files. 198 /// will return all non-hidden, non-blacklisted files.
180 /// 199 ///
181 /// If [beneath] is passed, this will only return files beneath that path, 200 /// If [beneath] is passed, this will only return files beneath that path,
182 /// which is expected to be relative to the package's root directory. If 201 /// which is expected to be relative to the package's root directory. If
183 /// [recursive] is true, this will return all files beneath that path; 202 /// [recursive] is true, this will return all files beneath that path;
184 /// otherwise, it will only return files one level beneath it. 203 /// otherwise, it will only return files one level beneath it.
185 /// 204 ///
186 /// If [useGitIgnore] is passed, this will take the .gitignore rules into 205 /// If [useGitIgnore] is passed, this will take the .gitignore rules into
187 /// account if the package's root directory is a Git repository. 206 /// account if the package's root directory is a Git repository.
kustermann 2015/08/26 12:55:52 dbc: This sentence is no longer correct. It also l
nweiz 2015/08/26 22:58:07 Done in https://codereview.chromium.org/1310253007
188 /// 207 ///
189 /// Note that the returned paths won't always be beneath [dir]. To safely 208 /// Note that the returned paths won't always be beneath [dir]. To safely
190 /// convert them to paths relative to the package root, use [relative]. 209 /// convert them to paths relative to the package root, use [relative].
191 List<String> listFiles({String beneath, bool recursive: true, 210 List<String> listFiles({String beneath, bool recursive: true,
192 bool useGitIgnore: false}) { 211 bool useGitIgnore: false}) {
193 // An in-memory package has no files. 212 // An in-memory package has no files.
194 if (dir == null) return []; 213 if (dir == null) return [];
195 214
196 if (beneath == null) { 215 if (beneath == null) {
197 beneath = dir; 216 beneath = dir;
198 } else { 217 } else {
199 beneath = p.join(dir, beneath); 218 beneath = p.join(dir, beneath);
200 } 219 }
201 220
202 if (!dirExists(beneath)) return []; 221 if (!dirExists(beneath)) return [];
203 222
204 // This is used in some performance-sensitive paths and can list many, many 223 // This is used in some performance-sensitive paths and can list many, many
205 // files. As such, it leans more havily towards optimization as opposed to 224 // files. As such, it leans more havily towards optimization as opposed to
206 // readability than most code in pub. In particular, it avoids using the 225 // readability than most code in pub. In particular, it avoids using the
207 // path package, since re-parsing a path is very expensive relative to 226 // path package, since re-parsing a path is very expensive relative to
208 // string operations. 227 // string operations.
209 var files; 228 var files;
210 if (useGitIgnore && git.isInstalled && dirExists(path('.git'))) { 229 if (useGitIgnore && _inGitRepo) {
211 // Later versions of git do not allow a path for ls-files that appears to 230 // Later versions of git do not allow a path for ls-files that appears to
212 // be outside of the repo, so make sure we give it a relative path. 231 // be outside of the repo, so make sure we give it a relative path.
213 var relativeBeneath = p.relative(beneath, from: dir); 232 var relativeBeneath = p.relative(beneath, from: dir);
214 233
215 // List all files that aren't gitignored, including those not checked in 234 // List all files that aren't gitignored, including those not checked in
216 // to Git. 235 // to Git.
217 files = git.runSync( 236 files = git.runSync(
218 ["ls-files", "--cached", "--others", "--exclude-standard", 237 ["ls-files", "--cached", "--others", "--exclude-standard",
219 relativeBeneath], 238 relativeBeneath],
220 workingDir: dir); 239 workingDir: dir);
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 435
417 bool operator ==(other) { 436 bool operator ==(other) {
418 // TODO(rnystrom): We're assuming here that we don't need to delve into the 437 // TODO(rnystrom): We're assuming here that we don't need to delve into the
419 // description. 438 // description.
420 return other is PackageDep && 439 return other is PackageDep &&
421 other.name == name && 440 other.name == name &&
422 other.source == source && 441 other.source == source &&
423 other.constraint == constraint; 442 other.constraint == constraint;
424 } 443 }
425 } 444 }
OLDNEW
« no previous file with comments | « no previous file | test/package_list_files_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698