|
| 1 | +use crate::repository::Repository; |
| 2 | +use napi::bindgen_prelude::*; |
| 3 | +use napi_derive::napi; |
| 4 | + |
| 5 | +#[napi] |
| 6 | +#[repr(u32)] |
| 7 | +/// Orderings that may be specified for Revwalk iteration. |
| 8 | +pub enum RevwalkSort { |
| 9 | + /// Sort the repository contents in no particular ordering. |
| 10 | + /// |
| 11 | + /// This sorting is arbitrary, implementation-specific, and subject to |
| 12 | + /// change at any time. This is the default sorting for new walkers. |
| 13 | + None = 0, |
| 14 | + /// Sort the repository contents in topological order (children before |
| 15 | + /// parents). |
| 16 | + /// |
| 17 | + /// This sorting mode can be combined with time sorting. |
| 18 | + /// (1 << 0) |
| 19 | + Topological = 1, |
| 20 | + /// Sort the repository contents by commit time. |
| 21 | + /// |
| 22 | + /// This sorting mode can be combined with topological sorting. |
| 23 | + /// (1 << 1) |
| 24 | + Time = 2, |
| 25 | + /// Iterate through the repository contents in reverse order. |
| 26 | + /// |
| 27 | + /// This sorting mode can be combined with any others. |
| 28 | + /// (1 << 2) |
| 29 | + Reverse = 4, |
| 30 | +} |
| 31 | + |
| 32 | +#[napi(iterator)] |
| 33 | +/// A revwalk allows traversal of the commit graph defined by including one or |
| 34 | +/// more leaves and excluding one or more roots. |
| 35 | +pub struct Revwalk { |
| 36 | + pub(crate) inner: SharedReference<Repository, git2::Revwalk<'static>>, |
| 37 | +} |
| 38 | + |
| 39 | +#[napi] |
| 40 | +impl Generator for Revwalk { |
| 41 | + type Yield = String; |
| 42 | + type Next = (); |
| 43 | + type Return = (); |
| 44 | + |
| 45 | + fn next(&mut self, _value: Option<Self::Next>) -> Option<Self::Yield> { |
| 46 | + self.inner.next().and_then(|x| x.ok().map(|x| x.to_string())) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +#[napi] |
| 51 | +impl Revwalk { |
| 52 | + #[napi] |
| 53 | + /// Reset a revwalk to allow re-configuring it. |
| 54 | + /// |
| 55 | + /// The revwalk is automatically reset when iteration of its commits |
| 56 | + /// completes. |
| 57 | + pub fn reset(&mut self) -> Result<&Self> { |
| 58 | + self.inner.reset().map_err(crate::Error::from).map_err(Error::from)?; |
| 59 | + Ok(self) |
| 60 | + } |
| 61 | + |
| 62 | + #[napi] |
| 63 | + /// Set the order in which commits are visited. |
| 64 | + pub fn set_sorting(&mut self, sort: u32) -> Result<&Self> { |
| 65 | + self |
| 66 | + .inner |
| 67 | + .set_sorting(git2::Sort::from_bits_retain(sort)) |
| 68 | + .map_err(crate::Error::from) |
| 69 | + .map_err(Error::from)?; |
| 70 | + Ok(self) |
| 71 | + } |
| 72 | + |
| 73 | + #[napi] |
| 74 | + /// Simplify the history by first-parent |
| 75 | + /// |
| 76 | + /// No parents other than the first for each commit will be enqueued. |
| 77 | + pub fn simplify_first_parent(&mut self) -> Result<&Self> { |
| 78 | + self |
| 79 | + .inner |
| 80 | + .simplify_first_parent() |
| 81 | + .map_err(crate::Error::from) |
| 82 | + .map_err(Error::from)?; |
| 83 | + Ok(self) |
| 84 | + } |
| 85 | + |
| 86 | + #[napi] |
| 87 | + /// Mark a commit to start traversal from. |
| 88 | + /// |
| 89 | + /// The given OID must belong to a commitish on the walked repository. |
| 90 | + /// |
| 91 | + /// The given commit will be used as one of the roots when starting the |
| 92 | + /// revision walk. At least one commit must be pushed onto the walker before |
| 93 | + /// a walk can be started. |
| 94 | + pub fn push(&mut self, oid: String) -> Result<&Self> { |
| 95 | + let oid = git2::Oid::from_str(&oid) |
| 96 | + .map_err(crate::Error::from) |
| 97 | + .map_err(Error::from)?; |
| 98 | + self.inner.push(oid).map_err(crate::Error::from).map_err(Error::from)?; |
| 99 | + Ok(self) |
| 100 | + } |
| 101 | + |
| 102 | + #[napi] |
| 103 | + /// Push the repository's HEAD |
| 104 | + /// |
| 105 | + /// For more information, see `push`. |
| 106 | + pub fn push_head(&mut self) -> Result<&Self> { |
| 107 | + self |
| 108 | + .inner |
| 109 | + .push_head() |
| 110 | + .map_err(crate::Error::from) |
| 111 | + .map_err(Error::from)?; |
| 112 | + Ok(self) |
| 113 | + } |
| 114 | + |
| 115 | + #[napi] |
| 116 | + /// Push matching references |
| 117 | + /// |
| 118 | + /// The OIDs pointed to by the references that match the given glob pattern |
| 119 | + /// will be pushed to the revision walker. |
| 120 | + /// |
| 121 | + /// A leading 'refs/' is implied if not present as well as a trailing `/ \ |
| 122 | + /// *` if the glob lacks '?', ' \ *' or '['. |
| 123 | + /// |
| 124 | + /// Any references matching this glob which do not point to a commitish |
| 125 | + /// will be ignored. |
| 126 | + pub fn push_glob(&mut self, glob: String) -> Result<&Self> { |
| 127 | + self |
| 128 | + .inner |
| 129 | + .push_glob(&glob) |
| 130 | + .map_err(crate::Error::from) |
| 131 | + .map_err(Error::from)?; |
| 132 | + Ok(self) |
| 133 | + } |
| 134 | + |
| 135 | + #[napi] |
| 136 | + /// Push and hide the respective endpoints of the given range. |
| 137 | + /// |
| 138 | + /// The range should be of the form `<commit>..<commit>` where each |
| 139 | + /// `<commit>` is in the form accepted by `revparse_single`. The left-hand |
| 140 | + /// commit will be hidden and the right-hand commit pushed. |
| 141 | + pub fn push_range(&mut self, range: String) -> Result<&Self> { |
| 142 | + self |
| 143 | + .inner |
| 144 | + .push_range(&range) |
| 145 | + .map_err(crate::Error::from) |
| 146 | + .map_err(Error::from)?; |
| 147 | + Ok(self) |
| 148 | + } |
| 149 | + |
| 150 | + #[napi] |
| 151 | + /// Push the OID pointed to by a reference |
| 152 | + /// |
| 153 | + /// The reference must point to a commitish. |
| 154 | + pub fn push_ref(&mut self, reference: String) -> Result<&Self> { |
| 155 | + self |
| 156 | + .inner |
| 157 | + .push_ref(&reference) |
| 158 | + .map_err(crate::Error::from) |
| 159 | + .map_err(Error::from)?; |
| 160 | + Ok(self) |
| 161 | + } |
| 162 | + |
| 163 | + #[napi] |
| 164 | + /// Mark a commit as not of interest to this revwalk. |
| 165 | + pub fn hide(&mut self, oid: String) -> Result<&Self> { |
| 166 | + let oid = git2::Oid::from_str(&oid) |
| 167 | + .map_err(crate::Error::from) |
| 168 | + .map_err(Error::from)?; |
| 169 | + self.inner.hide(oid).map_err(crate::Error::from).map_err(Error::from)?; |
| 170 | + Ok(self) |
| 171 | + } |
| 172 | + |
| 173 | + #[napi] |
| 174 | + /// Hide the repository's HEAD |
| 175 | + /// |
| 176 | + /// For more information, see `hide`. |
| 177 | + pub fn hide_head(&mut self) -> Result<&Self> { |
| 178 | + self |
| 179 | + .inner |
| 180 | + .hide_head() |
| 181 | + .map_err(crate::Error::from) |
| 182 | + .map_err(Error::from)?; |
| 183 | + Ok(self) |
| 184 | + } |
| 185 | + |
| 186 | + #[napi] |
| 187 | + /// Hide matching references. |
| 188 | + /// |
| 189 | + /// The OIDs pointed to by the references that match the given glob pattern |
| 190 | + /// and their ancestors will be hidden from the output on the revision walk. |
| 191 | + /// |
| 192 | + /// A leading 'refs/' is implied if not present as well as a trailing `/ \ |
| 193 | + /// *` if the glob lacks '?', ' \ *' or '['. |
| 194 | + /// |
| 195 | + /// Any references matching this glob which do not point to a commitish |
| 196 | + /// will be ignored. |
| 197 | + pub fn hide_glob(&mut self, glob: String) -> Result<&Self> { |
| 198 | + self |
| 199 | + .inner |
| 200 | + .hide_glob(&glob) |
| 201 | + .map_err(crate::Error::from) |
| 202 | + .map_err(Error::from)?; |
| 203 | + Ok(self) |
| 204 | + } |
| 205 | + |
| 206 | + #[napi] |
| 207 | + /// Hide the OID pointed to by a reference. |
| 208 | + /// |
| 209 | + /// The reference must point to a commitish. |
| 210 | + pub fn hide_ref(&mut self, reference: String) -> Result<&Self> { |
| 211 | + self |
| 212 | + .inner |
| 213 | + .hide_ref(&reference) |
| 214 | + .map_err(crate::Error::from) |
| 215 | + .map_err(Error::from)?; |
| 216 | + Ok(self) |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +#[napi] |
| 221 | +impl Repository { |
| 222 | + #[napi] |
| 223 | + /// Create a revwalk that can be used to traverse the commit graph. |
| 224 | + pub fn revwalk(&self, this: Reference<Repository>, env: Env) -> crate::Result<Revwalk> { |
| 225 | + let inner = this.share_with(env, |repo| { |
| 226 | + repo.inner.revwalk().map_err(crate::Error::from).map_err(|e| e.into()) |
| 227 | + })?; |
| 228 | + Ok(Revwalk { inner }) |
| 229 | + } |
| 230 | +} |
0 commit comments