From ef29257f2e1701dda328eb381092f0f3bb2679d2 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Mon, 9 Aug 2021 15:55:42 +0300 Subject: [PATCH] feat(repository): add ability to set working directory --- lib/src/bindings/repository.dart | 2 +- lib/src/repository.dart | 14 ++++++++++++++ test/repository_test.dart | 10 ++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/src/bindings/repository.dart b/lib/src/bindings/repository.dart index a1d1bf2..45b9ae4 100644 --- a/lib/src/bindings/repository.dart +++ b/lib/src/bindings/repository.dart @@ -349,7 +349,7 @@ void setHeadDetachedFromAnnotated( } } -/// Set the path to the working directory for this repository +/// Set the path to the working directory for this repository. /// /// The working directory doesn't need to be the same one that contains the /// `.git` folder for this repository. diff --git a/lib/src/repository.dart b/lib/src/repository.dart index a119645..0a31064 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -167,6 +167,20 @@ class Repository { /// If the repository is bare, this function will always return empty string. String get workdir => bindings.workdir(_repoPointer); + /// Sets the path to the working directory for this repository. + /// + /// The working directory doesn't need to be the same one that contains the + /// `.git` folder for this repository. + /// + /// If this repository is bare, setting its working directory will turn it into a + /// normal repository, capable of performing all the common workdir operations + /// (checkout, status, index manipulation, etc). + /// + /// Throws a [LibGit2Error] if error occured. + void setWorkdir(String path, [bool updateGitlink = false]) { + bindings.setWorkdir(_repoPointer, path, updateGitlink); + } + /// Releases memory allocated for repository object. void free() { bindings.free(_repoPointer); diff --git a/test/repository_test.dart b/test/repository_test.dart index c825abd..4b41af0 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -154,6 +154,16 @@ void main() { expect(repo.namespace, ''); }); + test('successfully sets working directory', () { + final tmpWorkDir = '${Directory.systemTemp.path}/tmp_work_dir/'; + Directory(tmpWorkDir).createSync(); + + repo.setWorkdir(tmpWorkDir); + expect(repo.workdir, tmpWorkDir); + + Directory(tmpWorkDir).deleteSync(); + }); + group('setHead', () { test('successfully sets head when target is reference', () { expect(repo.head.name, 'refs/heads/master');