git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC WIP PATCH 1/3] tag: factor out tag reading from write_tag_body()

From
Lucas Oshiro <lucasseikioshiro@gmail.com>
Date
Oct 8, 2019, 18:47 UTC
Message-ID
<20191008184727.14337-2-lucasseikioshiro@gmail.com>
In-Reply-To
<20191008184727.14337-1-lucasseikioshiro@gmail.com>

Improve code readability by moving tag reading to a new function called get_tag_body, which will be used in the implementation of the git tag --amend functionality.

Add warning in write_tag_body() in case the tag body is not successfully recovered.

Co-authored-by: Bárbara Fernandes <barbara.dcf@gmail.com>
Signed-off-by: Bárbara Fernandes <barbara.dcf@gmail.com>
Signed-off-by: Lucas Oshiro <lucasseikioshiro@gmail.com>
Helped-by: Matheus Tavares <matheus.bernardino@usp.br>
---
 builtin/tag.c | 42 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)
diff --git a/builtin/tag.c b/builtin/tag.c
index e0a4c25382..e1e3549af9 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -170,26 +170,52 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	return git_color_default_config(var, value, cb);
 }
 
-static void write_tag_body(int fd, const struct object_id *oid)
+/* 
+ * Returns the tag body of the given oid or NULL, in case of error. If size is
+ * not NULL it is assigned the body size in bytes (excluding the '\0').
+ */
+static char *get_tag_body(const struct object_id *oid, size_t *size) 
 {
-	unsigned long size;
+	unsigned long buf_size;
 	enum object_type type;
-	char *buf, *sp;
+	char *buf, *sp, *tag_body;
+	size_t tag_body_size, signature_offset;
 
-	buf = read_object_file(oid, &type, &size);
+	buf = read_object_file(oid, &type, &buf_size);
 	if (!buf)
-		return;
+		return NULL;
 	/* skip header */
 	sp = strstr(buf, "\n\n");
 
-	if (!sp || !size || type != OBJ_TAG) {
+	if (!sp || !buf_size || type != OBJ_TAG) {
 		free(buf);
-		return;
+		return NULL;
 	}
 	sp += 2; /* skip the 2 LFs */
-	write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
+	signature_offset = parse_signature(sp, buf + buf_size - sp);
+	sp[signature_offset] = '\0';
 
+	/* detach sp from buf */
+	tag_body_size = strlen(sp) + 1;
+	tag_body = xmalloc(tag_body_size);
+	xsnprintf(tag_body, tag_body_size, "%s", sp);
 	free(buf);
+	if (size)
+		*size = tag_body_size - 1; /* exclude '\0' */
+	return tag_body;
+}
+
+static void write_tag_body(int fd, const struct object_id *oid)
+{
+	size_t size;
+	const char *tag_body = get_tag_body(oid, &size);
+
+	if (!tag_body) {
+		warning("failed to get tag body for %s", oid->hash);
+		return;
+	}
+	printf("tag_body: <%s>\n", tag_body);
+	write_or_die(fd, tag_body, size);
 }
 
 static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
-- 
2.23.0
Previous: Lucas OshiroNext: Matheus Tavares Bernardino
Message 2 of 12 in “tag: fix --edit and --no-edit flags”
  1. 0/3 tag: fix --edit and --no-edit flagsLucas Oshiro, Oct 8, 2019
  2. 1/3 tag: factor out tag reading from write_tag_body()Lucas Oshiro, Oct 8, 2019
  3. Matheus Tavares BernardinoOct 9, 2019
  4. Junio C HamanoOct 10, 2019
  5. 2/3 tag: factor out prepare tag template codeLucas Oshiro, Oct 8, 2019
  6. Matheus Tavares BernardinoOct 9, 2019
  7. Junio C HamanoOct 10, 2019
  8. Junio C HamanoOct 10, 2019
  9. 3/3 tag: add full support for --edit and --no-editLucas Oshiro, Oct 8, 2019
  10. Matheus Tavares BernardinoOct 9, 2019
  11. Junio C HamanoOct 10, 2019
  12. Junio C HamanoOct 10, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.