From a03cf96d53a1b2a80f6a4b43381d62d2b0b38208 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 05 Apr 2025 11:27:56 +0800 Subject: [PATCH] git2d: Factor commands out into their own files --- git2d/cmd1.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ git2d/session.c | 101 ++--------------------------------------------------- git2d/x.h | 2 ++ main.go | 3 +++ diff --git a/git2d/cmd1.c b/git2d/cmd1.c new file mode 100644 index 0000000000000000000000000000000000000000..44f59db4134faed83a3f327e2d109697b8428f9b --- /dev/null +++ b/git2d/cmd1.c @@ -0,0 +1,117 @@ +#include "x.h" + +int +cmd1(git_repository *repo, struct bare_writer *writer) +{ + /* HEAD tree */ + git_object *obj = NULL; + int err = git_revparse_single(&obj, repo, "HEAD^{tree}"); + if (err != 0) { + bare_put_uint(writer, 4); + return -1; + } + git_tree *tree = (git_tree *) obj; + + /* README */ + + git_tree_entry *entry = NULL; + err = git_tree_entry_bypath(&entry, tree, "README.md"); + if (err != 0) { + bare_put_uint(writer, 5); + git_tree_free(tree); + return -1; + } + git_otype objtype = git_tree_entry_type(entry); + if (objtype != GIT_OBJECT_BLOB) { + bare_put_uint(writer, 6); + git_tree_entry_free(entry); + git_tree_free(tree); + return -1; + } + git_object *obj2 = NULL; + err = git_tree_entry_to_object(&obj2, repo, entry); + if (err != 0) { + bare_put_uint(writer, 7); + git_tree_entry_free(entry); + git_tree_free(tree); + return -1; + } + git_blob *blob = (git_blob *) obj2; + const void *content = git_blob_rawcontent(blob); + if (content == NULL) { + bare_put_uint(writer, 8); + git_blob_free(blob); + git_tree_entry_free(entry); + git_tree_free(tree); + return -1; + } + bare_put_uint(writer, 0); + bare_put_data(writer, content, git_blob_rawsize(blob)); + + /* Commits */ + + git_revwalk *walker = NULL; + if (git_revwalk_new(&walker, repo) != 0) { + bare_put_uint(writer, 9); + git_blob_free(blob); + git_tree_entry_free(entry); + git_tree_free(tree); + return -1; + } + + if (git_revwalk_push_head(walker) != 0) { + bare_put_uint(writer, 9); + git_revwalk_free(walker); + git_blob_free(blob); + git_tree_entry_free(entry); + git_tree_free(tree); + return -1; + } + + int count = 0; + git_oid oid; + while (count < 3 && git_revwalk_next(&oid, walker) == 0) { + git_commit *commit = NULL; + if (git_commit_lookup(&commit, repo, &oid) != 0) + break; + + const char *msg = git_commit_summary(commit); + const git_signature *author = git_commit_author(commit); + + /* ID */ + bare_put_data(writer, oid.id, GIT_OID_RAWSZ); + + /* Title */ + size_t msg_len = msg ? strlen(msg) : 0; + bare_put_data(writer, (const uint8_t *)(msg ? msg : ""), msg_len); + + /* Author's name */ + const char *author_name = author ? author->name : ""; + bare_put_data(writer, (const uint8_t *)author_name, strlen(author_name)); + + /* Author's email */ + const char *author_email = author ? author->email : ""; + bare_put_data(writer, (const uint8_t *)author_email, strlen(author_email)); + + /* Author's date */ + /* TODO: Pass the integer instead of a string */ + time_t time = git_commit_time(commit); + char timebuf[64]; + struct tm *tm = localtime(&time); + if (tm) + strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm); + else + strcpy(timebuf, "unknown"); + bare_put_data(writer, (const uint8_t *)timebuf, strlen(timebuf)); + + git_commit_free(commit); + count++; + } + + git_revwalk_free(walker); + git_blob_free(blob); + git_tree_entry_free(entry); + git_tree_free(tree); + + return 0; +} diff --git a/git2d/session.c b/git2d/session.c index aadab2a910ab30977f46a2ce86b0e402fed8f520..f7ad8439a962a2f95c76b273001952fd11981570 100644 --- a/git2d/session.c +++ b/git2d/session.c @@ -48,6 +48,9 @@ goto free_repo; } switch (cmd) { case 1: + err = cmd1(repo, &writer); + if (err != 0) + goto free_repo; break; case 0: bare_put_uint(&writer, 3); @@ -57,104 +60,6 @@ bare_put_uint(&writer, 3); goto free_repo; } - /* HEAD tree */ - git_object *obj = NULL; - err = git_revparse_single(&obj, repo, "HEAD^{tree}"); - if (err != 0) { - bare_put_uint(&writer, 4); - goto free_repo; - } - git_tree *tree = (git_tree *) obj; - - /* README */ - - git_tree_entry *entry = NULL; - err = git_tree_entry_bypath(&entry, tree, "README.md"); - if (err != 0) { - bare_put_uint(&writer, 5); - goto free_tree; - } - git_otype objtype = git_tree_entry_type(entry); - if (objtype != GIT_OBJECT_BLOB) { - bare_put_uint(&writer, 6); - goto free_tree_entry; - } - git_object *obj2 = NULL; - err = git_tree_entry_to_object(&obj2, repo, entry); - if (err != 0) { - bare_put_uint(&writer, 7); - goto free_tree_entry; - } - git_blob *blob = (git_blob *) obj2; - const void *content = git_blob_rawcontent(blob); - if (content == NULL) { - bare_put_uint(&writer, 8); - goto free_blob; - } - bare_put_uint(&writer, 0); - bare_put_data(&writer, content, git_blob_rawsize(blob)); - - /* Commits */ - - git_revwalk *walker = NULL; - if (git_revwalk_new(&walker, repo) != 0) { - bare_put_uint(&writer, 9); - goto free_blob; - } - - if (git_revwalk_push_head(walker) != 0) { - bare_put_uint(&writer, 9); - goto free_blob; - } - - int count = 0; - git_oid oid; - while (count < 3 && git_revwalk_next(&oid, walker) == 0) { - git_commit *commit = NULL; - if (git_commit_lookup(&commit, repo, &oid) != 0) - break; - - const char *msg = git_commit_summary(commit); - const git_signature *author = git_commit_author(commit); - - /* ID */ - bare_put_data(&writer, oid.id, GIT_OID_RAWSZ); - - /* Title */ - size_t msg_len = msg ? strlen(msg) : 0; - bare_put_data(&writer, (const uint8_t *)(msg ? msg : ""), msg_len); - - /* Author's name */ - const char *author_name = author ? author->name : ""; - bare_put_data(&writer, (const uint8_t *)author_name, strlen(author_name)); - - /* Author's email */ - const char *author_email = author ? author->email : ""; - bare_put_data(&writer, (const uint8_t *)author_email, strlen(author_email)); - - /* Author's date */ - /* TODO: Pass the integer instead of a string */ - time_t time = git_commit_time(commit); - char timebuf[64]; - struct tm *tm = localtime(&time); - if (tm) - strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm); - else - strcpy(timebuf, "unknown"); - bare_put_data(&writer, (const uint8_t *)timebuf, strlen(timebuf)); - - git_commit_free(commit); - count++; - } - - git_revwalk_free(walker); - -free_blob: - git_blob_free(blob); -free_tree_entry: - git_tree_entry_free(entry); -free_tree: - git_tree_free(tree); free_repo: git_repository_free(repo); diff --git a/git2d/x.h b/git2d/x.h index 92f741444b9246c6002d87eb8459a414a0e648a9..f852b2369be0313ae94700cc8059262b76318391 100644 --- a/git2d/x.h +++ b/git2d/x.h @@ -32,4 +32,6 @@ bare_error conn_write(void *buffer, const void *src, uint64_t sz); void * session(void *_conn); +int cmd1(git_repository *repo, struct bare_writer *writer); + #endif // X_H diff --git a/main.go b/main.go index 84458cbcb4ef27a540fee2b4e68ec6fe561a1e1a..49164d2786e19834e4610f594ec5babb6c759c6b 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "errors" "flag" + "log" "net" "net/http" "os/exec" @@ -39,6 +40,8 @@ // Launch Git2D go func() { cmd := exec.Command(config.Git.DaemonPath, config.Git.Socket) //#nosec G204 + cmd.Stderr = log.Writer() + cmd.Stdout = log.Writer() if err := cmd.Run(); err != nil { panic(err) } -- 2.48.1