-
Notifications
You must be signed in to change notification settings - Fork 232
Validate git is clean when publishing #4373
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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 modified 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) { | ||
return; | ||
} | ||
final Uint8List output; | ||
final String reporoot; | ||
try { | ||
final maybeReporoot = git.repoRoot(package.dir); | ||
if (maybeReporoot == null) { | ||
log.fine( | ||
'Could not determine the repository root from ${package.dir}.', | ||
); | ||
// This validation is only a warning. | ||
return; | ||
} | ||
reporoot = maybeReporoot; | ||
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, | ||
); | ||
} 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; | ||
} | ||
final List<String> modifiedFiles; | ||
try { | ||
modifiedFiles = git | ||
.splitZeroTerminated(output, skipPrefix: 3) | ||
.map((bytes) { | ||
try { | ||
final filename = utf8.decode(bytes); | ||
final fullPath = p.join(reporoot, filename); | ||
if (!files.any((f) => p.equals(fullPath, f))) { | ||
// File is not in the published set - ignore. | ||
return null; | ||
} | ||
return p.relative(fullPath); | ||
} on FormatException catch (e) { | ||
// Filename is not utf8 - ignore. | ||
log.fine('Cannot decode file name: $e'); | ||
return null; | ||
} | ||
}) | ||
.nonNulls | ||
.toList(); | ||
} on FormatException catch (e) { | ||
// Malformed output from `git status`. Skip this validation. | ||
log.fine('Malformed output from `git status -z`: $e'); | ||
return; | ||
} | ||
if (modifiedFiles.isNotEmpty) { | ||
warnings.add(''' | ||
${modifiedFiles.length} checked-in ${pluralize('file', modifiedFiles.length)} ${modifiedFiles.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: | ||
|
||
${modifiedFiles.take(10).map(p.relative).join('\n')} | ||
${modifiedFiles.length > 10 ? '...\n' : ''} | ||
Run `git status` for more information. | ||
'''); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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:typed_data'; | ||
|
||
import 'package:pub/src/git.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('splitZeroTerminated works', () { | ||
expect(splitZeroTerminated(Uint8List.fromList([])), <Uint8List>[]); | ||
expect( | ||
splitZeroTerminated(Uint8List.fromList([0])), | ||
<Uint8List>[Uint8List.fromList([])], | ||
); | ||
|
||
expect(splitZeroTerminated(Uint8List.fromList([1, 0, 1])), <Uint8List>[ | ||
Uint8List.fromList([1]), | ||
]); | ||
expect( | ||
splitZeroTerminated(Uint8List.fromList([2, 1, 0, 1, 0, 0])), | ||
<Uint8List>[ | ||
Uint8List.fromList([2, 1]), | ||
Uint8List.fromList([1]), | ||
Uint8List.fromList([]), | ||
], | ||
); | ||
expect( | ||
splitZeroTerminated( | ||
Uint8List.fromList([2, 1, 0, 1, 0, 2, 3, 0]), | ||
skipPrefix: 1, | ||
), | ||
<Uint8List>[ | ||
Uint8List.fromList([1]), | ||
Uint8List.fromList([]), | ||
Uint8List.fromList([3]), | ||
], | ||
); | ||
expect( | ||
() => splitZeroTerminated( | ||
Uint8List.fromList([2, 1, 0, 1, 0, 0]), | ||
skipPrefix: 1, | ||
), | ||
throwsA(isA<FormatException>()), | ||
); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
splitZeroTerminated
without theskipPrefix
might have been more sane.But this is also fine :D
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.
Yeah - more sane - just I wanted to avoid creating too much garbage, and (as far as I can tell)
utf8.decode
cannot work on a partial buffer...