@matheusbsbs / matheus-bagliano / commits / f35a6d3bce
Fork 0

Teach core object handling functions about gitlinks

This teaches the really fundamental core SHA1 object handling routines about gitlinks. We can compare trees with gitlinks in them (although we can not actually generate patches for them yet - just raw git diffs), and they show up as commits in "git ls-tree". We also know to compare gitlinks as if they were directories (ie the normal "sort as trees" rules apply). [jc: amended a cut&paste error] Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Apr 9, 2007 at 21:20 UTC f35a6d3bce79c2995bbf0a3bd9fcad29e54a8d3c
4 files changed +54 -6
builtin-ls-tree.c
+19 -1
@@ -6,6 +6,7 @@
6 #include "cache.h"
7 #include "blob.h"
8 #include "tree.h"
9 +#include "commit.h"
10 #include "quote.h"
11 #include "builtin.h"
12
@@ -59,7 +60,24 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
60 int retval = 0;
61 const char *type = blob_type;
62
62 - if (S_ISDIR(mode)) {
63 + if (S_ISDIRLNK(mode)) {
64 + /*
65 + * Maybe we want to have some recursive version here?
66 + *
67 + * Something like:
68 + *
69 + if (show_subprojects(base, baselen, pathname)) {
70 + if (fork()) {
71 + chdir(base);
72 + exec ls-tree;
73 + }
74 + waitpid();
75 + }
76 + *
77 + * ..or similar..
78 + */
79 + type = commit_type;
80 + } else if (S_ISDIR(mode)) {
81 if (show_recursive(base, baselen, pathname)) {
82 retval = READ_TREE_RECURSIVE;
83 if (!(ls_options & LS_SHOW_TREES))
cache-tree.c
+1 -1
@@ -326,7 +326,7 @@ static int update_one(struct cache_tree *it,
326 mode = ntohl(ce->ce_mode);
327 entlen = pathlen - baselen;
328 }
329 - if (!missing_ok && !has_sha1_file(sha1))
329 + if (mode != S_IFDIRLNK && !missing_ok && !has_sha1_file(sha1))
330 return error("invalid object %s", sha1_to_hex(sha1));
331
332 if (!ce->ce_mode)
read-cache.c
+31 -4
@@ -5,6 +5,7 @@
5 */
6 #include "cache.h"
7 #include "cache-tree.h"
8 +#include "refs.h"
9
10 /* Index extensions.
11 *
@@ -91,6 +92,23 @@ static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
92 return match;
93 }
94
95 +static int ce_compare_gitlink(struct cache_entry *ce)
96 +{
97 + unsigned char sha1[20];
98 +
99 + /*
100 + * We don't actually require that the .git directory
101 + * under DIRLNK directory be a valid git directory. It
102 + * might even be missing (in case nobody populated that
103 + * sub-project).
104 + *
105 + * If so, we consider it always to match.
106 + */
107 + if (resolve_gitlink_ref(ce->name, "HEAD", sha1) < 0)
108 + return 0;
109 + return hashcmp(sha1, ce->sha1);
110 +}
111 +
112 static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
113 {
114 switch (st->st_mode & S_IFMT) {
@@ -102,6 +120,9 @@ static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
120 if (ce_compare_link(ce, xsize_t(st->st_size)))
121 return DATA_CHANGED;
122 break;
123 + case S_IFDIRLNK:
124 + /* No need to do anything, we did the exact compare in "match_stat_basic" */
125 + break;
126 default:
127 return TYPE_CHANGED;
128 }
@@ -127,6 +148,12 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
148 (has_symlinks || !S_ISREG(st->st_mode)))
149 changed |= TYPE_CHANGED;
150 break;
151 + case S_IFDIRLNK:
152 + if (!S_ISDIR(st->st_mode))
153 + changed |= TYPE_CHANGED;
154 + else if (ce_compare_gitlink(ce))
155 + changed |= DATA_CHANGED;
156 + break;
157 default:
158 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
159 }
@@ -250,9 +277,9 @@ int base_name_compare(const char *name1, int len1, int mode1,
277 return cmp;
278 c1 = name1[len];
279 c2 = name2[len];
253 - if (!c1 && S_ISDIR(mode1))
280 + if (!c1 && (S_ISDIR(mode1) || S_ISDIRLNK(mode1)))
281 c1 = '/';
255 - if (!c2 && S_ISDIR(mode2))
282 + if (!c2 && (S_ISDIR(mode2) || S_ISDIRLNK(mode2)))
283 c2 = '/';
284 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
285 }
@@ -334,8 +361,8 @@ int add_file_to_cache(const char *path, int verbose)
361 if (lstat(path, &st))
362 die("%s: unable to stat (%s)", path, strerror(errno));
363
337 - if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
338 - die("%s: can only add regular files or symbolic links", path);
364 + if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
365 + die("%s: can only add regular files, symbolic links or git-directories", path);
366
367 namelen = strlen(path);
368 size = cache_entry_size(namelen);
sha1_file.c
+3
@@ -13,6 +13,7 @@
13 #include "commit.h"
14 #include "tag.h"
15 #include "tree.h"
16 +#include "refs.h"
17
18 #ifndef O_NOATIME
19 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
@@ -2332,6 +2333,8 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2333 path);
2334 free(target);
2335 break;
2336 + case S_IFDIR:
2337 + return resolve_gitlink_ref(path, "HEAD", sha1);
2338 default:
2339 return error("%s: unsupported file type", path);
2340 }