{"thread":{"id":"4257","subject":"[PATCH 5/8] Builtin git-commit-tree.","startedAt":"2006-05-23T08:31:17Z","lastAt":"2006-05-23T08:31:17Z","messageCount":1,"participants":["Peter Eriksen"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"20524","messageId":"34511.6158518524$1148373111@news.gmane.org","threadId":"4257","inReplyTo":"11483730804133-git-send-email-","subject":"[PATCH 5/8] Builtin git-commit-tree.","fromName":"Peter Eriksen","fromEmail":"s022018@student.dtu.dk","sentAt":"2006-05-23T08:31:17Z","receivedAt":"2006-05-23T08:31:17Z","isPatch":true,"sender":{"key":"s022018@student.dtu.dk","avatar":null},"body":"From: Peter Eriksen <s022018@student.dtu.dk>\n\nSigned-off-by: Peter Eriksen <s022018@student.dtu.dk>\n\n\n---\n\n762e5a2efcde796d84c573fe8bf3224c9fbf3588\n Makefile              |    6 +-\n builtin-commit-tree.c |  140 +++++++++++++++++++++++++++++++++++++++++++++++++\n builtin.h             |    1 \n commit-tree.c         |  139 -------------------------------------------------\n git.c                 |    3 +\n 5 files changed, 146 insertions(+), 143 deletions(-)\n create mode 100644 builtin-commit-tree.c\n delete mode 100644 commit-tree.c\n\n762e5a2efcde796d84c573fe8bf3224c9fbf3588\ndiff --git a/Makefile b/Makefile\nindex 667fa5d..a5efbc7 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -150,7 +150,7 @@ SIMPLE_PROGRAMS = \\\n # ... and all the rest that could be moved out of bindir to gitexecdir\n PROGRAMS = \\\n \tgit-apply$X git-cat-file$X \\\n-\tgit-checkout-index$X git-clone-pack$X git-commit-tree$X \\\n+\tgit-checkout-index$X git-clone-pack$X \\\n \tgit-convert-objects$X git-diff-files$X \\\n \tgit-diff-index$X git-diff-stages$X \\\n \tgit-diff-tree$X git-fetch-pack$X git-fsck-objects$X \\\n@@ -172,7 +172,7 @@ BUILT_INS = git-log$X git-whatchanged$X \n \tgit-count-objects$X git-diff$X git-push$X \\\n \tgit-grep$X git-rev-list$X git-check-ref-format$X \\\n \tgit-init-db$X git-ls-files$X git-ls-tree$X \\\n-\tgit-tar-tree$X git-read-tree$X\n+\tgit-tar-tree$X git-read-tree$X git-commit-tree$X\n \n # what 'all' will build and 'install' will install, in gitexecdir\n ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)\n@@ -222,7 +222,7 @@ BUILTIN_OBJS = \\\n \tbuiltin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \\\n \tbuiltin-grep.o builtin-rev-list.o builtin-check-ref-format.o \\\n \tbuiltin-init-db.o builtin-ls-files.o builtin-ls-tree.o \\\n-        builtin-tar-tree.o builtin-read-tree.o\n+        builtin-tar-tree.o builtin-read-tree.o builtin-commit-tree.o\n \n GITLIBS = $(LIB_FILE) $(XDIFF_LIB)\n LIBS = $(GITLIBS) -lz\ndiff --git a/builtin-commit-tree.c b/builtin-commit-tree.c\nnew file mode 100644\nindex 0000000..4ccdbec\n--- /dev/null\n+++ b/builtin-commit-tree.c\n@@ -0,0 +1,140 @@\n+/*\n+ * GIT - The information manager from hell\n+ *\n+ * Copyright (C) Linus Torvalds, 2005\n+ */\n+#include \"cache.h\"\n+#include \"commit.h\"\n+#include \"tree.h\"\n+#include \"builtin.h\"\n+\n+#define BLOCKING (1ul << 14)\n+\n+/*\n+ * FIXME! Share the code with \"write-tree.c\"\n+ */\n+static void init_buffer(char **bufp, unsigned int *sizep)\n+{\n+\tchar *buf = xmalloc(BLOCKING);\n+\t*sizep = 0;\n+\t*bufp = buf;\n+}\n+\n+static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)\n+{\n+\tchar one_line[2048];\n+\tva_list args;\n+\tint len;\n+\tunsigned long alloc, size, newsize;\n+\tchar *buf;\n+\n+\tva_start(args, fmt);\n+\tlen = vsnprintf(one_line, sizeof(one_line), fmt, args);\n+\tva_end(args);\n+\tsize = *sizep;\n+\tnewsize = size + len;\n+\talloc = (size + 32767) & ~32767;\n+\tbuf = *bufp;\n+\tif (newsize > alloc) {\n+\t\talloc = (newsize + 32767) & ~32767;\n+\t\tbuf = xrealloc(buf, alloc);\n+\t\t*bufp = buf;\n+\t}\n+\t*sizep = newsize;\n+\tmemcpy(buf + size, one_line, len);\n+}\n+\n+static void check_valid(unsigned char *sha1, const char *expect)\n+{\n+\tchar type[20];\n+\n+\tif (sha1_object_info(sha1, type, NULL))\n+\t\tdie(\"%s is not a valid object\", sha1_to_hex(sha1));\n+\tif (expect && strcmp(type, expect))\n+\t\tdie(\"%s is not a valid '%s' object\", sha1_to_hex(sha1),\n+\t\t    expect);\n+}\n+\n+/*\n+ * Having more than two parents is not strange at all, and this is\n+ * how multi-way merges are represented.\n+ */\n+#define MAXPARENT (16)\n+static unsigned char parent_sha1[MAXPARENT][20];\n+\n+static const char commit_tree_usage[] = \"git-commit-tree <sha1> [-p <sha1>]* < changelog\";\n+\n+static int new_parent(int idx)\n+{\n+\tint i;\n+\tunsigned char *sha1 = parent_sha1[idx];\n+\tfor (i = 0; i < idx; i++) {\n+\t\tif (!memcmp(parent_sha1[i], sha1, 20)) {\n+\t\t\terror(\"duplicate parent %s ignored\", sha1_to_hex(sha1));\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n+\treturn 1;\n+}\n+\n+int cmd_commit_tree(int argc, const char **argv, char **envp)\n+{\n+\tint i;\n+\tint parents = 0;\n+\tunsigned char tree_sha1[20];\n+\tunsigned char commit_sha1[20];\n+\tchar comment[1000];\n+\tchar *buffer;\n+\tunsigned int size;\n+\n+\tsetup_ident();\n+\tsetup_git_directory();\n+\n+\tgit_config(git_default_config);\n+\n+\tif (argc < 2)\n+\t\tusage(commit_tree_usage);\n+\tif (get_sha1(argv[1], tree_sha1))\n+\t\tdie(\"Not a valid object name %s\", argv[1]);\n+\n+\tcheck_valid(tree_sha1, tree_type);\n+\tfor (i = 2; i < argc; i += 2) {\n+\t\tchar *a, *b;\n+\t\ta = argv[i]; b = argv[i+1];\n+\t\tif (!b || strcmp(a, \"-p\"))\n+\t\t\tusage(commit_tree_usage);\n+\t\tif (get_sha1(b, parent_sha1[parents]))\n+\t\t\tdie(\"Not a valid object name %s\", b);\n+\t\tcheck_valid(parent_sha1[parents], commit_type);\n+\t\tif (new_parent(parents))\n+\t\t\tparents++;\n+\t}\n+\tif (!parents)\n+\t\tfprintf(stderr, \"Committing initial tree %s\\n\", argv[1]);\n+\n+\tinit_buffer(&buffer, &size);\n+\tadd_buffer(&buffer, &size, \"tree %s\\n\", sha1_to_hex(tree_sha1));\n+\n+\t/*\n+\t * NOTE! This ordering means that the same exact tree merged with a\n+\t * different order of parents will be a _different_ changeset even\n+\t * if everything else stays the same.\n+\t */\n+\tfor (i = 0; i < parents; i++)\n+\t\tadd_buffer(&buffer, &size, \"parent %s\\n\", sha1_to_hex(parent_sha1[i]));\n+\n+\t/* Person/date information */\n+\tadd_buffer(&buffer, &size, \"author %s\\n\", git_author_info(1));\n+\tadd_buffer(&buffer, &size, \"committer %s\\n\\n\", git_committer_info(1));\n+\n+\t/* And add the comment */\n+\twhile (fgets(comment, sizeof(comment), stdin) != NULL)\n+\t\tadd_buffer(&buffer, &size, \"%s\", comment);\n+\n+\tif (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {\n+\t\tprintf(\"%s\\n\", sha1_to_hex(commit_sha1));\n+\t\treturn 0;\n+\t}\n+\telse\n+\t\treturn 1;\n+}\ndiff --git a/builtin.h b/builtin.h\nindex 88b3523..c6b07d9 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -31,5 +31,6 @@ extern int cmd_ls_files(int argc, const \n extern int cmd_ls_tree(int argc, const char **argv, char **envp);\n extern int cmd_tar_tree(int argc, const char **argv, char **envp);\n extern int cmd_read_tree(int argc, const char **argv, char **envp);\n+extern int cmd_commit_tree(int argc, const char **argv, char **envp);\n \n #endif\ndiff --git a/commit-tree.c b/commit-tree.c\ndeleted file mode 100644\nindex 0320036..0000000\n--- a/commit-tree.c\n+++ /dev/null\n@@ -1,139 +0,0 @@\n-/*\n- * GIT - The information manager from hell\n- *\n- * Copyright (C) Linus Torvalds, 2005\n- */\n-#include \"cache.h\"\n-#include \"commit.h\"\n-#include \"tree.h\"\n-\n-#define BLOCKING (1ul << 14)\n-\n-/*\n- * FIXME! Share the code with \"write-tree.c\"\n- */\n-static void init_buffer(char **bufp, unsigned int *sizep)\n-{\n-\tchar *buf = xmalloc(BLOCKING);\n-\t*sizep = 0;\n-\t*bufp = buf;\n-}\n-\n-static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)\n-{\n-\tchar one_line[2048];\n-\tva_list args;\n-\tint len;\n-\tunsigned long alloc, size, newsize;\n-\tchar *buf;\n-\n-\tva_start(args, fmt);\n-\tlen = vsnprintf(one_line, sizeof(one_line), fmt, args);\n-\tva_end(args);\n-\tsize = *sizep;\n-\tnewsize = size + len;\n-\talloc = (size + 32767) & ~32767;\n-\tbuf = *bufp;\n-\tif (newsize > alloc) {\n-\t\talloc = (newsize + 32767) & ~32767;\n-\t\tbuf = xrealloc(buf, alloc);\n-\t\t*bufp = buf;\n-\t}\n-\t*sizep = newsize;\n-\tmemcpy(buf + size, one_line, len);\n-}\n-\n-static void check_valid(unsigned char *sha1, const char *expect)\n-{\n-\tchar type[20];\n-\n-\tif (sha1_object_info(sha1, type, NULL))\n-\t\tdie(\"%s is not a valid object\", sha1_to_hex(sha1));\n-\tif (expect && strcmp(type, expect))\n-\t\tdie(\"%s is not a valid '%s' object\", sha1_to_hex(sha1),\n-\t\t    expect);\n-}\n-\n-/*\n- * Having more than two parents is not strange at all, and this is\n- * how multi-way merges are represented.\n- */\n-#define MAXPARENT (16)\n-static unsigned char parent_sha1[MAXPARENT][20];\n-\n-static const char commit_tree_usage[] = \"git-commit-tree <sha1> [-p <sha1>]* < changelog\";\n-\n-static int new_parent(int idx)\n-{\n-\tint i;\n-\tunsigned char *sha1 = parent_sha1[idx];\n-\tfor (i = 0; i < idx; i++) {\n-\t\tif (!memcmp(parent_sha1[i], sha1, 20)) {\n-\t\t\terror(\"duplicate parent %s ignored\", sha1_to_hex(sha1));\n-\t\t\treturn 0;\n-\t\t}\n-\t}\n-\treturn 1;\n-}\n-\n-int main(int argc, char **argv)\n-{\n-\tint i;\n-\tint parents = 0;\n-\tunsigned char tree_sha1[20];\n-\tunsigned char commit_sha1[20];\n-\tchar comment[1000];\n-\tchar *buffer;\n-\tunsigned int size;\n-\n-\tsetup_ident();\n-\tsetup_git_directory();\n-\n-\tgit_config(git_default_config);\n-\n-\tif (argc < 2)\n-\t\tusage(commit_tree_usage);\n-\tif (get_sha1(argv[1], tree_sha1))\n-\t\tdie(\"Not a valid object name %s\", argv[1]);\n-\n-\tcheck_valid(tree_sha1, tree_type);\n-\tfor (i = 2; i < argc; i += 2) {\n-\t\tchar *a, *b;\n-\t\ta = argv[i]; b = argv[i+1];\n-\t\tif (!b || strcmp(a, \"-p\"))\n-\t\t\tusage(commit_tree_usage);\n-\t\tif (get_sha1(b, parent_sha1[parents]))\n-\t\t\tdie(\"Not a valid object name %s\", b);\n-\t\tcheck_valid(parent_sha1[parents], commit_type);\n-\t\tif (new_parent(parents))\n-\t\t\tparents++;\n-\t}\n-\tif (!parents)\n-\t\tfprintf(stderr, \"Committing initial tree %s\\n\", argv[1]);\n-\n-\tinit_buffer(&buffer, &size);\n-\tadd_buffer(&buffer, &size, \"tree %s\\n\", sha1_to_hex(tree_sha1));\n-\n-\t/*\n-\t * NOTE! This ordering means that the same exact tree merged with a\n-\t * different order of parents will be a _different_ changeset even\n-\t * if everything else stays the same.\n-\t */\n-\tfor (i = 0; i < parents; i++)\n-\t\tadd_buffer(&buffer, &size, \"parent %s\\n\", sha1_to_hex(parent_sha1[i]));\n-\n-\t/* Person/date information */\n-\tadd_buffer(&buffer, &size, \"author %s\\n\", git_author_info(1));\n-\tadd_buffer(&buffer, &size, \"committer %s\\n\\n\", git_committer_info(1));\n-\n-\t/* And add the comment */\n-\twhile (fgets(comment, sizeof(comment), stdin) != NULL)\n-\t\tadd_buffer(&buffer, &size, \"%s\", comment);\n-\n-\tif (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {\n-\t\tprintf(\"%s\\n\", sha1_to_hex(commit_sha1));\n-\t\treturn 0;\n-\t}\n-\telse\n-\t\treturn 1;\n-}\ndiff --git a/git.c b/git.c\nindex 300e2b2..4c2c062 100644\n--- a/git.c\n+++ b/git.c\n@@ -56,7 +56,8 @@ static void handle_internal_command(int \n \t\t{ \"ls-files\", cmd_ls_files },\n \t\t{ \"ls-tree\", cmd_ls_tree },\n \t\t{ \"tar-tree\", cmd_tar_tree },\n-\t\t{ \"read-tree\", cmd_read_tree }\n+\t\t{ \"read-tree\", cmd_read_tree },\n+\t\t{ \"commit-tree\", cmd_commit_tree }\n \t};\n \tint i;\n \n-- \n1.3.3.g288c\n"}]}