-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate git is clean when publishing #4373
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||||||
// Copyright (c) 2024, 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:convert'; | ||||||||||
import 'dart:typed_data'; | ||||||||||
|
||||||||||
import 'package:path/path.dart' as p; | ||||||||||
|
||||||||||
import '../git.dart' as git; | ||||||||||
import '../log.dart' as log; | ||||||||||
import '../utils.dart'; | ||||||||||
import '../validator.dart'; | ||||||||||
|
||||||||||
/// A validator that validates that no checked in files are modiofied in git. | ||||||||||
/// | ||||||||||
/// Doesn't report on newly added files, as generated files might not be checked | ||||||||||
/// in to git. | ||||||||||
class GitStatusValidator extends Validator { | ||||||||||
@override | ||||||||||
Future<void> validate() async { | ||||||||||
if (package.inGitRepo) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please return early instead of nesting a bit more 🤣
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||
final modfiedFiles = <String>[]; | ||||||||||
try { | ||||||||||
final reporoot = git.repoRoot(package.dir); | ||||||||||
if (reporoot == null) { | ||||||||||
log.fine( | ||||||||||
'Could not determine the repository root from ${package.dir}.', | ||||||||||
); | ||||||||||
// This validation is only a warning. | ||||||||||
return; | ||||||||||
} | ||||||||||
final output = git.runSyncBytes( | ||||||||||
[ | ||||||||||
'status', | ||||||||||
'-z', // Machine parsable | ||||||||||
'--no-renames', // We don't care about renames. | ||||||||||
|
||||||||||
'--untracked-files=no', // Don't show untracked files. | ||||||||||
], | ||||||||||
workingDir: package.dir, | ||||||||||
); | ||||||||||
// Split at \0. | ||||||||||
var start = 0; | ||||||||||
for (var i = 0; i < output.length; i++) { | ||||||||||
if (output[i] != 0) { | ||||||||||
continue; | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps consider splitting this out into a helper library.. This might be worth having unit tests cover. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I split something out - but it is hard to make something really general I think... |
||||||||||
final filename = utf8.decode( | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might want to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! Done! |
||||||||||
Uint8List.sublistView( | ||||||||||
output, | ||||||||||
// The first 3 bytes are the modification status. | ||||||||||
// Skip those. | ||||||||||
start + 3, | ||||||||||
i, | ||||||||||
), | ||||||||||
); | ||||||||||
final fullPath = p.join(reporoot, filename); | ||||||||||
if (!files.any((f) => p.equals(fullPath, f))) { | ||||||||||
// File is not in the published set - ignore. | ||||||||||
continue; | ||||||||||
} | ||||||||||
modfiedFiles.add(p.relative(fullPath)); | ||||||||||
start = i + 1; | ||||||||||
} | ||||||||||
} on git.GitException catch (e) { | ||||||||||
log.fine('Could not run `git status` files in repo (${e.message}).'); | ||||||||||
// This validation is only a warning. | ||||||||||
// If git is not supported on the platform, we just continue silently. | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
if (modfiedFiles.isNotEmpty) { | ||||||||||
warnings.add(''' | ||||||||||
${modfiedFiles.length} checked-in ${pluralize('file', modfiedFiles.length)} ${modfiedFiles.length == 1 ? 'is' : 'are'} modified in git. | ||||||||||
|
||||||||||
Usually you want to publish from a clean git state. | ||||||||||
|
||||||||||
Consider committing these files or reverting the changes. | ||||||||||
|
||||||||||
Modified files: | ||||||||||
|
||||||||||
${modfiedFiles.take(10).map(p.relative).join('\n')} | ||||||||||
${modfiedFiles.length > 10 ? '...\n' : ''} | ||||||||||
Run `git status` for more information. | ||||||||||
'''); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright (c) 2020, 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 'package:pub/src/exit_codes.dart' as exit_codes; | ||
import 'package:test/test.dart'; | ||
|
||
import '../descriptor.dart' as d; | ||
import '../test_pub.dart'; | ||
|
||
Future<void> expectValidation( | ||
Matcher error, | ||
int exitCode, { | ||
Map<String, String> environment = const {}, | ||
String? workingDirectory, | ||
}) async { | ||
await runPub( | ||
error: error, | ||
args: ['publish', '--dry-run'], | ||
environment: environment, | ||
workingDirectory: workingDirectory ?? d.path(appPath), | ||
exitCode: exitCode, | ||
); | ||
} | ||
|
||
void main() { | ||
test( | ||
'should consider a package valid ' | ||
'if it contains no modified files (but contains a newly created one)', | ||
() async { | ||
await d.git('myapp', [ | ||
...d.validPackage().contents, | ||
d.file('foo.txt', 'foo'), | ||
d.file('.pubignore', 'bob.txt\n'), | ||
d.file('bob.txt', 'bob'), | ||
]).create(); | ||
|
||
await d.dir('myapp', [ | ||
d.file('bar.txt', 'bar'), // Create untracked file. | ||
d.file('bob.txt', 'bob2'), // Modify pub-ignored file. | ||
]).create(); | ||
|
||
await expectValidation(contains('Package has 0 warnings.'), 0); | ||
}); | ||
|
||
test('Warns if files are modified', () async { | ||
await d.git('myapp', [ | ||
...d.validPackage().contents, | ||
d.file('foo.txt', 'foo'), | ||
]).create(); | ||
|
||
await d.dir('myapp', [ | ||
d.file('foo.txt', 'foo2'), | ||
]).create(); | ||
|
||
await expectValidation( | ||
allOf([ | ||
contains('Package has 1 warning.'), | ||
contains( | ||
''' | ||
* 1 checked-in file is modified in git. | ||
|
||
Usually you want to publish from a clean git state. | ||
|
||
Consider committing these files or reverting the changes. | ||
|
||
Modified files: | ||
|
||
foo.txt | ||
|
||
Run `git status` for more information.''', | ||
), | ||
]), | ||
exit_codes.DATA, | ||
); | ||
|
||
// Stage but do not commit foo.txt. The warning should still be active. | ||
await d.git('myapp').runGit(['add', 'foo.txt']); | ||
await expectValidation( | ||
allOf([ | ||
contains('Package has 1 warning.'), | ||
contains('foo.txt'), | ||
]), | ||
exit_codes.DATA, | ||
); | ||
await d.git('myapp').runGit(['commit', '-m', 'message']); | ||
|
||
await d.dir('myapp', [ | ||
d.file('bar.txt', 'bar'), // Create untracked file. | ||
d.file('bob.txt', 'bob2'), // Modify pub-ignored file. | ||
]).create(); | ||
|
||
// Stage untracked file, now the warning should be about that. | ||
await d.git('myapp').runGit(['add', 'bar.txt']); | ||
|
||
await expectValidation( | ||
allOf([ | ||
contains('Package has 1 warning.'), | ||
contains( | ||
''' | ||
* 1 checked-in file is modified in git. | ||
|
||
Usually you want to publish from a clean git state. | ||
|
||
Consider committing these files or reverting the changes. | ||
|
||
Modified files: | ||
|
||
bar.txt | ||
|
||
Run `git status` for more information.''', | ||
), | ||
]), | ||
exit_codes.DATA, | ||
); | ||
}); | ||
|
||
test('Works with non-ascii unicode characters in file name', () async { | ||
await d.git('myapp', [ | ||
...d.validPackage().contents, | ||
d.file('non_ascii_и.txt', 'foo'), | ||
d.file('non_ascii_и_ignored.txt', 'foo'), | ||
d.file('.pubignore', 'non_ascii_и_ignored.txt'), | ||
]).create(); | ||
await d.dir('myapp', [ | ||
...d.validPackage().contents, | ||
d.file('non_ascii_и.txt', 'foo2'), | ||
d.file('non_ascii_и_ignored.txt', 'foo2'), | ||
]).create(); | ||
|
||
await expectValidation( | ||
allOf([ | ||
contains('Package has 1 warning.'), | ||
contains( | ||
''' | ||
* 1 checked-in file is modified in git. | ||
|
||
Usually you want to publish from a clean git state. | ||
|
||
Consider committing these files or reverting the changes. | ||
|
||
Modified files: | ||
|
||
non_ascii_и.txt | ||
|
||
Run `git status` for more information.''', | ||
), | ||
]), | ||
exit_codes.DATA, | ||
); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done