2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static int missing_ok
= 0;
10 static int check_valid_sha1(unsigned char *sha1
)
14 /* If we were anal, we'd check that the sha1 of the contents actually matches */
15 ret
= has_sha1_file(sha1
);
17 perror(sha1_file_name(sha1
));
21 static int write_tree(struct cache_entry
**cachep
, int maxentries
, const char *base
, int baselen
, unsigned char *returnsha1
)
23 unsigned char subdir_sha1
[20];
24 unsigned long size
, offset
;
28 /* Guess at some random initial size */
30 buffer
= xmalloc(size
);
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
;
41 /* Did we hit the end of the directory? Return how many we wrote */
42 if (baselen
>= pathlen
|| memcmp(base
, pathname
, baselen
))
46 mode
= ntohl(ce
->ce_mode
);
48 /* Do we have _further_ subdirectories? */
49 filename
= pathname
+ baselen
;
50 dirname
= strchr(filename
, '/');
54 subdir_written
= write_tree(cachep
+ nr
, maxentries
- nr
, pathname
, dirname
-pathname
+1, subdir_sha1
);
57 /* Now we need to write out the directory entry into this tree.. */
59 pathlen
= dirname
- pathname
;
61 /* ..but the directory entry doesn't count towards the total count */
66 if (!missing_ok
&& check_valid_sha1(sha1
) < 0)
69 entrylen
= pathlen
- baselen
;
70 if (offset
+ entrylen
+ 100 > size
) {
71 size
= alloc_nr(offset
+ entrylen
+ 100);
72 buffer
= xrealloc(buffer
, size
);
74 offset
+= sprintf(buffer
+ offset
, "%o %.*s", mode
, entrylen
, filename
);
76 memcpy(buffer
+ offset
, sha1
, 20);
81 write_sha1_file(buffer
, offset
, "tree", returnsha1
);
86 int main(int argc
, char **argv
)
89 int entries
= read_cache();
90 unsigned char sha1
[20];
93 if (!strcmp(argv
[1], "--missing-ok"))
96 die("unknown option %s", argv
[1]);
100 die("too many options");
103 die("git-write-tree: error reading cache");
105 /* Verify that the tree is merged */
107 for (i
= 0; i
< entries
; i
++) {
108 struct cache_entry
*ce
= active_cache
[i
];
109 if (ntohs(ce
->ce_flags
) & ~CE_NAMEMASK
) {
111 fprintf(stderr
, "...\n");
114 fprintf(stderr
, "%s: unmerged (%s)\n", ce
->name
, sha1_to_hex(ce
->sha1
));
118 die("git-write-tree: not able to write tree");
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
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.
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
] == '/') {
137 fprintf(stderr
, "...\n");
140 fprintf(stderr
, "You have both %s and %s\n",
141 this_name
, next_name
);
145 die("git-write-tree: not able to write tree");
147 /* Ok, write it out */
148 if (write_tree(active_cache
, entries
, "", 0, sha1
) != entries
)
149 die("git-write-tree: internal error");
150 printf("%s\n", sha1_to_hex(sha1
));