Skip to content

Commit 846e9c5

Browse files
committed
Set calling process directly because delta started it
This info then takes precedence over whatever start_determining_calling_process_in_thread() finds or rather doesn't find. (The simple yet generous SeqCst is used on purpose for the atomic operations.)
1 parent f0aa2f9 commit 846e9c5

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod subcommands;
2424

2525
mod tests;
2626

27-
use std::ffi::OsString;
27+
use std::ffi::{OsStr, OsString};
2828
use std::io::{self, BufRead, Cursor, ErrorKind, IsTerminal, Write};
2929
use std::process::{self, Command, Stdio};
3030

@@ -91,6 +91,15 @@ pub fn run_app(
9191
} else if let Call::Help(msg) = call {
9292
OutputType::oneshot_write(msg)?;
9393
return Ok(0);
94+
} else if let Call::SubCommand(_, cmd) = &call {
95+
// Set before creating the Config, which already asks for the calling process
96+
// (not required for Call::DeltaDiff)
97+
utils::process::set_calling_process(
98+
&cmd.args
99+
.iter()
100+
.map(|arg| OsStr::to_string_lossy(arg).to_string())
101+
.collect::<Vec<_>>(),
102+
);
94103
}
95104
let opt = opt.expect("Opt is set");
96105

src/utils/process.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::{HashMap, HashSet};
22
use std::path::Path;
3+
use std::sync::atomic::{AtomicUsize, Ordering};
34
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
45

56
use lazy_static::lazy_static;
@@ -19,7 +20,6 @@ pub enum CallingProcess {
1920
None, // no matching process could be found
2021
Pending, // calling process is currently being determined
2122
}
22-
// TODO: Git blame is currently handled differently
2323

2424
impl CallingProcess {
2525
pub fn paths_in_input_are_relative_to_cwd(&self) -> bool {
@@ -47,6 +47,15 @@ lazy_static! {
4747
Arc::new((Mutex::new(CallingProcess::Pending), Condvar::new()));
4848
}
4949

50+
// The information where the calling process info comes from *should* be inside
51+
// `CallingProcess`, but that is handed out (within a MutexGuard) to callers.
52+
// To keep the interface simple, store the info here:
53+
static CALLER_INFO_SOURCE: AtomicUsize = AtomicUsize::new(CALLER_GUESSED);
54+
const CALLER_GUESSED: usize = 1;
55+
const CALLER_KNOWN: usize = 2;
56+
57+
// delta was called by this process (or called by something which called delta and it),
58+
// try looking up this information in the process tree.
5059
pub fn start_determining_calling_process_in_thread() {
5160
// The handle is neither kept nor returned nor joined but dropped, so the main
5261
// thread can exit early if it does not need to know its parent process.
@@ -58,12 +67,28 @@ pub fn start_determining_calling_process_in_thread() {
5867
let (caller_mutex, determine_done) = &**CALLER;
5968

6069
let mut caller = caller_mutex.lock().unwrap();
61-
*caller = calling_process;
70+
71+
if CALLER_INFO_SOURCE.load(Ordering::SeqCst) <= CALLER_GUESSED {
72+
*caller = calling_process;
73+
}
74+
6275
determine_done.notify_all();
6376
})
6477
.unwrap();
6578
}
6679

80+
// delta starts the process, so it is known.
81+
pub fn set_calling_process(args: &[String]) {
82+
if let ProcessArgs::Args(result) = describe_calling_process(args) {
83+
let (caller_mutex, determine_done) = &**CALLER;
84+
85+
let mut caller = caller_mutex.lock().unwrap();
86+
*caller = result;
87+
CALLER_INFO_SOURCE.store(CALLER_KNOWN, Ordering::SeqCst);
88+
determine_done.notify_all();
89+
}
90+
}
91+
6792
#[cfg(not(test))]
6893
pub fn calling_process() -> MutexGuard<'static, CallingProcess> {
6994
let (caller_mutex, determine_done) = &**CALLER;

0 commit comments

Comments
 (0)