# ... and all the rest that could be moved out of bindir to gitexecdir
PROGRAMS = \
- git-checkout-index$X \
git-convert-objects$X git-fetch-pack$X git-fsck-objects$X \
git-hash-object$X git-index-pack$X git-local-fetch$X \
git-merge-base$X \
- git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
+ git-merge-index$X git-mktag$X git-mktree$X git-patch-id$X \
git-peek-remote$X git-receive-pack$X \
git-send-pack$X git-shell$X \
git-show-index$X git-ssh-fetch$X \
git-ssh-upload$X git-unpack-file$X \
- git-unpack-objects$X git-update-server-info$X \
+ git-update-server-info$X \
git-upload-pack$X git-verify-pack$X \
- git-symbolic-ref$X \
- git-name-rev$X git-pack-redundant$X git-var$X \
+ git-pack-redundant$X git-var$X \
git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
-BUILT_INS = git-log$X git-whatchanged$X git-show$X git-update-ref$X \
- git-count-objects$X git-diff$X git-push$X git-mailsplit$X \
- git-grep$X git-add$X git-rm$X git-rev-list$X git-stripspace$X \
- git-check-ref-format$X git-rev-parse$X git-mailinfo$X \
- git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
- git-ls-files$X git-ls-tree$X git-get-tar-commit-id$X \
- git-read-tree$X git-commit-tree$X git-write-tree$X \
- git-apply$X git-show-branch$X git-diff-files$X git-update-index$X \
- git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X \
- git-fmt-merge-msg$X git-prune$X git-mv$X git-prune-packed$X \
- git-repo-config$X
+BUILT_INS = \
+ git-format-patch$X git-show$X git-whatchanged$X \
+ git-get-tar-commit-id$X \
+ $(patsubst builtin-%.o,git-%$X,$(BUILTIN_OBJS))
# what 'all' will build and 'install' will install, in gitexecdir
ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)
blob.h cache.h commit.h csum-file.h delta.h \
diff.h object.h pack.h pkt-line.h quote.h refs.h \
run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
- tree-walk.h log-tree.h dir.h path-list.h
+ tree-walk.h log-tree.h dir.h path-list.h builtin.h
DIFF_OBJS = \
diff.o diff-lib.o diffcore-break.o diffcore-order.o \
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
- alloc.o merge-file.o path-list.o $(DIFF_OBJS)
+ alloc.o merge-file.o path-list.o help.o $(DIFF_OBJS)
BUILTIN_OBJS = \
- builtin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \
- builtin-grep.o builtin-add.o builtin-rev-list.o builtin-check-ref-format.o \
- builtin-rm.o builtin-init-db.o builtin-rev-parse.o \
- builtin-tar-tree.o builtin-upload-tar.o builtin-update-index.o \
- builtin-ls-files.o builtin-ls-tree.o builtin-write-tree.o \
- builtin-read-tree.o builtin-commit-tree.o builtin-mailinfo.o \
- builtin-apply.o builtin-show-branch.o builtin-diff-files.o \
- builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \
- builtin-cat-file.o builtin-mailsplit.o builtin-stripspace.o \
- builtin-update-ref.o builtin-fmt-merge-msg.o builtin-prune.o \
- builtin-mv.o builtin-prune-packed.o builtin-repo-config.o
+ builtin-add.o \
+ builtin-apply.o \
+ builtin-cat-file.o \
+ builtin-checkout-index.o \
+ builtin-check-ref-format.o \
+ builtin-commit-tree.o \
+ builtin-count-objects.o \
+ builtin-diff.o \
+ builtin-diff-files.o \
+ builtin-diff-index.o \
+ builtin-diff-stages.o \
+ builtin-diff-tree.o \
+ builtin-fmt-merge-msg.o \
+ builtin-grep.o \
+ builtin-init-db.o \
+ builtin-log.o \
+ builtin-ls-files.o \
+ builtin-ls-tree.o \
+ builtin-mailinfo.o \
+ builtin-mailsplit.o \
+ builtin-mv.o \
+ builtin-name-rev.o \
+ builtin-pack-objects.o \
+ builtin-prune.o \
+ builtin-prune-packed.o \
+ builtin-push.o \
+ builtin-read-tree.o \
+ builtin-repo-config.o \
+ builtin-rev-list.o \
+ builtin-rev-parse.o \
+ builtin-rm.o \
+ builtin-show-branch.o \
+ builtin-stripspace.o \
+ builtin-symbolic-ref.o \
+ builtin-tar-tree.o \
+ builtin-unpack-objects.o \
+ builtin-update-index.o \
+ builtin-update-ref.o \
+ builtin-upload-tar.o \
+ builtin-write-tree.o
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
LIBS = $(GITLIBS) -lz
$(ALL_CFLAGS) -o $@ $(filter %.c,$^) \
$(BUILTIN_OBJS) $(ALL_LDFLAGS) $(LIBS)
-builtin-help.o: common-cmds.h
+help.o: common-cmds.h
$(BUILT_INS): git$X
rm -f $@ && ln git$X $@
--- /dev/null
+/*
+ * Check-out files from the "current cache directory"
+ *
+ * Copyright (C) 2005 Linus Torvalds
+ *
+ * Careful: order of argument flags does matter. For example,
+ *
+ * git-checkout-index -a -f file.c
+ *
+ * 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).
+ *
+ * Also, just doing "git-checkout-index" does nothing. You probably
+ * meant "git-checkout-index -a". And if you want to force it, you
+ * want "git-checkout-index -f -a".
+ *
+ * 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
+ *
+ * find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
+ *
+ * or:
+ *
+ * find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
+ *
+ * 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!).
+ */
+#include "cache.h"
+#include "strbuf.h"
+#include "quote.h"
+#include "cache-tree.h"
+
+#define CHECKOUT_ALL 4
+static int line_termination = '\n';
+static int checkout_stage; /* default to checkout stage0 */
+static int to_tempfile;
+static char topath[4][MAXPATHLEN+1];
+
+static struct checkout state;
+
+static void write_tempfile_record(const char *name, int prefix_length)
+{
+ int i;
+
+ if (CHECKOUT_ALL == checkout_stage) {
+ for (i = 1; i < 4; i++) {
+ if (i > 1)
+ putchar(' ');
+ if (topath[i][0])
+ fputs(topath[i], stdout);
+ else
+ putchar('.');
+ }
+ } else
+ fputs(topath[checkout_stage], stdout);
+
+ putchar('\t');
+ write_name_quoted("", 0, name + prefix_length,
+ line_termination, stdout);
+ putchar(line_termination);
+
+ for (i = 0; i < 4; i++) {
+ topath[i][0] = 0;
+ }
+}
+
+static int checkout_file(const char *name, int prefix_length)
+{
+ int namelen = strlen(name);
+ int pos = cache_name_pos(name, namelen);
+ int has_same_name = 0;
+ int did_checkout = 0;
+ int errs = 0;
+
+ if (pos < 0)
+ pos = -pos - 1;
+
+ while (pos < active_nr) {
+ struct cache_entry *ce = active_cache[pos];
+ if (ce_namelen(ce) != namelen ||
+ memcmp(ce->name, name, namelen))
+ break;
+ has_same_name = 1;
+ pos++;
+ if (ce_stage(ce) != checkout_stage
+ && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
+ continue;
+ did_checkout = 1;
+ if (checkout_entry(ce, &state,
+ to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
+ errs++;
+ }
+
+ if (did_checkout) {
+ if (to_tempfile)
+ write_tempfile_record(name, prefix_length);
+ return errs > 0 ? -1 : 0;
+ }
+
+ if (!state.quiet) {
+ fprintf(stderr, "git-checkout-index: %s ", name);
+ if (!has_same_name)
+ fprintf(stderr, "is not in the cache");
+ else if (checkout_stage)
+ fprintf(stderr, "does not exist at stage %d",
+ checkout_stage);
+ else
+ fprintf(stderr, "is unmerged");
+ fputc('\n', stderr);
+ }
+ return -1;
+}
+
+static int checkout_all(const char *prefix, int prefix_length)
+{
+ int i, errs = 0;
+ struct cache_entry* last_ce = NULL;
+
+ for (i = 0; i < active_nr ; i++) {
+ struct cache_entry *ce = active_cache[i];
+ if (ce_stage(ce) != checkout_stage
+ && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
+ continue;
+ if (prefix && *prefix &&
+ (ce_namelen(ce) <= prefix_length ||
+ memcmp(prefix, ce->name, prefix_length)))
+ continue;
+ if (last_ce && to_tempfile) {
+ if (ce_namelen(last_ce) != ce_namelen(ce)
+ || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
+ write_tempfile_record(last_ce->name, prefix_length);
+ }
+ if (checkout_entry(ce, &state,
+ to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
+ errs++;
+ last_ce = ce;
+ }
+ if (last_ce && to_tempfile)
+ write_tempfile_record(last_ce->name, prefix_length);
+ if (errs)
+ /* we have already done our error reporting.
+ * exit with the same code as die().
+ */
+ exit(128);
+ return 0;
+}
+
+static const char checkout_cache_usage[] =
+"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
+
+static struct lock_file lock_file;
+
+int cmd_checkout_index(int argc, const char **argv, const char *prefix)
+{
+ int i;
+ int newfd = -1;
+ int all = 0;
+ int read_from_stdin = 0;
+ int prefix_length;
+
+ git_config(git_default_config);
+ state.base_dir = "";
+ prefix_length = prefix ? strlen(prefix) : 0;
+
+ if (read_cache() < 0) {
+ die("invalid cache");
+ }
+
+ for (i = 1; i < argc; i++) {
+ const char *arg = argv[i];
+
+ if (!strcmp(arg, "--")) {
+ i++;
+ break;
+ }
+ if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
+ all = 1;
+ continue;
+ }
+ if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
+ state.force = 1;
+ continue;
+ }
+ if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
+ state.quiet = 1;
+ continue;
+ }
+ if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
+ state.not_new = 1;
+ continue;
+ }
+ if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
+ state.refresh_cache = 1;
+ if (newfd < 0)
+ newfd = hold_lock_file_for_update
+ (&lock_file, get_index_file());
+ if (newfd < 0)
+ die("cannot open index.lock file.");
+ continue;
+ }
+ if (!strcmp(arg, "-z")) {
+ line_termination = 0;
+ continue;
+ }
+ if (!strcmp(arg, "--stdin")) {
+ if (i != argc - 1)
+ die("--stdin must be at the end");
+ read_from_stdin = 1;
+ i++; /* do not consider arg as a file name */
+ break;
+ }
+ if (!strcmp(arg, "--temp")) {
+ to_tempfile = 1;
+ continue;
+ }
+ if (!strncmp(arg, "--prefix=", 9)) {
+ state.base_dir = arg+9;
+ state.base_dir_len = strlen(state.base_dir);
+ continue;
+ }
+ if (!strncmp(arg, "--stage=", 8)) {
+ if (!strcmp(arg + 8, "all")) {
+ to_tempfile = 1;
+ checkout_stage = CHECKOUT_ALL;
+ } else {
+ int ch = arg[8];
+ if ('1' <= ch && ch <= '3')
+ checkout_stage = arg[8] - '0';
+ else
+ die("stage should be between 1 and 3 or all");
+ }
+ continue;
+ }
+ if (arg[0] == '-')
+ usage(checkout_cache_usage);
+ break;
+ }
+
+ if (state.base_dir_len || to_tempfile) {
+ /* when --prefix is specified we do not
+ * want to update cache.
+ */
+ if (state.refresh_cache) {
+ close(newfd); newfd = -1;
+ rollback_lock_file(&lock_file);
+ }
+ state.refresh_cache = 0;
+ }
+
+ /* Check out named files first */
+ for ( ; i < argc; i++) {
+ const char *arg = argv[i];
+ const char *p;
+
+ if (all)
+ die("git-checkout-index: don't mix '--all' and explicit filenames");
+ if (read_from_stdin)
+ die("git-checkout-index: don't mix '--stdin' and explicit filenames");
+ p = prefix_path(prefix, prefix_length, arg);
+ checkout_file(p, prefix_length);
+ if (p < arg || p > arg + strlen(arg))
+ free((char*)p);
+ }
+
+ if (read_from_stdin) {
+ struct strbuf buf;
+ if (all)
+ die("git-checkout-index: don't mix '--all' and '--stdin'");
+ strbuf_init(&buf);
+ while (1) {
+ char *path_name;
+ const char *p;
+
+ read_line(&buf, stdin, line_termination);
+ if (buf.eof)
+ break;
+ if (line_termination && buf.buf[0] == '"')
+ path_name = unquote_c_style(buf.buf, NULL);
+ else
+ path_name = buf.buf;
+ p = prefix_path(prefix, prefix_length, path_name);
+ checkout_file(p, prefix_length);
+ if (p < path_name || p > path_name + strlen(path_name))
+ free((char *)p);
+ if (path_name != buf.buf)
+ free(path_name);
+ }
+ }
+
+ if (all)
+ checkout_all(prefix, prefix_length);
+
+ if (0 <= newfd &&
+ (write_cache(newfd, active_cache, active_nr) ||
+ close(newfd) || commit_lock_file(&lock_file)))
+ die("Unable to write new index file");
+ return 0;
+}
--- /dev/null
+/*
+ * Builtin "git count-objects".
+ *
+ * Copyright (c) 2006 Junio C Hamano
+ */
+
+#include "cache.h"
+#include "builtin.h"
+
+static const char count_objects_usage[] = "git-count-objects [-v]";
+
+static void count_objects(DIR *d, char *path, int len, int verbose,
+ unsigned long *loose,
+ unsigned long *loose_size,
+ unsigned long *packed_loose,
+ unsigned long *garbage)
+{
+ struct dirent *ent;
+ while ((ent = readdir(d)) != NULL) {
+ char hex[41];
+ unsigned char sha1[20];
+ const char *cp;
+ int bad = 0;
+
+ if ((ent->d_name[0] == '.') &&
+ (ent->d_name[1] == 0 ||
+ ((ent->d_name[1] == '.') && (ent->d_name[2] == 0))))
+ continue;
+ for (cp = ent->d_name; *cp; cp++) {
+ int ch = *cp;
+ if (('0' <= ch && ch <= '9') ||
+ ('a' <= ch && ch <= 'f'))
+ continue;
+ bad = 1;
+ break;
+ }
+ if (cp - ent->d_name != 38)
+ bad = 1;
+ else {
+ struct stat st;
+ memcpy(path + len + 3, ent->d_name, 38);
+ path[len + 2] = '/';
+ path[len + 41] = 0;
+ if (lstat(path, &st) || !S_ISREG(st.st_mode))
+ bad = 1;
+ else
+ (*loose_size) += st.st_blocks;
+ }
+ if (bad) {
+ if (verbose) {
+ error("garbage found: %.*s/%s",
+ len + 2, path, ent->d_name);
+ (*garbage)++;
+ }
+ continue;
+ }
+ (*loose)++;
+ if (!verbose)
+ continue;
+ memcpy(hex, path+len, 2);
+ memcpy(hex+2, ent->d_name, 38);
+ hex[40] = 0;
+ if (get_sha1_hex(hex, sha1))
+ die("internal error");
+ if (has_sha1_pack(sha1))
+ (*packed_loose)++;
+ }
+}
+
+int cmd_count_objects(int ac, const char **av, const char *prefix)
+{
+ int i;
+ int verbose = 0;
+ const char *objdir = get_object_directory();
+ int len = strlen(objdir);
+ char *path = xmalloc(len + 50);
+ unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
+ unsigned long loose_size = 0;
+
+ for (i = 1; i < ac; i++) {
+ const char *arg = av[i];
+ if (*arg != '-')
+ break;
+ else if (!strcmp(arg, "-v"))
+ verbose = 1;
+ else
+ usage(count_objects_usage);
+ }
+
+ /* we do not take arguments other than flags for now */
+ if (i < ac)
+ usage(count_objects_usage);
+ memcpy(path, objdir, len);
+ if (len && objdir[len-1] != '/')
+ path[len++] = '/';
+ for (i = 0; i < 256; i++) {
+ DIR *d;
+ sprintf(path + len, "%02x", i);
+ d = opendir(path);
+ if (!d)
+ continue;
+ count_objects(d, path, len, verbose,
+ &loose, &loose_size, &packed_loose, &garbage);
+ closedir(d);
+ }
+ if (verbose) {
+ struct packed_git *p;
+ if (!packed_git)
+ prepare_packed_git();
+ for (p = packed_git; p; p = p->next) {
+ if (!p->pack_local)
+ continue;
+ packed += num_packed_objects(p);
+ }
+ printf("count: %lu\n", loose);
+ printf("size: %lu\n", loose_size / 2);
+ printf("in-pack: %lu\n", packed);
+ printf("prune-packable: %lu\n", packed_loose);
+ printf("garbage: %lu\n", garbage);
+ }
+ else
+ printf("%lu objects, %lu kilobytes\n",
+ loose, loose_size / 2);
+ return 0;
+}
+++ /dev/null
-/*
- * Builtin "git count-objects".
- *
- * Copyright (c) 2006 Junio C Hamano
- */
-
-#include "cache.h"
-#include "builtin.h"
-
-static const char count_objects_usage[] = "git-count-objects [-v]";
-
-static void count_objects(DIR *d, char *path, int len, int verbose,
- unsigned long *loose,
- unsigned long *loose_size,
- unsigned long *packed_loose,
- unsigned long *garbage)
-{
- struct dirent *ent;
- while ((ent = readdir(d)) != NULL) {
- char hex[41];
- unsigned char sha1[20];
- const char *cp;
- int bad = 0;
-
- if ((ent->d_name[0] == '.') &&
- (ent->d_name[1] == 0 ||
- ((ent->d_name[1] == '.') && (ent->d_name[2] == 0))))
- continue;
- for (cp = ent->d_name; *cp; cp++) {
- int ch = *cp;
- if (('0' <= ch && ch <= '9') ||
- ('a' <= ch && ch <= 'f'))
- continue;
- bad = 1;
- break;
- }
- if (cp - ent->d_name != 38)
- bad = 1;
- else {
- struct stat st;
- memcpy(path + len + 3, ent->d_name, 38);
- path[len + 2] = '/';
- path[len + 41] = 0;
- if (lstat(path, &st) || !S_ISREG(st.st_mode))
- bad = 1;
- else
- (*loose_size) += st.st_blocks;
- }
- if (bad) {
- if (verbose) {
- error("garbage found: %.*s/%s",
- len + 2, path, ent->d_name);
- (*garbage)++;
- }
- continue;
- }
- (*loose)++;
- if (!verbose)
- continue;
- memcpy(hex, path+len, 2);
- memcpy(hex+2, ent->d_name, 38);
- hex[40] = 0;
- if (get_sha1_hex(hex, sha1))
- die("internal error");
- if (has_sha1_pack(sha1))
- (*packed_loose)++;
- }
-}
-
-int cmd_count_objects(int ac, const char **av, const char *prefix)
-{
- int i;
- int verbose = 0;
- const char *objdir = get_object_directory();
- int len = strlen(objdir);
- char *path = xmalloc(len + 50);
- unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
- unsigned long loose_size = 0;
-
- for (i = 1; i < ac; i++) {
- const char *arg = av[i];
- if (*arg != '-')
- break;
- else if (!strcmp(arg, "-v"))
- verbose = 1;
- else
- usage(count_objects_usage);
- }
-
- /* we do not take arguments other than flags for now */
- if (i < ac)
- usage(count_objects_usage);
- memcpy(path, objdir, len);
- if (len && objdir[len-1] != '/')
- path[len++] = '/';
- for (i = 0; i < 256; i++) {
- DIR *d;
- sprintf(path + len, "%02x", i);
- d = opendir(path);
- if (!d)
- continue;
- count_objects(d, path, len, verbose,
- &loose, &loose_size, &packed_loose, &garbage);
- closedir(d);
- }
- if (verbose) {
- struct packed_git *p;
- if (!packed_git)
- prepare_packed_git();
- for (p = packed_git; p; p = p->next) {
- if (!p->pack_local)
- continue;
- packed += num_packed_objects(p);
- }
- printf("count: %lu\n", loose);
- printf("size: %lu\n", loose_size / 2);
- printf("in-pack: %lu\n", packed);
- printf("prune-packable: %lu\n", packed_loose);
- printf("garbage: %lu\n", garbage);
- }
- else
- printf("%lu objects, %lu kilobytes\n",
- loose, loose_size / 2);
- return 0;
-}
+++ /dev/null
-/*
- * builtin-help.c
- *
- * Builtin help-related commands (help, usage, version)
- */
-#include <sys/ioctl.h>
-#include "cache.h"
-#include "builtin.h"
-#include "exec_cmd.h"
-#include "common-cmds.h"
-
-
-/* most GUI terminals set COLUMNS (although some don't export it) */
-static int term_columns(void)
-{
- char *col_string = getenv("COLUMNS");
- int n_cols = 0;
-
- if (col_string && (n_cols = atoi(col_string)) > 0)
- return n_cols;
-
-#ifdef TIOCGWINSZ
- {
- struct winsize ws;
- if (!ioctl(1, TIOCGWINSZ, &ws)) {
- if (ws.ws_col)
- return ws.ws_col;
- }
- }
-#endif
-
- return 80;
-}
-
-static void oom(void)
-{
- fprintf(stderr, "git: out of memory\n");
- exit(1);
-}
-
-static inline void mput_char(char c, unsigned int num)
-{
- while(num--)
- putchar(c);
-}
-
-static struct cmdname {
- size_t len;
- char name[1];
-} **cmdname;
-static int cmdname_alloc, cmdname_cnt;
-
-static void add_cmdname(const char *name, int len)
-{
- struct cmdname *ent;
- if (cmdname_alloc <= cmdname_cnt) {
- cmdname_alloc = cmdname_alloc + 200;
- cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
- if (!cmdname)
- oom();
- }
- ent = malloc(sizeof(*ent) + len);
- if (!ent)
- oom();
- ent->len = len;
- memcpy(ent->name, name, len);
- ent->name[len] = 0;
- cmdname[cmdname_cnt++] = ent;
-}
-
-static int cmdname_compare(const void *a_, const void *b_)
-{
- struct cmdname *a = *(struct cmdname **)a_;
- struct cmdname *b = *(struct cmdname **)b_;
- return strcmp(a->name, b->name);
-}
-
-static void pretty_print_string_list(struct cmdname **cmdname, int longest)
-{
- int cols = 1, rows;
- int space = longest + 1; /* min 1 SP between words */
- int max_cols = term_columns() - 1; /* don't print *on* the edge */
- int i, j;
-
- if (space < max_cols)
- cols = max_cols / space;
- rows = (cmdname_cnt + cols - 1) / cols;
-
- qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
-
- for (i = 0; i < rows; i++) {
- printf(" ");
-
- for (j = 0; j < cols; j++) {
- int n = j * rows + i;
- int size = space;
- if (n >= cmdname_cnt)
- break;
- if (j == cols-1 || n + rows >= cmdname_cnt)
- size = 1;
- printf("%-*s", size, cmdname[n]->name);
- }
- putchar('\n');
- }
-}
-
-static void list_commands(const char *exec_path, const char *pattern)
-{
- unsigned int longest = 0;
- char path[PATH_MAX];
- int dirlen;
- DIR *dir = opendir(exec_path);
- struct dirent *de;
-
- if (!dir) {
- fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
- exit(1);
- }
-
- dirlen = strlen(exec_path);
- if (PATH_MAX - 20 < dirlen) {
- fprintf(stderr, "git: insanely long exec-path '%s'\n",
- exec_path);
- exit(1);
- }
-
- memcpy(path, exec_path, dirlen);
- path[dirlen++] = '/';
-
- while ((de = readdir(dir)) != NULL) {
- struct stat st;
- int entlen;
-
- if (strncmp(de->d_name, "git-", 4))
- continue;
- strcpy(path+dirlen, de->d_name);
- if (stat(path, &st) || /* stat, not lstat */
- !S_ISREG(st.st_mode) ||
- !(st.st_mode & S_IXUSR))
- continue;
-
- entlen = strlen(de->d_name);
- if (has_extension(de->d_name, entlen, ".exe"))
- entlen -= 4;
-
- if (longest < entlen)
- longest = entlen;
-
- add_cmdname(de->d_name + 4, entlen-4);
- }
- closedir(dir);
-
- printf("git commands available in '%s'\n", exec_path);
- printf("----------------------------");
- mput_char('-', strlen(exec_path));
- putchar('\n');
- pretty_print_string_list(cmdname, longest - 4);
- putchar('\n');
-}
-
-static void list_common_cmds_help(void)
-{
- int i, longest = 0;
-
- for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
- if (longest < strlen(common_cmds[i].name))
- longest = strlen(common_cmds[i].name);
- }
-
- puts("The most commonly used git commands are:");
- for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
- printf(" %s", common_cmds[i].name);
- mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
- puts(common_cmds[i].help);
- }
- puts("(use 'git help -a' to get a list of all installed git commands)");
-}
-
-static void show_man_page(const char *git_cmd)
-{
- const char *page;
-
- if (!strncmp(git_cmd, "git", 3))
- page = git_cmd;
- else {
- int page_len = strlen(git_cmd) + 4;
- char *p = malloc(page_len + 1);
- strcpy(p, "git-");
- strcpy(p + 4, git_cmd);
- p[page_len] = 0;
- page = p;
- }
-
- execlp("man", "man", page, NULL);
-}
-
-void help_unknown_cmd(const char *cmd)
-{
- printf("git: '%s' is not a git-command\n\n", cmd);
- list_common_cmds_help();
- exit(1);
-}
-
-int cmd_version(int argc, const char **argv, const char *prefix)
-{
- printf("git version %s\n", git_version_string);
- return 0;
-}
-
-int cmd_help(int argc, const char **argv, const char *prefix)
-{
- const char *help_cmd = argc > 1 ? argv[1] : NULL;
- const char *exec_path = git_exec_path();
-
- if (!help_cmd) {
- printf("usage: %s\n\n", git_usage_string);
- list_common_cmds_help();
- exit(1);
- }
-
- else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
- printf("usage: %s\n\n", git_usage_string);
- if(exec_path)
- list_commands(exec_path, "git-*");
- exit(1);
- }
-
- else
- show_man_page(help_cmd);
-
- return 0;
-}
-
-
--- /dev/null
+#include <stdlib.h>
+#include "builtin.h"
+#include "cache.h"
+#include "commit.h"
+#include "tag.h"
+#include "refs.h"
+
+static const char name_rev_usage[] =
+ "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
+
+typedef struct rev_name {
+ const char *tip_name;
+ int merge_traversals;
+ int generation;
+} rev_name;
+
+static long cutoff = LONG_MAX;
+
+static void name_rev(struct commit *commit,
+ const char *tip_name, int merge_traversals, int generation,
+ int deref)
+{
+ struct rev_name *name = (struct rev_name *)commit->util;
+ struct commit_list *parents;
+ int parent_number = 1;
+
+ if (!commit->object.parsed)
+ parse_commit(commit);
+
+ if (commit->date < cutoff)
+ return;
+
+ if (deref) {
+ char *new_name = xmalloc(strlen(tip_name)+3);
+ strcpy(new_name, tip_name);
+ strcat(new_name, "^0");
+ tip_name = new_name;
+
+ if (generation)
+ die("generation: %d, but deref?", generation);
+ }
+
+ if (name == NULL) {
+ name = xmalloc(sizeof(rev_name));
+ commit->util = name;
+ goto copy_data;
+ } else if (name->merge_traversals > merge_traversals ||
+ (name->merge_traversals == merge_traversals &&
+ name->generation > generation)) {
+copy_data:
+ name->tip_name = tip_name;
+ name->merge_traversals = merge_traversals;
+ name->generation = generation;
+ } else
+ return;
+
+ for (parents = commit->parents;
+ parents;
+ parents = parents->next, parent_number++) {
+ if (parent_number > 1) {
+ char *new_name = xmalloc(strlen(tip_name)+8);
+
+ if (generation > 0)
+ sprintf(new_name, "%s~%d^%d", tip_name,
+ generation, parent_number);
+ else
+ sprintf(new_name, "%s^%d", tip_name, parent_number);
+
+ name_rev(parents->item, new_name,
+ merge_traversals + 1 , 0, 0);
+ } else {
+ name_rev(parents->item, tip_name, merge_traversals,
+ generation + 1, 0);
+ }
+ }
+}
+
+static int tags_only = 0;
+
+static int name_ref(const char *path, const unsigned char *sha1)
+{
+ struct object *o = parse_object(sha1);
+ int deref = 0;
+
+ if (tags_only && strncmp(path, "refs/tags/", 10))
+ return 0;
+
+ while (o && o->type == OBJ_TAG) {
+ struct tag *t = (struct tag *) o;
+ if (!t->tagged)
+ break; /* broken repository */
+ o = parse_object(t->tagged->sha1);
+ deref = 1;
+ }
+ if (o && o->type == OBJ_COMMIT) {
+ struct commit *commit = (struct commit *)o;
+
+ if (!strncmp(path, "refs/heads/", 11))
+ path = path + 11;
+ else if (!strncmp(path, "refs/", 5))
+ path = path + 5;
+
+ name_rev(commit, strdup(path), 0, 0, deref);
+ }
+ return 0;
+}
+
+/* returns a static buffer */
+static const char* get_rev_name(struct object *o)
+{
+ static char buffer[1024];
+ struct rev_name *n;
+ struct commit *c;
+
+ if (o->type != OBJ_COMMIT)
+ return "undefined";
+ c = (struct commit *) o;
+ n = c->util;
+ if (!n)
+ return "undefined";
+
+ if (!n->generation)
+ return n->tip_name;
+
+ snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
+
+ return buffer;
+}
+
+int cmd_name_rev(int argc, const char **argv, const char *prefix)
+{
+ struct object_array revs = { 0, 0, NULL };
+ int as_is = 0, all = 0, transform_stdin = 0;
+
+ git_config(git_default_config);
+
+ if (argc < 2)
+ usage(name_rev_usage);
+
+ for (--argc, ++argv; argc; --argc, ++argv) {
+ unsigned char sha1[20];
+ struct object *o;
+ struct commit *commit;
+
+ if (!as_is && (*argv)[0] == '-') {
+ if (!strcmp(*argv, "--")) {
+ as_is = 1;
+ continue;
+ } else if (!strcmp(*argv, "--tags")) {
+ tags_only = 1;
+ continue;
+ } else if (!strcmp(*argv, "--all")) {
+ if (argc > 1)
+ die("Specify either a list, or --all, not both!");
+ all = 1;
+ cutoff = 0;
+ continue;
+ } else if (!strcmp(*argv, "--stdin")) {
+ if (argc > 1)
+ die("Specify either a list, or --stdin, not both!");
+ transform_stdin = 1;
+ cutoff = 0;
+ continue;
+ }
+ usage(name_rev_usage);
+ }
+
+ if (get_sha1(*argv, sha1)) {
+ fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
+ *argv);
+ continue;
+ }
+
+ o = deref_tag(parse_object(sha1), *argv, 0);
+ if (!o || o->type != OBJ_COMMIT) {
+ fprintf(stderr, "Could not get commit for %s. Skipping.\n",
+ *argv);
+ continue;
+ }
+
+ commit = (struct commit *)o;
+
+ if (cutoff > commit->date)
+ cutoff = commit->date;
+
+ add_object_array((struct object *)commit, *argv, &revs);
+ }
+
+ for_each_ref(name_ref);
+
+ if (transform_stdin) {
+ char buffer[2048];
+ char *p, *p_start;
+
+ while (!feof(stdin)) {
+ int forty = 0;
+ p = fgets(buffer, sizeof(buffer), stdin);
+ if (!p)
+ break;
+
+ for (p_start = p; *p; p++) {
+#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
+ if (!ishex(*p))
+ forty = 0;
+ else if (++forty == 40 &&
+ !ishex(*(p+1))) {
+ unsigned char sha1[40];
+ const char *name = "undefined";
+ char c = *(p+1);
+
+ forty = 0;
+
+ *(p+1) = 0;
+ if (!get_sha1(p - 39, sha1)) {
+ struct object *o =
+ lookup_object(sha1);
+ if (o)
+ name = get_rev_name(o);
+ }
+ *(p+1) = c;
+
+ if (!strcmp(name, "undefined"))
+ continue;
+
+ fwrite(p_start, p - p_start + 1, 1,
+ stdout);
+ printf(" (%s)", name);
+ p_start = p + 1;
+ }
+ }
+
+ /* flush */
+ if (p_start != p)
+ fwrite(p_start, p - p_start, 1, stdout);
+ }
+ } else if (all) {
+ int i, max;
+
+ max = get_max_object_index();
+ for (i = 0; i < max; i++) {
+ struct object * obj = get_indexed_object(i);
+ if (!obj)
+ continue;
+ printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
+ }
+ } else {
+ int i;
+ for (i = 0; i < revs.nr; i++)
+ printf("%s %s\n",
+ revs.objects[i].name,
+ get_rev_name(revs.objects[i].item));
+ }
+
+ return 0;
+}
+
--- /dev/null
+#include "builtin.h"
+#include "cache.h"
+#include "object.h"
+#include "blob.h"
+#include "commit.h"
+#include "tag.h"
+#include "tree.h"
+#include "delta.h"
+#include "pack.h"
+#include "csum-file.h"
+#include "tree-walk.h"
+#include <sys/time.h>
+#include <signal.h>
+
+static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
+
+struct object_entry {
+ unsigned char sha1[20];
+ unsigned long size; /* uncompressed size */
+ unsigned long offset; /* offset into the final pack file;
+ * nonzero if already written.
+ */
+ unsigned int depth; /* delta depth */
+ unsigned int delta_limit; /* base adjustment for in-pack delta */
+ unsigned int hash; /* name hint hash */
+ enum object_type type;
+ enum object_type in_pack_type; /* could be delta */
+ unsigned long delta_size; /* delta data size (uncompressed) */
+ struct object_entry *delta; /* delta base object */
+ struct packed_git *in_pack; /* already in pack */
+ unsigned int in_pack_offset;
+ struct object_entry *delta_child; /* deltified objects who bases me */
+ struct object_entry *delta_sibling; /* other deltified objects who
+ * uses the same base as me
+ */
+ int preferred_base; /* we do not pack this, but is encouraged to
+ * be used as the base objectto delta huge
+ * objects against.
+ */
+};
+
+/*
+ * Objects we are going to pack are collected in objects array (dynamically
+ * expanded). nr_objects & nr_alloc controls this array. They are stored
+ * in the order we see -- typically rev-list --objects order that gives us
+ * nice "minimum seek" order.
+ *
+ * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
+ * elements in the objects array. The former is used to build the pack
+ * index (lists object names in the ascending order to help offset lookup),
+ * and the latter is used to group similar things together by try_delta()
+ * heuristics.
+ */
+
+static unsigned char object_list_sha1[20];
+static int non_empty = 0;
+static int no_reuse_delta = 0;
+static int local = 0;
+static int incremental = 0;
+static struct object_entry **sorted_by_sha, **sorted_by_type;
+static struct object_entry *objects = NULL;
+static int nr_objects = 0, nr_alloc = 0, nr_result = 0;
+static const char *base_name;
+static unsigned char pack_file_sha1[20];
+static int progress = 1;
+static volatile sig_atomic_t progress_update = 0;
+static int window = 10;
+
+/*
+ * The object names in objects array are hashed with this hashtable,
+ * to help looking up the entry by object name. Binary search from
+ * sorted_by_sha is also possible but this was easier to code and faster.
+ * This hashtable is built after all the objects are seen.
+ */
+static int *object_ix = NULL;
+static int object_ix_hashsz = 0;
+
+/*
+ * Pack index for existing packs give us easy access to the offsets into
+ * corresponding pack file where each object's data starts, but the entries
+ * do not store the size of the compressed representation (uncompressed
+ * size is easily available by examining the pack entry header). We build
+ * a hashtable of existing packs (pack_revindex), and keep reverse index
+ * here -- pack index file is sorted by object name mapping to offset; this
+ * pack_revindex[].revindex array is an ordered list of offsets, so if you
+ * know the offset of an object, next offset is where its packed
+ * representation ends.
+ */
+struct pack_revindex {
+ struct packed_git *p;
+ unsigned long *revindex;
+} *pack_revindex = NULL;
+static int pack_revindex_hashsz = 0;
+
+/*
+ * stats
+ */
+static int written = 0;
+static int written_delta = 0;
+static int reused = 0;
+static int reused_delta = 0;
+
+static int pack_revindex_ix(struct packed_git *p)
+{
+ unsigned long ui = (unsigned long)p;
+ int i;
+
+ ui = ui ^ (ui >> 16); /* defeat structure alignment */
+ i = (int)(ui % pack_revindex_hashsz);
+ while (pack_revindex[i].p) {
+ if (pack_revindex[i].p == p)
+ return i;
+ if (++i == pack_revindex_hashsz)
+ i = 0;
+ }
+ return -1 - i;
+}
+
+static void prepare_pack_ix(void)
+{
+ int num;
+ struct packed_git *p;
+ for (num = 0, p = packed_git; p; p = p->next)
+ num++;
+ if (!num)
+ return;
+ pack_revindex_hashsz = num * 11;
+ pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
+ for (p = packed_git; p; p = p->next) {
+ num = pack_revindex_ix(p);
+ num = - 1 - num;
+ pack_revindex[num].p = p;
+ }
+ /* revindex elements are lazily initialized */
+}
+
+static int cmp_offset(const void *a_, const void *b_)
+{
+ unsigned long a = *(unsigned long *) a_;
+ unsigned long b = *(unsigned long *) b_;
+ if (a < b)
+ return -1;
+ else if (a == b)
+ return 0;
+ else
+ return 1;
+}
+
+/*
+ * Ordered list of offsets of objects in the pack.
+ */
+static void prepare_pack_revindex(struct pack_revindex *rix)
+{
+ struct packed_git *p = rix->p;
+ int num_ent = num_packed_objects(p);
+ int i;
+ void *index = p->index_base + 256;
+
+ rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
+ for (i = 0; i < num_ent; i++) {
+ unsigned int hl = *((unsigned int *)((char *) index + 24*i));
+ rix->revindex[i] = ntohl(hl);
+ }
+ /* This knows the pack format -- the 20-byte trailer
+ * follows immediately after the last object data.
+ */
+ rix->revindex[num_ent] = p->pack_size - 20;
+ qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
+}
+
+static unsigned long find_packed_object_size(struct packed_git *p,
+ unsigned long ofs)
+{
+ int num;
+ int lo, hi;
+ struct pack_revindex *rix;
+ unsigned long *revindex;
+ num = pack_revindex_ix(p);
+ if (num < 0)
+ die("internal error: pack revindex uninitialized");
+ rix = &pack_revindex[num];
+ if (!rix->revindex)
+ prepare_pack_revindex(rix);
+ revindex = rix->revindex;
+ lo = 0;
+ hi = num_packed_objects(p) + 1;
+ do {
+ int mi = (lo + hi) / 2;
+ if (revindex[mi] == ofs) {
+ return revindex[mi+1] - ofs;
+ }
+ else if (ofs < revindex[mi])
+ hi = mi;
+ else
+ lo = mi + 1;
+ } while (lo < hi);
+ die("internal error: pack revindex corrupt");
+}
+
+static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
+{
+ unsigned long othersize, delta_size;
+ char type[10];
+ void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
+ void *delta_buf;
+
+ if (!otherbuf)
+ die("unable to read %s", sha1_to_hex(entry->delta->sha1));
+ delta_buf = diff_delta(otherbuf, othersize,
+ buf, size, &delta_size, 0);
+ if (!delta_buf || delta_size != entry->delta_size)
+ die("delta size changed");
+ free(buf);
+ free(otherbuf);
+ return delta_buf;
+}
+
+/*
+ * The per-object header is a pretty dense thing, which is
+ * - first byte: low four bits are "size", then three bits of "type",
+ * and the high bit is "size continues".
+ * - each byte afterwards: low seven bits are size continuation,
+ * with the high bit being "size continues"
+ */
+static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
+{
+ int n = 1;
+ unsigned char c;
+
+ if (type < OBJ_COMMIT || type > OBJ_DELTA)
+ die("bad type %d", type);
+
+ c = (type << 4) | (size & 15);
+ size >>= 4;
+ while (size) {
+ *hdr++ = c | 0x80;
+ c = size & 0x7f;
+ size >>= 7;
+ n++;
+ }
+ *hdr = c;
+ return n;
+}
+
+static unsigned long write_object(struct sha1file *f,
+ struct object_entry *entry)
+{
+ unsigned long size;
+ char type[10];
+ void *buf;
+ unsigned char header[10];
+ unsigned hdrlen, datalen;
+ enum object_type obj_type;
+ int to_reuse = 0;
+
+ if (entry->preferred_base)
+ return 0;
+
+ obj_type = entry->type;
+ if (! entry->in_pack)
+ to_reuse = 0; /* can't reuse what we don't have */
+ else if (obj_type == OBJ_DELTA)
+ to_reuse = 1; /* check_object() decided it for us */
+ else if (obj_type != entry->in_pack_type)
+ to_reuse = 0; /* pack has delta which is unusable */
+ else if (entry->delta)
+ to_reuse = 0; /* we want to pack afresh */
+ else
+ to_reuse = 1; /* we have it in-pack undeltified,
+ * and we do not need to deltify it.
+ */
+
+ if (! to_reuse) {
+ buf = read_sha1_file(entry->sha1, type, &size);
+ if (!buf)
+ die("unable to read %s", sha1_to_hex(entry->sha1));
+ if (size != entry->size)
+ die("object %s size inconsistency (%lu vs %lu)",
+ sha1_to_hex(entry->sha1), size, entry->size);
+ if (entry->delta) {
+ buf = delta_against(buf, size, entry);
+ size = entry->delta_size;
+ obj_type = OBJ_DELTA;
+ }
+ /*
+ * The object header is a byte of 'type' followed by zero or
+ * more bytes of length. For deltas, the 20 bytes of delta
+ * sha1 follows that.
+ */
+ hdrlen = encode_header(obj_type, size, header);
+ sha1write(f, header, hdrlen);
+
+ if (entry->delta) {
+ sha1write(f, entry->delta, 20);
+ hdrlen += 20;
+ }
+ datalen = sha1write_compressed(f, buf, size);
+ free(buf);
+ }
+ else {
+ struct packed_git *p = entry->in_pack;
+ use_packed_git(p);
+
+ datalen = find_packed_object_size(p, entry->in_pack_offset);
+ buf = (char *) p->pack_base + entry->in_pack_offset;
+ sha1write(f, buf, datalen);
+ unuse_packed_git(p);
+ hdrlen = 0; /* not really */
+ if (obj_type == OBJ_DELTA)
+ reused_delta++;
+ reused++;
+ }
+ if (obj_type == OBJ_DELTA)
+ written_delta++;
+ written++;
+ return hdrlen + datalen;
+}
+
+static unsigned long write_one(struct sha1file *f,
+ struct object_entry *e,
+ unsigned long offset)
+{
+ if (e->offset)
+ /* offset starts from header size and cannot be zero
+ * if it is written already.
+ */
+ return offset;
+ e->offset = offset;
+ offset += write_object(f, e);
+ /* if we are deltified, write out its base object. */
+ if (e->delta)
+ offset = write_one(f, e->delta, offset);
+ return offset;
+}
+
+static void write_pack_file(void)
+{
+ int i;
+ struct sha1file *f;
+ unsigned long offset;
+ struct pack_header hdr;
+ unsigned last_percent = 999;
+ int do_progress = 0;
+
+ if (!base_name)
+ f = sha1fd(1, "<stdout>");
+ else {
+ f = sha1create("%s-%s.%s", base_name,
+ sha1_to_hex(object_list_sha1), "pack");
+ do_progress = progress;
+ }
+ if (do_progress)
+ fprintf(stderr, "Writing %d objects.\n", nr_result);
+
+ hdr.hdr_signature = htonl(PACK_SIGNATURE);
+ hdr.hdr_version = htonl(PACK_VERSION);
+ hdr.hdr_entries = htonl(nr_result);
+ sha1write(f, &hdr, sizeof(hdr));
+ offset = sizeof(hdr);
+ if (!nr_result)
+ goto done;
+ for (i = 0; i < nr_objects; i++) {
+ offset = write_one(f, objects + i, offset);
+ if (do_progress) {
+ unsigned percent = written * 100 / nr_result;
+ if (progress_update || percent != last_percent) {
+ fprintf(stderr, "%4u%% (%u/%u) done\r",
+ percent, written, nr_result);
+ progress_update = 0;
+ last_percent = percent;
+ }
+ }
+ }
+ if (do_progress)
+ fputc('\n', stderr);
+ done:
+ sha1close(f, pack_file_sha1, 1);
+}
+
+static void write_index_file(void)
+{
+ int i;
+ struct sha1file *f = sha1create("%s-%s.%s", base_name,
+ sha1_to_hex(object_list_sha1), "idx");
+ struct object_entry **list = sorted_by_sha;
+ struct object_entry **last = list + nr_result;
+ unsigned int array[256];
+
+ /*
+ * Write the first-level table (the list is sorted,
+ * but we use a 256-entry lookup to be able to avoid
+ * having to do eight extra binary search iterations).
+ */
+ for (i = 0; i < 256; i++) {
+ struct object_entry **next = list;
+ while (next < last) {
+ struct object_entry *entry = *next;
+ if (entry->sha1[0] != i)
+ break;
+ next++;
+ }
+ array[i] = htonl(next - sorted_by_sha);
+ list = next;
+ }
+ sha1write(f, array, 256 * sizeof(int));
+
+ /*
+ * Write the actual SHA1 entries..
+ */
+ list = sorted_by_sha;
+ for (i = 0; i < nr_result; i++) {
+ struct object_entry *entry = *list++;
+ unsigned int offset = htonl(entry->offset);
+ sha1write(f, &offset, 4);
+ sha1write(f, entry->sha1, 20);
+ }
+ sha1write(f, pack_file_sha1, 20);
+ sha1close(f, NULL, 1);
+}
+
+static int locate_object_entry_hash(const unsigned char *sha1)
+{
+ int i;
+ unsigned int ui;
+ memcpy(&ui, sha1, sizeof(unsigned int));
+ i = ui % object_ix_hashsz;
+ while (0 < object_ix[i]) {
+ if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
+ return i;
+ if (++i == object_ix_hashsz)
+ i = 0;
+ }
+ return -1 - i;
+}
+
+static struct object_entry *locate_object_entry(const unsigned char *sha1)
+{
+ int i;
+
+ if (!object_ix_hashsz)
+ return NULL;
+
+ i = locate_object_entry_hash(sha1);
+ if (0 <= i)
+ return &objects[object_ix[i]-1];
+ return NULL;
+}
+
+static void rehash_objects(void)
+{
+ int i;
+ struct object_entry *oe;
+
+ object_ix_hashsz = nr_objects * 3;
+ if (object_ix_hashsz < 1024)
+ object_ix_hashsz = 1024;
+ object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
+ memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
+ for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
+ int ix = locate_object_entry_hash(oe->sha1);
+ if (0 <= ix)
+ continue;
+ ix = -1 - ix;
+ object_ix[ix] = i + 1;
+ }
+}
+
+static unsigned name_hash(const char *name)
+{
+ unsigned char c;
+ unsigned hash = 0;
+
+ /*
+ * This effectively just creates a sortable number from the
+ * last sixteen non-whitespace characters. Last characters
+ * count "most", so things that end in ".c" sort together.
+ */
+ while ((c = *name++) != 0) {
+ if (isspace(c))
+ continue;
+ hash = (hash >> 2) + (c << 24);
+ }
+ return hash;
+}
+
+static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
+{
+ unsigned int idx = nr_objects;
+ struct object_entry *entry;
+ struct packed_git *p;
+ unsigned int found_offset = 0;
+ struct packed_git *found_pack = NULL;
+ int ix, status = 0;
+
+ if (!exclude) {
+ for (p = packed_git; p; p = p->next) {
+ struct pack_entry e;
+ if (find_pack_entry_one(sha1, &e, p)) {
+ if (incremental)
+ return 0;
+ if (local && !p->pack_local)
+ return 0;
+ if (!found_pack) {
+ found_offset = e.offset;
+ found_pack = e.p;
+ }
+ }
+ }
+ }
+ if ((entry = locate_object_entry(sha1)) != NULL)
+ goto already_added;
+
+ if (idx >= nr_alloc) {
+ unsigned int needed = (idx + 1024) * 3 / 2;
+ objects = xrealloc(objects, needed * sizeof(*entry));
+ nr_alloc = needed;
+ }
+ entry = objects + idx;
+ nr_objects = idx + 1;
+ memset(entry, 0, sizeof(*entry));
+ memcpy(entry->sha1, sha1, 20);
+ entry->hash = hash;
+
+ if (object_ix_hashsz * 3 <= nr_objects * 4)
+ rehash_objects();
+ else {
+ ix = locate_object_entry_hash(entry->sha1);
+ if (0 <= ix)
+ die("internal error in object hashing.");
+ object_ix[-1 - ix] = idx + 1;
+ }
+ status = 1;
+
+ already_added:
+ if (progress_update) {
+ fprintf(stderr, "Counting objects...%d\r", nr_objects);
+ progress_update = 0;
+ }
+ if (exclude)
+ entry->preferred_base = 1;
+ else {
+ if (found_pack) {
+ entry->in_pack = found_pack;
+ entry->in_pack_offset = found_offset;
+ }
+ }
+ return status;
+}
+
+struct pbase_tree_cache {
+ unsigned char sha1[20];
+ int ref;
+ int temporary;
+ void *tree_data;
+ unsigned long tree_size;
+};
+
+static struct pbase_tree_cache *(pbase_tree_cache[256]);
+static int pbase_tree_cache_ix(const unsigned char *sha1)
+{
+ return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
+}
+static int pbase_tree_cache_ix_incr(int ix)
+{
+ return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
+}
+
+static struct pbase_tree {
+ struct pbase_tree *next;
+ /* This is a phony "cache" entry; we are not
+ * going to evict it nor find it through _get()
+ * mechanism -- this is for the toplevel node that
+ * would almost always change with any commit.
+ */
+ struct pbase_tree_cache pcache;
+} *pbase_tree;
+
+static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
+{
+ struct pbase_tree_cache *ent, *nent;
+ void *data;
+ unsigned long size;
+ char type[20];
+ int neigh;
+ int my_ix = pbase_tree_cache_ix(sha1);
+ int available_ix = -1;
+
+ /* pbase-tree-cache acts as a limited hashtable.
+ * your object will be found at your index or within a few
+ * slots after that slot if it is cached.
+ */
+ for (neigh = 0; neigh < 8; neigh++) {
+ ent = pbase_tree_cache[my_ix];
+ if (ent && !memcmp(ent->sha1, sha1, 20)) {
+ ent->ref++;
+ return ent;
+ }
+ else if (((available_ix < 0) && (!ent || !ent->ref)) ||
+ ((0 <= available_ix) &&
+ (!ent && pbase_tree_cache[available_ix])))
+ available_ix = my_ix;
+ if (!ent)
+ break;
+ my_ix = pbase_tree_cache_ix_incr(my_ix);
+ }
+
+ /* Did not find one. Either we got a bogus request or
+ * we need to read and perhaps cache.
+ */
+ data = read_sha1_file(sha1, type, &size);
+ if (!data)
+ return NULL;
+ if (strcmp(type, tree_type)) {
+ free(data);
+ return NULL;
+ }
+
+ /* We need to either cache or return a throwaway copy */
+
+ if (available_ix < 0)
+ ent = NULL;
+ else {
+ ent = pbase_tree_cache[available_ix];
+ my_ix = available_ix;
+ }
+
+ if (!ent) {
+ nent = xmalloc(sizeof(*nent));
+ nent->temporary = (available_ix < 0);
+ }
+ else {
+ /* evict and reuse */
+ free(ent->tree_data);
+ nent = ent;
+ }
+ memcpy(nent->sha1, sha1, 20);
+ nent->tree_data = data;
+ nent->tree_size = size;
+ nent->ref = 1;
+ if (!nent->temporary)
+ pbase_tree_cache[my_ix] = nent;
+ return nent;
+}
+
+static void pbase_tree_put(struct pbase_tree_cache *cache)
+{
+ if (!cache->temporary) {
+ cache->ref--;
+ return;
+ }
+ free(cache->tree_data);
+ free(cache);
+}
+
+static int name_cmp_len(const char *name)
+{
+ int i;
+ for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
+ ;
+ return i;
+}
+
+static void add_pbase_object(struct tree_desc *tree,
+ const char *name,
+ int cmplen,
+ const char *fullname)
+{
+ struct name_entry entry;
+
+ while (tree_entry(tree,&entry)) {
+ unsigned long size;
+ char type[20];
+
+ if (entry.pathlen != cmplen ||
+ memcmp(entry.path, name, cmplen) ||
+ !has_sha1_file(entry.sha1) ||
+ sha1_object_info(entry.sha1, type, &size))
+ continue;
+ if (name[cmplen] != '/') {
+ unsigned hash = name_hash(fullname);
+ add_object_entry(entry.sha1, hash, 1);
+ return;
+ }
+ if (!strcmp(type, tree_type)) {
+ struct tree_desc sub;
+ struct pbase_tree_cache *tree;
+ const char *down = name+cmplen+1;
+ int downlen = name_cmp_len(down);
+
+ tree = pbase_tree_get(entry.sha1);
+ if (!tree)
+ return;
+ sub.buf = tree->tree_data;
+ sub.size = tree->tree_size;
+
+ add_pbase_object(&sub, down, downlen, fullname);
+ pbase_tree_put(tree);
+ }
+ }
+}
+
+static unsigned *done_pbase_paths;
+static int done_pbase_paths_num;
+static int done_pbase_paths_alloc;
+static int done_pbase_path_pos(unsigned hash)
+{
+ int lo = 0;
+ int hi = done_pbase_paths_num;
+ while (lo < hi) {
+ int mi = (hi + lo) / 2;
+ if (done_pbase_paths[mi] == hash)
+ return mi;
+ if (done_pbase_paths[mi] < hash)
+ hi = mi;
+ else
+ lo = mi + 1;
+ }
+ return -lo-1;
+}
+
+static int check_pbase_path(unsigned hash)
+{
+ int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
+ if (0 <= pos)
+ return 1;
+ pos = -pos - 1;
+ if (done_pbase_paths_alloc <= done_pbase_paths_num) {
+ done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
+ done_pbase_paths = xrealloc(done_pbase_paths,
+ done_pbase_paths_alloc *
+ sizeof(unsigned));
+ }
+ done_pbase_paths_num++;
+ if (pos < done_pbase_paths_num)
+ memmove(done_pbase_paths + pos + 1,
+ done_pbase_paths + pos,
+ (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
+ done_pbase_paths[pos] = hash;
+ return 0;
+}
+
+static void add_preferred_base_object(char *name, unsigned hash)
+{
+ struct pbase_tree *it;
+ int cmplen = name_cmp_len(name);
+
+ if (check_pbase_path(hash))
+ return;
+
+ for (it = pbase_tree; it; it = it->next) {
+ if (cmplen == 0) {
+ hash = name_hash("");
+ add_object_entry(it->pcache.sha1, hash, 1);
+ }
+ else {
+ struct tree_desc tree;
+ tree.buf = it->pcache.tree_data;
+ tree.size = it->pcache.tree_size;
+ add_pbase_object(&tree, name, cmplen, name);
+ }
+ }
+}
+
+static void add_preferred_base(unsigned char *sha1)
+{
+ struct pbase_tree *it;
+ void *data;
+ unsigned long size;
+ unsigned char tree_sha1[20];
+
+ data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
+ if (!data)
+ return;
+
+ for (it = pbase_tree; it; it = it->next) {
+ if (!memcmp(it->pcache.sha1, tree_sha1, 20)) {
+ free(data);
+ return;
+ }
+ }
+
+ it = xcalloc(1, sizeof(*it));
+ it->next = pbase_tree;
+ pbase_tree = it;
+
+ memcpy(it->pcache.sha1, tree_sha1, 20);
+ it->pcache.tree_data = data;
+ it->pcache.tree_size = size;
+}
+
+static void check_object(struct object_entry *entry)
+{
+ char type[20];
+
+ if (entry->in_pack && !entry->preferred_base) {
+ unsigned char base[20];
+ unsigned long size;
+ struct object_entry *base_entry;
+
+ /* We want in_pack_type even if we do not reuse delta.
+ * There is no point not reusing non-delta representations.
+ */
+ check_reuse_pack_delta(entry->in_pack,
+ entry->in_pack_offset,
+ base, &size,
+ &entry->in_pack_type);
+
+ /* Check if it is delta, and the base is also an object
+ * we are going to pack. If so we will reuse the existing
+ * delta.
+ */
+ if (!no_reuse_delta &&
+ entry->in_pack_type == OBJ_DELTA &&
+ (base_entry = locate_object_entry(base)) &&
+ (!base_entry->preferred_base)) {
+
+ /* Depth value does not matter - find_deltas()
+ * will never consider reused delta as the
+ * base object to deltify other objects
+ * against, in order to avoid circular deltas.
+ */
+
+ /* uncompressed size of the delta data */
+ entry->size = entry->delta_size = size;
+ entry->delta = base_entry;
+ entry->type = OBJ_DELTA;
+
+ entry->delta_sibling = base_entry->delta_child;
+ base_entry->delta_child = entry;
+
+ return;
+ }
+ /* Otherwise we would do the usual */
+ }
+
+ if (sha1_object_info(entry->sha1, type, &entry->size))
+ die("unable to get type of object %s",
+ sha1_to_hex(entry->sha1));
+
+ if (!strcmp(type, commit_type)) {
+ entry->type = OBJ_COMMIT;
+ } else if (!strcmp(type, tree_type)) {
+ entry->type = OBJ_TREE;
+ } else if (!strcmp(type, blob_type)) {
+ entry->type = OBJ_BLOB;
+ } else if (!strcmp(type, tag_type)) {
+ entry->type = OBJ_TAG;
+ } else
+ die("unable to pack object %s of type %s",
+ sha1_to_hex(entry->sha1), type);
+}
+
+static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
+{
+ struct object_entry *child = me->delta_child;
+ unsigned int m = n;
+ while (child) {
+ unsigned int c = check_delta_limit(child, n + 1);
+ if (m < c)
+ m = c;
+ child = child->delta_sibling;
+ }
+ return m;
+}
+
+static void get_object_details(void)
+{
+ int i;
+ struct object_entry *entry;
+
+ prepare_pack_ix();
+ for (i = 0, entry = objects; i < nr_objects; i++, entry++)
+ check_object(entry);
+
+ if (nr_objects == nr_result) {
+ /*
+ * Depth of objects that depend on the entry -- this
+ * is subtracted from depth-max to break too deep
+ * delta chain because of delta data reusing.
+ * However, we loosen this restriction when we know we
+ * are creating a thin pack -- it will have to be
+ * expanded on the other end anyway, so do not
+ * artificially cut the delta chain and let it go as
+ * deep as it wants.
+ */
+ for (i = 0, entry = objects; i < nr_objects; i++, entry++)
+ if (!entry->delta && entry->delta_child)
+ entry->delta_limit =
+ check_delta_limit(entry, 1);
+ }
+}
+
+typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
+
+static entry_sort_t current_sort;
+
+static int sort_comparator(const void *_a, const void *_b)
+{
+ struct object_entry *a = *(struct object_entry **)_a;
+ struct object_entry *b = *(struct object_entry **)_b;
+ return current_sort(a,b);
+}
+
+static struct object_entry **create_sorted_list(entry_sort_t sort)
+{
+ struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
+ int i;
+
+ for (i = 0; i < nr_objects; i++)
+ list[i] = objects + i;
+ current_sort = sort;
+ qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
+ return list;
+}
+
+static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
+{
+ return memcmp(a->sha1, b->sha1, 20);
+}
+
+static struct object_entry **create_final_object_list(void)
+{
+ struct object_entry **list;
+ int i, j;
+
+ for (i = nr_result = 0; i < nr_objects; i++)
+ if (!objects[i].preferred_base)
+ nr_result++;
+ list = xmalloc(nr_result * sizeof(struct object_entry *));
+ for (i = j = 0; i < nr_objects; i++) {
+ if (!objects[i].preferred_base)
+ list[j++] = objects + i;
+ }
+ current_sort = sha1_sort;
+ qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
+ return list;
+}
+
+static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
+{
+ if (a->type < b->type)
+ return -1;
+ if (a->type > b->type)
+ return 1;
+ if (a->hash < b->hash)
+ return -1;
+ if (a->hash > b->hash)
+ return 1;
+ if (a->preferred_base < b->preferred_base)
+ return -1;
+ if (a->preferred_base > b->preferred_base)
+ return 1;
+ if (a->size < b->size)
+ return -1;
+ if (a->size > b->size)
+ return 1;
+ return a < b ? -1 : (a > b);
+}
+
+struct unpacked {
+ struct object_entry *entry;
+ void *data;
+ struct delta_index *index;
+};
+
+/*
+ * We search for deltas _backwards_ in a list sorted by type and
+ * by size, so that we see progressively smaller and smaller files.
+ * That's because we prefer deltas to be from the bigger file
+ * to the smaller - deletes are potentially cheaper, but perhaps
+ * more importantly, the bigger file is likely the more recent
+ * one.
+ */
+static int try_delta(struct unpacked *trg, struct unpacked *src,
+ unsigned max_depth)
+{
+ struct object_entry *trg_entry = trg->entry;
+ struct object_entry *src_entry = src->entry;
+ unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
+ char type[10];
+ void *delta_buf;
+
+ /* Don't bother doing diffs between different types */
+ if (trg_entry->type != src_entry->type)
+ return -1;
+
+ /* We do not compute delta to *create* objects we are not
+ * going to pack.
+ */
+ if (trg_entry->preferred_base)
+ return -1;
+
+ /*
+ * We do not bother to try a delta that we discarded
+ * on an earlier try, but only when reusing delta data.
+ */
+ if (!no_reuse_delta && trg_entry->in_pack &&
+ trg_entry->in_pack == src_entry->in_pack)
+ return 0;
+
+ /*
+ * If the current object is at pack edge, take the depth the
+ * objects that depend on the current object into account --
+ * otherwise they would become too deep.
+ */
+ if (trg_entry->delta_child) {
+ if (max_depth <= trg_entry->delta_limit)
+ return 0;
+ max_depth -= trg_entry->delta_limit;
+ }
+ if (src_entry->depth >= max_depth)
+ return 0;
+
+ /* Now some size filtering heuristics. */
+ trg_size = trg_entry->size;
+ max_size = trg_size/2 - 20;
+ max_size = max_size * (max_depth - src_entry->depth) / max_depth;
+ if (max_size == 0)
+ return 0;
+ if (trg_entry->delta && trg_entry->delta_size <= max_size)
+ max_size = trg_entry->delta_size-1;
+ src_size = src_entry->size;
+ sizediff = src_size < trg_size ? trg_size - src_size : 0;
+ if (sizediff >= max_size)
+ return 0;
+
+ /* Load data if not already done */
+ if (!trg->data) {
+ trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
+ if (sz != trg_size)
+ die("object %s inconsistent object length (%lu vs %lu)",
+ sha1_to_hex(trg_entry->sha1), sz, trg_size);
+ }
+ if (!src->data) {
+ src->data = read_sha1_file(src_entry->sha1, type, &sz);
+ if (sz != src_size)
+ die("object %s inconsistent object length (%lu vs %lu)",
+ sha1_to_hex(src_entry->sha1), sz, src_size);
+ }
+ if (!src->index) {
+ src->index = create_delta_index(src->data, src_size);
+ if (!src->index)
+ die("out of memory");
+ }
+
+ delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
+ if (!delta_buf)
+ return 0;
+
+ trg_entry->delta = src_entry;
+ trg_entry->delta_size = delta_size;
+ trg_entry->depth = src_entry->depth + 1;
+ free(delta_buf);
+ return 1;
+}
+
+static void progress_interval(int signum)
+{
+ progress_update = 1;
+}
+
+static void find_deltas(struct object_entry **list, int window, int depth)
+{
+ int i, idx;
+ unsigned int array_size = window * sizeof(struct unpacked);
+ struct unpacked *array = xmalloc(array_size);
+ unsigned processed = 0;
+ unsigned last_percent = 999;
+
+ memset(array, 0, array_size);
+ i = nr_objects;
+ idx = 0;
+ if (progress)
+ fprintf(stderr, "Deltifying %d objects.\n", nr_result);
+
+ while (--i >= 0) {
+ struct object_entry *entry = list[i];
+ struct unpacked *n = array + idx;
+ int j;
+
+ if (!entry->preferred_base)
+ processed++;
+
+ if (progress) {
+ unsigned percent = processed * 100 / nr_result;
+ if (percent != last_percent || progress_update) {
+ fprintf(stderr, "%4u%% (%u/%u) done\r",
+ percent, processed, nr_result);
+ progress_update = 0;
+ last_percent = percent;
+ }
+ }
+
+ if (entry->delta)
+ /* This happens if we decided to reuse existing
+ * delta from a pack. "!no_reuse_delta &&" is implied.
+ */
+ continue;
+
+ if (entry->size < 50)
+ continue;
+ free_delta_index(n->index);
+ n->index = NULL;
+ free(n->data);
+ n->data = NULL;
+ n->entry = entry;
+
+ j = window;
+ while (--j > 0) {
+ unsigned int other_idx = idx + j;
+ struct unpacked *m;
+ if (other_idx >= window)
+ other_idx -= window;
+ m = array + other_idx;
+ if (!m->entry)
+ break;
+ if (try_delta(n, m, depth) < 0)
+ break;
+ }
+ /* if we made n a delta, and if n is already at max
+ * depth, leaving it in the window is pointless. we
+ * should evict it first.
+ */
+ if (entry->delta && depth <= entry->depth)
+ continue;
+
+ idx++;
+ if (idx >= window)
+ idx = 0;
+ }
+
+ if (progress)
+ fputc('\n', stderr);
+
+ for (i = 0; i < window; ++i) {
+ free_delta_index(array[i].index);
+ free(array[i].data);
+ }
+ free(array);
+}
+
+static void prepare_pack(int window, int depth)
+{
+ get_object_details();
+ sorted_by_type = create_sorted_list(type_size_sort);
+ if (window && depth)
+ find_deltas(sorted_by_type, window+1, depth);
+}
+
+static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
+{
+ static const char cache[] = "pack-cache/pack-%s.%s";
+ char *cached_pack, *cached_idx;
+ int ifd, ofd, ifd_ix = -1;
+
+ cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
+ ifd = open(cached_pack, O_RDONLY);
+ if (ifd < 0)
+ return 0;
+
+ if (!pack_to_stdout) {
+ cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
+ ifd_ix = open(cached_idx, O_RDONLY);
+ if (ifd_ix < 0) {
+ close(ifd);
+ return 0;
+ }
+ }
+
+ if (progress)
+ fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
+ sha1_to_hex(sha1));
+
+ if (pack_to_stdout) {
+ if (copy_fd(ifd, 1))
+ exit(1);
+ close(ifd);
+ }
+ else {
+ char name[PATH_MAX];
+ snprintf(name, sizeof(name),
+ "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
+ ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
+ if (ofd < 0)
+ die("unable to open %s (%s)", name, strerror(errno));
+ if (copy_fd(ifd, ofd))
+ exit(1);
+ close(ifd);
+
+ snprintf(name, sizeof(name),
+ "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
+ ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
+ if (ofd < 0)
+ die("unable to open %s (%s)", name, strerror(errno));
+ if (copy_fd(ifd_ix, ofd))
+ exit(1);
+ close(ifd_ix);
+ puts(sha1_to_hex(sha1));
+ }
+
+ return 1;
+}
+
+static void setup_progress_signal(void)
+{
+ struct sigaction sa;
+ struct itimerval v;
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = progress_interval;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = SA_RESTART;
+ sigaction(SIGALRM, &sa, NULL);
+
+ v.it_interval.tv_sec = 1;
+ v.it_interval.tv_usec = 0;
+ v.it_value = v.it_interval;
+ setitimer(ITIMER_REAL, &v, NULL);
+}
+
+static int git_pack_config(const char *k, const char *v)
+{
+ if(!strcmp(k, "pack.window")) {
+ window = git_config_int(k, v);
+ return 0;
+ }
+ return git_default_config(k, v);
+}
+
+int cmd_pack_objects(int argc, const char **argv, const char *prefix)
+{
+ SHA_CTX ctx;
+ char line[40 + 1 + PATH_MAX + 2];
+ int depth = 10, pack_to_stdout = 0;
+ struct object_entry **list;
+ int num_preferred_base = 0;
+ int i;
+
+ git_config(git_pack_config);
+
+ progress = isatty(2);
+ for (i = 1; i < argc; i++) {
+ const char *arg = argv[i];
+
+ if (*arg == '-') {
+ if (!strcmp("--non-empty", arg)) {
+ non_empty = 1;
+ continue;
+ }
+ if (!strcmp("--local", arg)) {
+ local = 1;
+ continue;
+ }
+ if (!strcmp("--progress", arg)) {
+ progress = 1;
+ continue;
+ }
+ if (!strcmp("--incremental", arg)) {
+ incremental = 1;
+ continue;
+ }
+ if (!strncmp("--window=", arg, 9)) {
+ char *end;
+ window = strtoul(arg+9, &end, 0);
+ if (!arg[9] || *end)
+ usage(pack_usage);
+ continue;
+ }
+ if (!strncmp("--depth=", arg, 8)) {
+ char *end;
+ depth = strtoul(arg+8, &end, 0);
+ if (!arg[8] || *end)
+ usage(pack_usage);
+ continue;
+ }
+ if (!strcmp("--progress", arg)) {
+ progress = 1;
+ continue;
+ }
+ if (!strcmp("-q", arg)) {
+ progress = 0;
+ continue;
+ }
+ if (!strcmp("--no-reuse-delta", arg)) {
+ no_reuse_delta = 1;
+ continue;
+ }
+ if (!strcmp("--stdout", arg)) {
+ pack_to_stdout = 1;
+ continue;
+ }
+ usage(pack_usage);
+ }
+ if (base_name)
+ usage(pack_usage);
+ base_name = arg;
+ }
+
+ if (pack_to_stdout != !base_name)
+ usage(pack_usage);
+
+ prepare_packed_git();
+
+ if (progress) {
+ fprintf(stderr, "Generating pack...\n");
+ setup_progress_signal();
+ }
+
+ for (;;) {
+ unsigned char sha1[20];
+ unsigned hash;
+
+ if (!fgets(line, sizeof(line), stdin)) {
+ if (feof(stdin))
+ break;
+ if (!ferror(stdin))
+ die("fgets returned NULL, not EOF, not error!");
+ if (errno != EINTR)
+ die("fgets: %s", strerror(errno));
+ clearerr(stdin);
+ continue;
+ }
+
+ if (line[0] == '-') {
+ if (get_sha1_hex(line+1, sha1))
+ die("expected edge sha1, got garbage:\n %s",
+ line+1);
+ if (num_preferred_base++ < window)
+ add_preferred_base(sha1);
+ continue;
+ }
+ if (get_sha1_hex(line, sha1))
+ die("expected sha1, got garbage:\n %s", line);
+ hash = name_hash(line+41);
+ add_preferred_base_object(line+41, hash);
+ add_object_entry(sha1, hash, 0);
+ }
+ if (progress)
+ fprintf(stderr, "Done counting %d objects.\n", nr_objects);
+ sorted_by_sha = create_final_object_list();
+ if (non_empty && !nr_result)
+ return 0;
+
+ SHA1_Init(&ctx);
+ list = sorted_by_sha;
+ for (i = 0; i < nr_result; i++) {
+ struct object_entry *entry = *list++;
+ SHA1_Update(&ctx, entry->sha1, 20);
+ }
+ SHA1_Final(object_list_sha1, &ctx);
+ if (progress && (nr_objects != nr_result))
+ fprintf(stderr, "Result has %d objects.\n", nr_result);
+
+ if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
+ ;
+ else {
+ if (nr_result)
+ prepare_pack(window, depth);
+ if (progress && pack_to_stdout) {
+ /* the other end usually displays progress itself */
+ struct itimerval v = {{0,},};
+ setitimer(ITIMER_REAL, &v, NULL);
+ signal(SIGALRM, SIG_IGN );
+ progress_update = 0;
+ }
+ write_pack_file();
+ if (!pack_to_stdout) {
+ write_index_file();
+ puts(sha1_to_hex(object_list_sha1));
+ }
+ }
+ if (progress)
+ fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
+ nr_result, written, written_delta, reused, reused_delta);
+ return 0;
+}
--- /dev/null
+#include "builtin.h"
+#include "cache.h"
+
+static const char git_symbolic_ref_usage[] =
+"git-symbolic-ref name [ref]";
+
+static void check_symref(const char *HEAD)
+{
+ unsigned char sha1[20];
+ const char *git_HEAD = strdup(git_path("%s", HEAD));
+ const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 0);
+ if (git_refs_heads_master) {
+ /* we want to strip the .git/ part */
+ int pfxlen = strlen(git_HEAD) - strlen(HEAD);
+ puts(git_refs_heads_master + pfxlen);
+ }
+ else
+ die("No such ref: %s", HEAD);
+}
+
+int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
+{
+ git_config(git_default_config);
+ switch (argc) {
+ case 2:
+ check_symref(argv[1]);
+ break;
+ case 3:
+ create_symref(strdup(git_path("%s", argv[1])), argv[2]);
+ break;
+ default:
+ usage(git_symbolic_ref_usage);
+ }
+ return 0;
+}
--- /dev/null
+#include "builtin.h"
+#include "cache.h"
+#include "object.h"
+#include "delta.h"
+#include "pack.h"
+#include "blob.h"
+#include "commit.h"
+#include "tag.h"
+#include "tree.h"
+
+#include <sys/time.h>
+
+static int dry_run, quiet;
+static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
+
+/* We always read in 4kB chunks. */
+static unsigned char buffer[4096];
+static unsigned long offset, len, eof;
+static SHA_CTX ctx;
+
+/*
+ * Make sure at least "min" bytes are available in the buffer, and
+ * return the pointer to the buffer.
+ */
+static void * fill(int min)
+{
+ if (min <= len)
+ return buffer + offset;
+ if (eof)
+ die("unable to fill input");
+ if (min > sizeof(buffer))
+ die("cannot fill %d bytes", min);
+ if (offset) {
+ SHA1_Update(&ctx, buffer, offset);
+ memcpy(buffer, buffer + offset, len);
+ offset = 0;
+ }
+ do {
+ int ret = xread(0, buffer + len, sizeof(buffer) - len);
+ if (ret <= 0) {
+ if (!ret)
+ die("early EOF");
+ die("read error on input: %s", strerror(errno));
+ }
+ len += ret;
+ } while (len < min);
+ return buffer;
+}
+
+static void use(int bytes)
+{
+ if (bytes > len)
+ die("used more bytes than were available");
+ len -= bytes;
+ offset += bytes;
+}
+
+static void *get_data(unsigned long size)
+{
+ z_stream stream;
+ void *buf = xmalloc(size);
+
+ memset(&stream, 0, sizeof(stream));
+
+ stream.next_out = buf;
+ stream.avail_out = size;
+ stream.next_in = fill(1);
+ stream.avail_in = len;
+ inflateInit(&stream);
+
+ for (;;) {
+ int ret = inflate(&stream, 0);
+ use(len - stream.avail_in);
+ if (stream.total_out == size && ret == Z_STREAM_END)
+ break;
+ if (ret != Z_OK)
+ die("inflate returned %d\n", ret);
+ stream.next_in = fill(1);
+ stream.avail_in = len;
+ }
+ inflateEnd(&stream);
+ return buf;
+}
+
+struct delta_info {
+ unsigned char base_sha1[20];
+ unsigned long size;
+ void *delta;
+ struct delta_info *next;
+};
+
+static struct delta_info *delta_list;
+
+static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
+{
+ struct delta_info *info = xmalloc(sizeof(*info));
+
+ memcpy(info->base_sha1, base_sha1, 20);
+ info->size = size;
+ info->delta = delta;
+ info->next = delta_list;
+ delta_list = info;
+}
+
+static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
+
+static void write_object(void *buf, unsigned long size, const char *type)
+{
+ unsigned char sha1[20];
+ if (write_sha1_file(buf, size, type, sha1) < 0)
+ die("failed to write object");
+ added_object(sha1, type, buf, size);
+}
+
+static int resolve_delta(const char *type,
+ void *base, unsigned long base_size,
+ void *delta, unsigned long delta_size)
+{
+ void *result;
+ unsigned long result_size;
+
+ result = patch_delta(base, base_size,
+ delta, delta_size,
+ &result_size);
+ if (!result)
+ die("failed to apply delta");
+ free(delta);
+ write_object(result, result_size, type);
+ free(result);
+ return 0;
+}
+
+static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
+{
+ struct delta_info **p = &delta_list;
+ struct delta_info *info;
+
+ while ((info = *p) != NULL) {
+ if (!memcmp(info->base_sha1, sha1, 20)) {
+ *p = info->next;
+ p = &delta_list;
+ resolve_delta(type, data, size, info->delta, info->size);
+ free(info);
+ continue;
+ }
+ p = &info->next;
+ }
+}
+
+static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
+{
+ void *buf = get_data(size);
+ const char *type;
+
+ switch (kind) {
+ case OBJ_COMMIT: type = commit_type; break;
+ case OBJ_TREE: type = tree_type; break;
+ case OBJ_BLOB: type = blob_type; break;
+ case OBJ_TAG: type = tag_type; break;
+ default: die("bad type %d", kind);
+ }
+ if (!dry_run)
+ write_object(buf, size, type);
+ free(buf);
+ return 0;
+}
+
+static int unpack_delta_entry(unsigned long delta_size)
+{
+ void *delta_data, *base;
+ unsigned long base_size;
+ char type[20];
+ unsigned char base_sha1[20];
+ int result;
+
+ memcpy(base_sha1, fill(20), 20);
+ use(20);
+
+ delta_data = get_data(delta_size);
+ if (dry_run) {
+ free(delta_data);
+ return 0;
+ }
+
+ if (!has_sha1_file(base_sha1)) {
+ add_delta_to_list(base_sha1, delta_data, delta_size);
+ return 0;
+ }
+ base = read_sha1_file(base_sha1, type, &base_size);
+ if (!base)
+ die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
+ result = resolve_delta(type, base, base_size, delta_data, delta_size);
+ free(base);
+ return result;
+}
+
+static void unpack_one(unsigned nr, unsigned total)
+{
+ unsigned shift;
+ unsigned char *pack, c;
+ unsigned long size;
+ enum object_type type;
+
+ pack = fill(1);
+ c = *pack;
+ use(1);
+ type = (c >> 4) & 7;
+ size = (c & 15);
+ shift = 4;
+ while (c & 0x80) {
+ pack = fill(1);
+ c = *pack++;
+ use(1);
+ size += (c & 0x7f) << shift;
+ shift += 7;
+ }
+ if (!quiet) {
+ static unsigned long last_sec;
+ static unsigned last_percent;
+ struct timeval now;
+ unsigned percentage = (nr * 100) / total;
+
+ gettimeofday(&now, NULL);
+ if (percentage != last_percent || now.tv_sec != last_sec) {
+ last_sec = now.tv_sec;
+ last_percent = percentage;
+ fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
+ }
+ }
+ switch (type) {
+ case OBJ_COMMIT:
+ case OBJ_TREE:
+ case OBJ_BLOB:
+ case OBJ_TAG:
+ unpack_non_delta_entry(type, size);
+ return;
+ case OBJ_DELTA:
+ unpack_delta_entry(size);
+ return;
+ default:
+ die("bad object type %d", type);
+ }
+}
+
+static void unpack_all(void)
+{
+ int i;
+ struct pack_header *hdr = fill(sizeof(struct pack_header));
+ unsigned nr_objects = ntohl(hdr->hdr_entries);
+
+ if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
+ die("bad pack file");
+ if (!pack_version_ok(hdr->hdr_version))
+ die("unknown pack file version %d", ntohl(hdr->hdr_version));
+ fprintf(stderr, "Unpacking %d objects\n", nr_objects);
+
+ use(sizeof(struct pack_header));
+ for (i = 0; i < nr_objects; i++)
+ unpack_one(i+1, nr_objects);
+ if (delta_list)
+ die("unresolved deltas left after unpacking");
+}
+
+int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
+{
+ int i;
+ unsigned char sha1[20];
+
+ quiet = !isatty(2);
+
+ for (i = 1 ; i < argc; i++) {
+ const char *arg = argv[i];
+
+ if (*arg == '-') {
+ if (!strcmp(arg, "-n")) {
+ dry_run = 1;
+ continue;
+ }
+ if (!strcmp(arg, "-q")) {
+ quiet = 1;
+ continue;
+ }
+ usage(unpack_usage);
+ }
+
+ /* We don't take any non-flag arguments now.. Maybe some day */
+ usage(unpack_usage);
+ }
+ SHA1_Init(&ctx);
+ unpack_all();
+ SHA1_Update(&ctx, buffer, offset);
+ SHA1_Final(sha1, &ctx);
+ if (memcmp(fill(20), sha1, 20))
+ die("final sha1 did not match");
+ use(20);
+
+ /* Write the last part of the buffer to stdout */
+ while (len) {
+ int ret = xwrite(1, buffer + offset, len);
+ if (ret <= 0)
+ break;
+ len -= ret;
+ offset += ret;
+ }
+
+ /* All done */
+ if (!quiet)
+ fprintf(stderr, "\n");
+ return 0;
+}
extern const char git_usage_string[];
extern void help_unknown_cmd(const char *cmd);
+extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch);
+extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip);
+extern void stripspace(FILE *in, FILE *out);
+extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
-extern int cmd_help(int argc, const char **argv, const char *prefix);
-extern int cmd_version(int argc, const char **argv, const char *prefix);
-
-extern int cmd_whatchanged(int argc, const char **argv, const char *prefix);
-extern int cmd_show(int argc, const char **argv, const char *prefix);
-extern int cmd_log(int argc, const char **argv, const char *prefix);
-extern int cmd_diff(int argc, const char **argv, const char *prefix);
-extern int cmd_format_patch(int argc, const char **argv, const char *prefix);
-extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
-
-extern int cmd_prune(int argc, const char **argv, const char *prefix);
-extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
-
-extern int cmd_push(int argc, const char **argv, const char *prefix);
-extern int cmd_grep(int argc, const char **argv, const char *prefix);
-extern int cmd_rm(int argc, const char **argv, const char *prefix);
extern int cmd_add(int argc, const char **argv, const char *prefix);
-extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
+extern int cmd_apply(int argc, const char **argv, const char *prefix);
+extern int cmd_cat_file(int argc, const char **argv, const char *prefix);
+extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
-extern int cmd_init_db(int argc, const char **argv, const char *prefix);
-extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_upload_tar(int argc, const char **argv, const char *prefix);
-extern int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
-extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
-extern int cmd_ls_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_apply(int argc, const char **argv, const char *prefix);
-extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
+extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
extern int cmd_diff_files(int argc, const char **argv, const char *prefix);
extern int cmd_diff_index(int argc, const char **argv, const char *prefix);
+extern int cmd_diff(int argc, const char **argv, const char *prefix);
extern int cmd_diff_stages(int argc, const char **argv, const char *prefix);
extern int cmd_diff_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_cat_file(int argc, const char **argv, const char *prefix);
-extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
-extern int cmd_update_index(int argc, const char **argv, const char *prefix);
-extern int cmd_update_ref(int argc, const char **argv, const char *prefix);
extern int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
+extern int cmd_format_patch(int argc, const char **argv, const char *prefix);
+extern int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
+extern int cmd_grep(int argc, const char **argv, const char *prefix);
+extern int cmd_help(int argc, const char **argv, const char *prefix);
+extern int cmd_init_db(int argc, const char **argv, const char *prefix);
+extern int cmd_log(int argc, const char **argv, const char *prefix);
+extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
+extern int cmd_ls_tree(int argc, const char **argv, const char *prefix);
+extern int cmd_mailinfo(int argc, const char **argv, const char *prefix);
+extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
extern int cmd_mv(int argc, const char **argv, const char *prefix);
+extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
+extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);
+extern int cmd_prune(int argc, const char **argv, const char *prefix);
+extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
+extern int cmd_push(int argc, const char **argv, const char *prefix);
+extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
extern int cmd_repo_config(int argc, const char **argv, const char *prefix);
-
+extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
+extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
+extern int cmd_rm(int argc, const char **argv, const char *prefix);
+extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
+extern int cmd_show(int argc, const char **argv, const char *prefix);
+extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
+extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
+extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
+extern int cmd_unpack_objects(int argc, const char **argv, const char *prefix);
+extern int cmd_update_index(int argc, const char **argv, const char *prefix);
+extern int cmd_update_ref(int argc, const char **argv, const char *prefix);
+extern int cmd_upload_tar(int argc, const char **argv, const char *prefix);
+extern int cmd_version(int argc, const char **argv, const char *prefix);
+extern int cmd_whatchanged(int argc, const char **argv, const char *prefix);
extern int cmd_write_tree(int argc, const char **argv, const char *prefix);
-extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
-extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
-extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip);
-
-extern int cmd_mailinfo(int argc, const char **argv, const char *prefix);
-extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch);
-
-extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
-extern void stripspace(FILE *in, FILE *out);
#endif
+++ /dev/null
-/*
- * Check-out files from the "current cache directory"
- *
- * Copyright (C) 2005 Linus Torvalds
- *
- * Careful: order of argument flags does matter. For example,
- *
- * git-checkout-index -a -f file.c
- *
- * 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).
- *
- * Also, just doing "git-checkout-index" does nothing. You probably
- * meant "git-checkout-index -a". And if you want to force it, you
- * want "git-checkout-index -f -a".
- *
- * 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
- *
- * find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
- *
- * or:
- *
- * find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
- *
- * 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!).
- */
-#include "cache.h"
-#include "strbuf.h"
-#include "quote.h"
-#include "cache-tree.h"
-
-#define CHECKOUT_ALL 4
-static const char *prefix;
-static int prefix_length;
-static int line_termination = '\n';
-static int checkout_stage; /* default to checkout stage0 */
-static int to_tempfile;
-static char topath[4][MAXPATHLEN+1];
-
-static struct checkout state;
-
-static void write_tempfile_record (const char *name)
-{
- int i;
-
- if (CHECKOUT_ALL == checkout_stage) {
- for (i = 1; i < 4; i++) {
- if (i > 1)
- putchar(' ');
- if (topath[i][0])
- fputs(topath[i], stdout);
- else
- putchar('.');
- }
- } else
- fputs(topath[checkout_stage], stdout);
-
- putchar('\t');
- write_name_quoted("", 0, name + prefix_length,
- line_termination, stdout);
- putchar(line_termination);
-
- for (i = 0; i < 4; i++) {
- topath[i][0] = 0;
- }
-}
-
-static int checkout_file(const char *name)
-{
- int namelen = strlen(name);
- int pos = cache_name_pos(name, namelen);
- int has_same_name = 0;
- int did_checkout = 0;
- int errs = 0;
-
- if (pos < 0)
- pos = -pos - 1;
-
- while (pos < active_nr) {
- struct cache_entry *ce = active_cache[pos];
- if (ce_namelen(ce) != namelen ||
- memcmp(ce->name, name, namelen))
- break;
- has_same_name = 1;
- pos++;
- if (ce_stage(ce) != checkout_stage
- && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
- continue;
- did_checkout = 1;
- if (checkout_entry(ce, &state,
- to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
- errs++;
- }
-
- if (did_checkout) {
- if (to_tempfile)
- write_tempfile_record(name);
- return errs > 0 ? -1 : 0;
- }
-
- if (!state.quiet) {
- fprintf(stderr, "git-checkout-index: %s ", name);
- if (!has_same_name)
- fprintf(stderr, "is not in the cache");
- else if (checkout_stage)
- fprintf(stderr, "does not exist at stage %d",
- checkout_stage);
- else
- fprintf(stderr, "is unmerged");
- fputc('\n', stderr);
- }
- return -1;
-}
-
-static int checkout_all(void)
-{
- int i, errs = 0;
- struct cache_entry* last_ce = NULL;
-
- for (i = 0; i < active_nr ; i++) {
- struct cache_entry *ce = active_cache[i];
- if (ce_stage(ce) != checkout_stage
- && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
- continue;
- if (prefix && *prefix &&
- (ce_namelen(ce) <= prefix_length ||
- memcmp(prefix, ce->name, prefix_length)))
- continue;
- if (last_ce && to_tempfile) {
- if (ce_namelen(last_ce) != ce_namelen(ce)
- || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
- write_tempfile_record(last_ce->name);
- }
- if (checkout_entry(ce, &state,
- to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
- errs++;
- last_ce = ce;
- }
- if (last_ce && to_tempfile)
- write_tempfile_record(last_ce->name);
- if (errs)
- /* we have already done our error reporting.
- * exit with the same code as die().
- */
- exit(128);
- return 0;
-}
-
-static const char checkout_cache_usage[] =
-"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
-
-static struct lock_file lock_file;
-
-int main(int argc, char **argv)
-{
- int i;
- int newfd = -1;
- int all = 0;
- int read_from_stdin = 0;
-
- state.base_dir = "";
- prefix = setup_git_directory();
- git_config(git_default_config);
- prefix_length = prefix ? strlen(prefix) : 0;
-
- if (read_cache() < 0) {
- die("invalid cache");
- }
-
- for (i = 1; i < argc; i++) {
- const char *arg = argv[i];
-
- if (!strcmp(arg, "--")) {
- i++;
- break;
- }
- if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
- all = 1;
- continue;
- }
- if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
- state.force = 1;
- continue;
- }
- if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
- state.quiet = 1;
- continue;
- }
- if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
- state.not_new = 1;
- continue;
- }
- if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
- state.refresh_cache = 1;
- if (newfd < 0)
- newfd = hold_lock_file_for_update
- (&lock_file, get_index_file());
- if (newfd < 0)
- die("cannot open index.lock file.");
- continue;
- }
- if (!strcmp(arg, "-z")) {
- line_termination = 0;
- continue;
- }
- if (!strcmp(arg, "--stdin")) {
- if (i != argc - 1)
- die("--stdin must be at the end");
- read_from_stdin = 1;
- i++; /* do not consider arg as a file name */
- break;
- }
- if (!strcmp(arg, "--temp")) {
- to_tempfile = 1;
- continue;
- }
- if (!strncmp(arg, "--prefix=", 9)) {
- state.base_dir = arg+9;
- state.base_dir_len = strlen(state.base_dir);
- continue;
- }
- if (!strncmp(arg, "--stage=", 8)) {
- if (!strcmp(arg + 8, "all")) {
- to_tempfile = 1;
- checkout_stage = CHECKOUT_ALL;
- } else {
- int ch = arg[8];
- if ('1' <= ch && ch <= '3')
- checkout_stage = arg[8] - '0';
- else
- die("stage should be between 1 and 3 or all");
- }
- continue;
- }
- if (arg[0] == '-')
- usage(checkout_cache_usage);
- break;
- }
-
- if (state.base_dir_len || to_tempfile) {
- /* when --prefix is specified we do not
- * want to update cache.
- */
- if (state.refresh_cache) {
- close(newfd); newfd = -1;
- rollback_lock_file(&lock_file);
- }
- state.refresh_cache = 0;
- }
-
- /* Check out named files first */
- for ( ; i < argc; i++) {
- const char *arg = argv[i];
- const char *p;
-
- if (all)
- die("git-checkout-index: don't mix '--all' and explicit filenames");
- if (read_from_stdin)
- die("git-checkout-index: don't mix '--stdin' and explicit filenames");
- p = prefix_path(prefix, prefix_length, arg);
- checkout_file(p);
- if (p < arg || p > arg + strlen(arg))
- free((char*)p);
- }
-
- if (read_from_stdin) {
- struct strbuf buf;
- if (all)
- die("git-checkout-index: don't mix '--all' and '--stdin'");
- strbuf_init(&buf);
- while (1) {
- char *path_name;
- const char *p;
-
- read_line(&buf, stdin, line_termination);
- if (buf.eof)
- break;
- if (line_termination && buf.buf[0] == '"')
- path_name = unquote_c_style(buf.buf, NULL);
- else
- path_name = buf.buf;
- p = prefix_path(prefix, prefix_length, path_name);
- checkout_file(p);
- if (p < path_name || p > path_name + strlen(path_name))
- free((char *)p);
- if (path_name != buf.buf)
- free(path_name);
- }
- }
-
- if (all)
- checkout_all();
-
- if (0 <= newfd &&
- (write_cache(newfd, active_cache, active_nr) ||
- close(newfd) || commit_lock_file(&lock_file)))
- die("Unable to write new index file");
- return 0;
-}
const char git_version_string[] = GIT_VERSION;
-#define NEEDS_PREFIX 1
-#define USE_PAGER 2
+#define RUN_SETUP (1<<0)
+#define USE_PAGER (1<<1)
static void handle_internal_command(int argc, const char **argv, char **envp)
{
int (*fn)(int, const char **, const char *);
int option;
} commands[] = {
- { "version", cmd_version },
- { "help", cmd_help },
- { "log", cmd_log, NEEDS_PREFIX | USE_PAGER },
- { "whatchanged", cmd_whatchanged, NEEDS_PREFIX | USE_PAGER },
- { "show", cmd_show, NEEDS_PREFIX | USE_PAGER },
- { "push", cmd_push, NEEDS_PREFIX },
- { "format-patch", cmd_format_patch, NEEDS_PREFIX },
+ { "add", cmd_add, RUN_SETUP },
+ { "apply", cmd_apply },
+ { "cat-file", cmd_cat_file, RUN_SETUP },
+ { "checkout-index", cmd_checkout_index, RUN_SETUP },
+ { "check-ref-format", cmd_check_ref_format },
+ { "commit-tree", cmd_commit_tree, RUN_SETUP },
{ "count-objects", cmd_count_objects },
- { "diff", cmd_diff, NEEDS_PREFIX },
- { "grep", cmd_grep, NEEDS_PREFIX },
- { "rm", cmd_rm, NEEDS_PREFIX },
- { "add", cmd_add, NEEDS_PREFIX },
- { "rev-list", cmd_rev_list, NEEDS_PREFIX },
- { "init-db", cmd_init_db },
+ { "diff", cmd_diff, RUN_SETUP },
+ { "diff-files", cmd_diff_files, RUN_SETUP },
+ { "diff-index", cmd_diff_index, RUN_SETUP },
+ { "diff-stages", cmd_diff_stages, RUN_SETUP },
+ { "diff-tree", cmd_diff_tree, RUN_SETUP },
+ { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
+ { "format-patch", cmd_format_patch, RUN_SETUP },
{ "get-tar-commit-id", cmd_get_tar_commit_id },
- { "upload-tar", cmd_upload_tar },
- { "check-ref-format", cmd_check_ref_format },
- { "ls-files", cmd_ls_files, NEEDS_PREFIX },
- { "ls-tree", cmd_ls_tree, NEEDS_PREFIX },
- { "tar-tree", cmd_tar_tree, NEEDS_PREFIX },
- { "read-tree", cmd_read_tree, NEEDS_PREFIX },
- { "commit-tree", cmd_commit_tree, NEEDS_PREFIX },
- { "apply", cmd_apply },
- { "show-branch", cmd_show_branch, NEEDS_PREFIX },
- { "diff-files", cmd_diff_files, NEEDS_PREFIX },
- { "diff-index", cmd_diff_index, NEEDS_PREFIX },
- { "diff-stages", cmd_diff_stages, NEEDS_PREFIX },
- { "diff-tree", cmd_diff_tree, NEEDS_PREFIX },
- { "cat-file", cmd_cat_file, NEEDS_PREFIX },
- { "rev-parse", cmd_rev_parse, NEEDS_PREFIX },
- { "write-tree", cmd_write_tree, NEEDS_PREFIX },
- { "mailsplit", cmd_mailsplit },
+ { "grep", cmd_grep, RUN_SETUP },
+ { "help", cmd_help },
+ { "init-db", cmd_init_db },
+ { "log", cmd_log, RUN_SETUP | USE_PAGER },
+ { "ls-files", cmd_ls_files, RUN_SETUP },
+ { "ls-tree", cmd_ls_tree, RUN_SETUP },
{ "mailinfo", cmd_mailinfo },
- { "stripspace", cmd_stripspace },
- { "update-index", cmd_update_index, NEEDS_PREFIX },
- { "update-ref", cmd_update_ref, NEEDS_PREFIX },
- { "fmt-merge-msg", cmd_fmt_merge_msg, NEEDS_PREFIX },
- { "prune", cmd_prune, NEEDS_PREFIX },
- { "mv", cmd_mv, NEEDS_PREFIX },
- { "prune-packed", cmd_prune_packed, NEEDS_PREFIX },
+ { "mailsplit", cmd_mailsplit },
+ { "mv", cmd_mv, RUN_SETUP },
+ { "name-rev", cmd_name_rev, RUN_SETUP },
+ { "pack-objects", cmd_pack_objects, RUN_SETUP },
+ { "prune", cmd_prune, RUN_SETUP },
+ { "prune-packed", cmd_prune_packed, RUN_SETUP },
+ { "push", cmd_push, RUN_SETUP },
+ { "read-tree", cmd_read_tree, RUN_SETUP },
{ "repo-config", cmd_repo_config },
+ { "rev-list", cmd_rev_list, RUN_SETUP },
+ { "rev-parse", cmd_rev_parse, RUN_SETUP },
+ { "rm", cmd_rm, RUN_SETUP },
+ { "show-branch", cmd_show_branch, RUN_SETUP },
+ { "show", cmd_show, RUN_SETUP | USE_PAGER },
+ { "stripspace", cmd_stripspace },
+ { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
+ { "tar-tree", cmd_tar_tree, RUN_SETUP },
+ { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
+ { "update-index", cmd_update_index, RUN_SETUP },
+ { "update-ref", cmd_update_ref, RUN_SETUP },
+ { "upload-tar", cmd_upload_tar },
+ { "version", cmd_version },
+ { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
+ { "write-tree", cmd_write_tree, RUN_SETUP },
};
int i;
continue;
prefix = NULL;
- if (p->option & NEEDS_PREFIX)
+ if (p->option & RUN_SETUP)
prefix = setup_git_directory();
if (p->option & USE_PAGER)
setup_pager();
--- /dev/null
+/*
+ * builtin-help.c
+ *
+ * Builtin help-related commands (help, usage, version)
+ */
+#include <sys/ioctl.h>
+#include "cache.h"
+#include "builtin.h"
+#include "exec_cmd.h"
+#include "common-cmds.h"
+
+
+/* most GUI terminals set COLUMNS (although some don't export it) */
+static int term_columns(void)
+{
+ char *col_string = getenv("COLUMNS");
+ int n_cols = 0;
+
+ if (col_string && (n_cols = atoi(col_string)) > 0)
+ return n_cols;
+
+#ifdef TIOCGWINSZ
+ {
+ struct winsize ws;
+ if (!ioctl(1, TIOCGWINSZ, &ws)) {
+ if (ws.ws_col)
+ return ws.ws_col;
+ }
+ }
+#endif
+
+ return 80;
+}
+
+static void oom(void)
+{
+ fprintf(stderr, "git: out of memory\n");
+ exit(1);
+}
+
+static inline void mput_char(char c, unsigned int num)
+{
+ while(num--)
+ putchar(c);
+}
+
+static struct cmdname {
+ size_t len;
+ char name[1];
+} **cmdname;
+static int cmdname_alloc, cmdname_cnt;
+
+static void add_cmdname(const char *name, int len)
+{
+ struct cmdname *ent;
+ if (cmdname_alloc <= cmdname_cnt) {
+ cmdname_alloc = cmdname_alloc + 200;
+ cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
+ if (!cmdname)
+ oom();
+ }
+ ent = malloc(sizeof(*ent) + len);
+ if (!ent)
+ oom();
+ ent->len = len;
+ memcpy(ent->name, name, len);
+ ent->name[len] = 0;
+ cmdname[cmdname_cnt++] = ent;
+}
+
+static int cmdname_compare(const void *a_, const void *b_)
+{
+ struct cmdname *a = *(struct cmdname **)a_;
+ struct cmdname *b = *(struct cmdname **)b_;
+ return strcmp(a->name, b->name);
+}
+
+static void pretty_print_string_list(struct cmdname **cmdname, int longest)
+{
+ int cols = 1, rows;
+ int space = longest + 1; /* min 1 SP between words */
+ int max_cols = term_columns() - 1; /* don't print *on* the edge */
+ int i, j;
+
+ if (space < max_cols)
+ cols = max_cols / space;
+ rows = (cmdname_cnt + cols - 1) / cols;
+
+ qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
+
+ for (i = 0; i < rows; i++) {
+ printf(" ");
+
+ for (j = 0; j < cols; j++) {
+ int n = j * rows + i;
+ int size = space;
+ if (n >= cmdname_cnt)
+ break;
+ if (j == cols-1 || n + rows >= cmdname_cnt)
+ size = 1;
+ printf("%-*s", size, cmdname[n]->name);
+ }
+ putchar('\n');
+ }
+}
+
+static void list_commands(const char *exec_path, const char *pattern)
+{
+ unsigned int longest = 0;
+ char path[PATH_MAX];
+ int dirlen;
+ DIR *dir = opendir(exec_path);
+ struct dirent *de;
+
+ if (!dir) {
+ fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
+ exit(1);
+ }
+
+ dirlen = strlen(exec_path);
+ if (PATH_MAX - 20 < dirlen) {
+ fprintf(stderr, "git: insanely long exec-path '%s'\n",
+ exec_path);
+ exit(1);
+ }
+
+ memcpy(path, exec_path, dirlen);
+ path[dirlen++] = '/';
+
+ while ((de = readdir(dir)) != NULL) {
+ struct stat st;
+ int entlen;
+
+ if (strncmp(de->d_name, "git-", 4))
+ continue;
+ strcpy(path+dirlen, de->d_name);
+ if (stat(path, &st) || /* stat, not lstat */
+ !S_ISREG(st.st_mode) ||
+ !(st.st_mode & S_IXUSR))
+ continue;
+
+ entlen = strlen(de->d_name);
+ if (has_extension(de->d_name, entlen, ".exe"))
+ entlen -= 4;
+
+ if (longest < entlen)
+ longest = entlen;
+
+ add_cmdname(de->d_name + 4, entlen-4);
+ }
+ closedir(dir);
+
+ printf("git commands available in '%s'\n", exec_path);
+ printf("----------------------------");
+ mput_char('-', strlen(exec_path));
+ putchar('\n');
+ pretty_print_string_list(cmdname, longest - 4);
+ putchar('\n');
+}
+
+static void list_common_cmds_help(void)
+{
+ int i, longest = 0;
+
+ for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+ if (longest < strlen(common_cmds[i].name))
+ longest = strlen(common_cmds[i].name);
+ }
+
+ puts("The most commonly used git commands are:");
+ for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+ printf(" %s", common_cmds[i].name);
+ mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
+ puts(common_cmds[i].help);
+ }
+ puts("(use 'git help -a' to get a list of all installed git commands)");
+}
+
+static void show_man_page(const char *git_cmd)
+{
+ const char *page;
+
+ if (!strncmp(git_cmd, "git", 3))
+ page = git_cmd;
+ else {
+ int page_len = strlen(git_cmd) + 4;
+ char *p = malloc(page_len + 1);
+ strcpy(p, "git-");
+ strcpy(p + 4, git_cmd);
+ p[page_len] = 0;
+ page = p;
+ }
+
+ execlp("man", "man", page, NULL);
+}
+
+void help_unknown_cmd(const char *cmd)
+{
+ printf("git: '%s' is not a git-command\n\n", cmd);
+ list_common_cmds_help();
+ exit(1);
+}
+
+int cmd_version(int argc, const char **argv, const char *prefix)
+{
+ printf("git version %s\n", git_version_string);
+ return 0;
+}
+
+int cmd_help(int argc, const char **argv, const char *prefix)
+{
+ const char *help_cmd = argc > 1 ? argv[1] : NULL;
+ const char *exec_path = git_exec_path();
+
+ if (!help_cmd) {
+ printf("usage: %s\n\n", git_usage_string);
+ list_common_cmds_help();
+ exit(1);
+ }
+
+ else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
+ printf("usage: %s\n\n", git_usage_string);
+ if(exec_path)
+ list_commands(exec_path, "git-*");
+ exit(1);
+ }
+
+ else
+ show_man_page(help_cmd);
+
+ return 0;
+}
+
+
+++ /dev/null
-#include <stdlib.h>
-#include "cache.h"
-#include "commit.h"
-#include "tag.h"
-#include "refs.h"
-
-static const char name_rev_usage[] =
- "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
-
-typedef struct rev_name {
- const char *tip_name;
- int merge_traversals;
- int generation;
-} rev_name;
-
-static long cutoff = LONG_MAX;
-
-static void name_rev(struct commit *commit,
- const char *tip_name, int merge_traversals, int generation,
- int deref)
-{
- struct rev_name *name = (struct rev_name *)commit->util;
- struct commit_list *parents;
- int parent_number = 1;
-
- if (!commit->object.parsed)
- parse_commit(commit);
-
- if (commit->date < cutoff)
- return;
-
- if (deref) {
- char *new_name = xmalloc(strlen(tip_name)+3);
- strcpy(new_name, tip_name);
- strcat(new_name, "^0");
- tip_name = new_name;
-
- if (generation)
- die("generation: %d, but deref?", generation);
- }
-
- if (name == NULL) {
- name = xmalloc(sizeof(rev_name));
- commit->util = name;
- goto copy_data;
- } else if (name->merge_traversals > merge_traversals ||
- (name->merge_traversals == merge_traversals &&
- name->generation > generation)) {
-copy_data:
- name->tip_name = tip_name;
- name->merge_traversals = merge_traversals;
- name->generation = generation;
- } else
- return;
-
- for (parents = commit->parents;
- parents;
- parents = parents->next, parent_number++) {
- if (parent_number > 1) {
- char *new_name = xmalloc(strlen(tip_name)+8);
-
- if (generation > 0)
- sprintf(new_name, "%s~%d^%d", tip_name,
- generation, parent_number);
- else
- sprintf(new_name, "%s^%d", tip_name, parent_number);
-
- name_rev(parents->item, new_name,
- merge_traversals + 1 , 0, 0);
- } else {
- name_rev(parents->item, tip_name, merge_traversals,
- generation + 1, 0);
- }
- }
-}
-
-static int tags_only = 0;
-
-static int name_ref(const char *path, const unsigned char *sha1)
-{
- struct object *o = parse_object(sha1);
- int deref = 0;
-
- if (tags_only && strncmp(path, "refs/tags/", 10))
- return 0;
-
- while (o && o->type == OBJ_TAG) {
- struct tag *t = (struct tag *) o;
- if (!t->tagged)
- break; /* broken repository */
- o = parse_object(t->tagged->sha1);
- deref = 1;
- }
- if (o && o->type == OBJ_COMMIT) {
- struct commit *commit = (struct commit *)o;
-
- if (!strncmp(path, "refs/heads/", 11))
- path = path + 11;
- else if (!strncmp(path, "refs/", 5))
- path = path + 5;
-
- name_rev(commit, strdup(path), 0, 0, deref);
- }
- return 0;
-}
-
-/* returns a static buffer */
-static const char* get_rev_name(struct object *o)
-{
- static char buffer[1024];
- struct rev_name *n;
- struct commit *c;
-
- if (o->type != OBJ_COMMIT)
- return "undefined";
- c = (struct commit *) o;
- n = c->util;
- if (!n)
- return "undefined";
-
- if (!n->generation)
- return n->tip_name;
-
- snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
-
- return buffer;
-}
-
-int main(int argc, char **argv)
-{
- struct object_array revs = { 0, 0, NULL };
- int as_is = 0, all = 0, transform_stdin = 0;
-
- setup_git_directory();
- git_config(git_default_config);
-
- if (argc < 2)
- usage(name_rev_usage);
-
- for (--argc, ++argv; argc; --argc, ++argv) {
- unsigned char sha1[20];
- struct object *o;
- struct commit *commit;
-
- if (!as_is && (*argv)[0] == '-') {
- if (!strcmp(*argv, "--")) {
- as_is = 1;
- continue;
- } else if (!strcmp(*argv, "--tags")) {
- tags_only = 1;
- continue;
- } else if (!strcmp(*argv, "--all")) {
- if (argc > 1)
- die("Specify either a list, or --all, not both!");
- all = 1;
- cutoff = 0;
- continue;
- } else if (!strcmp(*argv, "--stdin")) {
- if (argc > 1)
- die("Specify either a list, or --stdin, not both!");
- transform_stdin = 1;
- cutoff = 0;
- continue;
- }
- usage(name_rev_usage);
- }
-
- if (get_sha1(*argv, sha1)) {
- fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
- *argv);
- continue;
- }
-
- o = deref_tag(parse_object(sha1), *argv, 0);
- if (!o || o->type != OBJ_COMMIT) {
- fprintf(stderr, "Could not get commit for %s. Skipping.\n",
- *argv);
- continue;
- }
-
- commit = (struct commit *)o;
-
- if (cutoff > commit->date)
- cutoff = commit->date;
-
- add_object_array((struct object *)commit, *argv, &revs);
- }
-
- for_each_ref(name_ref);
-
- if (transform_stdin) {
- char buffer[2048];
- char *p, *p_start;
-
- while (!feof(stdin)) {
- int forty = 0;
- p = fgets(buffer, sizeof(buffer), stdin);
- if (!p)
- break;
-
- for (p_start = p; *p; p++) {
-#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
- if (!ishex(*p))
- forty = 0;
- else if (++forty == 40 &&
- !ishex(*(p+1))) {
- unsigned char sha1[40];
- const char *name = "undefined";
- char c = *(p+1);
-
- forty = 0;
-
- *(p+1) = 0;
- if (!get_sha1(p - 39, sha1)) {
- struct object *o =
- lookup_object(sha1);
- if (o)
- name = get_rev_name(o);
- }
- *(p+1) = c;
-
- if (!strcmp(name, "undefined"))
- continue;
-
- fwrite(p_start, p - p_start + 1, 1,
- stdout);
- printf(" (%s)", name);
- p_start = p + 1;
- }
- }
-
- /* flush */
- if (p_start != p)
- fwrite(p_start, p - p_start, 1, stdout);
- }
- } else if (all) {
- int i, max;
-
- max = get_max_object_index();
- for (i = 0; i < max; i++) {
- struct object * obj = get_indexed_object(i);
- if (!obj)
- continue;
- printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
- }
- } else {
- int i;
- for (i = 0; i < revs.nr; i++)
- printf("%s %s\n",
- revs.objects[i].name,
- get_rev_name(revs.objects[i].item));
- }
-
- return 0;
-}
-
+++ /dev/null
-#include "cache.h"
-#include "object.h"
-#include "blob.h"
-#include "commit.h"
-#include "tag.h"
-#include "tree.h"
-#include "delta.h"
-#include "pack.h"
-#include "csum-file.h"
-#include "tree-walk.h"
-#include <sys/time.h>
-#include <signal.h>
-
-static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
-
-struct object_entry {
- unsigned char sha1[20];
- unsigned long size; /* uncompressed size */
- unsigned long offset; /* offset into the final pack file;
- * nonzero if already written.
- */
- unsigned int depth; /* delta depth */
- unsigned int delta_limit; /* base adjustment for in-pack delta */
- unsigned int hash; /* name hint hash */
- enum object_type type;
- enum object_type in_pack_type; /* could be delta */
- unsigned long delta_size; /* delta data size (uncompressed) */
- struct object_entry *delta; /* delta base object */
- struct packed_git *in_pack; /* already in pack */
- unsigned int in_pack_offset;
- struct object_entry *delta_child; /* deltified objects who bases me */
- struct object_entry *delta_sibling; /* other deltified objects who
- * uses the same base as me
- */
- int preferred_base; /* we do not pack this, but is encouraged to
- * be used as the base objectto delta huge
- * objects against.
- */
-};
-
-/*
- * Objects we are going to pack are collected in objects array (dynamically
- * expanded). nr_objects & nr_alloc controls this array. They are stored
- * in the order we see -- typically rev-list --objects order that gives us
- * nice "minimum seek" order.
- *
- * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
- * elements in the objects array. The former is used to build the pack
- * index (lists object names in the ascending order to help offset lookup),
- * and the latter is used to group similar things together by try_delta()
- * heuristics.
- */
-
-static unsigned char object_list_sha1[20];
-static int non_empty = 0;
-static int no_reuse_delta = 0;
-static int local = 0;
-static int incremental = 0;
-static struct object_entry **sorted_by_sha, **sorted_by_type;
-static struct object_entry *objects = NULL;
-static int nr_objects = 0, nr_alloc = 0, nr_result = 0;
-static const char *base_name;
-static unsigned char pack_file_sha1[20];
-static int progress = 1;
-static volatile sig_atomic_t progress_update = 0;
-static int window = 10;
-
-/*
- * The object names in objects array are hashed with this hashtable,
- * to help looking up the entry by object name. Binary search from
- * sorted_by_sha is also possible but this was easier to code and faster.
- * This hashtable is built after all the objects are seen.
- */
-static int *object_ix = NULL;
-static int object_ix_hashsz = 0;
-
-/*
- * Pack index for existing packs give us easy access to the offsets into
- * corresponding pack file where each object's data starts, but the entries
- * do not store the size of the compressed representation (uncompressed
- * size is easily available by examining the pack entry header). We build
- * a hashtable of existing packs (pack_revindex), and keep reverse index
- * here -- pack index file is sorted by object name mapping to offset; this
- * pack_revindex[].revindex array is an ordered list of offsets, so if you
- * know the offset of an object, next offset is where its packed
- * representation ends.
- */
-struct pack_revindex {
- struct packed_git *p;
- unsigned long *revindex;
-} *pack_revindex = NULL;
-static int pack_revindex_hashsz = 0;
-
-/*
- * stats
- */
-static int written = 0;
-static int written_delta = 0;
-static int reused = 0;
-static int reused_delta = 0;
-
-static int pack_revindex_ix(struct packed_git *p)
-{
- unsigned long ui = (unsigned long)p;
- int i;
-
- ui = ui ^ (ui >> 16); /* defeat structure alignment */
- i = (int)(ui % pack_revindex_hashsz);
- while (pack_revindex[i].p) {
- if (pack_revindex[i].p == p)
- return i;
- if (++i == pack_revindex_hashsz)
- i = 0;
- }
- return -1 - i;
-}
-
-static void prepare_pack_ix(void)
-{
- int num;
- struct packed_git *p;
- for (num = 0, p = packed_git; p; p = p->next)
- num++;
- if (!num)
- return;
- pack_revindex_hashsz = num * 11;
- pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
- for (p = packed_git; p; p = p->next) {
- num = pack_revindex_ix(p);
- num = - 1 - num;
- pack_revindex[num].p = p;
- }
- /* revindex elements are lazily initialized */
-}
-
-static int cmp_offset(const void *a_, const void *b_)
-{
- unsigned long a = *(unsigned long *) a_;
- unsigned long b = *(unsigned long *) b_;
- if (a < b)
- return -1;
- else if (a == b)
- return 0;
- else
- return 1;
-}
-
-/*
- * Ordered list of offsets of objects in the pack.
- */
-static void prepare_pack_revindex(struct pack_revindex *rix)
-{
- struct packed_git *p = rix->p;
- int num_ent = num_packed_objects(p);
- int i;
- void *index = p->index_base + 256;
-
- rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
- for (i = 0; i < num_ent; i++) {
- unsigned int hl = *((unsigned int *)((char *) index + 24*i));
- rix->revindex[i] = ntohl(hl);
- }
- /* This knows the pack format -- the 20-byte trailer
- * follows immediately after the last object data.
- */
- rix->revindex[num_ent] = p->pack_size - 20;
- qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
-}
-
-static unsigned long find_packed_object_size(struct packed_git *p,
- unsigned long ofs)
-{
- int num;
- int lo, hi;
- struct pack_revindex *rix;
- unsigned long *revindex;
- num = pack_revindex_ix(p);
- if (num < 0)
- die("internal error: pack revindex uninitialized");
- rix = &pack_revindex[num];
- if (!rix->revindex)
- prepare_pack_revindex(rix);
- revindex = rix->revindex;
- lo = 0;
- hi = num_packed_objects(p) + 1;
- do {
- int mi = (lo + hi) / 2;
- if (revindex[mi] == ofs) {
- return revindex[mi+1] - ofs;
- }
- else if (ofs < revindex[mi])
- hi = mi;
- else
- lo = mi + 1;
- } while (lo < hi);
- die("internal error: pack revindex corrupt");
-}
-
-static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
-{
- unsigned long othersize, delta_size;
- char type[10];
- void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
- void *delta_buf;
-
- if (!otherbuf)
- die("unable to read %s", sha1_to_hex(entry->delta->sha1));
- delta_buf = diff_delta(otherbuf, othersize,
- buf, size, &delta_size, 0);
- if (!delta_buf || delta_size != entry->delta_size)
- die("delta size changed");
- free(buf);
- free(otherbuf);
- return delta_buf;
-}
-
-/*
- * The per-object header is a pretty dense thing, which is
- * - first byte: low four bits are "size", then three bits of "type",
- * and the high bit is "size continues".
- * - each byte afterwards: low seven bits are size continuation,
- * with the high bit being "size continues"
- */
-static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
-{
- int n = 1;
- unsigned char c;
-
- if (type < OBJ_COMMIT || type > OBJ_DELTA)
- die("bad type %d", type);
-
- c = (type << 4) | (size & 15);
- size >>= 4;
- while (size) {
- *hdr++ = c | 0x80;
- c = size & 0x7f;
- size >>= 7;
- n++;
- }
- *hdr = c;
- return n;
-}
-
-static unsigned long write_object(struct sha1file *f,
- struct object_entry *entry)
-{
- unsigned long size;
- char type[10];
- void *buf;
- unsigned char header[10];
- unsigned hdrlen, datalen;
- enum object_type obj_type;
- int to_reuse = 0;
-
- if (entry->preferred_base)
- return 0;
-
- obj_type = entry->type;
- if (! entry->in_pack)
- to_reuse = 0; /* can't reuse what we don't have */
- else if (obj_type == OBJ_DELTA)
- to_reuse = 1; /* check_object() decided it for us */
- else if (obj_type != entry->in_pack_type)
- to_reuse = 0; /* pack has delta which is unusable */
- else if (entry->delta)
- to_reuse = 0; /* we want to pack afresh */
- else
- to_reuse = 1; /* we have it in-pack undeltified,
- * and we do not need to deltify it.
- */
-
- if (! to_reuse) {
- buf = read_sha1_file(entry->sha1, type, &size);
- if (!buf)
- die("unable to read %s", sha1_to_hex(entry->sha1));
- if (size != entry->size)
- die("object %s size inconsistency (%lu vs %lu)",
- sha1_to_hex(entry->sha1), size, entry->size);
- if (entry->delta) {
- buf = delta_against(buf, size, entry);
- size = entry->delta_size;
- obj_type = OBJ_DELTA;
- }
- /*
- * The object header is a byte of 'type' followed by zero or
- * more bytes of length. For deltas, the 20 bytes of delta
- * sha1 follows that.
- */
- hdrlen = encode_header(obj_type, size, header);
- sha1write(f, header, hdrlen);
-
- if (entry->delta) {
- sha1write(f, entry->delta, 20);
- hdrlen += 20;
- }
- datalen = sha1write_compressed(f, buf, size);
- free(buf);
- }
- else {
- struct packed_git *p = entry->in_pack;
- use_packed_git(p);
-
- datalen = find_packed_object_size(p, entry->in_pack_offset);
- buf = (char *) p->pack_base + entry->in_pack_offset;
- sha1write(f, buf, datalen);
- unuse_packed_git(p);
- hdrlen = 0; /* not really */
- if (obj_type == OBJ_DELTA)
- reused_delta++;
- reused++;
- }
- if (obj_type == OBJ_DELTA)
- written_delta++;
- written++;
- return hdrlen + datalen;
-}
-
-static unsigned long write_one(struct sha1file *f,
- struct object_entry *e,
- unsigned long offset)
-{
- if (e->offset)
- /* offset starts from header size and cannot be zero
- * if it is written already.
- */
- return offset;
- e->offset = offset;
- offset += write_object(f, e);
- /* if we are deltified, write out its base object. */
- if (e->delta)
- offset = write_one(f, e->delta, offset);
- return offset;
-}
-
-static void write_pack_file(void)
-{
- int i;
- struct sha1file *f;
- unsigned long offset;
- struct pack_header hdr;
- unsigned last_percent = 999;
- int do_progress = 0;
-
- if (!base_name)
- f = sha1fd(1, "<stdout>");
- else {
- f = sha1create("%s-%s.%s", base_name,
- sha1_to_hex(object_list_sha1), "pack");
- do_progress = progress;
- }
- if (do_progress)
- fprintf(stderr, "Writing %d objects.\n", nr_result);
-
- hdr.hdr_signature = htonl(PACK_SIGNATURE);
- hdr.hdr_version = htonl(PACK_VERSION);
- hdr.hdr_entries = htonl(nr_result);
- sha1write(f, &hdr, sizeof(hdr));
- offset = sizeof(hdr);
- if (!nr_result)
- goto done;
- for (i = 0; i < nr_objects; i++) {
- offset = write_one(f, objects + i, offset);
- if (do_progress) {
- unsigned percent = written * 100 / nr_result;
- if (progress_update || percent != last_percent) {
- fprintf(stderr, "%4u%% (%u/%u) done\r",
- percent, written, nr_result);
- progress_update = 0;
- last_percent = percent;
- }
- }
- }
- if (do_progress)
- fputc('\n', stderr);
- done:
- sha1close(f, pack_file_sha1, 1);
-}
-
-static void write_index_file(void)
-{
- int i;
- struct sha1file *f = sha1create("%s-%s.%s", base_name,
- sha1_to_hex(object_list_sha1), "idx");
- struct object_entry **list = sorted_by_sha;
- struct object_entry **last = list + nr_result;
- unsigned int array[256];
-
- /*
- * Write the first-level table (the list is sorted,
- * but we use a 256-entry lookup to be able to avoid
- * having to do eight extra binary search iterations).
- */
- for (i = 0; i < 256; i++) {
- struct object_entry **next = list;
- while (next < last) {
- struct object_entry *entry = *next;
- if (entry->sha1[0] != i)
- break;
- next++;
- }
- array[i] = htonl(next - sorted_by_sha);
- list = next;
- }
- sha1write(f, array, 256 * sizeof(int));
-
- /*
- * Write the actual SHA1 entries..
- */
- list = sorted_by_sha;
- for (i = 0; i < nr_result; i++) {
- struct object_entry *entry = *list++;
- unsigned int offset = htonl(entry->offset);
- sha1write(f, &offset, 4);
- sha1write(f, entry->sha1, 20);
- }
- sha1write(f, pack_file_sha1, 20);
- sha1close(f, NULL, 1);
-}
-
-static int locate_object_entry_hash(const unsigned char *sha1)
-{
- int i;
- unsigned int ui;
- memcpy(&ui, sha1, sizeof(unsigned int));
- i = ui % object_ix_hashsz;
- while (0 < object_ix[i]) {
- if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
- return i;
- if (++i == object_ix_hashsz)
- i = 0;
- }
- return -1 - i;
-}
-
-static struct object_entry *locate_object_entry(const unsigned char *sha1)
-{
- int i;
-
- if (!object_ix_hashsz)
- return NULL;
-
- i = locate_object_entry_hash(sha1);
- if (0 <= i)
- return &objects[object_ix[i]-1];
- return NULL;
-}
-
-static void rehash_objects(void)
-{
- int i;
- struct object_entry *oe;
-
- object_ix_hashsz = nr_objects * 3;
- if (object_ix_hashsz < 1024)
- object_ix_hashsz = 1024;
- object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
- memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
- for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
- int ix = locate_object_entry_hash(oe->sha1);
- if (0 <= ix)
- continue;
- ix = -1 - ix;
- object_ix[ix] = i + 1;
- }
-}
-
-static unsigned name_hash(const char *name)
-{
- unsigned char c;
- unsigned hash = 0;
-
- /*
- * This effectively just creates a sortable number from the
- * last sixteen non-whitespace characters. Last characters
- * count "most", so things that end in ".c" sort together.
- */
- while ((c = *name++) != 0) {
- if (isspace(c))
- continue;
- hash = (hash >> 2) + (c << 24);
- }
- return hash;
-}
-
-static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
-{
- unsigned int idx = nr_objects;
- struct object_entry *entry;
- struct packed_git *p;
- unsigned int found_offset = 0;
- struct packed_git *found_pack = NULL;
- int ix, status = 0;
-
- if (!exclude) {
- for (p = packed_git; p; p = p->next) {
- struct pack_entry e;
- if (find_pack_entry_one(sha1, &e, p)) {
- if (incremental)
- return 0;
- if (local && !p->pack_local)
- return 0;
- if (!found_pack) {
- found_offset = e.offset;
- found_pack = e.p;
- }
- }
- }
- }
- if ((entry = locate_object_entry(sha1)) != NULL)
- goto already_added;
-
- if (idx >= nr_alloc) {
- unsigned int needed = (idx + 1024) * 3 / 2;
- objects = xrealloc(objects, needed * sizeof(*entry));
- nr_alloc = needed;
- }
- entry = objects + idx;
- nr_objects = idx + 1;
- memset(entry, 0, sizeof(*entry));
- memcpy(entry->sha1, sha1, 20);
- entry->hash = hash;
-
- if (object_ix_hashsz * 3 <= nr_objects * 4)
- rehash_objects();
- else {
- ix = locate_object_entry_hash(entry->sha1);
- if (0 <= ix)
- die("internal error in object hashing.");
- object_ix[-1 - ix] = idx + 1;
- }
- status = 1;
-
- already_added:
- if (progress_update) {
- fprintf(stderr, "Counting objects...%d\r", nr_objects);
- progress_update = 0;
- }
- if (exclude)
- entry->preferred_base = 1;
- else {
- if (found_pack) {
- entry->in_pack = found_pack;
- entry->in_pack_offset = found_offset;
- }
- }
- return status;
-}
-
-struct pbase_tree_cache {
- unsigned char sha1[20];
- int ref;
- int temporary;
- void *tree_data;
- unsigned long tree_size;
-};
-
-static struct pbase_tree_cache *(pbase_tree_cache[256]);
-static int pbase_tree_cache_ix(const unsigned char *sha1)
-{
- return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
-}
-static int pbase_tree_cache_ix_incr(int ix)
-{
- return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
-}
-
-static struct pbase_tree {
- struct pbase_tree *next;
- /* This is a phony "cache" entry; we are not
- * going to evict it nor find it through _get()
- * mechanism -- this is for the toplevel node that
- * would almost always change with any commit.
- */
- struct pbase_tree_cache pcache;
-} *pbase_tree;
-
-static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
-{
- struct pbase_tree_cache *ent, *nent;
- void *data;
- unsigned long size;
- char type[20];
- int neigh;
- int my_ix = pbase_tree_cache_ix(sha1);
- int available_ix = -1;
-
- /* pbase-tree-cache acts as a limited hashtable.
- * your object will be found at your index or within a few
- * slots after that slot if it is cached.
- */
- for (neigh = 0; neigh < 8; neigh++) {
- ent = pbase_tree_cache[my_ix];
- if (ent && !memcmp(ent->sha1, sha1, 20)) {
- ent->ref++;
- return ent;
- }
- else if (((available_ix < 0) && (!ent || !ent->ref)) ||
- ((0 <= available_ix) &&
- (!ent && pbase_tree_cache[available_ix])))
- available_ix = my_ix;
- if (!ent)
- break;
- my_ix = pbase_tree_cache_ix_incr(my_ix);
- }
-
- /* Did not find one. Either we got a bogus request or
- * we need to read and perhaps cache.
- */
- data = read_sha1_file(sha1, type, &size);
- if (!data)
- return NULL;
- if (strcmp(type, tree_type)) {
- free(data);
- return NULL;
- }
-
- /* We need to either cache or return a throwaway copy */
-
- if (available_ix < 0)
- ent = NULL;
- else {
- ent = pbase_tree_cache[available_ix];
- my_ix = available_ix;
- }
-
- if (!ent) {
- nent = xmalloc(sizeof(*nent));
- nent->temporary = (available_ix < 0);
- }
- else {
- /* evict and reuse */
- free(ent->tree_data);
- nent = ent;
- }
- memcpy(nent->sha1, sha1, 20);
- nent->tree_data = data;
- nent->tree_size = size;
- nent->ref = 1;
- if (!nent->temporary)
- pbase_tree_cache[my_ix] = nent;
- return nent;
-}
-
-static void pbase_tree_put(struct pbase_tree_cache *cache)
-{
- if (!cache->temporary) {
- cache->ref--;
- return;
- }
- free(cache->tree_data);
- free(cache);
-}
-
-static int name_cmp_len(const char *name)
-{
- int i;
- for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
- ;
- return i;
-}
-
-static void add_pbase_object(struct tree_desc *tree,
- const char *name,
- int cmplen,
- const char *fullname)
-{
- struct name_entry entry;
-
- while (tree_entry(tree,&entry)) {
- unsigned long size;
- char type[20];
-
- if (entry.pathlen != cmplen ||
- memcmp(entry.path, name, cmplen) ||
- !has_sha1_file(entry.sha1) ||
- sha1_object_info(entry.sha1, type, &size))
- continue;
- if (name[cmplen] != '/') {
- unsigned hash = name_hash(fullname);
- add_object_entry(entry.sha1, hash, 1);
- return;
- }
- if (!strcmp(type, tree_type)) {
- struct tree_desc sub;
- struct pbase_tree_cache *tree;
- const char *down = name+cmplen+1;
- int downlen = name_cmp_len(down);
-
- tree = pbase_tree_get(entry.sha1);
- if (!tree)
- return;
- sub.buf = tree->tree_data;
- sub.size = tree->tree_size;
-
- add_pbase_object(&sub, down, downlen, fullname);
- pbase_tree_put(tree);
- }
- }
-}
-
-static unsigned *done_pbase_paths;
-static int done_pbase_paths_num;
-static int done_pbase_paths_alloc;
-static int done_pbase_path_pos(unsigned hash)
-{
- int lo = 0;
- int hi = done_pbase_paths_num;
- while (lo < hi) {
- int mi = (hi + lo) / 2;
- if (done_pbase_paths[mi] == hash)
- return mi;
- if (done_pbase_paths[mi] < hash)
- hi = mi;
- else
- lo = mi + 1;
- }
- return -lo-1;
-}
-
-static int check_pbase_path(unsigned hash)
-{
- int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
- if (0 <= pos)
- return 1;
- pos = -pos - 1;
- if (done_pbase_paths_alloc <= done_pbase_paths_num) {
- done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
- done_pbase_paths = xrealloc(done_pbase_paths,
- done_pbase_paths_alloc *
- sizeof(unsigned));
- }
- done_pbase_paths_num++;
- if (pos < done_pbase_paths_num)
- memmove(done_pbase_paths + pos + 1,
- done_pbase_paths + pos,
- (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
- done_pbase_paths[pos] = hash;
- return 0;
-}
-
-static void add_preferred_base_object(char *name, unsigned hash)
-{
- struct pbase_tree *it;
- int cmplen = name_cmp_len(name);
-
- if (check_pbase_path(hash))
- return;
-
- for (it = pbase_tree; it; it = it->next) {
- if (cmplen == 0) {
- hash = name_hash("");
- add_object_entry(it->pcache.sha1, hash, 1);
- }
- else {
- struct tree_desc tree;
- tree.buf = it->pcache.tree_data;
- tree.size = it->pcache.tree_size;
- add_pbase_object(&tree, name, cmplen, name);
- }
- }
-}
-
-static void add_preferred_base(unsigned char *sha1)
-{
- struct pbase_tree *it;
- void *data;
- unsigned long size;
- unsigned char tree_sha1[20];
-
- data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
- if (!data)
- return;
-
- for (it = pbase_tree; it; it = it->next) {
- if (!memcmp(it->pcache.sha1, tree_sha1, 20)) {
- free(data);
- return;
- }
- }
-
- it = xcalloc(1, sizeof(*it));
- it->next = pbase_tree;
- pbase_tree = it;
-
- memcpy(it->pcache.sha1, tree_sha1, 20);
- it->pcache.tree_data = data;
- it->pcache.tree_size = size;
-}
-
-static void check_object(struct object_entry *entry)
-{
- char type[20];
-
- if (entry->in_pack && !entry->preferred_base) {
- unsigned char base[20];
- unsigned long size;
- struct object_entry *base_entry;
-
- /* We want in_pack_type even if we do not reuse delta.
- * There is no point not reusing non-delta representations.
- */
- check_reuse_pack_delta(entry->in_pack,
- entry->in_pack_offset,
- base, &size,
- &entry->in_pack_type);
-
- /* Check if it is delta, and the base is also an object
- * we are going to pack. If so we will reuse the existing
- * delta.
- */
- if (!no_reuse_delta &&
- entry->in_pack_type == OBJ_DELTA &&
- (base_entry = locate_object_entry(base)) &&
- (!base_entry->preferred_base)) {
-
- /* Depth value does not matter - find_deltas()
- * will never consider reused delta as the
- * base object to deltify other objects
- * against, in order to avoid circular deltas.
- */
-
- /* uncompressed size of the delta data */
- entry->size = entry->delta_size = size;
- entry->delta = base_entry;
- entry->type = OBJ_DELTA;
-
- entry->delta_sibling = base_entry->delta_child;
- base_entry->delta_child = entry;
-
- return;
- }
- /* Otherwise we would do the usual */
- }
-
- if (sha1_object_info(entry->sha1, type, &entry->size))
- die("unable to get type of object %s",
- sha1_to_hex(entry->sha1));
-
- if (!strcmp(type, commit_type)) {
- entry->type = OBJ_COMMIT;
- } else if (!strcmp(type, tree_type)) {
- entry->type = OBJ_TREE;
- } else if (!strcmp(type, blob_type)) {
- entry->type = OBJ_BLOB;
- } else if (!strcmp(type, tag_type)) {
- entry->type = OBJ_TAG;
- } else
- die("unable to pack object %s of type %s",
- sha1_to_hex(entry->sha1), type);
-}
-
-static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
-{
- struct object_entry *child = me->delta_child;
- unsigned int m = n;
- while (child) {
- unsigned int c = check_delta_limit(child, n + 1);
- if (m < c)
- m = c;
- child = child->delta_sibling;
- }
- return m;
-}
-
-static void get_object_details(void)
-{
- int i;
- struct object_entry *entry;
-
- prepare_pack_ix();
- for (i = 0, entry = objects; i < nr_objects; i++, entry++)
- check_object(entry);
-
- if (nr_objects == nr_result) {
- /*
- * Depth of objects that depend on the entry -- this
- * is subtracted from depth-max to break too deep
- * delta chain because of delta data reusing.
- * However, we loosen this restriction when we know we
- * are creating a thin pack -- it will have to be
- * expanded on the other end anyway, so do not
- * artificially cut the delta chain and let it go as
- * deep as it wants.
- */
- for (i = 0, entry = objects; i < nr_objects; i++, entry++)
- if (!entry->delta && entry->delta_child)
- entry->delta_limit =
- check_delta_limit(entry, 1);
- }
-}
-
-typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
-
-static entry_sort_t current_sort;
-
-static int sort_comparator(const void *_a, const void *_b)
-{
- struct object_entry *a = *(struct object_entry **)_a;
- struct object_entry *b = *(struct object_entry **)_b;
- return current_sort(a,b);
-}
-
-static struct object_entry **create_sorted_list(entry_sort_t sort)
-{
- struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
- int i;
-
- for (i = 0; i < nr_objects; i++)
- list[i] = objects + i;
- current_sort = sort;
- qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
- return list;
-}
-
-static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
-{
- return memcmp(a->sha1, b->sha1, 20);
-}
-
-static struct object_entry **create_final_object_list(void)
-{
- struct object_entry **list;
- int i, j;
-
- for (i = nr_result = 0; i < nr_objects; i++)
- if (!objects[i].preferred_base)
- nr_result++;
- list = xmalloc(nr_result * sizeof(struct object_entry *));
- for (i = j = 0; i < nr_objects; i++) {
- if (!objects[i].preferred_base)
- list[j++] = objects + i;
- }
- current_sort = sha1_sort;
- qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
- return list;
-}
-
-static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
-{
- if (a->type < b->type)
- return -1;
- if (a->type > b->type)
- return 1;
- if (a->hash < b->hash)
- return -1;
- if (a->hash > b->hash)
- return 1;
- if (a->preferred_base < b->preferred_base)
- return -1;
- if (a->preferred_base > b->preferred_base)
- return 1;
- if (a->size < b->size)
- return -1;
- if (a->size > b->size)
- return 1;
- return a < b ? -1 : (a > b);
-}
-
-struct unpacked {
- struct object_entry *entry;
- void *data;
- struct delta_index *index;
-};
-
-/*
- * We search for deltas _backwards_ in a list sorted by type and
- * by size, so that we see progressively smaller and smaller files.
- * That's because we prefer deltas to be from the bigger file
- * to the smaller - deletes are potentially cheaper, but perhaps
- * more importantly, the bigger file is likely the more recent
- * one.
- */
-static int try_delta(struct unpacked *trg, struct unpacked *src,
- unsigned max_depth)
-{
- struct object_entry *trg_entry = trg->entry;
- struct object_entry *src_entry = src->entry;
- unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
- char type[10];
- void *delta_buf;
-
- /* Don't bother doing diffs between different types */
- if (trg_entry->type != src_entry->type)
- return -1;
-
- /* We do not compute delta to *create* objects we are not
- * going to pack.
- */
- if (trg_entry->preferred_base)
- return -1;
-
- /*
- * We do not bother to try a delta that we discarded
- * on an earlier try, but only when reusing delta data.
- */
- if (!no_reuse_delta && trg_entry->in_pack &&
- trg_entry->in_pack == src_entry->in_pack)
- return 0;
-
- /*
- * If the current object is at pack edge, take the depth the
- * objects that depend on the current object into account --
- * otherwise they would become too deep.
- */
- if (trg_entry->delta_child) {
- if (max_depth <= trg_entry->delta_limit)
- return 0;
- max_depth -= trg_entry->delta_limit;
- }
- if (src_entry->depth >= max_depth)
- return 0;
-
- /* Now some size filtering heuristics. */
- trg_size = trg_entry->size;
- max_size = trg_size/2 - 20;
- max_size = max_size * (max_depth - src_entry->depth) / max_depth;
- if (max_size == 0)
- return 0;
- if (trg_entry->delta && trg_entry->delta_size <= max_size)
- max_size = trg_entry->delta_size-1;
- src_size = src_entry->size;
- sizediff = src_size < trg_size ? trg_size - src_size : 0;
- if (sizediff >= max_size)
- return 0;
-
- /* Load data if not already done */
- if (!trg->data) {
- trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
- if (sz != trg_size)
- die("object %s inconsistent object length (%lu vs %lu)",
- sha1_to_hex(trg_entry->sha1), sz, trg_size);
- }
- if (!src->data) {
- src->data = read_sha1_file(src_entry->sha1, type, &sz);
- if (sz != src_size)
- die("object %s inconsistent object length (%lu vs %lu)",
- sha1_to_hex(src_entry->sha1), sz, src_size);
- }
- if (!src->index) {
- src->index = create_delta_index(src->data, src_size);
- if (!src->index)
- die("out of memory");
- }
-
- delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
- if (!delta_buf)
- return 0;
-
- trg_entry->delta = src_entry;
- trg_entry->delta_size = delta_size;
- trg_entry->depth = src_entry->depth + 1;
- free(delta_buf);
- return 1;
-}
-
-static void progress_interval(int signum)
-{
- progress_update = 1;
-}
-
-static void find_deltas(struct object_entry **list, int window, int depth)
-{
- int i, idx;
- unsigned int array_size = window * sizeof(struct unpacked);
- struct unpacked *array = xmalloc(array_size);
- unsigned processed = 0;
- unsigned last_percent = 999;
-
- memset(array, 0, array_size);
- i = nr_objects;
- idx = 0;
- if (progress)
- fprintf(stderr, "Deltifying %d objects.\n", nr_result);
-
- while (--i >= 0) {
- struct object_entry *entry = list[i];
- struct unpacked *n = array + idx;
- int j;
-
- if (!entry->preferred_base)
- processed++;
-
- if (progress) {
- unsigned percent = processed * 100 / nr_result;
- if (percent != last_percent || progress_update) {
- fprintf(stderr, "%4u%% (%u/%u) done\r",
- percent, processed, nr_result);
- progress_update = 0;
- last_percent = percent;
- }
- }
-
- if (entry->delta)
- /* This happens if we decided to reuse existing
- * delta from a pack. "!no_reuse_delta &&" is implied.
- */
- continue;
-
- if (entry->size < 50)
- continue;
- free_delta_index(n->index);
- n->index = NULL;
- free(n->data);
- n->data = NULL;
- n->entry = entry;
-
- j = window;
- while (--j > 0) {
- unsigned int other_idx = idx + j;
- struct unpacked *m;
- if (other_idx >= window)
- other_idx -= window;
- m = array + other_idx;
- if (!m->entry)
- break;
- if (try_delta(n, m, depth) < 0)
- break;
- }
- /* if we made n a delta, and if n is already at max
- * depth, leaving it in the window is pointless. we
- * should evict it first.
- */
- if (entry->delta && depth <= entry->depth)
- continue;
-
- idx++;
- if (idx >= window)
- idx = 0;
- }
-
- if (progress)
- fputc('\n', stderr);
-
- for (i = 0; i < window; ++i) {
- free_delta_index(array[i].index);
- free(array[i].data);
- }
- free(array);
-}
-
-static void prepare_pack(int window, int depth)
-{
- get_object_details();
- sorted_by_type = create_sorted_list(type_size_sort);
- if (window && depth)
- find_deltas(sorted_by_type, window+1, depth);
-}
-
-static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
-{
- static const char cache[] = "pack-cache/pack-%s.%s";
- char *cached_pack, *cached_idx;
- int ifd, ofd, ifd_ix = -1;
-
- cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
- ifd = open(cached_pack, O_RDONLY);
- if (ifd < 0)
- return 0;
-
- if (!pack_to_stdout) {
- cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
- ifd_ix = open(cached_idx, O_RDONLY);
- if (ifd_ix < 0) {
- close(ifd);
- return 0;
- }
- }
-
- if (progress)
- fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
- sha1_to_hex(sha1));
-
- if (pack_to_stdout) {
- if (copy_fd(ifd, 1))
- exit(1);
- close(ifd);
- }
- else {
- char name[PATH_MAX];
- snprintf(name, sizeof(name),
- "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
- ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
- if (ofd < 0)
- die("unable to open %s (%s)", name, strerror(errno));
- if (copy_fd(ifd, ofd))
- exit(1);
- close(ifd);
-
- snprintf(name, sizeof(name),
- "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
- ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
- if (ofd < 0)
- die("unable to open %s (%s)", name, strerror(errno));
- if (copy_fd(ifd_ix, ofd))
- exit(1);
- close(ifd_ix);
- puts(sha1_to_hex(sha1));
- }
-
- return 1;
-}
-
-static void setup_progress_signal(void)
-{
- struct sigaction sa;
- struct itimerval v;
-
- memset(&sa, 0, sizeof(sa));
- sa.sa_handler = progress_interval;
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = SA_RESTART;
- sigaction(SIGALRM, &sa, NULL);
-
- v.it_interval.tv_sec = 1;
- v.it_interval.tv_usec = 0;
- v.it_value = v.it_interval;
- setitimer(ITIMER_REAL, &v, NULL);
-}
-
-static int git_pack_config(const char *k, const char *v)
-{
- if(!strcmp(k, "pack.window")) {
- window = git_config_int(k, v);
- return 0;
- }
- return git_default_config(k, v);
-}
-
-int main(int argc, char **argv)
-{
- SHA_CTX ctx;
- char line[40 + 1 + PATH_MAX + 2];
- int depth = 10, pack_to_stdout = 0;
- struct object_entry **list;
- int num_preferred_base = 0;
- int i;
-
- setup_git_directory();
- git_config(git_pack_config);
-
- progress = isatty(2);
- for (i = 1; i < argc; i++) {
- const char *arg = argv[i];
-
- if (*arg == '-') {
- if (!strcmp("--non-empty", arg)) {
- non_empty = 1;
- continue;
- }
- if (!strcmp("--local", arg)) {
- local = 1;
- continue;
- }
- if (!strcmp("--progress", arg)) {
- progress = 1;
- continue;
- }
- if (!strcmp("--incremental", arg)) {
- incremental = 1;
- continue;
- }
- if (!strncmp("--window=", arg, 9)) {
- char *end;
- window = strtoul(arg+9, &end, 0);
- if (!arg[9] || *end)
- usage(pack_usage);
- continue;
- }
- if (!strncmp("--depth=", arg, 8)) {
- char *end;
- depth = strtoul(arg+8, &end, 0);
- if (!arg[8] || *end)
- usage(pack_usage);
- continue;
- }
- if (!strcmp("--progress", arg)) {
- progress = 1;
- continue;
- }
- if (!strcmp("-q", arg)) {
- progress = 0;
- continue;
- }
- if (!strcmp("--no-reuse-delta", arg)) {
- no_reuse_delta = 1;
- continue;
- }
- if (!strcmp("--stdout", arg)) {
- pack_to_stdout = 1;
- continue;
- }
- usage(pack_usage);
- }
- if (base_name)
- usage(pack_usage);
- base_name = arg;
- }
-
- if (pack_to_stdout != !base_name)
- usage(pack_usage);
-
- prepare_packed_git();
-
- if (progress) {
- fprintf(stderr, "Generating pack...\n");
- setup_progress_signal();
- }
-
- for (;;) {
- unsigned char sha1[20];
- unsigned hash;
-
- if (!fgets(line, sizeof(line), stdin)) {
- if (feof(stdin))
- break;
- if (!ferror(stdin))
- die("fgets returned NULL, not EOF, not error!");
- if (errno != EINTR)
- die("fgets: %s", strerror(errno));
- clearerr(stdin);
- continue;
- }
-
- if (line[0] == '-') {
- if (get_sha1_hex(line+1, sha1))
- die("expected edge sha1, got garbage:\n %s",
- line+1);
- if (num_preferred_base++ < window)
- add_preferred_base(sha1);
- continue;
- }
- if (get_sha1_hex(line, sha1))
- die("expected sha1, got garbage:\n %s", line);
- hash = name_hash(line+41);
- add_preferred_base_object(line+41, hash);
- add_object_entry(sha1, hash, 0);
- }
- if (progress)
- fprintf(stderr, "Done counting %d objects.\n", nr_objects);
- sorted_by_sha = create_final_object_list();
- if (non_empty && !nr_result)
- return 0;
-
- SHA1_Init(&ctx);
- list = sorted_by_sha;
- for (i = 0; i < nr_result; i++) {
- struct object_entry *entry = *list++;
- SHA1_Update(&ctx, entry->sha1, 20);
- }
- SHA1_Final(object_list_sha1, &ctx);
- if (progress && (nr_objects != nr_result))
- fprintf(stderr, "Result has %d objects.\n", nr_result);
-
- if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
- ;
- else {
- if (nr_result)
- prepare_pack(window, depth);
- if (progress && pack_to_stdout) {
- /* the other end usually displays progress itself */
- struct itimerval v = {{0,},};
- setitimer(ITIMER_REAL, &v, NULL);
- signal(SIGALRM, SIG_IGN );
- progress_update = 0;
- }
- write_pack_file();
- if (!pack_to_stdout) {
- write_index_file();
- puts(sha1_to_hex(object_list_sha1));
- }
- }
- if (progress)
- fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
- nr_result, written, written_delta, reused, reused_delta);
- return 0;
-}
+++ /dev/null
-#include "cache.h"
-
-static const char git_symbolic_ref_usage[] =
-"git-symbolic-ref name [ref]";
-
-static void check_symref(const char *HEAD)
-{
- unsigned char sha1[20];
- const char *git_HEAD = strdup(git_path("%s", HEAD));
- const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 0);
- if (git_refs_heads_master) {
- /* we want to strip the .git/ part */
- int pfxlen = strlen(git_HEAD) - strlen(HEAD);
- puts(git_refs_heads_master + pfxlen);
- }
- else
- die("No such ref: %s", HEAD);
-}
-
-int main(int argc, const char **argv)
-{
- setup_git_directory();
- git_config(git_default_config);
- switch (argc) {
- case 2:
- check_symref(argv[1]);
- break;
- case 3:
- create_symref(strdup(git_path("%s", argv[1])), argv[2]);
- break;
- default:
- usage(git_symbolic_ref_usage);
- }
- return 0;
-}
+++ /dev/null
-#include "cache.h"
-#include "object.h"
-#include "delta.h"
-#include "pack.h"
-#include "blob.h"
-#include "commit.h"
-#include "tag.h"
-#include "tree.h"
-
-#include <sys/time.h>
-
-static int dry_run, quiet;
-static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
-
-/* We always read in 4kB chunks. */
-static unsigned char buffer[4096];
-static unsigned long offset, len, eof;
-static SHA_CTX ctx;
-
-/*
- * Make sure at least "min" bytes are available in the buffer, and
- * return the pointer to the buffer.
- */
-static void * fill(int min)
-{
- if (min <= len)
- return buffer + offset;
- if (eof)
- die("unable to fill input");
- if (min > sizeof(buffer))
- die("cannot fill %d bytes", min);
- if (offset) {
- SHA1_Update(&ctx, buffer, offset);
- memcpy(buffer, buffer + offset, len);
- offset = 0;
- }
- do {
- int ret = xread(0, buffer + len, sizeof(buffer) - len);
- if (ret <= 0) {
- if (!ret)
- die("early EOF");
- die("read error on input: %s", strerror(errno));
- }
- len += ret;
- } while (len < min);
- return buffer;
-}
-
-static void use(int bytes)
-{
- if (bytes > len)
- die("used more bytes than were available");
- len -= bytes;
- offset += bytes;
-}
-
-static void *get_data(unsigned long size)
-{
- z_stream stream;
- void *buf = xmalloc(size);
-
- memset(&stream, 0, sizeof(stream));
-
- stream.next_out = buf;
- stream.avail_out = size;
- stream.next_in = fill(1);
- stream.avail_in = len;
- inflateInit(&stream);
-
- for (;;) {
- int ret = inflate(&stream, 0);
- use(len - stream.avail_in);
- if (stream.total_out == size && ret == Z_STREAM_END)
- break;
- if (ret != Z_OK)
- die("inflate returned %d\n", ret);
- stream.next_in = fill(1);
- stream.avail_in = len;
- }
- inflateEnd(&stream);
- return buf;
-}
-
-struct delta_info {
- unsigned char base_sha1[20];
- unsigned long size;
- void *delta;
- struct delta_info *next;
-};
-
-static struct delta_info *delta_list;
-
-static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
-{
- struct delta_info *info = xmalloc(sizeof(*info));
-
- memcpy(info->base_sha1, base_sha1, 20);
- info->size = size;
- info->delta = delta;
- info->next = delta_list;
- delta_list = info;
-}
-
-static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
-
-static void write_object(void *buf, unsigned long size, const char *type)
-{
- unsigned char sha1[20];
- if (write_sha1_file(buf, size, type, sha1) < 0)
- die("failed to write object");
- added_object(sha1, type, buf, size);
-}
-
-static int resolve_delta(const char *type,
- void *base, unsigned long base_size,
- void *delta, unsigned long delta_size)
-{
- void *result;
- unsigned long result_size;
-
- result = patch_delta(base, base_size,
- delta, delta_size,
- &result_size);
- if (!result)
- die("failed to apply delta");
- free(delta);
- write_object(result, result_size, type);
- free(result);
- return 0;
-}
-
-static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
-{
- struct delta_info **p = &delta_list;
- struct delta_info *info;
-
- while ((info = *p) != NULL) {
- if (!memcmp(info->base_sha1, sha1, 20)) {
- *p = info->next;
- p = &delta_list;
- resolve_delta(type, data, size, info->delta, info->size);
- free(info);
- continue;
- }
- p = &info->next;
- }
-}
-
-static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
-{
- void *buf = get_data(size);
- const char *type;
-
- switch (kind) {
- case OBJ_COMMIT: type = commit_type; break;
- case OBJ_TREE: type = tree_type; break;
- case OBJ_BLOB: type = blob_type; break;
- case OBJ_TAG: type = tag_type; break;
- default: die("bad type %d", kind);
- }
- if (!dry_run)
- write_object(buf, size, type);
- free(buf);
- return 0;
-}
-
-static int unpack_delta_entry(unsigned long delta_size)
-{
- void *delta_data, *base;
- unsigned long base_size;
- char type[20];
- unsigned char base_sha1[20];
- int result;
-
- memcpy(base_sha1, fill(20), 20);
- use(20);
-
- delta_data = get_data(delta_size);
- if (dry_run) {
- free(delta_data);
- return 0;
- }
-
- if (!has_sha1_file(base_sha1)) {
- add_delta_to_list(base_sha1, delta_data, delta_size);
- return 0;
- }
- base = read_sha1_file(base_sha1, type, &base_size);
- if (!base)
- die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
- result = resolve_delta(type, base, base_size, delta_data, delta_size);
- free(base);
- return result;
-}
-
-static void unpack_one(unsigned nr, unsigned total)
-{
- unsigned shift;
- unsigned char *pack, c;
- unsigned long size;
- enum object_type type;
-
- pack = fill(1);
- c = *pack;
- use(1);
- type = (c >> 4) & 7;
- size = (c & 15);
- shift = 4;
- while (c & 0x80) {
- pack = fill(1);
- c = *pack++;
- use(1);
- size += (c & 0x7f) << shift;
- shift += 7;
- }
- if (!quiet) {
- static unsigned long last_sec;
- static unsigned last_percent;
- struct timeval now;
- unsigned percentage = (nr * 100) / total;
-
- gettimeofday(&now, NULL);
- if (percentage != last_percent || now.tv_sec != last_sec) {
- last_sec = now.tv_sec;
- last_percent = percentage;
- fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
- }
- }
- switch (type) {
- case OBJ_COMMIT:
- case OBJ_TREE:
- case OBJ_BLOB:
- case OBJ_TAG:
- unpack_non_delta_entry(type, size);
- return;
- case OBJ_DELTA:
- unpack_delta_entry(size);
- return;
- default:
- die("bad object type %d", type);
- }
-}
-
-static void unpack_all(void)
-{
- int i;
- struct pack_header *hdr = fill(sizeof(struct pack_header));
- unsigned nr_objects = ntohl(hdr->hdr_entries);
-
- if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
- die("bad pack file");
- if (!pack_version_ok(hdr->hdr_version))
- die("unknown pack file version %d", ntohl(hdr->hdr_version));
- fprintf(stderr, "Unpacking %d objects\n", nr_objects);
-
- use(sizeof(struct pack_header));
- for (i = 0; i < nr_objects; i++)
- unpack_one(i+1, nr_objects);
- if (delta_list)
- die("unresolved deltas left after unpacking");
-}
-
-int main(int argc, char **argv)
-{
- int i;
- unsigned char sha1[20];
-
- setup_git_directory();
-
- quiet = !isatty(2);
-
- for (i = 1 ; i < argc; i++) {
- const char *arg = argv[i];
-
- if (*arg == '-') {
- if (!strcmp(arg, "-n")) {
- dry_run = 1;
- continue;
- }
- if (!strcmp(arg, "-q")) {
- quiet = 1;
- continue;
- }
- usage(unpack_usage);
- }
-
- /* We don't take any non-flag arguments now.. Maybe some day */
- usage(unpack_usage);
- }
- SHA1_Init(&ctx);
- unpack_all();
- SHA1_Update(&ctx, buffer, offset);
- SHA1_Final(sha1, &ctx);
- if (memcmp(fill(20), sha1, 20))
- die("final sha1 did not match");
- use(20);
-
- /* Write the last part of the buffer to stdout */
- while (len) {
- int ret = xwrite(1, buffer + offset, len);
- if (ret <= 0)
- break;
- len -= ret;
- offset += ret;
- }
-
- /* All done */
- if (!quiet)
- fprintf(stderr, "\n");
- return 0;
-}