|
| 1 | +/* |
| 2 | + * project_resolve.c — Canonical path identity and duplicate-index prevention. |
| 3 | + */ |
| 4 | +#include "pipeline/project_resolve.h" |
| 5 | +#include "foundation/platform.h" |
| 6 | +#include "foundation/compat_fs.h" |
| 7 | +#include "foundation/str_util.h" |
| 8 | +#include "git/git_context.h" |
| 9 | +#include "store/store.h" |
| 10 | + |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +typedef struct { |
| 16 | + char *identity_key; |
| 17 | + char *project_name; |
| 18 | +} proj_identity_entry_t; |
| 19 | + |
| 20 | +typedef struct { |
| 21 | + bool loaded; |
| 22 | + char cache_dir[1024]; |
| 23 | + proj_identity_entry_t *entries; |
| 24 | + size_t count; |
| 25 | +} proj_identity_cache_t; |
| 26 | + |
| 27 | +static proj_identity_cache_t g_identity_cache; |
| 28 | + |
| 29 | +bool cbm_path_canonicalize(const char *path, char *out, size_t out_sz) { |
| 30 | + if (!path || !out || out_sz == 0) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + out[0] = '\0'; |
| 34 | +#ifdef _WIN32 |
| 35 | + if (!_fullpath(out, path, out_sz)) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + cbm_normalize_path_sep(out); |
| 39 | +#else |
| 40 | + if (!realpath(path, real)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | +#endif |
| 44 | + return out[0] != '\0'; |
| 45 | +} |
| 46 | + |
| 47 | +bool cbm_project_identity_key(const char *repo_path, char *out, size_t out_sz) { |
| 48 | + if (!repo_path || !out || out_sz == 0) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + cbm_git_context_t ctx = {0}; |
| 53 | + if (cbm_git_context_resolve(repo_path, &ctx) == 0 && ctx.is_git && ctx.worktree_root && |
| 54 | + ctx.worktree_root[0]) { |
| 55 | + bool ok = cbm_path_canonicalize(ctx.worktree_root, out, out_sz); |
| 56 | + cbm_git_context_free(&ctx); |
| 57 | + return ok; |
| 58 | + } |
| 59 | + cbm_git_context_free(&ctx); |
| 60 | + return cbm_path_canonicalize(repo_path, out, out_sz); |
| 61 | +} |
| 62 | + |
| 63 | +static bool identity_is_child(const char *child, const char *parent) { |
| 64 | + if (!child[0] || !parent[0]) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + if (strcmp(child, parent) == 0) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + size_t plen = strlen(parent); |
| 71 | + if (strncmp(child, parent, plen) != 0) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + return child[plen] == '/'; |
| 75 | +} |
| 76 | + |
| 77 | +static bool is_project_db_file(const char *name, size_t len) { |
| 78 | + if (len < 5 || strcmp(name + len - 3, ".db") != 0) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + if (name[0] == '_') { |
| 82 | + return false; |
| 83 | + } |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +static void identity_cache_free(void) { |
| 88 | + for (size_t i = 0; i < g_identity_cache.count; i++) { |
| 89 | + free(g_identity_cache.entries[i].identity_key); |
| 90 | + free(g_identity_cache.entries[i].project_name); |
| 91 | + } |
| 92 | + free(g_identity_cache.entries); |
| 93 | + memset(&g_identity_cache, 0, sizeof(g_identity_cache)); |
| 94 | +} |
| 95 | + |
| 96 | +void cbm_project_identity_cache_invalidate(void) { |
| 97 | + identity_cache_free(); |
| 98 | +} |
| 99 | + |
| 100 | +static bool identity_cache_load(void) { |
| 101 | + const char *cache_dir = cbm_resolve_cache_dir(); |
| 102 | + if (g_identity_cache.loaded && |
| 103 | + strcmp(g_identity_cache.cache_dir, cache_dir) == 0) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + identity_cache_free(); |
| 108 | + |
| 109 | + cbm_dir_t *d = cbm_opendir(cache_dir); |
| 110 | + if (!d) { |
| 111 | + snprintf(g_identity_cache.cache_dir, sizeof(g_identity_cache.cache_dir), "%s", cache_dir); |
| 112 | + g_identity_cache.loaded = true; |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + cbm_dirent_t *entry; |
| 117 | + while ((entry = cbm_readdir(d)) != NULL) { |
| 118 | + const char *name = entry->name; |
| 119 | + size_t len = strlen(name); |
| 120 | + if (!is_project_db_file(name, len)) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + char db_path[2048]; |
| 125 | + snprintf(db_path, sizeof(db_path), "%s/%s", cache_dir, name); |
| 126 | + |
| 127 | + cbm_store_t *store = cbm_store_open_path_query(db_path); |
| 128 | + if (!store) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + char project_name[1024]; |
| 133 | + snprintf(project_name, sizeof(project_name), "%.*s", (int)(len - 3), name); |
| 134 | + |
| 135 | + cbm_project_t proj = {0}; |
| 136 | + if (cbm_store_get_project(store, project_name, &proj) != CBM_STORE_OK || !proj.root_path) { |
| 137 | + safe_str_free(&proj.name); |
| 138 | + safe_str_free(&proj.indexed_at); |
| 139 | + safe_str_free(&proj.root_path); |
| 140 | + cbm_store_close(store); |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + char indexed_key[4096]; |
| 145 | + bool has_key = cbm_project_identity_key(proj.root_path, indexed_key, sizeof(indexed_key)); |
| 146 | + |
| 147 | + safe_str_free(&proj.name); |
| 148 | + safe_str_free(&proj.indexed_at); |
| 149 | + safe_str_free(&proj.root_path); |
| 150 | + cbm_store_close(store); |
| 151 | + |
| 152 | + if (!has_key) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + proj_identity_entry_t row = { |
| 157 | + .identity_key = strdup(indexed_key), |
| 158 | + .project_name = strdup(project_name), |
| 159 | + }; |
| 160 | + if (!row.identity_key || !row.project_name) { |
| 161 | + free(row.identity_key); |
| 162 | + free(row.project_name); |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + proj_identity_entry_t *grown = |
| 167 | + realloc(g_identity_cache.entries, |
| 168 | + (g_identity_cache.count + 1) * sizeof(*g_identity_cache.entries)); |
| 169 | + if (!grown) { |
| 170 | + free(row.identity_key); |
| 171 | + free(row.project_name); |
| 172 | + continue; |
| 173 | + } |
| 174 | + g_identity_cache.entries = grown; |
| 175 | + g_identity_cache.entries[g_identity_cache.count++] = row; |
| 176 | + } |
| 177 | + |
| 178 | + cbm_closedir(d); |
| 179 | + snprintf(g_identity_cache.cache_dir, sizeof(g_identity_cache.cache_dir), "%s", cache_dir); |
| 180 | + g_identity_cache.loaded = true; |
| 181 | + return true; |
| 182 | +} |
| 183 | + |
| 184 | +char *cbm_find_existing_project_name(const char *repo_path) { |
| 185 | + if (!repo_path || !repo_path[0]) { |
| 186 | + return NULL; |
| 187 | + } |
| 188 | + |
| 189 | + char query_key[4096]; |
| 190 | + if (!cbm_project_identity_key(repo_path, query_key, sizeof(query_key))) { |
| 191 | + return NULL; |
| 192 | + } |
| 193 | + |
| 194 | + if (!identity_cache_load()) { |
| 195 | + return NULL; |
| 196 | + } |
| 197 | + |
| 198 | + char *best_name = NULL; |
| 199 | + size_t best_root_len = 0; |
| 200 | + |
| 201 | + for (size_t i = 0; i < g_identity_cache.count; i++) { |
| 202 | + const char *indexed_key = g_identity_cache.entries[i].identity_key; |
| 203 | + const char *project_name = g_identity_cache.entries[i].project_name; |
| 204 | + if (!indexed_key || !project_name) { |
| 205 | + continue; |
| 206 | + } |
| 207 | + |
| 208 | + if (!identity_is_child(query_key, indexed_key)) { |
| 209 | + continue; |
| 210 | + } |
| 211 | + |
| 212 | + size_t root_len = strlen(indexed_key); |
| 213 | + if (!best_name || root_len > best_root_len) { |
| 214 | + free(best_name); |
| 215 | + best_name = strdup(project_name); |
| 216 | + best_root_len = root_len; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return best_name; |
| 221 | +} |
0 commit comments