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