2005-04-09 20:53:05 +04:00
|
|
|
/*
|
|
|
|
* Check-out files from the "current cache directory"
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Careful: order of argument flags does matter. For example,
|
|
|
|
*
|
2005-05-19 15:17:16 +04:00
|
|
|
* git-checkout-cache -a -f file.c
|
2005-04-09 20:53:05 +04:00
|
|
|
*
|
|
|
|
* Will first check out all files listed in the cache (but not
|
|
|
|
* overwrite any old ones), and then force-checkout "file.c" a
|
|
|
|
* second time (ie that one _will_ overwrite any old contents
|
|
|
|
* with the same filename).
|
|
|
|
*
|
2005-05-19 15:17:16 +04:00
|
|
|
* Also, just doing "git-checkout-cache" does nothing. You probably
|
|
|
|
* meant "git-checkout-cache -a". And if you want to force it, you
|
|
|
|
* want "git-checkout-cache -f -a".
|
2005-04-09 20:53:05 +04:00
|
|
|
*
|
|
|
|
* Intuitiveness is not the goal here. Repeatability is. The
|
|
|
|
* reason for the "no arguments means no work" thing is that
|
|
|
|
* from scripts you are supposed to be able to do things like
|
|
|
|
*
|
2005-05-19 15:17:16 +04:00
|
|
|
* find . -name '*.h' -print0 | xargs -0 git-checkout-cache -f --
|
2005-04-09 20:53:05 +04:00
|
|
|
*
|
|
|
|
* which will force all existing *.h files to be replaced with
|
|
|
|
* their cached copies. If an empty command line implied "all",
|
|
|
|
* then this would force-refresh everything in the cache, which
|
|
|
|
* was not the point.
|
|
|
|
*
|
|
|
|
* Oh, and the "--" is just a good idea when you know the rest
|
|
|
|
* will be filenames. Just so that you wouldn't have a filename
|
|
|
|
* of "-a" causing problems (not possible in the above example,
|
|
|
|
* but get used to it in scripting!).
|
|
|
|
*/
|
2005-05-12 04:02:45 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2005-04-09 20:53:05 +04:00
|
|
|
#include "cache.h"
|
|
|
|
|
2005-05-16 01:23:12 +04:00
|
|
|
static int force = 0, quiet = 0, not_new = 0, refresh_cache = 0;
|
2005-04-09 20:53:05 +04:00
|
|
|
|
2005-04-09 23:11:25 +04:00
|
|
|
static void create_directories(const char *path)
|
|
|
|
{
|
|
|
|
int len = strlen(path);
|
2005-04-26 23:00:58 +04:00
|
|
|
char *buf = xmalloc(len + 1);
|
2005-04-09 23:11:25 +04:00
|
|
|
const char *slash = path;
|
|
|
|
|
|
|
|
while ((slash = strchr(slash+1, '/')) != NULL) {
|
|
|
|
len = slash - path;
|
|
|
|
memcpy(buf, path, len);
|
|
|
|
buf[len] = 0;
|
2005-05-12 04:02:45 +04:00
|
|
|
if (mkdir(buf, 0755)) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
struct stat st;
|
|
|
|
if (!lstat(buf, &st) && S_ISDIR(st.st_mode))
|
|
|
|
continue; /* ok */
|
|
|
|
if (force && !unlink(buf) && !mkdir(buf, 0755))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
die("cannot create directory at %s", buf);
|
|
|
|
}
|
2005-04-09 23:11:25 +04:00
|
|
|
}
|
2005-04-14 10:26:38 +04:00
|
|
|
free(buf);
|
2005-04-09 23:11:25 +04:00
|
|
|
}
|
|
|
|
|
2005-05-12 04:02:45 +04:00
|
|
|
static void remove_subtree(const char *path)
|
|
|
|
{
|
|
|
|
DIR *dir = opendir(path);
|
|
|
|
struct dirent *de;
|
|
|
|
char pathbuf[PATH_MAX];
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
die("cannot opendir %s", path);
|
|
|
|
strcpy(pathbuf, path);
|
|
|
|
name = pathbuf + strlen(path);
|
|
|
|
*name++ = '/';
|
|
|
|
while ((de = readdir(dir)) != NULL) {
|
|
|
|
struct stat st;
|
|
|
|
if ((de->d_name[0] == '.') &&
|
|
|
|
((de->d_name[1] == 0) ||
|
|
|
|
((de->d_name[1] == '.') && de->d_name[2] == 0)))
|
|
|
|
continue;
|
|
|
|
strcpy(name, de->d_name);
|
|
|
|
if (lstat(pathbuf, &st))
|
|
|
|
die("cannot lstat %s", pathbuf);
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
remove_subtree(pathbuf);
|
|
|
|
else if (unlink(pathbuf))
|
|
|
|
die("cannot unlink %s", pathbuf);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
if (rmdir(path))
|
|
|
|
die("cannot rmdir %s", path);
|
|
|
|
}
|
|
|
|
|
2005-04-09 23:11:25 +04:00
|
|
|
static int create_file(const char *path, unsigned int mode)
|
|
|
|
{
|
2005-04-17 09:26:31 +04:00
|
|
|
int fd;
|
|
|
|
|
2005-04-17 20:55:36 +04:00
|
|
|
mode = (mode & 0100) ? 0777 : 0666;
|
2005-05-12 04:02:45 +04:00
|
|
|
create_directories(path);
|
2005-04-17 09:26:31 +04:00
|
|
|
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
|
2005-04-09 23:11:25 +04:00
|
|
|
if (fd < 0) {
|
2005-05-12 04:02:45 +04:00
|
|
|
if (errno == EISDIR && force) {
|
|
|
|
remove_subtree(path);
|
2005-04-17 09:26:31 +04:00
|
|
|
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
|
2005-04-09 23:11:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2005-04-21 21:15:02 +04:00
|
|
|
static int write_entry(struct cache_entry *ce, const char *path)
|
2005-04-09 20:53:05 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
void *new;
|
|
|
|
unsigned long size;
|
|
|
|
long wrote;
|
2005-04-09 22:24:41 +04:00
|
|
|
char type[20];
|
2005-05-05 16:38:25 +04:00
|
|
|
char target[1024];
|
2005-04-09 20:53:05 +04:00
|
|
|
|
2005-04-09 22:24:41 +04:00
|
|
|
new = read_sha1_file(ce->sha1, type, &size);
|
|
|
|
if (!new || strcmp(type, "blob")) {
|
2005-04-14 14:35:27 +04:00
|
|
|
if (new)
|
|
|
|
free(new);
|
2005-05-19 15:17:16 +04:00
|
|
|
return error("git-checkout-cache: unable to read sha1 file of %s (%s)",
|
2005-04-21 21:15:02 +04:00
|
|
|
path, sha1_to_hex(ce->sha1));
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
2005-05-05 16:38:25 +04:00
|
|
|
switch (ntohl(ce->ce_mode) & S_IFMT) {
|
|
|
|
case S_IFREG:
|
|
|
|
fd = create_file(path, ntohl(ce->ce_mode));
|
|
|
|
if (fd < 0) {
|
|
|
|
free(new);
|
2005-05-19 15:17:16 +04:00
|
|
|
return error("git-checkout-cache: unable to create file %s (%s)",
|
2005-05-05 16:38:25 +04:00
|
|
|
path, strerror(errno));
|
|
|
|
}
|
|
|
|
wrote = write(fd, new, size);
|
|
|
|
close(fd);
|
|
|
|
free(new);
|
|
|
|
if (wrote != size)
|
2005-05-19 15:17:16 +04:00
|
|
|
return error("git-checkout-cache: unable to write file %s", path);
|
2005-05-05 16:38:25 +04:00
|
|
|
break;
|
|
|
|
case S_IFLNK:
|
|
|
|
memcpy(target, new, size);
|
|
|
|
target[size] = '\0';
|
2005-05-06 03:34:10 +04:00
|
|
|
create_directories(path);
|
2005-05-05 16:38:25 +04:00
|
|
|
if (symlink(target, path)) {
|
|
|
|
free(new);
|
2005-05-19 15:17:16 +04:00
|
|
|
return error("git-checkout-cache: unable to create symlink %s (%s)",
|
2005-05-05 16:38:25 +04:00
|
|
|
path, strerror(errno));
|
|
|
|
}
|
|
|
|
free(new);
|
|
|
|
break;
|
|
|
|
default:
|
2005-04-09 20:53:05 +04:00
|
|
|
free(new);
|
2005-05-19 15:17:16 +04:00
|
|
|
return error("git-checkout-cache: unknown file mode for %s", path);
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
2005-05-16 01:23:12 +04:00
|
|
|
|
|
|
|
if (refresh_cache) {
|
|
|
|
struct stat st;
|
|
|
|
lstat(ce->name, &st);
|
|
|
|
fill_stat_cache_info(ce, &st);
|
|
|
|
}
|
2005-04-13 13:28:48 +04:00
|
|
|
return 0;
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
|
|
|
|
2005-04-21 21:15:02 +04:00
|
|
|
static int checkout_entry(struct cache_entry *ce, const char *base_dir)
|
2005-04-09 20:53:05 +04:00
|
|
|
{
|
2005-04-13 01:17:28 +04:00
|
|
|
struct stat st;
|
2005-04-21 21:15:02 +04:00
|
|
|
static char path[MAXPATHLEN+1];
|
|
|
|
int len = strlen(base_dir);
|
|
|
|
|
|
|
|
memcpy(path, base_dir, len);
|
|
|
|
strcpy(path + len, ce->name);
|
2005-04-09 20:53:05 +04:00
|
|
|
|
2005-05-05 16:38:25 +04:00
|
|
|
if (!lstat(path, &st)) {
|
2005-05-15 06:04:25 +04:00
|
|
|
unsigned changed = ce_match_stat(ce, &st);
|
2005-04-13 01:17:28 +04:00
|
|
|
if (!changed)
|
|
|
|
return 0;
|
|
|
|
if (!force) {
|
|
|
|
if (!quiet)
|
2005-05-19 15:17:16 +04:00
|
|
|
fprintf(stderr, "git-checkout-cache: %s already exists\n", path);
|
2005-04-09 20:59:44 +04:00
|
|
|
return 0;
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
2005-04-17 09:26:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We unlink the old file, to get the new one with the
|
|
|
|
* right permissions (including umask, which is nasty
|
|
|
|
* to emulate by hand - much easier to let the system
|
|
|
|
* just do the right thing)
|
|
|
|
*/
|
2005-04-21 21:15:02 +04:00
|
|
|
unlink(path);
|
2005-04-25 02:14:16 +04:00
|
|
|
} else if (not_new)
|
|
|
|
return 0;
|
2005-04-21 21:15:02 +04:00
|
|
|
return write_entry(ce, path);
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
|
|
|
|
2005-04-21 21:15:02 +04:00
|
|
|
static int checkout_file(const char *name, const char *base_dir)
|
2005-04-09 20:53:05 +04:00
|
|
|
{
|
|
|
|
int pos = cache_name_pos(name, strlen(name));
|
|
|
|
if (pos < 0) {
|
2005-04-17 21:02:21 +04:00
|
|
|
if (!quiet) {
|
|
|
|
pos = -pos - 1;
|
|
|
|
fprintf(stderr,
|
2005-05-19 15:17:16 +04:00
|
|
|
"git-checkout-cache: %s is %s.\n",
|
2005-04-17 21:02:21 +04:00
|
|
|
name,
|
|
|
|
(pos < active_nr &&
|
|
|
|
!strcmp(active_cache[pos]->name, name)) ?
|
|
|
|
"unmerged" : "not in the cache");
|
|
|
|
}
|
2005-04-09 20:53:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2005-04-21 21:15:02 +04:00
|
|
|
return checkout_entry(active_cache[pos], base_dir);
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
|
|
|
|
2005-04-21 21:15:02 +04:00
|
|
|
static int checkout_all(const char *base_dir)
|
2005-04-09 20:53:05 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < active_nr ; i++) {
|
|
|
|
struct cache_entry *ce = active_cache[i];
|
2005-04-18 05:39:14 +04:00
|
|
|
if (ce_stage(ce))
|
|
|
|
continue;
|
2005-04-21 21:15:02 +04:00
|
|
|
if (checkout_entry(ce, base_dir) < 0)
|
2005-04-09 20:53:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i, force_filename = 0;
|
2005-04-21 21:15:02 +04:00
|
|
|
const char *base_dir = "";
|
2005-05-16 01:23:12 +04:00
|
|
|
struct cache_file cache_file;
|
|
|
|
int newfd = -1;
|
2005-04-09 20:53:05 +04:00
|
|
|
|
|
|
|
if (read_cache() < 0) {
|
2005-04-13 13:28:48 +04:00
|
|
|
die("invalid cache");
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
const char *arg = argv[i];
|
|
|
|
if (!force_filename) {
|
|
|
|
if (!strcmp(arg, "-a")) {
|
2005-04-21 21:15:02 +04:00
|
|
|
checkout_all(base_dir);
|
2005-04-09 20:53:05 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "--")) {
|
|
|
|
force_filename = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "-f")) {
|
|
|
|
force = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "-q")) {
|
|
|
|
quiet = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-25 02:14:16 +04:00
|
|
|
if (!strcmp(arg, "-n")) {
|
|
|
|
not_new = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-05-16 01:23:12 +04:00
|
|
|
if (!strcmp(arg, "-u")) {
|
|
|
|
refresh_cache = 1;
|
|
|
|
if (newfd < 0)
|
|
|
|
newfd = hold_index_file_for_update
|
|
|
|
(&cache_file,
|
|
|
|
get_index_file());
|
|
|
|
if (newfd < 0)
|
|
|
|
die("cannot open index.lock file.");
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-21 21:15:02 +04:00
|
|
|
if (!memcmp(arg, "--prefix=", 9)) {
|
|
|
|
base_dir = arg+9;
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
2005-05-16 01:23:12 +04:00
|
|
|
if (base_dir[0]) {
|
|
|
|
/* when --prefix is specified we do not
|
|
|
|
* want to update cache.
|
|
|
|
*/
|
|
|
|
if (refresh_cache) {
|
|
|
|
close(newfd); newfd = -1;
|
|
|
|
rollback_index_file(&cache_file);
|
|
|
|
}
|
|
|
|
refresh_cache = 0;
|
|
|
|
}
|
2005-04-21 21:15:02 +04:00
|
|
|
checkout_file(arg, base_dir);
|
2005-04-09 20:53:05 +04:00
|
|
|
}
|
2005-05-16 01:23:12 +04:00
|
|
|
|
|
|
|
if (0 <= newfd &&
|
|
|
|
(write_cache(newfd, active_cache, active_nr) ||
|
|
|
|
commit_index_file(&cache_file)))
|
|
|
|
die("Unable to write new cachefile");
|
2005-04-09 20:53:05 +04:00
|
|
|
return 0;
|
|
|
|
}
|