builtin / commit-tree.con commit Merge branch 'ls/p4-empty-file-on-lfs' (796bd3b)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "commit.h"
   8#include "tree.h"
   9#include "builtin.h"
  10#include "utf8.h"
  11#include "gpg-interface.h"
  12
  13static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
  14
  15static const char *sign_commit;
  16
  17static void new_parent(struct commit *parent, struct commit_list **parents_p)
  18{
  19        struct object_id *oid = &parent->object.oid;
  20        struct commit_list *parents;
  21        for (parents = *parents_p; parents; parents = parents->next) {
  22                if (parents->item == parent) {
  23                        error("duplicate parent %s ignored", oid_to_hex(oid));
  24                        return;
  25                }
  26                parents_p = &parents->next;
  27        }
  28        commit_list_insert(parent, parents_p);
  29}
  30
  31static int commit_tree_config(const char *var, const char *value, void *cb)
  32{
  33        int status = git_gpg_config(var, value, NULL);
  34        if (status)
  35                return status;
  36        return git_default_config(var, value, cb);
  37}
  38
  39int cmd_commit_tree(int argc, const char **argv, const char *prefix)
  40{
  41        int i, got_tree = 0;
  42        struct commit_list *parents = NULL;
  43        struct object_id tree_oid;
  44        struct object_id commit_oid;
  45        struct strbuf buffer = STRBUF_INIT;
  46
  47        git_config(commit_tree_config, NULL);
  48
  49        if (argc < 2 || !strcmp(argv[1], "-h"))
  50                usage(commit_tree_usage);
  51
  52        for (i = 1; i < argc; i++) {
  53                const char *arg = argv[i];
  54                if (!strcmp(arg, "-p")) {
  55                        struct object_id oid;
  56                        if (argc <= ++i)
  57                                usage(commit_tree_usage);
  58                        if (get_sha1_commit(argv[i], oid.hash))
  59                                die("Not a valid object name %s", argv[i]);
  60                        assert_sha1_type(oid.hash, OBJ_COMMIT);
  61                        new_parent(lookup_commit(oid.hash), &parents);
  62                        continue;
  63                }
  64
  65                if (skip_prefix(arg, "-S", &sign_commit))
  66                        continue;
  67
  68                if (!strcmp(arg, "--no-gpg-sign")) {
  69                        sign_commit = NULL;
  70                        continue;
  71                }
  72
  73                if (!strcmp(arg, "-m")) {
  74                        if (argc <= ++i)
  75                                usage(commit_tree_usage);
  76                        if (buffer.len)
  77                                strbuf_addch(&buffer, '\n');
  78                        strbuf_addstr(&buffer, argv[i]);
  79                        strbuf_complete_line(&buffer);
  80                        continue;
  81                }
  82
  83                if (!strcmp(arg, "-F")) {
  84                        int fd;
  85
  86                        if (argc <= ++i)
  87                                usage(commit_tree_usage);
  88                        if (buffer.len)
  89                                strbuf_addch(&buffer, '\n');
  90                        if (!strcmp(argv[i], "-"))
  91                                fd = 0;
  92                        else {
  93                                fd = open(argv[i], O_RDONLY);
  94                                if (fd < 0)
  95                                        die_errno("git commit-tree: failed to open '%s'",
  96                                                  argv[i]);
  97                        }
  98                        if (strbuf_read(&buffer, fd, 0) < 0)
  99                                die_errno("git commit-tree: failed to read '%s'",
 100                                          argv[i]);
 101                        if (fd && close(fd))
 102                                die_errno("git commit-tree: failed to close '%s'",
 103                                          argv[i]);
 104                        strbuf_complete_line(&buffer);
 105                        continue;
 106                }
 107
 108                if (get_sha1_tree(arg, tree_oid.hash))
 109                        die("Not a valid object name %s", arg);
 110                if (got_tree)
 111                        die("Cannot give more than one trees");
 112                got_tree = 1;
 113        }
 114
 115        if (!buffer.len) {
 116                if (strbuf_read(&buffer, 0, 0) < 0)
 117                        die_errno("git commit-tree: failed to read");
 118        }
 119
 120        if (commit_tree(buffer.buf, buffer.len, tree_oid.hash, parents,
 121                        commit_oid.hash, NULL, sign_commit)) {
 122                strbuf_release(&buffer);
 123                return 1;
 124        }
 125
 126        printf("%s\n", oid_to_hex(&commit_oid));
 127        strbuf_release(&buffer);
 128        return 0;
 129}