summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWesley Moore <wes@wezm.net>2019-07-08 11:09:04 +1000
committerWesley Moore <wes@wezm.net>2019-07-08 11:09:04 +1000
commit21855fb6e2e5f5a263dd47db0992350e4806f4cf (patch)
tree3abf891e8655a7f36a2c603a6b6e00984635c582
parent6d3266134a4ddad9818cb9ddd89785289216e5fa (diff)
Read and parse git ls-tree output
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock122
-rw-r--r--Cargo.toml5
-rw-r--r--src/annotate.rs19
-rw-r--r--src/git.rs71
-rw-r--r--src/lib.rs2
6 files changed, 218 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 53eaa21..ebdff5a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
/target
**/*.rs.bk
+/.idea
diff --git a/Cargo.lock b/Cargo.lock
index 7a56626..6d2b720 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,13 +1,39 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
-name = "annotate-rust"
+name = "aho-corasick"
+version = "0.7.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "annotaterust"
version = "0.1.0"
dependencies = [
+ "regex 1.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
+name = "itoa"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "lazy_static"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "memchr"
+version = "2.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "proc-macro2"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -16,20 +42,114 @@ dependencies = [
]
[[package]]
+name = "quote"
+version = "0.6.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "regex"
+version = "1.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "aho-corasick 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "regex-syntax 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "ryu"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "serde"
+version = "1.0.94"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.94"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "syn"
version = "0.15.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
+name = "thread_local"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "ucd-util"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
+[[package]]
+name = "utf8-ranges"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
[metadata]
+"checksum aho-corasick 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "36b7aa1ccb7d7ea3f437cf025a2ab1c47cc6c1bc9fc84918ff449def12f5e282"
+"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
+"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
+"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
+"checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
+"checksum regex 1.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "d9d8297cc20bbb6184f8b45ff61c8ee6a9ac56c156cec8e38c3e5084773c44ad"
+"checksum regex-syntax 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "9b01330cce219c1c6b2e209e5ed64ccd587ae5c67bed91c0b49eecf02ae40e21"
+"checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997"
+"checksum serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = "076a696fdea89c19d3baed462576b8f6d663064414b5c793642da8dfeb99475b"
+"checksum serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = "ef45eb79d6463b22f5f9e16d283798b7c0175ba6050bc25c1a946c122727fe7b"
+"checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704"
"checksum syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d960b829a55e56db167e861ddb43602c003c7be0bee1d345021703fac2fb7c"
+"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
+"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
+"checksum utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9d50aa7650df78abf942826607c62468ce18d9019673d4a2ebe1865dbb96ffde"
diff --git a/Cargo.toml b/Cargo.toml
index 5b899f6..a12ed6d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,8 +1,11 @@
[package]
-name = "annotate-rust"
+name = "annotaterust"
version = "0.1.0"
authors = ["Wesley Moore <wes@wezm.net>"]
edition = "2018"
[dependencies]
+regex = "1.1.9"
syn = { version = "0.15.39", default-features = false, features = ["full", "parsing", "visit", "extra-traits"] }
+serde = { version = "1.0", features = ["derive"] }
+serde_json = "1.0"
diff --git a/src/annotate.rs b/src/annotate.rs
new file mode 100644
index 0000000..4ba97bc
--- /dev/null
+++ b/src/annotate.rs
@@ -0,0 +1,19 @@
+use serde::Serialize;
+
+#[derive(Serialize)]
+#[serde(tag = "type")]
+enum Annotation {
+ Link {
+ lineno: u32,
+ colno: u32,
+ len: u32,
+ to: String,
+ title: Option<String>,
+ color: Option<String>,
+ },
+ Markdown {
+ lineno: u32,
+ title: String,
+ content: String,
+ },
+}
diff --git a/src/git.rs b/src/git.rs
new file mode 100644
index 0000000..293f225
--- /dev/null
+++ b/src/git.rs
@@ -0,0 +1,71 @@
+use std::io;
+use std::path::Path;
+use std::process::Command;
+use std::str;
+use std::str::Utf8Error;
+
+use regex::Regex;
+
+#[derive(Debug, PartialEq)]
+struct Blob<'a> {
+ object: &'a str,
+ path: &'a Path,
+}
+
+fn crawl_git_tree<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
+ let output = Command::new("git")
+ .current_dir(path)
+ .args(&["ls-tree", "-zr", "HEAD"])
+ .output()?;
+
+ if !output.status.success() {
+ return Err(io::Error::new(
+ io::ErrorKind::Other,
+ "git ls-tree did not run successfully",
+ ));
+ }
+
+ Ok(output.stdout)
+}
+
+fn parse_ls_tree_output<'a>(output: &'a [u8]) -> Result<Vec<Blob<'a>>, Utf8Error> {
+ let re = Regex::new(r"^[^ ]+ [^ ]+ ([^\t]+)\t(.+)$").unwrap();
+
+ let mut blobs = Vec::new();
+ for line in output.split(|&byte| byte == 0) {
+ let line = str::from_utf8(line)?;
+
+ if let Some(captures) = re.captures(line) {
+ blobs.push(Blob {
+ object: captures.get(1).unwrap().as_str(),
+ path: Path::new(captures.get(2).unwrap().as_str()),
+ })
+ }
+ }
+
+ Ok(blobs)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::git::{parse_ls_tree_output, Blob};
+
+ const TEST_OUTPUT: &[u8] = b"100644 blob 424965736fa85c814e0ecb12d42ec81021304ce9\tREADME.md\0invalid\0100644 blob 5ec8f34bce9b5b53f3d809591914fa1601534a8a src/main.rs\0";
+
+ #[test]
+ fn test_parse_ls_tree_output() {
+ let expected = vec![
+ Blob {
+ object: "424965736fa85c814e0ecb12d42ec81021304ce9",
+ path: Path::new("README.md"),
+ },
+ Blob {
+ object: "5ec8f34bce9b5b53f3d809591914fa1601534a8a",
+ path: Path::new("src/main.rs"),
+ },
+ ];
+
+ assert_eq!(parse_ls_tree_output(TEST_OUTPUT).unwrap(), expected);
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..4ec8e11
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,2 @@
+mod annotate;
+mod git;