LIB_H = \
blob.h cache.h commit.h csum-file.h delta.h \
diff.h object.h pack.h pkt-line.h quote.h refs.h \
- run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h
+ run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
+ tree-walk.h
DIFF_OBJS = \
diff.o diffcore-break.o diffcore-order.o diffcore-pathspec.o \
quote.o read-cache.o refs.o run-command.o \
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
- fetch-clone.o revision.o pager.o \
+ fetch-clone.o revision.o pager.o tree-walk.o \
$(DIFF_OBJS)
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
return htonl(S_IFLNK);
return htonl(S_IFREG | ce_permissions(mode));
}
+#define canon_mode(mode) \
+ (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
+ S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
#define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
int len = st.st_size;
int cnt = 0;
- elem->mode = DIFF_FILE_CANON_MODE(st.st_mode);
+ elem->mode = canon_mode(st.st_mode);
size = len;
result = xmalloc(len + 1);
while (cnt < len) {
memcpy(combine.p.parent[stage-2].sha1,
nce->sha1, 20);
combine.p.parent[stage-2].mode =
- DIFF_FILE_CANON_MODE(mode);
+ canon_mode(mode);
combine.p.parent[stage-2].status =
DIFF_STATUS_MODIFIED;
}
continue;
oldmode = ntohl(ce->ce_mode);
- newmode = DIFF_FILE_CANON_MODE(st.st_mode);
+ newmode = canon_mode(st.st_mode);
if (!trust_executable_bit &&
S_ISREG(newmode) && S_ISREG(oldmode) &&
((newmode ^ oldmode) == 0111))
unsigned short mode)
{
if (mode) {
- spec->mode = DIFF_FILE_CANON_MODE(mode);
+ spec->mode = canon_mode(mode);
memcpy(spec->sha1, sha1, 20);
spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
}
#ifndef DIFF_H
#define DIFF_H
-#define DIFF_FILE_CANON_MODE(mode) \
- (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
- S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
-
-struct tree_desc {
- void *buf;
- unsigned long size;
-};
-
-extern void update_tree_entry(struct tree_desc *);
-extern const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
+#include "tree-walk.h"
struct diff_options;
#include "cache.h"
-#include "diff.h"
+#include "tree-walk.h"
static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
static int resolve_directories = 1;
static void merge_trees(struct tree_desc t[3], const char *base);
-static void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
-{
- unsigned long size = 0;
- void *buf = NULL;
-
- if (sha1) {
- buf = read_object_with_reference(sha1, "tree", &size, NULL);
- if (!buf)
- die("unable to read tree %s", sha1_to_hex(sha1));
- }
- desc->size = size;
- desc->buf = buf;
- return buf;
-}
-
-struct name_entry {
- const unsigned char *sha1;
- const char *path;
- unsigned int mode;
- int pathlen;
-};
-
-static void entry_clear(struct name_entry *a)
-{
- memset(a, 0, sizeof(*a));
-}
-
-static int entry_compare(struct name_entry *a, struct name_entry *b)
-{
- return base_name_compare(
- a->path, a->pathlen, a->mode,
- b->path, b->pathlen, b->mode);
-}
-
-static void entry_extract(struct tree_desc *t, struct name_entry *a)
-{
- a->sha1 = tree_entry_extract(t, &a->path, &a->mode);
- a->pathlen = strlen(a->path);
-}
-
/* An empty entry never compares same, not even to another empty entry */
static int same_entry(struct name_entry *a, struct name_entry *b)
{
printf("3 %06o %s %s%s\n", n[2].mode, sha1_to_hex(n[2].sha1), base, n[2].path);
}
-typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
-
-static void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
-{
- struct name_entry *entry = xmalloc(n*sizeof(*entry));
-
- for (;;) {
- struct name_entry entry[3];
- unsigned long mask = 0;
- int i, last;
-
- last = -1;
- for (i = 0; i < n; i++) {
- if (!t[i].size)
- continue;
- entry_extract(t+i, entry+i);
- if (last >= 0) {
- int cmp = entry_compare(entry+i, entry+last);
-
- /*
- * Is the new name bigger than the old one?
- * Ignore it
- */
- if (cmp > 0)
- continue;
- /*
- * Is the new name smaller than the old one?
- * Ignore all old ones
- */
- if (cmp < 0)
- mask = 0;
- }
- mask |= 1ul << i;
- last = i;
- }
- if (!mask)
- break;
-
- /*
- * Update the tree entries we've walked, and clear
- * all the unused name-entries.
- */
- for (i = 0; i < n; i++) {
- if (mask & (1ul << i)) {
- update_tree_entry(t+i);
- continue;
- }
- entry_clear(entry + i);
- }
- callback(n, mask, entry, base);
- }
- free(entry);
-}
-
/*
* Merge two trees together (t[1] and t[2]), using a common base (t[0])
* as the origin.
#include "delta.h"
#include "pack.h"
#include "csum-file.h"
-#include "diff.h"
+#include "tree-walk.h"
#include <sys/time.h>
#include <signal.h>
#include "commit.h"
#include "tree.h"
#include "blob.h"
-#include "diff.h"
+#include "tree-walk.h"
#include "revision.h"
/* bits #0-5 in revision.h */
*/
#include <time.h>
#include "cache.h"
-#include "diff.h"
+#include "tree-walk.h"
#include "commit.h"
#include "strbuf.h"
#include "tar.h"
static const char **paths = NULL;
static int *pathlens = NULL;
-void update_tree_entry(struct tree_desc *desc)
-{
- void *buf = desc->buf;
- unsigned long size = desc->size;
- int len = strlen(buf) + 1 + 20;
-
- if (size < len)
- die("corrupt tree file");
- desc->buf = buf + len;
- desc->size = size - len;
-}
-
-const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
-{
- void *tree = desc->buf;
- unsigned long size = desc->size;
- int len = strlen(tree)+1;
- const unsigned char *sha1 = tree + len;
- const char *path = strchr(tree, ' ');
- unsigned int mode;
-
- if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
- die("corrupt tree file");
- *pathp = path+1;
- *modep = DIFF_FILE_CANON_MODE(mode);
- return sha1;
-}
-
static char *malloc_base(const char *base, const char *path, int pathlen)
{
int baselen = strlen(base);
--- /dev/null
+#include "cache.h"
+#include "tree-walk.h"
+
+void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
+{
+ unsigned long size = 0;
+ void *buf = NULL;
+
+ if (sha1) {
+ buf = read_object_with_reference(sha1, "tree", &size, NULL);
+ if (!buf)
+ die("unable to read tree %s", sha1_to_hex(sha1));
+ }
+ desc->size = size;
+ desc->buf = buf;
+ return buf;
+}
+
+static int entry_compare(struct name_entry *a, struct name_entry *b)
+{
+ return base_name_compare(
+ a->path, a->pathlen, a->mode,
+ b->path, b->pathlen, b->mode);
+}
+
+static void entry_clear(struct name_entry *a)
+{
+ memset(a, 0, sizeof(*a));
+}
+
+static void entry_extract(struct tree_desc *t, struct name_entry *a)
+{
+ a->sha1 = tree_entry_extract(t, &a->path, &a->mode);
+ a->pathlen = strlen(a->path);
+}
+
+void update_tree_entry(struct tree_desc *desc)
+{
+ void *buf = desc->buf;
+ unsigned long size = desc->size;
+ int len = strlen(buf) + 1 + 20;
+
+ if (size < len)
+ die("corrupt tree file");
+ desc->buf = buf + len;
+ desc->size = size - len;
+}
+
+const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
+{
+ void *tree = desc->buf;
+ unsigned long size = desc->size;
+ int len = strlen(tree)+1;
+ const unsigned char *sha1 = tree + len;
+ const char *path = strchr(tree, ' ');
+ unsigned int mode;
+
+ if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
+ die("corrupt tree file");
+ *pathp = path+1;
+ *modep = canon_mode(mode);
+ return sha1;
+}
+
+void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
+{
+ struct name_entry *entry = xmalloc(n*sizeof(*entry));
+
+ for (;;) {
+ struct name_entry entry[3];
+ unsigned long mask = 0;
+ int i, last;
+
+ last = -1;
+ for (i = 0; i < n; i++) {
+ if (!t[i].size)
+ continue;
+ entry_extract(t+i, entry+i);
+ if (last >= 0) {
+ int cmp = entry_compare(entry+i, entry+last);
+
+ /*
+ * Is the new name bigger than the old one?
+ * Ignore it
+ */
+ if (cmp > 0)
+ continue;
+ /*
+ * Is the new name smaller than the old one?
+ * Ignore all old ones
+ */
+ if (cmp < 0)
+ mask = 0;
+ }
+ mask |= 1ul << i;
+ last = i;
+ }
+ if (!mask)
+ break;
+
+ /*
+ * Update the tree entries we've walked, and clear
+ * all the unused name-entries.
+ */
+ for (i = 0; i < n; i++) {
+ if (mask & (1ul << i)) {
+ update_tree_entry(t+i);
+ continue;
+ }
+ entry_clear(entry + i);
+ }
+ callback(n, mask, entry, base);
+ }
+ free(entry);
+}
+
--- /dev/null
+#ifndef TREE_WALK_H
+#define TREE_WALK_H
+
+struct tree_desc {
+ void *buf;
+ unsigned long size;
+};
+
+struct name_entry {
+ const unsigned char *sha1;
+ const char *path;
+ unsigned int mode;
+ int pathlen;
+};
+
+void update_tree_entry(struct tree_desc *);
+const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
+
+void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
+
+typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
+
+void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback);
+
+#endif