diff hg-web/main.c @ 108:f07abbcd2ec5

[HgWeb] Will probably hold off on using it since it is not urgent.
author June Park <parkjune1995@gmail.com>
date Sat, 03 Jan 2026 17:29:12 -0800
parents 1423044e3d58
children 1c446ab6f945
line wrap: on
line diff
--- a/hg-web/main.c	Sat Jan 03 10:48:11 2026 -0800
+++ b/hg-web/main.c	Sat Jan 03 17:29:12 2026 -0800
@@ -269,6 +269,54 @@
   return ApiGetFile(req, arena);
 }
 
+Seobeo_Request_Entry* ApiHgWireProtocol(Seobeo_Request_Entry *req, Dowa_Arena *arena)
+{
+    Seobeo_Request_Entry *resp = NULL;
+
+    void *cmd_kv = Dowa_HashMap_Get_Ptr(req, "query_cmd");
+    const char *cmd = cmd_kv ? ((Seobeo_Request_Entry*)cmd_kv)->value : "";
+    if (strlen(cmd) == 0)
+    {
+        Dowa_HashMap_Push_Arena(resp, "status", "404", arena);
+        return resp;
+    }
+    Seobeo_Log(SEOBEO_DEBUG, "cmd: %s\n", cmd);
+
+    char command[MAX_PATH];
+    snprintf(command, sizeof(command), "hg -R %s serve --stdio 2>&1", REPO_ROOT);
+    
+    FILE *hg_pipe = popen(command, "r+");
+    if (!hg_pipe) {
+        Seobeo_Log(SEOBEO_DEBUG, "Failed to open pipe\n");
+        return resp;
+    }
+    
+    // 2. Write the command
+    fprintf(hg_pipe, "capabilities\n");
+    fflush(hg_pipe);
+    
+    // 3. Read the response
+    int buffer_size = 1024 * 1024 * 5;
+    char *output = Dowa_Arena_Allocate(arena, buffer_size);
+    if (fgets(output, buffer_size, hg_pipe) != NULL) {
+        Seobeo_Log(SEOBEO_DEBUG, "SUCCESS! Received: %s\n", output);
+    } else {
+        Seobeo_Log(SEOBEO_DEBUG, "FAILURE: No output received from hg.\n");
+    }
+    
+    // 4. Close and check exit code
+    int status = pclose(hg_pipe);
+    Seobeo_Log(SEOBEO_DEBUG, "Process exited with status: %d\n", status);
+
+    Seobeo_Log(SEOBEO_DEBUG, "body: %s\n", output);
+
+    Dowa_HashMap_Push_Arena(resp, "status", "200", arena);
+    Dowa_HashMap_Push_Arena(resp, "content-type", "application/mercurial-0.2", arena);    
+    Dowa_HashMap_Push_Arena(resp, "body", output, arena);
+
+    return resp;
+}
+
 int main(void) {
   Seobeo_Router_Init();
 
@@ -276,6 +324,9 @@
   Seobeo_Router_Register("GET", "/api/repo/file", ApiGetFile);
   Seobeo_Router_Register("GET", "/api/repo/readme", ApiGetReadme);
 
+  Seobeo_Router_Register("GET", "/repo", ApiHgWireProtocol);
+  Seobeo_Router_Register("POST", "/repo", ApiHgWireProtocol);
+
   printf("Starting on Port 6970...\n");
   printf("Repository: %s\n", REPO_ROOT);