Skip to content

Commit 9d0037c

Browse files
authored
feat: change RepositoryOpenFlags into options (#61)
* destruct repository opening options * built * update docs * tests
1 parent db6b325 commit 9d0037c

File tree

10 files changed

+172
-168
lines changed

10 files changed

+172
-168
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Use following tags to create robust references using JSDoc.
1111
- `@throws` : Write documentation for errors that should be noted.
1212
- `@example` : Examples are not required, but are recommended as they make the behavior easier to understand.
1313

14-
## Generate Reference
14+
## Generating Reference
1515

1616
To automatically generate reference documentations after writing a JSDoc, please run the command below.
1717

docs/ko/reference/Repository/openRepository.md

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,42 @@ function openRepository(
1818
<li class="param-li param-li-root">
1919
<span class="param-name">path</span><span class="param-required">필수</span>&nbsp;·&nbsp;<span class="param-type">string</span>
2020
<br>
21-
<p class="param-description">열려고 하는 기존 리포지토리의 디렉터리 경로예요.</p>
21+
<p class="param-description">이미 존재하는 리포지토리가 위치한 디렉터리 경로예요.</p>
2222
</li>
2323
<li class="param-li param-li-root">
2424
<span class="param-name">options</span><span class="param-type">null | RepositoryOpenOptions</span>
2525
<br>
26-
<p class="param-description">리포지토리 여는 방식을 설정하는 옵션이에요.</p>
26+
<p class="param-description">리포지토리를 오픈할 때 적용할 옵션들을 설정할 수 있어요.</p>
2727
<ul class="param-ul">
28+
<li class="param-li">
29+
<span class="param-name">bare</span><span class="param-type">boolean</span>
30+
<br>
31+
<p class="param-description">이 옵션이 <code>true</code>로 설정되면, 리포지토리가 bare하지 않더라도 강제로 bare 형태로 열며, 워킹 디렉터리는 무시하고 리포지토리 구성을 성능 향상을 위해 지연해요.</p>
32+
</li>
2833
<li class="param-li">
2934
<span class="param-name">ceilingDirs</span><span class="param-type">string[]</span>
3035
<br>
31-
<p class="param-description">상위 디렉터리를 검색할 때, 이 목록에 포함된 디렉터리 이전에서 검색을 멈춰요.</p>
36+
<p class="param-description">부모 디렉터리 탐색 중에, 진입하기 전에 멈출 경로들의 목록을 지정해요.</p>
37+
</li>
38+
<li class="param-li">
39+
<span class="param-name">crossFs</span><span class="param-type">boolean</span>
40+
<br>
41+
<p class="param-description">이 옵션이 <code>true</code>로 설정되면, 상위 디렉터리 탐색 시 파일시스템 경계를 넘지 않도록 해요 (stat의 st_dev 값이 변경될 때 감지해요).</p>
42+
</li>
43+
<li class="param-li">
44+
<span class="param-name">fromEnv</span><span class="param-type">boolean</span>
45+
<br>
46+
<p class="param-description">이 옵션이 <code>true</code>로 설정되면, <code>open</code>은 다른 옵션과 <code>ceilingDirs</code>를 무시하고 git이 사용하는 환경 변수를 따르게 돼요. 다만, <code>path</code>가 <code>$GIT_DIR</code>을 덮어써요.</p>
3247
</li>
3348
<li class="param-li">
34-
<span class="param-name">flags</span><span class="param-required">필수</span>&nbsp;·&nbsp;<span class="param-type">number</span>
49+
<span class="param-name">noDotgit</span><span class="param-type">boolean</span>
3550
<br>
36-
<p class="param-description">
37-
<code>flags</code>에 따라 리포지토리를 여는 방식이 달라져요.
38-
<br>
39-
- <code>RepositoryOpenFlags.NoSearch</code> :
40-
<code>path</code>가 반드시 리포지토리 경로여야 해요.
41-
(<code>open</code>이 상위 디렉터리를 검색하지 않음)
42-
설정하지 않으면 서브디렉터리에서 호출해도 상위 디렉터리를 검색해요.
43-
<br>
44-
- <code>RepositoryOpenFlags.CrossFS</code> :
45-
상위 디렉터리를 검색할 때 파일 시스템 경계를 넘지 않도록 설정해요.
46-
(<code>stat</code>의 <code>st_dev</code> 필드가 변경되면 검색을 중단)
47-
<br>
48-
- <code>RepositoryOpenFlags.Bare</code> :
49-
<code>bare</code> 리포지토리로 강제로 열어요.
50-
(워킹 디렉터리를 무시하고, 성능을 위해 리포지토리 설정을 지연 로드)
51-
<br>
52-
- <code>RepositoryOpenFlags.NoDotgit</code> :
53-
<code>path</code>에 자동으로 <code>/.git</code>을 추가하는 동작을 방지해요.
54-
<br>
55-
- <code>RepositoryOpenFlags.FromEnv</code> :
56-
환경 변수를 기반으로 리포지토리를 열어요.
57-
(다른 플래그 및 <code>ceilingDirs</code> 설정을 무시하고 Git의 환경 변수 사용)
58-
단, <code>path</code> 값이 <code>$GIT_DIR</code>보다 우선돼요.</p>
51+
<p class="param-description">이 옵션이 <code>true</code>로 설정되면, <code>path</code>에 <code>/.git</code>을 덧붙이지 않아요.</p>
52+
</li>
53+
<li class="param-li">
54+
<span class="param-name">noSearch</span><span class="param-type">boolean</span>
55+
<br>
56+
<p class="param-description">이 옵션이 <code>true</code>로 설정되면, 경로가 리포지토리를 직접 가리켜야 해요. 그렇지 않으면 <code>open</code>이 상위 디렉터리로 탐색해요.</p>
5957
</li>
6058
</ul>
6159
</li>
@@ -86,22 +84,12 @@ import { openRepository } from 'es-git';
8684
const repo = await openRepository('/path/to/repo');
8785
```
8886

89-
bare 리포지토리를 열어요.
87+
`bare` 리포지토리를 열어요.
9088

9189
```ts
9290
import { openRepository } from 'es-git';
9391

9492
const repo = await openRepository('/path/to/repo.git', {
9593
bare: true,
9694
});
97-
```
98-
99-
하위 경로에서 리포지토리를 열어요.
100-
101-
```ts
102-
import { openRepository, RepositoryOpenFlags } from 'es-git';
103-
104-
const repo = await openRepository('/path/to/repo/sub/dir', {
105-
flags: RepositoryOpenFlags.CrossFS,
106-
});
10795
```

docs/reference/Repository/openRepository.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,35 @@ function openRepository(
2525
<br>
2626
<p class="param-description">Options which can be used to configure how a repository is initialized.</p>
2727
<ul class="param-ul">
28+
<li class="param-li">
29+
<span class="param-name">bare</span><span class="param-type">boolean</span>
30+
<br>
31+
<p class="param-description">If this option is <code>true</code>, force opening the repository as bare event if it isn&#39;t, ignoring any working directory, and defer loading the repository configuration for performance.</p>
32+
</li>
2833
<li class="param-li">
2934
<span class="param-name">ceilingDirs</span><span class="param-type">string[]</span>
3035
<br>
3136
<p class="param-description">ceiling_dirs specifies a list of paths that the search through parent directories will stop before entering.</p>
3237
</li>
3338
<li class="param-li">
34-
<span class="param-name">flags</span><span class="param-required">required</span>&nbsp;·&nbsp;<span class="param-type">number</span>
39+
<span class="param-name">crossFs</span><span class="param-type">boolean</span>
40+
<br>
41+
<p class="param-description">If this option is <code>true</code>, the search through parent directories will not cross a filesystem boundary (detected when the stat st_dev field changes).</p>
42+
</li>
43+
<li class="param-li">
44+
<span class="param-name">fromEnv</span><span class="param-type">boolean</span>
45+
<br>
46+
<p class="param-description">If this option is <code>true</code>, <code>open</code> will ignore other options and <code>ceilingDirs</code>, and respect the same environment variables git does. Note, however, that <code>path</code> overrides <code>$GIT_DIR</code>.</p>
47+
</li>
48+
<li class="param-li">
49+
<span class="param-name">noDotgit</span><span class="param-type">boolean</span>
3550
<br>
36-
<p class="param-description">If flags contains <code>RepositoryOpenFlags.NoSearch</code>, the path must point directly to a repository; otherwise, this may point to a subdirectory of a repository, and <code>open</code> will search up through parent directories. If flags contains <code>RepositoryOpenFlags.CrossFS</code>, the search through parent directories will not cross a filesystem boundary (detected when the stat st_dev field changes). If flags contains <code>RepositoryOpenFlags.Bare</code>, force opening the repository as bare even if it isn&#39;t, ignoring any working directory, and defer loading the repository configuration for performance. If flags contains <code>RepositoryOpenFlags.NoDotgit</code>, don&#39;t try appending <code>/.git</code> to <code>path</code>. If flags contains <code>RepositoryOpenFlags.FromEnv</code>, <code>open</code> will ignore other flags and <code>ceilingDirs</code>, and respect the same environment variables git does. Note, however, that <code>path</code> overrides <code>$GIT_DIR</code>.</p>
51+
<p class="param-description">If this options is <code>true</code>, don&#39;t try appending <code>/.git</code> to <code>path</code>.</p>
52+
</li>
53+
<li class="param-li">
54+
<span class="param-name">noSearch</span><span class="param-type">boolean</span>
55+
<br>
56+
<p class="param-description">If this option is <code>true</code>, the path must point directly to a repository; otherwise, this may point to a subdirectory of a repository, and <code>open</code> will search up through parent directories.</p>
3757
</li>
3858
</ul>
3959
</li>
@@ -72,14 +92,4 @@ import { openRepository } from 'es-git';
7292
const repo = await openRepository('/path/to/repo.git', {
7393
bare: true,
7494
});
75-
```
76-
77-
Open in a subdirectory of the repository
78-
79-
```ts
80-
import { openRepository, RepositoryOpenFlags } from 'es-git';
81-
82-
const repo = await openRepository('/path/to/repo/sub/dir', {
83-
flags: RepositoryOpenFlags.CrossFS,
84-
});
8595
```

index.d.ts

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -781,46 +781,35 @@ export interface RepositoryInitOptions {
781781
}
782782
export interface RepositoryOpenOptions {
783783
/**
784-
* If flags contains `RepositoryOpenFlags.NoSearch`, the path must point
785-
* directly to a repository; otherwise, this may point to a subdirectory
786-
* of a repository, and `open` will search up through parent
784+
* If this option is `true`, the path must point directly to a repository; otherwise,
785+
* this may point to a subdirectory of a repository, and `open` will search up through parent
787786
* directories.
788-
*
789-
* If flags contains `RepositoryOpenFlags.CrossFS`, the search through parent
790-
* directories will not cross a filesystem boundary (detected when the
791-
* stat st_dev field changes).
792-
*
793-
* If flags contains `RepositoryOpenFlags.Bare`, force opening the repository as
794-
* bare even if it isn't, ignoring any working directory, and defer
795-
* loading the repository configuration for performance.
796-
*
797-
* If flags contains `RepositoryOpenFlags.NoDotgit`, don't try appending
798-
* `/.git` to `path`.
799-
*
800-
* If flags contains `RepositoryOpenFlags.FromEnv`, `open` will ignore
801-
* other flags and `ceilingDirs`, and respect the same environment
802-
* variables git does. Note, however, that `path` overrides `$GIT_DIR`.
803787
*/
804-
flags: number
788+
noSearch?: boolean
789+
/**
790+
* If this option is `true`, the search through parent directories will not cross
791+
* a filesystem boundary (detected when the stat st_dev field changes).
792+
*/
793+
crossFs?: boolean
794+
/**
795+
* If this option is `true`, force opening the repository as bare event if it isn't, ignoring
796+
* any working directory, and defer loading the repository configuration for performance.
797+
*/
798+
bare?: boolean
799+
/** If this options is `true`, don't try appending `/.git` to `path`. */
800+
noDotgit?: boolean
801+
/**
802+
* If this option is `true`, `open` will ignore other options and `ceilingDirs`, and respect
803+
* the same environment variables git does.
804+
* Note, however, that `path` overrides `$GIT_DIR`.
805+
*/
806+
fromEnv?: boolean
805807
/**
806808
* ceiling_dirs specifies a list of paths that the search through parent
807809
* directories will stop before entering.
808810
*/
809811
ceilingDirs?: Array<string>
810812
}
811-
/** Flags for opening repository. */
812-
export enum RepositoryOpenFlags {
813-
/** Only open the specified path; don't walk upward searching. */
814-
NoSearch = 1,
815-
/** Search across filesystem boundaries. */
816-
CrossFS = 2,
817-
/** Force opening as a bare repository, and defer loading its config. */
818-
Bare = 4,
819-
/** Don't try appending `/.git` to the specified repository path. */
820-
NoDotGit = 8,
821-
/** Respect environment variables like `$GIT_DIR`. */
822-
FromEnv = 16
823-
}
824813
export interface RepositoryCloneOptions {
825814
/**
826815
* Indicate whether the repository will be cloned as a bare repository or
@@ -923,16 +912,6 @@ export declare function initRepository(path: string, options?: RepositoryInitOpt
923912
* bare: true,
924913
* });
925914
* ```
926-
*
927-
* Open in a subdirectory of the repository
928-
*
929-
* ```ts
930-
* import { openRepository, RepositoryOpenFlags } from 'es-git';
931-
*
932-
* const repo = await openRepository('/path/to/repo/sub/dir', {
933-
* flags: RepositoryOpenFlags.CrossFS,
934-
* });
935-
* ```
936915
*/
937916
export declare function openRepository(path: string, options?: RepositoryOpenOptions | undefined | null, signal?: AbortSignal | undefined | null): Promise<Repository>
938917
/**

index.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/repository.rs

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -162,46 +162,49 @@ impl From<&RepositoryInitOptions> for git2::RepositoryInitOptions {
162162
}
163163

164164
#[napi(object)]
165+
#[derive(Clone, Default)]
165166
pub struct RepositoryOpenOptions {
166-
/// If flags contains `RepositoryOpenFlags.NoSearch`, the path must point
167-
/// directly to a repository; otherwise, this may point to a subdirectory
168-
/// of a repository, and `open` will search up through parent
167+
/// If this option is `true`, the path must point directly to a repository; otherwise,
168+
/// this may point to a subdirectory of a repository, and `open` will search up through parent
169169
/// directories.
170-
///
171-
/// If flags contains `RepositoryOpenFlags.CrossFS`, the search through parent
172-
/// directories will not cross a filesystem boundary (detected when the
173-
/// stat st_dev field changes).
174-
///
175-
/// If flags contains `RepositoryOpenFlags.Bare`, force opening the repository as
176-
/// bare even if it isn't, ignoring any working directory, and defer
177-
/// loading the repository configuration for performance.
178-
///
179-
/// If flags contains `RepositoryOpenFlags.NoDotgit`, don't try appending
180-
/// `/.git` to `path`.
181-
///
182-
/// If flags contains `RepositoryOpenFlags.FromEnv`, `open` will ignore
183-
/// other flags and `ceilingDirs`, and respect the same environment
184-
/// variables git does. Note, however, that `path` overrides `$GIT_DIR`.
185-
pub flags: u32,
170+
pub no_search: Option<bool>,
171+
/// If this option is `true`, the search through parent directories will not cross
172+
/// a filesystem boundary (detected when the stat st_dev field changes).
173+
pub cross_fs: Option<bool>,
174+
/// If this option is `true`, force opening the repository as bare event if it isn't, ignoring
175+
/// any working directory, and defer loading the repository configuration for performance.
176+
pub bare: Option<bool>,
177+
/// If this options is `true`, don't try appending `/.git` to `path`.
178+
pub no_dotgit: Option<bool>,
179+
/// If this option is `true`, `open` will ignore other options and `ceilingDirs`, and respect
180+
/// the same environment variables git does.
181+
/// Note, however, that `path` overrides `$GIT_DIR`.
182+
pub from_env: Option<bool>,
186183
/// ceiling_dirs specifies a list of paths that the search through parent
187184
/// directories will stop before entering.
188185
pub ceiling_dirs: Option<Vec<String>>,
189186
}
190187

191-
#[napi]
192-
#[repr(u32)]
193-
/// Flags for opening repository.
194-
pub enum RepositoryOpenFlags {
195-
/// Only open the specified path; don't walk upward searching.
196-
NoSearch = 1,
197-
/// Search across filesystem boundaries.
198-
CrossFS = 2,
199-
/// Force opening as a bare repository, and defer loading its config.
200-
Bare = 4,
201-
/// Don't try appending `/.git` to the specified repository path.
202-
NoDotGit = 8,
203-
/// Respect environment variables like `$GIT_DIR`.
204-
FromEnv = 16,
188+
impl RepositoryOpenOptions {
189+
pub fn flags(&self) -> git2::RepositoryOpenFlags {
190+
let mut flags = git2::RepositoryOpenFlags::empty();
191+
if let Some(true) = self.no_search {
192+
flags.insert(git2::RepositoryOpenFlags::NO_SEARCH);
193+
}
194+
if let Some(true) = self.cross_fs {
195+
flags.insert(git2::RepositoryOpenFlags::CROSS_FS);
196+
}
197+
if let Some(true) = self.bare {
198+
flags.insert(git2::RepositoryOpenFlags::BARE);
199+
}
200+
if let Some(true) = self.no_dotgit {
201+
flags.insert(git2::RepositoryOpenFlags::NO_DOTGIT);
202+
}
203+
if let Some(true) = self.from_env {
204+
flags.insert(git2::RepositoryOpenFlags::FROM_ENV);
205+
}
206+
flags
207+
}
205208
}
206209

207210
#[napi(object)]
@@ -501,14 +504,10 @@ impl Task for OpenRepositoryTask {
501504
type JsValue = Repository;
502505

503506
fn compute(&mut self) -> Result<Self::Output> {
504-
let inner = if let Some(opts) = &self.options {
505-
let flags = git2::RepositoryOpenFlags::from_bits_truncate(opts.flags);
506-
let ceiling_dirs = opts.ceiling_dirs.to_owned().unwrap_or_default();
507-
git2::Repository::open_ext(&self.path, flags, ceiling_dirs)
508-
} else {
509-
git2::Repository::open(&self.path)
510-
}
511-
.map_err(crate::Error::from)?;
507+
let options = self.options.to_owned().unwrap_or_default();
508+
let flags = options.flags();
509+
let ceiling_dirs = options.ceiling_dirs.to_owned().unwrap_or_default();
510+
let inner = git2::Repository::open_ext(&self.path, flags, ceiling_dirs).map_err(crate::Error::from)?;
512511
Ok(Repository { inner })
513512
}
514513

@@ -556,16 +555,6 @@ impl Task for OpenRepositoryTask {
556555
/// bare: true,
557556
/// });
558557
/// ```
559-
///
560-
/// Open in a subdirectory of the repository
561-
///
562-
/// ```ts
563-
/// import { openRepository, RepositoryOpenFlags } from 'es-git';
564-
///
565-
/// const repo = await openRepository('/path/to/repo/sub/dir', {
566-
/// flags: RepositoryOpenFlags.CrossFS,
567-
/// });
568-
/// ```
569558
pub fn open_repository(
570559
path: String,
571560
options: Option<RepositoryOpenOptions>,

0 commit comments

Comments
 (0)