{"thread":{"id":"365","subject":"[PATCH] add a diff-files command (revised)","startedAt":"2005-04-28T02:06:29Z","lastAt":"2005-04-28T02:39:00Z","messageCount":3,"participants":["Nicolas Pitre","Petr Baudis"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"1974","messageId":"Pine.LNX.4.62.0504272141560.14033@localhost.localdomain","threadId":"365","inReplyTo":null,"subject":"[PATCH] add a diff-files command (revised)","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2005-04-28T02:06:29Z","receivedAt":"2005-04-28T02:06:29Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"In the same spirit as diff-tree and diff-cache, here is a diff-files \ncommand that processes differences between the index cache and the \nworking directory content.  It produces lists of files that are either \nchanged, deleted and/or unknown with regards to the current cache, \ncontent. The -p option can also be used to generate a patch describing \nthe differences in patch form.\n\nIt also has the ability to accept exclude patterns for files and the \nability to read those exclude patterns from a file.\n\nTypical usage looks like:\n\n    diff-files --others --exclude=\\*.o arch/arm/ include/asm-arm/\n\nwhich lists all files the git cache doesn't know about in arch/arm/ and \ninclude/asm-arm/ but ignoring any object files.  Or:\n\n    diff-files --all -p --exclude-from=dontdiff.list\n\nwhich produces a patch of all changes currently in the work tree while \nexcluding all files matching any of the patterns listed in \ndontdiff.list (useful when one doesn't want to run 'make distclean').\n\n(revised after comments from Junio C Hamano)\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n\n--- k/Makefile\n+++ l/Makefile\n@@ -18,7 +18,7 @@ PROG=   update-cache show-diff init-db w\n \tcat-file fsck-cache checkout-cache diff-tree rev-tree show-files \\\n \tcheck-files ls-tree merge-base merge-cache unpack-file git-export \\\n \tdiff-cache convert-cache http-pull rpush rpull rev-list git-mktag \\\n-\tdiff-tree-helper\n+\tdiff-tree-helper diff-files\n \n all: $(PROG)\n \n--- k/diff-files.c\n+++ l/diff-files.c\n@@ -0,0 +1,347 @@\n+/*\n+ * GIT - The information manager from hell\n+ *\n+ * Copyright (C) Linus Torvalds, 2005\n+ */\n+\n+#include <dirent.h>\n+#include <fnmatch.h>\n+#include \"cache.h\"\n+#include \"diff.h\"\n+\n+static const char *diff_files_usage =\n+\t\"diff-files [--all] [--changed] [--deleted] [--others] [-p | -z] \"\n+\t\"[--exclude=<pattern>] [--exclude-from=<file>] [paths...]\";\n+\n+/* What paths are we interested in? */\n+static int nr_paths = 0;\n+static char **paths = NULL;\n+static int *pathlens = NULL;\n+\n+static int nr_excludes;\n+static const char **excludes;\n+static int excludes_alloc;\n+\n+static void add_exclude(const char *string)\n+{\n+\tif (nr_excludes == excludes_alloc) {\n+\t\texcludes_alloc = alloc_nr(excludes_alloc);\n+\t\texcludes = realloc(excludes, excludes_alloc*sizeof(char *));\n+\t}\n+\texcludes[nr_excludes++] = string;\n+}\n+\n+static void add_excludes_from_file(const char *fname)\n+{\n+\tint fd, i;\n+\tlong size;\n+\tchar *buf, *entry;\n+\n+\tfd = open(fname, O_RDONLY);\n+\tif (fd < 0)\n+\t\tgoto err;\n+\tsize = lseek(fd, 0, SEEK_END);\n+\tif (size < 0)\n+\t\tgoto err;\n+\tlseek(fd, 0, SEEK_SET);\n+\tif (size == 0) {\n+\t\tclose(fd);\n+\t\treturn;\n+\t}\n+\tbuf = malloc(size);\n+\tif (!buf) {\n+\t\terrno = ENOMEM;\n+\t\tgoto err;\n+\t}\n+\tif (read(fd, buf, size) != size)\n+\t\tgoto err;\n+\tclose(fd);\n+\n+\tentry = buf;\n+\tfor (i = 0; i < size; i++) {\n+\t\tif (buf[i] == '\\n') {\n+\t\t\tif (entry != buf + i) {\n+\t\t\t\tbuf[i] = 0;\n+\t\t\t\tadd_exclude(entry);\n+\t\t\t}\n+\t\t\tentry = buf + i + 1;\n+\t\t}\n+\t}\n+\treturn;\n+\n+err:\tperror(fname);\n+\texit(1);\n+}\n+\n+/*\n+ * See if name matches our specified paths and is not excluded.\n+ * return value:\n+ *\t-1 if no match\n+ *\t0 if partial match (name is a directory component)\n+ *\t1 = exact match\n+ *\t2 = name is under a specified directory path with no excludes\n+ */\n+static int path_match(const char *name, int namelen)\n+{\n+\tint i, ret;\n+\n+\t/* fast case: no path list and no exclude list */\n+\tif (!nr_paths && !nr_excludes)\n+\t\treturn 2;\n+\n+\tret = (nr_paths) ? -1 : 1;\n+\tfor (i = 0; i < nr_paths; i++) {\n+\t\tint pathlen = pathlens[i];\n+\t\tif (pathlen == namelen &&\n+\t\t    strncmp(paths[i], name, pathlen) == 0) {\n+\t\t\tret = 1;\n+\t\t\tbreak;\n+\t\t} else if (pathlen > namelen && \n+\t\t\t   strncmp(paths[i], name, namelen) == 0 &&\n+\t\t\t   paths[i][namelen] == '/') {\n+\t\t\tret = 0;\n+\t\t\tbreak;\n+\t\t} else if (pathlen < namelen &&\n+\t\t\t   strncmp(paths[i], name, pathlen) == 0 &&\n+\t\t\t   name[pathlen] == '/') {\n+\t\t\tret = (nr_excludes) ? 1 : 2;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (ret >= 0 && nr_excludes) {\n+\t\tconst char *basename = strrchr(name, '/');\n+\t\tbasename = (basename) ? basename+1 : name;\n+\t\tfor (i = 0; i < nr_excludes; i++) {\n+\t\t\tif (fnmatch(excludes[i], basename, 0) == 0) {\n+\t\t\t\tret = -1;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\treturn ret;\n+}\n+\n+static const char **others;\n+static int nr_others;\n+static int others_alloc;\n+\n+static void add_name(const char *pathname, int len)\n+{\n+\tchar *name;\n+\n+\tif (cache_name_pos(pathname, len) >= 0)\n+\t\treturn;\n+\n+\tif (nr_others == others_alloc) {\n+\t\tothers_alloc = alloc_nr(others_alloc);\n+\t\tothers = realloc(others, others_alloc*sizeof(char *));\n+\t}\n+\tname = malloc(len + 1);\n+\tmemcpy(name, pathname, len + 1);\n+\tothers[nr_others++] = name;\n+}\n+\n+/*\n+ * Read a directory tree. We currently ignore anything but\n+ * directories and regular files. That's because git doesn't\n+ * handle them at all yet. Maybe that will change some day.\n+ *\n+ * Also, we currently ignore all names starting with a dot.\n+ * That likely will not change.\n+ */\n+static void read_directory(const char *path, const char *base, int baselen, int match)\n+{\n+\tDIR *dir = opendir(path);\n+\n+\tif (dir) {\n+\t\tstruct dirent *de;\n+\t\tchar fullname[MAXPATHLEN + 1];\n+\t\tmemcpy(fullname, base, baselen);\n+\n+\t\twhile ((de = readdir(dir)) != NULL) {\n+\t\t\tint len;\n+\n+\t\t\tif (de->d_name[0] == '.')\n+\t\t\t\tcontinue;\n+\t\t\tlen = strlen(de->d_name);\n+\t\t\tmemcpy(fullname + baselen, de->d_name, len+1);\n+\t\t\tif (match < 2)\n+\t\t\t\tmatch = path_match(fullname, baselen+len);\n+\t\t\tif (match < 0)\n+\t\t\t\tcontinue;\n+\n+\t\t\tswitch (de->d_type) {\n+\t\t\tstruct stat st;\n+\t\t\tdefault:\n+\t\t\t\tcontinue;\n+\t\t\tcase DT_UNKNOWN:\n+\t\t\t\tif (lstat(fullname, &st))\n+\t\t\t\t\tcontinue;\n+\t\t\t\tif (S_ISREG(st.st_mode))\n+\t\t\t\t\tbreak;\n+\t\t\t\tif (!S_ISDIR(st.st_mode))\n+\t\t\t\t\tcontinue;\n+\t\t\t\t/* fallthrough */\n+\t\t\tcase DT_DIR:\n+\t\t\t\tmemcpy(fullname + baselen + len, \"/\", 2);\n+\t\t\t\tread_directory(fullname, fullname,\n+\t\t\t\t\t       baselen + len + 1, match);\n+\t\t\t\tcontinue;\n+\t\t\tcase DT_REG:\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tif (match > 0)\n+\t\t\t\tadd_name(fullname, baselen + len);\n+\t\t}\n+\t\tclosedir(dir);\n+\t}\n+}\n+\n+static int cmp_name(const void *p1, const void *p2)\n+{\n+\tconst char *n1 = *(const char **)p1;\n+\tconst char *n2 = *(const char **)p2;\n+\tint l1 = strlen(n1), l2 = strlen(n2);\n+\n+\treturn cache_name_compare(n1, l1, n2, l2);\n+}\n+\n+static int show_changed = 0;\n+static int show_deleted = 0;\n+static int show_others = 0;\n+static int generate_patch = 0;\n+static int line_terminator = '\\n';\n+\n+static const char null_sha1[20];\n+static const char null_sha1_hex[] = \"0000000000000000000000000000000000000000\";\n+\n+static void show_file(int prefix, unsigned int mode,\n+\t\t      const char *sha1, const char *name)\n+{\n+\tif (generate_patch)\n+\t\tdiff_addremove(prefix, mode, sha1, name, NULL);\n+\telse\n+\t\tprintf(\"%c%o\\t%s\\t%s\\t%s%c\", prefix, mode, \"blob\",\n+\t\t       sha1_to_hex(sha1), name, line_terminator);\n+}\n+\n+int main(int argc, char **argv)\n+{\n+\tint i, entries;\n+\n+\tfor (i = 1; i < argc; i++) {\n+\t\tchar *arg = argv[i];\n+\n+\t\tif (*arg != '-') {\n+\t\t\tbreak;\n+\t\t} else if (!strcmp(arg, \"-z\")) {\n+\t\t\tline_terminator = 0;\n+\t\t} else if (!strcmp(arg, \"-a\") || !strcmp(arg, \"--all\")) {\n+\t\t\tshow_changed = show_deleted = show_others = 1;\n+\t\t} else if (!strcmp(arg, \"-c\") || !strcmp(arg, \"--changed\")) {\n+\t\t\tshow_changed = 1;\n+\t\t} else if (!strcmp(arg, \"-d\") || !strcmp(arg, \"--deleted\")) {\n+\t\t\tshow_deleted = 1;\n+\t\t} else if (!strcmp(arg, \"-o\") || !strcmp(arg, \"--others\")) {\n+\t\t\tshow_others = 1;\n+\t\t} else if (!strcmp(arg, \"-p\")) {\n+\t\t\tgenerate_patch = 1;\n+\t\t} else if (!strcmp(arg, \"-x\") && i+1 < argc) {\n+\t\t\tadd_exclude(argv[++i]);\n+\t\t} else if (!strncmp(arg, \"--exclude=\", 10)) {\n+\t\t\tadd_exclude(arg+10);\n+\t\t} else if (!strcmp(arg, \"-X\") && i+1 < argc) {\n+\t\t\tadd_excludes_from_file(argv[++i]);\n+\t\t} else if (!strncmp(arg, \"--exclude-from=\", 15)) {\n+\t\t\tadd_excludes_from_file(arg+15);\n+\t\t} else if (!strcmp(arg, \"--\")) {\n+\t\t\ti++;\n+\t\t\tbreak;\n+\t\t} else\n+\t\t\tusage(diff_files_usage);\n+\t}\n+\n+\t/* default to -c if none of -c, -d nor -o have been specified */\n+\tif (!show_changed && !show_deleted && !show_others)\n+\t\tshow_changed = 1;\n+\n+\tif (i < argc) {\n+\t\tpaths = &argv[i];\n+\t\tnr_paths = argc - i;\n+\t\tpathlens = malloc(nr_paths * sizeof(int));\n+\t\tfor (i=0; i<nr_paths; i++) {\n+\t\t\tpathlens[i] = strlen(paths[i]);\n+\t\t\tif (paths[i][pathlens[i] - 1] == '/')\n+\t\t\t\tpathlens[i]--;\n+\t\t}\n+\t}\n+\n+\tentries = read_cache();\n+\tif (entries < 0) {\n+\t\tperror(\"read_cache\");\n+\t\texit(1);\n+\t}\n+\n+\tif (show_others) {\n+\t\tread_directory(\".\", \"\", 0, 0);\n+\t\tqsort(others, nr_others, sizeof(char *), cmp_name);\n+\t\tfor (i = 0; i < nr_others; i++) {\n+\t\t\tstruct stat st;\n+\t\t\tunsigned int mode;\n+\t\t\tif (stat(others[i], &st) < 0) {\n+\t\t\t\tperror(others[i]);\n+\t\t\t} else {\n+\t\t\t\tmode = S_IFREG | ce_permissions(st.st_mode);\n+\t\t\t\tshow_file('+', mode, null_sha1, others[i]);\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tfor (i = 0; i < entries; i++) {\n+\t\tstruct stat st;\n+\t\tunsigned int ce_mode, mode;\n+\t\tstruct cache_entry *ce = active_cache[i];\n+\n+\t\tif (path_match(ce->name, ce_namelen(ce)) < 1)\n+\t\t\tcontinue;\n+\n+\t\tif (show_changed && ce_stage(ce)) {\n+\t\t\tif (generate_patch)\n+\t\t\t\tdiff_unmerge(ce->name);\n+\t\t\telse\n+\t\t\t\tprintf(\"U %s%c\", ce->name, line_terminator);\n+\t\t\tdo {\n+\t\t\t\ti++;\n+\t\t\t} while (i < entries &&\n+\t\t\t\t !strcmp(ce->name, active_cache[i]->name));\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tce_mode = ntohl(ce->ce_mode);\n+\t\tif (stat(ce->name, &st) < 0) {\n+\t\t\tif (errno != ENOENT) {\n+\t\t\t\tperror(ce->name);\n+\t\t\t} else if (show_deleted) {\n+\t\t\t\tshow_file('-', ce_mode, ce->sha1, ce->name);\n+\t\t\t}\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (!show_changed || !cache_match_stat(ce, &st))\n+\t\t\tcontinue;\n+\n+\t\tmode = S_IFREG | ce_permissions(st.st_mode);\n+\t\tif (generate_patch)\n+\t\t\tdiff_change(ce_mode, mode, ce->sha1,\n+\t\t\t\t    null_sha1, ce->name, NULL);\n+\t\telse\n+\t\t\tprintf(\"*%o->%o\\t%s\\t%s->%s\\t%s%c\",\n+\t\t\t       ce_mode, mode, \"blob\",\n+\t\t\t       sha1_to_hex(ce->sha1), null_sha1_hex,\n+\t\t\t       ce->name, line_terminator);\n+\t}\n+\n+\treturn 0;\n+}\nBinary files k/diff-tree and l/diff-tree differ\nBinary files k/diff-tree-helper and l/diff-tree-helper differ\nBinary files k/diff.o and l/diff.o differ\nBinary files k/fsck-cache and l/fsck-cache differ\nBinary files k/git-export and l/git-export differ\nBinary files k/git-mktag and l/git-mktag differ\nBinary files k/http-pull and l/http-pull differ\nBinary files k/init-db and l/init-db differ\nBinary files k/init-db.o and l/init-db.o differ\nBinary files k/libgit.a and l/libgit.a differ\nBinary files k/ls-tree and l/ls-tree differ\nBinary files k/merge-base and l/merge-base differ\nBinary files k/merge-cache and l/merge-cache differ\nBinary files k/object.o and l/object.o differ\nBinary files k/read-cache.o and l/read-cache.o differ\nBinary files k/read-tree and l/read-tree differ\nBinary files k/rev-list and l/rev-list differ\nBinary files k/rev-tree and l/rev-tree differ\nBinary files k/rpull and l/rpull differ\nBinary files k/rpush and l/rpush differ\nBinary files k/sha1_file.o and l/sha1_file.o differ\nBinary files k/show-diff and l/show-diff differ\nBinary files k/show-files and l/show-files differ\nBinary files k/strbuf.o and l/strbuf.o differ\nBinary files k/tree.o and l/tree.o differ\nBinary files k/unpack-file and l/unpack-file differ\nBinary files k/update-cache and l/update-cache differ\n--- k/update-cache.c\n+++ l/update-cache.c\n@@ -0,0 +1,373 @@\n+/*\n+ * GIT - The information manager from hell\n+ *\n+ * Copyright (C) Linus Torvalds, 2005\n+ */\n+#include <signal.h>\n+#include \"cache.h\"\n+\n+/*\n+ * Default to not allowing changes to the list of files. The\n+ * tool doesn't actually care, but this makes it harder to add\n+ * files to the revision control by mistake by doing something\n+ * like \"update-cache *\" and suddenly having all the object\n+ * files be revision controlled.\n+ */\n+static int allow_add = 0, allow_remove = 0, not_new = 0;\n+\n+/* Three functions to allow overloaded pointer return; see linux/err.h */\n+static inline void *ERR_PTR(long error)\n+{\n+\treturn (void *) error;\n+}\n+\n+static inline long PTR_ERR(const void *ptr)\n+{\n+\treturn (long) ptr;\n+}\n+\n+static inline long IS_ERR(const void *ptr)\n+{\n+\treturn (unsigned long)ptr > (unsigned long)-1000L;\n+}\n+\n+static int index_fd(unsigned char *sha1, int fd, struct stat *st)\n+{\n+\tz_stream stream;\n+\tunsigned long size = st->st_size;\n+\tint max_out_bytes = size + 200;\n+\tvoid *out = xmalloc(max_out_bytes);\n+\tvoid *metadata = xmalloc(200);\n+\tint metadata_size;\n+\tvoid *in;\n+\tSHA_CTX c;\n+\n+\tin = \"\";\n+\tif (size)\n+\t\tin = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);\n+\tclose(fd);\n+\tif (!out || (int)(long)in == -1)\n+\t\treturn -1;\n+\n+\tmetadata_size = 1+sprintf(metadata, \"blob %lu\", size);\n+\n+\tSHA1_Init(&c);\n+\tSHA1_Update(&c, metadata, metadata_size);\n+\tSHA1_Update(&c, in, size);\n+\tSHA1_Final(sha1, &c);\n+\n+\tmemset(&stream, 0, sizeof(stream));\n+\tdeflateInit(&stream, Z_BEST_COMPRESSION);\n+\n+\t/*\n+\t * ASCII size + nul byte\n+\t */\t\n+\tstream.next_in = metadata;\n+\tstream.avail_in = metadata_size;\n+\tstream.next_out = out;\n+\tstream.avail_out = max_out_bytes;\n+\twhile (deflate(&stream, 0) == Z_OK)\n+\t\t/* nothing */;\n+\n+\t/*\n+\t * File content\n+\t */\n+\tstream.next_in = in;\n+\tstream.avail_in = size;\n+\twhile (deflate(&stream, Z_FINISH) == Z_OK)\n+\t\t/*nothing */;\n+\n+\tdeflateEnd(&stream);\n+\t\n+\treturn write_sha1_buffer(sha1, out, stream.total_out);\n+}\n+\n+/*\n+ * This only updates the \"non-critical\" parts of the directory\n+ * cache, ie the parts that aren't tracked by GIT, and only used\n+ * to validate the cache.\n+ */\n+static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)\n+{\n+\tce->ce_ctime.sec = htonl(st->st_ctime);\n+\tce->ce_mtime.sec = htonl(st->st_mtime);\n+#ifdef NSEC\n+\tce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);\n+\tce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);\n+#endif\n+\tce->ce_dev = htonl(st->st_dev);\n+\tce->ce_ino = htonl(st->st_ino);\n+\tce->ce_uid = htonl(st->st_uid);\n+\tce->ce_gid = htonl(st->st_gid);\n+\tce->ce_size = htonl(st->st_size);\n+}\n+\n+static int add_file_to_cache(char *path)\n+{\n+\tint size, namelen;\n+\tstruct cache_entry *ce;\n+\tstruct stat st;\n+\tint fd;\n+\n+\tfd = open(path, O_RDONLY);\n+\tif (fd < 0) {\n+\t\tif (errno == ENOENT) {\n+\t\t\tif (allow_remove)\n+\t\t\t\treturn remove_file_from_cache(path);\n+\t\t}\n+\t\treturn -1;\n+\t}\n+\tif (fstat(fd, &st) < 0) {\n+\t\tclose(fd);\n+\t\treturn -1;\n+\t}\n+\tnamelen = strlen(path);\n+\tsize = cache_entry_size(namelen);\n+\tce = xmalloc(size);\n+\tmemset(ce, 0, size);\n+\tmemcpy(ce->name, path, namelen);\n+\tfill_stat_cache_info(ce, &st);\n+\tce->ce_mode = create_ce_mode(st.st_mode);\n+\tce->ce_flags = htons(namelen);\n+\n+\tif (index_fd(ce->sha1, fd, &st) < 0)\n+\t\treturn -1;\n+\n+\treturn add_cache_entry(ce, allow_add);\n+}\n+\n+static int match_data(int fd, void *buffer, unsigned long size)\n+{\n+\twhile (size) {\n+\t\tchar compare[1024];\n+\t\tint ret = read(fd, compare, sizeof(compare));\n+\n+\t\tif (ret <= 0 || ret > size || memcmp(buffer, compare, ret))\n+\t\t\treturn -1;\n+\t\tsize -= ret;\n+\t\tbuffer += ret;\n+\t}\n+\treturn 0;\n+}\n+\n+static int compare_data(struct cache_entry *ce, unsigned long expected_size)\n+{\n+\tint match = -1;\n+\tint fd = open(ce->name, O_RDONLY);\n+\n+\tif (fd >= 0) {\n+\t\tvoid *buffer;\n+\t\tunsigned long size;\n+\t\tchar type[10];\n+\n+\t\tbuffer = read_sha1_file(ce->sha1, type, &size);\n+\t\tif (buffer) {\n+\t\t\tif (size == expected_size && !strcmp(type, \"blob\"))\n+\t\t\t\tmatch = match_data(fd, buffer, size);\n+\t\t\tfree(buffer);\n+\t\t}\n+\t\tclose(fd);\n+\t}\n+\treturn match;\n+}\n+\n+/*\n+ * \"refresh\" does not calculate a new sha1 file or bring the\n+ * cache up-to-date for mode/content changes. But what it\n+ * _does_ do is to \"re-match\" the stat information of a file\n+ * with the cache, so that you can refresh the cache for a\n+ * file that hasn't been changed but where the stat entry is\n+ * out of date.\n+ *\n+ * For example, you'd want to do this after doing a \"read-tree\",\n+ * to link up the stat cache details with the proper files.\n+ */\n+static struct cache_entry *refresh_entry(struct cache_entry *ce)\n+{\n+\tstruct stat st;\n+\tstruct cache_entry *updated;\n+\tint changed, size;\n+\n+\tif (stat(ce->name, &st) < 0)\n+\t\treturn ERR_PTR(-errno);\n+\n+\tchanged = cache_match_stat(ce, &st);\n+\tif (!changed)\n+\t\treturn ce;\n+\n+\t/*\n+\t * If the mode has changed, there's no point in trying\n+\t * to refresh the entry - it's not going to match\n+\t */\n+\tif (changed & MODE_CHANGED)\n+\t\treturn ERR_PTR(-EINVAL);\n+\n+\tif (compare_data(ce, st.st_size))\n+\t\treturn ERR_PTR(-EINVAL);\n+\n+\tsize = ce_size(ce);\n+\tupdated = xmalloc(size);\n+\tmemcpy(updated, ce, size);\n+\tfill_stat_cache_info(updated, &st);\n+\treturn updated;\n+}\n+\n+static void refresh_cache(void)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < active_nr; i++) {\n+\t\tstruct cache_entry *ce, *new;\n+\t\tce = active_cache[i];\n+\t\tif (ce_stage(ce)) {\n+\t\t\tprintf(\"%s: needs merge\\n\", ce->name);\n+\t\t\twhile ((i < active_nr) &&\n+\t\t\t       ! strcmp(active_cache[i]->name, ce->name))\n+\t\t\t\ti++;\n+\t\t\ti--;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tnew = refresh_entry(ce);\n+\t\tif (IS_ERR(new)) {\n+\t\t\tif (!(not_new && PTR_ERR(new) == -ENOENT))\n+\t\t\t\tprintf(\"%s: needs update\\n\", ce->name);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tactive_cache[i] = new;\n+\t}\n+}\n+\n+/*\n+ * We fundamentally don't like some paths: we don't want\n+ * dot or dot-dot anywhere, and in fact, we don't even want\n+ * any other dot-files (.git or anything else). They\n+ * are hidden, for chist sake.\n+ *\n+ * Also, we don't want double slashes or slashes at the\n+ * end that can make pathnames ambiguous.\n+ */\n+static int verify_path(char *path)\n+{\n+\tchar c;\n+\n+\tgoto inside;\n+\tfor (;;) {\n+\t\tif (!c)\n+\t\t\treturn 1;\n+\t\tif (c == '/') {\n+inside:\n+\t\t\tc = *path++;\n+\t\t\tif (c != '/' && c != '.' && c != '\\0')\n+\t\t\t\tcontinue;\n+\t\t\treturn 0;\n+\t\t}\n+\t\tc = *path++;\n+\t}\n+}\n+\n+static int add_cacheinfo(char *arg1, char *arg2, char *arg3)\n+{\n+\tint size, len;\n+\tunsigned int mode;\n+\tunsigned char sha1[20];\n+\tstruct cache_entry *ce;\n+\n+\tif (sscanf(arg1, \"%o\", &mode) != 1)\n+\t\treturn -1;\n+\tif (get_sha1_hex(arg2, sha1))\n+\t\treturn -1;\n+\tif (!verify_path(arg3))\n+\t\treturn -1;\n+\n+\tlen = strlen(arg3);\n+\tsize = cache_entry_size(len);\n+\tce = xmalloc(size);\n+\tmemset(ce, 0, size);\n+\n+\tmemcpy(ce->sha1, sha1, 20);\n+\tmemcpy(ce->name, arg3, len);\n+\tce->ce_flags = htons(len);\n+\tce->ce_mode = create_ce_mode(mode);\n+\treturn add_cache_entry(ce, allow_add);\n+}\n+\n+static const char *lockfile_name = NULL;\n+\n+static void remove_lock_file(void)\n+{\n+\tif (lockfile_name)\n+\t\tunlink(lockfile_name);\n+}\n+\n+static void remove_lock_file_on_signal(int signo)\n+{\n+\tremove_lock_file();\n+}\n+\n+int main(int argc, char **argv)\n+{\n+\tint i, newfd, entries;\n+\tint allow_options = 1;\n+\tstatic char lockfile[MAXPATHLEN+1];\n+\tconst char *indexfile = get_index_file();\n+\n+\tsnprintf(lockfile, sizeof(lockfile), \"%s.lock\", indexfile);\n+\n+\tnewfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);\n+\tif (newfd < 0)\n+\t\tdie(\"unable to create new cachefile\");\n+\n+\tsignal(SIGINT, remove_lock_file_on_signal);\n+\tatexit(remove_lock_file);\n+\tlockfile_name = lockfile;\n+\n+\tentries = read_cache();\n+\tif (entries < 0)\n+\t\tdie(\"cache corrupted\");\n+\n+\tfor (i = 1 ; i < argc; i++) {\n+\t\tchar *path = argv[i];\n+\n+\t\tif (allow_options && *path == '-') {\n+\t\t\tif (!strcmp(path, \"--\")) {\n+\t\t\t\tallow_options = 0;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strcmp(path, \"--add\")) {\n+\t\t\t\tallow_add = 1;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strcmp(path, \"--remove\")) {\n+\t\t\t\tallow_remove = 1;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strcmp(path, \"--refresh\")) {\n+\t\t\t\trefresh_cache();\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strcmp(path, \"--cacheinfo\")) {\n+\t\t\t\tif (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))\n+\t\t\t\t\tdie(\"update-cache: --cacheinfo <mode> <sha1> <path>\");\n+\t\t\t\ti += 3;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strcmp(path, \"--ignore-missing\")) {\n+\t\t\t\tnot_new = 1;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tdie(\"unknown option %s\", path);\n+\t\t}\n+\t\tif (!verify_path(path)) {\n+\t\t\tfprintf(stderr, \"Ignoring path %s\\n\", argv[i]);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (add_file_to_cache(path))\n+\t\t\tdie(\"Unable to add %s to database\", path);\n+\t}\n+\tif (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))\n+\t\tdie(\"Unable to write new cachefile\");\n+\n+\tlockfile_name = NULL;\n+\treturn 0;\n+}\n"},{"id":"1977","messageId":"20050428021547.GB8612@pasky.ji.cz","threadId":"365","inReplyTo":"Pine.LNX.4.62.0504272141560.14033@localhost.localdomain","subject":"Re: [PATCH] add a diff-files command (revised)","fromName":"Petr Baudis","fromEmail":"pasky@ucw.cz","sentAt":"2005-04-28T02:15:47Z","receivedAt":"2005-04-28T02:15:47Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Thu, Apr 28, 2005 at 04:06:29AM CEST, I got a letter\nwhere Nicolas Pitre <nico@cam.org> told me that...\n> In the same spirit as diff-tree and diff-cache, here is a diff-files \n> command that processes differences between the index cache and the \n> working directory content.  It produces lists of files that are either \n> changed, deleted and/or unknown with regards to the current cache, \n> content. The -p option can also be used to generate a patch describing \n> the differences in patch form.\n\nExcept some usage enhancement, how does this differ from show-diff?\n\nAlso, for some reason you have update-cache.c in your patch too.\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nC++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor\n"},{"id":"1980","messageId":"Pine.LNX.4.62.0504272229290.14033@localhost.localdomain","threadId":"365","inReplyTo":"20050428021547.GB8612@pasky.ji.cz","subject":"Re: [PATCH] add a diff-files command (revised)","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2005-04-28T02:39:00Z","receivedAt":"2005-04-28T02:39:00Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"On Thu, 28 Apr 2005, Petr Baudis wrote:\n\n> Dear diary, on Thu, Apr 28, 2005 at 04:06:29AM CEST, I got a letter\n> where Nicolas Pitre <nico@cam.org> told me that...\n> > In the same spirit as diff-tree and diff-cache, here is a diff-files \n> > command that processes differences between the index cache and the \n> > working directory content.  It produces lists of files that are either \n> > changed, deleted and/or unknown with regards to the current cache, \n> > content. The -p option can also be used to generate a patch describing \n> > the differences in patch form.\n> \n> Except some usage enhancement, how does this differ from show-diff?\n\nThis is intended to supercede show-diff.  But since its argument list is \ndifferent I thought creating a new command would be nicer while \nshow-diff usage (which has accumulated cruft already with now unused \nswitches) is phased out.  Also the name \"diff-files\" is more inline with \nthe other diff commands.\n\n> Also, for some reason you have update-cache.c in your patch too.\n\nHuh!?\n\n\nNicolas\n"}]}