From 27e74cfd04a6af6d374ad671995705fc54ba3ac7 Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Jan 31 2019 18:04:29 +0000 Subject: Fix 'doc.rs' documentation generation error. Seperate Cargo build script from actual source code. --- diff --git a/Cargo.toml b/Cargo.toml index 035c3c6..8069041 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,15 @@ [package] name = "ambassade-debug" -version = "0.1.0" +version = "0.2.0" authors = ["Jan Allersma "] -description = "A crate to submit debug information when an application crashes." -license = "MIT OR Apache-2.0" +description = "A crate to submit postmortem debug information when an application crashes." +license = "MIT" build = "build.rs" repository = "https://pagure.io/Ambassade-debug" +documentation = "https://docs.rs/crate/ambassade-debug" [build-dependencies] git2 = "0.8" [dependencies] +git2 = "0.8" diff --git a/build.rs b/build.rs index 9a82f6e..11189ba 100644 --- a/build.rs +++ b/build.rs @@ -1,20 +1,10 @@ extern crate git2; -use std::env; -use std::fs::File; -use std::io::Write; -use std::path::Path; - fn main() { - let mut dirty_repo = String::new(); + println!("cargo:rerun-if-changed=."); match git2::Repository::discover("./") { Ok(repo) => { - let remotes = match repo.remotes() { - Ok(array) => array, - Err(e) => panic!("No remotes found. Details: {}", e) - }; - let head = match repo.head() { Ok(reference) => match reference.peel_to_tree() { Ok(tree) => tree, @@ -26,35 +16,22 @@ fn main() { let mut diff_option = git2::DiffOptions::new(); diff_option.include_untracked(true); - let _diff = match repo.diff_tree_to_workdir(Some(&head), Some(&mut diff_option)) { - Ok(result) => { - diff_check(); - dirty_repo = head.id().to_string() + " (dirty)"; - result - }, + match repo.diff_tree_to_workdir(Some(&head), Some(&mut diff_option)) { + Ok(ref diff) => diff_check(diff), Err(e) => panic!("Could not create diff. Details: {}", e) - }; - generate_build_info(dirty_repo, remotes); + } }, Err(e) => println!("No git repo found. Details: {}", e) } } -fn diff_check() { - println!("cargo:warning=ambassade-debug: Current repository is dirty, meaning that debug info from this build will be less reliable."); -} - -fn generate_build_info(sha: String, _remotes: git2::string_array::StringArray) { - let out_dir = env::var("OUT_DIR").unwrap(); - let dest_path = Path::new(&out_dir).join("build_info.rs"); - - let mut content = String::from("/// Generated by a Cargo build script. Indicates the git status of the working directory.\n"); - content.push_str("pub fn sha() -> &'static str {"); - content.push_str("\""); - content.push_str(sha.as_str()); - content.push_str("\"\n}"); - - let mut file = File::create(&dest_path).unwrap(); - - file.write_all(content.as_bytes()).unwrap(); +fn diff_check(diff: &git2::Diff) { + match diff.stats() { + Ok(ref stats) if stats.files_changed() > 0 => { + println!("cargo:warning=ambassade-debug: Current repository is dirty, meaning that debug info from this build will be less reliable."); + println!("cargo:warning=ambassade-debug: Changed files: {:?}", stats.files_changed()); + }, + Ok(_) => {}, + Err(e) => panic!("Could not read stats from git tree. Details: {}", e.to_string()) + } } diff --git a/src/git.rs b/src/git.rs new file mode 100644 index 0000000..b510324 --- /dev/null +++ b/src/git.rs @@ -0,0 +1,32 @@ +extern crate git2; + +pub fn sha() -> Option { + match git2::Repository::discover("./") { + Ok(repo) => { + let _remotes = match repo.remotes() { + Ok(array) => array, + Err(e) => panic!("No remotes found. Details: {}", e) + }; + + let head = match repo.head() { + Ok(reference) => match reference.peel_to_tree() { + Ok(tree) => tree, + Err(e) => panic!("Could not peel head to tree! Details: {}", e) + } + Err(e) => panic!("Could not find head. Details: {}", e) + }; + + let mut diff_option = git2::DiffOptions::new(); + diff_option.include_untracked(true); + + match repo.diff_tree_to_workdir(Some(&head), Some(&mut diff_option)) { + Ok(_) => return Some(head.id().to_string() + " (dirty)"), + Err(e) => panic!("Could not create diff. Details: {}", e) + } + }, + Err(e) => { + println!("No git repo found. Details: {}", e); + None + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 68b5001..804a73a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ -include!(concat!(env!("OUT_DIR"), "/build_info.rs")); - /// This module contains all the tools that the `watch()` function needs. pub mod debug; +mod git; use std::{io, panic}; @@ -33,8 +32,13 @@ pub fn watch(method: F) where F: debug::SubmitMethod { Err(_) => String::from("Error while describing bug.") }; + let sha = match git::sha() { + Some(tree) => tree, + None => String::from("None") + }; + let dbg = debug::DebugInfo::new ( - sha(), + &sha, desc, panic_info.payload().downcast_ref::<&str>().unwrap(), panic_info.location()