write-tree.con commit ls-remote: define die() now we do not use git-sh-setup (1abacf3)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8static int missing_ok = 0;
   9
  10static int check_valid_sha1(unsigned char *sha1)
  11{
  12        int ret;
  13
  14        /* If we were anal, we'd check that the sha1 of the contents actually matches */
  15        ret = has_sha1_file(sha1);
  16        if (ret == 0)
  17                perror(sha1_file_name(sha1));
  18        return ret ? 0 : -1;
  19}
  20
  21static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
  22{
  23        unsigned char subdir_sha1[20];
  24        unsigned long size, offset;
  25        char *buffer;
  26        int nr;
  27
  28        /* Guess at some random initial size */
  29        size = 8192;
  30        buffer = xmalloc(size);
  31        offset = 0;
  32
  33        nr = 0;
  34        while (nr < maxentries) {
  35                struct cache_entry *ce = cachep[nr];
  36                const char *pathname = ce->name, *filename, *dirname;
  37                int pathlen = ce_namelen(ce), entrylen;
  38                unsigned char *sha1;
  39                unsigned int mode;
  40
  41                /* Did we hit the end of the directory? Return how many we wrote */
  42                if (baselen >= pathlen || memcmp(base, pathname, baselen))
  43                        break;
  44
  45                sha1 = ce->sha1;
  46                mode = ntohl(ce->ce_mode);
  47
  48                /* Do we have _further_ subdirectories? */
  49                filename = pathname + baselen;
  50                dirname = strchr(filename, '/');
  51                if (dirname) {
  52                        int subdir_written;
  53
  54                        subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1);
  55                        nr += subdir_written;
  56
  57                        /* Now we need to write out the directory entry into this tree.. */
  58                        mode = S_IFDIR;
  59                        pathlen = dirname - pathname;
  60
  61                        /* ..but the directory entry doesn't count towards the total count */
  62                        nr--;
  63                        sha1 = subdir_sha1;
  64                }
  65
  66                if (!missing_ok && check_valid_sha1(sha1) < 0)
  67                        exit(1);
  68
  69                entrylen = pathlen - baselen;
  70                if (offset + entrylen + 100 > size) {
  71                        size = alloc_nr(offset + entrylen + 100);
  72                        buffer = xrealloc(buffer, size);
  73                }
  74                offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
  75                buffer[offset++] = 0;
  76                memcpy(buffer + offset, sha1, 20);
  77                offset += 20;
  78                nr++;
  79        }
  80
  81        write_sha1_file(buffer, offset, "tree", returnsha1);
  82        free(buffer);
  83        return nr;
  84}
  85
  86int main(int argc, char **argv)
  87{
  88        int i, funny;
  89        int entries;
  90        unsigned char sha1[20];
  91        
  92        setup_git_directory();
  93
  94        entries = read_cache();
  95        if (argc == 2) {
  96                if (!strcmp(argv[1], "--missing-ok"))
  97                        missing_ok = 1;
  98                else
  99                        die("unknown option %s", argv[1]);
 100        }
 101        
 102        if (argc > 2)
 103                die("too many options");
 104
 105        if (entries < 0)
 106                die("git-write-tree: error reading cache");
 107
 108        /* Verify that the tree is merged */
 109        funny = 0;
 110        for (i = 0; i < entries; i++) {
 111                struct cache_entry *ce = active_cache[i];
 112                if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {
 113                        if (10 < ++funny) {
 114                                fprintf(stderr, "...\n");
 115                                break;
 116                        }
 117                        fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
 118                }
 119        }
 120        if (funny)
 121                die("git-write-tree: not able to write tree");
 122
 123        /* Also verify that the cache does not have path and path/file
 124         * at the same time.  At this point we know the cache has only
 125         * stage 0 entries.
 126         */
 127        funny = 0;
 128        for (i = 0; i < entries - 1; i++) {
 129                /* path/file always comes after path because of the way
 130                 * the cache is sorted.  Also path can appear only once,
 131                 * which means conflicting one would immediately follow.
 132                 */
 133                const char *this_name = active_cache[i]->name;
 134                const char *next_name = active_cache[i+1]->name;
 135                int this_len = strlen(this_name);
 136                if (this_len < strlen(next_name) &&
 137                    strncmp(this_name, next_name, this_len) == 0 &&
 138                    next_name[this_len] == '/') {
 139                        if (10 < ++funny) {
 140                                fprintf(stderr, "...\n");
 141                                break;
 142                        }
 143                        fprintf(stderr, "You have both %s and %s\n",
 144                                this_name, next_name);
 145                }
 146        }
 147        if (funny)
 148                die("git-write-tree: not able to write tree");
 149
 150        /* Ok, write it out */
 151        if (write_tree(active_cache, entries, "", 0, sha1) != entries)
 152                die("git-write-tree: internal error");
 153        printf("%s\n", sha1_to_hex(sha1));
 154        return 0;
 155}