14
#include "resolve-undo.h"
15
#include "strbuf.h"
16
#include "varint.h"
17
+#include "split-index.h"
18
19
static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
20
unsigned int options);
35
#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
36
#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
37
#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
38
+#define CACHE_EXT_LINK 0x6c696e6b /* "link" */
39
+
40
+/* changes that can be kept in $GIT_DIR/index (basically all extensions) */
41
+#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED)
42
43
struct index_state the_index;
44
static const char *alternate_index_output;
68
copy_cache_entry(new, old);
69
new->ce_flags &= ~CE_HASHED;
70
new->ce_namelen = namelen;
71
+ new->index = 0;
72
memcpy(new->name, new_name, namelen + 1);
73
74
cache_tree_invalidate_path(istate, old->name);
1341
case CACHE_EXT_RESOLVE_UNDO:
1342
istate->resolve_undo = resolve_undo_read(data, sz);
1343
break;
1344
+ case CACHE_EXT_LINK:
1345
+ if (read_link_extension(istate, data, sz))
1346
+ return -1;
1347
+ break;
1348
default:
1349
if (*ext < 'A' || 'Z' < *ext)
1350
return error("index uses %.4s extension, which we do not understand",
1379
ce->ce_stat_data.sd_size = get_be32(&ondisk->size);
1380
ce->ce_flags = flags & ~CE_NAMEMASK;
1381
ce->ce_namelen = len;
1382
+ ce->index = 0;
1383
hashcpy(ce->sha1, ondisk->sha1);
1384
memcpy(ce->name, name, len);
1385
ce->name[len] = '\0';
1454
}
1455
1456
/* remember to discard_cache() before reading a different cache! */
1446
-int read_index_from(struct index_state *istate, const char *path)
1457
+static int do_read_index(struct index_state *istate, const char *path,
1458
+ int must_exist)
1459
{
1460
int fd, i;
1461
struct stat st;
1472
istate->timestamp.nsec = 0;
1473
fd = open(path, O_RDONLY);
1474
if (fd < 0) {
1463
- if (errno == ENOENT)
1475
+ if (!must_exist && errno == ENOENT)
1476
return 0;
1465
- die_errno("index file open failed");
1477
+ die_errno("%s: index file open failed", path);
1478
}
1479
1480
if (fstat(fd, &st))
1547
die("index file corrupt");
1548
}
1549
1550
+int read_index_from(struct index_state *istate, const char *path)
1551
+{
1552
+ struct split_index *split_index;
1553
+ int ret;
1554
+
1555
+ /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
1556
+ if (istate->initialized)
1557
+ return istate->cache_nr;
1558
+
1559
+ ret = do_read_index(istate, path, 0);
1560
+ split_index = istate->split_index;
1561
+ if (!split_index)
1562
+ return ret;
1563
+
1564
+ if (is_null_sha1(split_index->base_sha1))
1565
+ return ret;
1566
+ if (istate->cache_nr)
1567
+ die("index in split-index mode must contain no entries");
1568
+
1569
+ if (split_index->base)
1570
+ discard_index(split_index->base);
1571
+ else
1572
+ split_index->base = xcalloc(1, sizeof(*split_index->base));
1573
+ ret = do_read_index(split_index->base,
1574
+ git_path("sharedindex.%s",
1575
+ sha1_to_hex(split_index->base_sha1)), 1);
1576
+ if (hashcmp(split_index->base_sha1, split_index->base->sha1))
1577
+ die("broken index, expect %s in %s, got %s",
1578
+ sha1_to_hex(split_index->base_sha1),
1579
+ git_path("sharedindex.%s",
1580
+ sha1_to_hex(split_index->base_sha1)),
1581
+ sha1_to_hex(split_index->base->sha1));
1582
+ merge_base_index(istate);
1583
+ return ret;
1584
+}
1585
+
1586
int is_index_unborn(struct index_state *istate)
1587
{
1588
return (!istate->cache_nr && !istate->timestamp.sec);
1592
{
1593
int i;
1594
1547
- for (i = 0; i < istate->cache_nr; i++)
1595
+ for (i = 0; i < istate->cache_nr; i++) {
1596
+ if (istate->cache[i]->index &&
1597
+ istate->split_index &&
1598
+ istate->split_index->base &&
1599
+ istate->cache[i]->index <= istate->split_index->base->cache_nr &&
1600
+ istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index - 1])
1601
+ continue;
1602
free(istate->cache[i]);
1603
+ }
1604
resolve_undo_clear_index(istate);
1605
istate->cache_nr = 0;
1606
istate->cache_changed = 0;
1612
free(istate->cache);
1613
istate->cache = NULL;
1614
istate->cache_alloc = 0;
1615
+ discard_split_index(istate);
1616
return 0;
1617
}
1618
1908
strbuf_release(&previous_name_buf);
1909
1910
/* Write extension data here */
1911
+ if (istate->split_index) {
1912
+ struct strbuf sb = STRBUF_INIT;
1913
+
1914
+ err = write_link_extension(&sb, istate) < 0 ||
1915
+ write_index_ext_header(&c, newfd, CACHE_EXT_LINK,
1916
+ sb.len) < 0 ||
1917
+ ce_write(&c, newfd, sb.buf, sb.len) < 0;
1918
+ strbuf_release(&sb);
1919
+ if (err)
1920
+ return -1;
1921
+ }
1922
if (istate->cache_tree) {
1923
struct strbuf sb = STRBUF_INIT;
1924
1983
return ret;
1984
}
1985
1986
+static int write_split_index(struct index_state *istate,
1987
+ struct lock_file *lock,
1988
+ unsigned flags)
1989
+{
1990
+ int ret;
1991
+ prepare_to_write_split_index(istate);
1992
+ ret = do_write_locked_index(istate, lock, flags);
1993
+ finish_writing_split_index(istate);
1994
+ return ret;
1995
+}
1996
+
1997
int write_locked_index(struct index_state *istate, struct lock_file *lock,
1998
unsigned flags)
1999
{
1922
- return do_write_locked_index(istate, lock, flags);
2000
+ struct split_index *si = istate->split_index;
2001
+
2002
+ if (!si || (istate->cache_changed & ~EXTMASK)) {
2003
+ if (si)
2004
+ hashclr(si->base_sha1);
2005
+ return do_write_locked_index(istate, lock, flags);
2006
+ }
2007
+
2008
+ return write_split_index(istate, lock, flags);
2009
}
2010
2011
/*