
Instead of a menagerie of macros for implementing `Action`, now there are just two: * `actions!(editor, [MoveLeft, MoveRight])` * `#[derive(..., Action)]` with `#[action(namespace = editor)]` In both contexts, `///` doc comments can be provided and will be used in `JsonSchema`. In both contexts, parameters can provided in `#[action(...)]`: - `namespace = some_namespace` sets the namespace. In Zed this is required. - `name = "ActionName"` overrides the action's name. This must not contain "::". - `no_json` causes the `build` method to always error and `action_json_schema` to return `None` and allows actions not implement `serde::Serialize` and `schemars::JsonSchema`. - `no_register` skips registering the action. This is useful for implementing the `Action` trait while not supporting invocation by name or JSON deserialization. - `deprecated_aliases = ["editor::SomeAction"]` specifies deprecated old names for the action. These action names should *not* correspond to any actions that are registered. These old names can then still be used to refer to invoke this action. In Zed, the keymap JSON schema will accept these old names and provide warnings. - `deprecated = "Message about why this action is deprecation"` specifies a deprecation message. In Zed, the keymap JSON schema will cause this to be displayed as a warning. This is a new feature. Also makes the following changes since this seems like a good time to make breaking changes: * In `zed.rs` tests adds a test with an explicit list of namespaces. The rationale for this is that there is otherwise no checking of `namespace = ...` attributes. * `Action::debug_name` renamed to `name_for_type`, since its only difference with `name` was that it * `Action::name` now returns `&'static str` instead of `&str` to match the return of `name_for_type`. This makes the action trait more limited, but the code was already assuming that `name_for_type` is the same as `name`, and it requires `&'static`. So really this just makes the trait harder to misuse. * Various action reflection methods now use `&'static str` instead of `SharedString`. Release Notes: - N/A
168 lines
4.2 KiB
Rust
168 lines
4.2 KiB
Rust
pub mod blame;
|
|
pub mod commit;
|
|
mod hosting_provider;
|
|
mod remote;
|
|
pub mod repository;
|
|
pub mod status;
|
|
|
|
pub use crate::hosting_provider::*;
|
|
pub use crate::remote::*;
|
|
use anyhow::{Context as _, Result};
|
|
pub use git2 as libgit;
|
|
use gpui::{Action, actions};
|
|
pub use repository::WORK_DIRECTORY_REPO_PATH;
|
|
use schemars::JsonSchema;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::ffi::OsStr;
|
|
use std::fmt;
|
|
use std::str::FromStr;
|
|
use std::sync::LazyLock;
|
|
|
|
pub static DOT_GIT: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new(".git"));
|
|
pub static GITIGNORE: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new(".gitignore"));
|
|
pub static FSMONITOR_DAEMON: LazyLock<&'static OsStr> =
|
|
LazyLock::new(|| OsStr::new("fsmonitor--daemon"));
|
|
pub static LFS_DIR: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new("lfs"));
|
|
pub static COMMIT_MESSAGE: LazyLock<&'static OsStr> =
|
|
LazyLock::new(|| OsStr::new("COMMIT_EDITMSG"));
|
|
pub static INDEX_LOCK: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new("index.lock"));
|
|
|
|
actions!(
|
|
git,
|
|
[
|
|
// per-hunk
|
|
ToggleStaged,
|
|
StageAndNext,
|
|
UnstageAndNext,
|
|
#[action(deprecated_aliases = ["editor::RevertSelectedHunks"])]
|
|
Restore,
|
|
// per-file
|
|
#[action(deprecated_aliases = ["editor::ToggleGitBlame"])]
|
|
Blame,
|
|
StageFile,
|
|
UnstageFile,
|
|
// repo-wide
|
|
StageAll,
|
|
UnstageAll,
|
|
RestoreTrackedFiles,
|
|
TrashUntrackedFiles,
|
|
Uncommit,
|
|
Push,
|
|
PushTo,
|
|
ForcePush,
|
|
Pull,
|
|
Fetch,
|
|
FetchFrom,
|
|
Commit,
|
|
Amend,
|
|
Cancel,
|
|
ExpandCommitEditor,
|
|
GenerateCommitMessage,
|
|
Init,
|
|
OpenModifiedFiles,
|
|
]
|
|
);
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
|
|
#[action(namespace = git, deprecated_aliases = ["editor::RevertFile"])]
|
|
pub struct RestoreFile {
|
|
#[serde(default)]
|
|
pub skip_prompt: bool,
|
|
}
|
|
|
|
/// The length of a Git short SHA.
|
|
pub const SHORT_SHA_LENGTH: usize = 7;
|
|
|
|
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
|
|
pub struct Oid(libgit::Oid);
|
|
|
|
impl Oid {
|
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
|
|
let oid = libgit::Oid::from_bytes(bytes).context("failed to parse bytes into git oid")?;
|
|
Ok(Self(oid))
|
|
}
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
self.0.as_bytes()
|
|
}
|
|
|
|
pub(crate) fn is_zero(&self) -> bool {
|
|
self.0.is_zero()
|
|
}
|
|
|
|
/// Returns this [`Oid`] as a short SHA.
|
|
pub fn display_short(&self) -> String {
|
|
self.to_string().chars().take(SHORT_SHA_LENGTH).collect()
|
|
}
|
|
}
|
|
|
|
impl FromStr for Oid {
|
|
type Err = anyhow::Error;
|
|
|
|
fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
|
|
libgit::Oid::from_str(s)
|
|
.context("parsing git oid")
|
|
.map(Self)
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Oid {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
fmt::Display::fmt(self, f)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Oid {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
self.0.fmt(f)
|
|
}
|
|
}
|
|
|
|
impl Serialize for Oid {
|
|
fn serialize<S>(&self, serializer: S) -> std::prelude::v1::Result<S::Ok, S::Error>
|
|
where
|
|
S: serde::Serializer,
|
|
{
|
|
serializer.serialize_str(&self.0.to_string())
|
|
}
|
|
}
|
|
|
|
impl<'de> Deserialize<'de> for Oid {
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: serde::Deserializer<'de>,
|
|
{
|
|
let s = String::deserialize(deserializer)?;
|
|
s.parse::<Oid>().map_err(serde::de::Error::custom)
|
|
}
|
|
}
|
|
|
|
impl Default for Oid {
|
|
fn default() -> Self {
|
|
Self(libgit::Oid::zero())
|
|
}
|
|
}
|
|
|
|
impl From<Oid> for u32 {
|
|
fn from(oid: Oid) -> Self {
|
|
let bytes = oid.0.as_bytes();
|
|
debug_assert!(bytes.len() > 4);
|
|
|
|
let mut u32_bytes: [u8; 4] = [0; 4];
|
|
u32_bytes.copy_from_slice(&bytes[..4]);
|
|
|
|
u32::from_ne_bytes(u32_bytes)
|
|
}
|
|
}
|
|
|
|
impl From<Oid> for usize {
|
|
fn from(oid: Oid) -> Self {
|
|
let bytes = oid.0.as_bytes();
|
|
debug_assert!(bytes.len() > 8);
|
|
|
|
let mut u64_bytes: [u8; 8] = [0; 8];
|
|
u64_bytes.copy_from_slice(&bytes[..8]);
|
|
|
|
u64::from_ne_bytes(u64_bytes) as usize
|
|
}
|
|
}
|