Prefer PWD environment variable over cwd if available to better support symbolic links (#783)

This commit is contained in:
Patrick Decat 2023-03-15 00:15:39 +01:00 committed by GitHub
parent 88deec31c8
commit efd2230eba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 12 deletions

View file

@ -1,6 +1,7 @@
use std::{env, path::Path, str::FromStr}; use std::{env, path::Path, str::FromStr};
use async_trait::async_trait; use async_trait::async_trait;
use atuin_common::utils;
use chrono::{prelude::*, Utc}; use chrono::{prelude::*, Utc};
use fs_err as fs; use fs_err as fs;
use itertools::Itertools; use itertools::Itertools;
@ -31,10 +32,7 @@ pub fn current_context() -> Context {
std::process::exit(1); std::process::exit(1);
}; };
let hostname = format!("{}:{}", whoami::hostname(), whoami::username()); let hostname = format!("{}:{}", whoami::hostname(), whoami::username());
let cwd = match env::current_dir() { let cwd = utils::get_current_dir();
Ok(dir) => dir.display().to_string(),
Err(_) => String::from(""),
};
Context { Context {
session, session,

View file

@ -1,3 +1,4 @@
use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use chrono::NaiveDate; use chrono::NaiveDate;
@ -36,6 +37,17 @@ pub fn data_dir() -> PathBuf {
data_dir.join("atuin") data_dir.join("atuin")
} }
pub fn get_current_dir() -> String {
// Prefer PWD environment variable over cwd if available to better support symbolic links
match env::var("PWD") {
Ok(v) => v,
Err(_) => match env::current_dir() {
Ok(dir) => dir.display().to_string(),
Err(_) => String::from(""),
},
}
}
pub fn get_days_from_month(year: i32, month: u32) -> i64 { pub fn get_days_from_month(year: i32, month: u32) -> i64 {
NaiveDate::from_ymd( NaiveDate::from_ymd(
match month { match month {

View file

@ -5,6 +5,7 @@ use std::{
time::Duration, time::Duration,
}; };
use atuin_common::utils;
use clap::Subcommand; use clap::Subcommand;
use eyre::Result; use eyre::Result;
use runtime_format::{FormatKey, FormatKeyError, ParsedFmt}; use runtime_format::{FormatKey, FormatKeyError, ParsedFmt};
@ -181,8 +182,7 @@ impl Cmd {
// It's better for atuin to silently fail here and attempt to // It's better for atuin to silently fail here and attempt to
// store whatever is ran, than to throw an error to the terminal // store whatever is ran, than to throw an error to the terminal
let cwd = env::current_dir() let cwd = utils::get_current_dir();
.map_or_else(|_| String::new(), |dir| dir.display().to_string());
let h = History::new(chrono::Utc::now(), command, cwd, -1, -1, None, None); let h = History::new(chrono::Utc::now(), command, cwd, -1, -1, None, None);
@ -240,7 +240,7 @@ impl Cmd {
None None
}; };
let cwd = if *cwd { let cwd = if *cwd {
Some(env::current_dir()?.display().to_string()) Some(utils::get_current_dir())
} else { } else {
None None
}; };

View file

@ -1,3 +1,4 @@
use atuin_common::utils;
use chrono::Utc; use chrono::Utc;
use clap::Parser; use clap::Parser;
use eyre::Result; use eyre::Result;
@ -137,11 +138,7 @@ async fn run_non_interactive(
db: &mut impl Database, db: &mut impl Database,
) -> Result<usize> { ) -> Result<usize> {
let dir = if cwd.as_deref() == Some(".") { let dir = if cwd.as_deref() == Some(".") {
let current = std::env::current_dir()?; Some(utils::get_current_dir())
let current = current.as_os_str();
let current = current.to_str().unwrap();
Some(current.to_owned())
} else { } else {
cwd cwd
}; };