Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Add git log / history for folders and whole project (#52634)

robert7k7cf379622178
diff --git a/crates/git/src/git.rs b/crates/git/src/git.rs
index d5274d24b51..cc3fbe1e259 100644
--- a/crates/git/src/git.rs
+++ b/crates/git/src/git.rs
@@ -47,7 +47,7 @@ actions!(
/// Shows git blame information for the current file.
#[action(deprecated_aliases = ["editor::ToggleGitBlame"])]
Blame,
- /// Shows the git history for the current file.
+ /// Shows the git history for the selected file, folder, or project.
FileHistory,
/// Stages the current file.
StageFile,
diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rs
index 3e5e14b310b..69910d706e4 100644
--- a/crates/git/src/repository.rs
+++ b/crates/git/src/repository.rs
@@ -721,7 +721,7 @@ pub enum LogSource {
All,
Branch(SharedString),
Sha(Oid),
- File(RepoPath),
+ Path(RepoPath),
}
impl LogSource {
@@ -732,7 +732,7 @@ impl LogSource {
LogSource::Sha(oid) => {
str::from_utf8(oid.as_bytes()).context("Failed to build str from sha")
}
- LogSource::File(_) => Ok("--follow"),
+ LogSource::Path(_) => Ok("--follow"),
}
}
}
@@ -2954,8 +2954,8 @@ impl GitRepository for RealGitRepository {
log_source.get_arg()?,
];
- if let LogSource::File(file_path) = &log_source {
- git_log_command.extend(["--", file_path.as_unix_str()]);
+ if let LogSource::Path(path) = &log_source {
+ git_log_command.extend(["--", path.as_unix_str()]);
}
let mut command = git.build_command(&git_log_command);
@@ -3040,8 +3040,8 @@ impl GitRepository for RealGitRepository {
args.push("--grep");
args.push(search_args.query.as_str());
- if let LogSource::File(file_path) = &log_source {
- args.extend(["--", file_path.as_unix_str()]);
+ if let LogSource::Path(path) = &log_source {
+ args.extend(["--", path.as_unix_str()]);
}
let mut command = git.build_command(&args);
diff --git a/crates/git_graph/src/git_graph.rs b/crates/git_graph/src/git_graph.rs
index e98e635c141..e92204c5f12 100644
--- a/crates/git_graph/src/git_graph.rs
+++ b/crates/git_graph/src/git_graph.rs
@@ -834,20 +834,25 @@ fn resolve_file_history_target(
) -> Option<(RepositoryId, LogSource)> {
if let Some(panel) = workspace.panel::<ProjectPanel>(cx)
&& panel.read(cx).focus_handle(cx).contains_focused(window, cx)
- && let Some(project_path) = panel.read(cx).selected_file_project_path(cx)
+ && let Some(project_path) = panel.read(cx).selected_entry_project_path(cx)
{
let git_store = workspace.project().read(cx).git_store();
let (repo, repo_path) = git_store
.read(cx)
.repository_and_path_for_project_path(&project_path, cx)?;
- return Some((repo.read(cx).id, LogSource::File(repo_path)));
+ let log_source = if repo_path.is_empty() {
+ LogSource::All
+ } else {
+ LogSource::Path(repo_path)
+ };
+ return Some((repo.read(cx).id, log_source));
}
if let Some(panel) = workspace.panel::<git_ui::git_panel::GitPanel>(cx)
&& panel.read(cx).focus_handle(cx).contains_focused(window, cx)
&& let Some((repository, repo_path)) = panel.read(cx).selected_file_history_target()
{
- return Some((repository.read(cx).id, LogSource::File(repo_path)));
+ return Some((repository.read(cx).id, LogSource::Path(repo_path)));
}
let editor = workspace.active_item_as::<Editor>(cx)?;
@@ -864,7 +869,7 @@ fn resolve_file_history_target(
let (repo, repo_path) = git_store
.read(cx)
.repository_and_path_for_project_path(&project_path, cx)?;
- Some((repo.read(cx).id, LogSource::File(repo_path)))
+ Some((repo.read(cx).id, LogSource::Path(repo_path)))
}
fn open_or_reuse_graph(
@@ -1027,9 +1032,9 @@ impl GitGraph {
.read(cx)
.preview_fractions(window.rem_size());
- let is_file_history = matches!(self.log_source, LogSource::File(_));
- let graph_fraction = if is_file_history { 0.0 } else { fractions[0] };
- let offset = if is_file_history { 0 } else { 1 };
+ let is_path_history = matches!(self.log_source, LogSource::Path(_));
+ let graph_fraction = if is_path_history { 0.0 } else { fractions[0] };
+ let offset = if is_path_history { 0 } else { 1 };
[
graph_fraction,
@@ -1107,7 +1112,7 @@ impl GitGraph {
let table_interaction_state = cx.new(|cx| TableInteractionState::new(cx));
- let column_widths = if matches!(log_source, LogSource::File(_)) {
+ let column_widths = if matches!(log_source, LogSource::Path(_)) {
cx.new(|_cx| {
RedistributableColumnsState::new(
4,
@@ -2738,7 +2743,7 @@ impl Render for GitGraph {
this.child(self.render_loading_spinner(cx))
})
} else {
- let is_file_history = matches!(self.log_source, LogSource::File(_));
+ let is_path_history = matches!(self.log_source, LogSource::Path(_));
let header_resize_info =
HeaderResizeInfo::from_redistributable(&self.column_widths, cx);
let header_context = TableRenderContext::for_column_widths(
@@ -2766,7 +2771,7 @@ impl Render for GitGraph {
.flex()
.flex_col()
.child(render_table_header(
- if !is_file_history {
+ if !is_path_history {
TableRow::from_vec(
vec![
Label::new("Graph")
@@ -2897,7 +2902,7 @@ impl Render for GitGraph {
.child(
h_flex()
.size_full()
- .when(!is_file_history, |this| {
+ .when(!is_path_history, |this| {
this.child(
div()
.w(DefiniteLength::Fraction(graph_fraction))
@@ -3015,20 +3020,20 @@ impl Item for GitGraph {
.file_name()
.map(|name| name.to_string_lossy().to_string())
});
- let file_history_path = match &self.log_source {
- LogSource::File(path) => Some(path.as_unix_str().to_string()),
+ let path_history_path = match &self.log_source {
+ LogSource::Path(path) => Some(path.as_unix_str().to_string()),
_ => None,
};
Some(TabTooltipContent::Custom(Box::new(Tooltip::element({
move |_, _| {
v_flex()
- .child(Label::new(if file_history_path.is_some() {
- "File History"
+ .child(Label::new(if path_history_path.is_some() {
+ "Path History"
} else {
"Git Graph"
}))
- .when_some(file_history_path.clone(), |this, path| {
+ .when_some(path_history_path.clone(), |this, path| {
this.child(Label::new(path).color(Color::Muted).size(LabelSize::Small))
})
.when_some(repo_name.clone(), |this, name| {
@@ -3040,7 +3045,7 @@ impl Item for GitGraph {
}
fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString {
- if let LogSource::File(path) = &self.log_source {
+ if let LogSource::Path(path) = &self.log_source {
return path
.as_ref()
.file_name()
@@ -3286,7 +3291,7 @@ mod persistence {
pub const LOG_SOURCE_ALL: i32 = 0;
pub const LOG_SOURCE_BRANCH: i32 = 1;
pub const LOG_SOURCE_SHA: i32 = 2;
- pub const LOG_SOURCE_FILE: i32 = 3;
+ pub const LOG_SOURCE_PATH: i32 = 3;
pub const LOG_ORDER_DATE: i32 = 0;
pub const LOG_ORDER_TOPO: i32 = 1;
@@ -3298,7 +3303,7 @@ mod persistence {
LogSource::All => LOG_SOURCE_ALL,
LogSource::Branch(_) => LOG_SOURCE_BRANCH,
LogSource::Sha(_) => LOG_SOURCE_SHA,
- LogSource::File(_) => LOG_SOURCE_FILE,
+ LogSource::Path(_) => LOG_SOURCE_PATH,
}
}
@@ -3307,7 +3312,7 @@ mod persistence {
LogSource::All => None,
LogSource::Branch(branch) => Some(branch.to_string()),
LogSource::Sha(oid) => Some(oid.to_string()),
- LogSource::File(path) => Some(path.as_unix_str().to_string()),
+ LogSource::Path(path) => Some(path.as_unix_str().to_string()),
}
}
@@ -3334,11 +3339,11 @@ mod persistence {
.and_then(|v| Oid::from_str(v).ok())
.map(LogSource::Sha)
.unwrap_or_default(),
- Some(LOG_SOURCE_FILE) => state
+ Some(LOG_SOURCE_PATH) => state
.log_source_value
.as_ref()
.and_then(|v| RepoPath::new(v).ok())
- .map(LogSource::File)
+ .map(LogSource::Path)
.unwrap_or_default(),
None | Some(_) => LogSource::default(),
}
@@ -4432,7 +4437,7 @@ mod tests {
assert_eq!(graphs.len(), 1);
assert_eq!(
graphs[0].read(cx).log_source,
- LogSource::File(tracked1_repo_path.clone())
+ LogSource::Path(tracked1_repo_path.clone())
);
});
@@ -4460,7 +4465,7 @@ mod tests {
assert_eq!(graphs.len(), 1);
assert_eq!(
graphs[0].read(cx).log_source,
- LogSource::File(tracked1_repo_path.clone())
+ LogSource::Path(tracked1_repo_path.clone())
);
});
@@ -4540,7 +4545,7 @@ mod tests {
.expect("expected a git graph");
assert_eq!(
latest.read(cx).log_source,
- LogSource::File(tracked2_repo_path)
+ LogSource::Path(tracked2_repo_path)
);
});
}
@@ -4549,11 +4554,11 @@ mod tests {
fn test_serialized_state_roundtrip(_cx: &mut TestAppContext) {
use persistence::SerializedGitGraphState;
- let file_path = RepoPath::new(&"src/main.rs").unwrap();
+ let path = RepoPath::new(&"src/main.rs").unwrap();
let sha = Oid::from_bytes(&[0xab; 20]).unwrap();
let state = SerializedGitGraphState {
- log_source_type: Some(persistence::LOG_SOURCE_FILE),
+ log_source_type: Some(persistence::LOG_SOURCE_PATH),
log_source_value: Some("src/main.rs".to_string()),
log_order: Some(persistence::LOG_ORDER_TOPO),
selected_sha: Some(sha.to_string()),
@@ -4563,7 +4568,7 @@ mod tests {
assert_eq!(
persistence::deserialize_log_source(&state),
- LogSource::File(file_path)
+ LogSource::Path(path)
);
assert!(matches!(
persistence::deserialize_log_order(&state),
diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs
index ba6779e3e8b..1ae5f424845 100644
--- a/crates/project_panel/src/project_panel.rs
+++ b/crates/project_panel/src/project_panel.rs
@@ -1053,7 +1053,7 @@ impl ProjectPanel {
|| (settings.hide_root && visible_worktrees_count == 1));
let should_show_compare = !is_dir && self.file_abs_paths_to_diff(cx).is_some();
- let (has_git_repo, has_file_history) = {
+ let (has_git_repo, has_history) = {
let project_path = project::ProjectPath {
worktree_id,
path: entry.path.clone(),
@@ -1062,12 +1062,11 @@ impl ProjectPanel {
let has_git_repo = git_store
.repository_and_path_for_project_path(&project_path, cx)
.is_some();
- let has_file_history = !is_dir
- && has_git_repo
+ let has_history = has_git_repo
&& !git_store
.project_path_git_status(&project_path, cx)
.is_some_and(|status| status.is_created());
- (has_git_repo, has_file_history)
+ (has_git_repo, has_history)
};
let has_pasteable_content = self.has_pasteable_content(cx);
@@ -1143,8 +1142,8 @@ impl ProjectPanel {
)
})
.action("Add to .gitignore", Box::new(git::AddToGitignore))
- .when(has_file_history, |menu| {
- menu.action("View File History", Box::new(git::FileHistory))
+ .when(has_history, |menu| {
+ menu.action("View History", Box::new(git::FileHistory))
})
})
.when(!should_hide_rename, |menu| {
@@ -3791,11 +3790,11 @@ impl ProjectPanel {
Some((worktree.read(cx), entry))
}
- pub fn selected_file_project_path(&self, cx: &App) -> Option<ProjectPath> {
+ pub fn selected_entry_project_path(&self, cx: &App) -> Option<ProjectPath> {
let (worktree, entry) = self.selected_sub_entry(cx)?;
Some(ProjectPath {
worktree_id: worktree.read(cx).id(),
- path: entry.is_file().then(|| entry.path.clone())?,
+ path: entry.path.clone(),
})
}