<commit_before>extern crate git2;

use git2::{Repository, RepositoryState};
use std::fs::File;
use std::path::Path;
use std::io::Write;

fn main() {
  let git = git_commit().map(|x| format!("-g{}", x)).unwrap_or_default();
  let clean = match git_clean() {
    Some(true) => "-dirty",
    _ => ""
  };
  let version = std::env::var("CARGO_PKG_VERSION").unwrap();
  let version_string = format!("{}{}{}", version, git, clean);
  let out_dir = std::env::var("OUT_DIR").unwrap();
  let p = Path::new(&out_dir);
  let mut f = File::create(p.join("version")).unwrap();
  f.write_all(version_string.as_bytes()).unwrap();
}

fn git_commit() -> Option<String> {
  let repo = Repository::open(".").ok()?;
  let revparse = repo.revparse_single("HEAD").ok()?;
  revparse.short_id().ok()?.as_str().map(ToOwned::to_owned)
}

fn git_clean() -> Option<bool> {
  let repo = Repository::open(".").ok()?;
  Some(repo.state() == RepositoryState::Clean)
}<commit_msg>fix: actually check if working dir is dirty<commit_after>extern crate git2;

use git2::{Repository, StatusOptions};
use std::fs::File;
use std::path::Path;
use std::io::Write;

fn main() {
  let git = git_commit().map(|x| format!("-g{}", x)).unwrap_or_default();
  let clean = match git_clean() {
    Some(false) => "-dirty",
    _ => ""
  };
  let version = std::env::var("CARGO_PKG_VERSION").unwrap();
  let version_string = format!("{}{}{}", version, git, clean);
  let out_dir = std::env::var("OUT_DIR").unwrap();
  let p = Path::new(&out_dir);
  let mut f = File::create(p.join("version")).unwrap();
  f.write_all(version_string.as_bytes()).unwrap();
}

fn git_commit() -> Option<String> {
  let repo = Repository::open(".").ok()?;
  let revparse = repo.revparse_single("HEAD").ok()?;
  revparse.short_id().ok()?.as_str().map(ToOwned::to_owned)
}

fn git_clean() -> Option<bool> {
  let repo = Repository::open(".").ok()?;
  let mut options = StatusOptions::new();
  options
    .include_ignored(false)
    .include_untracked(false);
  let statuses = repo.statuses(Some(&mut options)).ok()?;
  Some(statuses.is_empty())
}
{'fraction_non_alphanumeric': 0.220935841775205, 'fraction_numerical': 0.001929570670525808, 'mean_word_length': 5.117994100294985, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7158324', 'n_tokens_mistral': 742, 'n_tokens_neox': 755, 'n_words': 143}