diff options
46 files changed, 1880 insertions, 447 deletions
diff --git a/.gitignore b/.gitignore index ec4c0dc3df..afd0876218 100644 --- a/.gitignore +++ b/.gitignore @@ -125,6 +125,7 @@ git-write-tree git-core-*/?* test-date test-delta +test-dump-cache-tree common-cmds.h *.tar.gz *.dsc diff --git a/Documentation/config.txt b/Documentation/config.txt index d1a4bec0d4..e178ee2de1 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -70,6 +70,14 @@ core.preferSymlinkRefs:: This is sometimes needed to work with old scripts that expect HEAD to be a symbolic link. +core.logAllRefUpdates:: + If true, `git-update-ref` will append a line to + "$GIT_DIR/logs/<ref>" listing the new SHA1 and the date/time + of the update. If the file does not exist it will be + created automatically. This information can be used to + determine what commit was the tip of a branch "2 days ago". + This value is false by default (no logging). + core.repositoryFormatVersion:: Internal variable identifying the repository format and layout version. diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt index 0b37e2bfc8..d43ef1dec4 100644 --- a/Documentation/git-branch.txt +++ b/Documentation/git-branch.txt @@ -9,7 +9,7 @@ SYNOPSIS -------- [verse] 'git-branch' [-r] -'git-branch' [-f] <branchname> [<start-point>] +'git-branch' [-l] [-f] <branchname> [<start-point>] 'git-branch' (-d | -D) <branchname>... DESCRIPTION @@ -23,7 +23,8 @@ If no <start-point> is given, the branch will be created with a head equal to that of the currently checked out branch. With a `-d` or `-D` option, `<branchname>` will be deleted. You may -specify more than one branch for deletion. +specify more than one branch for deletion. If the branch currently +has a ref log then the ref log will also be deleted. OPTIONS @@ -34,6 +35,11 @@ OPTIONS -D:: Delete a branch irrespective of its index status. +-l:: + Create the branch's ref log. This activates recording of + all changes to made the branch ref, enabling use of date + based sha1 expressions such as "<branchname>@{yesterday}". + -f:: Force the creation of a new branch even if it means deleting a branch that already exists with the same name. diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index d82efc00d4..fbdbadc74f 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -8,7 +8,7 @@ git-checkout - Checkout and switch to a branch SYNOPSIS -------- [verse] -'git-checkout' [-f] [-b <new_branch>] [-m] [<branch>] +'git-checkout' [-f] [-b <new_branch> [-l]] [-m] [<branch>] 'git-checkout' [-m] [<branch>] <paths>... DESCRIPTION @@ -40,6 +40,11 @@ OPTIONS by gitlink:git-check-ref-format[1]. Some of these checks may restrict the characters allowed in a branch name. +-l:: + Create the new branch's ref log. This activates recording of + all changes to made the branch ref, enabling use of date + based sha1 expressions such as "<branchname>@{yesterday}". + -m:: If you have local modifications to one or more files that are different between the current branch and the branch to diff --git a/Documentation/git-read-tree.txt b/Documentation/git-read-tree.txt index 844cfda8d2..1f21d95684 100644 --- a/Documentation/git-read-tree.txt +++ b/Documentation/git-read-tree.txt @@ -8,7 +8,7 @@ git-read-tree - Reads tree information into the index SYNOPSIS -------- -'git-read-tree' (<tree-ish> | [[-m [--aggressive]| --reset] [-u | -i]] <tree-ish1> [<tree-ish2> [<tree-ish3>]]) +'git-read-tree' (<tree-ish> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <tree-ish1> [<tree-ish2> [<tree-ish3>]]) DESCRIPTION @@ -63,6 +63,15 @@ OPTIONS * when both sides adds a path identically. The resolution is to add that path. +--prefix=<prefix>/:: + Keep the current index contents, and read the contents + of named tree-ish under directory at `<prefix>`. The + original index file cannot have anything at the path + `<prefix>` itself, and have nothing in `<prefix>/` + directory. Note that the `<prefix>/` value must end + with a slash. + + <tree-ish#>:: The id of the tree object(s) to be read/merged. diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt index ab896fcf83..b894694367 100644 --- a/Documentation/git-rev-parse.txt +++ b/Documentation/git-rev-parse.txt @@ -124,6 +124,13 @@ syntax. happen to have both heads/master and tags/master, you can explicitly say 'heads/master' to tell git which one you mean. +* A suffix '@' followed by a date specification enclosed in a brace + pair (e.g. '\{yesterday\}', '\{1 month 2 weeks 3 days 1 hour 1 + second ago\}' or '\{1979-02-26 18:30:00\}') to specify the value + of the ref at a prior point in time. This suffix may only be + used immediately following a ref name and the ref must have an + existing log ($GIT_DIR/logs/<ref>). + * A suffix '{caret}' to a revision parameter means the first parent of that commit object. '{caret}<n>' means the <n>th parent (i.e. 'rev{caret}' diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt index 475237f19e..e062030e91 100644 --- a/Documentation/git-update-ref.txt +++ b/Documentation/git-update-ref.txt @@ -7,7 +7,7 @@ git-update-ref - update the object name stored in a ref safely SYNOPSIS -------- -'git-update-ref' <ref> <newvalue> [<oldvalue>] +'git-update-ref' [-m <reason>] <ref> <newvalue> [<oldvalue>] DESCRIPTION ----------- @@ -49,6 +49,32 @@ for reading but not for writing (so we'll never write through a ref symlink to some other tree, if you have copied a whole archive by creating a symlink tree). +Logging Updates +--------------- +If config parameter "core.logAllRefUpdates" is true or the file +"$GIT_DIR/logs/<ref>" exists then `git-update-ref` will append +a line to the log file "$GIT_DIR/logs/<ref>" (dereferencing all +symbolic refs before creating the log name) describing the change +in ref value. Log lines are formatted as: + + . oldsha1 SP newsha1 SP committer LF ++ +Where "oldsha1" is the 40 character hexadecimal value previously +stored in <ref>, "newsha1" is the 40 character hexadecimal value of +<newvalue> and "committer" is the committer's name, email address +and date in the standard GIT committer ident format. + +Optionally with -m: + + . oldsha1 SP newsha1 SP committer TAB message LF ++ +Where all fields are as described above and "message" is the +value supplied to the -m option. + +An update will fail (without changing <ref>) if the current user is +unable to create a new log file, append to the existing log file +or does not have committer information available. + Author ------ Written by Linus Torvalds <torvalds@osdl.org>. diff --git a/Documentation/git-write-tree.txt b/Documentation/git-write-tree.txt index 77e12cb949..c85fa89c30 100644 --- a/Documentation/git-write-tree.txt +++ b/Documentation/git-write-tree.txt @@ -8,7 +8,7 @@ git-write-tree - Creates a tree object from the current index SYNOPSIS -------- -'git-write-tree' [--missing-ok] +'git-write-tree' [--missing-ok] [--prefix=<prefix>/] DESCRIPTION ----------- @@ -30,6 +30,12 @@ OPTIONS directory exist in the object database. This option disables this check. +--prefix=<prefix>/:: + Writes a tree object that represents a subdirectory + `<prefix>`. This can be used to write the tree object + for a subproject that is in the named subdirectory. + + Author ------ Written by Linus Torvalds <torvalds@osdl.org> diff --git a/Documentation/repository-layout.txt b/Documentation/repository-layout.txt index 98fbe7db52..b52dfdc308 100644 --- a/Documentation/repository-layout.txt +++ b/Documentation/repository-layout.txt @@ -128,3 +128,14 @@ remotes:: Stores shorthands to be used to give URL and default refnames to interact with remote repository to `git fetch`, `git pull` and `git push` commands. + +logs:: + Records of changes made to refs are stored in this + directory. See the documentation on git-update-ref + for more information. + +logs/refs/heads/`name`:: + Records all changes made to the branch tip named `name`. + +logs/refs/tags/`name`:: + Records all changes made to the tag named `name`. @@ -210,7 +210,7 @@ DIFF_OBJS = \ diffcore-delta.o log-tree.o LIB_OBJS = \ - blob.o commit.o connect.o csum-file.o base85.o \ + blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \ date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \ object.o pack-check.o patch-delta.o path.o pkt-line.o \ quote.o read-cache.o refs.o run-command.o dir.o \ @@ -626,6 +626,9 @@ test-date$X: test-date.c date.o ctype.o test-delta$X: test-delta.c diff-delta.o patch-delta.o $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $^ +test-dump-cache-tree$X: dump-cache-tree.o $(GITLIBS) + $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) + check: for i in *.c; do sparse $(ALL_CFLAGS) $(SPARSE_FLAGS) $$i || exit; done diff --git a/builtin-add.c b/builtin-add.c index 6166f66bce..02fe38b0c4 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -8,6 +8,7 @@ #include "cache.h" #include "builtin.h" #include "dir.h" +#include "cache-tree.h" static const char builtin_add_usage[] = "git-add [-n] [-v] <filepattern>..."; @@ -117,6 +118,7 @@ static int add_file_to_index(const char *path, int verbose) die("unable to add %s to index",path); if (verbose) printf("add '%s'\n", path); + cache_tree_invalidate_path(active_cache_tree, path); return 0; } diff --git a/builtin-apply.c b/builtin-apply.c index 4056b9d67b..6a4fb9663d 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -8,6 +8,7 @@ */ #include <fnmatch.h> #include "cache.h" +#include "cache-tree.h" #include "quote.h" #include "blob.h" #include "delta.h" @@ -1334,6 +1335,7 @@ static int apply_line(char *output, const char *patch, int plen) static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag) { + int match_beginning, match_end; char *buf = desc->buffer; const char *patch = frag->patch; int offset, size = frag->size; @@ -1396,10 +1398,22 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag) newlines = new; leading = frag->leading; trailing = frag->trailing; + + /* + * If we don't have any leading/trailing data in the patch, + * we want it to match at the beginning/end of the file. + */ + match_beginning = !leading && (frag->oldpos == 1); + match_end = !trailing; + lines = 0; pos = frag->newpos; for (;;) { offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines); + if (match_end && offset + oldsize != desc->size) + offset = -1; + if (match_beginning && offset) + offset = -1; if (offset >= 0) { int diff = newsize - oldsize; unsigned long size = desc->size + diff; @@ -1429,6 +1443,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag) /* Am I at my context limits? */ if ((leading <= p_context) && (trailing <= p_context)) break; + if (match_beginning || match_end) { + match_beginning = match_end = 0; + continue; + } /* Reduce the number of context lines * Reduce both leading and trailing if they are equal * otherwise just reduce the larger context. @@ -1918,6 +1936,7 @@ static void remove_file(struct patch *patch) if (write_index) { if (remove_file_from_cache(patch->old_name) < 0) die("unable to remove %s from index", patch->old_name); + cache_tree_invalidate_path(active_cache_tree, patch->old_name); } if (!cached) unlink(patch->old_name); @@ -2019,8 +2038,9 @@ static void create_file(struct patch *patch) if (!mode) mode = S_IFREG | 0644; - create_one_file(path, mode, buf, size); + create_one_file(path, mode, buf, size); add_index_file(path, mode, buf, size); + cache_tree_invalidate_path(active_cache_tree, path); } static void write_out_one_result(struct patch *patch) diff --git a/builtin-read-tree.c b/builtin-read-tree.c index ec40d013c4..629d151639 100644 --- a/builtin-read-tree.c +++ b/builtin-read-tree.c @@ -9,6 +9,7 @@ #include "object.h" #include "tree.h" +#include "cache-tree.h" #include <sys/time.h> #include <signal.h> #include "builtin.h" @@ -22,6 +23,7 @@ static int trivial_merges_only = 0; static int aggressive = 0; static int verbose_update = 0; static volatile int progress_update = 0; +static const char *prefix = NULL; static int head_idx = -1; static int merge_size = 0; @@ -370,7 +372,8 @@ static int unpack_trees(merge_fn_t fn) posns[i] = ((struct tree *) posn->item)->entries; posn = posn->next; } - if (unpack_trees_rec(posns, len, "", fn, &indpos)) + if (unpack_trees_rec(posns, len, prefix ? prefix : "", + fn, &indpos)) return -1; } @@ -427,6 +430,12 @@ static void verify_uptodate(struct cache_entry *ce) die("Entry '%s' not uptodate. Cannot merge.", ce->name); } +static void invalidate_ce_path(struct cache_entry *ce) +{ + if (ce) + cache_tree_invalidate_path(active_cache_tree, ce->name); +} + /* * We do not want to remove or overwrite a working tree file that * is not tracked. @@ -457,10 +466,13 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old) *merge = *old; } else { verify_uptodate(old); + invalidate_ce_path(old); } } - else + else { verify_absent(merge->name, "overwritten"); + invalidate_ce_path(merge); + } merge->ce_flags &= ~htons(CE_STAGEMASK); add_cache_entry(merge, ADD_CACHE_OK_TO_ADD); @@ -475,6 +487,7 @@ static int deleted_entry(struct cache_entry *ce, struct cache_entry *old) verify_absent(ce->name, "removed"); ce->ce_mode = 0; add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); + invalidate_ce_path(ce); return 1; } @@ -710,6 +723,28 @@ static int twoway_merge(struct cache_entry **src) } /* + * Bind merge. + * + * Keep the index entries at stage0, collapse stage1 but make sure + * stage0 does not have anything there. + */ +static int bind_merge(struct cache_entry **src) +{ + struct cache_entry *old = src[0]; + struct cache_entry *a = src[1]; + + if (merge_size != 1) + return error("Cannot do a bind merge of %d trees\n", + merge_size); + if (a && old) + die("Entry '%s' overlaps. Cannot bind.", a->name); + if (!a) + return keep_entry(old); + else + return merged_entry(a, NULL); +} + +/* * One-way merge. * * The rule is: @@ -724,8 +759,10 @@ static int oneway_merge(struct cache_entry **src) return error("Cannot do a oneway merge of %d trees", merge_size); - if (!a) + if (!a) { + invalidate_ce_path(old); return deleted_entry(old, old); + } if (old && same(old, a)) { if (reset) { struct stat st; @@ -750,6 +787,7 @@ static int read_cache_unmerged(void) struct cache_entry *ce = active_cache[i]; if (ce_stage(ce)) { deleted++; + invalidate_ce_path(ce); continue; } if (deleted) @@ -760,7 +798,40 @@ static int read_cache_unmerged(void) return deleted; } -static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])"; +static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree) +{ + struct tree_entry_list *ent; + int cnt; + + memcpy(it->sha1, tree->object.sha1, 20); + for (cnt = 0, ent = tree->entries; ent; ent = ent->next) { + if (!ent->directory) + cnt++; + else { + struct cache_tree_sub *sub; + struct tree *subtree = (struct tree *)ent->item.tree; + if (!subtree->object.parsed) + parse_tree(subtree); + sub = cache_tree_sub(it, ent->name); + sub->cache_tree = cache_tree(); + prime_cache_tree_rec(sub->cache_tree, subtree); + cnt += sub->cache_tree->entry_count; + } + } + it->entry_count = cnt; +} + +static void prime_cache_tree(void) +{ + struct tree *tree = (struct tree *)trees->item; + if (!tree) + return; + active_cache_tree = cache_tree(); + prime_cache_tree_rec(active_cache_tree, tree); + +} + +static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])"; static struct cache_file cache_file; @@ -805,9 +876,24 @@ int cmd_read_tree(int argc, const char **argv, char **envp) continue; } + /* "--prefix=<subdirectory>/" means keep the current index + * entries and put the entries from the tree under the + * given subdirectory. + */ + if (!strncmp(arg, "--prefix=", 9)) { + if (stage || merge || prefix) + usage(read_tree_usage); + prefix = arg + 9; + merge = 1; + stage = 1; + if (read_cache_unmerged()) + die("you need to resolve your current index first"); + continue; + } + /* This differs from "-m" in that we'll silently ignore unmerged entries */ if (!strcmp(arg, "--reset")) { - if (stage || merge) + if (stage || merge || prefix) usage(read_tree_usage); reset = 1; merge = 1; @@ -828,7 +914,7 @@ int cmd_read_tree(int argc, const char **argv, char **envp) /* "-m" stands for "merge", meaning we start in stage 1 */ if (!strcmp(arg, "-m")) { - if (stage || merge) + if (stage || merge || prefix) usage(read_tree_usage); if (read_cache_unmerged()) die("you need to resolve your current index first"); @@ -850,21 +936,39 @@ int cmd_read_tree(int argc, const char **argv, char **envp) if ((update||index_only) && !merge) usage(read_tree_usage); + if (prefix) { + int pfxlen = strlen(prefix); + int pos; + if (prefix[pfxlen-1] != '/') + die("prefix must end with /"); + if (stage != 2) + die("binding merge takes only one tree"); + pos = cache_name_pos(prefix, pfxlen); + if (0 <= pos) + die("corrupt index file"); + pos = -pos-1; + if (pos < active_nr && + !strncmp(active_cache[pos]->name, prefix, pfxlen)) + die("subdirectory '%s' already exists.", prefix); + pos = cache_name_pos(prefix, pfxlen-1); + if (0 <= pos) + die("file '%.*s' already exists.", pfxlen-1, prefix); + } + if (merge) { if (stage < 2) die("just how do you expect me to merge %d trees?", stage-1); switch (stage - 1) { case 1: - fn = oneway_merge; + fn = prefix ? bind_merge : oneway_merge; break; case 2: fn = twoway_merge; break; case 3: - fn = threeway_merge; - break; default: fn = threeway_merge; + cache_tree_free(&active_cache_tree); break; } @@ -875,6 +979,18 @@ int cmd_read_tree(int argc, const char **argv, char **envp) } unpack_trees(fn); + + /* + * When reading only one tree (either the most basic form, + * "-m ent" or "--reset ent" form), we can obtain a fully + * valid cache-tree because the index must match exactly + * what came from the tree. + */ + if (trees && trees->item && (!merge || (stage == 2))) { + cache_tree_free(&active_cache_tree); + prime_cache_tree(); + } + if (write_cache(newfd, active_cache, active_nr) || commit_index_file(&cache_file)) die("unable to write new index file"); diff --git a/builtin-rm.c b/builtin-rm.c index 9014c61556..e7793a20f5 100644 --- a/builtin-rm.c +++ b/builtin-rm.c @@ -6,6 +6,7 @@ #include "cache.h" #include "builtin.h" #include "dir.h" +#include "cache-tree.h" static const char builtin_rm_usage[] = "git-rm [-n] [-v] [-f] <filepattern>..."; @@ -117,6 +118,7 @@ int cmd_rm(int argc, const char **argv, char **envp) if (remove_file_from_cache(path)) die("git rm: unable to remove %s", path); + cache_tree_invalidate_path(active_cache_tree, path); } /* diff --git a/cache-tree.c b/cache-tree.c new file mode 100644 index 0000000000..d9f7e1e3dd --- /dev/null +++ b/cache-tree.c @@ -0,0 +1,557 @@ +#include "cache.h" +#include "tree.h" +#include "cache-tree.h" + +#define DEBUG 0 + +struct cache_tree *cache_tree(void) +{ + struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree)); + it->entry_count = -1; + return it; +} + +void cache_tree_free(struct cache_tree **it_p) +{ + int i; + struct cache_tree *it = *it_p; + + if (!it) + return; + for (i = 0; i < it->subtree_nr; i++) + if (it->down[i]) + cache_tree_free(&it->down[i]->cache_tree); + free(it->down); + free(it); + *it_p = NULL; +} + +static int subtree_name_cmp(const char *one, int onelen, + const char *two, int twolen) +{ + if (onelen < twolen) + return -1; + if (twolen < onelen) + return 1; + return memcmp(one, two, onelen); +} + +static int subtree_pos(struct cache_tree *it, const char *path, int pathlen) +{ + struct cache_tree_sub **down = it->down; + int lo, hi; + lo = 0; + hi = it->subtree_nr; + while (lo < hi) { + int mi = (lo + hi) / 2; + struct cache_tree_sub *mdl = down[mi]; + int cmp = subtree_name_cmp(path, pathlen, + mdl->name, mdl->namelen); + if (!cmp) + return mi; + if (cmp < 0) + hi = mi; + else + lo = mi + 1; + } + return -lo-1; +} + +static struct cache_tree_sub *find_subtree(struct cache_tree *it, + const char *path, + int pathlen, + int create) +{ + struct cache_tree_sub *down; + int pos = subtree_pos(it, path, pathlen); + if (0 <= pos) + return it->down[pos]; + if (!create) + return NULL; + + pos = -pos-1; + if (it->subtree_alloc <= it->subtree_nr) { + it->subtree_alloc = alloc_nr(it->subtree_alloc); + it->down = xrealloc(it->down, it->subtree_alloc * + sizeof(*it->down)); + } + it->subtree_nr++; + + down = xmalloc(sizeof(*down) + pathlen + 1); + down->cache_tree = NULL; + down->namelen = pathlen; + memcpy(down->name, path, pathlen); + down->name[pathlen] = 0; + + if (pos < it->subtree_nr) + memmove(it->down + pos + 1, + it->down + pos, + sizeof(down) * (it->subtree_nr - pos - 1)); + it->down[pos] = down; + return down; +} + +struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path) +{ + int pathlen = strlen(path); + return find_subtree(it, path, pathlen, 1); +} + +void cache_tree_invalidate_path(struct cache_tree *it, const char *path) +{ + /* a/b/c + * ==> invalidate self + * ==> find "a", have it invalidate "b/c" + * a + * ==> invalidate self + * ==> if "a" exists as a subtree, remove it. + */ + const char *slash; + int namelen; + struct cache_tree_sub *down; + +#if DEBUG + fprintf(stderr, "cache-tree invalidate <%s>\n", path); +#endif + + if (!it) + return; + slash = strchr(path, '/'); + it->entry_count = -1; + if (!slash) { + int pos; + namelen = strlen(path); + pos = subtree_pos(it, path, namelen); + if (0 <= pos) { + cache_tree_free(&it->down[pos]->cache_tree); + free(it->down[pos]); + /* 0 1 2 3 4 5 + * ^ ^subtree_nr = 6 + * pos + * move 4 and 5 up one place (2 entries) + * 2 = 6 - 3 - 1 = subtree_nr - pos - 1 + */ + memmove(it->down+pos, it->down+pos+1, + sizeof(struct cache_tree_sub *) * + (it->subtree_nr - pos - 1)); + it->subtree_nr--; + } + return; + } + namelen = slash - path; + down = find_subtree(it, path, namelen, 0); + if (down) + cache_tree_invalidate_path(down->cache_tree, slash + 1); +} + +static int verify_cache(struct cache_entry **cache, + int entries) +{ + int i, funny; + + /* Verify that the tree is merged */ + funny = 0; + for (i = 0; i < entries; i++) { + struct cache_entry *ce = cache[i]; + if (ce_stage(ce)) { + if (10 < ++funny) { + fprintf(stderr, "...\n"); + break; + } + fprintf(stderr, "%s: unmerged (%s)\n", + ce->name, sha1_to_hex(ce->sha1)); + } + } + if (funny) + return -1; + + /* Also verify that the cache does not have path and path/file + * at the same time. At this point we know the cache has only + * stage 0 entries. + */ + funny = 0; + for (i = 0; i < entries - 1; i++) { + /* path/file always comes after path because of the way + * the cache is sorted. Also path can appear only once, + * which means conflicting one would immediately follow. + */ + const char *this_name = cache[i]->name; + const char *next_name = cache[i+1]->name; + int this_len = strlen(this_name); + if (this_len < strlen(next_name) && + strncmp(this_name, next_name, this_len) == 0 && + next_name[this_len] == '/') { + if (10 < ++funny) { + fprintf(stderr, "...\n"); + break; + } + fprintf(stderr, "You have both %s and %s\n", + this_name, next_name); + } + } + if (funny) + return -1; + return 0; +} + +static void discard_unused_subtrees(struct cache_tree *it) +{ + struct cache_tree_sub **down = it->down; + int nr = it->subtree_nr; + int dst, src; + for (dst = src = 0; src < nr; src++) { + struct cache_tree_sub *s = down[src]; + if (s->used) + down[dst++] = s; + else { + cache_tree_free(&s->cache_tree); + free(s); + it->subtree_nr--; + } + } +} + +int cache_tree_fully_valid(struct cache_tree *it) +{ + int i; + if (!it) + return 0; + if (it->entry_count < 0 || !has_sha1_file(it->sha1)) + return 0; + for (i = 0; i < it->subtree_nr; i++) { + if (!cache_tree_fully_valid(it->down[i]->cache_tree)) + return 0; + } + return 1; +} + +static int update_one(struct cache_tree *it, + struct cache_entry **cache, + int entries, + const char *base, + int baselen, + int missing_ok, + int dryrun) +{ + unsigned long size, offset; + char *buffer; + int i; + + if (0 <= it->entry_count && has_sha1_file(it->sha1)) + return it->entry_count; + + /* + * We first scan for subtrees and update them; we start by + * marking existing subtrees -- the ones that are unmarked + * should not be in the result. + */ + for (i = 0; i < it->subtree_nr; i++) + it->down[i]->used = 0; + + /* + * Find the subtrees and update them. + */ + for (i = 0; i < entries; i++) { + struct cache_entry *ce = cache[i]; + struct cache_tree_sub *sub; + const char *path, *slash; + int pathlen, sublen, subcnt; + + path = ce->name; + pathlen = ce_namelen(ce); + if (pathlen <= baselen || memcmp(base, path, baselen)) + break; /* at the end of this level */ + + slash = strchr(path + baselen, '/'); + if (!slash) + continue; + /* + * a/bbb/c (base = a/, slash = /c) + * ==> + * path+baselen = bbb/c, sublen = 3 + */ + sublen = slash - (path + baselen); + sub = find_subtree(it, path + baselen, sublen, 1); + if (!sub->cache_tree) + sub->cache_tree = cache_tree(); + subcnt = update_one(sub->cache_tree, + cache + i, entries - i, + path, + baselen + sublen + 1, + missing_ok, + dryrun); + i += subcnt - 1; + sub->used = 1; + } + + discard_unused_subtrees(it); + + /* + * Then write out the tree object for this level. + */ + size = 8192; + buffer = xmalloc(size); + offset = 0; + + for (i = 0; i < entries; i++) { + struct cache_entry *ce = cache[i]; + struct cache_tree_sub *sub; + const char *path, *slash; + int pathlen, entlen; + const unsigned char *sha1; + unsigned mode; + + path = ce->name; + pathlen = ce_namelen(ce); + if (pathlen <= baselen || memcmp(base, path, baselen)) + break; /* at the end of this level */ + + slash = strchr(path + baselen, '/'); + if (slash) { + entlen = slash - (path + baselen); + sub = find_subtree(it, path + baselen, entlen, 0); + if (!sub) + die("cache-tree.c: '%.*s' in '%s' not found", + entlen, path + baselen, path); + i += sub->cache_tree->entry_count - 1; + sha1 = sub->cache_tree->sha1; + mode = S_IFDIR; + } + else { + sha1 = ce->sha1; + mode = ntohl(ce->ce_mode); + entlen = pathlen - baselen; + } + if (!missing_ok && !has_sha1_file(sha1)) + return error("invalid object %s", sha1_to_hex(sha1)); + + if (!ce->ce_mode) + continue; /* entry being removed */ + + if (size < offset + entlen + 100) { + size = alloc_nr(offset + entlen + 100); + buffer = xrealloc(buffer, size); + } + offset += sprintf(buffer + offset, + "%o %.*s", mode, entlen, path + baselen); + buffer[offset++] = 0; + memcpy(buffer + offset, sha1, 20); + offset += 20; + +#if DEBUG + fprintf(stderr, "cache-tree update-one %o %.*s\n", + mode, entlen, path + baselen); +#endif + } + + if (dryrun) { + unsigned char hdr[200]; + int hdrlen; + write_sha1_file_prepare(buffer, offset, tree_type, it->sha1, + hdr, &hdrlen); + } + else + write_sha1_file(buffer, offset, tree_type, it->sha1); + free(buffer); + it->entry_count = i; +#if DEBUG + fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n", + it->entry_count, it->subtree_nr, + sha1_to_hex(it->sha1)); +#endif + return i; +} + +int cache_tree_update(struct cache_tree *it, + struct cache_entry **cache, + int entries, + int missing_ok, + int dryrun) +{ + int i; + i = verify_cache(cache, entries); + if (i) + return i; + i = update_one(it, cache, entries, "", 0, missing_ok, dryrun); + if (i < 0) + return i; + return 0; +} + +static void *write_one(struct cache_tree *it, + char *path, + int pathlen, + char *buffer, + unsigned long *size, + unsigned long *offset) +{ + int i; + + /* One "cache-tree" entry consists of the following: + * path (NUL terminated) + * entry_count, subtree_nr ("%d %d\n") + * tree-sha1 (missing if invalid) + * subtree_nr "cache-tree" entries for subtrees. + */ + if (*size < *offset + pathlen + 100) { + *size = alloc_nr(*offset + pathlen + 100); + buffer = xrealloc(buffer, *size); + } + *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n", + pathlen, path, 0, + it->entry_count, it->subtree_nr); + +#if DEBUG + if (0 <= it->entry_count) + fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n", + pathlen, path, it->entry_count, it->subtree_nr, + sha1_to_hex(it->sha1)); + else + fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n", + pathlen, path, it->subtree_nr); +#endif + + if (0 <= it->entry_count) { + memcpy(buffer + *offset, it->sha1, 20); + *offset += 20; + } + for (i = 0; i < it->subtree_nr; i++) { + struct cache_tree_sub *down = it->down[i]; + if (i) { + struct cache_tree_sub *prev = it->down[i-1]; + if (subtree_name_cmp(down->name, down->namelen, + prev->name, prev->namelen) <= 0) + die("fatal - unsorted cache subtree"); + } + buffer = write_one(down->cache_tree, down->name, down->namelen, + buffer, size, offset); + } + return buffer; +} + +void *cache_tree_write(struct cache_tree *root, unsigned long *size_p) +{ + char path[PATH_MAX]; + unsigned long size = 8192; + char *buffer = xmalloc(size); + + *size_p = 0; + path[0] = 0; + return write_one(root, path, 0, buffer, &size, size_p); +} + +static struct cache_tree *read_one(const char **buffer, unsigned long *size_p) +{ + const char *buf = *buffer; + unsigned long size = *size_p; + const char *cp; + char *ep; + struct cache_tree *it; + int i, subtree_nr; + + it = NULL; + /* skip name, but make sure name exists */ + while (size && *buf) { + size--; + buf++; + } + if (!size) + goto free_return; + buf++; size--; + it = cache_tree(); + + cp = buf; + it->entry_count = strtol(cp, &ep, 10); + if (cp == ep) + goto free_return; + cp = ep; + subtree_nr = strtol(cp, &ep, 10); + if (cp == ep) + goto free_return; + while (size && *buf && *buf != '\n') { + size--; + buf++; + } + if (!size) + goto free_return; + buf++; size--; + if (0 <= it->entry_count) { + if (size < 20) + goto free_return; + memcpy(it->sha1, buf, 20); + buf += 20; + size -= 20; + } + +#if DEBUG + if (0 <= it->entry_count) + fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n", + *buffer, it->entry_count, subtree_nr, + sha1_to_hex(it->sha1)); + else + fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n", + *buffer, subtree_nr); +#endif + + /* + * Just a heuristic -- we do not add directories that often but + * we do not want to have to extend it immediately when we do, + * hence +2. + */ + it->subtree_alloc = subtree_nr + 2; + it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *)); + for (i = 0; i < subtree_nr; i++) { + /* read each subtree */ + struct cache_tree *sub; + struct cache_tree_sub *subtree; + const char *name = buf; + + sub = read_one(&buf, &size); + if (!sub) + goto free_return; + subtree = cache_tree_sub(it, name); + subtree->cache_tree = sub; + } + if (subtree_nr != it->subtree_nr) + die("cache-tree: internal error"); + *buffer = buf; + *size_p = size; + return it; + + free_return: + cache_tree_free(&it); + return NULL; +} + +struct cache_tree *cache_tree_read(const char *buffer, unsigned long size) +{ + if (buffer[0]) + return NULL; /* not the whole tree */ + return read_one(&buffer, &size); +} + +struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path) +{ + while (*path) { + const char *slash; + struct cache_tree_sub *sub; + + slash = strchr(path, '/'); + if (!slash) + slash = path + strlen(path); + /* between path and slash is the name of the + * subtree to look for. + */ + sub = find_subtree(it, path, slash - path, 0); + if (!sub) + return NULL; + it = sub->cache_tree; + if (slash) + while (*slash && *slash == '/') + slash++; + if (!slash || !*slash) + return it; /* prefix ended with slashes */ + path = slash; + } + return it; +} diff --git a/cache-tree.h b/cache-tree.h new file mode 100644 index 0000000000..119407e3a1 --- /dev/null +++ b/cache-tree.h @@ -0,0 +1,33 @@ +#ifndef CACHE_TREE_H +#define CACHE_TREE_H + +struct cache_tree; +struct cache_tree_sub { + struct cache_tree *cache_tree; + int namelen; + int used; + char name[FLEX_ARRAY]; +}; + +struct cache_tree { + int entry_count; /* negative means "invalid" */ + unsigned char sha1[20]; + int subtree_nr; + int subtree_alloc; + struct cache_tree_sub **down; +}; + +struct cache_tree *cache_tree(void); +void cache_tree_free(struct cache_tree **); +void cache_tree_invalidate_path(struct cache_tree *, const char *); +struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *); + +void *cache_tree_write(struct cache_tree *root, unsigned long *size_p); +struct cache_tree *cache_tree_read(const char *buffer, unsigned long size); + +int cache_tree_fully_valid(struct cache_tree *); +int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int, int); + +struct cache_tree *cache_tree_find(struct cache_tree *, const char *); + +#endif @@ -114,6 +114,7 @@ static inline unsigned int create_ce_mode(unsigned int mode) extern struct cache_entry **active_cache; extern unsigned int active_nr, active_alloc, active_cache_changed; +extern struct cache_tree *active_cache_tree; #define GIT_DIR_ENVIRONMENT "GIT_DIR" #define DEFAULT_GIT_DIR_ENVIRONMENT ".git" @@ -178,6 +179,7 @@ extern void rollback_index_file(struct cache_file *); extern int trust_executable_bit; extern int assume_unchanged; extern int prefer_symlink_refs; +extern int log_all_ref_updates; extern int warn_ambiguous_refs; extern int diff_rename_limit_default; extern int shared_repository; diff --git a/checkout-index.c b/checkout-index.c index cc3a745c14..9876af6fd6 100644 --- a/checkout-index.c +++ b/checkout-index.c @@ -39,6 +39,7 @@ #include "cache.h" #include "strbuf.h" #include "quote.h" +#include "cache-tree.h" #define CHECKOUT_ALL 4 static const char *prefix; @@ -269,6 +269,11 @@ int git_default_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.logallrefupdates")) { + log_all_ref_updates = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "core.warnambiguousrefs")) { warn_ambiguous_refs = git_config_bool(var, value); return 0; diff --git a/dump-cache-tree.c b/dump-cache-tree.c new file mode 100644 index 0000000000..1ccaf51773 --- /dev/null +++ b/dump-cache-tree.c @@ -0,0 +1,64 @@ +#include "cache.h" +#include "tree.h" +#include "cache-tree.h" + + +static void dump_one(struct cache_tree *it, const char *pfx, const char *x) +{ + if (it->entry_count < 0) + printf("%-40s %s%s (%d subtrees)\n", + "invalid", x, pfx, it->subtree_nr); + else + printf("%s %s%s (%d entries, %d subtrees)\n", + sha1_to_hex(it->sha1), x, pfx, + it->entry_count, it->subtree_nr); +} + +static int dump_cache_tree(struct cache_tree *it, + struct cache_tree *ref, + const char *pfx) +{ + int i; + int errs = 0; + + if (!it || !ref) + /* missing in either */ + return 0; + + if (it->entry_count < 0) { + dump_one(it, pfx, ""); + dump_one(ref, pfx, "#(ref) "); + if (it->subtree_nr != ref->subtree_nr) + errs = 1; + } + else { + dump_one(it, pfx, ""); + if (memcmp(it->sha1, ref->sha1, 20) || + ref->entry_count != it->entry_count || + ref->subtree_nr != it->subtree_nr) { + dump_one(ref, pfx, "#(ref) "); + errs = 1; + } + } + + for (i = 0; i < it->subtree_nr; i++) { + char path[PATH_MAX]; + struct cache_tree_sub *down = it->down[i]; + struct cache_tree_sub *rdwn; + + rdwn = cache_tree_sub(ref, down->name); + sprintf(path, "%s%.*s/", pfx, down->namelen, down->name); + if (dump_cache_tree(down->cache_tree, rdwn->cache_tree, path)) + errs = 1; + } + return errs; +} + +int main(int ac, char **av) +{ + struct cache_tree *another = cache_tree(); + if (read_cache() < 0) + die("unable to read index file"); + cache_tree_update(another, active_cache, active_nr, 0, 1); + return dump_cache_tree(active_cache_tree, another, ""); +} diff --git a/environment.c b/environment.c index 444c99ed6e..2e79eab18d 100644 --- a/environment.c +++ b/environment.c @@ -14,6 +14,7 @@ char git_default_name[MAX_GITNAME]; int trust_executable_bit = 1; int assume_unchanged = 0; int prefer_symlink_refs = 0; +int log_all_ref_updates = 0; int warn_ambiguous_refs = 1; int repository_format_version = 0; char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8"; diff --git a/fetch-pack.c b/fetch-pack.c index 8daa93d024..8371348556 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -18,6 +18,12 @@ static const char *exec = "git-upload-pack"; #define SEEN (1U << 3) #define POPPED (1U << 4) +/* + * After sending this many "have"s if we do not get any new ACK , we + * give up traversing our history. + */ +#define MAX_IN_VAIN 256 + static struct commit_list *rev_list = NULL; static int non_common_revs = 0, multi_ack = 0, use_thin_pack = 0; @@ -134,6 +140,8 @@ static int find_common(int fd[2], unsigned char *result_sha1, int fetching; int count = 0, flushes = 0, retval; const unsigned char *sha1; + unsigned in_vain = 0; + int got_continue = 0; for_each_ref(rev_list_insert_ref); @@ -172,6 +180,7 @@ static int find_common(int fd[2], unsigned char *result_sha1, packet_write(fd[1], "have %s\n", sha1_to_hex(sha1)); if (verbose) fprintf(stderr, "have %s\n", sha1_to_hex(sha1)); + in_vain++; if (!(31 & ++count)) { int ack; @@ -200,9 +209,16 @@ static int find_common(int fd[2], unsigned char *result_sha1, lookup_commit(result_sha1); mark_common(commit, 0, 1); retval = 0; + in_vain = 0; + got_continue = 1; } } while (ack); flushes--; + if (got_continue && MAX_IN_VAIN < in_vain) { + if (verbose) + fprintf(stderr, "giving up\n"); + break; /* give up */ + } } } done: @@ -8,6 +8,7 @@ #include "refs.h" const char *write_ref = NULL; +const char *write_ref_log_details = NULL; int get_tree = 0; int get_history = 0; @@ -202,23 +203,48 @@ static int mark_complete(const char *path, const unsigned char *sha1) int pull(char *target) { + struct ref_lock *lock; unsigned char sha1[20]; + char *msg; + int ret; save_commit_buffer = 0; track_object_refs = 0; + if (write_ref) { + lock = lock_ref_sha1(write_ref, NULL, 0); + if (!lock) { + error("Can't lock ref %s", write_ref); + return -1; + } + } if (!get_recover) for_each_ref(mark_complete); - if (interpret_target(target, sha1)) - return error("Could not interpret %s as something to pull", - target); - if (process(lookup_unknown_object(sha1))) + if (interpret_target(target, sha1)) { + error("Could not interpret %s as something to pull", target); + unlock_ref(lock); return -1; - if (loop()) + } + if (process(lookup_unknown_object(sha1))) { + unlock_ref(lock); return -1; - - if (write_ref) - write_ref_sha1_unlocked(write_ref, sha1); + } + if (loop()) { + unlock_ref(lock); + return -1; + } + + if (write_ref) { + if (write_ref_log_details) { + msg = xmalloc(strlen(write_ref_log_details) + 12); + sprintf(msg, "fetch from %s", write_ref_log_details); + } else + msg = NULL; + ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)"); + if (msg) + free(msg); + return ret; + } return 0; } @@ -25,6 +25,9 @@ extern int fetch_ref(char *ref, unsigned char *sha1); /* If set, the ref filename to write the target value to. */ extern const char *write_ref; +/* If set additional text will appear in the ref log. */ +extern const char *write_ref_log_details; + /* Set to fetch the target tree. */ extern int get_tree; diff --git a/fsck-objects.c b/fsck-objects.c index 59b25904cb..1922b6d84c 100644 --- a/fsck-objects.c +++ b/fsck-objects.c @@ -8,6 +8,7 @@ #include "tag.h" #include "refs.h" #include "pack.h" +#include "cache-tree.h" #define REACHABLE 0x0001 @@ -438,6 +439,28 @@ static int fsck_head_link(void) return 0; } +static int fsck_cache_tree(struct cache_tree *it) +{ + int i; + int err = 0; + + if (0 <= it->entry_count) { + struct object *obj = parse_object(it->sha1); + if (!obj) { + error("%s: invalid sha1 pointer in cache-tree", + sha1_to_hex(it->sha1)); + return 1; + } + mark_reachable(obj, REACHABLE); + obj->used = 1; + if (obj->type != tree_type) + err |= objerror(obj, "non-tree in cache-tree"); + } + for (i = 0; i < it->subtree_nr; i++) + err |= fsck_cache_tree(it->down[i]->cache_tree); + return err; +} + int main(int argc, char **argv) { int i, heads; @@ -547,6 +570,8 @@ int main(int argc, char **argv) obj->used = 1; mark_reachable(obj, REACHABLE); } + if (active_cache_tree) + fsck_cache_tree(active_cache_tree); } check_connectivity(); @@ -413,7 +413,7 @@ do parent=$(git-rev-parse --verify HEAD) && commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") && echo Committed: $commit && - git-update-ref HEAD $commit $parent || + git-update-ref -m "am: $SUBJECT" HEAD $commit $parent || stop_here $this if test -x "$GIT_DIR"/hooks/post-applypatch diff --git a/git-applypatch.sh b/git-applypatch.sh index 12cab1e0d4..e4b09472e1 100755 --- a/git-applypatch.sh +++ b/git-applypatch.sh @@ -204,7 +204,7 @@ echo Wrote tree $tree parent=$(git-rev-parse --verify HEAD) && commit=$(git-commit-tree $tree -p $parent <"$final") || exit 1 echo Committed: $commit -git-update-ref HEAD $commit $parent || exit +git-update-ref -m "applypatch: $SUBJECT" HEAD $commit $parent || exit if test -x "$GIT_DIR"/hooks/post-applypatch then diff --git a/git-branch.sh b/git-branch.sh index 134e68cf7f..e0501ec23f 100755 --- a/git-branch.sh +++ b/git-branch.sh @@ -1,6 +1,6 @@ #!/bin/sh -USAGE='[(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r' +USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r' LONG_USAGE='If no arguments, show available branches and mark current branch with a star. If one argument, create a new branch <branchname> based off of current HEAD. If two arguments, create a new branch <branchname> based off of <start-point>.' @@ -42,6 +42,7 @@ If you are sure you want to delete it, run 'git branch -D $branch_name'." esac ;; esac + rm -f "$GIT_DIR/logs/refs/heads/$branch_name" rm -f "$GIT_DIR/refs/heads/$branch_name" echo "Deleted branch $branch_name." done @@ -55,6 +56,7 @@ ls_remote_branches () { } force= +create_log= while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac do case "$1" in @@ -69,6 +71,9 @@ do -f) force="$1" ;; + -l) + create_log="yes" + ;; --) shift break @@ -117,4 +122,9 @@ then die "cannot force-update the current branch." fi fi -git update-ref "refs/heads/$branchname" $rev +if test "$create_log" = 'yes' +then + mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname") + touch "$GIT_DIR/logs/refs/heads/$branchname" +fi +git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev diff --git a/git-checkout.sh b/git-checkout.sh index a11c939c30..564117f006 100755 --- a/git-checkout.sh +++ b/git-checkout.sh @@ -5,10 +5,13 @@ SUBDIRECTORY_OK=Sometimes . git-sh-setup old=$(git-rev-parse HEAD) +old_name=HEAD new= +new_name= force= branch= newbranch= +newbranch_log= merge= while [ "$#" != "0" ]; do arg="$1" @@ -24,6 +27,9 @@ while [ "$#" != "0" ]; do git-check-ref-format "heads/$newbranch" || die "git checkout: we do not like '$newbranch' as a branch name." ;; + "-l") + newbranch_log=1 + ;; "-f") force=1 ;; @@ -44,6 +50,7 @@ while [ "$#" != "0" ]; do exit 1 fi new="$rev" + new_name="$arg^0" if [ -f "$GIT_DIR/refs/heads/$arg" ]; then branch="$arg" fi @@ -51,9 +58,11 @@ while [ "$#" != "0" ]; do then # checking out selected paths from a tree-ish. new="$rev" + new_name="$arg^{tree}" branch= else new= + new_name= branch= set x "$arg" "$@" shift @@ -114,7 +123,7 @@ then cd "$cdup" fi -[ -z "$new" ] && new=$old +[ -z "$new" ] && new=$old && new_name="$old_name" # If we don't have an old branch that we're switching to, # and we don't have a new branch name for the target we @@ -187,9 +196,11 @@ fi # if [ "$?" -eq 0 ]; then if [ "$newbranch" ]; then - leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` && - mkdir -p "$GIT_DIR/$leading" && - echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit + if [ "$newbranch_log" ]; then + mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch") + touch "$GIT_DIR/logs/refs/heads/$newbranch" + fi + git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit branch="$newbranch" fi [ "$branch" ] && diff --git a/git-commit.sh b/git-commit.sh index 6785826fef..0a01a0b96a 100755 --- a/git-commit.sh +++ b/git-commit.sh @@ -687,7 +687,8 @@ then rm -f "$TMP_INDEX" fi && commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) && - git-update-ref HEAD $commit $current && + rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) && + git-update-ref -m "commit: $rlogm" HEAD $commit $current && rm -f -- "$GIT_DIR/MERGE_HEAD" && if test -f "$NEXT_INDEX" then diff --git a/git-reset.sh b/git-reset.sh index 0ee3e3e154..296f3b779b 100755 --- a/git-reset.sh +++ b/git-reset.sh @@ -48,7 +48,7 @@ then else rm -f "$GIT_DIR/ORIG_HEAD" fi -git-update-ref HEAD "$rev" +git-update-ref -m "reset $reset_type $@" HEAD "$rev" case "$reset_type" in --hard ) diff --git a/http-fetch.c b/http-fetch.c index 861644b27e..661c909152 100644 --- a/http-fetch.c +++ b/http-fetch.c @@ -1223,6 +1223,7 @@ int main(int argc, char **argv) int rc = 0; setup_git_directory(); + git_config(git_default_config); while (arg < argc && argv[arg][0] == '-') { if (argv[arg][1] == 't') { @@ -1249,6 +1250,7 @@ int main(int argc, char **argv) } commit_id = argv[arg]; url = argv[arg + 1]; + write_ref_log_details = url; http_init(); @@ -1269,10 +1271,10 @@ int main(int argc, char **argv) if (pull(commit_id)) rc = 1; - curl_slist_free_all(no_pragma_header); - http_cleanup(); + curl_slist_free_all(no_pragma_header); + if (corrupt_object_found) { fprintf(stderr, "Some loose object were found to be corrupt, but they might be just\n" diff --git a/local-fetch.c b/local-fetch.c index fa9e697fd3..ffa4887570 100644 --- a/local-fetch.c +++ b/local-fetch.c @@ -208,6 +208,7 @@ int main(int argc, char **argv) int arg = 1; setup_git_directory(); + git_config(git_default_config); while (arg < argc && argv[arg][0] == '-') { if (argv[arg][1] == 't') @@ -239,6 +240,7 @@ int main(int argc, char **argv) usage(local_pull_usage); commit_id = argv[arg]; path = argv[arg + 1]; + write_ref_log_details = path; if (pull(commit_id)) return 1; diff --git a/mailinfo.c b/mailinfo.c index b27651935d..78d2a15dd0 100644 --- a/mailinfo.c +++ b/mailinfo.c @@ -72,11 +72,14 @@ static int bogus_from(char *line) return 1; } -static int handle_from(char *line) +static int handle_from(char *in_line) { - char *at = strchr(line, '@'); + char line[1000]; + char *at; char *dst; + strcpy(line, in_line); + at = strchr(line, '@'); if (!at) return bogus_from(line); @@ -237,38 +240,46 @@ static int eatspace(char *line) #define SEEN_FROM 01 #define SEEN_DATE 02 #define SEEN_SUBJECT 04 +#define SEEN_BOGUS_UNIX_FROM 010 +#define SEEN_PREFIX 020 /* First lines of body can have From:, Date:, and Subject: */ -static int handle_inbody_header(int *seen, char *line) +static void handle_inbody_header(int *seen, char *line) { + if (!memcmp(">From", line, 5) && isspace(line[5])) { + if (!(*seen & SEEN_BOGUS_UNIX_FROM)) { + *seen |= SEEN_BOGUS_UNIX_FROM; + return; + } + } if (!memcmp("From:", line, 5) && isspace(line[5])) { if (!(*seen & SEEN_FROM) && handle_from(line+6)) { *seen |= SEEN_FROM; - return 1; + return; } } if (!memcmp("Date:", line, 5) && isspace(line[5])) { if (!(*seen & SEEN_DATE)) { handle_date(line+6); *seen |= SEEN_DATE; - return 1; + return; } } if (!memcmp("Subject:", line, 8) && isspace(line[8])) { if (!(*seen & SEEN_SUBJECT)) { handle_subject(line+9); *seen |= SEEN_SUBJECT; - return 1; + return; } } if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) { if (!(*seen & SEEN_SUBJECT)) { handle_subject(line); *seen |= SEEN_SUBJECT; - return 1; + return; } } - return 0; + *seen |= SEEN_PREFIX; } static char *cleanup_subject(char *subject) @@ -324,6 +335,7 @@ static void cleanup_space(char *buf) } } +static void decode_header_bq(char *it); typedef int (*header_fn_t)(char *); struct header_def { const char *name; @@ -331,7 +343,7 @@ struct header_def { int namelen; }; -static void check_header(char *line, int len, struct header_def *header) +static void check_header(char *line, struct header_def *header) { int i; @@ -343,13 +355,17 @@ static void check_header(char *line, int len, struct header_def *header) int len = header[i].namelen; if (!strncasecmp(line, header[i].name, len) && line[len] == ':' && isspace(line[len + 1])) { + /* Unwrap inline B and Q encoding, and optionally + * normalize the meta information to utf8. + */ + decode_header_bq(line + len + 2); header[i].func(line + len + 2); break; } } } -static void check_subheader_line(char *line, int len) +static void check_subheader_line(char *line) { static struct header_def header[] = { { "Content-Type", handle_subcontent_type }, @@ -357,9 +373,9 @@ static void check_subheader_line(char *line, int len) handle_content_transfer_encoding }, { NULL }, }; - check_header(line, len, header); + check_header(line, header); } -static void check_header_line(char *line, int len) +static void check_header_line(char *line) { static struct header_def header[] = { { "From", handle_from }, @@ -370,27 +386,36 @@ static void check_header_line(char *line, int len) handle_content_transfer_encoding }, { NULL }, }; - check_header(line, len, header); + check_header(line, header); } static int read_one_header_line(char *line, int sz, FILE *in) { int ofs = 0; while (ofs < sz) { + const char *colon; int peek, len; if (fgets(line + ofs, sz - ofs, in) == NULL) - return ofs; + break; len = eatspace(line + ofs); if (len == 0) - return ofs; - peek = fgetc(in); ungetc(peek, in); - if (peek == ' ' || peek == '\t') { - /* Yuck, 2822 header "folding" */ - ofs += len; - continue; + break; + colon = strchr(line, ':'); + if (!colon || !isspace(colon[1])) { + /* Re-add the newline */ + line[ofs + len] = '\n'; + line[ofs + len + 1] = '\0'; + break; } - return ofs + len; + ofs += len; + /* Yuck, 2822 header "folding" */ + peek = fgetc(in); ungetc(peek, in); + if (peek != ' ' && peek != '\t') + break; } + /* Count mbox From headers as headers */ + if (!ofs && !memcmp(line, "From ", 5)) + ofs = 1; return ofs; } @@ -585,25 +610,13 @@ static void decode_transfer_encoding(char *line) static void handle_info(void) { char *sub; - static int done_info = 0; - - if (done_info) - return; - done_info = 1; sub = cleanup_subject(subject); cleanup_space(name); cleanup_space(date); cleanup_space(email); cleanup_space(sub); - /* Unwrap inline B and Q encoding, and optionally - * normalize the meta information to utf8. - */ - decode_header_bq(name); - decode_header_bq(date); - decode_header_bq(email); - decode_header_bq(sub); printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", name, email, sub, date); } @@ -611,7 +624,7 @@ static void handle_info(void) /* We are inside message body and have read line[] already. * Spit out the commit log. */ -static int handle_commit_msg(void) +static int handle_commit_msg(int *seen) { if (!cmitmsg) return 0; @@ -635,6 +648,11 @@ static int handle_commit_msg(void) decode_transfer_encoding(line); if (metainfo_charset) convert_to_utf8(line, charset); + + handle_inbody_header(seen, line); + if (!(*seen & SEEN_PREFIX)) + continue; + fputs(line, cmitmsg); } while (fgets(line, sizeof(line), stdin) != NULL); fclose(cmitmsg); @@ -666,26 +684,16 @@ static void handle_patch(void) * that the first part to contain commit message and a patch, and * handle other parts as pure patches. */ -static int handle_multipart_one_part(void) +static int handle_multipart_one_part(int *seen) { - int seen = 0; int n = 0; - int len; while (fgets(line, sizeof(line), stdin) != NULL) { again: - len = eatspace(line); n++; - if (!len) - continue; if (is_multipart_boundary(line)) break; - if (0 <= seen && handle_inbody_header(&seen, line)) - continue; - seen = -1; /* no more inbody headers */ - line[len] = '\n'; - handle_info(); - if (handle_commit_msg()) + if (handle_commit_msg(seen)) goto again; handle_patch(); break; @@ -697,6 +705,7 @@ static int handle_multipart_one_part(void) static void handle_multipart_body(void) { + int seen = 0; int part_num = 0; /* Skip up to the first boundary */ @@ -709,16 +718,16 @@ static void handle_multipart_body(void) return; /* We are on boundary line. Start slurping the subhead. */ while (1) { - int len = read_one_header_line(line, sizeof(line), stdin); - if (!len) { - if (handle_multipart_one_part() < 0) + int hdr = read_one_header_line(line, sizeof(line), stdin); + if (!hdr) { + if (handle_multipart_one_part(&seen) < 0) return; /* Reset per part headers */ transfer_encoding = TE_DONTCARE; charset[0] = 0; } else - check_subheader_line(line, len); + check_subheader_line(line); } fclose(patchfile); if (!patch_lines) { @@ -732,18 +741,9 @@ static void handle_body(void) { int seen = 0; - while (fgets(line, sizeof(line), stdin) != NULL) { - int len = eatspace(line); - if (!len) - continue; - if (0 <= seen && handle_inbody_header(&seen, line)) - continue; - seen = -1; /* no more inbody headers */ - line[len] = '\n'; - handle_info(); - handle_commit_msg(); + if (line[0] || fgets(line, sizeof(line), stdin) != NULL) { + handle_commit_msg(&seen); handle_patch(); - break; } fclose(patchfile); if (!patch_lines) { @@ -787,15 +787,16 @@ int main(int argc, char **argv) exit(1); } while (1) { - int len = read_one_header_line(line, sizeof(line), stdin); - if (!len) { + int hdr = read_one_header_line(line, sizeof(line), stdin); + if (!hdr) { if (multipart_boundary[0]) handle_multipart_body(); else handle_body(); + handle_info(); break; } - check_header_line(line, len); + check_header_line(line); } return 0; } diff --git a/read-cache.c b/read-cache.c index 32ba917923..36bd4ea0be 100644 --- a/read-cache.c +++ b/read-cache.c @@ -4,11 +4,26 @@ * Copyright (C) Linus Torvalds, 2005 */ #include "cache.h" +#include "cache-tree.h" + +/* Index extensions. + * + * The first letter should be 'A'..'Z' for extensions that are not + * necessary for a correct operation (i.e. optimization data). + * When new extensions are added that _needs_ to be understood in + * order to correctly interpret the index file, pick character that + * is outside the range, to cause the reader to abort. + */ + +#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) +#define CACHE_EXT_TREE 0x54524545 /* "TREE" */ struct cache_entry **active_cache = NULL; static time_t index_file_timestamp; unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0; +struct cache_tree *active_cache_tree = NULL; + /* * This only updates the "non-critical" parts of the directory * cache, ie the parts that aren't tracked by GIT, and only used @@ -696,6 +711,22 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size) return 0; } +static int read_index_extension(const char *ext, void *data, unsigned long sz) +{ + switch (CACHE_EXT(ext)) { + case CACHE_EXT_TREE: + active_cache_tree = cache_tree_read(data, sz); + break; + default: + if (*ext < 'A' || 'Z' < *ext) + return error("index uses %.4s extension, which we do not understand", + ext); + fprintf(stderr, "ignoring %.4s extension\n", ext); + break; + } + return 0; +} + int read_cache(void) { int fd, i; @@ -744,6 +775,22 @@ int read_cache(void) active_cache[i] = ce; } index_file_timestamp = st.st_mtime; + while (offset <= size - 20 - 8) { + /* After an array of active_nr index entries, + * there can be arbitrary number of extended + * sections, each of which is prefixed with + * extension name (4-byte) and section length + * in 4-byte network byte order. + */ + unsigned long extsize; + memcpy(&extsize, map + offset + 4, 4); + extsize = ntohl(extsize); + if (read_index_extension(map + offset, + map + offset + 8, extsize) < 0) + goto unmap; + offset += 8; + offset += extsize; + } return active_nr; unmap: @@ -778,6 +825,17 @@ static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) return 0; } +static int write_index_ext_header(SHA_CTX *context, int fd, + unsigned long ext, unsigned long sz) +{ + ext = htonl(ext); + sz = htonl(sz); + if ((ce_write(context, fd, &ext, 4) < 0) || + (ce_write(context, fd, &sz, 4) < 0)) + return -1; + return 0; +} + static int ce_flush(SHA_CTX *context, int fd) { unsigned int left = write_buffer_len; @@ -874,5 +932,19 @@ int write_cache(int newfd, struct cache_entry **cache, int entries) if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) return -1; } + + /* Write extension data here */ + if (active_cache_tree) { + unsigned long sz; + void *data = cache_tree_write(active_cache_tree, &sz); + if (data && + !write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) && + !ce_write(&c, newfd, data, sz)) + ; + else { + free(data); + return -1; + } + } return ce_flush(&c, newfd); } @@ -142,6 +142,8 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u namelen = strlen(de->d_name); if (namelen > 255) continue; + if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock")) + continue; memcpy(path + baselen, de->d_name, namelen+1); if (stat(git_path("%s", path), &st) < 0) continue; @@ -198,26 +200,6 @@ int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1)) return do_for_each_ref("refs/remotes", fn, 13); } -static char *ref_file_name(const char *ref) -{ - char *base = get_refs_directory(); - int baselen = strlen(base); - int reflen = strlen(ref); - char *ret = xmalloc(baselen + 2 + reflen); - sprintf(ret, "%s/%s", base, ref); - return ret; -} - -static char *ref_lock_file_name(const char *ref) -{ - char *base = get_refs_directory(); - int baselen = strlen(base); - int reflen = strlen(ref); - char *ret = xmalloc(baselen + 7 + reflen); - sprintf(ret, "%s/%s.lock", base, ref); - return ret; -} - int get_ref_sha1(const char *ref, unsigned char *sha1) { if (check_ref_format(ref)) @@ -225,94 +207,6 @@ int get_ref_sha1(const char *ref, unsigned char *sha1) return read_ref(git_path("refs/%s", ref), sha1); } -static int lock_ref_file(const char *filename, const char *lock_filename, - const unsigned char *old_sha1) -{ - int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666); - unsigned char current_sha1[20]; - int retval; - if (fd < 0) { - return error("Couldn't open lock file for %s: %s", - filename, strerror(errno)); - } - retval = read_ref(filename, current_sha1); - if (old_sha1) { - if (retval) { - close(fd); - unlink(lock_filename); - return error("Could not read the current value of %s", - filename); - } - if (memcmp(current_sha1, old_sha1, 20)) { - close(fd); - unlink(lock_filename); - error("The current value of %s is %s", - filename, sha1_to_hex(current_sha1)); - return error("Expected %s", - sha1_to_hex(old_sha1)); - } - } else { - if (!retval) { - close(fd); - unlink(lock_filename); - return error("Unexpectedly found a value of %s for %s", - sha1_to_hex(current_sha1), filename); - } - } - return fd; -} - -int lock_ref_sha1(const char *ref, const unsigned char *old_sha1) -{ - char *filename; - char *lock_filename; - int retval; - if (check_ref_format(ref)) - return -1; - filename = ref_file_name(ref); - lock_filename = ref_lock_file_name(ref); - retval = lock_ref_file(filename, lock_filename, old_sha1); - free(filename); - free(lock_filename); - return retval; -} - -static int write_ref_file(const char *filename, - const char *lock_filename, int fd, - const unsigned char *sha1) -{ - char *hex = sha1_to_hex(sha1); - char term = '\n'; - if (write(fd, hex, 40) < 40 || - write(fd, &term, 1) < 1) { - error("Couldn't write %s", filename); - close(fd); - return -1; - } - close(fd); - rename(lock_filename, filename); - return 0; -} - -int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1) -{ - char *filename; - char *lock_filename; - int retval; - if (fd < 0) - return -1; - if (check_ref_format(ref)) - return -1; - filename = ref_file_name(ref); - lock_filename = ref_lock_file_name(ref); - if (safe_create_leading_directories(filename)) - die("unable to create leading directory for %s", filename); - retval = write_ref_file(filename, lock_filename, fd, sha1); - free(filename); - free(lock_filename); - return retval; -} - /* * Make sure "ref" is something reasonable to have under ".git/refs/"; * We do not like it if: @@ -365,25 +259,255 @@ int check_ref_format(const char *ref) } } -int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1) +static struct ref_lock* verify_lock(struct ref_lock *lock, + const unsigned char *old_sha1, int mustexist) +{ + char buf[40]; + int nr, fd = open(lock->ref_file, O_RDONLY); + if (fd < 0 && (mustexist || errno != ENOENT)) { + error("Can't verify ref %s", lock->ref_file); + unlock_ref(lock); + return NULL; + } + nr = read(fd, buf, 40); + close(fd); + if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) { + error("Can't verify ref %s", lock->ref_file); + unlock_ref(lock); + return NULL; + } + if (memcmp(lock->old_sha1, old_sha1, 20)) { + error("Ref %s is at %s but expected %s", lock->ref_file, + sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1)); + unlock_ref(lock); + return NULL; + } + return lock; +} + +static struct ref_lock* lock_ref_sha1_basic(const char *path, + int plen, + const unsigned char *old_sha1, int mustexist) +{ + struct ref_lock *lock; + struct stat st; + + lock = xcalloc(1, sizeof(struct ref_lock)); + lock->lock_fd = -1; + + plen = strlen(path) - plen; + path = resolve_ref(path, lock->old_sha1, mustexist); + if (!path) { + unlock_ref(lock); + return NULL; + } + + lock->ref_file = strdup(path); + lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file)); + lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen)); + lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT; + + if (safe_create_leading_directories(lock->lock_file)) + die("unable to create directory for %s", lock->lock_file); + lock->lock_fd = open(lock->lock_file, + O_WRONLY | O_CREAT | O_EXCL, 0666); + if (lock->lock_fd < 0) { + error("Couldn't open lock file %s: %s", + lock->lock_file, strerror(errno)); + unlock_ref(lock); + return NULL; + } + + return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock; +} + +struct ref_lock* lock_ref_sha1(const char *ref, + const unsigned char *old_sha1, int mustexist) { - char *filename; - char *lock_filename; - int fd; - int retval; if (check_ref_format(ref)) + return NULL; + return lock_ref_sha1_basic(git_path("refs/%s", ref), + 5 + strlen(ref), old_sha1, mustexist); +} + +struct ref_lock* lock_any_ref_for_update(const char *ref, + const unsigned char *old_sha1, int mustexist) +{ + return lock_ref_sha1_basic(git_path("%s", ref), + strlen(ref), old_sha1, mustexist); +} + +void unlock_ref (struct ref_lock *lock) +{ + if (lock->lock_fd >= 0) { + close(lock->lock_fd); + unlink(lock->lock_file); + } + if (lock->ref_file) + free(lock->ref_file); + if (lock->lock_file) + free(lock->lock_file); + if (lock->log_file) + free(lock->log_file); + free(lock); +} + +static int log_ref_write(struct ref_lock *lock, + const unsigned char *sha1, const char *msg) +{ + int logfd, written, oflags = O_APPEND | O_WRONLY; + unsigned maxlen, len; + char *logrec; + const char *comitter; + + if (log_all_ref_updates) { + if (safe_create_leading_directories(lock->log_file) < 0) + return error("unable to create directory for %s", + lock->log_file); + oflags |= O_CREAT; + } + + logfd = open(lock->log_file, oflags, 0666); + if (logfd < 0) { + if (!log_all_ref_updates && errno == ENOENT) + return 0; + return error("Unable to append to %s: %s", + lock->log_file, strerror(errno)); + } + + setup_ident(); + comitter = git_committer_info(1); + if (msg) { + maxlen = strlen(comitter) + strlen(msg) + 2*40 + 5; + logrec = xmalloc(maxlen); + len = snprintf(logrec, maxlen, "%s %s %s\t%s\n", + sha1_to_hex(lock->old_sha1), + sha1_to_hex(sha1), + comitter, + msg); + } else { + maxlen = strlen(comitter) + 2*40 + 4; + logrec = xmalloc(maxlen); + len = snprintf(logrec, maxlen, "%s %s %s\n", + sha1_to_hex(lock->old_sha1), + sha1_to_hex(sha1), + comitter); + } + written = len <= maxlen ? write(logfd, logrec, len) : -1; + free(logrec); + close(logfd); + if (written != len) + return error("Unable to append to %s", lock->log_file); + return 0; +} + +int write_ref_sha1(struct ref_lock *lock, + const unsigned char *sha1, const char *logmsg) +{ + static char term = '\n'; + + if (!lock) return -1; - filename = ref_file_name(ref); - lock_filename = ref_lock_file_name(ref); - if (safe_create_leading_directories(filename)) - die("unable to create leading directory for %s", filename); - fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666); - if (fd < 0) { - error("Writing %s", lock_filename); - perror("Open"); + if (!lock->force_write && !memcmp(lock->old_sha1, sha1, 20)) { + unlock_ref(lock); + return 0; } - retval = write_ref_file(filename, lock_filename, fd, sha1); - free(filename); - free(lock_filename); - return retval; + if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 || + write(lock->lock_fd, &term, 1) != 1 + || close(lock->lock_fd) < 0) { + error("Couldn't write %s", lock->lock_file); + unlock_ref(lock); + return -1; + } + if (log_ref_write(lock, sha1, logmsg) < 0) { + unlock_ref(lock); + return -1; + } + if (rename(lock->lock_file, lock->ref_file) < 0) { + error("Couldn't set %s", lock->ref_file); + unlock_ref(lock); + return -1; + } + lock->lock_fd = -1; + unlock_ref(lock); + return 0; +} + +int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1) +{ + const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec; + char *tz_c; + int logfd, tz; + struct stat st; + unsigned long date; + unsigned char logged_sha1[20]; + + logfile = git_path("logs/%s", ref); + logfd = open(logfile, O_RDONLY, 0); + if (logfd < 0) + die("Unable to read log %s: %s", logfile, strerror(errno)); + fstat(logfd, &st); + if (!st.st_size) + die("Log %s is empty.", logfile); + logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0); + close(logfd); + + lastrec = NULL; + rec = logend = logdata + st.st_size; + while (logdata < rec) { + if (logdata < rec && *(rec-1) == '\n') + rec--; + lastgt = NULL; + while (logdata < rec && *(rec-1) != '\n') { + rec--; + if (*rec == '>') + lastgt = rec; + } + if (!lastgt) + die("Log %s is corrupt.", logfile); + date = strtoul(lastgt + 1, &tz_c, 10); + if (date <= at_time) { + if (lastrec) { + if (get_sha1_hex(lastrec, logged_sha1)) + die("Log %s is corrupt.", logfile); + if (get_sha1_hex(rec + 41, sha1)) + die("Log %s is corrupt.", logfile); + if (memcmp(logged_sha1, sha1, 20)) { + tz = strtoul(tz_c, NULL, 10); + fprintf(stderr, + "warning: Log %s has gap after %s.\n", + logfile, show_rfc2822_date(date, tz)); + } + } else if (date == at_time) { + if (get_sha1_hex(rec + 41, sha1)) + die("Log %s is corrupt.", logfile); + } else { + if (get_sha1_hex(rec + 41, logged_sha1)) + die("Log %s is corrupt.", logfile); + if (memcmp(logged_sha1, sha1, 20)) { + tz = strtoul(tz_c, NULL, 10); + fprintf(stderr, + "warning: Log %s unexpectedly ended on %s.\n", + logfile, show_rfc2822_date(date, tz)); + } + } + munmap((void*)logdata, st.st_size); + return 0; + } + lastrec = rec; + } + + rec = logdata; + while (rec < logend && *rec != '>' && *rec != '\n') + rec++; + if (rec == logend || *rec == '\n') + die("Log %s is corrupt.", logfile); + date = strtoul(rec + 1, &tz_c, 10); + tz = strtoul(tz_c, NULL, 10); + if (get_sha1_hex(logdata, sha1)) + die("Log %s is corrupt.", logfile); + munmap((void*)logdata, st.st_size); + fprintf(stderr, "warning: Log %s only goes back to %s.\n", + logfile, show_rfc2822_date(date, tz)); + return 0; } @@ -1,6 +1,15 @@ #ifndef REFS_H #define REFS_H +struct ref_lock { + char *ref_file; + char *lock_file; + char *log_file; + unsigned char old_sha1[20]; + int lock_fd; + int force_write; +}; + /* * Calls the specified function for each ref file until it returns nonzero, * and returns the value @@ -14,16 +23,20 @@ extern int for_each_remote_ref(int (*fn)(const char *path, const unsigned char * /** Reads the refs file specified into sha1 **/ extern int get_ref_sha1(const char *ref, unsigned char *sha1); -/** Locks ref and returns the fd to give to write_ref_sha1() if the ref - * has the given value currently; otherwise, returns -1. - **/ -extern int lock_ref_sha1(const char *ref, const unsigned char *old_sha1); +/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/ +extern struct ref_lock* lock_ref_sha1(const char *ref, const unsigned char *old_sha1, int mustexist); + +/** Locks any ref (for 'HEAD' type refs). */ +extern struct ref_lock* lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int mustexist); + +/** Release any lock taken but not written. **/ +extern void unlock_ref (struct ref_lock *lock); -/** Writes sha1 into the refs file specified, locked with the given fd. **/ -extern int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1); +/** Writes sha1 into the ref specified by the lock. **/ +extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg); -/** Writes sha1 into the refs file specified. **/ -extern int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1); +/** Reads log for the value of ref during at_time. **/ +extern int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1); /** Returns 0 if target has the right format for a ref. **/ extern int check_ref_format(const char *target); diff --git a/sha1_name.c b/sha1_name.c index dc6835520c..fbbde1cf7d 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -4,6 +4,7 @@ #include "tree.h" #include "blob.h" #include "tree-walk.h" +#include "refs.h" static int find_short_object_filename(int len, const char *name, unsigned char *sha1) { @@ -245,36 +246,61 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1) "refs/remotes/%.*s/HEAD", NULL }; - const char **p; - const char *warning = "warning: refname '%.*s' is ambiguous.\n"; - char *pathname; - int already_found = 0; + static const char *warning = "warning: refname '%.*s' is ambiguous.\n"; + const char **p, *pathname; + char *real_path = NULL; + int refs_found = 0, am; + unsigned long at_time = (unsigned long)-1; unsigned char *this_result; unsigned char sha1_from_ref[20]; if (len == 40 && !get_sha1_hex(str, sha1)) return 0; + /* At a given period of time? "@{2 hours ago}" */ + for (am = 1; am < len - 1; am++) { + if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') { + int date_len = len - am - 3; + char *date_spec = xmalloc(date_len + 1); + strncpy(date_spec, str + am + 2, date_len); + date_spec[date_len] = 0; + at_time = approxidate(date_spec); + free(date_spec); + len = am; + break; + } + } + /* Accept only unambiguous ref paths. */ if (ambiguous_path(str, len)) return -1; for (p = fmt; *p; p++) { - this_result = already_found ? sha1_from_ref : sha1; - pathname = git_path(*p, len, str); - if (!read_ref(pathname, this_result)) { - if (warn_ambiguous_refs) { - if (already_found) - fprintf(stderr, warning, len, str); - already_found++; - } - else - return 0; + this_result = refs_found ? sha1_from_ref : sha1; + pathname = resolve_ref(git_path(*p, len, str), this_result, 1); + if (pathname) { + if (!refs_found++) + real_path = strdup(pathname); + if (!warn_ambiguous_refs) + break; } } - if (already_found) - return 0; - return -1; + + if (!refs_found) + return -1; + + if (warn_ambiguous_refs && refs_found > 1) + fprintf(stderr, warning, len, str); + + if (at_time != (unsigned long)-1) { + read_ref_at( + real_path + strlen(git_path(".")) - 1, + at_time, + sha1); + } + + free(real_path); + return 0; } static int get_sha1_1(const char *name, int len, unsigned char *sha1); @@ -456,7 +482,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1) */ int get_sha1(const char *name, unsigned char *sha1) { - int ret; + int ret, bracket_depth; unsigned unused; int namelen = strlen(name); const char *cp; @@ -502,8 +528,15 @@ int get_sha1(const char *name, unsigned char *sha1) } return -1; } - cp = strchr(name, ':'); - if (cp) { + for (cp = name, bracket_depth = 0; *cp; cp++) { + if (*cp == '{') + bracket_depth++; + else if (bracket_depth && *cp == '}') + bracket_depth--; + else if (!bracket_depth && *cp == ':') + break; + } + if (*cp == ':') { unsigned char tree_sha1[20]; if (!get_sha1_1(name, cp-name, tree_sha1)) return get_tree_entry(tree_sha1, cp+1, sha1, diff --git a/ssh-fetch.c b/ssh-fetch.c index 4eb9e04829..e3067b878e 100644 --- a/ssh-fetch.c +++ b/ssh-fetch.c @@ -132,6 +132,7 @@ int main(int argc, char **argv) if (!prog) prog = "git-ssh-upload"; setup_git_directory(); + git_config(git_default_config); while (arg < argc && argv[arg][0] == '-') { if (argv[arg][1] == 't') { @@ -158,6 +159,7 @@ int main(int argc, char **argv) } commit_id = argv[arg]; url = argv[arg + 1]; + write_ref_log_details = url; if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1)) return 1; diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index cf33989b56..2c9bbb59b0 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -195,6 +195,20 @@ test_expect_success \ 'git-ls-tree -r output for a known tree.' \ 'diff current expected' +test_expect_success \ + 'writing partial tree out with git-write-tree --prefix.' \ + 'ptree=$(git-write-tree --prefix=path3)' +test_expect_success \ + 'validate object ID for a known tree.' \ + 'test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3' + +test_expect_success \ + 'writing partial tree out with git-write-tree --prefix.' \ + 'ptree=$(git-write-tree --prefix=path3/subp3)' +test_expect_success \ + 'validate object ID for a known tree.' \ + 'test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2' + ################################################################ rm .git/index test_expect_success \ diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh new file mode 100755 index 0000000000..df3e993365 --- /dev/null +++ b/t/t1400-update-ref.sh @@ -0,0 +1,213 @@ +#!/bin/sh +# +# Copyright (c) 2006 Shawn Pearce +# + +test_description='Test git-update-ref and basic ref logging' +. ./test-lib.sh + +Z=0000000000000000000000000000000000000000 +A=1111111111111111111111111111111111111111 +B=2222222222222222222222222222222222222222 +C=3333333333333333333333333333333333333333 +D=4444444444444444444444444444444444444444 +E=5555555555555555555555555555555555555555 +F=6666666666666666666666666666666666666666 +m=refs/heads/master + +test_expect_success \ + "create $m" \ + 'git-update-ref $m $A && + test $A = $(cat .git/$m)' +test_expect_success \ + "create $m" \ + 'git-update-ref $m $B $A && + test $B = $(cat .git/$m)' +rm -f .git/$m + +test_expect_success \ + "create $m (by HEAD)" \ + 'git-update-ref HEAD $A && + test $A = $(cat .git/$m)' +test_expect_success \ + "create $m (by HEAD)" \ + 'git-update-ref HEAD $B $A && + test $B = $(cat .git/$m)' +rm -f .git/$m + +test_expect_failure \ + '(not) create HEAD with old sha1' \ + 'git-update-ref HEAD $A $B' +test_expect_failure \ + "(not) prior created .git/$m" \ + 'test -f .git/$m' +rm -f .git/$m + +test_expect_success \ + "create HEAD" \ + 'git-update-ref HEAD $A' +test_expect_failure \ + '(not) change HEAD with wrong SHA1' \ + 'git-update-ref HEAD $B $Z' +test_expect_failure \ + "(not) changed .git/$m" \ + 'test $B = $(cat .git/$m)' +rm -f .git/$m + +mkdir -p .git/logs/refs/heads +touch .git/logs/refs/heads/master +test_expect_success \ + "create $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:30" \ + git-update-ref HEAD $A -m "Initial Creation" && + test $A = $(cat .git/$m)' +test_expect_success \ + "update $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:31" \ + git-update-ref HEAD $B $A -m "Switch" && + test $B = $(cat .git/$m)' +test_expect_success \ + "set $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:41" \ + git-update-ref HEAD $A && + test $A = $(cat .git/$m)' + +cat >expect <<EOF +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 +EOF +test_expect_success \ + "verifying $m's log" \ + 'diff expect .git/logs/$m' +rm -rf .git/$m .git/logs expect + +test_expect_success \ + 'enable core.logAllRefUpdates' \ + 'git-repo-config core.logAllRefUpdates true && + test true = $(git-repo-config --bool --get core.logAllRefUpdates)' + +test_expect_success \ + "create $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:32" \ + git-update-ref HEAD $A -m "Initial Creation" && + test $A = $(cat .git/$m)' +test_expect_success \ + "update $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:33" \ + git-update-ref HEAD $B $A -m "Switch" && + test $B = $(cat .git/$m)' +test_expect_success \ + "set $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:43" \ + git-update-ref HEAD $A && + test $A = $(cat .git/$m)' + +cat >expect <<EOF +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000 +EOF +test_expect_success \ + "verifying $m's log" \ + 'diff expect .git/logs/$m' +rm -f .git/$m .git/logs/$m expect + +git-update-ref $m $D +cat >.git/logs/$m <<EOF +$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500 +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500 +$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500 +$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500 +EOF + +ed="Thu, 26 May 2005 18:32:00 -0500" +gd="Thu, 26 May 2005 18:33:00 -0500" +ld="Thu, 26 May 2005 18:43:00 -0500" +test_expect_success \ + 'Query "master@{May 25 2005}" (before history)' \ + 'rm -f o e + git-rev-parse --verify "master@{May 25 2005}" >o 2>e && + test $C = $(cat o) && + test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"' +test_expect_success \ + "Query master@{2005-05-25} (before history)" \ + 'rm -f o e + git-rev-parse --verify master@{2005-05-25} >o 2>e && + test $C = $(cat o) && + echo test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \ + 'rm -f o e + git-rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e && + test $C = $(cat o) && + test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \ + 'rm -f o e + git-rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e && + test $A = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \ + 'rm -f o e + git-rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e && + test $B = $(cat o) && + test "warning: Log .git/logs/$m has gap after $gd." = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:38:00}" (middle of history)' \ + 'rm -f o e + git-rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e && + test $Z = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \ + 'rm -f o e + git-rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e && + test $E = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-28}" (past end of history)' \ + 'rm -f o e + git-rev-parse --verify "master@{2005-05-28}" >o 2>e && + test $D = $(cat o) && + test "warning: Log .git/logs/$m unexpectedly ended on $ld." = "$(cat e)"' + + +rm -f .git/$m .git/logs/$m expect + +test_expect_success \ + 'creating initial files' \ + 'echo TEST >F && + git-add F && + GIT_AUTHOR_DATE="2005-05-26 23:30" \ + GIT_COMMITTER_DATE="2005-05-26 23:30" git-commit -m add -a && + h_TEST=$(git-rev-parse --verify HEAD) + echo The other day this did not work. >M && + echo And then Bob told me how to fix it. >>M && + echo OTHER >F && + GIT_AUTHOR_DATE="2005-05-26 23:41" \ + GIT_COMMITTER_DATE="2005-05-26 23:41" git-commit -F M -a && + h_OTHER=$(git-rev-parse --verify HEAD) + rm -f M' + +cat >expect <<EOF +$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit: add +$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work. +EOF +test_expect_success \ + 'git-commit logged updates' \ + 'diff expect .git/logs/$m' +unset h_TEST h_OTHER + +test_expect_success \ + 'git-cat-file blob master:F (expect OTHER)' \ + 'test OTHER = $(git-cat-file blob master:F)' +test_expect_success \ + 'git-cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \ + 'test TEST = $(git-cat-file blob "master@{2005-05-26 23:30}:F")' +test_expect_success \ + 'git-cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \ + 'test OTHER = $(git-cat-file blob "master@{2005-05-26 23:42}:F")' + +test_done diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index c3de151942..5b04efc89d 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -14,7 +14,8 @@ test_expect_success \ 'prepare an trivial repository' \ 'echo Hello > A && git-update-index --add A && - git-commit -m "Initial commit."' + git-commit -m "Initial commit." && + HEAD=$(git-rev-parse --verify HEAD)' test_expect_success \ 'git branch --help should return success now.' \ @@ -32,4 +33,32 @@ test_expect_success \ 'git branch a/b/c should create a branch' \ 'git-branch a/b/c && test -f .git/refs/heads/a/b/c' +cat >expect <<EOF +0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from HEAD +EOF +test_expect_success \ + 'git branch -l d/e/f should create a branch and a log' \ + 'GIT_COMMITTER_DATE="2005-05-26 23:30" \ + git-branch -l d/e/f && + test -f .git/refs/heads/d/e/f && + test -f .git/logs/refs/heads/d/e/f && + diff expect .git/logs/refs/heads/d/e/f' + +test_expect_success \ + 'git branch -d d/e/f should delete a branch and a log' \ + 'git-branch -d d/e/f && + test ! -f .git/refs/heads/d/e/f && + test ! -f .git/logs/refs/heads/d/e/f' + +cat >expect <<EOF +0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 checkout: Created from master^0 +EOF +test_expect_success \ + 'git checkout -b g/h/i -l should create a branch and a log' \ + 'GIT_COMMITTER_DATE="2005-05-26 23:30" \ + git-checkout -b g/h/i -l master && + test -f .git/refs/heads/g/h/i && + test -f .git/logs/refs/heads/g/h/i && + diff expect .git/logs/refs/heads/g/h/i' + test_done diff --git a/t/t4113-apply-ending.sh b/t/t4113-apply-ending.sh new file mode 100755 index 0000000000..7fd0cf62ec --- /dev/null +++ b/t/t4113-apply-ending.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# +# Copyright (c) 2006 Catalin Marinas +# + +test_description='git-apply trying to add an ending line. + +' +. ./test-lib.sh + +# setup + +cat >test-patch <<\EOF +diff --git a/file b/file +--- a/file ++++ b/file +@@ -1,2 +1,3 @@ + a + b ++c +EOF + +echo 'a' >file +echo 'b' >>file +echo 'c' >>file + +test_expect_success setup \ + 'git-update-index --add file' + +# test + +test_expect_failure 'apply at the end' \ + 'git-apply --index test-patch' + +cat >test-patch <<\EOF +diff a/file b/file +--- a/file ++++ b/file +@@ -1,2 +1,3 @@ ++a + b + c +EOF + +echo >file 'a +b +c' +git-update-index file + +test_expect_failure 'apply at the beginning' \ + 'git-apply --index test-patch' + +test_done diff --git a/update-index.c b/update-index.c index 5854d11fd9..956b6b34f9 100644 --- a/update-index.c +++ b/update-index.c @@ -6,6 +6,7 @@ #include "cache.h" #include "strbuf.h" #include "quote.h" +#include "cache-tree.h" #include "tree-walk.h" /* @@ -51,6 +52,7 @@ static int mark_valid(const char *path) active_cache[pos]->ce_flags &= ~htons(CE_VALID); break; } + cache_tree_invalidate_path(active_cache_tree, path); active_cache_changed = 1; return 0; } @@ -64,6 +66,12 @@ static int add_file_to_cache(const char *path) struct stat st; status = lstat(path, &st); + + /* We probably want to do this in remove_file_from_cache() and + * add_cache_entry() instead... + */ + cache_tree_invalidate_path(active_cache_tree, path); + if (status < 0 || S_ISDIR(st.st_mode)) { /* When we used to have "path" and now we want to add * "path/file", we need a way to remove "path" before @@ -145,6 +153,7 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, return error("%s: cannot add to the index - missing --add option?", path); report("add '%s'", path); + cache_tree_invalidate_path(active_cache_tree, path); return 0; } @@ -169,6 +178,7 @@ static void chmod_path(int flip, const char *path) default: goto fail; } + cache_tree_invalidate_path(active_cache_tree, path); active_cache_changed = 1; report("chmod %cx '%s'", flip, path); return; @@ -190,6 +200,7 @@ static void update_one(const char *path, const char *prefix, int prefix_length) die("Unable to mark file %s", path); goto free_return; } + cache_tree_invalidate_path(active_cache_tree, path); if (force_remove) { if (remove_file_from_cache(p)) @@ -268,6 +279,7 @@ static void read_index_info(int line_termination) free(path_name); continue; } + cache_tree_invalidate_path(active_cache_tree, path_name); if (!mode) { /* mode == 0 means there is no such path -- remove */ @@ -374,6 +386,7 @@ static int unresolve_one(const char *path) goto free_return; } + cache_tree_invalidate_path(active_cache_tree, path); remove_file_from_cache(path); if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) { error("%s: cannot add our version to the index.", path); diff --git a/update-ref.c b/update-ref.c index fd487421cd..a1e6bb90fe 100644 --- a/update-ref.c +++ b/update-ref.c @@ -1,85 +1,56 @@ #include "cache.h" #include "refs.h" -static const char git_update_ref_usage[] = "git-update-ref <refname> <value> [<oldval>]"; - -static int re_verify(const char *path, unsigned char *oldsha1, unsigned char *currsha1) -{ - char buf[40]; - int fd = open(path, O_RDONLY), nr; - if (fd < 0) - return -1; - nr = read(fd, buf, 40); - close(fd); - if (nr != 40 || get_sha1_hex(buf, currsha1) < 0) - return -1; - return memcmp(oldsha1, currsha1, 20) ? -1 : 0; -} +static const char git_update_ref_usage[] = +"git-update-ref <refname> <value> [<oldval>] [-m <reason>]"; int main(int argc, char **argv) { - char *hex; - const char *refname, *value, *oldval, *path; - char *lockpath; - unsigned char sha1[20], oldsha1[20], currsha1[20]; - int fd, written; + const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL; + struct ref_lock *lock; + unsigned char sha1[20], oldsha1[20]; + int i; setup_git_directory(); git_config(git_default_config); - if (argc < 3 || argc > 4) + + for (i = 1; i < argc; i++) { + if (!strcmp("-m", argv[i])) { + if (i+1 >= argc) + usage(git_update_ref_usage); + msg = argv[++i]; + if (!*msg) + die("Refusing to perform update with empty message."); + if (strchr(msg, '\n')) + die("Refusing to perform update with \\n in message."); + continue; + } + if (!refname) { + refname = argv[i]; + continue; + } + if (!value) { + value = argv[i]; + continue; + } + if (!oldval) { + oldval = argv[i]; + continue; + } + } + if (!refname || !value) usage(git_update_ref_usage); - refname = argv[1]; - value = argv[2]; - oldval = argv[3]; if (get_sha1(value, sha1)) die("%s: not a valid SHA1", value); memset(oldsha1, 0, 20); if (oldval && get_sha1(oldval, oldsha1)) die("%s: not a valid old SHA1", oldval); - path = resolve_ref(git_path("%s", refname), currsha1, !!oldval); - if (!path) - die("No such ref: %s", refname); - - if (oldval) { - if (memcmp(currsha1, oldsha1, 20)) - die("Ref %s is at %s but expected %s", refname, sha1_to_hex(currsha1), sha1_to_hex(oldsha1)); - /* Nothing to do? */ - if (!memcmp(oldsha1, sha1, 20)) - exit(0); - } - path = strdup(path); - lockpath = mkpath("%s.lock", path); - if (safe_create_leading_directories(lockpath) < 0) - die("Unable to create all of %s", lockpath); - - fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); - if (fd < 0) - die("Unable to create %s", lockpath); - hex = sha1_to_hex(sha1); - hex[40] = '\n'; - written = write(fd, hex, 41); - close(fd); - if (written != 41) { - unlink(lockpath); - die("Unable to write to %s", lockpath); - } - - /* - * Re-read the ref after getting the lock to verify - */ - if (oldval && re_verify(path, oldsha1, currsha1) < 0) { - unlink(lockpath); - die("Ref lock failed"); - } - - /* - * Finally, replace the old ref with the new one - */ - if (rename(lockpath, path) < 0) { - unlink(lockpath); - die("Unable to create %s", path); - } + lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, 0); + if (!lock) + return 1; + if (write_ref_sha1(lock, sha1, msg) < 0) + return 1; return 0; } diff --git a/write-tree.c b/write-tree.c index dcad6e6670..895e7a359d 100644 --- a/write-tree.c +++ b/write-tree.c @@ -5,154 +5,68 @@ */ #include "cache.h" #include "tree.h" +#include "cache-tree.h" static int missing_ok = 0; +static char *prefix = NULL; -static int check_valid_sha1(unsigned char *sha1) -{ - int ret; - - /* If we were anal, we'd check that the sha1 of the contents actually matches */ - ret = has_sha1_file(sha1); - if (ret == 0) - perror(sha1_file_name(sha1)); - return ret ? 0 : -1; -} - -static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1) -{ - unsigned char subdir_sha1[20]; - unsigned long size, offset; - char *buffer; - int nr; - - /* Guess at some random initial size */ - size = 8192; - buffer = xmalloc(size); - offset = 0; - - nr = 0; - while (nr < maxentries) { - struct cache_entry *ce = cachep[nr]; - const char *pathname = ce->name, *filename, *dirname; - int pathlen = ce_namelen(ce), entrylen; - unsigned char *sha1; - unsigned int mode; - - /* Did we hit the end of the directory? Return how many we wrote */ - if (baselen >= pathlen || memcmp(base, pathname, baselen)) - break; - - sha1 = ce->sha1; - mode = ntohl(ce->ce_mode); - - /* Do we have _further_ subdirectories? */ - filename = pathname + baselen; - dirname = strchr(filename, '/'); - if (dirname) { - int subdir_written; - - subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1); - nr += subdir_written; - - /* Now we need to write out the directory entry into this tree.. */ - mode = S_IFDIR; - pathlen = dirname - pathname; - - /* ..but the directory entry doesn't count towards the total count */ - nr--; - sha1 = subdir_sha1; - } +static const char write_tree_usage[] = +"git-write-tree [--missing-ok] [--prefix=<prefix>/]"; - if (!missing_ok && check_valid_sha1(sha1) < 0) - exit(1); - - entrylen = pathlen - baselen; - if (offset + entrylen + 100 > size) { - size = alloc_nr(offset + entrylen + 100); - buffer = xrealloc(buffer, size); - } - offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename); - buffer[offset++] = 0; - memcpy(buffer + offset, sha1, 20); - offset += 20; - nr++; - } - - write_sha1_file(buffer, offset, tree_type, returnsha1); - free(buffer); - return nr; -} - -static const char write_tree_usage[] = "git-write-tree [--missing-ok]"; +static struct cache_file cache_file; int main(int argc, char **argv) { - int i, funny; - int entries; - unsigned char sha1[20]; - + int entries, was_valid, newfd; + setup_git_directory(); + newfd = hold_index_file_for_update(&cache_file, get_index_file()); entries = read_cache(); - if (argc == 2) { - if (!strcmp(argv[1], "--missing-ok")) + + while (1 < argc) { + char *arg = argv[1]; + if (!strcmp(arg, "--missing-ok")) missing_ok = 1; + else if (!strncmp(arg, "--prefix=", 9)) + prefix = arg + 9; else die(write_tree_usage); + argc--; argv++; } - + if (argc > 2) die("too many options"); if (entries < 0) die("git-write-tree: error reading cache"); - /* Verify that the tree is merged */ - funny = 0; - for (i = 0; i < entries; i++) { - struct cache_entry *ce = active_cache[i]; - if (ce_stage(ce)) { - if (10 < ++funny) { - fprintf(stderr, "...\n"); - break; - } - fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1)); + if (!active_cache_tree) + active_cache_tree = cache_tree(); + + was_valid = cache_tree_fully_valid(active_cache_tree); + if (!was_valid) { + if (cache_tree_update(active_cache_tree, + active_cache, active_nr, + missing_ok, 0) < 0) + die("git-write-tree: error building trees"); + if (0 <= newfd) { + if (!write_cache(newfd, active_cache, active_nr)) + commit_index_file(&cache_file); } - } - if (funny) - die("git-write-tree: not able to write tree"); - - /* Also verify that the cache does not have path and path/file - * at the same time. At this point we know the cache has only - * stage 0 entries. - */ - funny = 0; - for (i = 0; i < entries - 1; i++) { - /* path/file always comes after path because of the way - * the cache is sorted. Also path can appear only once, - * which means conflicting one would immediately follow. + /* Not being able to write is fine -- we are only interested + * in updating the cache-tree part, and if the next caller + * ends up using the old index with unupdated cache-tree part + * it misses the work we did here, but that is just a + * performance penalty and not a big deal. */ - const char *this_name = active_cache[i]->name; - const char *next_name = active_cache[i+1]->name; - int this_len = strlen(this_name); - if (this_len < strlen(next_name) && - strncmp(this_name, next_name, this_len) == 0 && - next_name[this_len] == '/') { - if (10 < ++funny) { - fprintf(stderr, "...\n"); - break; - } - fprintf(stderr, "You have both %s and %s\n", - this_name, next_name); - } } - if (funny) - die("git-write-tree: not able to write tree"); - - /* Ok, write it out */ - if (write_tree(active_cache, entries, "", 0, sha1) != entries) - die("git-write-tree: internal error"); - printf("%s\n", sha1_to_hex(sha1)); + if (prefix) { + struct cache_tree *subtree = + cache_tree_find(active_cache_tree, prefix); + printf("%s\n", sha1_to_hex(subtree->sha1)); + } + else + printf("%s\n", sha1_to_hex(active_cache_tree->sha1)); return 0; } |