summaryrefslogtreecommitdiff
path: root/attr.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2007-08-14 08:41:02 (GMT)
committerJunio C Hamano <gitster@pobox.com>2007-08-15 06:19:10 (GMT)
commit1a9d7e9b484e77436edc7f5cacd39c24ec605e6d (patch)
tree3492597bfe86cd1f650d78fa21cf7dbe0e5bd91f /attr.c
parenta44131181a13eb599ed35647709692b1699706eb (diff)
downloadgit-1a9d7e9b484e77436edc7f5cacd39c24ec605e6d.zip
git-1a9d7e9b484e77436edc7f5cacd39c24ec605e6d.tar.gz
git-1a9d7e9b484e77436edc7f5cacd39c24ec605e6d.tar.bz2
attr.c: read .gitattributes from index as well.
This makes .gitattributes files to be read from the index when they are not checked out to the work tree. This is in line with the way we always allowed low-level tools to operate in sparsely checked out work tree in a reasonable way. It swaps the order of new file creation and converting the blob to work tree representation; otherwise when we are in the middle of checking out .gitattributes we would notice an empty but unwritten .gitattributes file in the work tree and will ignore the copy in the index. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'attr.c')
-rw-r--r--attr.c61
1 files changed, 59 insertions, 2 deletions
diff --git a/attr.c b/attr.c
index 8d778f1..1293993 100644
--- a/attr.c
+++ b/attr.c
@@ -336,13 +336,70 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
return res;
}
+static void *read_index_data(const char *path)
+{
+ int pos, len;
+ unsigned long sz;
+ enum object_type type;
+ void *data;
+
+ len = strlen(path);
+ pos = cache_name_pos(path, len);
+ if (pos < 0) {
+ /*
+ * We might be in the middle of a merge, in which
+ * case we would read stage #2 (ours).
+ */
+ int i;
+ for (i = -pos - 1;
+ (pos < 0 && i < active_nr &&
+ !strcmp(active_cache[i]->name, path));
+ i++)
+ if (ce_stage(active_cache[i]) == 2)
+ pos = i;
+ }
+ if (pos < 0)
+ return NULL;
+ data = read_sha1_file(active_cache[pos]->sha1, &type, &sz);
+ if (!data || type != OBJ_BLOB) {
+ free(data);
+ return NULL;
+ }
+ return data;
+}
+
static struct attr_stack *read_attr(const char *path, int macro_ok)
{
struct attr_stack *res;
+ char *buf, *sp;
+ int lineno = 0;
res = read_attr_from_file(path, macro_ok);
- if (!res)
- res = xcalloc(1, sizeof(*res));
+ if (res)
+ return res;
+
+ res = xcalloc(1, sizeof(*res));
+
+ /*
+ * There is no checked out .gitattributes file there, but
+ * we might have it in the index. We allow operation in a
+ * sparsely checked out work tree, so read from it.
+ */
+ buf = read_index_data(path);
+ if (!buf)
+ return res;
+
+ for (sp = buf; *sp; ) {
+ char *ep;
+ int more;
+ for (ep = sp; *ep && *ep != '\n'; ep++)
+ ;
+ more = (*ep == '\n');
+ *ep = '\0';
+ handle_attr_line(res, sp, path, ++lineno, macro_ok);
+ sp = ep + more;
+ }
+ free(buf);
return res;
}