Skip to content

Support lfs and submodules #3806

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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
Prev Previous commit
Next Next commit
Support for lfs and submodules
  • Loading branch information
sigurdm committed Feb 28, 2023
commit 5c46570f5b0bd1f1a13fd9ef5823a09f64a191c1
64 changes: 36 additions & 28 deletions lib/src/source/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,11 @@ class GitSource extends CachedSource {
final path = description.path;
await _revisionCacheClones.putIfAbsent(revisionCachePath, () async {
if (!entryExists(revisionCachePath)) {
await _clone(_repoCachePath(description, cache), revisionCachePath);
await _checkOut(revisionCachePath, resolvedRef);
await _createWorktree(
_repoCachePath(description, cache),
revisionCachePath,
resolvedRef,
);
_writePackageList(revisionCachePath, [path]);
didUpdate = true;
} else {
Expand Down Expand Up @@ -415,7 +418,7 @@ class GitSource extends CachedSource {
final result = <RepairResult>[];

var packages = listDir(rootDir)
.where((entry) => dirExists(p.join(entry, '.git')))
.where((entry) => entryExists(p.join(entry, '.git')))
.expand((revisionCachePath) {
return _readPackageList(revisionCachePath).map((relative) {
// If we've already failed to load another package from this
Expand Down Expand Up @@ -535,7 +538,7 @@ class GitSource extends CachedSource {
var path = _repoCachePath(description, cache);
assert(!_updatedRepos.contains(path));
try {
await _clone(description.url, path, mirror: true);
await _mirrorClone(description.url, path);
} catch (_) {
await _deleteGitRepoIfInvalid(path);
rethrow;
Expand All @@ -555,7 +558,7 @@ class GitSource extends CachedSource {
) async {
var path = _repoCachePath(description, cache);
if (_updatedRepos.contains(path)) return false;
await git.run(['fetch'], workingDir: path);
await git.run(['fetch', 'origin'], workingDir: path);
_updatedRepos.add(path);
return true;
}
Expand Down Expand Up @@ -619,7 +622,7 @@ class GitSource extends CachedSource {
/// The path in a revision cache repository in which we keep a list of the
/// packages in the repository.
String _packageListPath(String revisionCachePath) =>
p.join(revisionCachePath, '.git/pub-packages');
p.join(revisionCachePath, '.pub-packages');

/// Runs "git rev-list" on [reference] in [path] and returns the first result.
///
Expand All @@ -640,33 +643,38 @@ class GitSource extends CachedSource {
return lines.first;
}

/// Clones the repo at the URI [from] to the path [to] on the local
/// filesystem.
///
/// If [mirror] is true, creates a bare, mirrored clone. This doesn't check
/// out the working tree, but instead makes the repository a local mirror of
/// the remote repository. See the manpage for `git clone` for more
/// information.
///
/// If [shallow] is true, creates a shallow clone that contains no history
/// for the repository.
Future<void> _clone(
String from,
String to, {
bool mirror = false,
}) async {
/// Creates a bare clone of the repo at the URI [from] to the path [to] on the
/// local filesystem.
Future<void> _mirrorClone(String from, String to) async {
// Git on Windows does not seem to automatically create the destination
// directory.
ensureDir(to);
var args = ['clone', if (mirror) '--mirror', from, to];

await git.run(args);
await git.run(['clone', '--mirror', from, to]);
}

/// Checks out the reference [ref] in [repoPath].
Future<void> _checkOut(String repoPath, String ref) {
return git
.run(['checkout', ref], workingDir: repoPath).then((result) => null);
/// Makes a working tree of the repo at the path [from] at ref [ref] to the
/// path [to] on the local filesystem.
///
/// Also checks out any submodules.
Future<void> _createWorktree(String from, String to, String ref) async {
// Git on Windows does not seem to automatically create the destination
// directory.
ensureDir(to);
await git.run(
[
'worktree', 'add',
// Checkout <branch> even if already checked out in other worktree.
// Should not be necessary, but cannot hurt either.
'--force',
to,
ref,
],
workingDir: from,
);
await git.run(
['submodule', 'update', '--init', '--recursive'],
workingDir: to,
);
}

String _revisionCachePath(PackageId id, SystemCache cache) => p.join(
Expand Down
11 changes: 9 additions & 2 deletions test/descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,15 @@ Descriptor tokensFile([Map<String, dynamic> contents = const {}]) {

/// Describes the application directory, containing only a pubspec specifying
/// the given [dependencies].
DirectoryDescriptor appDir({Map? dependencies, Map<String, Object>? pubspec}) =>
dir(appPath, [appPubspec(dependencies: dependencies, extras: pubspec)]);
DirectoryDescriptor appDir({
Map? dependencies,
Map<String, Object>? pubspec,
Iterable<Descriptor>? contents,
}) =>
dir(
appPath,
[appPubspec(dependencies: dependencies, extras: pubspec), ...?contents],
);

/// Describes a `.dart_tools/package_config.json` file.
///
Expand Down
32 changes: 32 additions & 0 deletions test/descriptor/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:pub/src/git.dart' as git;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart';

/// Describes a Git repository and its contents.
Expand Down Expand Up @@ -80,4 +82,34 @@ class GitRepoDescriptor extends DirectoryDescriptor {
await _runGit(command, parent);
}
}

/// Serves this git directory on localhost a fresh port
/// returns the port.
Future<int> serve() async {
// Use this to invent a fresh host.
final s = await ServerSocket.bind('localhost', 0);
int port = s.port;
await s.close();
final process = await Process.start(
'git',
[
'daemon',
'--verbose',
'--export-all',
'--base-path=.git',
'--reuseaddr',
'--strict-paths',
'--port=$port',
'.git/'
],
workingDirectory: path.join(sandbox, name),
);
final c = Completer();
process.stderr.listen((x) {
if (!c.isCompleted) c.complete();
});
await c.future;
addTearDown(process.kill);
return port;
}
}
49 changes: 41 additions & 8 deletions test/get/git/git_submodule_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ void main() {
).create();
await pubGet();

await runPub(
args: ['run', 'myapp:main'],
output: contains('hi'),
);
await runPub(args: ['run', 'myapp:main'], output: contains('hi'));

await d.git(
'lib.git',
Expand All @@ -48,9 +45,45 @@ void main() {
await foo.commit();

await pubUpgrade();
await runPub(
args: ['run', 'myapp:main'],
output: contains('bye'),
);
await runPub(args: ['run', 'myapp:main'], output: contains('bye'));
});

test('Can use LFS', () async {
ensureGit();

final foo = d.git('foo.git', [d.libPubspec('foo', '1.0.0')]);
await foo.create();
await foo.runGit(['lfs', 'install']);

await d.dir('foo.git', [
d.dir('lib', [d.file('foo.dart', 'main() => print("hello");')])
]).create();
await foo.runGit(['lfs', 'track', 'lib/foo.dart']);
await foo.runGit(['add', '.gitattributes']);
await foo.commit();

await d.appDir(
dependencies: {
'foo': {
'git': {'url': '../foo.git'}
}
},
contents: [
d.dir('bin', [d.file('main.dart', 'export "package:foo/foo.dart";')]),
],
).create();
await pubGet();

await runPub(args: ['run', 'myapp:main'], output: contains('hi'));

await d.git(
'foo.git',
[
d.dir('lib', [d.file('foo.dart', 'main() => print("bye");')])
],
).commit();

await pubUpgrade();
await runPub(args: ['run', 'myapp:main'], output: contains('bye'));
});
}