|
| 1 | +//! Utilities to fetch, confirm and save a list of untracked files, so we can |
| 2 | +//! prompt the user about them. |
| 3 | +
|
| 4 | +use console::{Key, Term}; |
| 5 | +use eyre::Context; |
| 6 | +use std::io::Write as IoWrite; |
| 7 | +use std::time::SystemTime; |
| 8 | +use std::{collections::HashSet, fmt::Write}; |
| 9 | +use tracing::instrument; |
| 10 | + |
| 11 | +use crate::git::{GitRunInfo, Repo}; |
| 12 | + |
| 13 | +use super::{effects::Effects, eventlog::EventTransactionId, formatting::Pluralize}; |
| 14 | + |
| 15 | +/// TODO |
| 16 | +#[instrument] |
| 17 | +pub fn prompt_about_untracked_files( |
| 18 | + effects: &Effects, |
| 19 | + git_run_info: &GitRunInfo, |
| 20 | + repo: &Repo, |
| 21 | + event_tx_id: EventTransactionId, |
| 22 | +) -> eyre::Result<Vec<String>> { |
| 23 | + let conn = repo.get_db_conn()?; |
| 24 | + |
| 25 | + let cached_files = get_cached_untracked_files(&conn)?; |
| 26 | + let real_files = get_real_untracked_files(repo, event_tx_id, git_run_info)?; |
| 27 | + let new_files: Vec<&String> = real_files.difference(&cached_files).collect(); |
| 28 | + |
| 29 | + let mut files_to_add = vec![]; |
| 30 | + if !new_files.is_empty() { |
| 31 | + writeln!( |
| 32 | + effects.get_output_stream(), |
| 33 | + "Found {}:", |
| 34 | + Pluralize { |
| 35 | + determiner: None, |
| 36 | + amount: new_files.len(), |
| 37 | + unit: ("new untracked file", "new untracked files"), |
| 38 | + }, |
| 39 | + )?; |
| 40 | + 'outer: for file in new_files { |
| 41 | + write!( |
| 42 | + effects.get_output_stream(), |
| 43 | + " Add file '{file}'? [Yes/(N)o/nOne] " |
| 44 | + )?; |
| 45 | + std::io::stdout().flush()?; |
| 46 | + |
| 47 | + let term = Term::stderr(); |
| 48 | + 'inner: loop { |
| 49 | + let key = term.read_key()?; |
| 50 | + match key { |
| 51 | + Key::Char('y') | Key::Char('Y') => { |
| 52 | + files_to_add.push(file.clone()); |
| 53 | + writeln!(effects.get_output_stream(), "adding")?; |
| 54 | + } |
| 55 | + Key::Char('n') | Key::Char('N') | Key::Enter => { |
| 56 | + writeln!(effects.get_output_stream(), "not adding")?; |
| 57 | + } |
| 58 | + Key::Char('o') | Key::Char('O') => { |
| 59 | + writeln!(effects.get_output_stream(), "skipping remaining")?; |
| 60 | + break 'outer; |
| 61 | + } |
| 62 | + _ => continue 'inner, |
| 63 | + }; |
| 64 | + continue 'outer; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + cache_untracked_files(&conn, real_files)?; |
| 70 | + |
| 71 | + Ok(files_to_add) |
| 72 | +} |
| 73 | + |
| 74 | +/// TODO |
| 75 | +#[instrument] |
| 76 | +fn get_real_untracked_files( |
| 77 | + repo: &Repo, |
| 78 | + event_tx_id: EventTransactionId, |
| 79 | + git_run_info: &GitRunInfo, |
| 80 | +) -> eyre::Result<HashSet<String>> { |
| 81 | + let args = vec!["ls-files", "--others", "--exclude-standard", "-z"]; |
| 82 | + let files_str = git_run_info |
| 83 | + .run_silent(repo, Some(event_tx_id), &args, Default::default()) |
| 84 | + .wrap_err("calling `git ls-files`")? |
| 85 | + .stdout; |
| 86 | + let files_str = String::from_utf8(files_str).wrap_err("Decoding stdout from Git subprocess")?; |
| 87 | + let files = files_str |
| 88 | + .trim() |
| 89 | + .split('\0') |
| 90 | + .filter_map(|s| { |
| 91 | + if s.is_empty() { |
| 92 | + None |
| 93 | + } else { |
| 94 | + Some(s.to_owned()) |
| 95 | + } |
| 96 | + }) |
| 97 | + .collect(); |
| 98 | + Ok(files) |
| 99 | +} |
| 100 | + |
| 101 | +/// TODO |
| 102 | +#[instrument] |
| 103 | +fn cache_untracked_files(conn: &rusqlite::Connection, files: HashSet<String>) -> eyre::Result<()> { |
| 104 | + { |
| 105 | + conn.execute("DROP TABLE IF EXISTS untracked_files", rusqlite::params![]) |
| 106 | + .wrap_err("Removing `untracked_files` table")?; |
| 107 | + } |
| 108 | + |
| 109 | + init_untracked_files_table(conn)?; |
| 110 | + |
| 111 | + { |
| 112 | + let tx = conn.unchecked_transaction()?; |
| 113 | + |
| 114 | + let timestamp = SystemTime::now() |
| 115 | + .duration_since(SystemTime::UNIX_EPOCH) |
| 116 | + .wrap_err("Calculating event transaction timestamp")? |
| 117 | + .as_secs_f64(); |
| 118 | + for file in files { |
| 119 | + tx.execute( |
| 120 | + " |
| 121 | + INSERT INTO untracked_files |
| 122 | + (timestamp, file) |
| 123 | + VALUES |
| 124 | + (:timestamp, :file) |
| 125 | + ", |
| 126 | + rusqlite::named_params! { |
| 127 | + ":timestamp": timestamp, |
| 128 | + ":file": file, |
| 129 | + }, |
| 130 | + )?; |
| 131 | + } |
| 132 | + tx.commit()?; |
| 133 | + } |
| 134 | + |
| 135 | + Ok(()) |
| 136 | +} |
| 137 | + |
| 138 | +/// Ensure the untracked_files table exists; creating it if it does not. |
| 139 | +#[instrument] |
| 140 | +fn init_untracked_files_table(conn: &rusqlite::Connection) -> eyre::Result<()> { |
| 141 | + conn.execute( |
| 142 | + " |
| 143 | + CREATE TABLE IF NOT EXISTS untracked_files ( |
| 144 | + timestamp REAL NOT NULL, |
| 145 | + file TEXT NOT NULL |
| 146 | + ) |
| 147 | + ", |
| 148 | + rusqlite::params![], |
| 149 | + ) |
| 150 | + .wrap_err("Creating `untracked_files` table")?; |
| 151 | + |
| 152 | + Ok(()) |
| 153 | +} |
| 154 | + |
| 155 | +/// TODO |
| 156 | +#[instrument] |
| 157 | +pub fn get_cached_untracked_files(conn: &rusqlite::Connection) -> eyre::Result<HashSet<String>> { |
| 158 | + init_untracked_files_table(conn)?; |
| 159 | + |
| 160 | + let mut stmt = conn.prepare("SELECT file FROM untracked_files")?; |
| 161 | + let paths = stmt |
| 162 | + .query_map(rusqlite::named_params![], |row| row.get("file"))? |
| 163 | + .filter_map(|p| p.ok()) |
| 164 | + .collect(); |
| 165 | + Ok(paths) |
| 166 | +} |
0 commit comments