mirror of
https://github.com/tstack/lnav.git
synced 2024-08-17 00:40:30 +03:00
[session] try avoid using absolute file paths in session exports
This commit is contained in:
parent
1c793f6f28
commit
4e0dab843a
@ -3,6 +3,9 @@
|
||||
top_srcdir="@abssrcdir@"
|
||||
export top_srcdir
|
||||
|
||||
top_srcdir_parent=`dirname ${top_srcdir}`
|
||||
export top_srcdir_parent
|
||||
|
||||
srcdir="@abssrcdir@/test"
|
||||
export srcdir
|
||||
|
||||
@ -100,6 +103,7 @@ run_cap_test() {
|
||||
-e "s;${test_dir};{test_dir};g" \
|
||||
-e "s;${builddir};{builddir};g" \
|
||||
-e "s;${top_srcdir};{top_srcdir};g" \
|
||||
-e "s;${top_srcdir_parent};{top_srcdir_parent};g" \
|
||||
${test_file_base}_${test_hash}.out
|
||||
echo
|
||||
printf "%s \033[0;35m=============================================================\033[0m\n" $(date -Iseconds)
|
||||
|
@ -109,6 +109,60 @@ struct from_sqlite<log_file_session_state> {
|
||||
namespace lnav {
|
||||
namespace session {
|
||||
|
||||
static nonstd::optional<ghc::filesystem::path>
|
||||
find_container_dir(ghc::filesystem::path file_path)
|
||||
{
|
||||
nonstd::optional<ghc::filesystem::path> dir_with_last_readme;
|
||||
|
||||
while (file_path.has_parent_path()) {
|
||||
auto parent = file_path.parent_path();
|
||||
bool has_readme_entry = false;
|
||||
|
||||
for (const auto& entry : ghc::filesystem::directory_iterator(parent)) {
|
||||
if (!entry.is_regular_file()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto entry_filename = tolower(entry.path().filename().string());
|
||||
if (startswith(entry_filename, "readme")) {
|
||||
has_readme_entry = true;
|
||||
dir_with_last_readme = parent;
|
||||
}
|
||||
}
|
||||
if (!has_readme_entry && dir_with_last_readme) {
|
||||
return dir_with_last_readme;
|
||||
}
|
||||
|
||||
file_path = parent;
|
||||
}
|
||||
|
||||
return nonstd::nullopt;
|
||||
}
|
||||
|
||||
static std::string
|
||||
replace_home_dir(std::string path)
|
||||
{
|
||||
auto home_dir_opt = getenv_opt("HOME");
|
||||
|
||||
if (!home_dir_opt) {
|
||||
return path;
|
||||
}
|
||||
|
||||
const auto* home_dir = home_dir_opt.value();
|
||||
|
||||
if (startswith(path, home_dir)) {
|
||||
auto retval = path.substr(strlen(home_dir));
|
||||
|
||||
if (retval.front() != '/') {
|
||||
retval.insert(0, "/");
|
||||
}
|
||||
retval.insert(0, "$HOME");
|
||||
return retval;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
Result<void, lnav::console::user_message>
|
||||
export_to(FILE* file)
|
||||
{
|
||||
@ -141,6 +195,10 @@ SELECT content_id, format, time_offset FROM lnav_file
|
||||
|
||||
# The files loaded into the session were:
|
||||
|
||||
)";
|
||||
|
||||
static constexpr const char LOG_DIR_INSERT[] = R"(
|
||||
;INSERT OR IGNORE INTO environ (name, value) VALUES ('LOG_DIR_{}', {})
|
||||
)";
|
||||
|
||||
static constexpr const char MARK_HEADER[] = R"(
|
||||
@ -194,6 +252,9 @@ SELECT content_id, format, time_offset FROM lnav_file
|
||||
}
|
||||
|
||||
fmt::print(file, FMT_STRING(HEADER), sqlitepp::quote(PACKAGE_VERSION));
|
||||
|
||||
std::map<std::string, std::vector<std::string>> file_containers;
|
||||
std::set<std::string> raw_files;
|
||||
for (const auto& name_pair : lnav_data.ld_active_files.fc_file_names) {
|
||||
const auto& open_opts = name_pair.second;
|
||||
|
||||
@ -203,7 +264,36 @@ SELECT content_id, format, time_offset FROM lnav_file
|
||||
{
|
||||
continue;
|
||||
}
|
||||
fmt::print(file, FMT_STRING(":open {}"), name_pair.first);
|
||||
|
||||
auto file_path_str = name_pair.first;
|
||||
auto file_path = ghc::filesystem::path(file_path_str);
|
||||
auto container_path_opt = find_container_dir(file_path);
|
||||
if (container_path_opt) {
|
||||
auto container_parent = container_path_opt.value().parent_path();
|
||||
file_containers[container_parent.string()].push_back(
|
||||
ghc::filesystem::relative(file_path, container_parent)
|
||||
.string());
|
||||
} else {
|
||||
raw_files.insert(file_path_str);
|
||||
}
|
||||
}
|
||||
for (const auto& file_path_str : raw_files) {
|
||||
fmt::print(
|
||||
file, FMT_STRING(":open {}"), replace_home_dir(file_path_str));
|
||||
}
|
||||
size_t container_index = 0;
|
||||
for (const auto& container_pair : file_containers) {
|
||||
fmt::print(file,
|
||||
FMT_STRING(LOG_DIR_INSERT),
|
||||
container_index,
|
||||
sqlitepp::quote(container_pair.first));
|
||||
for (const auto& file_path_str : container_pair.second) {
|
||||
fmt::print(file,
|
||||
FMT_STRING(":open $LOG_DIR_{}/{}"),
|
||||
container_index,
|
||||
file_path_str);
|
||||
}
|
||||
container_index += 1;
|
||||
}
|
||||
|
||||
fmt::print(file, FMT_STRING("\n:rebuild\n"));
|
||||
|
@ -8,7 +8,9 @@
|
||||
|
||||
# The files loaded into the session were:
|
||||
|
||||
:open {test_dir}/logfile_access_log.0
|
||||
|
||||
;INSERT OR IGNORE INTO environ (name, value) VALUES ('LOG_DIR_0', '{top_srcdir_parent}')
|
||||
:open $LOG_DIR_0/lnav/test/logfile_access_log.0
|
||||
:rebuild
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user