Add support for a "GIT_INDEX_FILE" environment variable.
We use that to specify alternative index files, which can be useful if you want to (for example) generate a temporary index file to do some specific operation that you don't want to mess with your main one with. It defaults to the regular ".git/index" if it hasn't been specified.
This commit is contained in:
parent
65bb49144d
commit
bb233d690a
8
cache.h
8
cache.h
@ -11,6 +11,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
#include <sys/param.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
@ -84,6 +85,13 @@ unsigned int active_nr, active_alloc;
|
|||||||
#define DB_ENVIRONMENT "SHA1_FILE_DIRECTORY"
|
#define DB_ENVIRONMENT "SHA1_FILE_DIRECTORY"
|
||||||
#define DEFAULT_DB_ENVIRONMENT ".git/objects"
|
#define DEFAULT_DB_ENVIRONMENT ".git/objects"
|
||||||
|
|
||||||
|
#define get_object_directory() (getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT)
|
||||||
|
|
||||||
|
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
|
||||||
|
#define DEFAULT_INDEX_ENVIRONMENT ".git/index"
|
||||||
|
|
||||||
|
#define get_index_file() (getenv(INDEX_ENVIRONMENT) ? : DEFAULT_INDEX_ENVIRONMENT)
|
||||||
|
|
||||||
#define alloc_nr(x) (((x)+16)*3/2)
|
#define alloc_nr(x) (((x)+16)*3/2)
|
||||||
|
|
||||||
/* Initialize and use the cache information */
|
/* Initialize and use the cache information */
|
||||||
|
@ -32,8 +32,6 @@
|
|||||||
* of "-a" causing problems (not possible in the above example,
|
* of "-a" causing problems (not possible in the above example,
|
||||||
* but get used to it in scripting!).
|
* but get used to it in scripting!).
|
||||||
*/
|
*/
|
||||||
#include <sys/param.h>
|
|
||||||
|
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
|
|
||||||
static int force = 0, quiet = 0;
|
static int force = 0, quiet = 0;
|
||||||
|
@ -188,7 +188,7 @@ int read_cache(void)
|
|||||||
sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
|
sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
|
||||||
if (access(sha1_file_directory, X_OK) < 0)
|
if (access(sha1_file_directory, X_OK) < 0)
|
||||||
return error("no access to SHA1 file directory");
|
return error("no access to SHA1 file directory");
|
||||||
fd = open(".git/index", O_RDONLY);
|
fd = open(get_index_file(), O_RDONLY);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return (errno == ENOENT) ? 0 : error("open failed");
|
return (errno == ENOENT) ? 0 : error("open failed");
|
||||||
|
|
||||||
|
19
read-tree.c
19
read-tree.c
@ -79,12 +79,12 @@ static int read_tree(unsigned char *sha1, const char *base, int baselen)
|
|||||||
return read_tree_recursive(buffer, "tree", size, base, baselen);
|
return read_tree_recursive(buffer, "tree", size, base, baselen);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int remove_lock = 0;
|
static char *lockfile_name;
|
||||||
|
|
||||||
static void remove_lock_file(void)
|
static void remove_lock_file(void)
|
||||||
{
|
{
|
||||||
if (remove_lock)
|
if (lockfile_name)
|
||||||
unlink(".git/index.lock");
|
unlink(lockfile_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int path_matches(struct cache_entry *a, struct cache_entry *b)
|
static int path_matches(struct cache_entry *a, struct cache_entry *b)
|
||||||
@ -223,12 +223,16 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
int i, newfd, merge;
|
int i, newfd, merge;
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
|
static char lockfile[MAXPATHLEN+1];
|
||||||
|
const char *indexfile = get_index_file();
|
||||||
|
|
||||||
newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
|
snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
|
||||||
|
|
||||||
|
newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
|
||||||
if (newfd < 0)
|
if (newfd < 0)
|
||||||
die("unable to create new cachefile");
|
die("unable to create new cachefile");
|
||||||
atexit(remove_lock_file);
|
atexit(remove_lock_file);
|
||||||
remove_lock = 1;
|
lockfile_name = lockfile;
|
||||||
|
|
||||||
merge = 0;
|
merge = 0;
|
||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i++) {
|
||||||
@ -268,9 +272,8 @@ int main(int argc, char **argv)
|
|||||||
die("just how do you expect me to merge %d trees?", stage-1);
|
die("just how do you expect me to merge %d trees?", stage-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (write_cache(newfd, active_cache, active_nr) ||
|
if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
|
||||||
rename(".git/index.lock", ".git/index"))
|
|
||||||
die("unable to write new index file");
|
die("unable to write new index file");
|
||||||
remove_lock = 0;
|
lockfile_name = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
* Copyright (C) Linus Torvalds, 2005
|
* Copyright (C) Linus Torvalds, 2005
|
||||||
*/
|
*/
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/param.h>
|
|
||||||
|
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
|
|
||||||
|
@ -274,25 +274,29 @@ static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
|
|||||||
return add_cache_entry(ce, allow_add);
|
return add_cache_entry(ce, allow_add);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int remove_lock = 0;
|
static const char *lockfile_name = NULL;
|
||||||
|
|
||||||
static void remove_lock_file(void)
|
static void remove_lock_file(void)
|
||||||
{
|
{
|
||||||
if (remove_lock)
|
if (lockfile_name)
|
||||||
unlink(".git/index.lock");
|
unlink(lockfile_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i, newfd, entries;
|
int i, newfd, entries;
|
||||||
int allow_options = 1;
|
int allow_options = 1;
|
||||||
|
static char lockfile[MAXPATHLEN+1];
|
||||||
|
const char *indexfile = get_index_file();
|
||||||
|
|
||||||
newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
|
snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
|
||||||
|
|
||||||
|
newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
|
||||||
if (newfd < 0)
|
if (newfd < 0)
|
||||||
die("unable to create new cachefile");
|
die("unable to create new cachefile");
|
||||||
|
|
||||||
atexit(remove_lock_file);
|
atexit(remove_lock_file);
|
||||||
remove_lock = 1;
|
lockfile_name = lockfile;
|
||||||
|
|
||||||
entries = read_cache();
|
entries = read_cache();
|
||||||
if (entries < 0)
|
if (entries < 0)
|
||||||
@ -333,10 +337,9 @@ int main(int argc, char **argv)
|
|||||||
if (add_file_to_cache(path))
|
if (add_file_to_cache(path))
|
||||||
die("Unable to add %s to database", path);
|
die("Unable to add %s to database", path);
|
||||||
}
|
}
|
||||||
if (write_cache(newfd, active_cache, active_nr) ||
|
if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
|
||||||
rename(".git/index.lock", ".git/index"))
|
|
||||||
die("Unable to write new cachefile");
|
die("Unable to write new cachefile");
|
||||||
|
|
||||||
remove_lock = 0;
|
lockfile_name = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user