Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 18 additions & 2 deletions docs/ko/reference/Diff/Methods/print.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

포맷된 텍스트 출력을 생성하며 diff를 순회해요.

출력은 `git diff`를 따릅니다. `Patch`와 `PatchId` 포맷에서는 각 본문 줄 앞에 origin이 붙어요. 추가 줄은 `+`, 삭제 줄은 `-`, 컨텍스트 줄은 공백 한 칸이에요. 파일 헤더, hunk 헤더(`@@ ... @@`), binary 안내, `\ No newline at end of file` 마커는 그대로 출력돼요.

## 시그니처

```ts
Expand All @@ -16,7 +18,7 @@ class Diff {
<li class="param-li param-li-root">
<span class="param-name">options</span><span class="param-type">null | DiffPrintOptions</span>
<br>
<p class="param-description">츌력에 사용할 옵션이에요.</p>
<p class="param-description">출력에 사용할 옵션이에요.</p>
<ul class="param-ul">
<li class="param-li">
<span class="param-name">format</span><span class="param-type">DiffFormat</span>
Expand All @@ -33,6 +35,20 @@ class Diff {
<li class="param-li param-li-root">
<span class="param-type">string</span>
<br>
<p class="param-description">포맷팅된 출력 값을 반환해요.</p>
<p class="param-description">포맷팅된 출력 값을 반환해요. 기본 <code>Patch</code> 포맷이면 <code>git diff</code>와 같은 unified patch예요.</p>
</li>
</ul>

### 예제

```ts
const diff = repo.diffIndexToWorkdir();
console.log(diff.print());
// diff --git a/A b/A
// index f70f10e..784f93d 100644
// --- a/A
// +++ b/A
// @@ -1 +1 @@
// -A
// +A modified
```
7 changes: 3 additions & 4 deletions docs/ko/usage/diff.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ console.log(diff.print());
* --- a/first
* +++ b/first
* @@ -1 +1,2 @@
* first
* first modified
*
* first
* +first modified
* diff --git a/second b/second
* deleted file mode 100644
* index e019be0..0000000
* --- a/second
* +++ /dev/null
* @@ -1 +0,0 @@
* second
* -second
*/
```

Expand Down
18 changes: 17 additions & 1 deletion docs/reference/Diff/Methods/print.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Iterate over a diff generating formatted text output.

The output follows `git diff`. For the `Patch` and `PatchId` formats, each content line is prefixed with its origin: `+` for an added line, `-` for a deleted line and a single space for a context line. File headers, hunk headers (`@@ ... @@`), binary notices and `\ No newline at end of file` markers are emitted as-is.

## Signature

```ts
Expand Down Expand Up @@ -33,6 +35,20 @@ class Diff {
<li class="param-li param-li-root">
<span class="param-type">string</span>
<br>
<p class="param-description">Formatted text output.</p>
<p class="param-description">Formatted text output. With the default <code>Patch</code> format, this is a unified patch like <code>git diff</code>.</p>
</li>
</ul>

### Examples

```ts
const diff = repo.diffIndexToWorkdir();
console.log(diff.print());
// diff --git a/A b/A
// index f70f10e..784f93d 100644
// --- a/A
// +++ b/A
// @@ -1 +1 @@
// -A
// +A modified
```
7 changes: 3 additions & 4 deletions docs/usage/diff.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,15 @@ console.log(diff.print());
* --- a/first
* +++ b/first
* @@ -1 +1,2 @@
* first
* first modified
*
* first
* +first modified
* diff --git a/second b/second
* deleted file mode 100644
* index e019be0..0000000
* --- a/second
* +++ /dev/null
* @@ -1 +0,0 @@
* second
* -second
*/
```

Expand Down
30 changes: 28 additions & 2 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ impl Diff {
#[napi]
/// Iterate over a diff generating formatted text output.
///
/// The output follows `git diff`. For the `Patch` and `PatchId` formats, each
/// content line is prefixed with its origin: `+` for an added line, `-` for a
/// deleted line and a single space for a context line. File headers, hunk
/// headers (`@@ ... @@`), binary notices and `\ No newline at end of file`
/// markers are emitted as-is.
///
/// @category Diff/Methods
/// @signature
/// ```ts
Expand All @@ -375,13 +381,33 @@ impl Diff {
/// ```
///
/// @param {DiffPrintOptions} [options] - Print options for diff.
/// @returns Formatted text output.
/// @returns Formatted text output. With the default `Patch` format, this is a unified patch like `git diff`.
/// @example
/// ```ts
/// const diff = repo.diffIndexToWorkdir();
/// console.log(diff.print());
/// // diff --git a/A b/A
/// // index f70f10e..784f93d 100644
/// // --- a/A
/// // +++ b/A
/// // @@ -1 +1 @@
/// // -A
/// // +A modified
/// ```
pub fn print(&self, options: Option<DiffPrintOptions>) -> String {
let format = options.and_then(|x| x.format).unwrap_or_default();
let mut lines: Vec<String> = vec![];
let _ = self.inner.print(format.into(), |_delta, _hunk, line| {
if let Ok(content) = std::str::from_utf8(line.content()) {
lines.push(content.to_string());
let origin = line.origin();
match origin {
' ' | '+' | '-' => {
lines.push(format!("{}{}", origin, content));
}
_ => {
lines.push(content.to_string());
}
}
}
true
});
Expand Down
6 changes: 3 additions & 3 deletions tests/diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ index f70f10e..784f93d 100644
--- a/A
+++ b/A
@@ -1 +1 @@
A
A modified
-A
+A modified
\\ No newline at end of file
diff --git a/B b/B
deleted file mode 100644
index 223b783..0000000
--- a/B
+++ /dev/null
@@ -1 +0,0 @@
B
-B
`);
expect(diff.print({ format: 'PatchHeader' })).toEqual(`diff --git a/A b/A
index f70f10e..784f93d 100644
Expand Down