Radish alpha
r
rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt
Git libraries for Radicle
Radicle
Git
surf: Remove `last_commit` field from `Entry` and `Tree`
Merged did:key:z6MkkfM3...sVz5 opened 9 months ago

Instead of providing the last commit for each Entry and Tree, one should use surf::repo::Repository::last_commit to calculate the last commit of a file where needed.

3 files changed +35 -98 603a1882 786248ad
modified radicle-surf/src/repo.rs
@@ -259,19 +259,12 @@ impl Repository {
            .entries(self)?
            .map(|en| {
                let name = en.name().to_string();
-
                let path = en.path();
-
                let commit = self
-
                    .last_commit(&path, commit.id)?
-
                    .ok_or(error::Repo::PathNotFound(path))?;
-
                Ok(Entry::new(name, en.into(), commit))
+
                Ok(Entry::new(name, en.into()))
            })
            .collect::<Result<Vec<Entry>, Error>>()?;
        entries.sort();

-
        let last_commit = self
-
            .last_commit(path, commit)?
-
            .ok_or_else(|| error::Repo::PathNotFound(path.as_ref().to_path_buf()))?;
-
        Ok(Tree::new(dir.id(), entries, last_commit))
+
        Ok(Tree::new(dir.id(), entries))
    }

    /// Returns a [`Blob`] for `path` in `commit`.
modified radicle-surf/src/tree.rs
@@ -19,6 +19,7 @@
//! See git [doc](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects) for more details.

use std::cmp::Ordering;
+
use std::path::Path;

use radicle_git_ext::Oid;
#[cfg(feature = "serde")]
@@ -28,7 +29,7 @@ use serde::{
};
use url::Url;

-
use crate::{fs, Commit};
+
use crate::{fs, repo, Commit, Error, Repository, ToCommit};

/// Represents a tree object as in git. It is essentially the content of
/// one directory. Note that multiple directories can have the same content,
@@ -39,19 +40,13 @@ pub struct Tree {
    id: Oid,
    /// The first descendant entries for this tree.
    entries: Vec<Entry>,
-
    /// The commit object that created this tree object.
-
    commit: Commit,
}

