-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathalgo_loc.rs
More file actions
201 lines (188 loc) · 11.5 KB
/
Copy pathalgo_loc.rs
File metadata and controls
201 lines (188 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::git_command_algo::get_latest_commit;
use crate::git_command_algo::index_some_commits;
use crate::{
config, contextgpt_structs::AuthorDetailsV2, git_command_algo::extract_details_parallel,
};
use std::collections::HashMap;
use std::path::PathBuf;
pub async fn perform_for_whole_file(
origin_file_path: String,
should_print: bool,
commits_to_index: Option<Vec<String>>,
workspace_path: Option<String>,
) -> HashMap<u32, AuthorDetailsV2> {
if let Some(workspace) = workspace_path.as_ref() {
match get_latest_commit(&origin_file_path) {
Some(recent_commit) => {
if let Some(home) = simple_home_dir::home_dir() {
let workspace_path_buf = PathBuf::from(workspace);
let workspace_name = workspace_path_buf
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("default_workspace");
println!(
"DEBUG: Workspace name: {}",
workspace_path_buf.to_string_lossy()
);
let db_folder = format!(
"{}{}{}{}{}",
home.to_string_lossy(),
std::path::MAIN_SEPARATOR,
config::DB_FOLDER,
std::path::MAIN_SEPARATOR,
workspace_path_buf.to_string_lossy().to_string()
);
if let path_str = db_folder.to_string() {
// let indexing_path = db_folder.join(path_str).join("indexing_metadata.json");
let indexing_path = format!(
"{}{}indexing_metadata.json",
path_str,
std::path::MAIN_SEPARATOR
);
if PathBuf::from(indexing_path.clone()).exists() {
match std::fs::read_to_string(&indexing_path) {
Ok(metadata_str) => {
match serde_json::from_str::<HashMap<String, Vec<String>>>(
&metadata_str,
) {
Ok(indexing_metadata) => {
// Normalize paths for proper comparison
let origin_path_buf = PathBuf::from(&origin_file_path);
let workspace_path_buf = PathBuf::from(workspace);
// Try to canonicalize both paths for accurate comparison
let canonical_origin = origin_path_buf
.canonicalize()
.unwrap_or(origin_path_buf.clone());
let canonical_workspace = workspace_path_buf
.canonicalize()
.unwrap_or(workspace_path_buf.clone());
let relative_path = if let Ok(rel) =
canonical_origin.strip_prefix(&canonical_workspace)
{
rel.to_string_lossy().replace('\\', "/")
} else {
// Fallback: try without canonicalization
if let Ok(rel) = origin_path_buf
.strip_prefix(&workspace_path_buf)
{
rel.to_string_lossy().replace('\\', "/")
} else {
// Last resort: use filename
origin_path_buf
.file_name()
.map(|name| {
name.to_string_lossy().to_string()
})
.unwrap_or_else(|| origin_file_path.clone())
}
};
// Check if file is already indexed with latest commit
// Try different path variations to find existing metadata
let possible_paths = vec![
origin_file_path.clone(),
if let Ok(canonical) =
PathBuf::from(&origin_file_path).canonicalize()
{
canonical.to_string_lossy().to_string()
} else {
origin_file_path.clone()
},
relative_path.clone(),
];
println!(
"DEBUG: Checking if file is already indexed with latest commit: {}",
recent_commit
);
println!("DEBUG: Possible paths to check:");
for (i, path) in possible_paths.iter().enumerate() {
println!("DEBUG: {}: {}", i, path);
}
for path_variant in possible_paths {
println!(
"DEBUG: Checking path variant: {}",
path_variant
);
if let Some(last_indexing_data) =
indexing_metadata.get(&path_variant)
{
println!(
"DEBUG: Found indexing data for path: {}",
path_variant
);
println!(
"DEBUG: Last indexing data: {:?}",
last_indexing_data
);
if let Some(last_indexed_commit) =
last_indexing_data.last()
{
println!(
"DEBUG: Last indexed commit: {}",
last_indexed_commit
);
if last_indexed_commit == &recent_commit {
println!(
"DEBUG: Commit matches! No reindexing needed."
);
if should_print {
println!(
"File {} is already indexed with the latest commit {}",
path_variant, recent_commit
);
}
return HashMap::new();
} else {
println!(
"DEBUG: Commit doesn't match. Reindexing needed."
);
if should_print {
println!(
"File {} has a new commit {} (last indexed: {})",
path_variant,
recent_commit,
last_indexed_commit
);
}
}
} else {
println!(
"DEBUG: No last indexed commit found."
);
}
} else {
println!(
"DEBUG: No indexing data found for path: {}",
path_variant
);
}
}
println!(
"DEBUG: No matching indexed commit found. Proceeding with indexing."
);
}
Err(e) => {
eprintln!("Failed to parse metadata JSON check: {}", e)
}
}
}
Err(e) => eprintln!("Failed to read metadata file: {}", e),
}
}
}
}
}
None => {}
}
}
// Proceed with indexing if needed
let output: HashMap<u32, AuthorDetailsV2>;
if should_print {
println!("Indexing file: {}", origin_file_path);
}
if commits_to_index.is_none() {
output = extract_details_parallel(origin_file_path.clone()).await;
} else {
output = index_some_commits(origin_file_path.clone(), commits_to_index.unwrap()).await;
}
output
}