Skip to content
Open
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
17 changes: 17 additions & 0 deletions crates/winc-app/src/app/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) mod log;
pub(crate) mod merge;
pub(crate) mod question;
pub(crate) mod rebase;
mod refresh;
mod repository;
pub(super) mod rewrite;
mod stepping;
Expand All @@ -26,6 +27,7 @@ use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};

use ropey::Rope;
use winc_editor::diff_view::{BlameSource, DiffSide, DiffView};
Expand All @@ -41,6 +43,9 @@ use super::{App, Focus, UserEvent};
use crate::app::NoticeKind;
use crate::layout::SidebarView;

/// Git 视图可见时补查本地状态的间隔;系统漏发通知时定期重新读取(经验值)。
const STATUS_RECHECK: Duration = Duration::from_secs(5);

/// 界面线程与各后台任务共用的 [`Git`]。
///
/// 第一个找到 `git` 的任务写入结果,之后的任务直接使用;未找到时不写入,由下一个任务重新查找
Expand Down Expand Up @@ -254,6 +259,8 @@ pub(crate) struct GitState {
ignored_paths: HashSet<PathBuf>,
/// 正在运行的刷新;`None` 表示没有在运行。
running: Option<Cancel>,
/// 下一次补查本地状态的时刻;Git 视图不可见时不安排。
recheck_due: Option<Instant>,
/// 运行期间是否又收到刷新请求;为真时完成后再刷新一次。
again: bool,
/// 下一次刷新是否同时读取分支与储藏(由 [`App::git_refresh`] 设置,读取时清除)。
Expand Down Expand Up @@ -353,6 +360,7 @@ impl Default for GitState {
decorations: HashMap::new(),
ignored_paths: HashSet::new(),
running: None,
recheck_due: None,
again: false,
refs_wanted: false,
logs_outdated: false,
Expand Down Expand Up @@ -387,6 +395,13 @@ impl Default for GitState {
}

impl GitState {
/// 测试用:提供后台读到的仓库状态,不启动 Git 进程。
#[cfg(test)]
pub(in crate::app) fn summary_for_test(&self, root: PathBuf, status: Status) {
self.summaries.lock().unwrap().insert(root, status);
self.summaries_changed();
}

/// 记下各仓库的状态(`summaries`)改过了。
pub(crate) fn summaries_changed(&self) {
(self.summaries_version).fetch_add(1, std::sync::atomic::Ordering::Release);
Expand Down Expand Up @@ -437,6 +452,7 @@ impl GitState {
cancel.cancel();
}
self.again = false;
self.recheck_due = None;
self.refs_wanted = false;
self.root = None;
self.tool.forget();
Expand Down Expand Up @@ -889,6 +905,7 @@ impl App {
self.git.again = true;
return;
}
self.git.recheck_due = Some(Instant::now() + STATUS_RECHECK);
// 尚未读取到状态时(刚打开、上次出错)同样没有分支列表,因此一并读取。
let refs = std::mem::take(&mut self.git.refs_wanted) || self.git.status.is_none();
let cancel = Cancel::default();
Expand Down
7 changes: 6 additions & 1 deletion crates/winc-app/src/app/git/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ pub enum CommitTarget {
/// 另一个仓库里的一组的标题行(仓库的下标,组):折叠或展开这一组;暂存区视图中悬停时行尾
/// 显示暂存、取消暂存整组的按钮。
OtherGroup(usize, others::OtherGroup),
/// 其他仓库更改树的目录行,在 `commit_other_rows` 中的下标。
OtherDir(usize),
/// 勾选式下另一个仓库里一组标题的勾选框:勾上或取消这一组的全部文件。
OtherGroupFiles(usize, others::OtherGroup),
/// 另一个仓库标题行左端的折叠箭头:折叠或展开这个仓库的一节。
Expand Down Expand Up @@ -787,6 +789,8 @@ pub(crate) struct CommitState {
pub(in crate::app::git) carry_in: bool,
/// 多仓库时折叠着的其他仓库(组为 `None`)和其中折叠着的组,按仓库的根目录记。
other_folds: HashSet<(PathBuf, Option<others::OtherGroup>)>,
/// 其他仓库更改树中收起的目录:仓库、分组、相对目录。
other_collapsed_dirs: HashSet<(PathBuf, others::OtherGroup, PathBuf)>,
/// 「没有更改的仓库」一组是否展开;未选过时为 `None`,见 [`App::quiet_repositories_open`]。
pub(crate) quiet_open: Option<bool>,
/// 其他仓库各行的缓存(见 [`App::commit_other_rows`])。
Expand Down Expand Up @@ -959,6 +963,7 @@ impl Default for CommitState {
others_left: 0,
carry_in: false,
other_folds: HashSet::new(),
other_collapsed_dirs: HashSet::new(),
quiet_open: None,
other_rows: Default::default(),
head: None,
Expand Down Expand Up @@ -1315,7 +1320,7 @@ impl App {

/// 窗口坐标的 `y` 是否落在写全名的那一改动行上:鼠标在这一行上时不唤出列表的滚动条,
/// 和文件树相同。
pub(in crate::app) fn over_commit_overflow_row(&self, y: f32) -> bool {
pub(crate) fn over_commit_overflow_row(&self, y: f32) -> bool {
self.commit_shown()
&& (self.git.commit.overflow).is_some_and(|o| (o.y..o.y + COMMIT_ROW).contains(&y))
}
Expand Down
8 changes: 7 additions & 1 deletion crates/winc-app/src/app/git/commit/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ impl App {
};
let all_collapsed = shown().all(|group| self.git.commit.group_collapsed(group));
let all_expanded = shown().all(|group| !self.git.commit.group_collapsed(group))
&& self.git.commit.collapsed_dirs.iter().all(HashSet::is_empty);
&& self.git.commit.collapsed_dirs.iter().all(HashSet::is_empty)
&& self.git.commit.other_collapsed_dirs.is_empty();
let busy = self.git.branch.running.is_some();
// 拉取自…、推送到…在远程仓库子菜单里,在分支上、有远程时可用。
let transfer = self.picker_action_enabled(PickerPart::Push);
Expand Down Expand Up @@ -252,6 +253,11 @@ impl App {
pub(in crate::app) fn toggle_commit_layout(&mut self) {
let flat = !self.git.commit.flat();
self.git.commit.set_flat(flat);
// 展开、收起动画保存着旧模式的行,切换后立即按新模式绘制。
self.git.commit.motion = None;
self.git.commit.fold_motion = None;
self.git.commit.shift = None;
self.git.commit.overflow = None;
self.update_state(|state| state.commit_tree = !flat);
self.git.commit.scroll = self.git.commit.scroll.min(self.commit_max_scroll());
self.reveal_commit_selection();
Expand Down
130 changes: 125 additions & 5 deletions crates/winc-app/src/app/git/commit/others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl OtherGroup {
}

/// 多仓库时改动列表中当前仓库那一节以外的一行。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum OtherRow {
/// 当前仓库的标题行;它的改动列表([`App::commit_rows`])紧接在下面。
Current,
Expand All @@ -59,6 +59,10 @@ pub(crate) enum OtherRow {
/// 另一个仓库中有更改的文件:仓库的下标,文件在该仓库状态各项中的下标。暂存区视图中是
/// 尚未暂存的那一部分。
File(usize, usize),
/// 其他仓库更改树中的目录:仓库、分组、相对路径、合并显示的名称、层数。
Dir(usize, OtherGroup, PathBuf, String, usize),
/// 其他仓库更改树中的文件:仓库、状态下标、分组、层数。
TreeFile(usize, usize, OtherGroup, usize),
}

/// 决定 [`App::commit_other_rows`] 的各项:都没变时各行也不变。
Expand All @@ -70,6 +74,8 @@ pub(crate) struct OtherRowsKey {
root: Option<PathBuf>,
/// 是否暂存区视图(分组不同)。
staging: bool,
/// 列表、树形、排序、紧凑目录及目录折叠状态的版本。
layout: u64,
/// 仓库列表。
repositories: Vec<PathBuf>,
/// 折叠着的仓库和组。
Expand All @@ -86,6 +92,68 @@ fn commits_staged(entry: &Entry) -> bool {
entry.staged.is_some() && !entry.conflicted()
}

/// 将其他仓库一组的改动按目录排列,沿用当前仓库的紧凑目录和文件排序规则。
fn push_other_level<'a>(
commit: &CommitState,
status: &Status,
(repo, root, group): (usize, &Path, OtherGroup),
dir: &Path,
items: Vec<(usize, &'a Path)>,
depth: usize,
out: &mut Vec<OtherRow>,
) {
let mut files = Vec::new();
let mut dirs: BTreeMap<NaturalName<'a>, Vec<(usize, &'a Path)>> = BTreeMap::new();
for (index, path) in items {
let mut parts = path.components();
match (parts.next(), parts.as_path()) {
(Some(first), rest) if !rest.as_os_str().is_empty() => {
dirs.entry(NaturalName(first.as_os_str()))
.or_default()
.push((index, rest));
}
_ => files.push((index, path)),
}
}
for (NaturalName(name), mut sub) in dirs {
let mut label = PathBuf::from(name);
while let Some(next) = only_subdir(&sub).filter(|_| commit.compact) {
label.push(next);
for (_, path) in &mut sub {
let rest: &'a Path = path;
*path = rest.strip_prefix(next).unwrap_or(Path::new(""));
}
}
let full = dir.join(&label);
let label = label
.components()
.map(|p| p.as_os_str().to_string_lossy())
.collect::<Vec<_>>()
.join("/");
out.push(OtherRow::Dir(repo, group, full.clone(), label, depth));
if !commit
.other_collapsed_dirs
.contains(&(root.to_path_buf(), group, full.clone()))
{
push_other_level(
commit,
status,
(repo, root, group),
&full,
sub,
depth + 1,
out,
);
}
}
commit.sort_files(status, &mut files);
out.extend(
files
.into_iter()
.map(|(index, _)| OtherRow::TreeFile(repo, index, group, depth)),
);
}

impl App {
/// 「显示全部仓库」是否开着:没有选过时勾选式开着,暂存区视图关着。
pub(crate) fn commit_all_repositories(&self) -> bool {
Expand Down Expand Up @@ -114,6 +182,7 @@ impl App {
version: self.git.summaries_version(),
root: self.git.root.clone(),
staging: self.git.commit.staging(),
layout: self.git.commit.layout,
repositories: self.git.repositories.clone(),
folds: self.git.commit.other_folds.clone(),
current_changed: self.current_changed(),
Expand Down Expand Up @@ -185,10 +254,30 @@ impl App {
if folded.contains(&(repo.clone(), Some(group))) {
continue;
}
rows.extend(files.into_iter().map(|n| match group {
OtherGroup::Staged => OtherRow::Staged(i, n),
_ => OtherRow::File(i, n),
}));
let mut files: Vec<(usize, &Path)> = files
.into_iter()
.map(|n| {
let path = &status.entries[n].path;
(n, path.strip_prefix(repo).unwrap_or(path))
})
.collect();
if self.git.commit.flat() {
self.git.commit.sort_files(status, &mut files);
rows.extend(files.into_iter().map(|(n, _)| match group {
OtherGroup::Staged => OtherRow::Staged(i, n),
_ => OtherRow::File(i, n),
}));
} else {
push_other_level(
&self.git.commit,
status,
(i, repo, group),
Path::new(""),
files,
0,
&mut rows,
);
}
}
}
// 当前仓库不在仓库列表里(刚打开、还没查找完)时排在最前。
Expand Down Expand Up @@ -351,6 +440,37 @@ impl App {
self.git.commit.other_folds.contains(&(root.clone(), group))
}

/// 其他仓库中某一组的目录是否收起。
pub(crate) fn other_dir_folded(&self, repo: usize, group: OtherGroup, dir: &Path) -> bool {
self.git.repositories.get(repo).is_some_and(|root| {
self.git
.commit
.other_collapsed_dirs
.contains(&(root.clone(), group, dir.to_path_buf()))
})
}

/// 点击其他仓库的目录,仅展开、收起该目录,保留当前仓库。
pub(super) fn toggle_other_dir(&mut self, row: usize) {
let rows = self.commit_other_rows();
let Some(OtherRow::Dir(repo, group, dir, ..)) = rows.get(row) else {
return;
};
let Some(root) = self.git.repositories.get(*repo) else {
return;
};
let key = (root.clone(), *group, dir.clone());
let commit = &mut self.git.commit;
if !commit.other_collapsed_dirs.remove(&key) {
commit.other_collapsed_dirs.insert(key);
}
commit.layout += 1;
commit.motion = None;
commit.fold_motion = None;
self.git.commit.scroll = self.git.commit.scroll.min(self.commit_max_scroll());
self.request_redraw();
}

/// 标题行 `header` 的折叠箭头转过的比例:0 指向右(收起),1 指向下(展开)。该行正在展开
/// 或收起时随动画转动,否则按 `open`。
pub(crate) fn other_fold_turn(&self, header: OtherRow, open: bool) -> f32 {
Expand Down
1 change: 1 addition & 0 deletions crates/winc-app/src/app/git/commit/press.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl App {
CommitTarget::OtherGroup(repo, group) => {
self.toggle_other_fold(Some(repo), Some(group))
}
CommitTarget::OtherDir(row) => self.toggle_other_dir(row),
CommitTarget::OtherFold(repo) => self.toggle_other_fold(Some(repo), None),
CommitTarget::CurrentFold => self.toggle_other_fold(None, None),
CommitTarget::CurrentRepository => self.focus = Focus::CommitList,
Expand Down
3 changes: 2 additions & 1 deletion crates/winc-app/src/app/git/commit/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl CommitState {
}

/// 按 [`CommitState::sort`] 排 `files`(各项在状态中的下标和相对仓库根的路径)。
fn sort_files(&self, status: &Status, files: &mut [(usize, &Path)]) {
pub(super) fn sort_files(&self, status: &Status, files: &mut [(usize, &Path)]) {
let letter = |index: usize| status.entries.get(index).map(|e| e.shown().letter());
files.sort_by(|a, b| match self.sort {
CommitSort::Path => natural_path_cmp(a.1, b.1),
Expand All @@ -284,6 +284,7 @@ impl CommitState {
self.collapsed = [collapse; CommitGroup::COUNT];
if !collapse {
self.forget_dirs();
self.other_collapsed_dirs.clear();
}
self.layout += 1;
}
Expand Down
39 changes: 39 additions & 0 deletions crates/winc-app/src/app/git/refresh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Git 视图可见时补查本地状态,恢复目录监视未报告的改动。

use std::time::Instant;

use super::STATUS_RECHECK;
use crate::app::App;

impl App {
/// Git 视图可见且窗口有焦点时低频补查本地状态,兜底系统未报告的文件变化。
///
/// 通知仍负责即时刷新;补查只读取状态,不读取分支、储藏或访问远程仓库。
pub(in crate::app) fn git_status_tick(&mut self, now: Instant) -> Option<Instant> {
let visible = self.commit_shown()
|| self
.editor
.active()
.is_some_and(|d| d.log.is_some() || d.diff.is_some());
let local = self
.git
.root
.as_deref()
.is_some_and(|root| !winc_backend::is_remote(root));
if !self.git_enabled() || !local || !visible || !self.window.focused || self.unseen() {
self.git.recheck_due = None;
return None;
}
let due = *self.git.recheck_due.get_or_insert(now + STATUS_RECHECK);
if now >= due {
self.git.recheck_due = Some(now + STATUS_RECHECK);
if self.git.running.is_none()
&& self.git.branch.running.is_none()
&& !self.git.commit.staging_active()
{
self.git_refresh_status();
}
}
self.git.recheck_due
}
}
Loading