#if PLATFORM_WINDOWS #include #endif #ifndef S_ISDIR #define S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR) #endif //- source links: node.link -> a repo url (browser) or a local file + line (editor) // NOTE: a link is "path/in/repo.c:42", "path/in/repo.c#L42", "path/in/repo.c" or a full url. paths are // relative to the repo root. the diagram says where the repo lives online (repo + ref) and, when it // isn't the git checkout around the diagram file, where it lives on disk (root). internal b32 link_is_url(const char *link) { return !strncmp(link, "http://", 7) || !strncmp(link, "https://", 8) || !strncmp(link, "file://", 7); } internal b32 path_is_absolute(const char *path) { if(path[0] == '/' || path[0] == '\\') return true; return ((path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z')) && path[1] == ':' && (path[2] == '\\' || path[2] == '/'); } // NOTE: splits "file.c:42", "file.c:42-50", "file.c#L42" into path + first line (0 = none) internal void link_split(const char *link, char *path, u32 path_cap, u32 *line) { *line = 0; snprintf(path, path_cap, "%s", link); u32 len = (u32)strlen(path); char *hash = strstr(path, "#L"); if(hash) { *line = (u32)atoi(hash + 2); *hash = 0; return; } // last ':' followed only by digits (and an optional -digits range); "C:\x" has no digits after it for(s32 i = (s32)len - 1; i > 0; --i) { if(path[i] != ':') continue; const char *n = path + i + 1; b32 digits = (*n >= '0' && *n <= '9'); for(const char *c = n; *c && digits; ++c) if(!((*c >= '0' && *c <= '9') || *c == '-')) digits = false; if(digits) { *line = (u32)atoi(n); path[i] = 0; } break; } } // NOTE: git@github.com:user/repo.git -> https://github.com/user/repo, ssh://git@host/x -> https://host/x internal void repo_normalize_url(const char *in, char *out, u32 cap) { char tmp[512]; snprintf(tmp, sizeof(tmp), "%s", in); u32 len = (u32)strlen(tmp); while(len > 0 && (tmp[len - 1] == '/' || tmp[len - 1] == '\n' || tmp[len - 1] == '\r' || tmp[len - 1] == ' ')) tmp[--len] = 0; if(len > 4 && !strcmp(tmp + len - 4, ".git")) tmp[len -= 4] = 0; if(!strncmp(tmp, "git@", 4)) { char *colon = strchr(tmp + 4, ':'); if(colon) { *colon = 0; snprintf(out, cap, "https://%s/%s", tmp + 4, colon + 1); return; } } if(!strncmp(tmp, "ssh://", 6)) { const char *host = tmp + 6; const char *at = strchr(host, '@'); if(at) host = at + 1; snprintf(out, cap, "https://%s", host); return; } if(!strncmp(tmp, "git://", 6)) { snprintf(out, cap, "https://%s", tmp + 6); return; } snprintf(out, cap, "%s", tmp); } internal void str_replace_all(char *out, u32 cap, const char *in, const char *what, const char *with) { u32 n = 0, wl = (u32)strlen(what); for(const char *at = in; *at && n + 1 < cap;) { if(!strncmp(at, what, wl)) { for(const char *w = with; *w && n + 1 < cap; ++w) out[n++] = *w; at += wl; } else { out[n++] = *at++; } } out[n] = 0; } // NOTE: the browser url for a link. false when there is nothing to open online internal b32 diagram_link_url(Diagram *d, const char *link, char *out, u32 cap) { out[0] = 0; if(!link || !link[0]) return false; if(link_is_url(link)) { snprintf(out, cap, "%s", link); return true; } if(!d->repo[0] || path_is_absolute(link)) return false; char path[DIAGRAM_LINK_SIZE]; u32 line = 0; link_split(link, path, sizeof(path), &line); const char *rel = path; while(rel[0] == '.' && rel[1] == '/') rel += 2; while(rel[0] == '/') ++rel; const char *ref = d->repo_ref[0] ? d->repo_ref : "HEAD"; char line_s[16]; snprintf(line_s, sizeof(line_s), "%u", line); // a template: "https://cgit.example.org/repo/tree/{path}?h={ref}#n{line}" if(strstr(d->repo, "{path}")) { char a[1024], b[1024]; str_replace_all(a, sizeof(a), d->repo, "{path}", rel); str_replace_all(b, sizeof(b), a, "{ref}", ref); str_replace_all(out, cap, b, "{line}", line ? line_s : ""); return true; } char base[512]; repo_normalize_url(d->repo, base, sizeof(base)); if(strstr(base, "bitbucket.org")) { if(line) snprintf(out, cap, "%s/src/%s/%s#lines-%u", base, ref, rel, line); else snprintf(out, cap, "%s/src/%s/%s", base, ref, rel); } else if(strstr(base, "gitlab")) { if(line) snprintf(out, cap, "%s/-/blob/%s/%s#L%u", base, ref, rel, line); else snprintf(out, cap, "%s/-/blob/%s/%s", base, ref, rel); } else { // github, gitea, forgejo, codeberg: /blob//#L if(line) snprintf(out, cap, "%s/blob/%s/%s#L%u", base, ref, rel, line); else snprintf(out, cap, "%s/blob/%s/%s", base, ref, rel); } return true; } internal b32 file_exists(const char *path) { struct stat st; return stat(path, &st) == 0; } internal void path_dirname(const char *path, char *out, u32 cap) { snprintf(out, cap, "%s", path); char *slash = strrchr(out, '/'); char *back = strrchr(out, '\\'); if(back && (!slash || back > slash)) slash = back; if(slash) *slash = 0; else snprintf(out, cap, "."); } // NOTE: walks up from dir to the first folder holding .git (a dir, or a file for worktrees) internal b32 repo_find_toplevel(const char *dir, char *out, u32 cap) { char cur[1024]; snprintf(cur, sizeof(cur), "%s", dir && dir[0] ? dir : "."); if(!path_is_absolute(cur)) { char cwd[1024]; if(getcwd(cwd, sizeof(cwd))) { char joined[1024]; snprintf(joined, sizeof(joined), "%s/%s", cwd, cur); snprintf(cur, sizeof(cur), "%s", joined); } } for(u32 guard = 0; guard < 64; ++guard) { char probe[1100]; snprintf(probe, sizeof(probe), "%s/.git", cur); if(file_exists(probe)) { snprintf(out, cap, "%s", cur); return true; } char *slash = strrchr(cur, '/'); char *back = strrchr(cur, '\\'); if(back && (!slash || back > slash)) slash = back; if(!slash || slash == cur) break; *slash = 0; } return false; } // NOTE: where links resolve on disk: root= (absolute, or relative to the diagram file), else the git // checkout around the diagram file (or the working directory), else that directory itself internal void diagram_link_root(Diagram *d, char *out, u32 cap) { char base[1024]; if(d->file_path[0]) path_dirname(d->file_path, base, sizeof(base)); else if(!getcwd(base, sizeof(base))) snprintf(base, sizeof(base), "."); if(d->repo_root[0]) { if(path_is_absolute(d->repo_root)) snprintf(out, cap, "%s", d->repo_root); else snprintf(out, cap, "%s/%s", base, d->repo_root); return; } if(repo_find_toplevel(base, out, cap)) return; snprintf(out, cap, "%s", base); } // NOTE: the local file of a link. false for urls internal b32 diagram_link_local(Diagram *d, const char *link, char *out, u32 cap, u32 *line) { out[0] = 0; *line = 0; if(!link || !link[0]) return false; if(!strncmp(link, "file://", 7)) { link_split(link + 7, out, cap, line); return true; } if(link_is_url(link)) return false; char path[DIAGRAM_LINK_SIZE]; link_split(link, path, sizeof(path), line); if(path_is_absolute(path)) { snprintf(out, cap, "%s", path); return true; } char root[1024]; diagram_link_root(d, root, sizeof(root)); const char *rel = path; while(rel[0] == '.' && rel[1] == '/') rel += 2; snprintf(out, cap, "%s/%s", root, rel); return true; } // NOTE: reads the origin remote and the checked out branch of the repo around dir. fills repo / // repo_ref when they are empty. returns the toplevel (out) when there is a repo at all internal b32 diagram_detect_repo(Diagram *d, const char *dir, char *toplevel, u32 cap) { if(!repo_find_toplevel(dir, toplevel, cap)) return false; ScratchArena scratch = ScratchGet(); char git[1200]; snprintf(git, sizeof(git), "%s/.git", toplevel); struct stat st; if(stat(git, &st) == 0 && !S_ISDIR(st.st_mode)) { // worktree: ".git" is a file "gitdir: ", the config lives in the main repo String8 link = FileLoad(scratch.arena, str8(git)); if(link.size > 8 && !strncmp((char *)link.data, "gitdir:", 7)) { char gd[1024]; u64 n = Min(link.size - 7, (u64)sizeof(gd) - 1); MemCpy(gd, link.data + 7, n); gd[n] = 0; const char *g = dsl_skip_ws(gd); char clean[1024]; snprintf(clean, sizeof(clean), "%s", g); u32 cl = (u32)strlen(clean); while(cl > 0 && (clean[cl - 1] == '\n' || clean[cl - 1] == '\r' || clean[cl - 1] == ' ')) clean[--cl] = 0; char *wt = strstr(clean, "/worktrees/"); if(wt) *wt = 0; if(path_is_absolute(clean)) snprintf(git, sizeof(git), "%s", clean); else { char joined[1200]; snprintf(joined, sizeof(joined), "%s/%s", toplevel, clean); snprintf(git, sizeof(git), "%s", joined); } } } if(!d->repo[0]) { char config_path[1300]; snprintf(config_path, sizeof(config_path), "%s/config", git); String8 config = FileLoad(scratch.arena, str8(config_path)); // [remote "origin"] ... url = , else the first remote const char *text = (const char *)config.data; const char *best = NULL; b32 in_remote = false, in_origin = false; for(u64 i = 0; i < config.size;) { u64 e = i; while(e < config.size && text[e] != '\n') ++e; char line[600]; u64 n = Min(e - i, (u64)sizeof(line) - 1); MemCpy(line, text + i, n); line[n] = 0; const char *p = dsl_skip_ws(line); if(*p == '[') { in_remote = !strncmp(p, "[remote ", 8); in_origin = in_remote && strstr(p, "\"origin\"") != NULL; } else if(in_remote && !strncmp(p, "url", 3)) { const char *eq = strchr(p, '='); if(eq && (in_origin || !best)) { static char found[600]; snprintf(found, sizeof(found), "%s", dsl_skip_ws(eq + 1)); best = found; if(in_origin) break; } } i = e + 1; } if(best) repo_normalize_url(best, d->repo, sizeof(d->repo)); } if(!d->repo_ref[0]) { char head_path[1300]; snprintf(head_path, sizeof(head_path), "%s/HEAD", git); String8 head = FileLoad(scratch.arena, str8(head_path)); if(head.size > 16 && !strncmp((char *)head.data, "ref: refs/heads/", 16)) { u64 n = Min(head.size - 16, (u64)sizeof(d->repo_ref) - 1); MemCpy(d->repo_ref, head.data + 16, n); d->repo_ref[n] = 0; u32 rl = (u32)strlen(d->repo_ref); while(rl > 0 && (d->repo_ref[rl - 1] == '\n' || d->repo_ref[rl - 1] == '\r')) d->repo_ref[--rl] = 0; } } scratch_end(&scratch); return true; } internal void path_make_absolute(const char *path, char *out, u32 cap) { if(path_is_absolute(path)) { snprintf(out, cap, "%s", path); } else { char cwd[1024]; if(!getcwd(cwd, sizeof(cwd))) cwd[0] = 0; const char *rel = path; while(rel[0] == '.' && (rel[1] == '/' || rel[1] == '\\')) rel += 2; if(!strcmp(rel, ".")) rel = ""; snprintf(out, cap, "%s%s%s", cwd, rel[0] ? "/" : "", rel); } u32 n = (u32)strlen(out); while(n > 1 && (out[n - 1] == '/' || out[n - 1] == '\\')) out[--n] = 0; #if PLATFORM_WINDOWS for(char *c = out; *c; ++c) if(*c == '\\') *c = '/'; #endif } // NOTE: path relative to root when it is inside it ("/r/src/a.h" in "/r" -> "src/a.h") internal void path_relative_to(const char *path, const char *root, char *out, u32 cap) { u64 rl = strlen(root); if(rl && !strncmp(path, root, rl) && (path[rl] == '/' || path[rl] == '\\')) snprintf(out, cap, "%s", path + rl + 1); else snprintf(out, cap, "%s", path); }