impl Tree {
    /// Creates a new tree, ensuring the `entries` are sorted.
-
    pub(crate) fn new(id: Oid, mut entries: Vec<Entry>, commit: Commit) -> Self {
+
    pub(crate) fn new(id: Oid, mut entries: Vec<Entry>) -> Self {
        entries.sort();
-
        Self {
-
            id,
-
            entries,
-
            commit,
-
        }
+
        Self { id, entries }
    }

    pub fn object_id(&self) -> Oid {
@@ -59,8 +54,18 @@ impl Tree {
    }

    /// Returns the commit that created this tree.
-
    pub fn commit(&self) -> &Commit {
-
        &self.commit
+
    pub fn commit<C: ToCommit, P: AsRef<Path>>(
+
        &self,
+
        commit: C,
+
        path: &P,
+
        repo: &Repository,
+
    ) -> Result<Commit, Error> {
+
        let commit = commit
+
            .to_commit(repo)
+
            .map_err(|e| Error::ToCommit(e.into()))?;
+
        repo.last_commit(path, commit.id)?
+
            .ok_or(repo::error::Repo::PathNotFound(path.as_ref().to_path_buf()))
+
            .map_err(Into::into)
    }

    /// Returns the entries of the tree.
@@ -104,7 +109,6 @@ impl Serialize for Tree {
        let mut state = serializer.serialize_struct("Tree", FIELDS)?;
        state.serialize_field("oid", &self.id)?;
        state.serialize_field("entries", &self.entries)?;
-
        state.serialize_field("lastCommit", &self.commit)?;
        state.end()
    }
}
@@ -150,18 +154,11 @@ impl Ord for EntryKind {
pub struct Entry {
    name: String,
    entry: EntryKind,
-

-
    /// The commit object that created this entry object.
-
    commit: Commit,
}

impl Entry {
-
    pub(crate) fn new(name: String, entry: EntryKind, commit: Commit) -> Self {
-
        Self {
-
            name,
-
            entry,
-
            commit,
-
        }
+
    pub(crate) fn new(name: String, entry: EntryKind) -> Self {
+
        Self { name, entry }
    }

    pub fn name(&self) -> &str {
@@ -176,8 +173,18 @@ impl Entry {
        matches!(self.entry, EntryKind::Tree(_))
    }

-
    pub fn commit(&self) -> &Commit {
-
        &self.commit
+
    pub fn commit<C: ToCommit, P: AsRef<Path>>(
+
        &self,
+
        commit: C,
+
        path: &P,
+
        repo: &Repository,
+
    ) -> Result<Commit, Error> {
+
        let commit = commit
+
            .to_commit(repo)
+
            .map_err(|e| Error::ToCommit(e.into()))?;
+
        repo.last_commit(path, commit.id)?
+
            .ok_or(repo::error::Repo::PathNotFound(path.as_ref().to_path_buf()))
+
            .map_err(Into::into)
    }

    pub fn object_id(&self) -> Oid {
@@ -268,7 +275,6 @@ impl Serialize for Entry {
            state.serialize_field("url", url)?;
        };
        state.serialize_field("oid", &self.object_id())?;
-
        state.serialize_field("lastCommit", &self.commit)?;
        state.end()
    }
}
modified radicle-surf/t/src/source.rs
@@ -15,66 +15,15 @@ fn tree_serialization() {
      "entries": [
        {
          "kind": "blob",
-
          "lastCommit": {
-
            "author": {
-
              "email": "fintan.halpenny@gmail.com",
-
              "name": "Fintan Halpenny",
-
              "time": 1578309972
-
            },
-
            "committer": {
-
              "email": "noreply@github.com",
-
              "name": "GitHub",
-
              "time": 1578309972
-
            },
-
            "description": "I want to have files under src that have separate commits.\r\nThat way src's latest commit isn't the same as all its files, instead it's the file that was touched last.",
-
            "id": "3873745c8f6ffb45c990eb23b491d4b4b6182f95",
-
            "message": "Extend the docs (#2)\n\nI want to have files under src that have separate commits.\r\nThat way src's latest commit isn't the same as all its files, instead it's the file that was touched last.",
-
            "parents": ["d6880352fc7fda8f521ae9b7357668b17bb5bad5"],
-
            "summary": "Extend the docs (#2)"
-
          },
          "name": "Eval.hs",
          "oid": "7d6240123a8d8ea8a8376610168a0a4bcb96afd0"
        },
        {
          "kind": "blob",
-
          "lastCommit": {
-
            "author": {
-
              "email": "rudolfs@osins.org",
-
              "name": "Rūdolfs Ošiņš",
-
              "time": 1575283266
-
            },
-
            "committer": {
-
              "email": "rudolfs@osins.org",
-
              "name": "Rūdolfs Ošiņš",
-
              "time": 1575283266
-
            },
-
            "description": "",
-
            "id": "e24124b7538658220b5aaf3b6ef53758f0a106dc",
-
            "message": "Move examples to \"src\"\n",
-
            "parents": ["19bec071db6474af89c866a1bd0e4b1ff76e2b97"],
-
            "summary": "Move examples to \"src\""
-
          },
          "name": "memory.rs",
          "oid": "b84992d24be67536837f5ab45a943f1b3f501878"
        }
      ],
-
      "lastCommit": {
-
        "author": {
-
          "email": "rudolfs@osins.org",
-
          "name": "Rūdolfs Ošiņš",
-
          "time": 1582198877
-
        },
-
        "committer": {
-
          "email": "noreply@github.com",
-
          "name": "GitHub",
-
          "time": 1582198877
-
        },
-
        "description": "It was a bad idea to have an actual source file which is used by\r\nradicle-upstream in the fixtures repository. It gets in the way of\r\nlinting and editors pick it up as a regular source file by accident.",
-
        "id": "a57846bbc8ced6587bf8329fc4bce970eb7b757e",
-
        "message": "Remove src/Folder.svelte (#3)\n\nIt was a bad idea to have an actual source file which is used by\r\nradicle-upstream in the fixtures repository. It gets in the way of\r\nlinting and editors pick it up as a regular source file by accident.",
-
        "parents": ["3873745c8f6ffb45c990eb23b491d4b4b6182f95"],
-
        "summary": "Remove src/Folder.svelte (#3)"
-
      },
      "oid": "ed52e9f8dfe1d8b374b2a118c25235349a743dd2"
    });
    assert_eq!(serde_json::to_value(tree).unwrap(), expected)
@@ -89,9 +38,9 @@ fn repo_tree_empty_branch() {

    // Verify the last commit is the empty commit.
    assert_eq!(
-
        tree.commit().id.to_string(),
-
        "e972683fe8136bf8a5cb2378cf50303554008049"
-
    );
+
        tree.object_id().to_string(),
+
        "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
+
    )
}

#[test]
@@ -102,12 +51,6 @@ fn repo_tree() {
        .unwrap();
    assert_eq!(tree.entries().len(), 3);

-
    let commit_header = tree.commit();
-
    assert_eq!(
-
        commit_header.id.to_string(),
-
        "e24124b7538658220b5aaf3b6ef53758f0a106dc"
-
    );
-

    let tree_oid = tree.object_id();
    assert_eq!(
        tree_oid.to_string(),
@@ -123,11 +66,6 @@ fn repo_tree() {
        entry.object_id().to_string(),
        "8c7447d13b907aa994ac3a38317c1e9633bf0732"
    );
-
    let commit = entry.commit();
-
    assert_eq!(
-
        commit.id.to_string(),
-
        "e24124b7538658220b5aaf3b6ef53758f0a106dc"
-
    );

    // Verify that an empty path works for getting the root tree.
    let root_tree = repo