Certainly! Below is a Rust implementation of a tool to search SVN commit messages, including those from its externals. This tool uses the svn command-line tool to interact with the SVN repository.
First, you'll need to add the std and serde dependencies to your Cargo.toml:
toml
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
regex = "1"
Here's the implementation of the tool:
rust
use std::process::Command;
use std::str;
use regex::Regex;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Commit {
revision: String,
author: String,
date: String,
message: String,
}
fn get_svn_log(path: &str, limit: Option<usize>) -> Result<String, std::io::Error> {
let mut cmd = Command::new("svn");
cmd.arg("log");
if let Some(limit) = limit {
cmd.arg(format!("--limit={}", limit));
}
cmd.arg("--xml");
cmd.arg(path);
let output = cmd.output()?;
if output.status.success() {
Ok(str::from_utf8(&output.stdout)?.to_string())
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"SVN command failed",
))
}
}
fn parse_svn_log(log: &str) -> Result<Vec<Commit>, serde_xml_rs::Error> {
let commits: Vec<Commit> = serde_xml_rs::from_str(log)?
.entries
.into_iter()
.map(|entry| Commit {
revision: entry.revision.number,
author: entry.author,
date: entry.date,
message: entry.msg,
})
.collect();
Ok(commits)
}
fn search_commits(commits: &[Commit], query: &str) -> Vec<&Commit> {
let re = Regex::new(query).unwrap();
commits.iter().filter(|commit| re.is_match(&commit.message)).collect()
}
fn main() {
let path = ".";
let limit = Some(100); // Adjust the limit as needed
let query = "your_search_query"; // Replace with your actual search query
match get_svn_log(path, limit) {
Ok(log) => {
match parse_svn_log(&log) {
Ok(commits) => {
let results = search_commits(&commits, query);
for commit in results {
println!("Revision: {}, Author: {}, Date: {}, Message: {}",
commit.revision, commit.author, commit.date, commit.message);
}
}
Err(e) => eprintln!("Failed to parse SVN log: {}", e),
}
}
Err(e) => eprintln!("Failed to get SVN log: {}", e),
}
}
Explanation:
Dependencies:
serde and serde_json for serializing and deserializing data.
regex for performing regex searches on commit messages.
Functions:
get_svn_log: Executes the svn log command and retrieves the log in XML format.
parse_svn_log: Parses the XML log into a vector of Commit structs.
search_commits: Searches the commit messages using a regex pattern.
Main Function:
Specifies the path to the SVN repository.
Sets a limit on the number of commits to retrieve (optional).
Defines the search query.
Retrieves and parses the SVN log.
Searches the commit messages and prints the results.
Notes:
Ensure you have svn installed and available in your PATH.
The search query is a regex pattern. You can adjust it as needed.
The tool currently searches only the main repository. To include externals, you would need to recursively call get_svn_log on each external directory, which is more complex and depends on your repository structure.