@matheusbsbs / matheus-bagliano / commits / 1b0c7174a1
Fork 0

tree/diff header cleanup.

Introduce tree-walk.[ch] and move "struct tree_desc" and associated functions from various places. Rename DIFF_FILE_CANON_MODE(mode) macro to canon_mode(mode) and move it to cache.h. This macro returns the canonicalized st_mode value in the host byte order for files, symlinks and directories -- to be compared with a tree_desc entry. create_ce_mode(mode) in cache.h is similar but is intended to be used for index entries (so it does not work for directories) and returns the value in the network byte order. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 29, 2006 at 22:55 UTC 1b0c7174a17de801051402ed35ac085ebd91e88c
13 files changed +156 -143
Makefile
+3 -2
@@ -193,7 +193,8 @@ XDIFF_LIB=xdiff/lib.a
193 LIB_H = \
194 blob.h cache.h commit.h csum-file.h delta.h \
195 diff.h object.h pack.h pkt-line.h quote.h refs.h \
196 - run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h
196 + run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
197 + tree-walk.h
198
199 DIFF_OBJS = \
200 diff.o diffcore-break.o diffcore-order.o diffcore-pathspec.o \
@@ -207,7 +208,7 @@ LIB_OBJS = \
208 quote.o read-cache.o refs.o run-command.o \
209 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
210 tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
210 - fetch-clone.o revision.o pager.o \
211 + fetch-clone.o revision.o pager.o tree-walk.o \
212 $(DIFF_OBJS)
213
214 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
cache.h
+3
@@ -106,6 +106,9 @@ static inline unsigned int create_ce_mode(unsigned int mode)
106 return htonl(S_IFLNK);
107 return htonl(S_IFREG | ce_permissions(mode));
108 }
109 +#define canon_mode(mode) \
110 + (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
111 + S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
112
113 #define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
114
combine-diff.c
+1 -1
@@ -649,7 +649,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
649 int len = st.st_size;
650 int cnt = 0;
651
652 - elem->mode = DIFF_FILE_CANON_MODE(st.st_mode);
652 + elem->mode = canon_mode(st.st_mode);
653 size = len;
654 result = xmalloc(len + 1);
655 while (cnt < len) {
diff-files.c
+2 -2
@@ -149,7 +149,7 @@ int main(int argc, const char **argv)
149 memcpy(combine.p.parent[stage-2].sha1,
150 nce->sha1, 20);
151 combine.p.parent[stage-2].mode =
152 - DIFF_FILE_CANON_MODE(mode);
152 + canon_mode(mode);
153 combine.p.parent[stage-2].status =
154 DIFF_STATUS_MODIFIED;
155 }
@@ -198,7 +198,7 @@ int main(int argc, const char **argv)
198 continue;
199 oldmode = ntohl(ce->ce_mode);
200
201 - newmode = DIFF_FILE_CANON_MODE(st.st_mode);
201 + newmode = canon_mode(st.st_mode);
202 if (!trust_executable_bit &&
203 S_ISREG(newmode) && S_ISREG(oldmode) &&
204 ((newmode ^ oldmode) == 0111))
diff.c
+1 -1
@@ -300,7 +300,7 @@ void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
300 unsigned short mode)
301 {
302 if (mode) {
303 - spec->mode = DIFF_FILE_CANON_MODE(mode);
303 + spec->mode = canon_mode(mode);
304 memcpy(spec->sha1, sha1, 20);
305 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
306 }
diff.h
+1 -11
@@ -4,17 +4,7 @@
4 #ifndef DIFF_H
5 #define DIFF_H
6
7 -#define DIFF_FILE_CANON_MODE(mode) \
8 - (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
9 - S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
10 -
11 -struct tree_desc {
12 - void *buf;
13 - unsigned long size;
14 -};
15 -
16 -extern void update_tree_entry(struct tree_desc *);
17 -extern const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
7 +#include "tree-walk.h"
8
9 struct diff_options;
10
merge-tree.c
+1 -95
@@ -1,51 +1,11 @@
1 #include "cache.h"
2 -#include "diff.h"
2 +#include "tree-walk.h"
3
4 static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
5 static int resolve_directories = 1;
6
7 static void merge_trees(struct tree_desc t[3], const char *base);
8
9 -static void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
10 -{
11 - unsigned long size = 0;
12 - void *buf = NULL;
13 -
14 - if (sha1) {
15 - buf = read_object_with_reference(sha1, "tree", &size, NULL);
16 - if (!buf)
17 - die("unable to read tree %s", sha1_to_hex(sha1));
18 - }
19 - desc->size = size;
20 - desc->buf = buf;
21 - return buf;
22 -}
23 -
24 -struct name_entry {
25 - const unsigned char *sha1;
26 - const char *path;
27 - unsigned int mode;
28 - int pathlen;
29 -};
30 -
31 -static void entry_clear(struct name_entry *a)
32 -{
33 - memset(a, 0, sizeof(*a));
34 -}
35 -
36 -static int entry_compare(struct name_entry *a, struct name_entry *b)
37 -{
38 - return base_name_compare(
39 - a->path, a->pathlen, a->mode,
40 - b->path, b->pathlen, b->mode);
41 -}
42 -
43 -static void entry_extract(struct tree_desc *t, struct name_entry *a)
44 -{
45 - a->sha1 = tree_entry_extract(t, &a->path, &a->mode);
46 - a->pathlen = strlen(a->path);
47 -}
48 -
9 /* An empty entry never compares same, not even to another empty entry */
10 static int same_entry(struct name_entry *a, struct name_entry *b)
11 {
@@ -125,60 +85,6 @@ static void unresolved(const char *base, struct name_entry n[3])
85 printf("3 %06o %s %s%s\n", n[2].mode, sha1_to_hex(n[2].sha1), base, n[2].path);
86 }
87
128 -typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
129 -
130 -static void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
131 -{
132 - struct name_entry *entry = xmalloc(n*sizeof(*entry));
133 -
134 - for (;;) {
135 - struct name_entry entry[3];
136 - unsigned long mask = 0;
137 - int i, last;
138 -
139 - last = -1;
140 - for (i = 0; i < n; i++) {
141 - if (!t[i].size)
142 - continue;
143 - entry_extract(t+i, entry+i);
144 - if (last >= 0) {
145 - int cmp = entry_compare(entry+i, entry+last);
146 -
147 - /*
148 - * Is the new name bigger than the old one?
149 - * Ignore it
150 - */
151 - if (cmp > 0)
152 - continue;
153 - /*
154 - * Is the new name smaller than the old one?
155 - * Ignore all old ones
156 - */
157 - if (cmp < 0)
158 - mask = 0;
159 - }
160 - mask |= 1ul << i;
161 - last = i;
162 - }
163 - if (!mask)
164 - break;
165 -
166 - /*
167 - * Update the tree entries we've walked, and clear
168 - * all the unused name-entries.
169 - */
170 - for (i = 0; i < n; i++) {
171 - if (mask & (1ul << i)) {
172 - update_tree_entry(t+i);
173 - continue;
174 - }
175 - entry_clear(entry + i);
176 - }
177 - callback(n, mask, entry, base);
178 - }
179 - free(entry);
180 -}
181 -
88 /*
89 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
90 * as the origin.
pack-objects.c
+1 -1
@@ -3,7 +3,7 @@
3 #include "delta.h"
4 #include "pack.h"
5 #include "csum-file.h"
6 -#include "diff.h"
6 +#include "tree-walk.h"
7 #include <sys/time.h>
8 #include <signal.h>
9
rev-list.c
+1 -1
@@ -4,7 +4,7 @@
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 -#include "diff.h"
7 +#include "tree-walk.h"
8 #include "revision.h"
9
10 /* bits #0-5 in revision.h */
tar-tree.c
+1 -1
@@ -3,7 +3,7 @@
3 */
4 #include <time.h>
5 #include "cache.h"
6 -#include "diff.h"
6 +#include "tree-walk.h"
7 #include "commit.h"
8 #include "strbuf.h"
9 #include "tar.h"
tree-diff.c
-28
@@ -9,34 +9,6 @@ static int nr_paths = 0;
9 static const char **paths = NULL;
10 static int *pathlens = NULL;
11
12 -void update_tree_entry(struct tree_desc *desc)
13 -{
14 - void *buf = desc->buf;
15 - unsigned long size = desc->size;
16 - int len = strlen(buf) + 1 + 20;
17 -
18 - if (size < len)
19 - die("corrupt tree file");
20 - desc->buf = buf + len;
21 - desc->size = size - len;
22 -}
23 -
24 -const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
25 -{
26 - void *tree = desc->buf;
27 - unsigned long size = desc->size;
28 - int len = strlen(tree)+1;
29 - const unsigned char *sha1 = tree + len;
30 - const char *path = strchr(tree, ' ');
31 - unsigned int mode;
32 -
33 - if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
34 - die("corrupt tree file");
35 - *pathp = path+1;
36 - *modep = DIFF_FILE_CANON_MODE(mode);
37 - return sha1;
38 -}
39 -
12 static char *malloc_base(const char *base, const char *path, int pathlen)
13 {
14 int baselen = strlen(base);
tree-walk.c new
+116
@@ -0,0 +1,116 @@
1 +#include "cache.h"
2 +#include "tree-walk.h"
3 +
4 +void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
5 +{
6 + unsigned long size = 0;
7 + void *buf = NULL;
8 +
9 + if (sha1) {
10 + buf = read_object_with_reference(sha1, "tree", &size, NULL);
11 + if (!buf)
12 + die("unable to read tree %s", sha1_to_hex(sha1));
13 + }
14 + desc->size = size;
15 + desc->buf = buf;
16 + return buf;
17 +}
18 +
19 +static int entry_compare(struct name_entry *a, struct name_entry *b)
20 +{
21 + return base_name_compare(
22 + a->path, a->pathlen, a->mode,
23 + b->path, b->pathlen, b->mode);
24 +}
25 +
26 +static void entry_clear(struct name_entry *a)
27 +{
28 + memset(a, 0, sizeof(*a));
29 +}
30 +
31 +static void entry_extract(struct tree_desc *t, struct name_entry *a)
32 +{
33 + a->sha1 = tree_entry_extract(t, &a->path, &a->mode);
34 + a->pathlen = strlen(a->path);
35 +}
36 +
37 +void update_tree_entry(struct tree_desc *desc)
38 +{
39 + void *buf = desc->buf;
40 + unsigned long size = desc->size;
41 + int len = strlen(buf) + 1 + 20;
42 +
43 + if (size < len)
44 + die("corrupt tree file");
45 + desc->buf = buf + len;
46 + desc->size = size - len;
47 +}
48 +
49 +const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
50 +{
51 + void *tree = desc->buf;
52 + unsigned long size = desc->size;
53 + int len = strlen(tree)+1;
54 + const unsigned char *sha1 = tree + len;
55 + const char *path = strchr(tree, ' ');
56 + unsigned int mode;
57 +
58 + if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
59 + die("corrupt tree file");
60 + *pathp = path+1;
61 + *modep = canon_mode(mode);
62 + return sha1;
63 +}
64 +
65 +void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
66 +{
67 + struct name_entry *entry = xmalloc(n*sizeof(*entry));
68 +
69 + for (;;) {
70 + struct name_entry entry[3];
71 + unsigned long mask = 0;
72 + int i, last;
73 +
74 + last = -1;
75 + for (i = 0; i < n; i++) {
76 + if (!t[i].size)
77 + continue;
78 + entry_extract(t+i, entry+i);
79 + if (last >= 0) {
80 + int cmp = entry_compare(entry+i, entry+last);
81 +
82 + /*
83 + * Is the new name bigger than the old one?
84 + * Ignore it
85 + */
86 + if (cmp > 0)
87 + continue;
88 + /*
89 + * Is the new name smaller than the old one?
90 + * Ignore all old ones
91 + */
92 + if (cmp < 0)
93 + mask = 0;
94 + }
95 + mask |= 1ul << i;
96 + last = i;
97 + }
98 + if (!mask)
99 + break;
100 +
101 + /*
102 + * Update the tree entries we've walked, and clear
103 + * all the unused name-entries.
104 + */
105 + for (i = 0; i < n; i++) {
106 + if (mask & (1ul << i)) {
107 + update_tree_entry(t+i);
108 + continue;
109 + }
110 + entry_clear(entry + i);
111 + }
112 + callback(n, mask, entry, base);
113 + }
114 + free(entry);
115 +}
116 +
tree-walk.h new
+25
@@ -0,0 +1,25 @@
1 +#ifndef TREE_WALK_H
2 +#define TREE_WALK_H
3 +
4 +struct tree_desc {
5 + void *buf;
6 + unsigned long size;
7 +};
8 +
9 +struct name_entry {
10 + const unsigned char *sha1;
11 + const char *path;
12 + unsigned int mode;
13 + int pathlen;
14 +};
15 +
16 +void update_tree_entry(struct tree_desc *);
17 +const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
18 +
19 +void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
20 +
21 +typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
22 +
23 +void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback);
24 +
25 +#endif