#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
struct index_state the_index;
+static const char *alternate_index_output;
static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
{
remove_name_hash(istate, old);
free(old);
set_index_entry(istate, nr, ce);
- istate->cache_changed = 1;
+ istate->cache_changed |= CE_ENTRY_CHANGED;
}
void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
record_resolve_undo(istate, ce);
remove_name_hash(istate, ce);
free(ce);
- istate->cache_changed = 1;
+ istate->cache_changed |= CE_ENTRY_REMOVED;
istate->cache_nr--;
if (pos >= istate->cache_nr)
return 0;
else
ce_array[j++] = ce_array[i];
}
- istate->cache_changed = 1;
+ if (j == istate->cache_nr)
+ return;
+ istate->cache_changed |= CE_ENTRY_REMOVED;
istate->cache_nr = j;
}
istate->cache + pos,
(istate->cache_nr - pos - 1) * sizeof(ce));
set_index_entry(istate, pos, ce);
- istate->cache_changed = 1;
+ istate->cache_changed |= CE_ENTRY_ADDED;
return 0;
}
!(ce->ce_flags & CE_VALID))
updated->ce_flags &= ~CE_VALID;
+ /* istate->cache_changed is updated in the caller */
return updated;
}
* means the index is not valid anymore.
*/
ce->ce_flags &= ~CE_VALID;
- istate->cache_changed = 1;
+ istate->cache_changed |= CE_ENTRY_CHANGED;
}
if (quiet)
continue;
ondisk->size = htonl(ce->ce_stat_data.sd_size);
hashcpy(ondisk->sha1, ce->sha1);
- flags = ce->ce_flags;
+ flags = ce->ce_flags & ~CE_NAMEMASK;
flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));
ondisk->flags = htons(flags);
if (ce->ce_flags & CE_EXTENDED) {
void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)
{
if ((istate->cache_changed || has_racy_timestamp(istate)) &&
- !write_index(istate, lockfile->fd))
- commit_locked_index(lockfile);
- else
+ write_locked_index(istate, lockfile, COMMIT_LOCK))
rollback_lock_file(lockfile);
}
-int write_index(struct index_state *istate, int newfd)
+static int do_write_index(struct index_state *istate, int newfd)
{
git_SHA_CTX c;
struct cache_header hdr;
return 0;
}
+void set_alternate_index_output(const char *name)
+{
+ alternate_index_output = name;
+}
+
+static int commit_locked_index(struct lock_file *lk)
+{
+ if (alternate_index_output) {
+ if (lk->fd >= 0 && close_lock_file(lk))
+ return -1;
+ if (rename(lk->filename, alternate_index_output))
+ return -1;
+ lk->filename[0] = 0;
+ return 0;
+ } else {
+ return commit_lock_file(lk);
+ }
+}
+
+static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
+ unsigned flags)
+{
+ int ret = do_write_index(istate, lock->fd);
+ if (ret)
+ return ret;
+ assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
+ (COMMIT_LOCK | CLOSE_LOCK));
+ if (flags & COMMIT_LOCK)
+ return commit_locked_index(lock);
+ else if (flags & CLOSE_LOCK)
+ return close_lock_file(lock);
+ else
+ return ret;
+}
+
+int write_locked_index(struct index_state *istate, struct lock_file *lock,
+ unsigned flags)
+{
+ return do_write_locked_index(istate, lock, flags);
+}
+
/*
* Read the index file that is potentially unmerged into given
* index_state, dropping any unmerged entries. Returns true if