git: Implement commit creation (#23263)
- [x] Basic implementation - [x] Disable commit buttons when committing is not possible (empty message, no changes) - [x] Upgrade GitSummary to efficiently figure out whether there are any staged changes - [x] Make CommitAll work - [x] Surface errors with toasts - [x] Channel shutdown - [x] Empty commit message or no changes - [x] Failed git operations - [x] Fix added files no longer appearing correctly in the project panel (GitSummary breakage) - [x] Fix handling of commit message Release Notes: - N/A --------- Co-authored-by: Nate <nate@zed.dev>
This commit is contained in:
parent
3767e7e5f0
commit
5da67899b7
10 changed files with 387 additions and 198 deletions
|
@ -1,52 +1,65 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use futures::channel::mpsc;
|
||||
use futures::StreamExt as _;
|
||||
use git::repository::{GitRepository, RepoPath};
|
||||
use futures::{SinkExt as _, StreamExt as _};
|
||||
use git::{
|
||||
repository::{GitRepository, RepoPath},
|
||||
status::{GitSummary, TrackedSummary},
|
||||
};
|
||||
use gpui::{AppContext, SharedString};
|
||||
use settings::WorktreeId;
|
||||
use util::ResultExt as _;
|
||||
use worktree::RepositoryEntry;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum StatusAction {
|
||||
Stage,
|
||||
Unstage,
|
||||
}
|
||||
|
||||
pub struct GitState {
|
||||
/// The current commit message being composed.
|
||||
pub commit_message: Option<SharedString>,
|
||||
pub commit_message: SharedString,
|
||||
|
||||
/// When a git repository is selected, this is used to track which repository's changes
|
||||
/// are currently being viewed or modified in the UI.
|
||||
pub active_repository: Option<(WorktreeId, RepositoryEntry, Arc<dyn GitRepository>)>,
|
||||
|
||||
pub update_sender: mpsc::UnboundedSender<(Arc<dyn GitRepository>, Vec<RepoPath>, StatusAction)>,
|
||||
update_sender: mpsc::UnboundedSender<(Message, mpsc::Sender<anyhow::Error>)>,
|
||||
}
|
||||
|
||||
enum Message {
|
||||
StageAndCommit(Arc<dyn GitRepository>, SharedString, Vec<RepoPath>),
|
||||
Commit(Arc<dyn GitRepository>, SharedString),
|
||||
Stage(Arc<dyn GitRepository>, Vec<RepoPath>),
|
||||
Unstage(Arc<dyn GitRepository>, Vec<RepoPath>),
|
||||
}
|
||||
|
||||
impl GitState {
|
||||
pub fn new(cx: &AppContext) -> Self {
|
||||
let (tx, mut rx) =
|
||||
mpsc::unbounded::<(Arc<dyn GitRepository>, Vec<RepoPath>, StatusAction)>();
|
||||
let (update_sender, mut update_receiver) =
|
||||
mpsc::unbounded::<(Message, mpsc::Sender<anyhow::Error>)>();
|
||||
cx.spawn(|cx| async move {
|
||||
while let Some((git_repo, paths, action)) = rx.next().await {
|
||||
cx.background_executor()
|
||||
while let Some((msg, mut err_sender)) = update_receiver.next().await {
|
||||
let result = cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
match action {
|
||||
StatusAction::Stage => git_repo.stage_paths(&paths),
|
||||
StatusAction::Unstage => git_repo.unstage_paths(&paths),
|
||||
match msg {
|
||||
Message::StageAndCommit(repo, message, paths) => {
|
||||
repo.stage_paths(&paths)?;
|
||||
repo.commit(&message)?;
|
||||
Ok(())
|
||||
}
|
||||
Message::Stage(repo, paths) => repo.stage_paths(&paths),
|
||||
Message::Unstage(repo, paths) => repo.unstage_paths(&paths),
|
||||
Message::Commit(repo, message) => repo.commit(&message),
|
||||
}
|
||||
})
|
||||
.await
|
||||
.log_err();
|
||||
.await;
|
||||
if let Err(e) = result {
|
||||
err_sender.send(e).await.ok();
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
GitState {
|
||||
commit_message: None,
|
||||
commit_message: SharedString::default(),
|
||||
active_repository: None,
|
||||
update_sender: tx,
|
||||
update_sender,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,55 +78,64 @@ impl GitState {
|
|||
self.active_repository.as_ref()
|
||||
}
|
||||
|
||||
pub fn commit_message(&mut self, message: Option<SharedString>) {
|
||||
self.commit_message = message;
|
||||
}
|
||||
|
||||
pub fn clear_commit_message(&mut self) {
|
||||
self.commit_message = None;
|
||||
}
|
||||
|
||||
fn act_on_entries(&self, entries: Vec<RepoPath>, action: StatusAction) {
|
||||
pub fn stage_entries(
|
||||
&self,
|
||||
entries: Vec<RepoPath>,
|
||||
err_sender: mpsc::Sender<anyhow::Error>,
|
||||
) -> anyhow::Result<()> {
|
||||
if entries.is_empty() {
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
if let Some((_, _, git_repo)) = self.active_repository.as_ref() {
|
||||
let _ = self
|
||||
.update_sender
|
||||
.unbounded_send((git_repo.clone(), entries, action));
|
||||
let Some((_, _, git_repo)) = self.active_repository.as_ref() else {
|
||||
return Err(anyhow!("No active repository"));
|
||||
};
|
||||
self.update_sender
|
||||
.unbounded_send((Message::Stage(git_repo.clone(), entries), err_sender))
|
||||
.map_err(|_| anyhow!("Failed to submit stage operation"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unstage_entries(
|
||||
&self,
|
||||
entries: Vec<RepoPath>,
|
||||
err_sender: mpsc::Sender<anyhow::Error>,
|
||||
) -> anyhow::Result<()> {
|
||||
if entries.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let Some((_, _, git_repo)) = self.active_repository.as_ref() else {
|
||||
return Err(anyhow!("No active repository"));
|
||||
};
|
||||
self.update_sender
|
||||
.unbounded_send((Message::Unstage(git_repo.clone(), entries), err_sender))
|
||||
.map_err(|_| anyhow!("Failed to submit unstage operation"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn stage_entries(&self, entries: Vec<RepoPath>) {
|
||||
self.act_on_entries(entries, StatusAction::Stage);
|
||||
}
|
||||
|
||||
pub fn unstage_entries(&self, entries: Vec<RepoPath>) {
|
||||
self.act_on_entries(entries, StatusAction::Unstage);
|
||||
}
|
||||
|
||||
pub fn stage_all(&self) {
|
||||
pub fn stage_all(&self, err_sender: mpsc::Sender<anyhow::Error>) -> anyhow::Result<()> {
|
||||
let Some((_, entry, _)) = self.active_repository.as_ref() else {
|
||||
return;
|
||||
return Err(anyhow!("No active repository"));
|
||||
};
|
||||
let to_stage = entry
|
||||
.status()
|
||||
.filter(|entry| !entry.status.is_staged().unwrap_or(false))
|
||||
.map(|entry| entry.repo_path.clone())
|
||||
.collect();
|
||||
self.stage_entries(to_stage);
|
||||
self.stage_entries(to_stage, err_sender)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unstage_all(&self) {
|
||||
pub fn unstage_all(&self, err_sender: mpsc::Sender<anyhow::Error>) -> anyhow::Result<()> {
|
||||
let Some((_, entry, _)) = self.active_repository.as_ref() else {
|
||||
return;
|
||||
return Err(anyhow!("No active repository"));
|
||||
};
|
||||
let to_unstage = entry
|
||||
.status()
|
||||
.filter(|entry| entry.status.is_staged().unwrap_or(true))
|
||||
.map(|entry| entry.repo_path.clone())
|
||||
.collect();
|
||||
self.unstage_entries(to_unstage);
|
||||
self.unstage_entries(to_unstage, err_sender)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get a count of all entries in the active repository, including
|
||||
|
@ -123,4 +145,61 @@ impl GitState {
|
|||
.as_ref()
|
||||
.map_or(0, |(_, entry, _)| entry.status_len())
|
||||
}
|
||||
|
||||
fn have_changes(&self) -> bool {
|
||||
let Some((_, entry, _)) = self.active_repository.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
entry.status_summary() != GitSummary::UNCHANGED
|
||||
}
|
||||
|
||||
fn have_staged_changes(&self) -> bool {
|
||||
let Some((_, entry, _)) = self.active_repository.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
entry.status_summary().index != TrackedSummary::UNCHANGED
|
||||
}
|
||||
|
||||
pub fn can_commit(&self, commit_all: bool) -> bool {
|
||||
return !self.commit_message.trim().is_empty()
|
||||
&& self.have_changes()
|
||||
&& (commit_all || self.have_staged_changes());
|
||||
}
|
||||
|
||||
pub fn commit(&mut self, err_sender: mpsc::Sender<anyhow::Error>) -> anyhow::Result<()> {
|
||||
if !self.can_commit(false) {
|
||||
return Err(anyhow!("Unable to commit"));
|
||||
}
|
||||
let Some((_, _, git_repo)) = self.active_repository() else {
|
||||
return Err(anyhow!("No active repository"));
|
||||
};
|
||||
let git_repo = git_repo.clone();
|
||||
let message = std::mem::take(&mut self.commit_message);
|
||||
self.update_sender
|
||||
.unbounded_send((Message::Commit(git_repo, message), err_sender))
|
||||
.map_err(|_| anyhow!("Failed to submit commit operation"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn commit_all(&mut self, err_sender: mpsc::Sender<anyhow::Error>) -> anyhow::Result<()> {
|
||||
if !self.can_commit(true) {
|
||||
return Err(anyhow!("Unable to commit"));
|
||||
}
|
||||
let Some((_, entry, git_repo)) = self.active_repository.as_ref() else {
|
||||
return Err(anyhow!("No active repository"));
|
||||
};
|
||||
let to_stage = entry
|
||||
.status()
|
||||
.filter(|entry| !entry.status.is_staged().unwrap_or(false))
|
||||
.map(|entry| entry.repo_path.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let message = std::mem::take(&mut self.commit_message);
|
||||
self.update_sender
|
||||
.unbounded_send((
|
||||
Message::StageAndCommit(git_repo.clone(), message, to_stage),
|
||||
err_sender,
|
||||
))
|
||||
.map_err(|_| anyhow!("Failed to submit commit operation"))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue