Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ authors = ["Atomic Contributors"]
license = "Apache-2.0"
repository = "https://github.com/atomicdotdev/atomic"
homepage = "https://atomic.dev"
rust-version = "1.98"
rust-version = "1.90"

[workspace.dependencies]
# Core
Expand Down
2 changes: 2 additions & 0 deletions atomic-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ teams = ["atomic-teams"]
# test suite builds the binary with the deterministic adoption seams. The
# default/shipping configuration compiles no injection code at all.
adoption-test-injection = ["atomic-repository/adoption-test-injection"]
# Non-default Git span-import experiment; no native semantic history is invented.
experimental-span-import = []

[package]
name = "atomic-cli"
Expand Down
241 changes: 175 additions & 66 deletions atomic-cli/src/commands/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Blame {

struct LineAttribution {
line_number: usize,
owner: Hash,
owners: Vec<Hash>,
content: Vec<u8>,
}

Expand Down Expand Up @@ -67,73 +67,169 @@ impl Command for Blame {
.ok_or_else(|| CliError::InvalidRepository {
reason: format!("view '{view}' not found"),
})?;
let _ = view_state;
let mut changes: HashMap<Hash, Change> = HashMap::new();
let mut lines: Vec<LineAttribution> = Vec::new();
let mut skipped_unbound = 0usize;

// Span histories imported from Git deliberately have no historical
// CRDT rows. Interpret their verified visible bytes directly instead
// of attributing them to a later native edit. A line can contain bytes
// from several changes, in which case every owner remains visible.
use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY};
let span_enabled = txn
.required_capability_version(SPAN_RANGE_CAPABILITY.id())
.map_err(|e| CliError::Repository(e.into()))?
.is_some();
let mut span_handled = false;
if span_enabled {
use atomic_core::pristine::span_patch_storage::{SpanPatchReadTxn, SpanRenderContext};
if self.path.as_bytes().contains(&b'%') {
return Err(CliError::InvalidArgument {
message: "span blame cannot safely resolve percent-bearing paths yet".into(),
});
}
let visibility = atomic_repository::graph_visibility_closure(&txn, &view_state)
.map_err(CliError::Repository)?;
let binding = repo
.projected_path_binding_for_visibility(&txn, &visibility, &self.path)
.map_err(CliError::Repository)?
.ok_or_else(|| CliError::InvalidArgument {
message: format!("'{}' is absent or conflicted on view '{}'", self.path, view),
})?;
if !binding.directory
&& txn
.span_root(binding.inode)
.map_err(|error| CliError::InvalidArgument {
message: error.to_string(),
})?
.is_some()
{
let visible: std::collections::HashSet<_> =
visibility.iter_dependency_first().copied().collect();
let attrs =
atomic_core::output::project_inode_attributes(&txn, binding.position, &visible)
.map_err(|error| CliError::Repository(error.into()))?;
if attrs.is_conflicted()
|| attrs.materialization.kind != atomic_core::change::InodeKind::Regular
{
return Err(CliError::InvalidArgument {
message: format!("'{}' is not an unambiguous regular text file", self.path),
});
}
let context = SpanRenderContext::new(&txn, repo.change_store(), &visibility)
.map_err(|error| CliError::InvalidArgument {
message: error.to_string(),
})?;
let state = context
.record_state(binding.inode)
.map_err(|error| CliError::InvalidArgument {
message: error.to_string(),
})?
.ok_or_else(|| CliError::InvalidArgument {
message: format!("'{}' has no visible span state", self.path),
})?;
let semantic =
state
.semantic_snapshot()
.map_err(|error| CliError::InvalidArgument {
message: error.to_string(),
})?;
for (index, line) in semantic.lines.into_iter().enumerate() {
let mut owners = Vec::new();
for token in line.tokens {
for fragment in token.authors {
let owner = fragment.target.owner;
if !owners.contains(&owner) {
owners.push(owner);
}
if let std::collections::hash_map::Entry::Vacant(slot) =
changes.entry(owner)
{
slot.insert(
repo.load_change(&owner).map_err(CliError::Repository)?,
);
}
}
}
if owners.is_empty() {
return Err(CliError::InvalidArgument {
message: format!("'{}' has an unattributed visible line", self.path),
});
}
lines.push(LineAttribution {
line_number: index + 1,
owners,
content: state.content[line.output].to_vec(),
});
}
span_handled = true;
}
}

// The trunk must exist and hold graph content. Binary/opaque trunks
// are attributed at line granularity only when the CRDT layer bound
// branches; otherwise the command fails explicitly rather than
// printing fabricated lines.
let trunk = txn
.get_trunk_by_path(&self.path)
.map_err(|e| CliError::Repository(e.into()))?
.ok_or_else(|| CliError::InvalidArgument {
message: format!(
"'{path}' is not a tracked text file (no semantic trunk)",
path = self.path
),
})?;
if !span_handled {
let trunk = txn
.get_trunk_by_path(&self.path)
.map_err(|e| CliError::Repository(e.into()))?
.ok_or_else(|| CliError::InvalidArgument {
message: format!(
"'{path}' is not a tracked text file (no semantic trunk)",
path = self.path
),
})?;

let branch_ids: Vec<atomic_core::crdt::BranchId> =
iter_trunk_branches_in_file_order(&txn, trunk)
.map_err(|e| CliError::Repository(e.into()))?;
let branch_keys: Vec<[u8; 12]> = branch_ids
.iter()
.map(atomic_core::crdt::tables::encode_branch_id)
.collect();
let branch_ids: Vec<atomic_core::crdt::BranchId> =
iter_trunk_branches_in_file_order(&txn, trunk)
.map_err(|e| CliError::Repository(e.into()))?;
let branch_keys: Vec<[u8; 12]> = branch_ids
.iter()
.map(atomic_core::crdt::tables::encode_branch_id)
.collect();

let mut changes: HashMap<Hash, Change> = HashMap::new();
let mut lines: Vec<LineAttribution> = Vec::new();
let mut skipped_unbound = 0usize;
for branch_key in branch_keys {
let branch = txn
.get_crdt_branch(&branch_key)
.map_err(|e| CliError::Repository(e.into()))?;
let Some(branch) = branch else { continue };
if !branch.state.is_alive() {
continue;
for branch_key in branch_keys {
let branch = txn
.get_crdt_branch(&branch_key)
.map_err(|e| CliError::Repository(e.into()))?;
let Some(branch) = branch else { continue };
if !branch.state.is_alive() {
continue;
}
let Some(vertex) = txn
.get_crdt_branch_vertex(&branch_key)
.map_err(|e| CliError::Repository(e.into()))?
else {
skipped_unbound += 1;
continue;
};
let Some(owner) = txn
.get_external(vertex.change)
.map_err(|e| CliError::Repository(e.into()))?
else {
skipped_unbound += 1;
continue;
};
let change = match changes.get(&owner) {
Some(change) => change.clone(),
None => repo.load_change(&owner).map_err(CliError::Repository)?,
};
let start = usize::try_from(vertex.start.get()).unwrap_or(0);
let end = usize::try_from(vertex.end.get()).unwrap_or(0);
let content: Vec<u8> = if start <= end && end <= change.contents.len() {
change.contents[start..end].to_vec()
} else {
Vec::new()
};
changes.insert(owner, change);
let decoded = decode_branch_id(&branch_key);
lines.push(LineAttribution {
line_number: decoded.branch_idx() as usize + 1,
owners: vec![owner],
content,
});
}
let Some(vertex) = txn
.get_crdt_branch_vertex(&branch_key)
.map_err(|e| CliError::Repository(e.into()))?
else {
skipped_unbound += 1;
continue;
};
let Some(owner) = txn
.get_external(vertex.change)
.map_err(|e| CliError::Repository(e.into()))?
else {
skipped_unbound += 1;
continue;
};
let change = match changes.get(&owner) {
Some(change) => change.clone(),
None => repo.load_change(&owner).map_err(CliError::Repository)?,
};
let start = usize::try_from(vertex.start.get()).unwrap_or(0);
let end = usize::try_from(vertex.end.get()).unwrap_or(0);
let content: Vec<u8> = if start <= end && end <= change.contents.len() {
change.contents[start..end].to_vec()
} else {
Vec::new()
};
changes.insert(owner, change);
let decoded = decode_branch_id(&branch_key);
lines.push(LineAttribution {
line_number: decoded.branch_idx() as usize + 1,
owner,
content,
});
}

if lines.is_empty() {
Expand All @@ -151,12 +247,22 @@ impl Command for Blame {

let mut stdout = std::io::stdout().lock();
for attribution in &lines {
let hash = if self.short {
attribution.owner.to_base32()[..12].to_string()
} else {
attribution.owner.to_base32()
};
let change = changes.get(&attribution.owner);
let hash = attribution
.owners
.iter()
.map(|owner| {
let hash = owner.to_base32();
if self.short {
hash[..12].to_string()
} else {
hash
}
})
.collect::<Vec<_>>()
.join("+");
let change = (attribution.owners.len() == 1)
.then(|| changes.get(&attribution.owners[0]))
.flatten();
let (author, date) = match change {
Some(change) => {
let author = change
Expand All @@ -174,6 +280,9 @@ impl Command for Blame {
.to_string();
(author, date)
}
None if attribution.owners.len() > 1 => {
("multiple".to_string(), "mixed".to_string())
}
None => ("unknown".to_string(), "unknown".to_string()),
};
let content = String::from_utf8_lossy(&attribution.content);
Expand Down
Loading
Loading