Radish alpha
r
rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt
Git libraries for Radicle
Radicle
Git
Merge remote-tracking branch 'origin/cloudhead/hunk-ranges'
Fintan Halpenny committed 2 years ago
commit 2a555e726ae1c978c2daf03c35ee7ab590336d2f
parent 045a2d6
3 files changed +61 -40
modified radicle-surf/src/diff.rs
@@ -17,7 +17,7 @@

//! Types that represent diff(s) in a Git repo.

-
use std::{borrow::Cow, path::PathBuf, string::FromUtf8Error};
+
use std::{borrow::Cow, ops::Range, path::PathBuf, string::FromUtf8Error};

#[cfg(feature = "serde")]
use serde::{ser, ser::SerializeStruct, Serialize, Serializer};
@@ -358,6 +358,10 @@ pub struct Stats {
pub struct Hunk<T> {
    pub header: Line,
    pub lines: Vec<T>,
+
    /// Old line range.
+
    pub old: Range<u32>,
+
    /// New line range.
+
    pub new: Range<u32>,
}

/// A set of [`Hunk`] changes.
modified radicle-surf/src/diff/git.rs
@@ -179,7 +179,12 @@ impl TryFrom<git2::Patch<'_>> for DiffContent {
                let line = Modification::try_from(line)?;
                lines.push(line);
            }
-
            hunks.push(Hunk { header, lines });
+
            hunks.push(Hunk {
+
                header,
+
                lines,
+
                old: hunk.old_start()..hunk.old_start() + hunk.old_lines(),
+
                new: hunk.new_start()..hunk.new_start() + hunk.new_lines(),
+
            });
        }
        let eof = match (old_missing_eof, new_missing_eof) {
            (true, true) => EofNewLine::BothMissing,
modified radicle-surf/t/src/diff.rs
@@ -44,6 +44,8 @@ fn test_initial_diff() -> Result<(), Error> {
                        .to_vec(),
                    1,
                )],
+
                old: 0..0,
+
                new: 1..2,
            }]
            .into(),
            eof: EofNewLine::default(),
@@ -93,6 +95,8 @@ fn test_diff_file() -> Result<(), Error> {
                    Modification::addition(b"This repository is a data source for the Upstream front-end tests and the\n".to_vec(), 1),
                    Modification::addition(b"[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n".to_vec(), 2),
                ],
+
                old: 1..2,
+
                new: 1..3,
            }]
            .into(),
            eof: EofNewLine::default(),
@@ -129,6 +133,8 @@ fn test_diff() -> Result<(), Error> {
                    Modification::addition(b"This repository is a data source for the Upstream front-end tests and the\n".to_vec(), 1),
                    Modification::addition(b"[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n".to_vec(), 2),
                ],
+
                old: 1..2,
+
                new: 1..3,
            }]
            .into(),
            eof: EofNewLine::default(),
@@ -228,7 +234,9 @@ fn test_diff_serde() -> Result<(), Error> {
                        "line": "\n",
                        "lineNo": 2,
                        "type": "addition",
-
                    }]
+
                    }],
+
                    "old": { "start": 0, "end": 0 },
+
                    "new": { "start": 1, "end": 3 },
                }],
                "eof": "noneMissing",
            },
@@ -248,42 +256,44 @@ fn test_diff_serde() -> Result<(), Error> {
                "hunks": [{
                    "header": "@@ -1,7 +0,0 @@\n",
                    "lines": [
-
                    {
-
                        "line": "  ;;;;;        ;;;;;        ;;;;;\n",
-
                        "lineNo": 1,
-
                        "type": "deletion",
-
                    },
-
                    {
-
                        "line": "  ;;;;;        ;;;;;        ;;;;;\n",
-
                        "lineNo": 2,
-
                        "type": "deletion",
-
                    },
-
                    {
-
                        "line": "  ;;;;;        ;;;;;        ;;;;;\n",
-
                        "lineNo": 3,
-
                        "type": "deletion",
-
                    },
-
                    {
-
                        "line": "  ;;;;;        ;;;;;        ;;;;;\n",
-
                        "lineNo": 4,
-
                        "type": "deletion",
-
                    },
-
                    {
-
                        "line": "..;;;;;..    ..;;;;;..    ..;;;;;..\n",
-
                        "lineNo": 5,
-
                        "type": "deletion",
-
                    },
-
                    {
-
                        "line": " ':::::'      ':::::'      ':::::'\n",
-
                        "lineNo": 6,
-
                        "type": "deletion",
-
                    },
-
                    {
-
                        "line": "   ':`          ':`          ':`\n",
-
                        "lineNo": 7,
-
                        "type": "deletion",
-
                    },
-
                    ]
+
                        {
+
                            "line": "  ;;;;;        ;;;;;        ;;;;;\n",
+
                            "lineNo": 1,
+
                            "type": "deletion",
+
                        },
+
                        {
+
                            "line": "  ;;;;;        ;;;;;        ;;;;;\n",
+
                            "lineNo": 2,
+
                            "type": "deletion",
+
                        },
+
                        {
+
                            "line": "  ;;;;;        ;;;;;        ;;;;;\n",
+
                            "lineNo": 3,
+
                            "type": "deletion",
+
                        },
+
                        {
+
                            "line": "  ;;;;;        ;;;;;        ;;;;;\n",
+
                            "lineNo": 4,
+
                            "type": "deletion",
+
                        },
+
                        {
+
                            "line": "..;;;;;..    ..;;;;;..    ..;;;;;..\n",
+
                            "lineNo": 5,
+
                            "type": "deletion",
+
                        },
+
                        {
+
                            "line": " ':::::'      ':::::'      ':::::'\n",
+
                            "lineNo": 6,
+
                            "type": "deletion",
+
                        },
+
                        {
+
                            "line": "   ':`          ':`          ':`\n",
+
                            "lineNo": 7,
+
                            "type": "deletion",
+
                        },
+
                    ],
+
                    "old": { "start": 1, "end": 8 },
+
                    "new": { "start": 0, "end": 0 },
                }],
                "eof": "noneMissing",
            },
@@ -316,7 +326,9 @@ fn test_diff_serde() -> Result<(), Error> {
                          "line": "[`radicle-surf`](https://github.com/radicle-dev/radicle-surf) unit tests.\n",
                          "type": "addition"
                        },
-
                    ]
+
                    ],
+
                    "old": { "start": 1, "end": 3 },
+
                    "new": { "start": 1, "end": 3 },
                }],
                "eof": "noneMissing",
            },