|
| 1 | +use crate::diff::Diff; |
| 2 | +use crate::index::Index; |
| 3 | +use crate::repository::Repository; |
| 4 | +use crate::tree::Tree; |
| 5 | +use napi_derive::napi; |
| 6 | + |
| 7 | +#[derive(Debug, Copy, Clone)] |
| 8 | +#[napi(string_enum)] |
| 9 | +/// Possible application locations for git_apply |
| 10 | +/// see <https://libgit2.org/libgit2/#HEAD/type/git_apply_options> |
| 11 | +pub enum ApplyLocation { |
| 12 | + /// Apply the patch to the workdir |
| 13 | + WorkDir, |
| 14 | + /// Apply the patch to the index |
| 15 | + Index, |
| 16 | + /// Apply the patch to both the working directory and the index |
| 17 | + Both, |
| 18 | +} |
| 19 | + |
| 20 | +impl From<ApplyLocation> for git2::ApplyLocation { |
| 21 | + fn from(value: ApplyLocation) -> Self { |
| 22 | + match value { |
| 23 | + ApplyLocation::Both => Self::Both, |
| 24 | + ApplyLocation::Index => Self::Index, |
| 25 | + ApplyLocation::WorkDir => Self::WorkDir, |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[napi(object, object_to_js = false)] |
| 31 | +/// Options to specify when applying a diff |
| 32 | +pub struct ApplyOptions { |
| 33 | + /// Don't actually make changes, just test that the patch applies. |
| 34 | + pub check: Option<bool>, |
| 35 | + // TODO(@seokju-na): Consider node.js is single-thread so the calling callback from Rust side |
| 36 | + // will make dead-lock. May be we should make `apply` function as async? |
| 37 | + // |
| 38 | + // #[napi(ts_type = "(data: DiffHunkData | null | undefined) => boolean")] |
| 39 | + // pub hunk_callback: Option<HunkCallback>, |
| 40 | + // #[napi(ts_type = "(data: DeltaData | null | undefined) => boolean")] |
| 41 | + // pub delta_callback: Option<DeltaCallback>, |
| 42 | +} |
| 43 | + |
| 44 | +impl From<ApplyOptions> for git2::ApplyOptions<'_> { |
| 45 | + fn from(value: ApplyOptions) -> Self { |
| 46 | + let mut options = git2::ApplyOptions::new(); |
| 47 | + if let Some(check) = value.check { |
| 48 | + options.check(check); |
| 49 | + } |
| 50 | + options |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[napi] |
| 55 | +impl Repository { |
| 56 | + #[napi] |
| 57 | + /// Apply a Diff to the given repo, making changes directly in the working directory, the index, or both. |
| 58 | + /// |
| 59 | + /// @category Repository/Methods |
| 60 | + /// ```ts |
| 61 | + /// class Repository { |
| 62 | + /// apply(diff: Diff, location: ApplyLocation, options?: ApplyOptions | null | undefined): void; |
| 63 | + /// } |
| 64 | + /// ``` |
| 65 | + /// |
| 66 | + /// @param {Diff} diff - The diff to apply |
| 67 | + /// @param {ApplyLocation} location - The location to apply |
| 68 | + /// @param {ApplyOptions} [options] - The options for the apply |
| 69 | + pub fn apply(&self, diff: &Diff, location: ApplyLocation, options: Option<ApplyOptions>) -> crate::Result<()> { |
| 70 | + self |
| 71 | + .inner |
| 72 | + .apply(&diff.inner, location.into(), options.map(|x| x.into()).as_mut())?; |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | + |
| 76 | + #[napi] |
| 77 | + /// Apply a Diff to the provided tree, and return the resulting Index. |
| 78 | + /// |
| 79 | + /// @category Repository/Methods |
| 80 | + /// @signature |
| 81 | + /// ```ts |
| 82 | + /// class Repository { |
| 83 | + /// applyToTree( |
| 84 | + /// tree: Tree, |
| 85 | + /// diff: Diff, |
| 86 | + /// options?: ApplyOptions | null | undefined |
| 87 | + /// ): Index; |
| 88 | + /// } |
| 89 | + /// ``` |
| 90 | + /// |
| 91 | + /// @param {Tree} tree - The tree to apply the diff to |
| 92 | + /// @param {Diff} diff - The diff to apply |
| 93 | + /// @param {ApplyOptions} [options] - The options for the apply |
| 94 | + /// |
| 95 | + /// @returns The postimage of the application |
| 96 | + pub fn apply_to_tree(&self, tree: &Tree, diff: &Diff, options: Option<ApplyOptions>) -> crate::Result<Index> { |
| 97 | + let inner = self |
| 98 | + .inner |
| 99 | + .apply_to_tree(&tree.inner, &diff.inner, options.map(|x| x.into()).as_mut())?; |
| 100 | + Ok(Index { inner }) |
| 101 | + } |
| 102 | +} |
0 commit comments