Browse Source

Make "update-cache" a bit friendlier to use (and harder to mis-use).

It now requires the "--add" flag before you add any new files, and
a "--remove" file if you want to mark files for removal. And giving
it the "--refresh" flag makes it just update all the files that it
already knows about.
maint
Linus Torvalds 20 years ago
parent
commit
121481abf8
  1. 4
      cache.h
  2. 13
      read-cache.c
  3. 2
      read-tree.c
  4. 60
      update-cache.c
  5. 2
      write-tree.c

4
cache.h

@ -75,7 +75,7 @@ unsigned int active_nr, active_alloc;
extern int read_cache(void); extern int read_cache(void);
extern int write_cache(int newfd, struct cache_entry **cache, int entries); extern int write_cache(int newfd, struct cache_entry **cache, int entries);
extern int cache_name_pos(const char *name, int namelen); extern int cache_name_pos(const char *name, int namelen);
extern int add_cache_entry(struct cache_entry *ce); extern int add_cache_entry(struct cache_entry *ce, int ok_to_add);
extern int remove_file_from_cache(char *path); extern int remove_file_from_cache(char *path);
extern int cache_match_stat(struct cache_entry *ce, struct stat *st); extern int cache_match_stat(struct cache_entry *ce, struct stat *st);


@ -104,7 +104,7 @@ extern int get_sha1_hex(const char *hex, unsigned char *sha1);
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */ extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */


/* General helper functions */ /* General helper functions */
extern void usage(const char *err); extern void usage(const char *err, ...);
extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2); extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);


#endif /* CACHE_H */ #endif /* CACHE_H */

13
read-cache.c

@ -9,9 +9,13 @@ const char *sha1_file_directory = NULL;
struct cache_entry **active_cache = NULL; struct cache_entry **active_cache = NULL;
unsigned int active_nr = 0, active_alloc = 0; unsigned int active_nr = 0, active_alloc = 0;


void usage(const char *err) void usage(const char *err, ...)
{ {
fprintf(stderr, "read-tree: %s\n", err); va_list args;

va_start(args, err);
vfprintf(stderr, err, args);
va_end(args);
exit(1); exit(1);
} }


@ -294,7 +298,7 @@ int remove_file_from_cache(char *path)
return 0; return 0;
} }


int add_cache_entry(struct cache_entry *ce) int add_cache_entry(struct cache_entry *ce, int ok_to_add)
{ {
int pos; int pos;


@ -306,6 +310,9 @@ int add_cache_entry(struct cache_entry *ce)
return 0; return 0;
} }


if (!ok_to_add)
return -1;

/* Make sure the array is big enough .. */ /* Make sure the array is big enough .. */
if (active_nr == active_alloc) { if (active_nr == active_alloc) {
active_alloc = alloc_nr(active_alloc); active_alloc = alloc_nr(active_alloc);

2
read-tree.c

@ -18,7 +18,7 @@ static int read_one_entry(unsigned char *sha1, const char *base, int baselen, co
memcpy(ce->name, base, baselen); memcpy(ce->name, base, baselen);
memcpy(ce->name + baselen, pathname, len+1); memcpy(ce->name + baselen, pathname, len+1);
memcpy(ce->sha1, sha1, 20); memcpy(ce->sha1, sha1, 20);
return add_cache_entry(ce); return add_cache_entry(ce, 1);
} }


static int read_tree(unsigned char *sha1, const char *base, int baselen) static int read_tree(unsigned char *sha1, const char *base, int baselen)

60
update-cache.c

@ -5,6 +5,15 @@
*/ */
#include "cache.h" #include "cache.h"


/*
* Default to not allowing changes to the list of files. The
* tool doesn't actually care, but this makes it harder to add
* files to the revision control by mistake by doing something
* like "update-cache *" and suddenly having all the object
* files be revision controlled.
*/
static int allow_add = 0, allow_remove = 0;

static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st) static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
{ {
z_stream stream; z_stream stream;
@ -57,8 +66,10 @@ static int add_file_to_cache(char *path)


fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
if (errno == ENOENT) if (errno == ENOENT) {
return remove_file_from_cache(path); if (allow_remove)
return remove_file_from_cache(path);
}
return -1; return -1;
} }
if (fstat(fd, &st) < 0) { if (fstat(fd, &st) < 0) {
@ -85,7 +96,29 @@ static int add_file_to_cache(char *path)
if (index_fd(path, namelen, ce, fd, &st) < 0) if (index_fd(path, namelen, ce, fd, &st) < 0)
return -1; return -1;


return add_cache_entry(ce); return add_cache_entry(ce, allow_add);
}

static void refresh_entry(struct cache_entry *ce)
{
/*
* This is really not the right way to do it, but
* add_file_to_cache() does do the right thing.
*
* We should really just update the cache
* entry in-place, I think. With this approach we
* end up allocating a new one, searching for where
* to insert it etc etc crud.
*/
add_file_to_cache(ce->name);
}

static void refresh_cache(void)
{
int i;

for (i = 0; i < active_nr; i++)
refresh_entry(active_cache[i]);
} }


/* /*
@ -119,6 +152,7 @@ inside:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i, newfd, entries; int i, newfd, entries;
int allow_options = 1;


entries = read_cache(); entries = read_cache();
if (entries < 0) { if (entries < 0) {
@ -133,6 +167,26 @@ int main(int argc, char **argv)
} }
for (i = 1 ; i < argc; i++) { for (i = 1 ; i < argc; i++) {
char *path = argv[i]; char *path = argv[i];

if (allow_options && *path == '-') {
if (!strcmp(path, "--")) {
allow_options = 0;
continue;
}
if (!strcmp(path, "--add")) {
allow_add = 1;
continue;
}
if (!strcmp(path, "--remove")) {
allow_remove = 1;
continue;
}
if (!strcmp(path, "--refresh")) {
refresh_cache();
continue;
}
usage("unknown option %s", path);
}
if (!verify_path(path)) { if (!verify_path(path)) {
fprintf(stderr, "Ignoring path %s\n", argv[i]); fprintf(stderr, "Ignoring path %s\n", argv[i]);
continue; continue;

2
write-tree.c

@ -63,8 +63,6 @@ static int write_tree(struct cache_entry **cachep, int maxentries, const char *b
int subdir_written; int subdir_written;


subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1); subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1);
fprintf(stderr, "Wrote %d entries from subdirectory '%.*s'\n",
subdir_written, dirname-pathname, pathname);
nr += subdir_written; nr += subdir_written;


/* Now we need to write out the directory entry into this tree.. */ /* Now we need to write out the directory entry into this tree.. */
Loading…
Cancel
Save