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

fix: fix diff functionality #558

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 56 additions & 17 deletions packages/melos/lib/src/common/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,8 @@ Future<List<GitCommit>> gitCommitsForPackage(
String? diff,
required MelosLogger logger,
}) async {
var revisionRange = diff?.trim();
if (revisionRange != null) {
if (revisionRange.isEmpty) {
revisionRange = null;
} else if (!_gitVersionRangeShortHandRegExp.hasMatch(revisionRange)) {
// If the revision range is not a valid revision range short hand then we
// assume it's a commit or tag and default to the range from that
// commit/tag to HEAD.
revisionRange = '$revisionRange...HEAD';
}
}

if (revisionRange == null) {
final latestTag = await gitLatestTagForPackage(package, logger: logger);
// If no latest tag is found then we default to the entire git history.
revisionRange = latestTag != null ? '$latestTag...HEAD' : 'HEAD';
}
final revisionRange =
await _resolveRevisionRange(package, diff: diff, logger: logger);

logger.trace(
'[GIT] Getting commits for package ${package.name} for revision range '
Expand Down Expand Up @@ -328,6 +313,34 @@ Future<List<GitCommit>> gitCommitsForPackage(
}).toList();
}

Future<bool> gitHasDiffInPackage(
Package package, {
required String? diff,
required MelosLogger logger,
}) async {
final revisionRange =
await _resolveRevisionRange(package, diff: diff, logger: logger);

logger.trace(
'[GIT] Getting $diff diff for package ${package.name}.',
);

final processResult = await gitExecuteCommand(
arguments: [
'--no-pager',
'diff',
'--name-status',
revisionRange,
'--',
'.',
],
workingDirectory: package.path,
logger: logger,
);

return (processResult.stdout as String).isNotEmpty;
}

/// Returns the current branch name of the local git repository.
Future<String> gitGetCurrentBranchName({
required String workingDirectory,
Expand Down Expand Up @@ -396,3 +409,29 @@ Future<bool> gitIsBehindUpstream({

return isBehind;
}

Future<String> _resolveRevisionRange(
Package package, {
required String? diff,
required MelosLogger logger,
}) async {
var revisionRange = diff?.trim();
if (revisionRange != null) {
if (revisionRange.isEmpty) {
revisionRange = null;
} else if (!_gitVersionRangeShortHandRegExp.hasMatch(revisionRange)) {
// If the revision range is not a valid revision range short hand then we
// assume it's a commit or tag and default to the range from that
// commit/tag to HEAD.
return '$revisionRange...HEAD';
}
}

if (revisionRange == null) {
final latestTag = await gitLatestTagForPackage(package, logger: logger);
// If no latest tag is found then we default to the entire git history.
return latestTag != null ? '$latestTag...HEAD' : 'HEAD';
}

return 'HEAD';
}
6 changes: 3 additions & 3 deletions packages/melos/lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,9 @@ extension on Iterable<Package> {

return Pool(10)
.forEach(this, (package) async {
final commits =
await gitCommitsForPackage(package, diff: diff, logger: logger);
return MapEntry(package, commits.isNotEmpty);
final hasDiff =
await gitHasDiffInPackage(package, diff: diff, logger: logger);
return MapEntry(package, hasDiff);
})
.where((event) => event.value)
.map((event) => event.key)
Expand Down
Loading