diff --git a/docs/ko/reference/Diff/Methods/print.md b/docs/ko/reference/Diff/Methods/print.md index de6192fc..0bcf616d 100644 --- a/docs/ko/reference/Diff/Methods/print.md +++ b/docs/ko/reference/Diff/Methods/print.md @@ -2,6 +2,8 @@ 포맷된 텍스트 출력을 생성하며 diff를 순회해요. +출력은 `git diff`를 따릅니다. `Patch`와 `PatchId` 포맷에서는 각 본문 줄 앞에 origin이 붙어요. 추가 줄은 `+`, 삭제 줄은 `-`, 컨텍스트 줄은 공백 한 칸이에요. 파일 헤더, hunk 헤더(`@@ ... @@`), binary 안내, `\ No newline at end of file` 마커는 그대로 출력돼요. + ## 시그니처 ```ts @@ -16,7 +18,7 @@ class Diff {
  • optionsnull | DiffPrintOptions
    -

    츌력에 사용할 옵션이에요.

    +

    출력에 사용할 옵션이에요.

    + +### 예제 + +```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 +``` diff --git a/docs/ko/usage/diff.md b/docs/ko/usage/diff.md index 88d682b7..83d01a87 100644 --- a/docs/ko/usage/diff.md +++ b/docs/ko/usage/diff.md @@ -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 */ ``` diff --git a/docs/reference/Diff/Methods/print.md b/docs/reference/Diff/Methods/print.md index b2521483..fdfb23aa 100644 --- a/docs/reference/Diff/Methods/print.md +++ b/docs/reference/Diff/Methods/print.md @@ -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 @@ -33,6 +35,20 @@ class Diff {
  • string
    -

    Formatted text output.

    +

    Formatted text output. With the default Patch format, this is a unified patch like git diff.

  • + +### 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 +``` diff --git a/docs/usage/diff.md b/docs/usage/diff.md index e1f50d93..c2d2d84b 100644 --- a/docs/usage/diff.md +++ b/docs/usage/diff.md @@ -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 */ ``` diff --git a/src/diff.rs b/src/diff.rs index 13a852b8..34d08325 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -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 @@ -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) -> String { let format = options.and_then(|x| x.format).unwrap_or_default(); let mut lines: Vec = 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 }); diff --git a/tests/diff.spec.ts b/tests/diff.spec.ts index 0ee56fca..c2be4764 100644 --- a/tests/diff.spec.ts +++ b/tests/diff.spec.ts @@ -163,8 +163,8 @@ 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 @@ -172,7 +172,7 @@ 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