2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 static int missing_ok
= 0;
11 static int check_valid_sha1(unsigned char *sha1
)
15 /* If we were anal, we'd check that the sha1 of the contents actually matches */
16 ret
= has_sha1_file(sha1
);
18 perror(sha1_file_name(sha1
));
22 static int write_tree(struct cache_entry
**cachep
, int maxentries
, const char *base
, int baselen
, unsigned char *returnsha1
)
24 unsigned char subdir_sha1
[20];
25 unsigned long size
, offset
;
29 /* Guess at some random initial size */
31 buffer
= xmalloc(size
);
35 while (nr
< maxentries
) {
36 struct cache_entry
*ce
= cachep
[nr
];
37 const char *pathname
= ce
->name
, *filename
, *dirname
;
38 int pathlen
= ce_namelen(ce
), entrylen
;
42 /* Did we hit the end of the directory? Return how many we wrote */
43 if (baselen
>= pathlen
|| memcmp(base
, pathname
, baselen
))
47 mode
= ntohl(ce
->ce_mode
);
49 /* Do we have _further_ subdirectories? */
50 filename
= pathname
+ baselen
;
51 dirname
= strchr(filename
, '/');
55 subdir_written
= write_tree(cachep
+ nr
, maxentries
- nr
, pathname
, dirname
-pathname
+1, subdir_sha1
);
58 /* Now we need to write out the directory entry into this tree.. */
60 pathlen
= dirname
- pathname
;
62 /* ..but the directory entry doesn't count towards the total count */
67 if (!missing_ok
&& check_valid_sha1(sha1
) < 0)
70 entrylen
= pathlen
- baselen
;
71 if (offset
+ entrylen
+ 100 > size
) {
72 size
= alloc_nr(offset
+ entrylen
+ 100);
73 buffer
= xrealloc(buffer
, size
);
75 offset
+= sprintf(buffer
+ offset
, "%o %.*s", mode
, entrylen
, filename
);
77 memcpy(buffer
+ offset
, sha1
, 20);
82 write_sha1_file(buffer
, offset
, tree_type
, returnsha1
);
87 static const char write_tree_usage
[] = "git-write-tree [--missing-ok]";
89 int main(int argc
, char **argv
)
93 unsigned char sha1
[20];
95 setup_git_directory();
97 entries
= read_cache();
99 if (!strcmp(argv
[1], "--missing-ok"))
102 die(write_tree_usage
);
106 die("too many options");
109 die("git-write-tree: error reading cache");
111 /* Verify that the tree is merged */
113 for (i
= 0; i
< entries
; i
++) {
114 struct cache_entry
*ce
= active_cache
[i
];
117 fprintf(stderr
, "...\n");
120 fprintf(stderr
, "%s: unmerged (%s)\n", ce
->name
, sha1_to_hex(ce
->sha1
));
124 die("git-write-tree: not able to write tree");
126 /* Also verify that the cache does not have path and path/file
127 * at the same time. At this point we know the cache has only
131 for (i
= 0; i
< entries
- 1; i
++) {
132 /* path/file always comes after path because of the way
133 * the cache is sorted. Also path can appear only once,
134 * which means conflicting one would immediately follow.
136 const char *this_name
= active_cache
[i
]->name
;
137 const char *next_name
= active_cache
[i
+1]->name
;
138 int this_len
= strlen(this_name
);
139 if (this_len
< strlen(next_name
) &&
140 strncmp(this_name
, next_name
, this_len
) == 0 &&
141 next_name
[this_len
] == '/') {
143 fprintf(stderr
, "...\n");
146 fprintf(stderr
, "You have both %s and %s\n",
147 this_name
, next_name
);
151 die("git-write-tree: not able to write tree");
153 /* Ok, write it out */
154 if (write_tree(active_cache
, entries
, "", 0, sha1
) != entries
)
155 die("git-write-tree: internal error");
156 printf("%s\n", sha1_to_hex(sha1
));