*/
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
+#include "config.h"
#include "tempfile.h"
#include "lockfile.h"
#include "cache-tree.h"
istate->cache_nr--;
if (pos >= istate->cache_nr)
return 0;
- memmove(istate->cache + pos,
- istate->cache + pos + 1,
- (istate->cache_nr - pos) * sizeof(struct cache_entry *));
+ MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
+ istate->cache_nr - pos);
return 1;
}
{
int i;
- unshare_split_index(istate, 1);
- for (i = 0; i < istate->cache_nr; i++)
+ for (i = 0; i < istate->cache_nr; i++) {
+ if (istate->cache[i]->index &&
+ istate->split_index &&
+ istate->split_index->base &&
+ istate->cache[i]->index <= istate->split_index->base->cache_nr &&
+ istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index - 1])
+ continue;
free(istate->cache[i]);
+ }
resolve_undo_clear_index(istate);
istate->cache_nr = 0;
istate->cache_changed = 0;
free_name_hash(istate);
cache_tree_free(&(istate->cache_tree));
istate->initialized = 0;
- free(istate->cache);
- istate->cache = NULL;
+ FREE_AND_NULL(istate->cache);
istate->cache_alloc = 0;
discard_split_index(istate);
free_untracked_cache(istate->untracked);
rollback_lock_file(lockfile);
}
-static int do_write_index(struct index_state *istate, int newfd,
+static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
int strip_extensions)
{
+ int newfd = tempfile->fd;
git_SHA_CTX c;
struct cache_header hdr;
int i, err, removed, extended, hdr_version;
return -1;
}
- if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
+ if (ce_flush(&c, newfd, istate->sha1))
+ return -1;
+ if (close_tempfile(tempfile))
+ return error(_("could not close '%s'"), tempfile->filename.buf);
+ if (stat(tempfile->filename.buf, &st))
return -1;
istate->timestamp.sec = (unsigned int)st.st_mtime;
istate->timestamp.nsec = ST_MTIME_NSEC(st);
static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
unsigned flags)
{
- int ret = do_write_index(istate, get_lock_file_fd(lock), 0);
+ int ret = do_write_index(istate, &lock->tempfile, 0);
if (ret)
return ret;
assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
return do_write_locked_index(istate, lock, flags);
}
move_cache_to_base_index(istate);
- ret = do_write_index(si->base, fd, 1);
+ ret = do_write_index(si->base, &temporary_sharedindex, 1);
+ if (ret) {
+ delete_tempfile(&temporary_sharedindex);
+ return ret;
+ }
+ ret = adjust_shared_perm(get_tempfile_path(&temporary_sharedindex));
if (ret) {
+ int save_errno = errno;
+ error("cannot fix permission bits on %s", get_tempfile_path(&temporary_sharedindex));
delete_tempfile(&temporary_sharedindex);
+ errno = save_errno;
return ret;
}
ret = rename_tempfile(&temporary_sharedindex,
void stat_validity_clear(struct stat_validity *sv)
{
- free(sv->sd);
- sv->sd = NULL;
+ FREE_AND_NULL(sv->sd);
}
int stat_validity_check(struct stat_validity *sv, const char *path)