From f5e43f3d902538d520de31d2e1bf280e18ae774f Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Wed, 11 Aug 2021 18:51:33 +0300 Subject: [PATCH] docs(reference): update examples --- example/helpers.dart | 16 ++++++++++++++++ example/reference_example.dart | 29 +++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 example/helpers.dart diff --git a/example/helpers.dart b/example/helpers.dart new file mode 100644 index 0000000..ecf2712 --- /dev/null +++ b/example/helpers.dart @@ -0,0 +1,16 @@ +import 'dart:io'; +import '../test/helpers/util.dart'; + +Future prepareRepo(String path) async { + if (await Directory(path).exists()) { + await Directory(path).delete(recursive: true); + } + await copyRepo( + from: Directory('test/assets/testrepo/'), + to: await Directory(path).create(), + ); +} + +Future disposeRepo(String path) async { + await Directory(path).delete(recursive: true); +} diff --git a/example/reference_example.dart b/example/reference_example.dart index 97844c3..3108b7a 100644 --- a/example/reference_example.dart +++ b/example/reference_example.dart @@ -1,18 +1,43 @@ import 'dart:io'; import 'package:libgit2dart/libgit2dart.dart'; +import 'helpers.dart'; -void main() { - final repo = Repository.open(Directory.current.path); +void main() async { + // Preparing example repository. + final tmpDir = '${Directory.systemTemp.path}/example_repo/'; + await prepareRepo(tmpDir); + final repo = Repository.open(tmpDir); + + // Get list of repo's references. print('Repository references: ${repo.references.list()}'); + // Get reference by name. final ref = repo.references['refs/heads/master']; print('Reference SHA hex: ${ref.target.sha}'); print('Is reference a local branch: ${ref.isBranch}'); print('Reference full name: ${ref.name}'); + print('Reference shorthand name: ${ref.shorthand}'); + // Create new reference (direct or symbolic). + final newRef = repo.createReference( + name: 'refs/tags/v1', + target: 'refs/heads/master', + ); + + // Rename reference. + newRef.rename('refs/tags/v1.1'); + + // Delete reference. + newRef.delete(); + + // free() should be called on object to free memory when done. ref.free(); + newRef.free(); repo.free(); + + // Removing example repository. + await disposeRepo(tmpDir); }