| 1 |
// Copyright 2020 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#![expect(missing_docs)] |
| 16 |
|
| 17 |
use std::cmp::Ordering; |
| 18 |
use std::fmt::Debug; |
| 19 |
use std::fmt::Error; |
| 20 |
use std::fmt::Formatter; |
| 21 |
use std::hash::Hash; |
| 22 |
use std::hash::Hasher; |
| 23 |
use std::sync::Arc; |
| 24 |
|
| 25 |
use futures::future::try_join_all; |
| 26 |
use itertools::Itertools as _; |
| 27 |
|
| 28 |
use crate::backend; |
| 29 |
use crate::backend::BackendError; |
| 30 |
use crate::backend::BackendResult; |
| 31 |
use crate::backend::ChangeId; |
| 32 |
use crate::backend::CommitId; |
| 33 |
use crate::backend::Signature; |
| 34 |
use crate::backend::TreeId; |
| 35 |
use crate::conflict_labels::ConflictLabels; |
| 36 |
use crate::index::IndexResult; |
| 37 |
use crate::merge::Merge; |
| 38 |
use crate::merged_tree::MergedTree; |
| 39 |
use crate::repo::Repo; |
| 40 |
use crate::rewrite::merge_commit_trees; |
| 41 |
use crate::rewrite::merge_commit_trees_no_resolve; |
| 42 |
use crate::signing::SignResult; |
| 43 |
use crate::signing::Verification; |
| 44 |
use crate::store::Store; |
| 45 |
|
| 46 |
#[derive(Clone, serde::Serialize)] |
| 47 |
pub struct Commit { |
| 48 |
#[serde(skip)] |
| 49 |
store: Arc<Store>, |
| 50 |
#[serde(rename = "commit_id")] |
| 51 |
id: CommitId, |
| 52 |
#[serde(flatten)] |
| 53 |
data: Arc<backend::Commit>, |
| 54 |
} |
| 55 |
|
| 56 |
impl Debug for Commit { |
| 57 |
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { |
| 58 |
f.debug_struct("Commit").field("id", &self.id).finish() |
| 59 |
// We intentionally don't print the `data` field. You can debug-print |
| 60 |
// `commit.store_commit()` to get those details. |
| 61 |
// |
| 62 |
// The reason is that `Commit` objects are debug-printed as part of many |
| 63 |
// other data structures and in tracing. |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl PartialEq for Commit { |
| 68 |
fn eq(&self, other: &Self) -> bool { |
| 69 |
self.id == other.id |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl Eq for Commit {} |
| 74 |
|
| 75 |
impl Ord for Commit { |
| 76 |
fn cmp(&self, other: &Self) -> Ordering { |
| 77 |
self.id.cmp(&other.id) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl PartialOrd for Commit { |
| 82 |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 83 |
Some(self.cmp(other)) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl Hash for Commit { |
| 88 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 89 |
self.id.hash(state); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl Commit { |
| 94 |
pub fn new(store: Arc<Store>, id: CommitId, data: Arc<backend::Commit>) -> Self { |
| 95 |
Self { store, id, data } |
| 96 |
} |
| 97 |
|
| 98 |
pub fn store(&self) -> &Arc<Store> { |
| 99 |
&self.store |
| 100 |
} |
| 101 |
|
| 102 |
pub fn id(&self) -> &CommitId { |
| 103 |
&self.id |
| 104 |
} |
| 105 |
|
| 106 |
pub fn parent_ids(&self) -> &[CommitId] { |
| 107 |
&self.data.parents |
| 108 |
} |
| 109 |
|
| 110 |
pub async fn parents(&self) -> BackendResult<Vec<Self>> { |
| 111 |
try_join_all( |
| 112 |
self.data |
| 113 |
.parents |
| 114 |
.iter() |
| 115 |
.map(|id| self.store.get_commit_async(id)), |
| 116 |
) |
| 117 |
.await |
| 118 |
} |
| 119 |
|
| 120 |
pub fn tree(&self) -> MergedTree { |
| 121 |
MergedTree::new( |
| 122 |
self.store.clone(), |
| 123 |
self.data.root_tree.clone(), |
| 124 |
ConflictLabels::from_merge(self.data.conflict_labels.clone()), |
| 125 |
) |
| 126 |
} |
| 127 |
|
| 128 |
pub fn tree_ids(&self) -> &Merge<TreeId> { |
| 129 |
&self.data.root_tree |
| 130 |
} |
| 131 |
|
| 132 |
/// Return the parent tree, merging the parent trees if there are multiple |
| 133 |
/// parents. |
| 134 |
pub async fn parent_tree(&self, repo: &dyn Repo) -> BackendResult<MergedTree> { |
| 135 |
// Avoid merging parent trees if known to be empty. The index could be |
| 136 |
// queried only when parents.len() > 1, but index query would be cheaper |
| 137 |
// than extracting parent commit from the store. |
| 138 |
if is_commit_empty_by_index(repo, &self.id).await? == Some(true) { |
| 139 |
return Ok(self.tree()); |
| 140 |
} |
| 141 |
let parents = self.parents().await?; |
| 142 |
merge_commit_trees(repo, &parents).await |
| 143 |
} |
| 144 |
|
| 145 |
/// Returns the parent tree, merging the parent trees if there are multiple |
| 146 |
/// parents, without resolving conflicts. |
| 147 |
pub async fn parent_tree_no_resolve(&self, repo: &dyn Repo) -> BackendResult<MergedTree> { |
| 148 |
// Avoid merging parent trees if known to be empty. The index could be |
| 149 |
// queried only when parents.len() > 1, but index query would be cheaper |
| 150 |
// than extracting parent commit from the store. |
| 151 |
if is_commit_empty_by_index(repo, &self.id).await? == Some(true) { |
| 152 |
return Ok(self.tree()); |
| 153 |
} |
| 154 |
let parents = self.parents().await?; |
| 155 |
merge_commit_trees_no_resolve(repo, &parents).await |
| 156 |
} |
| 157 |
|
| 158 |
/// Returns whether commit's content is empty. Commit description is not |
| 159 |
/// taken into consideration. |
| 160 |
pub async fn is_empty(&self, repo: &dyn Repo) -> BackendResult<bool> { |
| 161 |
if let Some(empty) = is_commit_empty_by_index(repo, &self.id).await? { |
| 162 |
return Ok(empty); |
| 163 |
} |
| 164 |
is_backend_commit_empty(repo, &self.store, &self.data).await |
| 165 |
} |
| 166 |
|
| 167 |
pub fn has_conflict(&self) -> bool { |
| 168 |
!self.tree_ids().is_resolved() |
| 169 |
} |
| 170 |
|
| 171 |
pub fn change_id(&self) -> &ChangeId { |
| 172 |
&self.data.change_id |
| 173 |
} |
| 174 |
|
| 175 |
pub fn store_commit(&self) -> &Arc<backend::Commit> { |
| 176 |
&self.data |
| 177 |
} |
| 178 |
|
| 179 |
pub fn description(&self) -> &str { |
| 180 |
&self.data.description |
| 181 |
} |
| 182 |
|
| 183 |
pub fn author(&self) -> &Signature { |
| 184 |
&self.data.author |
| 185 |
} |
| 186 |
|
| 187 |
pub fn committer(&self) -> &Signature { |
| 188 |
&self.data.committer |
| 189 |
} |
| 190 |
|
| 191 |
/// A commit is hidden if its commit id is not in the change id index. |
| 192 |
pub async fn is_hidden(&self, repo: &dyn Repo) -> IndexResult<bool> { |
| 193 |
let maybe_targets = repo.resolve_change_id(self.change_id()).await?; |
| 194 |
Ok(maybe_targets.is_none_or(|targets| !targets.has_visible(&self.id))) |
| 195 |
} |
| 196 |
|
| 197 |
/// A commit is discardable if it has no change from its parent, and an |
| 198 |
/// empty description. |
| 199 |
pub async fn is_discardable(&self, repo: &dyn Repo) -> BackendResult<bool> { |
| 200 |
Ok(self.description().is_empty() && self.is_empty(repo).await?) |
| 201 |
} |
| 202 |
|
| 203 |
/// A quick way to just check if a signature is present. |
| 204 |
pub fn is_signed(&self) -> bool { |
| 205 |
self.data.secure_sig.is_some() |
| 206 |
} |
| 207 |
|
| 208 |
/// A slow (but cached) way to get the full verification. |
| 209 |
pub fn verification(&self) -> SignResult<Option<Verification>> { |
| 210 |
self.data |
| 211 |
.secure_sig |
| 212 |
.as_ref() |
| 213 |
.map(|sig| self.store.signer().verify(&self.id, &sig.data, &sig.sig)) |
| 214 |
.transpose() |
| 215 |
} |
| 216 |
|
| 217 |
/// A string describing the commit to be used in conflict markers. If a |
| 218 |
/// description is set, it will include the first line of the description. |
| 219 |
pub fn conflict_label(&self) -> String { |
| 220 |
if let Some(subject) = self.description().lines().next() { |
| 221 |
// Example: nlqwxzwn 7dd24e73 "first line of description" |
| 222 |
format!( |
| 223 |
"{} \"{}\"", |
| 224 |
self.conflict_label_short(), |
| 225 |
// Control characters shouldn't be written in conflict markers, and '\0' isn't |
| 226 |
// supported by the Git backend, so we just remove them. Unicode characters are |
| 227 |
// supported, so we don't have to remove them. |
| 228 |
subject.trim().replace(char::is_control, "") |
| 229 |
) |
| 230 |
} else { |
| 231 |
self.conflict_label_short() |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/// A short string describing the commit to be used in conflict markers. |
| 236 |
/// Does not include the commit description. |
| 237 |
fn conflict_label_short(&self) -> String { |
| 238 |
// Example: nlqwxzwn 7dd24e73 |
| 239 |
format!("{:.8} {:.8}", self.change_id(), self.id()) |
| 240 |
} |
| 241 |
|
| 242 |
/// A string describing the commit's parents to be used in conflict markers. |
| 243 |
pub async fn parents_conflict_label(&self) -> BackendResult<String> { |
| 244 |
let parents = self.parents().await?; |
| 245 |
Ok(conflict_label_for_commits(&parents)) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// If there is a single commit, returns the detailed conflict label for that |
| 250 |
// commit. If there are multiple commits, joins the short conflict labels of |
| 251 |
// each commit. |
| 252 |
pub fn conflict_label_for_commits(commits: &[Commit]) -> String { |
| 253 |
if commits.len() == 1 { |
| 254 |
commits[0].conflict_label() |
| 255 |
} else { |
| 256 |
commits.iter().map(Commit::conflict_label_short).join(", ") |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
pub(crate) async fn is_backend_commit_empty( |
| 261 |
repo: &dyn Repo, |
| 262 |
store: &Arc<Store>, |
| 263 |
commit: &backend::Commit, |
| 264 |
) -> BackendResult<bool> { |
| 265 |
if let [parent_id] = &*commit.parents { |
| 266 |
return Ok(commit.root_tree == *store.get_commit_async(parent_id).await?.tree_ids()); |
| 267 |
} |
| 268 |
let parents = try_join_all(commit.parents.iter().map(|id| store.get_commit_async(id))).await?; |
| 269 |
let parent_tree = merge_commit_trees(repo, &parents).await?; |
| 270 |
Ok(commit.root_tree == *parent_tree.tree_ids()) |
| 271 |
} |
| 272 |
|
| 273 |
async fn is_commit_empty_by_index(repo: &dyn Repo, id: &CommitId) -> BackendResult<Option<bool>> { |
| 274 |
let maybe_paths = repo |
| 275 |
.index() |
| 276 |
.changed_paths_in_commit(id) |
| 277 |
.await |
| 278 |
// TODO: index error shouldn't be a "BackendError" |
| 279 |
.map_err(|err| BackendError::Other(err.into()))?; |
| 280 |
Ok(maybe_paths.map(|mut paths| paths.next().is_none())) |
| 281 |
} |
| 282 |
|
| 283 |
pub trait CommitIteratorExt<'c, I> { |
| 284 |
fn ids(self) -> impl Iterator<Item = &'c CommitId>; |
| 285 |
} |
| 286 |
|
| 287 |
impl<'c, I> CommitIteratorExt<'c, I> for I |
| 288 |
where |
| 289 |
I: Iterator<Item = &'c Commit>, |
| 290 |
{ |
| 291 |
fn ids(self) -> impl Iterator<Item = &'c CommitId> { |
| 292 |
self.map(|commit| commit.id()) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/// Wrapper to sort `Commit` by committer timestamp. |
| 297 |
#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 298 |
pub(crate) struct CommitByCommitterTimestamp(pub Commit); |
| 299 |
|
| 300 |
impl Ord for CommitByCommitterTimestamp { |
| 301 |
fn cmp(&self, other: &Self) -> Ordering { |
| 302 |
let self_timestamp = &self.0.committer().timestamp.timestamp; |
| 303 |
let other_timestamp = &other.0.committer().timestamp.timestamp; |
| 304 |
self_timestamp |
| 305 |
.cmp(other_timestamp) |
| 306 |
.then_with(|| self.0.cmp(&other.0)) // to comply with Eq |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
impl PartialOrd for CommitByCommitterTimestamp { |
| 311 |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 312 |
Some(self.cmp(other)) |
| 313 |
} |
| 314 |
} |