Skip to content
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

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'validator/executable.dart';
import 'validator/file_case.dart';
import 'validator/flutter_constraint.dart';
import 'validator/flutter_plugin_format.dart';
import 'validator/git_status.dart';
import 'validator/gitignore.dart';
import 'validator/leak_detection.dart';
import 'validator/license.dart';
Expand Down Expand Up @@ -143,6 +144,7 @@ abstract class Validator {
FileCaseValidator(),
AnalyzeValidator(),
GitignoreValidator(),
GitStatusValidator(),
PubspecValidator(),
LicenseValidator(),
NameValidator(),
Expand Down
91 changes: 91 additions & 0 deletions lib/src/validator/git_status.dart
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A validator that validates that no checked in files are modiofied in git.
/// A validator that validates that no checked in files are modified in git.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

///
/// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please return early instead of nesting a bit more 🤣

Suggested change
if (package.inGitRepo) {
if (package.inGitRepo) {
return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps consider splitting this out into a helper library.. git.parseMachineStatus or something.

This might be worth having unit tests cover.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might want to handle FormatException

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
''');
}
}
}
}
152 changes: 152 additions & 0 deletions test/validator/git_status_test.dart
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,
);
});
}