From 62e09ce998dd7f6b844deb650101c743a5c4ce50 Mon Sep 17 00:00:00 2001 From: Carlos Rica Date: Fri, 20 Jul 2007 01:42:28 +0200 Subject: [PATCH 01/35] Make git tag a builtin. This replaces the script "git-tag.sh" with "builtin-tag.c". The existing test suite for "git tag" guarantees the compatibility with the features provided by the script version. There are some minor changes in the behaviour of "git tag" here: "git tag -v" now can get more than one tag to verify, like "git tag -d" does, "git tag" with no arguments prints all tags, more like "git branch" does, and "git tag -n" also prints all tags with annotations (without needing -l). Tests and documentation were also updated to reflect these changes. The program is currently calling the script "git verify-tag" for verify. This can be changed porting it to C and calling its functions directly from builtin-tag.c. Signed-off-by: Carlos Rica Signed-off-by: Junio C Hamano --- Documentation/git-tag.txt | 8 +- Makefile | 3 +- builtin-tag.c | 450 ++++++++++++++++++++++ builtin.h | 1 + git-tag.sh => contrib/examples/git-tag.sh | 0 git.c | 1 + t/t7004-tag.sh | 88 ++++- 7 files changed, 538 insertions(+), 13 deletions(-) create mode 100644 builtin-tag.c rename git-tag.sh => contrib/examples/git-tag.sh (100%) diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt index aee2c1bdc7..119117f0bd 100644 --- a/Documentation/git-tag.txt +++ b/Documentation/git-tag.txt @@ -12,7 +12,7 @@ SYNOPSIS 'git-tag' [-a | -s | -u ] [-f] [-m | -F ] [] 'git-tag' -d ... 'git-tag' [-n []] -l [] -'git-tag' -v +'git-tag' -v ... DESCRIPTION ----------- @@ -23,7 +23,7 @@ Unless `-f` is given, the tag must not yet exist in If one of `-a`, `-s`, or `-u ` is passed, the command creates a 'tag' object, and requires the tag message. Unless -`-m ` is given, an editor is started for the user to type +`-m ` or `-F ` is given, an editor is started for the user to type in the tag message. Otherwise just the SHA1 object name of the commit object is @@ -59,15 +59,17 @@ OPTIONS Delete existing tags with the given names. -v:: - Verify the gpg signature of given the tag + Verify the gpg signature of the given tag names. -n :: specifies how many lines from the annotation, if any, are printed when using -l. The default is not to print any annotation lines. + If no number is given to `-n`, only the first line is printed. -l :: List tags with names that match the given pattern (or all if no pattern is given). + Typing "git tag" without arguments, also lists all tags. -m :: Use the given tag message (instead of prompting) diff --git a/Makefile b/Makefile index 73b487fba9..8db6646245 100644 --- a/Makefile +++ b/Makefile @@ -206,7 +206,7 @@ SCRIPT_SH = \ git-pull.sh git-rebase.sh git-rebase--interactive.sh \ git-repack.sh git-request-pull.sh git-reset.sh \ git-sh-setup.sh \ - git-tag.sh git-verify-tag.sh \ + git-verify-tag.sh \ git-am.sh \ git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \ git-merge-resolve.sh git-merge-ours.sh \ @@ -361,6 +361,7 @@ BUILTIN_OBJS = \ builtin-show-branch.o \ builtin-stripspace.o \ builtin-symbolic-ref.o \ + builtin-tag.o \ builtin-tar-tree.o \ builtin-unpack-objects.o \ builtin-update-index.o \ diff --git a/builtin-tag.c b/builtin-tag.c new file mode 100644 index 0000000000..507f51076d --- /dev/null +++ b/builtin-tag.c @@ -0,0 +1,450 @@ +/* + * Builtin "git tag" + * + * Copyright (c) 2007 Kristian Høgsberg , + * Carlos Rica + * Based on git-tag.sh and mktag.c by Linus Torvalds. + */ + +#include "cache.h" +#include "builtin.h" +#include "refs.h" +#include "tag.h" +#include "run-command.h" + +static const char builtin_tag_usage[] = + "git-tag [-n []] -l [] | [-a | -s | -u ] [-f | -d | -v] [-m | -F ] []"; + +static char signingkey[1000]; + +static void launch_editor(const char *path, char **buffer, unsigned long *len) +{ + const char *editor, *terminal; + struct child_process child; + const char *args[3]; + int fd; + + editor = getenv("VISUAL"); + if (!editor) + editor = getenv("EDITOR"); + + terminal = getenv("TERM"); + if (!editor && (!terminal || !strcmp(terminal, "dumb"))) { + fprintf(stderr, + "Terminal is dumb but no VISUAL nor EDITOR defined.\n" + "Please supply the message using either -m or -F option.\n"); + exit(1); + } + + if (!editor) + editor = "vi"; + + memset(&child, 0, sizeof(child)); + child.argv = args; + args[0] = editor; + args[1] = path; + args[2] = NULL; + + if (run_command(&child)) + die("There was a problem with the editor %s.", editor); + + fd = open(path, O_RDONLY); + if (fd < 0) + die("could not open '%s': %s", path, strerror(errno)); + if (read_fd(fd, buffer, len)) { + free(*buffer); + die("could not read message file '%s': %s", + path, strerror(errno)); + } + close(fd); +} + +struct tag_filter { + const char *pattern; + int lines; +}; + +#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" + +static int show_reference(const char *refname, const unsigned char *sha1, + int flag, void *cb_data) +{ + struct tag_filter *filter = cb_data; + + if (!fnmatch(filter->pattern, refname, 0)) { + int i; + unsigned long size; + enum object_type type; + char *buf, *sp, *eol; + size_t len; + + if (!filter->lines) { + printf("%s\n", refname); + return 0; + } + printf("%-15s ", refname); + + sp = buf = read_sha1_file(sha1, &type, &size); + if (!buf || !size) + return 0; + /* skip header */ + while (sp + 1 < buf + size && + !(sp[0] == '\n' && sp[1] == '\n')) + sp++; + /* only take up to "lines" lines, and strip the signature */ + for (i = 0, sp += 2; i < filter->lines && sp < buf + size && + prefixcmp(sp, PGP_SIGNATURE "\n"); + i++) { + if (i) + printf("\n "); + eol = memchr(sp, '\n', size - (sp - buf)); + len = eol ? eol - sp : size - (sp - buf); + fwrite(sp, len, 1, stdout); + if (!eol) + break; + sp = eol + 1; + } + putchar('\n'); + free(buf); + } + + return 0; +} + +static int list_tags(const char *pattern, int lines) +{ + struct tag_filter filter; + char *newpattern; + + if (pattern == NULL) + pattern = ""; + + /* prepend/append * to the shell pattern: */ + newpattern = xmalloc(strlen(pattern) + 3); + sprintf(newpattern, "*%s*", pattern); + + filter.pattern = newpattern; + filter.lines = lines; + + for_each_tag_ref(show_reference, (void *) &filter); + + free(newpattern); + + return 0; +} + +typedef int (*func_tag)(const char *name, const char *ref, + const unsigned char *sha1); + +static int do_tag_names(const char **argv, func_tag fn) +{ + const char **p; + char ref[PATH_MAX]; + int had_error = 0; + unsigned char sha1[20]; + + for (p = argv; *p; p++) { + if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p) + >= sizeof(ref)) { + error("tag name too long: %.*s...", 50, *p); + had_error = 1; + continue; + } + if (!resolve_ref(ref, sha1, 1, NULL)) { + error("tag '%s' not found.", *p); + had_error = 1; + continue; + } + if (fn(*p, ref, sha1)) + had_error = 1; + } + return had_error; +} + +static int delete_tag(const char *name, const char *ref, + const unsigned char *sha1) +{ + if (delete_ref(ref, sha1)) + return 1; + printf("Deleted tag '%s'\n", name); + return 0; +} + +static int verify_tag(const char *name, const char *ref, + const unsigned char *sha1) +{ + const char *argv_verify_tag[] = {"git-verify-tag", + "-v", "SHA1_HEX", NULL}; + argv_verify_tag[2] = sha1_to_hex(sha1); + + if (run_command_v_opt(argv_verify_tag, 0)) + return error("could not verify the tag '%s'", name); + return 0; +} + +static ssize_t do_sign(char *buffer, size_t size, size_t max) +{ + struct child_process gpg; + const char *args[4]; + char *bracket; + int len; + + if (!*signingkey) { + if (strlcpy(signingkey, git_committer_info(1), + sizeof(signingkey)) >= sizeof(signingkey)) + return error("committer info too long."); + bracket = strchr(signingkey, '>'); + if (bracket) + bracket[1] = '\0'; + } + + memset(&gpg, 0, sizeof(gpg)); + gpg.argv = args; + gpg.in = -1; + gpg.out = -1; + args[0] = "gpg"; + args[1] = "-bsau"; + args[2] = signingkey; + args[3] = NULL; + + if (start_command(&gpg)) + return error("could not run gpg."); + + write_or_die(gpg.in, buffer, size); + close(gpg.in); + gpg.close_in = 0; + len = read_in_full(gpg.out, buffer + size, max - size); + + finish_command(&gpg); + + if (len == max - size) + return error("could not read the entire signature from gpg."); + + return size + len; +} + +static const char tag_template[] = + "\n" + "#\n" + "# Write a tag message\n" + "#\n"; + +static int git_tag_config(const char *var, const char *value) +{ + if (!strcmp(var, "user.signingkey")) { + if (!value) + die("user.signingkey without value"); + if (strlcpy(signingkey, value, sizeof(signingkey)) + >= sizeof(signingkey)) + die("user.signingkey value too long"); + return 0; + } + + return git_default_config(var, value); +} + +#define MAX_SIGNATURE_LENGTH 1024 +/* message must be NULL or allocated, it will be reallocated and freed */ +static void create_tag(const unsigned char *object, const char *tag, + char *message, int sign, unsigned char *result) +{ + enum object_type type; + char header_buf[1024], *buffer; + int header_len, max_size; + unsigned long size; + + type = sha1_object_info(object, NULL); + if (type <= 0) + die("bad object type."); + + header_len = snprintf(header_buf, sizeof(header_buf), + "object %s\n" + "type %s\n" + "tag %s\n" + "tagger %s\n\n", + sha1_to_hex(object), + typename(type), + tag, + git_committer_info(1)); + + if (header_len >= sizeof(header_buf)) + die("tag header too big."); + + if (!message) { + char *path; + int fd; + + /* write the template message before editing: */ + path = xstrdup(git_path("TAG_EDITMSG")); + fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); + if (fd < 0) + die("could not create file '%s': %s", + path, strerror(errno)); + write_or_die(fd, tag_template, strlen(tag_template)); + close(fd); + + launch_editor(path, &buffer, &size); + + unlink(path); + free(path); + } + else { + buffer = message; + size = strlen(message); + } + + size = stripspace(buffer, size, 1); + + if (!message && !size) + die("no tag message?"); + + /* insert the header and add the '\n' if needed: */ + max_size = header_len + size + (sign ? MAX_SIGNATURE_LENGTH : 0) + 1; + buffer = xrealloc(buffer, max_size); + if (size) + buffer[size++] = '\n'; + memmove(buffer + header_len, buffer, size); + memcpy(buffer, header_buf, header_len); + size += header_len; + + if (sign) { + size = do_sign(buffer, size, max_size); + if (size < 0) + die("unable to sign the tag"); + } + + if (write_sha1_file(buffer, size, tag_type, result) < 0) + die("unable to write tag file"); + free(buffer); +} + +int cmd_tag(int argc, const char **argv, const char *prefix) +{ + unsigned char object[20], prev[20]; + int annotate = 0, sign = 0, force = 0, lines = 0; + char *message = NULL; + char ref[PATH_MAX]; + const char *object_ref, *tag; + int i; + struct ref_lock *lock; + + git_config(git_tag_config); + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + + if (arg[0] != '-') + break; + if (!strcmp(arg, "-a")) { + annotate = 1; + continue; + } + if (!strcmp(arg, "-s")) { + annotate = 1; + sign = 1; + continue; + } + if (!strcmp(arg, "-f")) { + force = 1; + continue; + } + if (!strcmp(arg, "-n")) { + if (i + 1 == argc || *argv[i + 1] == '-') + /* no argument */ + lines = 1; + else + lines = isdigit(*argv[++i]) ? + atoi(argv[i]) : 1; + continue; + } + if (!strcmp(arg, "-m")) { + annotate = 1; + i++; + if (i == argc) + die("option -m needs an argument."); + message = xstrdup(argv[i]); + continue; + } + if (!strcmp(arg, "-F")) { + unsigned long len; + int fd; + + annotate = 1; + i++; + if (i == argc) + die("option -F needs an argument."); + + if (!strcmp(argv[i], "-")) + fd = 0; + else { + fd = open(argv[i], O_RDONLY); + if (fd < 0) + die("could not open '%s': %s", + argv[i], strerror(errno)); + } + len = 1024; + message = xmalloc(len); + if (read_fd(fd, &message, &len)) { + free(message); + die("cannot read %s", argv[i]); + } + continue; + } + if (!strcmp(arg, "-u")) { + annotate = 1; + sign = 1; + i++; + if (i == argc) + die("option -u needs an argument."); + if (strlcpy(signingkey, argv[i], sizeof(signingkey)) + >= sizeof(signingkey)) + die("argument to option -u too long"); + continue; + } + if (!strcmp(arg, "-l")) { + return list_tags(argv[i + 1], lines); + } + if (!strcmp(arg, "-d")) { + return do_tag_names(argv + i + 1, delete_tag); + } + if (!strcmp(arg, "-v")) { + return do_tag_names(argv + i + 1, verify_tag); + } + usage(builtin_tag_usage); + } + + if (i == argc) { + if (annotate) + usage(builtin_tag_usage); + return list_tags(NULL, lines); + } + tag = argv[i++]; + + object_ref = i < argc ? argv[i] : "HEAD"; + if (i + 1 < argc) + die("too many params"); + + if (get_sha1(object_ref, object)) + die("Failed to resolve '%s' as a valid ref.", object_ref); + + if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) >= sizeof(ref)) + die("tag name too long: %.*s...", 50, tag); + if (check_ref_format(ref)) + die("'%s' is not a valid tag name.", tag); + + if (!resolve_ref(ref, prev, 1, NULL)) + hashclr(prev); + else if (!force) + die("tag '%s' already exists", tag); + + if (annotate) + create_tag(object, tag, message, sign, object); + + lock = lock_any_ref_for_update(ref, prev, 0); + if (!lock) + die("%s: cannot lock the ref", ref); + if (write_ref_sha1(lock, object, NULL) < 0) + die("%s: cannot update the ref", ref); + + return 0; +} diff --git a/builtin.h b/builtin.h index 4cc228dace..ac7417f7c5 100644 --- a/builtin.h +++ b/builtin.h @@ -70,6 +70,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix); extern int cmd_show_branch(int argc, const char **argv, const char *prefix); extern int cmd_stripspace(int argc, const char **argv, const char *prefix); extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix); +extern int cmd_tag(int argc, const char **argv, const char *prefix); extern int cmd_tar_tree(int argc, const char **argv, const char *prefix); extern int cmd_unpack_objects(int argc, const char **argv, const char *prefix); extern int cmd_update_index(int argc, const char **argv, const char *prefix); diff --git a/git-tag.sh b/contrib/examples/git-tag.sh similarity index 100% rename from git-tag.sh rename to contrib/examples/git-tag.sh diff --git a/git.c b/git.c index a647f9c61e..eb9e5ca972 100644 --- a/git.c +++ b/git.c @@ -363,6 +363,7 @@ static void handle_internal_command(int argc, const char **argv) { "show", cmd_show, RUN_SETUP | USE_PAGER }, { "stripspace", cmd_stripspace }, { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, + { "tag", cmd_tag, RUN_SETUP }, { "tar-tree", cmd_tar_tree }, { "unpack-objects", cmd_unpack_objects, RUN_SETUP }, { "update-index", cmd_update_index, RUN_SETUP }, diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index 17de2a90e6..a0be164619 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -5,7 +5,7 @@ test_description='git-tag -Basic tests for operations with tags.' +Tests for operations with tags.' . ./test-lib.sh @@ -16,11 +16,15 @@ tag_exists () { } # todo: git tag -l now returns always zero, when fixed, change this test -test_expect_success 'listing all tags in an empty tree should succeed' \ - 'git tag -l' +test_expect_success 'listing all tags in an empty tree should succeed' ' + git tag -l && + git tag +' -test_expect_success 'listing all tags in an empty tree should output nothing' \ - 'test `git-tag -l | wc -l` -eq 0' +test_expect_success 'listing all tags in an empty tree should output nothing' ' + test `git-tag -l | wc -l` -eq 0 && + test `git-tag | wc -l` -eq 0 +' test_expect_failure 'looking for a tag in an empty tree should fail' \ 'tag_exists mytag' @@ -49,11 +53,15 @@ test_expect_success 'creating a tag using default HEAD should succeed' ' git tag mytag ' -test_expect_success 'listing all tags if one exists should succeed' \ - 'git-tag -l' +test_expect_success 'listing all tags if one exists should succeed' ' + git-tag -l && + git-tag +' -test_expect_success 'listing all tags if one exists should output that tag' \ - 'test `git-tag -l` = mytag' +test_expect_success 'listing all tags if one exists should output that tag' ' + test `git-tag -l` = mytag && + test `git-tag` = mytag +' # pattern matching: @@ -165,6 +173,8 @@ test_expect_success 'listing all tags should print them ordered' ' git tag v1.0 && git tag t210 && git tag -l > actual && + git diff expect actual && + git tag > actual && git diff expect actual ' @@ -264,6 +274,10 @@ test_expect_failure \ 'trying to verify a non-annotated and non-signed tag should fail' \ 'git-tag -v non-annotated-tag' +test_expect_failure \ + 'trying to verify many non-annotated or unknown tags, should fail' \ + 'git-tag -v unknown-tag1 non-annotated-tag unknown-tag2' + # creating annotated tags: get_tag_msg () { @@ -306,6 +320,18 @@ test_expect_success \ git diff expect actual ' +cat >inputmsg <expect +cat inputmsg >>expect +test_expect_success 'creating an annotated tag with -F - should succeed' ' + git-tag -F - stdin-annotated-tag actual && + git diff expect actual +' + # blank and empty messages: get_tag_header empty-annotated-tag $commit commit $time >expect @@ -551,6 +577,12 @@ test_expect_success \ ! git-tag -v file-annotated-tag ' +test_expect_success \ + 'trying to verify two annotated non-signed tags should fail' ' + tag_exists annotated-tag file-annotated-tag && + ! git-tag -v annotated-tag file-annotated-tag +' + # creating and verifying signed tags: gpg --version >/dev/null @@ -589,9 +621,47 @@ test_expect_success 'creating a signed tag with -m message should succeed' ' git diff expect actual ' +cat >sigmsgfile <expect +cat sigmsgfile >>expect +echo '-----BEGIN PGP SIGNATURE-----' >>expect +test_expect_success \ + 'creating a signed tag with -F messagefile should succeed' ' + git-tag -s -F sigmsgfile file-signed-tag && + get_tag_msg file-signed-tag >actual && + git diff expect actual +' + +cat >siginputmsg <expect +cat siginputmsg >>expect +echo '-----BEGIN PGP SIGNATURE-----' >>expect +test_expect_success 'creating a signed tag with -F - should succeed' ' + git-tag -s -F - stdin-signed-tag actual && + git diff expect actual +' + test_expect_success 'verifying a signed tag should succeed' \ 'git-tag -v signed-tag' +test_expect_success 'verifying two signed tags in one command should succeed' \ + 'git-tag -v signed-tag file-signed-tag' + +test_expect_success \ + 'verifying many signed and non-signed tags should fail' ' + ! git-tag -v signed-tag annotated-tag && + ! git-tag -v file-annotated-tag file-signed-tag && + ! git-tag -v annotated-tag file-signed-tag file-annotated-tag && + ! git-tag -v signed-tag annotated-tag file-signed-tag +' + test_expect_success 'verifying a forged tag should fail' ' forged=$(git cat-file tag signed-tag | sed -e "s/signed-tag/forged-tag/" | From 4d87b9c5db2590f7616fedfc0a538fc78f528e14 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 20 Jul 2007 13:06:09 +0100 Subject: [PATCH 02/35] launch_editor(): Heed GIT_EDITOR and core.editor settings In the commit 'Add GIT_EDITOR environment and core.editor configuration variables', this was done for the shell scripts. Port it over to builtin-tag's version of launch_editor(), which is just about to be refactored into editor.c. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin-tag.c | 10 +++++++--- cache.h | 2 ++ config.c | 5 +++++ environment.c | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/builtin-tag.c b/builtin-tag.c index 507f51076d..81d37ce901 100644 --- a/builtin-tag.c +++ b/builtin-tag.c @@ -24,7 +24,11 @@ static void launch_editor(const char *path, char **buffer, unsigned long *len) const char *args[3]; int fd; - editor = getenv("VISUAL"); + editor = getenv("GIT_EDITOR"); + if (!editor && editor_program) + editor = editor_program; + if (!editor) + editor = getenv("VISUAL"); if (!editor) editor = getenv("EDITOR"); @@ -249,9 +253,9 @@ static void create_tag(const unsigned char *object, const char *tag, char *message, int sign, unsigned char *result) { enum object_type type; - char header_buf[1024], *buffer; + char header_buf[1024], *buffer = NULL; int header_len, max_size; - unsigned long size; + unsigned long size = 0; type = sha1_object_info(object, NULL); if (type <= 0) diff --git a/cache.h b/cache.h index 53801b8089..67b1af16d9 100644 --- a/cache.h +++ b/cache.h @@ -560,6 +560,8 @@ extern char *pager_program; extern int pager_in_use; extern int pager_use_color; +extern char *editor_program; + /* base85 */ int decode_85(char *dst, const char *line, int linelen); void encode_85(char *buf, const unsigned char *data, int bytes); diff --git a/config.c b/config.c index f89a611915..103b4fcc61 100644 --- a/config.c +++ b/config.c @@ -426,6 +426,11 @@ int git_default_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.editor")) { + editor_program = xstrdup(value); + return 0; + } + /* Add other config variables here and to Documentation/config.txt. */ return 0; } diff --git a/environment.c b/environment.c index f83fb9e448..35d3f4b595 100644 --- a/environment.c +++ b/environment.c @@ -33,6 +33,7 @@ size_t delta_base_cache_limit = 16 * 1024 * 1024; char *pager_program; int pager_in_use; int pager_use_color = 1; +char *editor_program; int auto_crlf = 0; /* 1: both ways, -1: only when adding git objects */ static const char *git_dir; From e317cfafd247b279055e9ee64a6a982043bd06e7 Mon Sep 17 00:00:00 2001 From: Carlos Rica Date: Sat, 21 Jul 2007 14:13:12 +0200 Subject: [PATCH 03/35] builtin-tag.c: Fix two memory leaks and minor notation changes. A repeated call to read_sha1_file was not freing memory when the buffer was allocated but returned size was zero. Also, now the program does not allow many -F or -m options, which was a bug too because it was not freing the memory allocated for any previous -F or -m options. Tests are provided for ensuring that only one option -F or -m is given. Also, another test is shipped here, to check that "git tag" fails when a non-existing file is passed to the -F option, something that git-tag.sh allowed creating the tag with an empty message. Signed-off-by: Carlos Rica Acked-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin-tag.c | 38 ++++++++++++++++++++++---------------- t/t7004-tag.sh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/builtin-tag.c b/builtin-tag.c index 81d37ce901..d6d38ad123 100644 --- a/builtin-tag.c +++ b/builtin-tag.c @@ -89,14 +89,19 @@ static int show_reference(const char *refname, const unsigned char *sha1, printf("%-15s ", refname); sp = buf = read_sha1_file(sha1, &type, &size); - if (!buf || !size) + if (!buf) return 0; + if (!size) { + free(buf); + return 0; + } /* skip header */ while (sp + 1 < buf + size && !(sp[0] == '\n' && sp[1] == '\n')) sp++; /* only take up to "lines" lines, and strip the signature */ - for (i = 0, sp += 2; i < filter->lines && sp < buf + size && + for (i = 0, sp += 2; + i < filter->lines && sp < buf + size && prefixcmp(sp, PGP_SIGNATURE "\n"); i++) { if (i) @@ -137,10 +142,10 @@ static int list_tags(const char *pattern, int lines) return 0; } -typedef int (*func_tag)(const char *name, const char *ref, +typedef int (*each_tag_name_fn)(const char *name, const char *ref, const unsigned char *sha1); -static int do_tag_names(const char **argv, func_tag fn) +static int for_each_tag_name(const char **argv, each_tag_name_fn fn) { const char **p; char ref[PATH_MAX]; @@ -195,7 +200,7 @@ static ssize_t do_sign(char *buffer, size_t size, size_t max) if (!*signingkey) { if (strlcpy(signingkey, git_committer_info(1), - sizeof(signingkey)) >= sizeof(signingkey)) + sizeof(signingkey)) > sizeof(signingkey) - 1) return error("committer info too long."); bracket = strchr(signingkey, '>'); if (bracket) @@ -258,7 +263,7 @@ static void create_tag(const unsigned char *object, const char *tag, unsigned long size = 0; type = sha1_object_info(object, NULL); - if (type <= 0) + if (type <= OBJ_NONE) die("bad object type."); header_len = snprintf(header_buf, sizeof(header_buf), @@ -271,7 +276,7 @@ static void create_tag(const unsigned char *object, const char *tag, tag, git_committer_info(1)); - if (header_len >= sizeof(header_buf)) + if (header_len > sizeof(header_buf) - 1) die("tag header too big."); if (!message) { @@ -366,6 +371,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix) i++; if (i == argc) die("option -m needs an argument."); + if (message) + die("only one -F or -m option is allowed."); message = xstrdup(argv[i]); continue; } @@ -377,6 +384,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix) i++; if (i == argc) die("option -F needs an argument."); + if (message) + die("only one -F or -m option is allowed."); if (!strcmp(argv[i], "-")) fd = 0; @@ -405,15 +414,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix) die("argument to option -u too long"); continue; } - if (!strcmp(arg, "-l")) { + if (!strcmp(arg, "-l")) return list_tags(argv[i + 1], lines); - } - if (!strcmp(arg, "-d")) { - return do_tag_names(argv + i + 1, delete_tag); - } - if (!strcmp(arg, "-v")) { - return do_tag_names(argv + i + 1, verify_tag); - } + if (!strcmp(arg, "-d")) + return for_each_tag_name(argv + i + 1, delete_tag); + if (!strcmp(arg, "-v")) + return for_each_tag_name(argv + i + 1, verify_tag); usage(builtin_tag_usage); } @@ -431,7 +437,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) if (get_sha1(object_ref, object)) die("Failed to resolve '%s' as a valid ref.", object_ref); - if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) >= sizeof(ref)) + if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1) die("tag name too long: %.*s...", 50, tag); if (check_ref_format(ref)) die("'%s' is not a valid tag name.", tag); diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index a0be164619..c4fa4461f7 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -332,6 +332,33 @@ test_expect_success 'creating an annotated tag with -F - should succeed' ' git diff expect actual ' +test_expect_success \ + 'trying to create a tag with a non-existing -F file should fail' ' + ! test -f nonexistingfile && + ! tag_exists notag && + ! git-tag -F nonexistingfile notag && + ! tag_exists notag +' + +test_expect_success \ + 'trying to create tags giving many -m or -F options should fail' ' + echo "message file 1" >msgfile1 && + echo "message file 2" >msgfile2 && + ! tag_exists msgtag && + ! git-tag -m "message 1" -m "message 2" msgtag && + ! tag_exists msgtag && + ! git-tag -F msgfile1 -F msgfile2 msgtag && + ! tag_exists msgtag && + ! git-tag -m "message 1" -F msgfile1 msgtag && + ! tag_exists msgtag && + ! git-tag -F msgfile1 -m "message 1" msgtag && + ! tag_exists msgtag && + ! git-tag -F msgfile1 -m "message 1" -F msgfile2 msgtag && + ! tag_exists msgtag && + ! git-tag -m "message 1" -F msgfile1 -m "message 2" msgtag && + ! tag_exists msgtag +' + # blank and empty messages: get_tag_header empty-annotated-tag $commit commit $time >expect @@ -648,6 +675,14 @@ test_expect_success 'creating a signed tag with -F - should succeed' ' git diff expect actual ' +test_expect_success \ + 'trying to create a signed tag with non-existing -F file should fail' ' + ! test -f nonexistingfile && + ! tag_exists nosigtag && + ! git-tag -s -F nonexistingfile nosigtag && + ! tag_exists nosigtag +' + test_expect_success 'verifying a signed tag should succeed' \ 'git-tag -v signed-tag' From 2ae68fcb785a617793813abcea19893e13e436b0 Mon Sep 17 00:00:00 2001 From: Carlos Rica Date: Fri, 27 Jul 2007 06:07:34 +0200 Subject: [PATCH 04/35] Make verify-tag a builtin. This replaces "git-verify-tag.sh" with "builtin-verify-tag.c". Testing relies on the "git tag -v" tests calling this command. A temporary file is needed when calling to gpg, because git is already creating detached signatures (gpg option -b) to sign tags (instead of leaving gpg to add the signature to the file by itself), and those signatures need to be supplied in a separate file to be verified by gpg. The program uses git_mkstemp to create that temporary file needed by gpg, instead of the previously used "$GIT_DIR/.tmp-vtag", in order to allow the command to be used in read-only repositories, and also prevent other instances of git to read or remove the same file. Signal SIGPIPE is ignored because the program sometimes was terminated because that signal when writing the input for gpg. The command now can receive many tag names to be verified. Documentation is also updated here to reflect this new behaviour. Signed-off-by: Carlos Rica Signed-off-by: Junio C Hamano --- Documentation/git-verify-tag.txt | 4 +- Makefile | 2 +- builtin-verify-tag.c | 110 ++++++++++++++++++ builtin.h | 1 + .../examples/git-verify-tag.sh | 0 git.c | 1 + 6 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 builtin-verify-tag.c rename git-verify-tag.sh => contrib/examples/git-verify-tag.sh (100%) diff --git a/Documentation/git-verify-tag.txt b/Documentation/git-verify-tag.txt index 48d17fd9c4..ac7fb19154 100644 --- a/Documentation/git-verify-tag.txt +++ b/Documentation/git-verify-tag.txt @@ -3,11 +3,11 @@ git-verify-tag(1) NAME ---- -git-verify-tag - Check the GPG signature of tag +git-verify-tag - Check the GPG signature of tags SYNOPSIS -------- -'git-verify-tag' +'git-verify-tag' ... DESCRIPTION ----------- diff --git a/Makefile b/Makefile index 8db6646245..98670bbd71 100644 --- a/Makefile +++ b/Makefile @@ -206,7 +206,6 @@ SCRIPT_SH = \ git-pull.sh git-rebase.sh git-rebase--interactive.sh \ git-repack.sh git-request-pull.sh git-reset.sh \ git-sh-setup.sh \ - git-verify-tag.sh \ git-am.sh \ git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \ git-merge-resolve.sh git-merge-ours.sh \ @@ -368,6 +367,7 @@ BUILTIN_OBJS = \ builtin-update-ref.o \ builtin-upload-archive.o \ builtin-verify-pack.o \ + builtin-verify-tag.o \ builtin-write-tree.o \ builtin-show-ref.o \ builtin-pack-refs.o diff --git a/builtin-verify-tag.c b/builtin-verify-tag.c new file mode 100644 index 0000000000..dfcfcd0455 --- /dev/null +++ b/builtin-verify-tag.c @@ -0,0 +1,110 @@ +/* + * Builtin "git verify-tag" + * + * Copyright (c) 2007 Carlos Rica + * + * Based on git-verify-tag.sh + */ +#include "cache.h" +#include "builtin.h" +#include "tag.h" +#include "run-command.h" +#include + +static const char builtin_verify_tag_usage[] = + "git-verify-tag [-v|--verbose] ..."; + +#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" + +static int run_gpg_verify(const char *buf, unsigned long size, int verbose) +{ + struct child_process gpg; + const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL}; + char path[PATH_MAX], *eol; + size_t len; + int fd, ret; + + fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX"); + if (fd < 0) + return error("could not create temporary file '%s': %s", + path, strerror(errno)); + if (write_in_full(fd, buf, size) < 0) + return error("failed writing temporary file '%s': %s", + path, strerror(errno)); + close(fd); + + /* find the length without signature */ + len = 0; + while (len < size && prefixcmp(buf + len, PGP_SIGNATURE "\n")) { + eol = memchr(buf + len, '\n', size - len); + len += eol ? eol - (buf + len) + 1 : size - len; + } + if (verbose) + write_in_full(1, buf, len); + + memset(&gpg, 0, sizeof(gpg)); + gpg.argv = args_gpg; + gpg.in = -1; + gpg.out = 1; + args_gpg[2] = path; + if (start_command(&gpg)) + return error("could not run gpg."); + + write_in_full(gpg.in, buf, len); + close(gpg.in); + gpg.close_in = 0; + ret = finish_command(&gpg); + + unlink(path); + + return ret; +} + +static int verify_tag(const char *name, int verbose) +{ + enum object_type type; + unsigned char sha1[20]; + char *buf; + unsigned long size; + int ret; + + if (get_sha1(name, sha1)) + return error("tag '%s' not found.", name); + + type = sha1_object_info(sha1, NULL); + if (type != OBJ_TAG) + return error("%s: cannot verify a non-tag object of type %s.", + name, typename(type)); + + buf = read_sha1_file(sha1, &type, &size); + if (!buf) + return error("%s: unable to read file.", name); + + ret = run_gpg_verify(buf, size, verbose); + + free(buf); + return ret; +} + +int cmd_verify_tag(int argc, const char **argv, const char *prefix) +{ + int i = 1, verbose = 0, had_error = 0; + + git_config(git_default_config); + + if (argc == 1) + usage(builtin_verify_tag_usage); + + if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")) { + verbose = 1; + i++; + } + + /* sometimes the program was terminated because this signal + * was received in the process of writing the gpg input: */ + signal(SIGPIPE, SIG_IGN); + while (i < argc) + if (verify_tag(argv[i++], verbose)) + had_error = 1; + return had_error; +} diff --git a/builtin.h b/builtin.h index ac7417f7c5..bb720004af 100644 --- a/builtin.h +++ b/builtin.h @@ -77,6 +77,7 @@ extern int cmd_update_index(int argc, const char **argv, const char *prefix); extern int cmd_update_ref(int argc, const char **argv, const char *prefix); extern int cmd_upload_archive(int argc, const char **argv, const char *prefix); extern int cmd_upload_tar(int argc, const char **argv, const char *prefix); +extern int cmd_verify_tag(int argc, const char **argv, const char *prefix); extern int cmd_version(int argc, const char **argv, const char *prefix); extern int cmd_whatchanged(int argc, const char **argv, const char *prefix); extern int cmd_write_tree(int argc, const char **argv, const char *prefix); diff --git a/git-verify-tag.sh b/contrib/examples/git-verify-tag.sh similarity index 100% rename from git-verify-tag.sh rename to contrib/examples/git-verify-tag.sh diff --git a/git.c b/git.c index eb9e5ca972..230e50611f 100644 --- a/git.c +++ b/git.c @@ -369,6 +369,7 @@ static void handle_internal_command(int argc, const char **argv) { "update-index", cmd_update_index, RUN_SETUP }, { "update-ref", cmd_update_ref, RUN_SETUP }, { "upload-archive", cmd_upload_archive }, + { "verify-tag", cmd_verify_tag, RUN_SETUP }, { "version", cmd_version }, { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER }, { "write-tree", cmd_write_tree, RUN_SETUP }, From f653aee5a37b909e772d612eb7e226f09fd2f3d3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 23 Jul 2007 12:58:27 +0100 Subject: [PATCH 05/35] Teach "git stripspace" the --strip-comments option With --strip-comments (or short -s), git stripspace now removes lines beginning with a '#', too. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- Documentation/git-stripspace.txt | 5 ++++- builtin-stripspace.c | 7 ++++++- t/t0030-stripspace.sh | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Documentation/git-stripspace.txt b/Documentation/git-stripspace.txt index 1306d7bab7..5212358306 100644 --- a/Documentation/git-stripspace.txt +++ b/Documentation/git-stripspace.txt @@ -8,7 +8,7 @@ git-stripspace - Filter out empty lines SYNOPSIS -------- -'git-stripspace' < +'git-stripspace' [-s | --strip-comments] < DESCRIPTION ----------- @@ -16,6 +16,9 @@ Remove multiple empty lines, and empty lines at beginning and end. OPTIONS ------- +-s\|--strip-comments:: + In addition to empty lines, also strip lines starting with '#'. + :: Byte stream to act on. diff --git a/builtin-stripspace.c b/builtin-stripspace.c index 55716873dc..916355ca5d 100644 --- a/builtin-stripspace.c +++ b/builtin-stripspace.c @@ -76,6 +76,11 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix) { char *buffer; unsigned long size; + int strip_comments = 0; + + if (argc > 1 && (!strcmp(argv[1], "-s") || + !strcmp(argv[1], "--strip-comments"))) + strip_comments = 1; size = 1024; buffer = xmalloc(size); @@ -84,7 +89,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix) die("could not read the input"); } - size = stripspace(buffer, size, 0); + size = stripspace(buffer, size, strip_comments); write_or_die(1, buffer, size); if (size) putc('\n', stdout); diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh index b1c900379b..cad95f35ad 100755 --- a/t/t0030-stripspace.sh +++ b/t/t0030-stripspace.sh @@ -392,4 +392,9 @@ test_expect_success \ git diff expect actual ' +test_expect_success 'strip comments, too' ' + test ! -z "$(echo "# comment" | git stripspace)" && + test -z "$(echo "# comment" | git stripspace -s)" +' + test_done From 72a4f4b657846af6c65360d92aa09d8b7f3dfbb0 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Wed, 1 Aug 2007 10:03:37 -0700 Subject: [PATCH 06/35] connect: accept file:// URL scheme We might make it something like: "if you use an url, we don't default to local", so the difference would be that git clone file:///directory/to/repo would work the way it does now, but git clone /directory/to/repo would default to "-l" behaviour. That kind of would make sense (and should be easy to implement. This adds support for "file://" URL to underlying connect codepath to make it happen. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- connect.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/connect.c b/connect.c index 715cdc0223..ae49c5a367 100644 --- a/connect.c +++ b/connect.c @@ -145,6 +145,8 @@ static enum protocol get_protocol(const char *name) return PROTO_SSH; if (!strcmp(name, "ssh+git")) return PROTO_SSH; + if (!strcmp(name, "file")) + return PROTO_LOCAL; die("I don't handle protocol '%s'", name); } @@ -498,13 +500,13 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags) end = host; path = strchr(end, c); - if (c == ':') { - if (path) { + if (path) { + if (c == ':') { protocol = PROTO_SSH; *path++ = '\0'; - } else - path = host; - } + } + } else + path = end; if (!path || !*path) die("No path specified. See 'man git-pull' for valid url syntax"); From 3d5c418ff56645e13bdbd8c9f7d84fdaf7c8494b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 1 Aug 2007 23:42:36 -0700 Subject: [PATCH 07/35] git-clone: aggressively optimize local clone behaviour. This changes the behaviour of cloning from a repository on the local machine, by defaulting to "-l" (use hardlinks to share files under .git/objects) and making "-l" a no-op. A new option, --no-hardlinks, is also added to cause file-level copy of files under .git/objects while still avoiding the normal "pack to pipe, then receive and index pack" network transfer overhead. The old behaviour of local cloning without -l nor -s is availble by specifying the source repository with the newly introduced file:///path/to/repo.git/ syntax (i.e. "same as network" cloning). * With --no-hardlinks (i.e. have all .git/objects/ copied via cpio) would not catch the source repository corruption, and also risks corrupted recipient repository if an alpha-particle hits memory cell while indexing and resolving deltas. As long as the recipient is created uncorrupted, you have a good back-up. * same-as-network is expensive, but it would catch the breakage of the source repository. It still risks corrupted recipient repository due to hardware failure. As long as the recipient is created uncorrupted, you have a good back-up. * The new default on the same filesystem, as long as the source repository is healthy, it is very likely that the recipient would be, too. Also it is very cheap. You do not get any back-up benefit, though. None of the method is resilient against the source repository corruption, so let's discount that from the comparison. Then the difference with and without --no-hardlinks matters primarily if you value the back-up benefit or not. If you want to use the cloned repository as a back-up, then it is cheaper to do a clone with --no-hardlinks and two git-fsck (source before clone, recipient after clone) than same-as-network clone, especially as you are likely to do a git-fsck on the recipient if you are so paranoid anyway. Which leads me to believe that being able to use file:/// is probably a good idea, if only for testability, but probably of little practical value. We default to hardlinked clone for everyday use, and paranoids can use --no-hardlinks as a way to make a back-up. Signed-off-by: Junio C Hamano --- Documentation/git-clone.txt | 18 +++++++++-- Documentation/urls.txt | 16 ++++++---- git-clone.sh | 64 ++++++++++++++++++++----------------- t/t5500-fetch-pack.sh | 2 +- t/t5700-clone-reference.sh | 2 +- t/t5701-clone-local.sh | 17 ++++++++++ 6 files changed, 79 insertions(+), 40 deletions(-) diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt index a0a10e3e26..227f092e26 100644 --- a/Documentation/git-clone.txt +++ b/Documentation/git-clone.txt @@ -9,7 +9,8 @@ git-clone - Clone a repository into a new directory SYNOPSIS -------- [verse] -'git-clone' [--template=] [-l [-s]] [-q] [-n] [--bare] +'git-clone' [--template=] + [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [-o ] [-u ] [--reference ] [--depth ] [] @@ -40,8 +41,19 @@ OPTIONS this flag bypasses normal "git aware" transport mechanism and clones the repository by making a copy of HEAD and everything under objects and refs directories. - The files under .git/objects/ directory are hardlinked - to save space when possible. + The files under `.git/objects/` directory are hardlinked + to save space when possible. This is now the default when + the source repository is specified with `/path/to/repo` + syntax, so it essentially is a no-op option. To force + copying instead of hardlinking (which may be desirable + if you are trying to make a back-up of your repository), + but still avoid the usual "git aware" transport + mechanism, `--no-hardlinks` can be used. + +--no-hardlinks:: + Optimize the cloning process from a repository on a + local filesystem by copying files under `.git/objects` + directory. --shared:: -s:: diff --git a/Documentation/urls.txt b/Documentation/urls.txt index 781df4174b..b38145faff 100644 --- a/Documentation/urls.txt +++ b/Documentation/urls.txt @@ -15,11 +15,11 @@ to name the remote repository: - ssh://{startsb}user@{endsb}host.xz/~/path/to/repo.git =============================================================== -SSH is the default transport protocol. You can optionally specify -which user to log-in as, and an alternate, scp-like syntax is also -supported. Both syntaxes support username expansion, -as does the native git protocol. The following three are -identical to the last three above, respectively: +SSH is the default transport protocol over the network. You can +optionally specify which user to log-in as, and an alternate, +scp-like syntax is also supported. Both syntaxes support +username expansion, as does the native git protocol. The following +three are identical to the last three above, respectively: =============================================================== - {startsb}user@{endsb}host.xz:/path/to/repo.git/ @@ -27,8 +27,12 @@ identical to the last three above, respectively: - {startsb}user@{endsb}host.xz:path/to/repo.git =============================================================== -To sync with a local directory, use: +To sync with a local directory, you can use: =============================================================== - /path/to/repo.git/ +- file:///path/to/repo.git/ =============================================================== + +They are mostly equivalent, except when cloning. See +gitlink:git-clone[1] for details. diff --git a/git-clone.sh b/git-clone.sh index 09225540e6..4c9b1c9710 100755 --- a/git-clone.sh +++ b/git-clone.sh @@ -87,7 +87,7 @@ Perhaps git-update-server-info needs to be run there?" quiet= local=no -use_local=no +use_local_hardlink=yes local_shared=no unset template no_checkout= @@ -108,9 +108,13 @@ while no_checkout=yes ;; *,--na|*,--nak|*,--nake|*,--naked|\ *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;; - *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;; + *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) + use_local_hardlink=yes ;; + *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\ + *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks) + use_local_hardlink=no ;; *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) - local_shared=yes; use_local=yes ;; + local_shared=yes; ;; 1,--template) usage ;; *,--template) shift; template="--template=$1" ;; @@ -249,34 +253,36 @@ fi rm -f "$GIT_DIR/CLONE_HEAD" # We do local magic only when the user tells us to. -case "$local,$use_local" in -yes,yes) +case "$local" in +yes) ( cd "$repo/objects" ) || - die "-l flag seen but repository '$repo' is not local." + die "cannot chdir to local '$repo/objects'." - case "$local_shared" in - no) - # See if we can hardlink and drop "l" if not. - sample_file=$(cd "$repo" && \ - find objects -type f -print | sed -e 1q) - - # objects directory should not be empty since we are cloning! - test -f "$repo/$sample_file" || exit - - l= - if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null - then - l=l - fi && - rm -f "$GIT_DIR/objects/sample" && - cd "$repo" && - find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1 - ;; - yes) - mkdir -p "$GIT_DIR/objects/info" - echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates" - ;; - esac + if test "$local_shared" = yes + then + mkdir -p "$GIT_DIR/objects/info" + echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates" + else + l= && + if test "$use_local_hardlink" = yes + then + # See if we can hardlink and drop "l" if not. + sample_file=$(cd "$repo" && \ + find objects -type f -print | sed -e 1q) + # objects directory should not be empty because + # we are cloning! + test -f "$repo/$sample_file" || exit + if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null + then + rm -f "$GIT_DIR/objects/sample" + l=l + else + echo >&2 "Warning: -l asked but cannot hardlink to $repo" + fi + fi && + cd "$repo" && + find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1 + fi git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1 ;; *) diff --git a/t/t5500-fetch-pack.sh b/t/t5500-fetch-pack.sh index 7da515361a..7b6798d8b5 100755 --- a/t/t5500-fetch-pack.sh +++ b/t/t5500-fetch-pack.sh @@ -129,7 +129,7 @@ pull_to_client 2nd "B" $((64*3)) pull_to_client 3rd "A" $((1*3)) # old fails -test_expect_success "clone shallow" "git-clone --depth 2 . shallow" +test_expect_success "clone shallow" "git-clone --depth 2 file://`pwd`/. shallow" (cd shallow; git count-objects -v) > count.shallow diff --git a/t/t5700-clone-reference.sh b/t/t5700-clone-reference.sh index 6d43252593..4e93aaab02 100755 --- a/t/t5700-clone-reference.sh +++ b/t/t5700-clone-reference.sh @@ -51,7 +51,7 @@ diff expected current' cd "$base_dir" test_expect_success 'cloning with reference (no -l -s)' \ -'git clone --reference B A D' +'git clone --reference B file://`pwd`/A D' cd "$base_dir" diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh index b0933274db..a3026ec4fc 100755 --- a/t/t5701-clone-local.sh +++ b/t/t5701-clone-local.sh @@ -43,4 +43,21 @@ test_expect_success 'local clone from x.git that does not exist' ' fi ' +test_expect_success 'With -no-hardlinks, local will make a copy' ' + cd "$D" && + git clone --bare --no-hardlinks x w && + cd w && + linked=$(find objects -type f ! -links 1 | wc -l) && + test "$linked" = 0 +' + +test_expect_success 'Even without -l, local will make a hardlink' ' + cd "$D" && + rm -fr w && + git clone -l --bare x w && + cd w && + copied=$(find objects -type f -links 1 | wc -l) && + test "$copied" = 0 +' + test_done From aec2196a671de8ef6ba38622869dc862c971df17 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 8 Aug 2007 13:41:46 -0700 Subject: [PATCH 08/35] Reorder the list of commands in the manual. The basic idea was proposed by Steve Hoelzer; in order to make the list easier to search, we keep the command list in the script that generates it with "sort -d". Signed-off-by: Junio C Hamano --- Documentation/cmd-list.perl | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Documentation/cmd-list.perl b/Documentation/cmd-list.perl index 2143995ece..4ee76eaf99 100755 --- a/Documentation/cmd-list.perl +++ b/Documentation/cmd-list.perl @@ -68,6 +68,8 @@ for my $cat (qw(ancillaryinterrogators } } +# The following list is sorted with "sort -d" to make it easier +# to find entry in the resulting git.html manual page. __DATA__ git-add mainporcelain git-am mainporcelain @@ -80,9 +82,9 @@ git-blame ancillaryinterrogators git-branch mainporcelain git-bundle mainporcelain git-cat-file plumbinginterrogators -git-checkout-index plumbingmanipulators -git-checkout mainporcelain git-check-attr purehelpers +git-checkout mainporcelain +git-checkout-index plumbingmanipulators git-check-ref-format purehelpers git-cherry ancillaryinterrogators git-cherry-pick mainporcelain @@ -91,6 +93,7 @@ git-clean mainporcelain git-clone mainporcelain git-commit mainporcelain git-commit-tree plumbingmanipulators +git-config ancillarymanipulators git-convert-objects ancillarymanipulators git-count-objects ancillaryinterrogators git-cvsexportcommit foreignscminterface @@ -98,9 +101,9 @@ git-cvsimport foreignscminterface git-cvsserver foreignscminterface git-daemon synchingrepositories git-describe mainporcelain +git-diff mainporcelain git-diff-files plumbinginterrogators git-diff-index plumbinginterrogators -git-diff mainporcelain git-diff-tree plumbinginterrogators git-fast-import ancillarymanipulators git-fetch mainporcelain @@ -130,13 +133,13 @@ git-ls-remote plumbinginterrogators git-ls-tree plumbinginterrogators git-mailinfo purehelpers git-mailsplit purehelpers +git-merge mainporcelain git-merge-base plumbinginterrogators git-merge-file plumbingmanipulators git-merge-index plumbingmanipulators -git-merge mainporcelain git-merge-one-file purehelpers -git-merge-tree ancillaryinterrogators git-mergetool ancillarymanipulators +git-merge-tree ancillaryinterrogators git-mktag plumbingmanipulators git-mktree plumbingmanipulators git-mv mainporcelain @@ -157,9 +160,8 @@ git-rebase mainporcelain git-receive-pack synchelpers git-reflog ancillarymanipulators git-relink ancillarymanipulators -git-repack ancillarymanipulators -git-config ancillarymanipulators git-remote ancillarymanipulators +git-repack ancillarymanipulators git-request-pull foreignscminterface git-rerere ancillaryinterrogators git-reset mainporcelain From ea99c3ae0e2cecaa0950532385319d60c59e97e0 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 8 Aug 2007 17:06:55 +0200 Subject: [PATCH 09/35] git-p4: Fix git-p4 submit to include only changed files in the perforce submit template. Parse the files section in the "p4 change -o" output and remove lines with file changes in unrelated depot paths. Signed-off-by: Simon Hausmann Signed-off-by: Marius Storm-Olsen Signed-off-by: Junio C Hamano --- contrib/fast-import/git-p4 | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4 index 3cbb2da221..805d632a68 100755 --- a/contrib/fast-import/git-p4 +++ b/contrib/fast-import/git-p4 @@ -390,6 +390,30 @@ class P4Submit(Command): return result + def prepareSubmitTemplate(self): + # remove lines in the Files section that show changes to files outside the depot path we're committing into + template = "" + inFilesSection = False + for line in read_pipe_lines("p4 change -o"): + if inFilesSection: + if line.startswith("\t"): + # path starts and ends with a tab + path = line[1:] + lastTab = path.rfind("\t") + if lastTab != -1: + path = path[:lastTab] + if not path.startswith(self.depotPath): + continue + else: + inFilesSection = False + else: + if line.startswith("Files:"): + inFilesSection = True + + template += line + + return template + def applyCommit(self, id): if self.directSubmit: print "Applying local change in working directory/index" @@ -467,7 +491,7 @@ class P4Submit(Command): logMessage = logMessage.replace("\n", "\r\n") logMessage = logMessage.strip() - template = read_pipe("p4 change -o") + template = self.prepareSubmitTemplate() if self.interactive: submitTemplate = self.prepareLogMessage(template, logMessage) @@ -558,24 +582,24 @@ class P4Submit(Command): return False [upstream, settings] = findUpstreamBranchPoint() - depotPath = settings['depot-paths'][0] + self.depotPath = settings['depot-paths'][0] if len(self.origin) == 0: self.origin = upstream if self.verbose: print "Origin branch is " + self.origin - if len(depotPath) == 0: + if len(self.depotPath) == 0: print "Internal error: cannot locate perforce depot path from existing branches" sys.exit(128) - self.clientPath = p4Where(depotPath) + self.clientPath = p4Where(self.depotPath) if len(self.clientPath) == 0: - print "Error: Cannot locate perforce checkout of %s in client view" % depotPath + print "Error: Cannot locate perforce checkout of %s in client view" % self.depotPath sys.exit(128) - print "Perforce checkout for depot path %s located at %s" % (depotPath, self.clientPath) + print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath) self.oldWorkingDirectory = os.getcwd() if self.directSubmit: From b50be1d80f4c151447d2ac96eaeb1c4d76ed4ef5 Mon Sep 17 00:00:00 2001 From: Brian Downing Date: Wed, 8 Aug 2007 23:26:10 -0500 Subject: [PATCH 10/35] cvsserver: Fix for work trees git-cvsserver used checkout-index internally for commit and annotate. Since a work tree is required for this to function now, this was breaking. Work around this by defining GIT_WORK_TREE=. in the appropriate places. Signed-off-by: Brian Downing Signed-off-by: Junio C Hamano --- git-cvsserver.perl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/git-cvsserver.perl b/git-cvsserver.perl index ae7d511589..13dbd27a80 100755 --- a/git-cvsserver.perl +++ b/git-cvsserver.perl @@ -1196,6 +1196,7 @@ sub req_ci $log->info("Lockless commit start, basing commit on '$tmpdir', index file is '$file_index'"); $ENV{GIT_DIR} = $state->{CVSROOT} . "/"; + $ENV{GIT_WORK_TREE} = "."; $ENV{GIT_INDEX_FILE} = $file_index; # Remember where the head was at the beginning. @@ -1721,6 +1722,7 @@ sub req_annotate $log->info("Temp checkoutdir creation successful, basing annotate session work on '$tmpdir', index file is '$file_index'"); $ENV{GIT_DIR} = $state->{CVSROOT} . "/"; + $ENV{GIT_WORK_TREE} = "."; $ENV{GIT_INDEX_FILE} = $file_index; chdir $tmpdir; From 3955d994dec590fb7c586b35094d25af92065c9d Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 10 Aug 2007 00:47:28 -0700 Subject: [PATCH 11/35] Fix formatting of git-blame documentation. blame-options.txt did not format multi-paragraph option description correctly. Signed-off-by: Junio C Hamano --- Documentation/blame-options.txt | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Documentation/blame-options.txt b/Documentation/blame-options.txt index a46bf6ce70..17379f0576 100644 --- a/Documentation/blame-options.txt +++ b/Documentation/blame-options.txt @@ -64,11 +64,11 @@ of lines before or after the line given by . assigns blame to the lines that were moved down (i.e. A) to the child commit. With this option, both groups of lines are blamed on the parent. - - is optional but it is the lower bound on the number of - alphanumeric characters that git must detect as moving - within a file for it to associate those lines with the parent - commit. ++ + is optional but it is the lower bound on the number of +alphanumeric characters that git must detect as moving +within a file for it to associate those lines with the parent +commit. -C||:: In addition to `-M`, detect lines copied from other @@ -77,11 +77,11 @@ of lines before or after the line given by . around across files. When this option is given twice, the command looks for copies from all other files in the parent for the commit that creates the file in addition. - - is optional but it is the lower bound on the number of - alphanumeric characters that git must detect as moving - between files for it to associate those lines with the parent - commit. ++ + is optional but it is the lower bound on the number of +alphanumeric characters that git must detect as moving +between files for it to associate those lines with the parent +commit. -h, --help:: Show help message. From b767c792fa202539cfb9bba36f46c62bcbf7c987 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 9 Aug 2007 02:38:09 -0400 Subject: [PATCH 12/35] Teach update-paranoid how to store ACLs organized by groups In some applications of this paranoid update hook the set of ACL rules that need to be applied to a user can be large, and the number of users that those rules must also be applied to can be more than a handful of individuals. Rather than repeating the same rules multiple times (once for each user) we now allow users to be members of groups, where the group supplies the list of ACL rules. For various reasons we don't depend on the underlying OS groups and instead perform our own group handling. Users can be made a member of one or more groups by setting the user.memberOf property within the "users/$who.acl" file: [user] memberOf = developer memberOf = administrator This will cause the hook to also parse the "groups/$groupname.acl" file for each value of user.memberOf, and merge any allow rules that match the current repository with the user's own private rules (if they had any). Since some rules are basically the same but may have a component differ based on the individual user, any user.* key may be inserted into a rule using the "${user.foo}" syntax. The allow rule does not match if the user does not define one (and exactly one) value for the key "foo". Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- contrib/hooks/update-paranoid | 60 +++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/contrib/hooks/update-paranoid b/contrib/hooks/update-paranoid index 5ee1835c80..fb2aca3628 100644 --- a/contrib/hooks/update-paranoid +++ b/contrib/hooks/update-paranoid @@ -118,22 +118,29 @@ sub info ($) { print STDERR "-Info- $_[0]\n" if $debug; } -sub parse_config ($$) { - my ($data, $fn) = @_; - info "Loading $fn"; - open(I,'-|','git',"--git-dir=$acl_git",'cat-file','blob',$fn); +sub git_value (@) { + open(T,'-|','git',@_); local $_ = ; chop; close T; $_; +} + +sub parse_config ($$$$) { + my $data = shift; + local $ENV{GIT_DIR} = shift; + my $br = shift; + my $fn = shift; + info "Loading $br:$fn"; + open(I,'-|','git','cat-file','blob',"$br:$fn"); my $section = ''; while () { chomp; if (/^\s*$/ || /^\s*#/) { } elsif (/^\[([a-z]+)\]$/i) { - $section = $1; + $section = lc $1; } elsif (/^\[([a-z]+)\s+"(.*)"\]$/i) { - $section = "$1.$2"; + $section = join('.',lc $1,$2); } elsif (/^\s*([a-z][a-z0-9]+)\s*=\s*(.*?)\s*$/i) { - push @{$data->{"$section.$1"}}, $2; + push @{$data->{join('.',$section,lc $1)}}, $2; } else { - deny "bad config file line $. in $fn"; + deny "bad config file line $. in $br:$fn"; } } close I; @@ -202,11 +209,6 @@ sub check_committers (@) { } } -sub git_value (@) { - open(T,'-|','git',@_); local $_ = ; chop; close T; - $_; -} - deny "No GIT_DIR inherited from caller" unless $git_dir; deny "Need a ref name" unless $ref; deny "Refusing funny ref $ref" unless $ref =~ s,^refs/,,; @@ -231,13 +233,39 @@ $op = 'U' if ($op eq 'R' && $ref =~ m,^heads/, && $old eq git_value('merge-base',$old,$new)); -# Load the user's ACL file. +# Load the user's ACL file. Expand groups (user.memberof) one level. { my %data = ('user.committer' => []); - parse_config(\%data, "$acl_branch:users/$this_user.acl"); + parse_config(\%data,$acl_git,$acl_branch,"external/$repository_name.acl"); + + %data = ( + 'user.committer' => $data{'user.committer'}, + 'user.memberof' => [], + ); + parse_config(\%data,$acl_git,$acl_branch,"users/$this_user.acl"); + %user_committer = map {$_ => $_} @{$data{'user.committer'}}; - my $rules = $data{"repository.$repository_name.allow"} || []; + my $rule_key = "repository.$repository_name.allow"; + my $rules = $data{$rule_key} || []; + + foreach my $group (@{$data{'user.memberof'}}) { + my %g; + parse_config(\%g,$acl_git,$acl_branch,"groups/$group.acl"); + my $group_rules = $g{$rule_key}; + push @$rules, @$group_rules if $group_rules; + } + +RULE: foreach (@$rules) { + while (/\${user\.([a-z][a-zA-Z0-9]+)}/) { + my $k = lc $1; + my $v = $data{"user.$k"}; + next RULE unless defined $v; + next RULE if @$v != 1; + next RULE unless defined $v->[0]; + s/\${user\.$k}/$v->[0]/g; + } + if (/^([CDRU ]+)\s+for\s+([^\s]+)$/) { my $ops = $1; my $ref = $2; From d47eed3272311acf16f136c49b0bb341c9a6e39c Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 9 Aug 2007 02:38:12 -0400 Subject: [PATCH 13/35] Teach the update-paranoid to look at file differences In some applications of the update hook a user may be allowed to modify a branch, but only if the file level difference is also an allowed change. This is the commonly requested feature of allowing users to modify only certain files. A new repository.*.allow syntax permits granting the three basic file level operations: A: file is added relative to the other tree M: file exists in both trees, but its SHA-1 or mode differs D: file is removed relative to the other tree on a per-branch and path-name basis. The user must also have a branch level allow line already granting them access to create, rewind or update (CRU) that branch before the hook will consult any file level rules. In order for a branch change to succeed _all_ files that differ relative to some base (by default the old value of this branch, but it can also be any valid tree-ish) must be allowed by file level allow rules. A push is rejected if any diff exists that is not covered by at least one allow rule. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- contrib/hooks/update-paranoid | 112 +++++++++++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 7 deletions(-) diff --git a/contrib/hooks/update-paranoid b/contrib/hooks/update-paranoid index fb2aca3628..84ed452480 100644 --- a/contrib/hooks/update-paranoid +++ b/contrib/hooks/update-paranoid @@ -102,6 +102,8 @@ my ($this_user) = getpwuid $<; # REAL_USER_ID my $repository_name; my %user_committer; my @allow_rules; +my @path_rules; +my %diff_cache; sub deny ($) { print STDERR "-Deny- $_[0]\n" if $debug; @@ -122,6 +124,13 @@ sub git_value (@) { open(T,'-|','git',@_); local $_ = ; chop; close T; $_; } +sub match_string ($$) { + my ($acl_n, $ref) = @_; + ($acl_n eq $ref) + || ($acl_n =~ m,/$, && substr($ref,0,length $acl_n) eq $acl_n) + || ($acl_n =~ m,^\^, && $ref =~ m:$acl_n:); +} + sub parse_config ($$$$) { my $data = shift; local $ENV{GIT_DIR} = shift; @@ -209,6 +218,31 @@ sub check_committers (@) { } } +sub load_diff ($) { + my $base = shift; + my $d = $diff_cache{$base}; + unless ($d) { + local $/ = "\0"; + open(T,'-|','git','diff-tree', + '-r','--name-status','-z', + $base,$new) or return undef; + my %this_diff; + while () { + my $op = $_; + chop $op; + + my $path = ; + chop $path; + + $this_diff{$path} = $op; + } + close T or return undef; + $d = \%this_diff; + $diff_cache{$base} = $d; + } + return $d; +} + deny "No GIT_DIR inherited from caller" unless $git_dir; deny "Need a ref name" unless $ref; deny "Refusing funny ref $ref" unless $ref =~ s,^refs/,,; @@ -266,7 +300,19 @@ RULE: s/\${user\.$k}/$v->[0]/g; } - if (/^([CDRU ]+)\s+for\s+([^\s]+)$/) { + if (/^([AMD ]+)\s+of\s+([^\s]+)\s+for\s+([^\s]+)\s+diff\s+([^\s]+)$/) { + my ($ops, $pth, $ref, $bst) = ($1, $2, $3, $4); + $ops =~ s/ //g; + $pth =~ s/\\\\/\\/g; + $ref =~ s/\\\\/\\/g; + push @path_rules, [$ops, $pth, $ref, $bst]; + } elsif (/^([AMD ]+)\s+of\s+([^\s]+)\s+for\s+([^\s]+)$/) { + my ($ops, $pth, $ref) = ($1, $2, $3); + $ops =~ s/ //g; + $pth =~ s/\\\\/\\/g; + $ref =~ s/\\\\/\\/g; + push @path_rules, [$ops, $pth, $ref, $old]; + } elsif (/^([CDRU ]+)\s+for\s+([^\s]+)$/) { my $ops = $1; my $ref = $2; $ops =~ s/ //g; @@ -300,13 +346,65 @@ foreach my $acl_entry (@allow_rules) { next unless $acl_ops =~ /^[CDRU]+$/; # Uhh.... shouldn't happen. next unless $acl_n; next unless $op =~ /^[$acl_ops]$/; + next unless match_string $acl_n, $ref; - grant "Allowed by: $acl_ops for $acl_n" - if ( - ($acl_n eq $ref) - || ($acl_n =~ m,/$, && substr($ref,0,length $acl_n) eq $acl_n) - || ($acl_n =~ m,^\^, && $ref =~ m:$acl_n:) - ); + # Don't test path rules on branch deletes. + # + grant "Allowed by: $acl_ops for $acl_n" if $op eq 'D'; + + # Aggregate matching path rules; allow if there aren't + # any matching this ref. + # + my %pr; + foreach my $p_entry (@path_rules) { + my ($p_ops, $p_n, $p_ref, $p_bst) = @$p_entry; + next unless $p_ref; + push @{$pr{$p_bst}}, $p_entry if match_string $p_ref, $ref; + } + grant "Allowed by: $acl_ops for $acl_n" unless %pr; + + # Allow only if all changes against a single base are + # allowed by file path rules. + # + my @bad; + foreach my $p_bst (keys %pr) { + my $diff_ref = load_diff $p_bst; + deny "Cannot difference trees." unless ref $diff_ref; + + my %fd = %$diff_ref; + foreach my $p_entry (@{$pr{$p_bst}}) { + my ($p_ops, $p_n, $p_ref, $p_bst) = @$p_entry; + next unless $p_ops =~ /^[AMD]+$/; + next unless $p_n; + + foreach my $f_n (keys %fd) { + my $f_op = $fd{$f_n}; + next unless $f_op; + next unless $f_op =~ /^[$p_ops]$/; + delete $fd{$f_n} if match_string $p_n, $f_n; + } + last unless %fd; + } + + if (%fd) { + push @bad, [$p_bst, \%fd]; + } else { + # All changes relative to $p_bst were allowed. + # + grant "Allowed by: $acl_ops for $acl_n diff $p_bst"; + } + } + + foreach my $bad_ref (@bad) { + my ($p_bst, $fd) = @$bad_ref; + print STDERR "\n"; + print STDERR "Not allowed to make the following changes:\n"; + print STDERR "(base: $p_bst)\n"; + foreach my $f_n (sort keys %$fd) { + print STDERR " $fd->{$f_n} $f_n\n"; + } + } + deny "You are not permitted to $op $ref"; } close A; deny "You are not permitted to $op $ref"; From cabead982b7cf40f8930e6e38327fc396b7fef81 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 9 Aug 2007 02:38:16 -0400 Subject: [PATCH 14/35] Use the empty tree for base diff in paranoid-update on new branches We have to load a tree difference for the purpose of testing file patterns. But if our branch is being created and there is no specific base to difference against in the rule our base will be '0'x40. This is (usually) not a valid tree-ish object in a Git repository, so there's nothing to difference against. Instead of creating the empty tree and running git-diff against that we just take the output of `ls-tree -r --name-only` and mark every returned pathname as an add. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- contrib/hooks/update-paranoid | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/contrib/hooks/update-paranoid b/contrib/hooks/update-paranoid index 84ed452480..068fa37083 100644 --- a/contrib/hooks/update-paranoid +++ b/contrib/hooks/update-paranoid @@ -223,20 +223,31 @@ sub load_diff ($) { my $d = $diff_cache{$base}; unless ($d) { local $/ = "\0"; - open(T,'-|','git','diff-tree', - '-r','--name-status','-z', - $base,$new) or return undef; my %this_diff; - while () { - my $op = $_; - chop $op; + if ($base =~ /^0{40}$/) { + open(T,'-|','git','ls-tree', + '-r','--name-only','-z', + $new) or return undef; + while () { + chop; + $this_diff{$_} = 'A'; + } + close T or return undef; + } else { + open(T,'-|','git','diff-tree', + '-r','--name-status','-z', + $base,$new) or return undef; + while () { + my $op = $_; + chop $op; - my $path = ; - chop $path; + my $path = ; + chop $path; - $this_diff{$path} = $op; + $this_diff{$path} = $op; + } + close T or return undef; } - close T or return undef; $d = \%this_diff; $diff_cache{$base} = $d; } From 155197e6e770599a5ed7a33a33f2916032184dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= Date: Thu, 9 Aug 2007 15:27:57 +0200 Subject: [PATCH 15/35] send-email: rfc822 forbids using without a non-empty "phrase" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Email::Valid does respect this considering such a mailbox specification invalid. b06c6bc831cbb9e9eb82fd3ffd5a2b674cd940d0 addressed the issue, but only if Email::Valid is available. Signed-off-by: Uwe Kleine-König Signed-off-by: Junio C Hamano --- git-send-email.perl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-send-email.perl b/git-send-email.perl index 39e433b76b..a02ab9694f 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -408,8 +408,8 @@ sub extract_valid_address { # check for a local address: return $address if ($address =~ /^($local_part_regexp)$/); + $address =~ s/^\s*<(.*)>\s*$/$1/; if ($have_email_valid) { - $address =~ s/^\s*<(.*)>\s*$/$1/; return scalar Email::Valid->address($address); } else { # less robust/correct than the monster regexp in Email::Valid, From 94638f89f5bcd1f92b86ebf988f7974167c3fd27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= Date: Thu, 9 Aug 2007 15:27:58 +0200 Subject: [PATCH 16/35] send-email: get all the quoting of realnames right MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - when sending several mails I got a slightly different behaviour for the first mail compared to the second to last one. The reason is that $from was assigned in line 608 and was not reset when beginning to handle the next mail. - Email::Valid can only handle properly quoted real names, so quote arguments to extract_valid_address. This patch cleans up variable naming to better differentiate between sender of the mail and it's author. Signed-off-by: Uwe Kleine-König Signed-off-by: Junio C Hamano --- git-send-email.perl | 50 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/git-send-email.perl b/git-send-email.perl index a02ab9694f..69559b289a 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -137,7 +137,7 @@ my $compose_filename = ".msg.$$"; # Variables we fill in automatically, or via prompting: my (@to,@cc,@initial_cc,@bcclist,@xh, - $initial_reply_to,$initial_subject,@files,$from,$compose,$time); + $initial_reply_to,$initial_subject,@files,$author,$sender,$compose,$time); my $smtp_server; my $envelope_sender; @@ -179,7 +179,7 @@ if (!@bcclist or !$bcclist[0]) { # Begin by accumulating all the variables (defined above), that we will end up # needing, first, from the command line: -my $rc = GetOptions("from=s" => \$from, +my $rc = GetOptions("sender|from=s" => \$sender, "in-reply-to=s" => \$initial_reply_to, "subject=s" => \$initial_subject, "to=s" => \@to, @@ -216,8 +216,8 @@ foreach my $entry (@bcclist) { # Now, let's fill any that aren't set in with defaults: -my ($author) = $repo->ident_person('author'); -my ($committer) = $repo->ident_person('committer'); +my ($repoauthor) = $repo->ident_person('author'); +my ($repocommitter) = $repo->ident_person('committer'); my %aliases; my @alias_files = $repo->config('sendemail.aliasesfile'); @@ -254,17 +254,17 @@ if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) { } } -($from) = expand_aliases($from) if defined $from; +($sender) = expand_aliases($sender) if defined $sender; my $prompting = 0; -if (!defined $from) { - $from = $author || $committer; +if (!defined $sender) { + $sender = $repoauthor || $repocommitter; do { - $_ = $term->readline("Who should the emails appear to be from? [$from] "); + $_ = $term->readline("Who should the emails appear to be from? [$sender] "); } while (!defined $_); - $from = $_ if ($_); - print "Emails will be sent from: ", $from, "\n"; + $sender = $_ if ($_); + print "Emails will be sent from: ", $sender, "\n"; $prompting++; } @@ -330,7 +330,7 @@ if ($compose) { # effort to have it be unique open(C,">",$compose_filename) or die "Failed to open for writing $compose_filename: $!"; - print C "From $from # This line is ignored.\n"; + print C "From $sender # This line is ignored.\n"; printf C "Subject: %s\n\n", $initial_subject; printf C <code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; @@ -582,7 +582,7 @@ $subject = $initial_subject; foreach my $t (@files) { open(F,"<",$t) or die "can't open file $t"; - my $author_not_sender = undef; + my $author = undef; @cc = @initial_cc; @xh = (); my $input_format = undef; @@ -604,12 +604,11 @@ foreach my $t (@files) { $subject = $1; } elsif (/^(Cc|From):\s+(.*)$/) { - if (unquote_rfc2047($2) eq $from) { - $from = $2; + if (unquote_rfc2047($2) eq $sender) { next if ($suppress_from); } elsif ($1 eq 'From') { - $author_not_sender = $2; + $author = unquote_rfc2047($2); } printf("(mbox) Adding cc: %s from line '%s'\n", $2, $_) unless $quiet; @@ -653,9 +652,8 @@ foreach my $t (@files) { } } close F; - if (defined $author_not_sender) { - $author_not_sender = unquote_rfc2047($author_not_sender); - $message = "From: $author_not_sender\n\n$message"; + if (defined $author) { + $message = "From: $author\n\n$message"; } From f1ec6b22a8c1ab1cca0f1875f85aea5d2434e5a6 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 10 Aug 2007 00:49:27 -0700 Subject: [PATCH 17/35] Fix an illustration in git-rev-parse.txt This hides the backslash at the end of line from AsciiDoc toolchain by introducing a trailing whitespace on one line in an illustration in git-rev-parse.txt. Signed-off-by: Junio C Hamano --- Documentation/git-rev-parse.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt index eea9c9cfe9..4b4d229e60 100644 --- a/Documentation/git-rev-parse.txt +++ b/Documentation/git-rev-parse.txt @@ -224,7 +224,7 @@ left-to-right. G H I J \ / \ / D E F - \ | / \ + \ | / \ \ | / | \|/ | B C From 524e5ffcf41a67ec113a9c3730ddc8fb8d3317f5 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 10 Aug 2007 00:49:26 -0700 Subject: [PATCH 18/35] tweak manpage formatting This attempts to force fixed-font in manpages for literal blocks. I have tested this with docbook 1.71 and it seems to work as expected. Signed-off-by: Junio C Hamano --- Documentation/callouts.xsl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Documentation/callouts.xsl b/Documentation/callouts.xsl index 6a361a2136..7941af7878 100644 --- a/Documentation/callouts.xsl +++ b/Documentation/callouts.xsl @@ -27,4 +27,17 @@ + + .RS + + + + + + .ft C .nf + + .fi .ft + .RE + + From 7efeb8f09866ddd09485c0e6f371a6cbba3d2a0a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 5 Aug 2007 14:12:53 +0100 Subject: [PATCH 19/35] Reinstate the old behaviour when GIT_DIR is set and GIT_WORK_TREE is unset The old behaviour was to unilaterally default to the cwd is the work tree when GIT_DIR was set, but GIT_WORK_TREE wasn't, no matter if we are inside the GIT_DIR, or if GIT_DIR is actually something like ../../../.git. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 52 +++++++++----------------------------------- t/t1500-rev-parse.sh | 6 +++-- 2 files changed, 14 insertions(+), 44 deletions(-) diff --git a/setup.c b/setup.c index b55b82c99e..06004f1587 100644 --- a/setup.c +++ b/setup.c @@ -189,53 +189,21 @@ int is_inside_work_tree(void) } /* - * If no worktree was given, and we are outside of a default work tree, - * now is the time to set it. - * - * In other words, if the user calls git with something like - * - * git --git-dir=/some/where/else/.git bla - * - * default to /some/where/else as working directory; if the specified - * git-dir does not end in "/.git", the cwd is used as working directory. + * set_work_tree() is only ever called if you set GIT_DIR explicitely. + * The old behaviour (which we retain here) is to set the work tree root + * to the cwd, unless overridden by the config, the command line, or + * GIT_WORK_TREE. */ -const char *set_work_tree(const char *dir) +static const char *set_work_tree(const char *dir) { - char dir_buffer[PATH_MAX], *rel = NULL; - static char buffer[PATH_MAX + 1]; - int len, suffix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1; + char buffer[PATH_MAX + 1]; - /* strip the variable 'dir' of the postfix "/.git" if it has it */ - len = strlen(dir); - if (len > suffix_len && - !strcmp(dir + len - suffix_len, "/" DEFAULT_GIT_DIR_ENVIRONMENT)) { - if ((len - suffix_len) >= sizeof(dir_buffer)) - die("directory name too long"); - memcpy(dir_buffer, dir, len - suffix_len); - dir_buffer[len - suffix_len] = '\0'; - - /* are we inside the default work tree? */ - rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer); - } - - /* if rel is set, the cwd is _not_ the current working tree */ - if (rel && *rel) { - if (!is_absolute_path(dir)) - set_git_dir(make_absolute_path(dir)); - dir = dir_buffer; - if (chdir(dir)) - die("cannot chdir to %s: %s", dir, strerror(errno)); - else - strcat(rel, "/"); - inside_git_dir = 0; - } else { - rel = NULL; - dir = getcwd(buffer, sizeof(buffer)); - } - git_work_tree_cfg = xstrdup(dir); + if (!getcwd(buffer, sizeof(buffer))) + die ("Could not get the current working directory"); + git_work_tree_cfg = xstrdup(buffer); inside_work_tree = 1; - return rel; + return NULL; } /* diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh index bea40cba8d..e474b3f1d5 100755 --- a/t/t1500-rev-parse.sh +++ b/t/t1500-rev-parse.sh @@ -28,6 +28,8 @@ test_rev_parse() { [ $# -eq 0 ] && return } +# label is-bare is-inside-git is-inside-work prefix + test_rev_parse toplevel false false true '' cd .git || exit 1 @@ -53,13 +55,13 @@ export GIT_DIR=../.git export GIT_CONFIG="$(pwd)"/../.git/config git config core.bare false -test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true work/ +test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true '' git config core.bare true test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false '' git config --unset core.bare -test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true work/ +test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true '' mv ../.git ../repo.git || exit 1 export GIT_DIR=../repo.git From 933bf40a5c6328b6c022b636f45a6f2c48c3838e Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 9 Aug 2007 22:21:29 -0700 Subject: [PATCH 20/35] Start moving unpack-trees to "struct tree_desc" This doesn't actually change any real code, but it changes the interface to unpack_trees() to take an array of "struct tree_desc" entries, the same way the tree-walk.c functions do. The reason for this is that we would be much better off if we can do the tree-unpacking using the generic "traverse_trees()" functionality instead of having to the special "unpack" infrastructure. This really is a pretty minimal diff, just to change the calling convention. It passes all the tests, and looks sane. There were only two users of "unpack_trees()": builtin-read-tree and merge-recursive, and I tried to keep the changes minimal. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- builtin-read-tree.c | 26 ++++++++++++++++++-------- merge-recursive.c | 16 +++++++++++----- unpack-trees.c | 25 +++++++++---------------- unpack-trees.h | 2 +- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/builtin-read-tree.c b/builtin-read-tree.c index a3b17a3bd9..1967d100f2 100644 --- a/builtin-read-tree.c +++ b/builtin-read-tree.c @@ -13,14 +13,19 @@ #include "dir.h" #include "builtin.h" -static struct object_list *trees; +static int nr_trees; +static struct tree *trees[4]; static int list_tree(unsigned char *sha1) { - struct tree *tree = parse_tree_indirect(sha1); + struct tree *tree; + + if (nr_trees >= 4) + return -1; + tree = parse_tree_indirect(sha1); if (!tree) return -1; - object_list_append(&tree->object, &trees); + trees[nr_trees++] = tree; return 0; } @@ -76,11 +81,10 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree) static void prime_cache_tree(void) { - struct tree *tree = (struct tree *)trees->item; - if (!tree) + if (!nr_trees) return; active_cache_tree = cache_tree(); - prime_cache_tree_rec(active_cache_tree, tree); + prime_cache_tree_rec(active_cache_tree, trees[0]); } @@ -92,6 +96,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) { int i, newfd, stage = 0; unsigned char sha1[20]; + struct tree_desc t[3]; struct unpack_trees_options opts; memset(&opts, 0, sizeof(opts)); @@ -258,7 +263,12 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) opts.head_idx = 1; } - unpack_trees(trees, &opts); + for (i = 0; i < nr_trees; i++) { + struct tree *tree = trees[i]; + parse_tree(tree); + init_tree_desc(t+i, tree->buffer, tree->size); + } + unpack_trees(nr_trees, t, &opts); /* * When reading only one tree (either the most basic form, @@ -266,7 +276,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) * valid cache-tree because the index must match exactly * what came from the tree. */ - if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) { + if (nr_trees && !opts.prefix && (!opts.merge || (stage == 2))) { cache_tree_free(&active_cache_tree); prime_cache_tree(); } diff --git a/merge-recursive.c b/merge-recursive.c index c8539ec0ba..f7d1b84999 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -216,13 +216,19 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, */ static int index_only = 0; +static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree) +{ + parse_tree(tree); + init_tree_desc(desc, tree->buffer, tree->size); +} + static int git_merge_trees(int index_only, struct tree *common, struct tree *head, struct tree *merge) { int rc; - struct object_list *trees = NULL; + struct tree_desc t[3]; struct unpack_trees_options opts; memset(&opts, 0, sizeof(opts)); @@ -234,11 +240,11 @@ static int git_merge_trees(int index_only, opts.head_idx = 2; opts.fn = threeway_merge; - object_list_append(&common->object, &trees); - object_list_append(&head->object, &trees); - object_list_append(&merge->object, &trees); + init_tree_desc_from_tree(t+0, common); + init_tree_desc_from_tree(t+1, head); + init_tree_desc_from_tree(t+2, merge); - rc = unpack_trees(trees, &opts); + rc = unpack_trees(3, t, &opts); cache_tree_free(&active_cache_tree); return rc; } diff --git a/unpack-trees.c b/unpack-trees.c index dfd985b0ef..5d1ffd1a32 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -16,19 +16,13 @@ struct tree_entry_list { const unsigned char *sha1; }; -static struct tree_entry_list *create_tree_entry_list(struct tree *tree) +static struct tree_entry_list *create_tree_entry_list(struct tree_desc *desc) { - struct tree_desc desc; struct name_entry one; struct tree_entry_list *ret = NULL; struct tree_entry_list **list_p = &ret; - if (!tree->object.parsed) - parse_tree(tree); - - init_tree_desc(&desc, tree->buffer, tree->size); - - while (tree_entry(&desc, &one)) { + while (tree_entry(desc, &one)) { struct tree_entry_list *entry; entry = xmalloc(sizeof(struct tree_entry_list)); @@ -173,9 +167,11 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len, if (S_ISDIR(posns[i]->mode)) { struct tree *tree = lookup_tree(posns[i]->sha1); + struct tree_desc t; any_dirs = 1; parse_tree(tree); - subposns[i] = create_tree_entry_list(tree); + init_tree_desc(&t, tree->buffer, tree->size); + subposns[i] = create_tree_entry_list(&t); posns[i] = posns[i]->next; src[i + o->merge] = o->df_conflict_entry; continue; @@ -331,12 +327,10 @@ static void check_updates(struct cache_entry **src, int nr, stop_progress(&progress);; } -int unpack_trees(struct object_list *trees, struct unpack_trees_options *o) +int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) { - unsigned len = object_list_length(trees); struct tree_entry_list **posns; int i; - struct object_list *posn = trees; struct tree_entry_list df_conflict_list; static struct cache_entry *dfc; @@ -356,10 +350,9 @@ int unpack_trees(struct object_list *trees, struct unpack_trees_options *o) if (len) { posns = xmalloc(len * sizeof(struct tree_entry_list *)); - for (i = 0; i < len; i++) { - posns[i] = create_tree_entry_list((struct tree *) posn->item); - posn = posn->next; - } + for (i = 0; i < len; i++) + posns[i] = create_tree_entry_list(t+i); + if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "", o, &df_conflict_list)) return -1; diff --git a/unpack-trees.h b/unpack-trees.h index fee7da4382..9cd39a28a9 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -26,7 +26,7 @@ struct unpack_trees_options { struct cache_entry *df_conflict_entry; }; -extern int unpack_trees(struct object_list *trees, +extern int unpack_trees(unsigned n, struct tree_desc *t, struct unpack_trees_options *options); int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o); From 63c21c494fc91eda339d06b3a466c4241afffc81 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 10 Aug 2007 11:32:42 -0700 Subject: [PATCH 21/35] Revert "tweak manpage formatting" This reverts commit 524e5ffcf41a67ec113a9c3730ddc8fb8d3317f5. It is reported that this change breaks formatting with docbook 1.69. --- Documentation/callouts.xsl | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/Documentation/callouts.xsl b/Documentation/callouts.xsl index 7941af7878..6a361a2136 100644 --- a/Documentation/callouts.xsl +++ b/Documentation/callouts.xsl @@ -27,17 +27,4 @@ - - .RS - - - - - - .ft C .nf - - .fi .ft - .RE - - From af3785dc5a76f4d5ddb8039e33c322e0e8b60e72 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 9 Aug 2007 13:42:50 -0700 Subject: [PATCH 22/35] Optimize "diff --cached" performance. The read_tree() function is called only from the call chain to run "git diff --cached" (this includes the internal call made by git-runstatus to run_diff_index()). The function vacates stage without any funky "merge" magic. The caller then goes and compares stage #1 entries from the tree with stage #0 entries from the original index. When adding the cache entries this way, it used the general purpose add_cache_entry(). This function looks for an existing entry to replace or if there is none to find where to insert the new entry, resolves D/F conflict and all the other things. For the purpose of reading entries into an empty stage, none of that processing is needed. We can instead append everything and then sort the result at the end. This commit changes read_tree() to first make sure that there is no existing cache entries at specified stage, and if that is the case, it runs add_cache_entry() with ADD_CACHE_JUST_APPEND flag (new), and then sort the resulting cache using qsort(). This new flag tells add_cache_entry() to omit all the checks such as "Does this path already exist? Does adding this path remove other existing entries because it turns a directory to a file?" and instead append the given cache entry straight at the end of the active cache. The caller of course is expected to sort the resulting cache at the end before using the result. Signed-off-by: Junio C Hamano --- cache.h | 1 + read-cache.c | 20 +++++++++++++-- tree.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/cache.h b/cache.h index e97af18eea..e5276e6add 100644 --- a/cache.h +++ b/cache.h @@ -258,6 +258,7 @@ extern int index_name_pos(struct index_state *, const char *name, int namelen); #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */ #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */ #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */ +#define ADD_CACHE_JUST_APPEND 8 /* Append only; tree.c::read_tree() */ extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option); extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really); extern int remove_index_entry_at(struct index_state *, int pos); diff --git a/read-cache.c b/read-cache.c index e060392d1d..865369df0e 100644 --- a/read-cache.c +++ b/read-cache.c @@ -665,7 +665,7 @@ static int check_file_directory_conflict(struct index_state *istate, return retval + has_dir_name(istate, ce, pos, ok_to_replace); } -int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option) +static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option) { int pos; int ok_to_add = option & ADD_CACHE_OK_TO_ADD; @@ -707,6 +707,22 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags)); pos = -pos-1; } + return pos + 1; +} + +int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option) +{ + int pos; + + if (option & ADD_CACHE_JUST_APPEND) + pos = istate->cache_nr; + else { + int ret; + ret = add_index_entry_with_check(istate, ce, option); + if (ret <= 0) + return ret; + pos = ret - 1; + } /* Make sure the array is big enough .. */ if (istate->cache_nr == istate->cache_alloc) { @@ -717,7 +733,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti /* Add it in.. */ istate->cache_nr++; - if (istate->cache_nr > pos) + if (istate->cache_nr > pos + 1) memmove(istate->cache + pos + 1, istate->cache + pos, (istate->cache_nr - pos - 1) * sizeof(ce)); diff --git a/tree.c b/tree.c index 04fe653a8e..8c0819fa72 100644 --- a/tree.c +++ b/tree.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "cache-tree.h" #include "tree.h" #include "blob.h" #include "commit.h" @@ -7,7 +8,7 @@ const char *tree_type = "tree"; -static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) +static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt) { int len; unsigned int size; @@ -25,7 +26,23 @@ static int read_one_entry(const unsigned char *sha1, const char *base, int basel memcpy(ce->name, base, baselen); memcpy(ce->name + baselen, pathname, len+1); hashcpy(ce->sha1, sha1); - return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); + return add_cache_entry(ce, opt); +} + +static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) +{ + return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage, + ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); +} + +/* + * This is used when the caller knows there is no existing entries at + * the stage that will conflict with the entry being added. + */ +static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) +{ + return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage, + ADD_CACHE_JUST_APPEND); } static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths) @@ -119,9 +136,55 @@ int read_tree_recursive(struct tree *tree, return 0; } +static int cmp_cache_name_compare(const void *a_, const void *b_) +{ + const struct cache_entry *ce1, *ce2; + + ce1 = *((const struct cache_entry **)a_); + ce2 = *((const struct cache_entry **)b_); + return cache_name_compare(ce1->name, ntohs(ce1->ce_flags), + ce2->name, ntohs(ce2->ce_flags)); +} + int read_tree(struct tree *tree, int stage, const char **match) { - return read_tree_recursive(tree, "", 0, stage, match, read_one_entry); + read_tree_fn_t fn = NULL; + int i, err; + + /* + * Currently the only existing callers of this function all + * call it with stage=1 and after making sure there is nothing + * at that stage; we could always use read_one_entry_quick(). + * + * But when we decide to straighten out git-read-tree not to + * use unpack_trees() in some cases, this will probably start + * to matter. + */ + + /* + * See if we have cache entry at the stage. If so, + * do it the original slow way, otherwise, append and then + * sort at the end. + */ + for (i = 0; !fn && i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + if (ce_stage(ce) == stage) + fn = read_one_entry; + } + + if (!fn) + fn = read_one_entry_quick; + err = read_tree_recursive(tree, "", 0, stage, match, fn); + if (fn == read_one_entry || err) + return err; + + /* + * Sort the cache entry -- we need to nuke the cache tree, though. + */ + cache_tree_free(&active_cache_tree); + qsort(active_cache, active_nr, sizeof(active_cache[0]), + cmp_cache_name_compare); + return 0; } struct tree *lookup_tree(const unsigned char *sha1) From 22631473e0b7a33356587ba3e38a9b4cc4dba2f1 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 10 Aug 2007 09:51:58 -0700 Subject: [PATCH 23/35] Fix "git commit directory/" performance anomaly This trivial patch avoids re-hashing files that are already clean in the index. This mirrors what commit 0781b8a9b2fe760fc4ed519a3a26e4b9bd6ccffe did for "git add .", only for "git commit ." instead. This improves the cold-cache case immensely, since we don't need to bring in all the file contents, just the index and any files dirty in the index. Before: [torvalds@woody linux]$ time git commit . real 1m49.537s user 0m3.892s sys 0m2.432s After: [torvalds@woody linux]$ time git commit . real 0m14.273s user 0m1.312s sys 0m0.516s (both after doing a "echo 3 > /proc/sys/vm/drop_caches" to get cold-cache behaviour - even with the index optimization git still has to "lstat()" all the files, so with a truly cold cache, bringing all the inodes in will take some time). [jc: trivial "return 0;" fixed] Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- builtin-update-index.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/builtin-update-index.c b/builtin-update-index.c index 509369e9e7..a7a4574f2b 100644 --- a/builtin-update-index.c +++ b/builtin-update-index.c @@ -86,9 +86,15 @@ static int process_lstat_error(const char *path, int err) static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st) { - int option, size = cache_entry_size(len); - struct cache_entry *ce = xcalloc(1, size); + int option, size; + struct cache_entry *ce; + /* Was the old index entry already up-to-date? */ + if (old && !ce_stage(old) && !ce_match_stat(old, st, 0)) + return 0; + + size = cache_entry_size(len); + ce = xcalloc(1, size); memcpy(ce->name, path, len); ce->ce_flags = htons(len); fill_stat_cache_info(ce, st); From b48d5a050afa30402c8281c223df9d7e58b4493c Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 10 Aug 2007 12:15:54 -0700 Subject: [PATCH 24/35] Move old index entry removal from "unpack_trees()" into the individual functions This makes no changes to current code, but it allows the individual merge functions to decide what to do about the old entry. They might decide to update it in place, rather than force them to always delete and re-add it. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- unpack-trees.c | 29 +++++++++++++++++++++++------ unpack-trees.h | 11 ++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/unpack-trees.c b/unpack-trees.c index 5d1ffd1a32..5e457b0bc9 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -58,10 +58,17 @@ static int entcmp(const char *name1, int dir1, const char *name2, int dir2) return ret; } +static inline void remove_entry(int remove) +{ + if (remove >= 0) + remove_cache_entry_at(remove); +} + static int unpack_trees_rec(struct tree_entry_list **posns, int len, const char *base, struct unpack_trees_options *o, struct tree_entry_list *df_conflict_list) { + int remove; int baselen = strlen(base); int src_size = len + 1; int i_stk = i_stk; @@ -145,10 +152,11 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len, subposns = xcalloc(len, sizeof(struct tree_list_entry *)); + remove = -1; if (cache_name && !strcmp(cache_name, first)) { any_files = 1; src[0] = active_cache[o->pos]; - remove_cache_entry_at(o->pos); + remove = o->pos; } for (i = 0; i < len; i++) { @@ -214,13 +222,14 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len, printf("\n"); } #endif - ret = o->fn(src, o); + ret = o->fn(src, o, remove); #if DBRT_DEBUG > 1 printf("Added %d entries\n", ret); #endif o->pos += ret; } else { + remove_entry(remove); for (i = 0; i < src_size; i++) { if (src[i]) { add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); @@ -641,7 +650,8 @@ static void show_stage_entry(FILE *o, #endif int threeway_merge(struct cache_entry **stages, - struct unpack_trees_options *o) + struct unpack_trees_options *o, + int remove) { struct cache_entry *index; struct cache_entry *head; @@ -657,6 +667,7 @@ int threeway_merge(struct cache_entry **stages, int no_anc_exists = 1; int i; + remove_entry(remove); for (i = 1; i < o->head_idx; i++) { if (!stages[i] || stages[i] == o->df_conflict_entry) any_anc_missing = 1; @@ -809,12 +820,14 @@ int threeway_merge(struct cache_entry **stages, * */ int twoway_merge(struct cache_entry **src, - struct unpack_trees_options *o) + struct unpack_trees_options *o, + int remove) { struct cache_entry *current = src[0]; struct cache_entry *oldtree = src[1]; struct cache_entry *newtree = src[2]; + remove_entry(remove); if (o->merge_size != 2) return error("Cannot do a twoway merge of %d trees", o->merge_size); @@ -868,11 +881,13 @@ int twoway_merge(struct cache_entry **src, * stage0 does not have anything there. */ int bind_merge(struct cache_entry **src, - struct unpack_trees_options *o) + struct unpack_trees_options *o, + int remove) { struct cache_entry *old = src[0]; struct cache_entry *a = src[1]; + remove_entry(remove); if (o->merge_size != 1) return error("Cannot do a bind merge of %d trees\n", o->merge_size); @@ -891,11 +906,13 @@ int bind_merge(struct cache_entry **src, * - take the stat information from stage0, take the data from stage1 */ int oneway_merge(struct cache_entry **src, - struct unpack_trees_options *o) + struct unpack_trees_options *o, + int remove) { struct cache_entry *old = src[0]; struct cache_entry *a = src[1]; + remove_entry(remove); if (o->merge_size != 1) return error("Cannot do a oneway merge of %d trees", o->merge_size); diff --git a/unpack-trees.h b/unpack-trees.h index 9cd39a28a9..5517faafad 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -4,7 +4,8 @@ struct unpack_trees_options; typedef int (*merge_fn_t)(struct cache_entry **src, - struct unpack_trees_options *options); + struct unpack_trees_options *options, + int remove); struct unpack_trees_options { int reset; @@ -29,9 +30,9 @@ struct unpack_trees_options { extern int unpack_trees(unsigned n, struct tree_desc *t, struct unpack_trees_options *options); -int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o); -int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o); -int bind_merge(struct cache_entry **src, struct unpack_trees_options *o); -int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o); +int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o, int); +int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o, int); +int bind_merge(struct cache_entry **src, struct unpack_trees_options *o, int); +int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o, int); #endif From 288f072ec033cf917eed949119428db3626ddc71 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 10 Aug 2007 12:21:20 -0700 Subject: [PATCH 25/35] Optimize the common cases of git-read-tree This optimizes bind_merge() and oneway_merge() to not unnecessarily remove and re-add the old index entries when they can just get replaced by updated ones. This makes these operations much faster for large trees (where "large" is in the 50,000+ file range), because we don't unnecessarily move index entries around in the index array all the time. Using the "bummer" tree (a test-tree with 100,000 files) we get: Before: [torvalds@woody bummer]$ time git commit -m"Change one file" 50/500 real 0m9.470s user 0m8.729s sys 0m0.476s After: [torvalds@woody bummer]$ time git commit -m"Change one file" 50/500 real 0m1.173s user 0m0.720s sys 0m0.452s so for large trees this is easily very noticeable indeed. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- unpack-trees.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/unpack-trees.c b/unpack-trees.c index 5e457b0bc9..d57b91c1b2 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -887,7 +887,6 @@ int bind_merge(struct cache_entry **src, struct cache_entry *old = src[0]; struct cache_entry *a = src[1]; - remove_entry(remove); if (o->merge_size != 1) return error("Cannot do a bind merge of %d trees\n", o->merge_size); @@ -912,13 +911,14 @@ int oneway_merge(struct cache_entry **src, struct cache_entry *old = src[0]; struct cache_entry *a = src[1]; - remove_entry(remove); if (o->merge_size != 1) return error("Cannot do a oneway merge of %d trees", o->merge_size); - if (!a) + if (!a) { + remove_entry(remove); return deleted_entry(old, old, o); + } if (old && same(old, a)) { if (o->reset) { struct stat st; From d699676dda5fdf0996601006c3bac2a9c077a862 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 10 Aug 2007 12:31:20 -0700 Subject: [PATCH 26/35] Optimize the two-way merge of git-read-tree too This trivially optimizes the two-way merge case of git-read-tree too, which affects switching branches. When you have tons and tons of files in your repository, but there are only small differences in the branches (maybe just a couple of files changed), the biggest cost of the branch switching was actually just the index calculations. This fixes it (timings for switching between the "testing" and "master" branches in the 100,000 file testing-repo-from-hell, where the branches only differ in one small file). Before: [torvalds@woody bummer]$ time git checkout master real 0m9.919s user 0m8.461s sys 0m0.264s After: [torvalds@woody bummer]$ time git checkout testing real 0m0.576s user 0m0.348s sys 0m0.228s so it's easily an order of magnitude different. This concludes the series. I think we could/should do the three-way merge too (to speed up merges), but I'm lazy. Somebody else can do it. The rule is very simple: you need to remove the old entry if: - you want to remove the file entirely - you replace it with a "merge conflict" entry (ie a non-stage-0 entry) and you can avoid removing it if you either - keep the old one - or resolve it to a new one. and these rules should all be valid for the three-way case too. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- unpack-trees.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/unpack-trees.c b/unpack-trees.c index d57b91c1b2..fda7729f1d 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -827,7 +827,6 @@ int twoway_merge(struct cache_entry **src, struct cache_entry *oldtree = src[1]; struct cache_entry *newtree = src[2]; - remove_entry(remove); if (o->merge_size != 2) return error("Cannot do a twoway merge of %d trees", o->merge_size); @@ -850,6 +849,7 @@ int twoway_merge(struct cache_entry **src, } else if (oldtree && !newtree && same(current, oldtree)) { /* 10 or 11 */ + remove_entry(remove); return deleted_entry(oldtree, current, o); } else if (oldtree && newtree && @@ -859,6 +859,7 @@ int twoway_merge(struct cache_entry **src, } else { /* all other failures */ + remove_entry(remove); if (oldtree) reject_merge(oldtree); if (current) @@ -870,8 +871,8 @@ int twoway_merge(struct cache_entry **src, } else if (newtree) return merged_entry(newtree, current, o); - else - return deleted_entry(oldtree, current, o); + remove_entry(remove); + return deleted_entry(oldtree, current, o); } /* From 7fa8254f9476de24661e93b2a90c6ce30dc10006 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 8 Aug 2007 17:01:49 -0700 Subject: [PATCH 27/35] allow git-bundle to create bottomless bundle While "git bundle" was a useful way to sneakernet incremental changes, we did not allow: $ git bundle create v2.6.20.bndl v2.6.20 to create a bundle that contains the whole history to a well-known good revision. Such a bundle can be mirrored everywhere, and people can prime their repository with it to reduce the load on the repository that serves near the tip of the development. Signed-off-by: Junio C Hamano --- builtin-bundle.c | 10 +++++++++- t/t5510-fetch.sh | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/builtin-bundle.c b/builtin-bundle.c index 6ae5ab04c9..cb439ca465 100644 --- a/builtin-bundle.c +++ b/builtin-bundle.c @@ -208,6 +208,10 @@ static int create_bundle(struct bundle_header *header, const char *path, struct rev_info revs; struct child_process rls; + /* + * NEEDSWORK: this should use something like lock-file + * to create temporary that is cleaned up upon error. + */ bundle_fd = (!strcmp(path, "-") ? 1 : open(path, O_CREAT | O_EXCL | O_WRONLY, 0666)); if (bundle_fd < 0) @@ -267,8 +271,12 @@ static int create_bundle(struct bundle_header *header, const char *path, * Make sure the refs we wrote out is correct; --max-count and * other limiting options could have prevented all the tips * from getting output. + * + * Non commit objects such as tags and blobs do not have + * this issue as they are not affected by those extra + * constraints. */ - if (!(e->item->flags & SHOWN)) { + if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) { warning("ref '%s' is excluded by the rev-list options", e->name); continue; diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh index 426017e1d0..439430f569 100755 --- a/t/t5510-fetch.sh +++ b/t/t5510-fetch.sh @@ -145,4 +145,12 @@ test_expect_success 'bundle does not prerequisite objects' ' test 4 = $(git verify-pack -v bundle.pack | wc -l) ' +test_expect_success 'bundle should be able to create a full history' ' + + cd "$D" && + git tag -a -m '1.0' v1.0 master && + git bundle create bundle4 v1.0 + +' + test_done From c06793a4ed1bf81902c324d1ed88dd055c3aa468 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 8 Aug 2007 22:04:06 -0700 Subject: [PATCH 28/35] allow git-bundle to create bottomless bundle Mark Levedahl writes: > Junio C Hamano wrote: >> While "git bundle" was a useful way to sneakernet incremental >> changes, we did not allow: >> > Thanks - I've been thinking for months I could fix this bug, never > figured it out and didn't want to nag Dscho one more time. I confirm > that this allows creation of bundles with arbitrary refs, not just > those under refs/heads. Yahoo! Actually, there is another bug nearby. If you do: git bundle create v2.6-20-v2.6.22.bndl v2.6.20..v2.6.22 the bundle records that it requires v2.6.20^0 commit (correct) and gives you tag v2.6.22 (incorrect); the bug is that the object it lists in fact is the commit v2.6.22^0, not the tag. This is because the revision range operation .. is always about set of commits, but the code near where my patch touches does not validate that the sha1 value obtained from dwim_ref() against the commit object name e->item->sha1 before placing the head information in the commit. The attached patch attempts to fix this problem. Signed-off-by: Junio C Hamano --- builtin-bundle.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/builtin-bundle.c b/builtin-bundle.c index cb439ca465..2d0e106c08 100644 --- a/builtin-bundle.c +++ b/builtin-bundle.c @@ -279,8 +279,41 @@ static int create_bundle(struct bundle_header *header, const char *path, if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) { warning("ref '%s' is excluded by the rev-list options", e->name); + free(ref); continue; } + /* + * If you run "git bundle create bndl v1.0..v2.0", the + * name of the positive ref is "v2.0" but that is the + * commit that is referenced by the tag, and not the tag + * itself. + */ + if (hashcmp(sha1, e->item->sha1)) { + /* + * Is this the positive end of a range expressed + * in terms of a tag (e.g. v2.0 from the range + * "v1.0..v2.0")? + */ + struct commit *one = lookup_commit_reference(sha1); + struct object *obj; + + if (e->item == &(one->object)) { + /* + * Need to include e->name as an + * independent ref to the pack-objects + * input, so that the tag is included + * in the output; otherwise we would + * end up triggering "empty bundle" + * error. + */ + obj = parse_object(sha1); + obj->flags |= SHOWN; + add_pending_object(&revs, obj, e->name); + } + free(ref); + continue; + } + ref_count++; write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40); write_or_die(bundle_fd, " ", 1); From 442b67a55972e69a054eb1206bbbdf044532130a Mon Sep 17 00:00:00 2001 From: Mark Levedahl Date: Fri, 10 Aug 2007 18:29:49 -0400 Subject: [PATCH 29/35] builtin-bundle.c - use stream buffered input for rev-list git-bundle create on cygwin was nearly unusable due to 1 character at a time (unbuffered) reading from an exec'ed process. Fix by using fdopen to get a buffered stream. Results for "time git bundle create test.bdl v1.0.3..v1.5.2" are: before this patch: cygwin linux real 1m38.828s 0m3.578s user 0m12.122s 0m2.896s sys 1m28.215s 0m0.692s after this patch: real 0m3.688s 0m2.835s user 0m3.075s 0m2.731s sys 0m1.075s 0m0.149s Signed-off-by: Mark Levedahl Signed-off-by: Junio C Hamano --- builtin-bundle.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/builtin-bundle.c b/builtin-bundle.c index 2d0e106c08..b954213f70 100644 --- a/builtin-bundle.c +++ b/builtin-bundle.c @@ -207,6 +207,7 @@ static int create_bundle(struct bundle_header *header, const char *path, char buffer[1024]; struct rev_info revs; struct child_process rls; + FILE *rls_fout; /* * NEEDSWORK: this should use something like lock-file @@ -236,10 +237,11 @@ static int create_bundle(struct bundle_header *header, const char *path, rls.git_cmd = 1; if (start_command(&rls)) return -1; - while ((i = read_string(rls.out, buffer, sizeof(buffer))) > 0) { + rls_fout = fdopen(rls.out, "r"); + while (fgets(buffer, sizeof(buffer), rls_fout)) { unsigned char sha1[20]; if (buffer[0] == '-') { - write_or_die(bundle_fd, buffer, i); + write_or_die(bundle_fd, buffer, strlen(buffer)); if (!get_sha1_hex(buffer + 1, sha1)) { struct object *object = parse_object(sha1); object->flags |= UNINTERESTING; @@ -250,6 +252,7 @@ static int create_bundle(struct bundle_header *header, const char *path, object->flags |= SHOWN; } } + fclose(rls_fout); if (finish_command(&rls)) return error("rev-list died"); From 21a02980f9025c3a338fb897796542ddef8707d1 Mon Sep 17 00:00:00 2001 From: Mark Levedahl Date: Fri, 10 Aug 2007 20:39:24 -0400 Subject: [PATCH 30/35] builtin-bundle - use buffered reads for bundle header This eliminates all use of byte-at-a-time reading of data in this function: as Junio noted, a bundle file is seekable so we can reset the file position to the first part of the pack-file using lseek after reading the header. Signed-off-by: Mark Levedahl Signed-off-by: Junio C Hamano --- builtin-bundle.c | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/builtin-bundle.c b/builtin-bundle.c index b954213f70..f4b4f034f4 100644 --- a/builtin-bundle.c +++ b/builtin-bundle.c @@ -44,38 +44,21 @@ struct bundle_header { struct ref_list references; }; -/* this function returns the length of the string */ -static int read_string(int fd, char *buffer, int size) -{ - int i; - for (i = 0; i < size - 1; i++) { - ssize_t count = xread(fd, buffer + i, 1); - if (count < 0) - return error("Read error: %s", strerror(errno)); - if (count == 0) { - i--; - break; - } - if (buffer[i] == '\n') - break; - } - buffer[i + 1] = '\0'; - return i + 1; -} - /* returns an fd */ static int read_header(const char *path, struct bundle_header *header) { char buffer[1024]; - int fd = open(path, O_RDONLY); + int fd; + long fpos; + FILE *ffd = fopen(path, "rb"); - if (fd < 0) + if (!ffd) return error("could not open '%s'", path); - if (read_string(fd, buffer, sizeof(buffer)) < 0 || + if (!fgets(buffer, sizeof(buffer), ffd) || strcmp(buffer, bundle_signature)) { - close(fd); + fclose(ffd); return error("'%s' does not look like a v2 bundle file", path); } - while (read_string(fd, buffer, sizeof(buffer)) > 0 + while (fgets(buffer, sizeof(buffer), ffd) && buffer[0] != '\n') { int is_prereq = buffer[0] == '-'; int offset = is_prereq ? 1 : 0; @@ -97,6 +80,12 @@ static int read_header(const char *path, struct bundle_header *header) { add_to_ref_list(sha1, isspace(delim) ? buffer + 41 + offset : "", list); } + fpos = ftell(ffd); + fclose(ffd); + fd = open(path, O_RDONLY); + if (fd < 0) + return error("could not open '%s'", path); + lseek(fd, fpos, SEEK_SET); return fd; } From cbbb218f8bd219d79907623a9304496ee69d8abd Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Fri, 10 Aug 2007 15:06:22 +0200 Subject: [PATCH 31/35] Fix filehandle leak in "git branch -D" On Windows (it can't touch open files in any way) the following fails: git branch -D branch1 branch2 if the both branches are in packed-refs. Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- refs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/refs.c b/refs.c index fac6548001..09a2c87fc2 100644 --- a/refs.c +++ b/refs.c @@ -869,6 +869,7 @@ static int repack_without_ref(const char *refname) die("too long a refname '%s'", list->name); write_or_die(fd, line, len); } + close(fd); return commit_lock_file(&packlock); } From 566b5c057c452d04605805ea2f7af210c6fb9b59 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 10 Aug 2007 12:53:51 -0700 Subject: [PATCH 32/35] Optimize the three-way merge of git-read-tree As mentioned, the three-way case *should* be as trivial as the following. It passes all the tests, and I verified that a conflicting merge in the 100,000 file horror-case merged correctly (with the conflict markers) in 0.687 seconds with this, so it works, but I'm lazy and somebody else should double-check it [jc: followed all three-way merge codepaths and verified it removes when it should]. Without this patch, the merge took 8.355 seconds, so this patch really does make a huge difference for merge performance with lots and lots of files, and we're not talking percentages, we're talking orders-of-magnitude differences! Now "unpack_trees()" is just fast enough that we don't need to avoid it (although it's probably still a good idea to eventually convert it to use the traverse_trees() infrastructure some day - just to avoid having extraneous tree traversal functions). Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- unpack-trees.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/unpack-trees.c b/unpack-trees.c index fda7729f1d..ccfeb6e245 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -667,7 +667,6 @@ int threeway_merge(struct cache_entry **stages, int no_anc_exists = 1; int i; - remove_entry(remove); for (i = 1; i < o->head_idx; i++) { if (!stages[i] || stages[i] == o->df_conflict_entry) any_anc_missing = 1; @@ -730,8 +729,10 @@ int threeway_merge(struct cache_entry **stages, } /* #1 */ - if (!head && !remote && any_anc_missing) + if (!head && !remote && any_anc_missing) { + remove_entry(remove); return 0; + } /* Under the new "aggressive" rule, we resolve mostly trivial * cases that we historically had git-merge-one-file resolve. @@ -763,6 +764,7 @@ int threeway_merge(struct cache_entry **stages, if ((head_deleted && remote_deleted) || (head_deleted && remote && remote_match) || (remote_deleted && head && head_match)) { + remove_entry(remove); if (index) return deleted_entry(index, index, o); else if (ce && !head_deleted) @@ -785,6 +787,7 @@ int threeway_merge(struct cache_entry **stages, verify_uptodate(index, o); } + remove_entry(remove); o->nontrivial_merge = 1; /* #2, #3, #4, #6, #7, #9, #10, #11. */ From 4739809cd0ea12a8de006f9f086fdff9285189b8 Mon Sep 17 00:00:00 2001 From: David Kastrup Date: Mon, 6 Aug 2007 12:22:57 +0200 Subject: [PATCH 33/35] Add support for an info version of the user manual These patches use docbook2x in order to create an info version of the git user manual. No existing Makefile targets (including "all") are touched, so you need to explicitly say make info sudo make install-info to get git.info created and installed. If the info target directory does not already contain a "dir" file, no directory entry is created. This facilitates $(DESTDIR)-based installations. The same could be achieved with sudo make INSTALL_INFO=: install-info explicitly. perl is used for patching up sub-par file and directory information in the Texinfo file. It would be cleaner to place the respective info straight into user-manual.txt or the conversion configurations, but I find myself unable to find out how to do this with Asciidoc/Texinfo. Signed-off-by: David Kastrup --- Documentation/Makefile | 24 +++++++++++++++++++++++- Documentation/fix-texi.perl | 15 +++++++++++++++ Makefile | 6 ++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100755 Documentation/fix-texi.perl diff --git a/Documentation/Makefile b/Documentation/Makefile index 443114b046..76a15ff520 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -44,6 +44,11 @@ INSTALL?=install RM ?= rm -f DOC_REF = origin/man +infodir?=$(prefix)/share/info +MAKEINFO=makeinfo +INSTALL_INFO=install-info +DOCBOOK2X_TEXI=docbook2x-texi + -include ../config.mak.autogen -include ../config.mak @@ -67,6 +72,8 @@ man1: $(DOC_MAN1) man5: $(DOC_MAN5) man7: $(DOC_MAN7) +info: git.info + install: man $(INSTALL) -d -m755 $(DESTDIR)$(man1dir) $(INSTALL) -d -m755 $(DESTDIR)$(man5dir) @@ -75,6 +82,14 @@ install: man $(INSTALL) -m644 $(DOC_MAN5) $(DESTDIR)$(man5dir) $(INSTALL) -m644 $(DOC_MAN7) $(DESTDIR)$(man7dir) +install-info: info + $(INSTALL) -d -m755 $(DESTDIR)$(infodir) + $(INSTALL) -m644 git.info $(DESTDIR)$(infodir) + if test -r $(DESTDIR)$(infodir)/dir; then \ + $(INSTALL_INFO) --info-dir=$(DESTDIR)$(infodir) git.info ;\ + else \ + echo "No directory found in $(DESTDIR)$(infodir)" >&2 ; \ + fi ../GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE $(MAKE) -C ../ GIT-VERSION-FILE @@ -110,7 +125,7 @@ cmd-list.made: cmd-list.perl $(MAN1_TXT) git.7 git.html: git.txt core-intro.txt clean: - $(RM) *.xml *.xml+ *.html *.html+ *.1 *.5 *.7 howto-index.txt howto/*.html doc.dep + $(RM) *.xml *.xml+ *.html *.html+ *.1 *.5 *.7 *.texi *.texi+ howto-index.txt howto/*.html doc.dep $(RM) $(cmds_txt) *.made %.html : %.txt @@ -138,6 +153,13 @@ XSLTOPTS = --xinclude --stringparam html.stylesheet docbook-xsl.css user-manual.html: user-manual.xml xsltproc $(XSLTOPTS) -o $@ $(XSLT) $< +git.info: user-manual.xml + $(RM) $@ $*.texi $*.texi+ + $(DOCBOOK2X_TEXI) user-manual.xml --to-stdout >$*.texi+ + perl fix-texi.perl <$*.texi+ >$*.texi + $(MAKEINFO) --no-split $*.texi + $(RM) $*.texi $*.texi+ + howto-index.txt: howto-index.sh $(wildcard howto/*.txt) $(RM) $@+ $@ sh ./howto-index.sh $(wildcard howto/*.txt) >$@+ diff --git a/Documentation/fix-texi.perl b/Documentation/fix-texi.perl new file mode 100755 index 0000000000..ff7d78f620 --- /dev/null +++ b/Documentation/fix-texi.perl @@ -0,0 +1,15 @@ +#!/usr/bin/perl -w + +while (<>) { + if (/^\@setfilename/) { + $_ = "\@setfilename git.info\n"; + } elsif (/^\@direntry/) { + print '@dircategory Development +@direntry +* Git: (git). A fast distributed revision control system +@end direntry +'; } + unless (/^\@direntry/../^\@end direntry/) { + print; + } +} diff --git a/Makefile b/Makefile index 2f3b9b23e3..b685c7e39e 100644 --- a/Makefile +++ b/Makefile @@ -913,6 +913,9 @@ perl/Makefile: perl/Git.pm perl/Makefile.PL GIT-CFLAGS doc: $(MAKE) -C Documentation all +info: + $(MAKE) -C Documentation info + TAGS: $(RM) TAGS $(FIND) . -name '*.[hcS]' -print | xargs etags -a @@ -1005,6 +1008,9 @@ endif install-doc: $(MAKE) -C Documentation install +install-info: + $(MAKE) -C Documentation install-info + quick-install-doc: $(MAKE) -C Documentation quick-install From 98e79f63be7e2cf043bd3150ae9ac0c8d118ce61 Mon Sep 17 00:00:00 2001 From: David Kastrup Date: Tue, 7 Aug 2007 12:02:12 +0200 Subject: [PATCH 34/35] INSTALL: explain info installation and dependencies. Signed-off-by: David Kastrup --- INSTALL | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/INSTALL b/INSTALL index c62b12c288..289b046a44 100644 --- a/INSTALL +++ b/INSTALL @@ -5,8 +5,8 @@ Normally you can just do "make" followed by "make install", and that will install the git programs in your own ~/bin/ directory. If you want to do a global install, you can do - $ make prefix=/usr all doc ;# as yourself - # make prefix=/usr install install-doc ;# as root + $ make prefix=/usr all doc info ;# as yourself + # make prefix=/usr install install-doc install-info ;# as root (or prefix=/usr/local, of course). Just like any program suite that uses $prefix, the built results have some paths encoded, @@ -91,9 +91,13 @@ Issues of note: - To build and install documentation suite, you need to have the asciidoc/xmlto toolchain. Because not many people are inclined to install the tools, the default build target - ("make all") does _not_ build them. The documentation is - written for AsciiDoc 7, but "make ASCIIDOC8=YesPlease doc" - will let you format with AsciiDoc 8. + ("make all") does _not_ build them. + + Building and installing the info file additionally requires + makeinfo and docbook2X. Version 0.8.3 is known to work. + + The documentation is written for AsciiDoc 7, but "make + ASCIIDOC8=YesPlease doc" will let you format with AsciiDoc 8. Alternatively, pre-formatted documentation are available in "html" and "man" branches of the git repository itself. For From f9286765b2c409e5b88efe8c20a2634d6842bc5f Mon Sep 17 00:00:00 2001 From: David Kastrup Date: Mon, 6 Aug 2007 15:05:56 +0200 Subject: [PATCH 35/35] Documentation/Makefile: remove cmd-list.made before redirecting to it. If cmd-list.made has been created by a previous run as root, output redirection to it will fail. So remove it before regeneration. Signed-off-by: David Kastrup Signed-off-by: Junio C Hamano --- Documentation/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/Makefile b/Documentation/Makefile index 76a15ff520..fbefe9a45b 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -119,6 +119,7 @@ cmds_txt = cmds-ancillaryinterrogators.txt \ $(cmds_txt): cmd-list.made cmd-list.made: cmd-list.perl $(MAN1_TXT) + $(RM) $@ perl ./cmd-list.perl date >$@