Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | |
Bryan Larsen | 9c1fa70 | 2005-07-10 20:53:44 -0700 | [diff] [blame] | 8 | static int missing_ok = 0; |
| 9 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 10 | static int check_valid_sha1(unsigned char *sha1) |
| 11 | { |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 12 | int ret; |
| 13 | |
| 14 | /* If we were anal, we'd check that the sha1 of the contents actually matches */ |
Jan Harkes | 7323aa1 | 2005-06-25 14:23:36 -0400 | [diff] [blame] | 15 | ret = has_sha1_file(sha1); |
| 16 | if (ret == 0) |
| 17 | perror(sha1_file_name(sha1)); |
| 18 | return ret ? 0 : -1; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 21 | static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 22 | { |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 23 | unsigned char subdir_sha1[20]; |
Linus Torvalds | 19b2860 | 2005-04-08 09:59:28 -0700 | [diff] [blame] | 24 | unsigned long size, offset; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 25 | char *buffer; |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 26 | int nr; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 28 | /* Guess at some random initial size */ |
| 29 | size = 8192; |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 30 | buffer = xmalloc(size); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 31 | offset = 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 33 | nr = 0; |
Petr Baudis | c899350 | 2005-05-08 16:15:59 +0200 | [diff] [blame] | 34 | while (nr < maxentries) { |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 35 | struct cache_entry *ce = cachep[nr]; |
| 36 | const char *pathname = ce->name, *filename, *dirname; |
Linus Torvalds | ccc4feb | 2005-04-15 10:44:27 -0700 | [diff] [blame] | 37 | int pathlen = ce_namelen(ce), entrylen; |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 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; |
Linus Torvalds | ccc4feb | 2005-04-15 10:44:27 -0700 | [diff] [blame] | 46 | mode = ntohl(ce->ce_mode); |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 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); |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 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 | |
Bryan Larsen | 9c1fa70 | 2005-07-10 20:53:44 -0700 | [diff] [blame] | 66 | if (!missing_ok && check_valid_sha1(sha1) < 0) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 67 | exit(1); |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 68 | |
| 69 | entrylen = pathlen - baselen; |
| 70 | if (offset + entrylen + 100 > size) { |
| 71 | size = alloc_nr(offset + entrylen + 100); |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 72 | buffer = xrealloc(buffer, size); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 73 | } |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 74 | offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 75 | buffer[offset++] = 0; |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 76 | memcpy(buffer + offset, sha1, 20); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 77 | offset += 20; |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 78 | nr++; |
Petr Baudis | c899350 | 2005-05-08 16:15:59 +0200 | [diff] [blame] | 79 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 81 | write_sha1_file(buffer, offset, "tree", returnsha1); |
Brad Roberts | 7223a88 | 2005-04-17 10:55:12 -0700 | [diff] [blame] | 82 | free(buffer); |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 83 | return nr; |
| 84 | } |
| 85 | |
| 86 | int main(int argc, char **argv) |
| 87 | { |
Junio C Hamano | 8eef4d3 | 2005-05-07 12:22:02 -0700 | [diff] [blame] | 88 | int i, funny; |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 89 | int entries = read_cache(); |
| 90 | unsigned char sha1[20]; |
Bryan Larsen | 9c1fa70 | 2005-07-10 20:53:44 -0700 | [diff] [blame] | 91 | |
Petr Baudis | 0b124bb | 2005-07-29 11:00:45 +0200 | [diff] [blame] | 92 | if (argc == 2) { |
Bryan Larsen | 9c1fa70 | 2005-07-10 20:53:44 -0700 | [diff] [blame] | 93 | if (!strcmp(argv[1], "--missing-ok")) |
| 94 | missing_ok = 1; |
| 95 | else |
| 96 | die("unknown option %s", argv[1]); |
| 97 | } |
| 98 | |
Petr Baudis | 0b124bb | 2005-07-29 11:00:45 +0200 | [diff] [blame] | 99 | if (argc > 2) |
Bryan Larsen | 9c1fa70 | 2005-07-10 20:53:44 -0700 | [diff] [blame] | 100 | die("too many options"); |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 101 | |
Petr Baudis | c899350 | 2005-05-08 16:15:59 +0200 | [diff] [blame] | 102 | if (entries < 0) |
Alexey Nezhdanov | 667bb59 | 2005-05-19 15:17:16 +0400 | [diff] [blame] | 103 | die("git-write-tree: error reading cache"); |
Linus Torvalds | c347ea5 | 2005-04-15 22:04:54 -0700 | [diff] [blame] | 104 | |
| 105 | /* Verify that the tree is merged */ |
Junio C Hamano | 8eef4d3 | 2005-05-07 12:22:02 -0700 | [diff] [blame] | 106 | funny = 0; |
Linus Torvalds | c347ea5 | 2005-04-15 22:04:54 -0700 | [diff] [blame] | 107 | for (i = 0; i < entries; i++) { |
| 108 | struct cache_entry *ce = active_cache[i]; |
| 109 | if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) { |
Junio C Hamano | 8eef4d3 | 2005-05-07 12:22:02 -0700 | [diff] [blame] | 110 | if (10 < ++funny) { |
Linus Torvalds | c347ea5 | 2005-04-15 22:04:54 -0700 | [diff] [blame] | 111 | fprintf(stderr, "...\n"); |
| 112 | break; |
| 113 | } |
| 114 | fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1)); |
| 115 | } |
| 116 | } |
Junio C Hamano | 8eef4d3 | 2005-05-07 12:22:02 -0700 | [diff] [blame] | 117 | if (funny) |
Alexey Nezhdanov | 667bb59 | 2005-05-19 15:17:16 +0400 | [diff] [blame] | 118 | die("git-write-tree: not able to write tree"); |
Junio C Hamano | 8eef4d3 | 2005-05-07 12:22:02 -0700 | [diff] [blame] | 119 | |
| 120 | /* Also verify that the cache does not have path and path/file |
| 121 | * at the same time. At this point we know the cache has only |
| 122 | * stage 0 entries. |
| 123 | */ |
| 124 | funny = 0; |
| 125 | for (i = 0; i < entries - 1; i++) { |
| 126 | /* path/file always comes after path because of the way |
| 127 | * the cache is sorted. Also path can appear only once, |
| 128 | * which means conflicting one would immediately follow. |
| 129 | */ |
| 130 | const char *this_name = active_cache[i]->name; |
| 131 | const char *next_name = active_cache[i+1]->name; |
| 132 | int this_len = strlen(this_name); |
| 133 | if (this_len < strlen(next_name) && |
| 134 | strncmp(this_name, next_name, this_len) == 0 && |
| 135 | next_name[this_len] == '/') { |
| 136 | if (10 < ++funny) { |
| 137 | fprintf(stderr, "...\n"); |
| 138 | break; |
| 139 | } |
| 140 | fprintf(stderr, "You have both %s and %s\n", |
| 141 | this_name, next_name); |
| 142 | } |
| 143 | } |
| 144 | if (funny) |
Alexey Nezhdanov | 667bb59 | 2005-05-19 15:17:16 +0400 | [diff] [blame] | 145 | die("git-write-tree: not able to write tree"); |
Linus Torvalds | c347ea5 | 2005-04-15 22:04:54 -0700 | [diff] [blame] | 146 | |
| 147 | /* Ok, write it out */ |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 148 | if (write_tree(active_cache, entries, "", 0, sha1) != entries) |
Alexey Nezhdanov | 667bb59 | 2005-05-19 15:17:16 +0400 | [diff] [blame] | 149 | die("git-write-tree: internal error"); |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 150 | printf("%s\n", sha1_to_hex(sha1)); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |