Skip to content
Newer
Older
100644 159 lines (134 sloc) 3.85 KB
8bc9a0c Add copyright notices.
Linus Torvalds authored Apr 7, 2005
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
6 #include "cache.h"
8e44025 Use blob_, commit_, tag_, and tree_type throughout.
Peter Eriksen authored Apr 2, 2006
7 #include "tree.h"
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
8
9c1fa70 [PATCH] add --missing-ok option to write-tree
Bryan Larsen authored Jul 10, 2005
9 static int missing_ok = 0;
10
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
11 static int check_valid_sha1(unsigned char *sha1)
12 {
13 int ret;
14
15 /* If we were anal, we'd check that the sha1 of the contents actually matches */
7323aa1 @jaharkes [PATCH] git-write-tree doesn't check alternate directories
jaharkes authored Jun 25, 2005
16 ret = has_sha1_file(sha1);
17 if (ret == 0)
18 perror(sha1_file_name(sha1));
19 return ret ? 0 : -1;
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
20 }
21
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 9, 2005
22 static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
23 {
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
24 unsigned char subdir_sha1[20];
19b2860 Use "-Wall -O2" for the compiler to get more warnings.
Linus Torvalds authored Apr 8, 2005
25 unsigned long size, offset;
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
26 char *buffer;
a44c9a5 Simplify "write_sha1_file()" interfaces
Linus Torvalds authored Apr 25, 2005
27 int nr;
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
28
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
29 /* Guess at some random initial size */
30 size = 8192;
812666c [PATCH] introduce xmalloc and xrealloc
Christopher Li authored Apr 26, 2005
31 buffer = xmalloc(size);
a44c9a5 Simplify "write_sha1_file()" interfaces
Linus Torvalds authored Apr 25, 2005
32 offset = 0;
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
33
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
34 nr = 0;
c899350 @pasky write-tree is now willing to write empty tree
pasky authored May 8, 2005
35 while (nr < maxentries) {
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
36 struct cache_entry *ce = cachep[nr];
37 const char *pathname = ce->name, *filename, *dirname;
ccc4feb Convert the index file reading/writing to use network byte order.
Linus Torvalds authored Apr 15, 2005
38 int pathlen = ce_namelen(ce), entrylen;
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
39 unsigned char *sha1;
40 unsigned int mode;
41
42 /* Did we hit the end of the directory? Return how many we wrote */
43 if (baselen >= pathlen || memcmp(base, pathname, baselen))
44 break;
45
46 sha1 = ce->sha1;
ccc4feb Convert the index file reading/writing to use network byte order.
Linus Torvalds authored Apr 15, 2005
47 mode = ntohl(ce->ce_mode);
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
48
49 /* Do we have _further_ subdirectories? */
50 filename = pathname + baselen;
51 dirname = strchr(filename, '/');
52 if (dirname) {
53 int subdir_written;
54
55 subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1);
56 nr += subdir_written;
57
58 /* Now we need to write out the directory entry into this tree.. */
59 mode = S_IFDIR;
60 pathlen = dirname - pathname;
61
62 /* ..but the directory entry doesn't count towards the total count */
63 nr--;
64 sha1 = subdir_sha1;
65 }
66
9c1fa70 [PATCH] add --missing-ok option to write-tree
Bryan Larsen authored Jul 11, 2005
67 if (!missing_ok && check_valid_sha1(sha1) < 0)
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
68 exit(1);
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
69
70 entrylen = pathlen - baselen;
71 if (offset + entrylen + 100 > size) {
72 size = alloc_nr(offset + entrylen + 100);
812666c [PATCH] introduce xmalloc and xrealloc
Christopher Li authored Apr 26, 2005
73 buffer = xrealloc(buffer, size);
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
74 }
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
75 offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
76 buffer[offset++] = 0;
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
77 memcpy(buffer + offset, sha1, 20);
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
78 offset += 20;
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
79 nr++;
c899350 @pasky write-tree is now willing to write empty tree
pasky authored May 8, 2005
80 }
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
81
8e44025 Use blob_, commit_, tag_, and tree_type throughout.
Peter Eriksen authored Apr 2, 2006
82 write_sha1_file(buffer, offset, tree_type, returnsha1);
7223a88 @braddr [PATCH] fix for memory leak in write-tree.c
braddr authored Apr 17, 2005
83 free(buffer);
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
84 return nr;
85 }
86
75a46f6 write-tree: check extra arguments and die but be a bit more helpful.
Junio C Hamano authored Dec 5, 2005
87 static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
88
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
89 int main(int argc, char **argv)
90 {
8eef4d3 Notice index that has path and path/file and refuse to write such a t…
Junio C Hamano authored May 7, 2005
91 int i, funny;
53228a5 Make the rest of commands work from a subdirectory.
Junio C Hamano authored Nov 26, 2005
92 int entries;
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
93 unsigned char sha1[20];
9c1fa70 [PATCH] add --missing-ok option to write-tree
Bryan Larsen authored Jul 11, 2005
94
53228a5 Make the rest of commands work from a subdirectory.
Junio C Hamano authored Nov 26, 2005
95 setup_git_directory();
96
97 entries = read_cache();
0b124bb [PATCH] Trivial tidyups
Petr Baudis authored Jul 29, 2005
98 if (argc == 2) {
9c1fa70 [PATCH] add --missing-ok option to write-tree
Bryan Larsen authored Jul 11, 2005
99 if (!strcmp(argv[1], "--missing-ok"))
100 missing_ok = 1;
101 else
75a46f6 write-tree: check extra arguments and die but be a bit more helpful.
Junio C Hamano authored Dec 6, 2005
102 die(write_tree_usage);
9c1fa70 [PATCH] add --missing-ok option to write-tree
Bryan Larsen authored Jul 11, 2005
103 }
104
0b124bb [PATCH] Trivial tidyups
Petr Baudis authored Jul 29, 2005
105 if (argc > 2)
9c1fa70 [PATCH] add --missing-ok option to write-tree
Bryan Larsen authored Jul 11, 2005
106 die("too many options");
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
107
c899350 @pasky write-tree is now willing to write empty tree
pasky authored May 8, 2005
108 if (entries < 0)
667bb59 [PATCH] cleanup of in-code names
Alexey Nezhdanov authored May 19, 2005
109 die("git-write-tree: error reading cache");
c347ea5 write-tree: refuse to write out trees with unmerged index entries.
Linus Torvalds authored Apr 15, 2005
110
111 /* Verify that the tree is merged */
8eef4d3 Notice index that has path and path/file and refuse to write such a t…
Junio C Hamano authored May 7, 2005
112 funny = 0;
c347ea5 write-tree: refuse to write out trees with unmerged index entries.
Linus Torvalds authored Apr 16, 2005
113 for (i = 0; i < entries; i++) {
114 struct cache_entry *ce = active_cache[i];
5f73076 "Assume unchanged" git
Junio C Hamano authored Feb 8, 2006
115 if (ce_stage(ce)) {
8eef4d3 Notice index that has path and path/file and refuse to write such a t…
Junio C Hamano authored May 7, 2005
116 if (10 < ++funny) {
c347ea5 write-tree: refuse to write out trees with unmerged index entries.
Linus Torvalds authored Apr 16, 2005
117 fprintf(stderr, "...\n");
118 break;
119 }
120 fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
121 }
122 }
8eef4d3 Notice index that has path and path/file and refuse to write such a t…
Junio C Hamano authored May 7, 2005
123 if (funny)
667bb59 [PATCH] cleanup of in-code names
Alexey Nezhdanov authored May 19, 2005
124 die("git-write-tree: not able to write tree");
8eef4d3 Notice index that has path and path/file and refuse to write such a t…
Junio C Hamano authored May 7, 2005
125
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
128 * stage 0 entries.
129 */
130 funny = 0;
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.
135 */
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] == '/') {
142 if (10 < ++funny) {
143 fprintf(stderr, "...\n");
144 break;
145 }
146 fprintf(stderr, "You have both %s and %s\n",
147 this_name, next_name);
148 }
149 }
150 if (funny)
667bb59 [PATCH] cleanup of in-code names
Alexey Nezhdanov authored May 19, 2005
151 die("git-write-tree: not able to write tree");
c347ea5 write-tree: refuse to write out trees with unmerged index entries.
Linus Torvalds authored Apr 16, 2005
152
153 /* Ok, write it out */
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
154 if (write_tree(active_cache, entries, "", 0, sha1) != entries)
667bb59 [PATCH] cleanup of in-code names
Alexey Nezhdanov authored May 19, 2005
155 die("git-write-tree: internal error");
d6d3f9d This implements the new "recursive tree" write-tree.
Linus Torvalds authored Apr 10, 2005
156 printf("%s\n", sha1_to_hex(sha1));
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored Apr 7, 2005
157 return 0;
158 }
Something went wrong with that request. Please try again.