write-tree.con commit [PATCH] Fix +x-related show-diff false positives (e8871e8)
   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        char *filename = sha1_file_name(sha1);
  11        int ret;
  12
  13        /* If we were anal, we'd check that the sha1 of the contents actually matches */
  14        ret = access(filename, R_OK);
  15        if (ret)
  16                perror(filename);
  17        return ret;
  18}
  19
  20static int prepend_integer(char *buffer, unsigned val, int i)
  21{
  22        buffer[--i] = '\0';
  23        do {
  24                buffer[--i] = '0' + (val % 10);
  25                val /= 10;
  26        } while (val);
  27        return i;
  28}
  29
  30#define ORIG_OFFSET (40)        /* Enough space to add the header of "tree <size>\0" */
  31
  32static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
  33{
  34        unsigned char subdir_sha1[20];
  35        unsigned long size, offset;
  36        char *buffer;
  37        int i, nr;
  38
  39        /* Guess at some random initial size */
  40        size = 8192;
  41        buffer = malloc(size);
  42        offset = ORIG_OFFSET;
  43
  44        nr = 0;
  45        do {
  46                struct cache_entry *ce = cachep[nr];
  47                const char *pathname = ce->name, *filename, *dirname;
  48                int pathlen = ce_namelen(ce), entrylen;
  49                unsigned char *sha1;
  50                unsigned int mode;
  51
  52                /* Did we hit the end of the directory? Return how many we wrote */
  53                if (baselen >= pathlen || memcmp(base, pathname, baselen))
  54                        break;
  55
  56                sha1 = ce->sha1;
  57                mode = ntohl(ce->ce_mode);
  58
  59                /* Do we have _further_ subdirectories? */
  60                filename = pathname + baselen;
  61                dirname = strchr(filename, '/');
  62                if (dirname) {
  63                        int subdir_written;
  64
  65                        subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1);
  66                        nr += subdir_written;
  67
  68                        /* Now we need to write out the directory entry into this tree.. */
  69                        mode = S_IFDIR;
  70                        pathlen = dirname - pathname;
  71
  72                        /* ..but the directory entry doesn't count towards the total count */
  73                        nr--;
  74                        sha1 = subdir_sha1;
  75                }
  76
  77                if (check_valid_sha1(sha1) < 0)
  78                        exit(1);
  79
  80                entrylen = pathlen - baselen;
  81                if (offset + entrylen + 100 > size) {
  82                        size = alloc_nr(offset + entrylen + 100);
  83                        buffer = realloc(buffer, size);
  84                }
  85                offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
  86                buffer[offset++] = 0;
  87                memcpy(buffer + offset, sha1, 20);
  88                offset += 20;
  89                nr++;
  90        } while (nr < maxentries);
  91
  92        i = prepend_integer(buffer, offset - ORIG_OFFSET, ORIG_OFFSET);
  93        i -= 5;
  94        memcpy(buffer+i, "tree ", 5);
  95
  96        buffer += i;
  97        offset -= i;
  98
  99        write_sha1_file(buffer, offset, returnsha1);
 100        return nr;
 101}
 102
 103int main(int argc, char **argv)
 104{
 105        int i, unmerged;
 106        int entries = read_cache();
 107        unsigned char sha1[20];
 108
 109        if (entries <= 0)
 110                die("write-tree: no cache contents to write");
 111
 112        /* Verify that the tree is merged */
 113        unmerged = 0;
 114        for (i = 0; i < entries; i++) {
 115                struct cache_entry *ce = active_cache[i];
 116                if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {
 117                        if (++unmerged > 10) {
 118                                fprintf(stderr, "...\n");
 119                                break;
 120                        }
 121                        fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
 122                }
 123        }
 124        if (unmerged)
 125                die("write-tree: not able to write tree");
 126
 127        /* Ok, write it out */
 128        if (write_tree(active_cache, entries, "", 0, sha1) != entries)
 129                die("write-tree: internal error");
 130        printf("%s\n", sha1_to_hex(sha1));
 131        return 0;
 132}