Skip to content

Commit

Permalink
refactor(aft): Remove libgit2dart dependency (#3562)
Browse files Browse the repository at this point in the history
* refactor(aft): Remove `libgit2dart` dependency

* chore(repo): Remove `libgit2dart` submodule

* ci: Remove `libgit2dart` patches

* chore(repo): Remove `libgit2` references from READMEs
  • Loading branch information
dnys1 authored Aug 25, 2023
1 parent 476b078 commit 0bf09b7
Show file tree
Hide file tree
Showing 23 changed files with 178 additions and 200 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/aft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ jobs:

- name: Get Packages
working-directory: packages/aft
run: |
# Patch libgit2dart (see https://github.com/dart-lang/pub/issues/3563)
( cd external/libgit2dart; git apply ../libgit2dart.patch )
dart pub upgrade
mkdir linux
cp external/libgit2dart/linux/*.so linux
run: dart pub upgrade

- name: Save Repo State
working-directory: packages/aft
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/dart_dart2js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ jobs:

- name: Setup aft
shell: bash # Run in bash regardless of platform
run: |
# Patch libgit2dart (see https://github.com/dart-lang/pub/issues/3563)
( cd packages/aft/external/libgit2dart; git apply ../libgit2dart.patch )
dart pub global activate -spath packages/aft
( cd packages/aft/external/libgit2dart; git reset --hard HEAD )
run: dart pub global activate -spath packages/aft

- name: Setup Firefox
if: ${{ matrix.browser == 'firefox' }}
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/dart_ddc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ jobs:

- name: Setup aft
shell: bash # Run in bash regardless of platform
run: |
# Patch libgit2dart (see https://github.com/dart-lang/pub/issues/3563)
( cd packages/aft/external/libgit2dart; git apply ../libgit2dart.patch )
dart pub global activate -spath packages/aft
( cd packages/aft/external/libgit2dart; git reset --hard HEAD )
run: dart pub global activate -spath packages/aft

- name: Setup Firefox
if: ${{ matrix.browser == 'firefox' }}
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/dart_native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ jobs:

- name: Setup aft
shell: bash # Run in bash regardless of platform
run: |
# Patch libgit2dart (see https://github.com/dart-lang/pub/issues/3563)
( cd packages/aft/external/libgit2dart; git apply ../libgit2dart.patch )
dart pub global activate -spath packages/aft
( cd packages/aft/external/libgit2dart; git reset --hard HEAD )
run: dart pub global activate -spath packages/aft

# This is needed to support running tests which depend on db_common.
- name: Install SQLite
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/dart_vm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ jobs:

- name: Setup aft
shell: bash # Run in bash regardless of platform
run: |
# Patch libgit2dart (see https://github.com/dart-lang/pub/issues/3563)
( cd packages/aft/external/libgit2dart; git apply ../libgit2dart.patch )
dart pub global activate -spath packages/aft
( cd packages/aft/external/libgit2dart; git reset --hard HEAD )
run: dart pub global activate -spath packages/aft

- name: Bootstrap
id: bootstrap
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/generate_workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ jobs:

- name: Setup aft
shell: bash # Run in bash regardless of platform
run: |
# Patch libgit2dart (see https://github.com/dart-lang/pub/issues/3563)
( cd packages/aft/external/libgit2dart; git apply ../libgit2dart.patch )
dart pub global activate -spath packages/aft
( cd packages/aft/external/libgit2dart; git reset --hard HEAD )
run: dart pub global activate -spath packages/aft

- name: Generate Workflows
run: aft generate workflows --set-exit-if-changed
2 changes: 0 additions & 2 deletions .github/workflows/smoke_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ jobs:

- name: Link Packages
run: |
# Patch libgit2dart (see https://github.com/dart-lang/pub/issues/3563)
( cd packages/aft/external/libgit2dart; git apply ../libgit2dart.patch )
dart pub global activate -spath packages/aft
aft link
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@
[submodule "packages/secure_storage/amplify_secure_storage_dart/external/libsecret"]
path = packages/secure_storage/amplify_secure_storage_dart/external/libsecret
url = https://github.com/GNOME/libsecret.git
[submodule "packages/aft/external/libgit2dart"]
path = packages/aft/external/libgit2dart
url = https://github.com/SkinnyMind/libgit2dart.git
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ $ cd amplify-flutter
Next setup Amplify Flutter Repo Tool (aft). This can be installed using the following:

```sh
$ brew install libgit2
$ dart pub global activate -spath packages/aft
```

Expand Down
14 changes: 0 additions & 14 deletions packages/aft/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,13 @@ A full list of available commands and options can be found by running `aft --hel

## Development

When developing `aft`, some commands will require the `libgit2` library for interacting with the repo's commit history. This library can be installed with the following commands:

```sh
# macOS
$ brew install libgit2
```

```sh
# Linux
$ sudo apt-get install libgit2-dev
```

To activate and run the local `aft` package:

```sh
$ dart pub global activate -spath packages/aft
$ aft --help
```

> Note: If you encounter a dependency error for libgit2dart, run `git submodule update --init`.
Make sure the Dart pub cache is in your PATH to run `aft` as a global executable after activating. See [here](https://dart.dev/tools/pub/cmd/pub-global#running-a-script-from-your-path) for more information.

### Writing Scripts
Expand Down
1 change: 0 additions & 1 deletion packages/aft/external/libgit2dart
Submodule libgit2dart deleted from 34d492
29 changes: 0 additions & 29 deletions packages/aft/external/libgit2dart.patch

This file was deleted.

21 changes: 21 additions & 0 deletions packages/aft/lib/src/changelog/commit_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import 'dart:convert';

import 'package:aft/aft.dart';
import 'package:aft/src/git.dart';
import 'package:aws_common/aws_common.dart';
import 'package:collection/collection.dart';

Expand Down Expand Up @@ -142,6 +143,26 @@ abstract class CommitMessage with AWSEquatable<CommitMessage> {
);
}

factory CommitMessage.fromCommitRef(CommitRef commitRef) {
final (sha, commit) = commitRef;
final [summary, ...body] = LineSplitter.split(commit.message).toList();
final commitTime = commit.committer
.split(_whitespace)
.firstWhereOrNull(_commitTimeRegex.hasMatch);
return CommitMessage.parse(
sha,
summary,
body: body.skip(1).join('\n'),
commitTimeSecs: switch (commitTime) {
final commitTime? => int.parse(commitTime),
_ => null,
},
);
}

static final _commitTimeRegex = RegExp(r'\d{10}');
static final _whitespace = RegExp(r'\s+');

/// The commit's OID SHA.
final String sha;

Expand Down
6 changes: 2 additions & 4 deletions packages/aft/lib/src/commands/amplify_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ abstract class AmplifyCommand extends Command<void>
return null;
}();

late final Repo repo = Repo(
aftConfig,
logger: logger,
);
late final Repo repo;

/// Runs `git` with the given [args] from the repo's root directory.
Future<void> runGit(
Expand Down Expand Up @@ -194,6 +191,7 @@ abstract class AmplifyCommand extends Command<void>
AWSLogger().logLevel = LogLevel.verbose;
}
logger.verbose('Got configuration: $aftConfig');
repo = await Repo.open(aftConfig, logger: logger);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import 'dart:io';

import 'package:aft/aft.dart';
import 'package:aws_common/aws_common.dart';
import 'package:collection/collection.dart';
import 'package:file/local.dart';
import 'package:git/git.dart';
import 'package:glob/glob.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:smithy/ast.dart';
import 'package:smithy_codegen/smithy_codegen.dart';
import 'package:stream_transform/stream_transform.dart';

const skipProtocols = [
'shared',
Expand Down Expand Up @@ -103,17 +102,18 @@ class GenerateGoldensCommand extends AmplifyCommand {
exit(1);
}

final gitRepo = Repository.open(tmpDir.path);
final versions = gitRepo.tags
final versions = await repo.git
.tags()
.map((tag) {
try {
return Version.parse(tag);
return Version.parse(tag.tag);
} on Object {
return null;
}
})
.nonNulls
.sorted((a, b) => a.compareTo(b));
.whereNotNull()
.toList();
versions.sort();
final latestVersion = versions.last.toString();
logger.info('Updating models to Smithy $latestVersion');

Expand Down
6 changes: 3 additions & 3 deletions packages/aft/lib/src/commands/version_bump_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class VersionBumpCommand extends AmplifyCommand
return null;
}();

GitChanges _changesForPackage(PackageInfo package) {
final baseRef = this.baseRef ?? repo.latestBumpRef(package);
Future<GitChanges> _changesForPackage(PackageInfo package) async {
final baseRef = this.baseRef ?? await repo.latestBumpRef(package);
if (baseRef == null) {
exitError(
'No previous version bumps for package (${package.name}). '
Expand All @@ -80,7 +80,7 @@ class VersionBumpCommand extends AmplifyCommand
}

Future<List<PackageInfo>> _updateVersions() async {
repo.bumpAllVersions(
await repo.bumpAllVersions(
commandPackages,
changesForPackage: _changesForPackage,
forcedBumpType: forcedBumpType,
Expand Down
65 changes: 65 additions & 0 deletions packages/aft/lib/src/git.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'dart:convert';

import 'package:git/git.dart';

extension GitHelpers on GitDir {
/// Returns the [CommitRef] for the current `HEAD`.
Future<CommitRef> get head => commitRef('HEAD');

/// Returns the [CommitRef] for the given [commit].
Future<CommitRef> commitRef(String commit) async {
if (_sha1Regex.hasMatch(commit)) {
return (
commit,
await commitFromRevision(commit),
);
}
return (
revParse(commit),
commitFromRevision(commit),
).wait;
}

/// Runs a `git diff` between [baseTree] and [headTree] and returns the list
/// of filepaths changed between the two commits.
Future<List<String>> diffTrees(String baseTree, String headTree) async {
final diff = await runCommand(
['diff', '--raw', '$baseTree..$headTree'],
);
final diffs = LineSplitter.split(diff.stdout as String);
final changedPaths = <String>[];
for (final diff in diffs) {
// Example line:
// :100644 100644 475c1a84d 3738675a1 M packages/aws_sdk/smoke_test/test/build_verify_test.dart
changedPaths.add(diff.split(_whitespace).last);
}
return changedPaths;
}

/// Runs `git rev-parse` with the provided [input].
Future<String> revParse(String input) async {
final revParse = await runCommand(['rev-parse', input]);
return (revParse.stdout as String).trim();
}

/// Lists all commits, in reverse chronological order, between [baseCommit]
/// and [headCommit].
Stream<CommitRef> revList(String baseCommit, String headCommit) async* {
final revList = await runCommand(
['rev-list', '$baseCommit..$headCommit'],
);
final commits = LineSplitter.split((revList.stdout as String).trim());
for (final oid in commits) {
yield await commitRef(oid);
}
}

static final _sha1Regex = RegExp(r'[a-f0-9]{40}');
static final _whitespace = RegExp(r'\s+');
}

/// A [Commit] and its associated SHA.
typedef CommitRef = (String oid, Commit commit);
Loading

0 comments on commit 0bf09b7

Please sign in to comment.