diff options
70 files changed, 3227 insertions, 550 deletions
diff --git a/Documentation/CodingGuidelines b/Documentation/CodingGuidelines index 994eb9159a..d2a0a76e6c 100644 --- a/Documentation/CodingGuidelines +++ b/Documentation/CodingGuidelines @@ -89,6 +89,8 @@ For C programs: of "else if" statements, it can make sense to add braces to single line blocks. + - We try to avoid assignments inside if(). + - Try to make your code understandable. You may put comments in, but comments invariably tend to stale out when the code they were describing changes. Often splitting a function diff --git a/Documentation/Makefile b/Documentation/Makefile index 4144d1e086..9750334b97 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -3,7 +3,8 @@ MAN1_TXT= \ $(wildcard git-*.txt)) \ gitk.txt MAN5_TXT=gitattributes.txt gitignore.txt gitmodules.txt githooks.txt -MAN7_TXT=git.txt gitcli.txt +MAN7_TXT=git.txt gitcli.txt gittutorial.txt gittutorial-2.txt \ + gitcvs-migration.txt MAN_TXT = $(MAN1_TXT) $(MAN5_TXT) $(MAN7_TXT) MAN_XML=$(patsubst %.txt,%.xml,$(MAN_TXT)) @@ -11,10 +12,7 @@ MAN_HTML=$(patsubst %.txt,%.html,$(MAN_TXT)) DOC_HTML=$(MAN_HTML) -ARTICLES = tutorial -ARTICLES += tutorial-2 -ARTICLES += core-tutorial -ARTICLES += cvs-migration +ARTICLES = core-tutorial ARTICLES += diffcore ARTICLES += howto-index ARTICLES += repository-layout diff --git a/Documentation/config.txt b/Documentation/config.txt index 002a066893..c298dc21c5 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -662,11 +662,24 @@ gitcvs.logfile:: Path to a log file where the CVS server interface well... logs various stuff. See linkgit:git-cvsserver[1]. +gitcvs.usecrlfattr + If true, the server will look up the `crlf` attribute for + files to determine the '-k' modes to use. If `crlf` is set, + the '-k' mode will be left blank, so cvs clients will + treat it as text. If `crlf` is explicitly unset, the file + will be set with '-kb' mode, which supresses any newline munging + the client might otherwise do. If `crlf` is not specified, + then 'gitcvs.allbinary' is used. See linkgit:gitattribute[5]. + gitcvs.allbinary:: - If true, all files are sent to the client in mode '-kb'. This - causes the client to treat all files as binary files which suppresses - any newline munging it otherwise might do. A work-around for the - fact that there is no way yet to set single files to mode '-kb'. + This is used if 'gitcvs.usecrlfattr' does not resolve + the correct '-kb' mode to use. If true, all + unresolved files are sent to the client in + mode '-kb'. This causes the client to treat them + as binary files, which suppresses any newline munging it + otherwise might do. Alternatively, if it is set to "guess", + then the contents of the file are examined to decide if + it is binary, similar to 'core.autocrlf'. gitcvs.dbname:: Database used by git-cvsserver to cache revision information @@ -697,8 +710,9 @@ gitcvs.dbTableNamePrefix:: linkgit:git-cvsserver[1] for details). Any non-alphabetic characters will be replaced with underscores. -All gitcvs variables except for 'gitcvs.allbinary' can also be -specified as 'gitcvs.<access_method>.<varname>' (where 'access_method' +All gitcvs variables except for 'gitcvs.usecrlfattr' and +'gitcvs.allbinary' can also be specified as +'gitcvs.<access_method>.<varname>' (where 'access_method' is one of "ext" and "pserver") to make them apply only for the given access method. diff --git a/Documentation/core-tutorial.txt b/Documentation/core-tutorial.txt index 5a5531222d..b50b5dd487 100644 --- a/Documentation/core-tutorial.txt +++ b/Documentation/core-tutorial.txt @@ -8,7 +8,7 @@ This tutorial explains how to use the "core" git programs to set up and work with a git repository. If you just need to use git as a revision control system you may prefer -to start with link:tutorial.html[a tutorial introduction to git] or +to start with linkgit:gittutorial[7][a tutorial introduction to git] or link:user-manual.html[the git user manual]. However, an understanding of these low-level tools can be helpful if @@ -1581,7 +1581,7 @@ suggested in the previous section may be new to you. You do not have to worry. git supports "shared public repository" style of cooperation you are probably more familiar with as well. -See link:cvs-migration.html[git for CVS users] for the details. +See linkgit:gitcvs-migration[7][git for CVS users] for the details. Bundling your work together --------------------------- diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt index 13234fa280..859d67990a 100644 --- a/Documentation/diff-options.txt +++ b/Documentation/diff-options.txt @@ -228,6 +228,9 @@ endif::git-format-patch[] --no-ext-diff:: Disallow external diff drivers. +--ignore-submodules:: + Ignore changes to submodules in the diff generation. + --src-prefix=<prefix>:: Show the given source prefix instead of "a/". diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt index df42cb10f2..f6c394c482 100644 --- a/Documentation/git-cat-file.txt +++ b/Documentation/git-cat-file.txt @@ -9,12 +9,16 @@ git-cat-file - Provide content or type/size information for repository objects SYNOPSIS -------- 'git-cat-file' [-t | -s | -e | -p | <type>] <object> +'git-cat-file' [--batch | --batch-check] < <list-of-objects> DESCRIPTION ----------- -Provides content or type of objects in the repository. The type -is required unless '-t' or '-p' is used to find the object type, -or '-s' is used to find the object size. +In the first form, provides content or type of objects in the repository. The +type is required unless '-t' or '-p' is used to find the object type, or '-s' +is used to find the object size. + +In the second form, a list of object (separated by LFs) is provided on stdin, +and the SHA1, type, and size of each object is printed on stdout. OPTIONS ------- @@ -46,6 +50,14 @@ OPTIONS or to ask for a "blob" with <object> being a tag object that points at it. +--batch:: + Print the SHA1, type, size, and contents of each object provided on + stdin. May not be combined with any other options or arguments. + +--batch-check:: + Print the SHA1, type, and size of each object provided on stdin. May not be + combined with any other options or arguments. + OUTPUT ------ If '-t' is specified, one of the <type>. @@ -56,9 +68,30 @@ If '-e' is specified, no output. If '-p' is specified, the contents of <object> are pretty-printed. -Otherwise the raw (though uncompressed) contents of the <object> will -be returned. +If <type> is specified, the raw (though uncompressed) contents of the <object> +will be returned. + +If '--batch' is specified, output of the following form is printed for each +object specified on stdin: + +------------ +<sha1> SP <type> SP <size> LF +<contents> LF +------------ + +If '--batch-check' is specified, output of the following form is printed for +each object specified fon stdin: + +------------ +<sha1> SP <type> SP <size> LF +------------ + +For both '--batch' and '--batch-check', output of the following form is printed +for each object specified on stdin that does not exist in the repository: +------------ +<object> SP missing LF +------------ Author ------ diff --git a/Documentation/git-cvsexportcommit.txt b/Documentation/git-cvsexportcommit.txt index 363c36d694..f75afaaadc 100644 --- a/Documentation/git-cvsexportcommit.txt +++ b/Documentation/git-cvsexportcommit.txt @@ -8,7 +8,7 @@ git-cvsexportcommit - Export a single commit to a CVS checkout SYNOPSIS -------- -'git-cvsexportcommit' [-h] [-u] [-v] [-c] [-P] [-p] [-a] [-d cvsroot] [-w cvsworkdir] [-f] [-m msgprefix] [PARENTCOMMIT] COMMITID +'git-cvsexportcommit' [-h] [-u] [-v] [-c] [-P] [-p] [-a] [-d cvsroot] [-w cvsworkdir] [-W] [-f] [-m msgprefix] [PARENTCOMMIT] COMMITID DESCRIPTION @@ -68,6 +68,11 @@ OPTIONS current directory is within a git repository. The default is the value of 'cvsexportcommit.cvsdir'. +-W:: + Tell cvsexportcommit that the current working directory is not only + a Git checkout, but also the CVS checkout. Therefore, Git will + reset the working directory to the parent commit before proceeding. + -v:: Verbose. diff --git a/Documentation/git-cvsserver.txt b/Documentation/git-cvsserver.txt index b1106714b2..a33382ec2d 100644 --- a/Documentation/git-cvsserver.txt +++ b/Documentation/git-cvsserver.txt @@ -301,11 +301,33 @@ checkout, diff, status, update, log, add, remove, commit. Legacy monitoring operations are not supported (edit, watch and related). Exports and tagging (tags and branches) are not supported at this stage. -The server should set the '-k' mode to binary when relevant, however, -this is not really implemented yet. For now, you can force the server -to set '-kb' for all files by setting the `gitcvs.allbinary` config -variable. In proper GIT tradition, the contents of the files are -always respected. No keyword expansion or newline munging is supported. +CRLF Line Ending Conversions +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default the server leaves the '-k' mode blank for all files, +which causes the cvs client to treat them as a text files, subject +to crlf conversion on some platforms. + +You can make the server use `crlf` attributes to set the '-k' modes +for files by setting the `gitcvs.usecrlfattr` config variable. +In this case, if `crlf` is explicitly unset ('-crlf'), then the +server will set '-kb' mode for binary files. If `crlf` is set, +then the '-k' mode will explicitly be left blank. See +also linkgit:gitattributes[5] for more information about the `crlf` +attribute. + +Alternatively, if `gitcvs.usecrlfattr` config is not enabled +or if the `crlf` attribute is unspecified for a filename, then +the server uses the `gitcvs.allbinary` config for the default setting. +If `gitcvs.allbinary` is set, then file not otherwise +specified will default to '-kb' mode. Otherwise the '-k' mode +is left blank. But if `gitcvs.allbinary` is set to "guess", then +the correct '-k' mode will be guessed based on the contents of +the file. + +For best consistency with cvs, it is probably best to override the +defaults by setting `gitcvs.usecrlfattr` to true, +and `gitcvs.allbinary` to "guess". Dependencies ------------ diff --git a/Documentation/git-hash-object.txt b/Documentation/git-hash-object.txt index 33030c022f..99a21434b5 100644 --- a/Documentation/git-hash-object.txt +++ b/Documentation/git-hash-object.txt @@ -8,7 +8,7 @@ git-hash-object - Compute object ID and optionally creates a blob from a file SYNOPSIS -------- -'git-hash-object' [-t <type>] [-w] [--stdin] [--] <file>... +'git-hash-object' [-t <type>] [-w] [--stdin | --stdin-paths] [--] <file>... DESCRIPTION ----------- @@ -32,6 +32,9 @@ OPTIONS --stdin:: Read the object from standard input instead of from a file. +--stdin-paths:: + Read file names from stdin instead of from the command-line. + Author ------ Written by Junio C Hamano <junkio@cox.net> diff --git a/Documentation/git-svn.txt b/Documentation/git-svn.txt index 3eae1ebb7d..c9e4efe7f4 100644 --- a/Documentation/git-svn.txt +++ b/Documentation/git-svn.txt @@ -61,6 +61,16 @@ COMMANDS Set the 'useSvnsyncProps' option in the [svn-remote] config. --rewrite-root=<URL>;; Set the 'rewriteRoot' option in the [svn-remote] config. +--use-log-author;; + When retrieving svn commits into git (as part of fetch, rebase, or + dcommit operations), look for the first From: or Signed-off-by: line + in the log message and use that as the author string. +--add-author-from;; + When committing to svn from git (as part of commit or dcommit + operations), if the existing log message doesn't already have a + From: or Signed-off-by: line, append a From: line based on the + git commit's author string. If you use this, then --use-log-author + will retrieve a valid author string for all commits. --username=<USER>;; For transports that SVN handles authentication for (http, https, and plain svn), specify the username. For other diff --git a/Documentation/git-update-index.txt b/Documentation/git-update-index.txt index 66be18ef36..06640603c4 100644 --- a/Documentation/git-update-index.txt +++ b/Documentation/git-update-index.txt @@ -15,6 +15,7 @@ SYNOPSIS [--cacheinfo <mode> <object> <file>]\* [--chmod=(+|-)x] [--assume-unchanged | --no-assume-unchanged] + [--ignore-submodules] [--really-refresh] [--unresolve] [--again | -g] [--info-only] [--index-info] [-z] [--stdin] @@ -54,6 +55,10 @@ OPTIONS default behavior is to error out. This option makes git-update-index continue anyway. +--ignore-submodules: + Do not try to update submodules. This option is only respected + when passed before --refresh. + --unmerged:: If --refresh finds unmerged changes in the index, the default behavior is to error out. This option makes git-update-index diff --git a/Documentation/git.txt b/Documentation/git.txt index adcd3e00b2..735f0d19c8 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -20,10 +20,10 @@ Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals. -See this link:tutorial.html[tutorial] to get started, then see +See this linkgit:gittutorial[7][tutorial] to get started, then see link:everyday.html[Everyday Git] for a useful minimum set of commands, and "man git-commandname" for documentation of each command. CVS users may -also want to read link:cvs-migration.html[CVS migration]. See +also want to read linkgit:gitcvs-migration[7][CVS migration]. See link:user-manual.html[Git User's Manual] for a more in-depth introduction. diff --git a/Documentation/cvs-migration.txt b/Documentation/gitcvs-migration.txt index 374bc87b10..c410805027 100644 --- a/Documentation/cvs-migration.txt +++ b/Documentation/gitcvs-migration.txt @@ -1,5 +1,16 @@ -git for CVS users -================= +gitcvs-migration(7) +=================== + +NAME +---- +gitcvs-migration - git for CVS users + +SYNOPSIS +-------- +git cvsimport * + +DESCRIPTION +----------- Git differs from CVS in that every working tree contains a repository with a full copy of the project history, and no repository is inherently more @@ -8,7 +19,7 @@ designating a single shared repository which people can synchronize with; this document explains how to do that. Some basic familiarity with git is required. This -link:tutorial.html[tutorial introduction to git] and the +linkgit:gittutorial[7][tutorial introduction to git] and the link:glossary.html[git glossary] should be sufficient. Developing against a shared repository @@ -71,7 +82,7 @@ Setting Up a Shared Repository We assume you have already created a git repository for your project, possibly created from scratch or from a tarball (see the -link:tutorial.html[tutorial]), or imported from an already existing CVS +linkgit:gittutorial[7][tutorial]), or imported from an already existing CVS repository (see the next section). Assume your existing repo is at /home/alice/myproject. Create a new "bare" @@ -170,3 +181,13 @@ variants of this model. With a small group, developers may just pull changes from each other's repositories without the need for a central maintainer. + +SEE ALSO +-------- +linkgit:gittutorial[7], linkgit:gittutorial-2[7], +link:everyday.html[Everyday Git], +link:user-manual.html[The Git User's Manual] + +GIT +--- +Part of the linkgit:git[7] suite. diff --git a/Documentation/tutorial-2.txt b/Documentation/gittutorial-2.txt index 7fac47de8b..5bbbf43056 100644 --- a/Documentation/tutorial-2.txt +++ b/Documentation/gittutorial-2.txt @@ -1,7 +1,18 @@ -A tutorial introduction to git: part two -======================================== +gittutorial-2(7) +================ -You should work through link:tutorial.html[A tutorial introduction to +NAME +---- +gittutorial-2 - A tutorial introduction to git: part two + +SYNOPSIS +-------- +git * + +DESCRIPTION +----------- + +You should work through linkgit:gittutorial[7][A tutorial introduction to git] before reading this tutorial. The goal of this tutorial is to introduce two fundamental pieces of @@ -394,7 +405,7 @@ link:glossary.html[Glossary]. The link:user-manual.html[Git User's Manual] provides a more comprehensive introduction to git. -The link:cvs-migration.html[CVS migration] document explains how to +The linkgit:gitcvs-migration[7][CVS migration] document explains how to import a CVS repository into git, and shows how to use git in a CVS-like way. @@ -404,3 +415,14 @@ link:howto-index.html[howtos]. For git developers, the link:core-tutorial.html[Core tutorial] goes into detail on the lower-level git mechanisms involved in, for example, creating a new commit. + +SEE ALSO +-------- +linkgit:gittutorial[7], +linkgit:gitcvs-migration[7], +link:everyday.html[Everyday git], +link:user-manual.html[The Git User's Manual] + +GIT +--- +Part of the linkgit:git[7] suite. diff --git a/Documentation/tutorial.txt b/Documentation/gittutorial.txt index e2bbda53f0..898acdb533 100644 --- a/Documentation/tutorial.txt +++ b/Documentation/gittutorial.txt @@ -1,5 +1,16 @@ -A tutorial introduction to git (for version 1.5.1 or newer) -=========================================================== +gittutorial(7) +============== + +NAME +---- +gittutorial - A tutorial introduction to git (for version 1.5.1 or newer) + +SYNOPSIS +-------- +git * + +DESCRIPTION +----------- This tutorial explains how to import a new project into git, make changes to it, and share changes with other developers. @@ -381,7 +392,7 @@ see linkgit:git-pull[1] for details. Git can also be used in a CVS-like mode, with a central repository that various users push changes to; see linkgit:git-push[1] and -link:cvs-migration.html[git for CVS users]. +linkgit:gitcvs-migration[7][git for CVS users]. Exploring history ----------------- @@ -560,7 +571,7 @@ is based: used to create commits, check out working directories, and hold the various trees involved in a merge. -link:tutorial-2.html[Part two of this tutorial] explains the object +linkgit:gittutorial-2[7][Part two of this tutorial] explains the object database, the index file, and a few other odds and ends that you'll need to make the most of git. @@ -581,4 +592,15 @@ digressions that may be interesting at this point are: * link:everyday.html[Everyday GIT with 20 Commands Or So] - * link:cvs-migration.html[git for CVS users]. + * linkgit:gitcvs-migration[7][git for CVS users]. + +SEE ALSO +-------- +linkgit:gittutorial-2[7], +linkgit:gitcvs-migration[7], +link:everyday.html[Everyday git], +link:user-manual.html[The Git User's Manual] + +GIT +--- +Part of the linkgit:git[7] suite. diff --git a/Documentation/user-manual.txt b/Documentation/user-manual.txt index e2db850150..fd8cdb625a 100644 --- a/Documentation/user-manual.txt +++ b/Documentation/user-manual.txt @@ -1993,7 +1993,7 @@ the right to push to the same repository. In that case, the correct solution is to retry the push after first updating your work by either a pull or a fetch followed by a rebase; see the <<setting-up-a-shared-repository,next section>> and -link:cvs-migration.html[git for CVS users] for more. +linkgit:gitcvs-migration[7][git for CVS users] for more. [[setting-up-a-shared-repository]] Setting up a shared repository @@ -2002,7 +2002,7 @@ Setting up a shared repository Another way to collaborate is by using a model similar to that commonly used in CVS, where several developers with special rights all push to and pull from a single shared repository. See -link:cvs-migration.html[git for CVS users] for instructions on how to +linkgit:gitcvs-migration[7][git for CVS users] for instructions on how to set this up. However, while there is nothing wrong with git's support for shared @@ -235,7 +235,6 @@ BASIC_LDFLAGS = SCRIPT_SH += git-am.sh SCRIPT_SH += git-bisect.sh -SCRIPT_SH += git-clone.sh SCRIPT_SH += git-filter-branch.sh SCRIPT_SH += git-lost-found.sh SCRIPT_SH += git-merge-octopus.sh @@ -484,6 +483,7 @@ BUILTIN_OBJS += builtin-check-ref-format.o BUILTIN_OBJS += builtin-checkout-index.o BUILTIN_OBJS += builtin-checkout.o BUILTIN_OBJS += builtin-clean.o +BUILTIN_OBJS += builtin-clone.o BUILTIN_OBJS += builtin-commit-tree.o BUILTIN_OBJS += builtin-commit.o BUILTIN_OBJS += builtin-config.o diff --git a/builtin-add.c b/builtin-add.c index 73235ed08a..6e4e645cb7 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -100,15 +100,16 @@ static void update_callback(struct diff_queue_struct *q, case DIFF_STATUS_UNMERGED: case DIFF_STATUS_MODIFIED: case DIFF_STATUS_TYPE_CHANGED: - if (add_file_to_cache(path, data->flags & ADD_FILES_VERBOSE)) { - if (!(data->flags & ADD_FILES_IGNORE_ERRORS)) + if (add_file_to_cache(path, data->flags)) { + if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) die("updating files failed"); data->add_errors++; } break; case DIFF_STATUS_DELETED: - remove_file_from_cache(path); - if (data->flags & ADD_FILES_VERBOSE) + if (!(data->flags & ADD_CACHE_PRETEND)) + remove_file_from_cache(path); + if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) printf("remove '%s'\n", path); break; } @@ -221,6 +222,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) int i, newfd; const char **pathspec; struct dir_struct dir; + int flags; argc = parse_options(argc, argv, builtin_add_options, builtin_add_usage, 0); @@ -233,18 +235,15 @@ int cmd_add(int argc, const char **argv, const char *prefix) newfd = hold_locked_index(&lock_file, 1); + flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | + (show_only ? ADD_CACHE_PRETEND : 0) | + (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0)); + if (take_worktree_changes) { - int flags = 0; const char **pathspec; if (read_cache() < 0) die("index file corrupt"); pathspec = get_pathspec(prefix, argv); - - if (verbose) - flags |= ADD_FILES_VERBOSE; - if (ignore_add_errors) - flags |= ADD_FILES_IGNORE_ERRORS; - exit_status = add_files_to_cache(prefix, pathspec, flags); goto finish; } @@ -263,17 +262,6 @@ int cmd_add(int argc, const char **argv, const char *prefix) fill_directory(&dir, pathspec, ignored_too); - if (show_only) { - const char *sep = "", *eof = ""; - for (i = 0; i < dir.nr; i++) { - printf("%s%s", sep, dir.entries[i]->name); - sep = " "; - eof = "\n"; - } - fputs(eof, stdout); - return 0; - } - if (read_cache() < 0) die("index file corrupt"); @@ -287,7 +275,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) } for (i = 0; i < dir.nr; i++) - if (add_file_to_cache(dir.entries[i]->name, verbose)) { + if (add_file_to_cache(dir.entries[i]->name, flags)) { if (!ignore_add_errors) die("adding files failed"); exit_status = 1; diff --git a/builtin-apply.c b/builtin-apply.c index 1103625a4a..1540f28ab4 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -418,7 +418,7 @@ static int guess_p_value(const char *nameline) } /* - * Get the name etc info from the --/+++ lines of a traditional patch header + * Get the name etc info from the ---/+++ lines of a traditional patch header * * FIXME! The end-of-filename heuristics are kind of screwy. For existing * files, we can happily check the index for a match, but for creating a @@ -1143,21 +1143,6 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc if (patch->is_delete < 0 && (newlines || (patch->fragments && patch->fragments->next))) patch->is_delete = 0; - if (!unidiff_zero || context) { - /* If the user says the patch is not generated with - * --unified=0, or if we have seen context lines, - * then not having oldlines means the patch is creation, - * and not having newlines means the patch is deletion. - */ - if (patch->is_new < 0 && !oldlines) { - patch->is_new = 1; - patch->old_name = NULL; - } - if (patch->is_delete < 0 && !newlines) { - patch->is_delete = 1; - patch->new_name = NULL; - } - } if (0 < patch->is_new && oldlines) die("new file %s depends on old contents", patch->new_name); @@ -2267,16 +2252,11 @@ static int verify_index_match(struct cache_entry *ce, struct stat *st) return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID); } -static int check_patch(struct patch *patch, struct patch *prev_patch) +static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st) { - struct stat st; const char *old_name = patch->old_name; - const char *new_name = patch->new_name; - const char *name = old_name ? old_name : new_name; - struct cache_entry *ce = NULL; - int ok_if_exists; - - patch->rejected = 1; /* we will drop this after we succeed */ + int stat_ret = 0; + unsigned st_mode = 0; /* * Make sure that we do not have local modifications from the @@ -2284,58 +2264,84 @@ static int check_patch(struct patch *patch, struct patch *prev_patch) * we have the preimage file to be patched in the work tree, * unless --cached, which tells git to apply only in the index. */ - if (old_name) { - int stat_ret = 0; - unsigned st_mode = 0; - - if (!cached) - stat_ret = lstat(old_name, &st); - if (check_index) { - int pos = cache_name_pos(old_name, strlen(old_name)); - if (pos < 0) - return error("%s: does not exist in index", - old_name); - ce = active_cache[pos]; - if (stat_ret < 0) { - struct checkout costate; - if (errno != ENOENT) - return error("%s: %s", old_name, - strerror(errno)); - /* checkout */ - costate.base_dir = ""; - costate.base_dir_len = 0; - costate.force = 0; - costate.quiet = 0; - costate.not_new = 0; - costate.refresh_cache = 1; - if (checkout_entry(ce, - &costate, - NULL) || - lstat(old_name, &st)) - return -1; - } - if (!cached && verify_index_match(ce, &st)) - return error("%s: does not match index", - old_name); - if (cached) - st_mode = ce->ce_mode; - } else if (stat_ret < 0) - return error("%s: %s", old_name, strerror(errno)); - - if (!cached) - st_mode = ce_mode_from_stat(ce, st.st_mode); + if (!old_name) + return 0; + assert(patch->is_new <= 0); + if (!cached) { + stat_ret = lstat(old_name, st); + if (stat_ret && errno != ENOENT) + return error("%s: %s", old_name, strerror(errno)); + } + if (check_index) { + int pos = cache_name_pos(old_name, strlen(old_name)); + if (pos < 0) { + if (patch->is_new < 0) + goto is_new; + return error("%s: does not exist in index", old_name); + } + *ce = active_cache[pos]; + if (stat_ret < 0) { + struct checkout costate; + /* checkout */ + costate.base_dir = ""; + costate.base_dir_len = 0; + costate.force = 0; + costate.quiet = 0; + costate.not_new = 0; + costate.refresh_cache = 1; + if (checkout_entry(*ce, &costate, NULL) || + lstat(old_name, st)) + return -1; + } + if (!cached && verify_index_match(*ce, st)) + return error("%s: does not match index", old_name); + if (cached) + st_mode = (*ce)->ce_mode; + } else if (stat_ret < 0) { if (patch->is_new < 0) - patch->is_new = 0; - if (!patch->old_mode) - patch->old_mode = st_mode; - if ((st_mode ^ patch->old_mode) & S_IFMT) - return error("%s: wrong type", old_name); - if (st_mode != patch->old_mode) - fprintf(stderr, "warning: %s has type %o, expected %o\n", - old_name, st_mode, patch->old_mode); + goto is_new; + return error("%s: %s", old_name, strerror(errno)); } + if (!cached) + st_mode = ce_mode_from_stat(*ce, st->st_mode); + + if (patch->is_new < 0) + patch->is_new = 0; + if (!patch->old_mode) + patch->old_mode = st_mode; + if ((st_mode ^ patch->old_mode) & S_IFMT) + return error("%s: wrong type", old_name); + if (st_mode != patch->old_mode) + fprintf(stderr, "warning: %s has type %o, expected %o\n", + old_name, st_mode, patch->old_mode); + return 0; + + is_new: + patch->is_new = 1; + patch->is_delete = 0; + patch->old_name = NULL; + return 0; +} + +static int check_patch(struct patch *patch, struct patch *prev_patch) +{ + struct stat st; + const char *old_name = patch->old_name; + const char *new_name = patch->new_name; + const char *name = old_name ? old_name : new_name; + struct cache_entry *ce = NULL; + int ok_if_exists; + int status; + + patch->rejected = 1; /* we will drop this after we succeed */ + + status = check_preimage(patch, &ce, &st); + if (status) + return status; + old_name = patch->old_name; + if (new_name && prev_patch && 0 < prev_patch->is_delete && !strcmp(prev_patch->old_name, new_name)) /* diff --git a/builtin-cat-file.c b/builtin-cat-file.c index f132d583d3..5ef15a4fa9 100644 --- a/builtin-cat-file.c +++ b/builtin-cat-file.c @@ -8,6 +8,10 @@ #include "tag.h" #include "tree.h" #include "builtin.h" +#include "parse-options.h" + +#define BATCH 1 +#define BATCH_CHECK 2 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size) { @@ -76,31 +80,16 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long write_or_die(1, cp, endp - cp); } -int cmd_cat_file(int argc, const char **argv, const char *prefix) +static int cat_one_file(int opt, const char *exp_type, const char *obj_name) { unsigned char sha1[20]; enum object_type type; void *buf; unsigned long size; - int opt; - const char *exp_type, *obj_name; - - git_config(git_default_config); - if (argc != 3) - usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>"); - exp_type = argv[1]; - obj_name = argv[2]; if (get_sha1(obj_name, sha1)) die("Not a valid object name %s", obj_name); - opt = 0; - if ( exp_type[0] == '-' ) { - opt = exp_type[1]; - if ( !opt || exp_type[2] ) - opt = -1; /* Not a single character option */ - } - buf = NULL; switch (opt) { case 't': @@ -157,3 +146,108 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) write_or_die(1, buf, size); return 0; } + +static int batch_one_object(const char *obj_name, int print_contents) +{ + unsigned char sha1[20]; + enum object_type type; + unsigned long size; + void *contents = contents; + + if (!obj_name) + return 1; + + if (get_sha1(obj_name, sha1)) { + printf("%s missing\n", obj_name); + return 0; + } + + if (print_contents == BATCH) + contents = read_sha1_file(sha1, &type, &size); + else + type = sha1_object_info(sha1, &size); + + if (type <= 0) + return 1; + + printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size); + fflush(stdout); + + if (print_contents == BATCH) { + write_or_die(1, contents, size); + printf("\n"); + fflush(stdout); + } + + return 0; +} + +static int batch_objects(int print_contents) +{ + struct strbuf buf; + + strbuf_init(&buf, 0); + while (strbuf_getline(&buf, stdin, '\n') != EOF) { + int error = batch_one_object(buf.buf, print_contents); + if (error) + return error; + } + + return 0; +} + +static const char * const cat_file_usage[] = { + "git-cat-file [-t|-s|-e|-p|<type>] <sha1>", + "git-cat-file [--batch|--batch-check] < <list_of_sha1s>", + NULL +}; + +int cmd_cat_file(int argc, const char **argv, const char *prefix) +{ + int opt = 0, batch = 0; + const char *exp_type = NULL, *obj_name = NULL; + + const struct option options[] = { + OPT_GROUP("<type> can be one of: blob, tree, commit, tag"), + OPT_SET_INT('t', NULL, &opt, "show object type", 't'), + OPT_SET_INT('s', NULL, &opt, "show object size", 's'), + OPT_SET_INT('e', NULL, &opt, + "exit with zero when there's no error", 'e'), + OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'), + OPT_SET_INT(0, "batch", &batch, + "show info and content of objects feeded on stdin", BATCH), + OPT_SET_INT(0, "batch-check", &batch, + "show info about objects feeded on stdin", + BATCH_CHECK), + OPT_END() + }; + + git_config(git_default_config); + + if (argc != 3 && argc != 2) + usage_with_options(cat_file_usage, options); + + argc = parse_options(argc, argv, options, cat_file_usage, 0); + + if (opt) { + if (argc == 1) + obj_name = argv[0]; + else + usage_with_options(cat_file_usage, options); + } + if (!opt && !batch) { + if (argc == 2) { + exp_type = argv[0]; + obj_name = argv[1]; + } else + usage_with_options(cat_file_usage, options); + } + if (batch && (opt || argc)) { + usage_with_options(cat_file_usage, options); + } + + if (batch) + return batch_objects(batch); + + return cat_one_file(opt, exp_type, obj_name); +} diff --git a/builtin-checkout.c b/builtin-checkout.c index 05c06421b6..68fffd28cb 100644 --- a/builtin-checkout.c +++ b/builtin-checkout.c @@ -236,6 +236,8 @@ static int merge_working_tree(struct checkout_opts *opts, topts.src_index = &the_index; topts.dst_index = &the_index; + topts.msgs.not_uptodate_file = "You have local changes to '%s'; cannot switch branches."; + refresh_cache(REFRESH_QUIET); if (unmerged_cache()) { diff --git a/builtin-clone.c b/builtin-clone.c new file mode 100644 index 0000000000..2a3f6732f2 --- /dev/null +++ b/builtin-clone.c @@ -0,0 +1,550 @@ +/* + * Builtin "git clone" + * + * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>, + * 2008 Daniel Barkalow <barkalow@iabervon.org> + * Based on git-commit.sh by Junio C Hamano and Linus Torvalds + * + * Clone a repository into a different directory that does not yet exist. + */ + +#include "cache.h" +#include "parse-options.h" +#include "fetch-pack.h" +#include "refs.h" +#include "tree.h" +#include "tree-walk.h" +#include "unpack-trees.h" +#include "transport.h" +#include "strbuf.h" +#include "dir.h" + +/* + * Overall FIXMEs: + * - respect DB_ENVIRONMENT for .git/objects. + * + * Implementation notes: + * - dropping use-separate-remote and no-separate-remote compatibility + * + */ +static const char * const builtin_clone_usage[] = { + "git-clone [options] [--] <repo> [<dir>]", + NULL +}; + +static int option_quiet, option_no_checkout, option_bare; +static int option_local, option_no_hardlinks, option_shared; +static char *option_template, *option_reference, *option_depth; +static char *option_origin = NULL; +static char *option_upload_pack = "git-upload-pack"; + +static struct option builtin_clone_options[] = { + OPT__QUIET(&option_quiet), + OPT_BOOLEAN('n', "no-checkout", &option_no_checkout, + "don't create a checkout"), + OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"), + OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"), + OPT_BOOLEAN('l', "local", &option_local, + "to clone from a local repository"), + OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks, + "don't use local hardlinks, always copy"), + OPT_BOOLEAN('s', "shared", &option_shared, + "setup as shared repository"), + OPT_STRING(0, "template", &option_template, "path", + "path the template repository"), + OPT_STRING(0, "reference", &option_reference, "repo", + "reference repository"), + OPT_STRING('o', "origin", &option_origin, "branch", + "use <branch> instead or 'origin' to track upstream"), + OPT_STRING('u', "upload-pack", &option_upload_pack, "path", + "path to git-upload-pack on the remote"), + OPT_STRING(0, "depth", &option_depth, "depth", + "create a shallow clone of that depth"), + + OPT_END() +}; + +static char *get_repo_path(const char *repo, int *is_bundle) +{ + static char *suffix[] = { "/.git", ".git", "" }; + static char *bundle_suffix[] = { ".bundle", "" }; + struct stat st; + int i; + + for (i = 0; i < ARRAY_SIZE(suffix); i++) { + const char *path; + path = mkpath("%s%s", repo, suffix[i]); + if (!stat(path, &st) && S_ISDIR(st.st_mode)) { + *is_bundle = 0; + return xstrdup(make_absolute_path(path)); + } + } + + for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) { + const char *path; + path = mkpath("%s%s", repo, bundle_suffix[i]); + if (!stat(path, &st) && S_ISREG(st.st_mode)) { + *is_bundle = 1; + return xstrdup(make_absolute_path(path)); + } + } + + return NULL; +} + +static char *guess_dir_name(const char *repo, int is_bundle) +{ + const char *p, *start, *end, *limit; + int after_slash_or_colon; + + /* Guess dir name from repository: strip trailing '/', + * strip trailing '[:/]*.{git,bundle}', strip leading '.*[/:]'. */ + + after_slash_or_colon = 1; + limit = repo + strlen(repo); + start = repo; + end = limit; + for (p = repo; p < limit; p++) { + const char *prefix = is_bundle ? ".bundle" : ".git"; + if (!prefixcmp(p, prefix)) { + if (!after_slash_or_colon) + end = p; + p += strlen(prefix) - 1; + } else if (!prefixcmp(p, ".bundle")) { + if (!after_slash_or_colon) + end = p; + p += 7; + } else if (*p == '/' || *p == ':') { + if (end == limit) + end = p; + after_slash_or_colon = 1; + } else if (after_slash_or_colon) { + start = p; + end = limit; + after_slash_or_colon = 0; + } + } + + return xstrndup(start, end - start); +} + +static int is_directory(const char *path) +{ + struct stat buf; + + return !stat(path, &buf) && S_ISDIR(buf.st_mode); +} + +static void setup_reference(const char *repo) +{ + const char *ref_git; + char *ref_git_copy; + + struct remote *remote; + struct transport *transport; + const struct ref *extra; + + ref_git = make_absolute_path(option_reference); + + if (is_directory(mkpath("%s/.git/objects", ref_git))) + ref_git = mkpath("%s/.git", ref_git); + else if (!is_directory(mkpath("%s/objects", ref_git))) + die("reference repository '%s' is not a local directory.", + option_reference); + + ref_git_copy = xstrdup(ref_git); + + add_to_alternates_file(ref_git_copy); + + remote = remote_get(ref_git_copy); + transport = transport_get(remote, ref_git_copy); + for (extra = transport_get_remote_refs(transport); extra; + extra = extra->next) + add_extra_ref(extra->name, extra->old_sha1, 0); + + transport_disconnect(transport); + + free(ref_git_copy); +} + +static void copy_or_link_directory(char *src, char *dest) +{ + struct dirent *de; + struct stat buf; + int src_len, dest_len; + DIR *dir; + + dir = opendir(src); + if (!dir) + die("failed to open %s\n", src); + + if (mkdir(dest, 0777)) { + if (errno != EEXIST) + die("failed to create directory %s\n", dest); + else if (stat(dest, &buf)) + die("failed to stat %s\n", dest); + else if (!S_ISDIR(buf.st_mode)) + die("%s exists and is not a directory\n", dest); + } + + src_len = strlen(src); + src[src_len] = '/'; + dest_len = strlen(dest); + dest[dest_len] = '/'; + + while ((de = readdir(dir)) != NULL) { + strcpy(src + src_len + 1, de->d_name); + strcpy(dest + dest_len + 1, de->d_name); + if (stat(src, &buf)) { + warning ("failed to stat %s\n", src); + continue; + } + if (S_ISDIR(buf.st_mode)) { + if (de->d_name[0] != '.') + copy_or_link_directory(src, dest); + continue; + } + + if (unlink(dest) && errno != ENOENT) + die("failed to unlink %s\n", dest); + if (!option_no_hardlinks) { + if (!link(src, dest)) + continue; + if (option_local) + die("failed to create link %s\n", dest); + option_no_hardlinks = 1; + } + if (copy_file(dest, src, 0666)) + die("failed to copy file to %s\n", dest); + } + closedir(dir); +} + +static const struct ref *clone_local(const char *src_repo, + const char *dest_repo) +{ + const struct ref *ret; + char src[PATH_MAX]; + char dest[PATH_MAX]; + struct remote *remote; + struct transport *transport; + + if (option_shared) + add_to_alternates_file(src_repo); + else { + snprintf(src, PATH_MAX, "%s/objects", src_repo); + snprintf(dest, PATH_MAX, "%s/objects", dest_repo); + copy_or_link_directory(src, dest); + } + + remote = remote_get(src_repo); + transport = transport_get(remote, src_repo); + ret = transport_get_remote_refs(transport); + transport_disconnect(transport); + return ret; +} + +static const char *junk_work_tree; +static const char *junk_git_dir; +pid_t junk_pid; + +static void remove_junk(void) +{ + struct strbuf sb; + if (getpid() != junk_pid) + return; + strbuf_init(&sb, 0); + if (junk_git_dir) { + strbuf_addstr(&sb, junk_git_dir); + remove_dir_recursively(&sb, 0); + strbuf_reset(&sb); + } + if (junk_work_tree) { + strbuf_addstr(&sb, junk_work_tree); + remove_dir_recursively(&sb, 0); + strbuf_reset(&sb); + } +} + +static void remove_junk_on_signal(int signo) +{ + remove_junk(); + signal(SIGINT, SIG_DFL); + raise(signo); +} + +static const struct ref *locate_head(const struct ref *refs, + const struct ref *mapped_refs, + const struct ref **remote_head_p) +{ + const struct ref *remote_head = NULL; + const struct ref *remote_master = NULL; + const struct ref *r; + for (r = refs; r; r = r->next) + if (!strcmp(r->name, "HEAD")) + remote_head = r; + + for (r = mapped_refs; r; r = r->next) + if (!strcmp(r->name, "refs/heads/master")) + remote_master = r; + + if (remote_head_p) + *remote_head_p = remote_head; + + /* If there's no HEAD value at all, never mind. */ + if (!remote_head) + return NULL; + + /* If refs/heads/master could be right, it is. */ + if (remote_master && !hashcmp(remote_master->old_sha1, + remote_head->old_sha1)) + return remote_master; + + /* Look for another ref that points there */ + for (r = mapped_refs; r; r = r->next) + if (r != remote_head && + !hashcmp(r->old_sha1, remote_head->old_sha1)) + return r; + + /* Nothing is the same */ + return NULL; +} + +static struct ref *write_remote_refs(const struct ref *refs, + struct refspec *refspec, const char *reflog) +{ + struct ref *local_refs = NULL; + struct ref **tail = &local_refs; + struct ref *r; + + get_fetch_map(refs, refspec, &tail, 0); + get_fetch_map(refs, tag_refspec, &tail, 0); + + for (r = local_refs; r; r = r->next) + update_ref(reflog, + r->peer_ref->name, r->old_sha1, NULL, 0, DIE_ON_ERR); + return local_refs; +} + +int cmd_clone(int argc, const char **argv, const char *prefix) +{ + int use_local_hardlinks = 1; + int use_separate_remote = 1; + int is_bundle = 0; + struct stat buf; + const char *repo_name, *repo, *work_tree, *git_dir; + char *path, *dir; + const struct ref *refs, *head_points_at, *remote_head, *mapped_refs; + char branch_top[256], key[256], value[256]; + struct strbuf reflog_msg; + + struct refspec refspec; + + junk_pid = getpid(); + + argc = parse_options(argc, argv, builtin_clone_options, + builtin_clone_usage, 0); + + if (argc == 0) + die("You must specify a repository to clone."); + + if (option_no_hardlinks) + use_local_hardlinks = 0; + + if (option_bare) { + if (option_origin) + die("--bare and --origin %s options are incompatible.", + option_origin); + option_no_checkout = 1; + use_separate_remote = 0; + } + + if (!option_origin) + option_origin = "origin"; + + repo_name = argv[0]; + + path = get_repo_path(repo_name, &is_bundle); + if (path) + repo = path; + else if (!strchr(repo_name, ':')) + repo = xstrdup(make_absolute_path(repo_name)); + else + repo = repo_name; + + if (argc == 2) + dir = xstrdup(argv[1]); + else + dir = guess_dir_name(repo_name, is_bundle); + + if (!stat(dir, &buf)) + die("destination directory '%s' already exists.", dir); + + strbuf_init(&reflog_msg, 0); + strbuf_addf(&reflog_msg, "clone: from %s", repo); + + if (option_bare) + work_tree = NULL; + else { + work_tree = getenv("GIT_WORK_TREE"); + if (work_tree && !stat(work_tree, &buf)) + die("working tree '%s' already exists.", work_tree); + } + + if (option_bare || work_tree) + git_dir = xstrdup(dir); + else { + work_tree = dir; + git_dir = xstrdup(mkpath("%s/.git", dir)); + } + + if (!option_bare) { + junk_work_tree = work_tree; + if (mkdir(work_tree, 0755)) + die("could not create work tree dir '%s'.", work_tree); + set_git_work_tree(work_tree); + } + junk_git_dir = git_dir; + atexit(remove_junk); + signal(SIGINT, remove_junk_on_signal); + + setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1); + + set_git_dir(make_absolute_path(git_dir)); + + fprintf(stderr, "Initialize %s\n", git_dir); + init_db(option_template, option_quiet ? INIT_DB_QUIET : 0); + + if (option_reference) + setup_reference(git_dir); + + git_config(git_default_config); + + if (option_bare) { + strcpy(branch_top, "refs/heads/"); + + git_config_set("core.bare", "true"); + } else { + snprintf(branch_top, sizeof(branch_top), + "refs/remotes/%s/", option_origin); + + /* Configure the remote */ + snprintf(key, sizeof(key), "remote.%s.url", option_origin); + git_config_set(key, repo); + + snprintf(key, sizeof(key), "remote.%s.fetch", option_origin); + snprintf(value, sizeof(value), + "+refs/heads/*:%s*", branch_top); + git_config_set_multivar(key, value, "^$", 0); + } + + refspec.force = 0; + refspec.pattern = 1; + refspec.src = "refs/heads/"; + refspec.dst = branch_top; + + if (path && !is_bundle) + refs = clone_local(path, git_dir); + else { + struct remote *remote = remote_get(argv[0]); + struct transport *transport = transport_get(remote, argv[0]); + + transport_set_option(transport, TRANS_OPT_KEEP, "yes"); + + if (option_depth) + transport_set_option(transport, TRANS_OPT_DEPTH, + option_depth); + + if (option_quiet) + transport->verbose = -1; + + refs = transport_get_remote_refs(transport); + transport_fetch_refs(transport, refs); + } + + clear_extra_refs(); + + mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf); + + head_points_at = locate_head(refs, mapped_refs, &remote_head); + + if (head_points_at) { + /* Local default branch link */ + create_symref("HEAD", head_points_at->name, NULL); + + if (!option_bare) { + struct strbuf head_ref; + const char *head = head_points_at->name; + + if (!prefixcmp(head, "refs/heads/")) + head += 11; + + /* Set up the initial local branch */ + + /* Local branch initial value */ + update_ref(reflog_msg.buf, "HEAD", + head_points_at->old_sha1, + NULL, 0, DIE_ON_ERR); + + strbuf_init(&head_ref, 0); + strbuf_addstr(&head_ref, branch_top); + strbuf_addstr(&head_ref, "HEAD"); + + /* Remote branch link */ + create_symref(head_ref.buf, + head_points_at->peer_ref->name, + reflog_msg.buf); + + snprintf(key, sizeof(key), "branch.%s.remote", head); + git_config_set(key, option_origin); + snprintf(key, sizeof(key), "branch.%s.merge", head); + git_config_set(key, head_points_at->name); + } + } else if (remote_head) { + /* Source had detached HEAD pointing somewhere. */ + if (!option_bare) + update_ref(reflog_msg.buf, "HEAD", + remote_head->old_sha1, + NULL, REF_NODEREF, DIE_ON_ERR); + } else { + /* Nothing to checkout out */ + if (!option_no_checkout) + warning("remote HEAD refers to nonexistent ref, " + "unable to checkout.\n"); + option_no_checkout = 1; + } + + if (!option_no_checkout) { + struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); + struct unpack_trees_options opts; + struct tree *tree; + struct tree_desc t; + int fd; + + /* We need to be in the new work tree for the checkout */ + setup_work_tree(); + + fd = hold_locked_index(lock_file, 1); + + memset(&opts, 0, sizeof opts); + opts.update = 1; + opts.merge = 1; + opts.fn = oneway_merge; + opts.verbose_update = !option_quiet; + opts.src_index = &the_index; + opts.dst_index = &the_index; + + tree = parse_tree_indirect(remote_head->old_sha1); + parse_tree(tree); + init_tree_desc(&t, tree->buffer, tree->size); + unpack_trees(1, &t, &opts); + + if (write_cache(fd, active_cache, active_nr) || + commit_locked_index(lock_file)) + die("unable to write new index file"); + } + + strbuf_release(&reflog_msg); + junk_pid = 0; + return 0; +} diff --git a/builtin-fetch.c b/builtin-fetch.c index f6584ecea1..bfe7711aa8 100644 --- a/builtin-fetch.c +++ b/builtin-fetch.c @@ -127,14 +127,8 @@ static struct ref *get_ref_map(struct transport *transport, /* Merge everything on the command line, but not --tags */ for (rm = ref_map; rm; rm = rm->next) rm->merge = 1; - if (tags == TAGS_SET) { - struct refspec refspec; - refspec.src = "refs/tags/"; - refspec.dst = "refs/tags/"; - refspec.pattern = 1; - refspec.force = 0; - get_fetch_map(remote_refs, &refspec, &tail, 0); - } + if (tags == TAGS_SET) + get_fetch_map(remote_refs, tag_refspec, &tail, 0); } else { /* Use the defaults */ struct remote *remote = transport->remote; diff --git a/builtin-init-db.c b/builtin-init-db.c index b061317275..3968c9911f 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -104,12 +104,14 @@ static void copy_templates_1(char *path, int baselen, } } -static void copy_templates(const char *git_dir, int len, const char *template_dir) +static void copy_templates(const char *template_dir) { char path[PATH_MAX]; char template_path[PATH_MAX]; int template_len; DIR *dir; + const char *git_dir = get_git_dir(); + int len = strlen(git_dir); if (!template_dir) template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); @@ -156,6 +158,8 @@ static void copy_templates(const char *git_dir, int len, const char *template_di } memcpy(path, git_dir, len); + if (len && path[len - 1] != '/') + path[len++] = '/'; path[len] = 0; copy_templates_1(path, len, template_path, template_len, @@ -163,8 +167,9 @@ static void copy_templates(const char *git_dir, int len, const char *template_di closedir(dir); } -static int create_default_files(const char *git_dir, const char *template_path) +static int create_default_files(const char *template_path) { + const char *git_dir = get_git_dir(); unsigned len = strlen(git_dir); static char path[PATH_MAX]; struct stat st1; @@ -183,19 +188,15 @@ static int create_default_files(const char *git_dir, const char *template_path) /* * Create .git/refs/{heads,tags} */ - strcpy(path + len, "refs"); - safe_create_dir(path, 1); - strcpy(path + len, "refs/heads"); - safe_create_dir(path, 1); - strcpy(path + len, "refs/tags"); - safe_create_dir(path, 1); + safe_create_dir(git_path("refs"), 1); + safe_create_dir(git_path("refs/heads"), 1); + safe_create_dir(git_path("refs/tags"), 1); /* First copy the templates -- we might have the default * config file there, in which case we would want to read * from it after installing. */ - path[len] = 0; - copy_templates(path, len, template_path); + copy_templates(template_path); git_config(git_default_config); @@ -204,14 +205,10 @@ static int create_default_files(const char *git_dir, const char *template_path) * shared-repository settings, we would need to fix them up. */ if (shared_repository) { - path[len] = 0; - adjust_shared_perm(path); - strcpy(path + len, "refs"); - adjust_shared_perm(path); - strcpy(path + len, "refs/heads"); - adjust_shared_perm(path); - strcpy(path + len, "refs/tags"); - adjust_shared_perm(path); + adjust_shared_perm(get_git_dir()); + adjust_shared_perm(git_path("refs")); + adjust_shared_perm(git_path("refs/heads")); + adjust_shared_perm(git_path("refs/tags")); } /* @@ -251,8 +248,10 @@ static int create_default_files(const char *git_dir, const char *template_path) /* allow template config file to override the default */ if (log_all_ref_updates == -1) git_config_set("core.logallrefupdates", "true"); - if (work_tree != git_work_tree_cfg) + if (prefixcmp(git_dir, work_tree) || + strcmp(git_dir + strlen(work_tree), "/.git")) { git_config_set("core.worktree", work_tree); + } } if (!reinit) { @@ -278,42 +277,90 @@ static int create_default_files(const char *git_dir, const char *template_path) return reinit; } -static void guess_repository_type(const char *git_dir) +int init_db(const char *template_dir, unsigned int flags) +{ + const char *sha1_dir; + char *path; + int len, reinit; + + safe_create_dir(get_git_dir(), 0); + + /* Check to see if the repository version is right. + * Note that a newly created repository does not have + * config file, so this will not fail. What we are catching + * is an attempt to reinitialize new repository with an old tool. + */ + check_repository_format(); + + reinit = create_default_files(template_dir); + + sha1_dir = get_object_directory(); + len = strlen(sha1_dir); + path = xmalloc(len + 40); + memcpy(path, sha1_dir, len); + + safe_create_dir(sha1_dir, 1); + strcpy(path+len, "/pack"); + safe_create_dir(path, 1); + strcpy(path+len, "/info"); + safe_create_dir(path, 1); + + if (shared_repository) { + char buf[10]; + /* We do not spell "group" and such, so that + * the configuration can be read by older version + * of git. Note, we use octal numbers for new share modes, + * and compatibility values for PERM_GROUP and + * PERM_EVERYBODY. + */ + if (shared_repository == PERM_GROUP) + sprintf(buf, "%d", OLD_PERM_GROUP); + else if (shared_repository == PERM_EVERYBODY) + sprintf(buf, "%d", OLD_PERM_EVERYBODY); + else + sprintf(buf, "0%o", shared_repository); + git_config_set("core.sharedrepository", buf); + git_config_set("receive.denyNonFastforwards", "true"); + } + + if (!(flags & INIT_DB_QUIET)) + printf("%s%s Git repository in %s/\n", + reinit ? "Reinitialized existing" : "Initialized empty", + shared_repository ? " shared" : "", + get_git_dir()); + + return 0; +} + +static int guess_repository_type(const char *git_dir) { char cwd[PATH_MAX]; const char *slash; - if (0 <= is_bare_repository_cfg) - return; - if (!git_dir) - return; - /* * "GIT_DIR=. git init" is always bare. * "GIT_DIR=`pwd` git init" too. */ if (!strcmp(".", git_dir)) - goto force_bare; + return 1; if (!getcwd(cwd, sizeof(cwd))) die("cannot tell cwd"); if (!strcmp(git_dir, cwd)) - goto force_bare; + return 1; /* * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. */ if (!strcmp(git_dir, ".git")) - return; + return 0; slash = strrchr(git_dir, '/'); if (slash && !strcmp(slash, "/.git")) - return; + return 0; /* * Otherwise it is often bare. At this point * we are just guessing. */ - force_bare: - is_bare_repository_cfg = 1; - return; + return 1; } static const char init_db_usage[] = @@ -328,11 +375,9 @@ static const char init_db_usage[] = int cmd_init_db(int argc, const char **argv, const char *prefix) { const char *git_dir; - const char *sha1_dir; const char *template_dir = NULL; - char *path; - int len, i, reinit; - int quiet = 0; + unsigned int flags = 0; + int i; for (i = 1; i < argc; i++, argv++) { const char *arg = argv[1]; @@ -343,7 +388,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) else if (!prefixcmp(arg, "--shared=")) shared_repository = git_config_perm("arg", arg+9); else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) - quiet = 1; + flags |= INIT_DB_QUIET; else usage(init_db_usage); } @@ -360,71 +405,35 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) GIT_WORK_TREE_ENVIRONMENT, GIT_DIR_ENVIRONMENT); - guess_repository_type(git_dir); - - if (is_bare_repository_cfg <= 0) { - git_work_tree_cfg = xcalloc(PATH_MAX, 1); - if (!getcwd(git_work_tree_cfg, PATH_MAX)) - die ("Cannot access current working directory."); - if (access(get_git_work_tree(), X_OK)) - die ("Cannot access work tree '%s'", - get_git_work_tree()); - } - /* * Set up the default .git directory contents */ - git_dir = getenv(GIT_DIR_ENVIRONMENT); if (!git_dir) git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; - safe_create_dir(git_dir, 0); - - /* Check to see if the repository version is right. - * Note that a newly created repository does not have - * config file, so this will not fail. What we are catching - * is an attempt to reinitialize new repository with an old tool. - */ - check_repository_format(); - - reinit = create_default_files(git_dir, template_dir); - - /* - * And set up the object store. - */ - sha1_dir = get_object_directory(); - len = strlen(sha1_dir); - path = xmalloc(len + 40); - memcpy(path, sha1_dir, len); - - safe_create_dir(sha1_dir, 1); - strcpy(path+len, "/pack"); - safe_create_dir(path, 1); - strcpy(path+len, "/info"); - safe_create_dir(path, 1); - if (shared_repository) { - char buf[10]; - /* We do not spell "group" and such, so that - * the configuration can be read by older version - * of git. Note, we use octal numbers for new share modes, - * and compatibility values for PERM_GROUP and - * PERM_EVERYBODY. - */ - if (shared_repository == PERM_GROUP) - sprintf(buf, "%d", OLD_PERM_GROUP); - else if (shared_repository == PERM_EVERYBODY) - sprintf(buf, "%d", OLD_PERM_EVERYBODY); - else - sprintf(buf, "0%o", shared_repository); - git_config_set("core.sharedrepository", buf); - git_config_set("receive.denyNonFastforwards", "true"); + if (is_bare_repository_cfg < 0) + is_bare_repository_cfg = guess_repository_type(git_dir); + + if (!is_bare_repository_cfg) { + if (git_dir) { + const char *git_dir_parent = strrchr(git_dir, '/'); + if (git_dir_parent) { + char *rel = xstrndup(git_dir, git_dir_parent - git_dir); + git_work_tree_cfg = xstrdup(make_absolute_path(rel)); + free(rel); + } + } + if (!git_work_tree_cfg) { + git_work_tree_cfg = xcalloc(PATH_MAX, 1); + if (!getcwd(git_work_tree_cfg, PATH_MAX)) + die ("Cannot access current working directory."); + } + if (access(get_git_work_tree(), X_OK)) + die ("Cannot access work tree '%s'", + get_git_work_tree()); } - if (!quiet) - printf("%s%s Git repository in %s/\n", - reinit ? "Reinitialized existing" : "Initialized empty", - shared_repository ? " shared" : "", - git_dir); + set_git_dir(make_absolute_path(git_dir)); - return 0; + return init_db(template_dir, flags); } diff --git a/builtin-mv.c b/builtin-mv.c index fb8ffb41aa..fb906b3fc7 100644 --- a/builtin-mv.c +++ b/builtin-mv.c @@ -256,7 +256,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) for (i = 0; i < added.nr; i++) { const char *path = added.items[i].path; - if (add_file_to_cache(path, verbose)) + if (add_file_to_cache(path, verbose ? ADD_CACHE_VERBOSE : 0)) die("updating index entries failed"); } diff --git a/builtin-update-index.c b/builtin-update-index.c index a8795d3d5f..d4c85c0cbc 100644 --- a/builtin-update-index.c +++ b/builtin-update-index.c @@ -593,6 +593,10 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) refresh_flags |= REFRESH_QUIET; continue; } + if (!strcmp(path, "--ignore-submodules")) { + refresh_flags |= REFRESH_IGNORE_SUBMODULES; + continue; + } if (!strcmp(path, "--add")) { allow_add = 1; continue; @@ -25,6 +25,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix); extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix); extern int cmd_cherry(int argc, const char **argv, const char *prefix); extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix); +extern int cmd_clone(int argc, const char **argv, const char *prefix); extern int cmd_clean(int argc, const char **argv, const char *prefix); extern int cmd_commit(int argc, const char **argv, const char *prefix); extern int cmd_commit_tree(int argc, const char **argv, const char *prefix); @@ -261,8 +261,8 @@ static inline void remove_name_hash(struct cache_entry *ce) #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option)) #define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos)) #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path)) -#define add_to_cache(path, st, verbose) add_to_index(&the_index, (path), (st), (verbose)) -#define add_file_to_cache(path, verbose) add_file_to_index(&the_index, (path), (verbose)) +#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags)) +#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags)) #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL) #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options)) #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options)) @@ -317,6 +317,7 @@ extern char *get_graft_file(void); extern int set_git_dir(const char *path); extern const char *get_git_work_tree(void); extern const char *read_gitfile_gently(const char *path); +extern void set_git_work_tree(const char *tree); #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" @@ -329,6 +330,10 @@ extern const char *prefix_filename(const char *prefix, int len, const char *path extern void verify_filename(const char *prefix, const char *name); extern void verify_non_filename(const char *prefix, const char *name); +#define INIT_DB_QUIET 0x0001 + +extern int init_db(const char *template_dir, unsigned int flags); + #define alloc_nr(x) (((x)+16)*3/2) /* @@ -366,8 +371,11 @@ extern int add_index_entry(struct index_state *, struct cache_entry *ce, int opt extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really); extern int remove_index_entry_at(struct index_state *, int pos); extern int remove_file_from_index(struct index_state *, const char *path); -extern int add_to_index(struct index_state *, const char *path, struct stat *, int verbose); -extern int add_file_to_index(struct index_state *, const char *path, int verbose); +#define ADD_CACHE_VERBOSE 1 +#define ADD_CACHE_PRETEND 2 +#define ADD_CACHE_IGNORE_ERRORS 4 +extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags); +extern int add_file_to_index(struct index_state *, const char *path, int flags); extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh); extern int ce_same_name(struct cache_entry *a, struct cache_entry *b); @@ -388,6 +396,7 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st); #define REFRESH_UNMERGED 0x0002 /* allow unmerged */ #define REFRESH_QUIET 0x0004 /* be quiet about it */ #define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */ +#define REFRESH_IGNORE_SUBMODULES 0x0008 /* ignore submodules */ extern int refresh_index(struct index_state *, unsigned int flags, const char **pathspec, char *seen); struct lock_file { @@ -398,6 +407,7 @@ struct lock_file { char filename[PATH_MAX]; }; extern int hold_lock_file_for_update(struct lock_file *, const char *path, int); +extern int hold_lock_file_for_append(struct lock_file *, const char *path, int); extern int commit_lock_file(struct lock_file *); extern int hold_locked_index(struct lock_file *, int); @@ -615,6 +625,7 @@ extern struct alternate_object_database { char base[FLEX_ARRAY]; /* more */ } *alt_odb_list; extern void prepare_alt_odb(void); +extern void add_to_alternates_file(const char *reference); struct pack_window { struct pack_window *next; @@ -783,8 +794,6 @@ extern int convert_to_git(const char *path, const char *src, size_t len, extern int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst); /* add */ -#define ADD_FILES_VERBOSE 01 -#define ADD_FILES_IGNORE_ERRORS 02 /* * return 0 if success, 1 - if addition of a file failed and * ADD_FILES_IGNORE_ERRORS was specified in flags diff --git a/git-clone.sh b/contrib/examples/git-clone.sh index 547228e13c..547228e13c 100755 --- a/git-clone.sh +++ b/contrib/examples/git-clone.sh @@ -2496,6 +2496,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac) DIFF_OPT_SET(options, ALLOW_EXTERNAL); else if (!strcmp(arg, "--no-ext-diff")) DIFF_OPT_CLR(options, ALLOW_EXTERNAL); + else if (!strcmp(arg, "--ignore-submodules")) + DIFF_OPT_SET(options, IGNORE_SUBMODULES); /* misc options */ else if (!strcmp(arg, "-z")) @@ -3355,6 +3357,9 @@ void diff_addremove(struct diff_options *options, char concatpath[PATH_MAX]; struct diff_filespec *one, *two; + if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(mode)) + return; + /* This may look odd, but it is a preparation for * feeding "there are unchanged files which should * not produce diffs, but when you are doing copy @@ -3399,6 +3404,10 @@ void diff_change(struct diff_options *options, char concatpath[PATH_MAX]; struct diff_filespec *one, *two; + if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(old_mode) + && S_ISGITLINK(new_mode)) + return; + if (DIFF_OPT_TST(options, REVERSE_DIFF)) { unsigned tmp; const unsigned char *tmp_c; @@ -63,6 +63,7 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q, #define DIFF_OPT_REVERSE_DIFF (1 << 15) #define DIFF_OPT_CHECK_FAILED (1 << 16) #define DIFF_OPT_RELATIVE_NAME (1 << 17) +#define DIFF_OPT_IGNORE_SUBMODULES (1 << 18) #define DIFF_OPT_TST(opts, flag) ((opts)->flags & DIFF_OPT_##flag) #define DIFF_OPT_SET(opts, flag) ((opts)->flags |= DIFF_OPT_##flag) #define DIFF_OPT_CLR(opts, flag) ((opts)->flags &= ~DIFF_OPT_##flag) diff --git a/environment.c b/environment.c index 4fcb471248..73feb2d03a 100644 --- a/environment.c +++ b/environment.c @@ -44,7 +44,7 @@ enum rebase_setup_type autorebase = AUTOREBASE_NEVER; /* This is set by setup_git_dir_gently() and/or git_default_config() */ char *git_work_tree_cfg; -static const char *work_tree; +static char *work_tree; static const char *git_dir; static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file; @@ -86,10 +86,26 @@ const char *get_git_dir(void) return git_dir; } +static int git_work_tree_initialized; + +/* + * Note. This works only before you used a work tree. This was added + * primarily to support git-clone to work in a new repository it just + * created, and is not meant to flip between different work trees. + */ +void set_git_work_tree(const char *new_work_tree) +{ + if (is_bare_repository_cfg >= 0) + die("cannot set work tree after initialization"); + git_work_tree_initialized = 1; + free(work_tree); + work_tree = xstrdup(make_absolute_path(new_work_tree)); + is_bare_repository_cfg = 0; +} + const char *get_git_work_tree(void) { - static int initialized = 0; - if (!initialized) { + if (!git_work_tree_initialized) { work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); /* core.bare = true overrides implicit and config work tree */ if (!work_tree && is_bare_repository_cfg < 1) { @@ -99,7 +115,7 @@ const char *get_git_work_tree(void) work_tree = xstrdup(make_absolute_path(git_path(work_tree))); } else if (work_tree) work_tree = xstrdup(make_absolute_path(work_tree)); - initialized = 1; + git_work_tree_initialized = 1; if (work_tree) is_bare_repository_cfg = 0; } diff --git a/git-bisect.sh b/git-bisect.sh index 164e8ed81f..4bcbaceb8b 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -63,40 +63,40 @@ bisect_autostart() { bisect_start() { # - # Verify HEAD. If we were bisecting before this, reset to the - # top-of-line master first! + # Verify HEAD. # head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) || head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) || die "Bad HEAD - I need a HEAD" + # - # Check that we either already have BISECT_START, or that the - # branches bisect, new-bisect don't exist, to not override them. + # Check if we are bisecting. # - test -s "$GIT_DIR/BISECT_START" || - if git show-ref --verify -q refs/heads/bisect || - git show-ref --verify -q refs/heads/new-bisect; then - die 'The branches "bisect" and "new-bisect" must not exist.' - fi start_head='' - case "$head" in - refs/heads/bisect) - branch=`cat "$GIT_DIR/BISECT_START"` - git checkout $branch || exit - ;; - refs/heads/*|$_x40) - # This error message should only be triggered by cogito usage, - # and cogito users should understand it relates to cg-seek. - [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree" - start_head="${head#refs/heads/}" - ;; - *) - die "Bad HEAD - strange symbolic ref" - ;; - esac + if test -s "$GIT_DIR/BISECT_START" + then + # Reset to the rev from where we started. + start_head=$(cat "$GIT_DIR/BISECT_START") + git checkout "$start_head" || exit + else + # Get rev from where we start. + case "$head" in + refs/heads/*|$_x40) + # This error message should only be triggered by + # cogito usage, and cogito users should understand + # it relates to cg-seek. + [ -s "$GIT_DIR/head-name" ] && + die "won't bisect on seeked tree" + start_head="${head#refs/heads/}" + ;; + *) + die "Bad HEAD - strange symbolic ref" + ;; + esac + fi # - # Get rid of any old bisect state + # Get rid of any old bisect state. # bisect_clean_state @@ -118,7 +118,7 @@ bisect_start() { break ;; *) - rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || { + rev=$(git rev-parse -q --verify "$arg^{commit}") || { test $has_double_dash -eq 1 && die "'$arg' does not appear to be a valid revision" break @@ -133,11 +133,29 @@ bisect_start() { esac done - sq "$@" >"$GIT_DIR/BISECT_NAMES" - test -n "$start_head" && echo "$start_head" >"$GIT_DIR/BISECT_START" - eval "$eval" - echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" + # + # Change state. + # In case of mistaken revs or checkout error, or signals received, + # "bisect_auto_next" below may exit or misbehave. + # We have to trap this to be able to clean up using + # "bisect_clean_state". + # + trap 'bisect_clean_state' 0 + trap 'exit 255' 1 2 3 15 + + # + # Write new start state. + # + sq "$@" >"$GIT_DIR/BISECT_NAMES" && + echo "$start_head" >"$GIT_DIR/BISECT_START" && + eval "$eval" && + echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit + # + # Check if we can proceed to the next bisect state. + # bisect_auto_next + + trap '-' 0 } bisect_write() { @@ -149,9 +167,9 @@ bisect_write() { good|skip) tag="$state"-"$rev" ;; *) die "Bad bisect_write argument: $state" ;; esac - git update-ref "refs/bisect/$tag" "$rev" + git update-ref "refs/bisect/$tag" "$rev" || exit echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG" - test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" + test -n "$nolog" || echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" } bisect_state() { @@ -348,9 +366,7 @@ bisect_next() { exit_if_skipped_commits "$bisect_rev" echo "Bisecting: $bisect_nr revisions left to test after this" - git branch -D new-bisect 2> /dev/null - git checkout -q -b new-bisect "$bisect_rev" || exit - git branch -M new-bisect bisect + git checkout -q "$bisect_rev" || exit git show-branch "$bisect_rev" } @@ -392,24 +408,22 @@ bisect_reset() { *) usage ;; esac - if git checkout "$branch"; then - # Cleanup head-name if it got left by an old version of git-bisect - rm -f "$GIT_DIR/head-name" - rm -f "$GIT_DIR/BISECT_START" - bisect_clean_state - fi + git checkout "$branch" && bisect_clean_state } bisect_clean_state() { # There may be some refs packed during bisection. - git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect | + git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* | while read ref hash do git update-ref -d $ref $hash done + rm -f "$GIT_DIR/BISECT_START" rm -f "$GIT_DIR/BISECT_LOG" rm -f "$GIT_DIR/BISECT_NAMES" rm -f "$GIT_DIR/BISECT_RUN" + # Cleanup head-name if it got left by an old version of git-bisect + rm -f "$GIT_DIR/head-name" } bisect_replay () { diff --git a/git-cvsexportcommit.perl b/git-cvsexportcommit.perl index c93bd9c9b5..c6c70e9eba 100755 --- a/git-cvsexportcommit.perl +++ b/git-cvsexportcommit.perl @@ -8,9 +8,9 @@ use File::Basename qw(basename dirname); use File::Spec; use Git; -our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w); +our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w, $opt_W); -getopts('uhPpvcfam:d:w:'); +getopts('uhPpvcfam:d:w:W'); $opt_h && usage(); @@ -20,7 +20,7 @@ die "Need at least one commit identifier!" unless @ARGV; my $repo = Git->repository(); $opt_w = $repo->config('cvsexportcommit.cvsdir') unless defined $opt_w; -if ($opt_w) { +if ($opt_w || $opt_W) { # Remember where GIT_DIR is before changing to CVS checkout unless ($ENV{GIT_DIR}) { # No GIT_DIR set. Figure it out for ourselves @@ -30,7 +30,9 @@ if ($opt_w) { } # Make sure GIT_DIR is absolute $ENV{GIT_DIR} = File::Spec->rel2abs($ENV{GIT_DIR}); +} +if ($opt_w) { if (! -d $opt_w."/CVS" ) { die "$opt_w is not a CVS checkout"; } @@ -121,6 +123,15 @@ if ($parent) { } } +my $go_back_to = 0; + +if ($opt_W) { + $opt_v && print "Resetting to $parent\n"; + $go_back_to = `git symbolic-ref HEAD 2> /dev/null || + git rev-parse HEAD` || die "Could not determine current branch"; + system("git checkout -q $parent^0") && die "Could not check out $parent^0"; +} + $opt_v && print "Applying to CVS commit $commit from parent $parent\n"; # grab the commit message @@ -215,7 +226,8 @@ if (@canstatusfiles) { my $basename = basename($name); $basename = "no file " . $basename if (exists($added{$basename})); - chomp($basename); + $basename =~ s/^\s+//; + $basename =~ s/\s+$//; if (!exists($fullname{$basename})) { $fullname{$basename} = $name; @@ -264,7 +276,11 @@ if ($dirty) { } print "Applying\n"; -`GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch"; +if ($opt_W) { + system("git checkout -q $commit^0") && die "cannot patch"; +} else { + `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch"; +} print "Patch applied successfully. Adding new files and directories to CVS\n"; my $dirtypatch = 0; @@ -317,7 +333,9 @@ if ($dirtypatch) { print "using a patch program. After applying the patch and resolving the\n"; print "problems you may commit using:"; print "\n cd \"$opt_w\"" if $opt_w; - print "\n $cmd\n\n"; + print "\n $cmd\n"; + print "\n git checkout $go_back_to\n" if $go_back_to; + print "\n"; exit(1); } @@ -337,6 +355,14 @@ if ($opt_c) { # clean up unlink(".cvsexportcommit.diff"); +if ($opt_W) { + system("git checkout $go_back_to") && die "cannot move back to $go_back_to"; + if (!($go_back_to =~ /^[0-9a-fA-F]{40}$/)) { + system("git symbolic-ref HEAD $go_back_to") && + die "cannot move back to $go_back_to"; + } +} + # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp # used by CVS and the one set by subsequence file modifications are different. # If they are not different CVS will not detect changes. diff --git a/git-cvsimport.perl b/git-cvsimport.perl index bdac5d51b6..5a0255052c 100755 --- a/git-cvsimport.perl +++ b/git-cvsimport.perl @@ -780,6 +780,7 @@ sub commit { $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY ** $xtag =~ tr/_/\./ if ( $opt_u ); $xtag =~ s/[\/]/$opt_s/g; + $xtag =~ s/\[//g; system('git-tag', '-f', $xtag, $cid) == 0 or die "Cannot create tag $xtag: $!\n"; diff --git a/git-cvsserver.perl b/git-cvsserver.perl index 29dbfc940b..920bbe15a3 100755 --- a/git-cvsserver.perl +++ b/git-cvsserver.perl @@ -21,6 +21,7 @@ use bytes; use Fcntl; use File::Temp qw/tempdir tempfile/; +use File::Path qw/rmtree/; use File::Basename; use Getopt::Long qw(:config require_order no_ignore_case); @@ -86,6 +87,17 @@ my $methods = { # $state holds all the bits of information the clients sends us that could # potentially be useful when it comes to actually _doing_ something. my $state = { prependdir => '' }; + +# Work is for managing temporary working directory +my $work = + { + state => undef, # undef, 1 (empty), 2 (with stuff) + workDir => undef, + index => undef, + emptyDir => undef, + tmpDir => undef + }; + $log->info("--------------- STARTING -----------------"); my $usage = @@ -189,6 +201,9 @@ while (<STDIN>) $log->debug("Processing time : user=" . (times)[0] . " system=" . (times)[1]); $log->info("--------------- FINISH -----------------"); +chdir '/'; +exit 0; + # Magic catchall method. # This is the method that will handle all commands we haven't yet # implemented. It simply sends a warning to the log file indicating a @@ -487,7 +502,7 @@ sub req_add print $state->{CVSROOT} . "/$state->{module}/$filename\n"; # this is an "entries" line - my $kopts = kopts_from_path($filepart); + my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash}); $log->debug("/$filepart/1.$meta->{revision}//$kopts/"); print "/$filepart/1.$meta->{revision}//$kopts/\n"; # permissions @@ -518,9 +533,26 @@ sub req_add print "Checked-in $dirpart\n"; print "$filename\n"; - my $kopts = kopts_from_path($filepart); + my $kopts = kopts_from_path($filename,"file", + $state->{entries}{$filename}{modified_filename}); print "/$filepart/0//$kopts/\n"; + my $requestedKopts = $state->{opt}{k}; + if(defined($requestedKopts)) + { + $requestedKopts = "-k$requestedKopts"; + } + else + { + $requestedKopts = ""; + } + if( $kopts ne $requestedKopts ) + { + $log->warn("Ignoring requested -k='$requestedKopts'" + . " for '$filename'; detected -k='$kopts' instead"); + #TODO: Also have option to send warning to user? + } + $addcount++; } @@ -600,7 +632,7 @@ sub req_remove print "Checked-in $dirpart\n"; print "$filename\n"; - my $kopts = kopts_from_path($filepart); + my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash}); print "/$filepart/-1.$wrev//$kopts/\n"; $rmcount++; @@ -770,6 +802,7 @@ sub req_co argsplit("co"); my $module = $state->{args}[0]; + $state->{module} = $module; my $checkout_path = $module; # use the user specified directory if we're given it @@ -847,6 +880,7 @@ sub req_co # Don't want to check out deleted files next if ( $git->{filehash} eq "deleted" ); + my $fullName = $git->{name}; ( $git->{name}, $git->{dir} ) = filenamesplit($git->{name}); if (length($git->{dir}) && $git->{dir} ne './' @@ -877,7 +911,7 @@ sub req_co print $state->{CVSROOT} . "/$module/" . ( defined ( $git->{dir} ) and $git->{dir} ne "./" ? $git->{dir} . "/" : "" ) . "$git->{name}\n"; # this is an "entries" line - my $kopts = kopts_from_path($git->{name}); + my $kopts = kopts_from_path($fullName,"sha1",$git->{filehash}); print "/$git->{name}/1.$git->{revision}//$kopts/\n"; # permissions print "u=$git->{mode},g=$git->{mode},o=$git->{mode}\n"; @@ -1086,7 +1120,7 @@ sub req_update print $state->{CVSROOT} . "/$state->{module}/$filename\n"; # this is an "entries" line - my $kopts = kopts_from_path($filepart); + my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash}); $log->debug("/$filepart/1.$meta->{revision}//$kopts/"); print "/$filepart/1.$meta->{revision}//$kopts/\n"; @@ -1101,10 +1135,10 @@ sub req_update $log->info("Updating '$filename'"); my ( $filepart, $dirpart ) = filenamesplit($meta->{name},1); - my $dir = tempdir( DIR => $TEMP_DIR, CLEANUP => 1 ) . "/"; + my $mergeDir = setupTmpDir(); - chdir $dir; my $file_local = $filepart . ".mine"; + my $mergedFile = "$mergeDir/$file_local"; system("ln","-s",$state->{entries}{$filename}{modified_filename}, $file_local); my $file_old = $filepart . "." . $oldmeta->{revision}; transmitfile($oldmeta->{filehash}, { targetfile => $file_old }); @@ -1115,11 +1149,13 @@ sub req_update $log->info("Merging $file_local, $file_old, $file_new"); print "M Merging differences between 1.$oldmeta->{revision} and 1.$meta->{revision} into $filename\n"; - $log->debug("Temporary directory for merge is $dir"); + $log->debug("Temporary directory for merge is $mergeDir"); my $return = system("git", "merge-file", $file_local, $file_old, $file_new); $return >>= 8; + cleanupTmpDir(); + if ( $return == 0 ) { $log->info("Merged successfully"); @@ -1132,7 +1168,8 @@ sub req_update print "Merged $dirpart\n"; $log->debug($state->{CVSROOT} . "/$state->{module}/$filename"); print $state->{CVSROOT} . "/$state->{module}/$filename\n"; - my $kopts = kopts_from_path($filepart); + my $kopts = kopts_from_path("$dirpart/$filepart", + "file",$mergedFile); $log->debug("/$filepart/1.$meta->{revision}//$kopts/"); print "/$filepart/1.$meta->{revision}//$kopts/\n"; } @@ -1148,7 +1185,8 @@ sub req_update { print "Merged $dirpart\n"; print $state->{CVSROOT} . "/$state->{module}/$filename\n"; - my $kopts = kopts_from_path($filepart); + my $kopts = kopts_from_path("$dirpart/$filepart", + "file",$mergedFile); print "/$filepart/1.$meta->{revision}/+/$kopts/\n"; } } @@ -1168,13 +1206,11 @@ sub req_update # transmit file, format is single integer on a line by itself (file # size) followed by the file contents # TODO : we should copy files in blocks - my $data = `cat $file_local`; + my $data = `cat $mergedFile`; $log->debug("File size : " . length($data)); print length($data) . "\n"; print $data; } - - chdir "/"; } } @@ -1195,6 +1231,7 @@ sub req_ci if ( $state->{method} eq 'pserver') { print "error 1 pserver access cannot commit\n"; + cleanupWorkTree(); exit; } @@ -1202,6 +1239,7 @@ sub req_ci { $log->warn("file 'index' already exists in the git repository"); print "error 1 Index already exists in git repo\n"; + cleanupWorkTree(); exit; } @@ -1209,31 +1247,20 @@ sub req_ci my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log); $updater->update(); - my $tmpdir = tempdir ( DIR => $TEMP_DIR ); - my ( undef, $file_index ) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 ); - $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. my $parenthash = `git show-ref -s refs/heads/$state->{module}`; chomp $parenthash; if ($parenthash !~ /^[0-9a-f]{40}$/) { print "error 1 pserver cannot find the current HEAD of module"; + cleanupWorkTree(); exit; } - chdir $tmpdir; + setupWorkTree($parenthash); - # populate the temporary index - system("git-read-tree", $parenthash); - unless ($? == 0) - { - die "Error running git-read-tree $state->{module} $file_index $!"; - } - $log->info("Created index '$file_index' for head $state->{module} - exit status $?"); + $log->info("Lockless commit start, basing commit on '$work->{workDir}', index file is '$work->{index}'"); + + $log->info("Created index '$work->{index}' for head $state->{module} - exit status $?"); my @committedfiles = (); my %oldmeta; @@ -1271,7 +1298,7 @@ sub req_ci { # fail everything if an up to date check fails print "error 1 Up to date check failed for $filename\n"; - chdir "/"; + cleanupWorkTree(); exit; } @@ -1313,7 +1340,7 @@ sub req_ci { print "E No files to commit\n"; print "ok\n"; - chdir "/"; + cleanupWorkTree(); return; } @@ -1336,7 +1363,7 @@ sub req_ci { $log->warn("Commit failed (Invalid commit hash)"); print "error 1 Commit failed (unknown reason)\n"; - chdir "/"; + cleanupWorkTree(); exit; } @@ -1348,7 +1375,7 @@ sub req_ci { $log->warn("Commit failed (update hook declined to update ref)"); print "error 1 Commit failed (update hook declined)\n"; - chdir "/"; + cleanupWorkTree(); exit; } } @@ -1358,6 +1385,7 @@ sub req_ci "refs/heads/$state->{module}", $commithash, $parenthash)) { $log->warn("update-ref for $state->{module} failed."); print "error 1 Cannot commit -- update first\n"; + cleanupWorkTree(); exit; } @@ -1409,12 +1437,12 @@ sub req_ci } print "Checked-in $dirpart\n"; print "$filename\n"; - my $kopts = kopts_from_path($filepart); + my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash}); print "/$filepart/1.$meta->{revision}//$kopts/\n"; } } - chdir "/"; + cleanupWorkTree(); print "ok\n"; } @@ -1757,15 +1785,9 @@ sub req_annotate argsfromdir($updater); # we'll need a temporary checkout dir - my $tmpdir = tempdir ( DIR => $TEMP_DIR ); - my ( undef, $file_index ) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 ); - $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; + setupWorkTree(); - chdir $tmpdir; + $log->info("Temp checkoutdir creation successful, basing annotate session work on '$work->{workDir}', index file is '$ENV{GIT_INDEX_FILE}'"); # foreach file specified on the command line ... foreach my $filename ( @{$state->{args}} ) @@ -1789,10 +1811,10 @@ sub req_annotate system("git-read-tree", $lastseenin); unless ($? == 0) { - print "E error running git-read-tree $lastseenin $file_index $!\n"; + print "E error running git-read-tree $lastseenin $ENV{GIT_INDEX_FILE} $!\n"; return; } - $log->info("Created index '$file_index' with commit $lastseenin - exit status $?"); + $log->info("Created index '$ENV{GIT_INDEX_FILE}' with commit $lastseenin - exit status $?"); # do a checkout of the file system('git-checkout-index', '-f', '-u', $filename); @@ -1808,7 +1830,7 @@ sub req_annotate # git-jsannotate telling us about commits we are hiding # from the client. - my $a_hints = "$tmpdir/.annotate_hints"; + my $a_hints = "$work->{workDir}/.annotate_hints"; if (!open(ANNOTATEHINTS, '>', $a_hints)) { print "E failed to open '$a_hints' for writing: $!\n"; return; @@ -1862,7 +1884,7 @@ sub req_annotate } # done; get out of the tempdir - chdir "/"; + cleanupWorkDir(); print "ok\n"; @@ -2115,26 +2137,388 @@ sub filecleanup return $filename; } +sub validateGitDir +{ + if( !defined($state->{CVSROOT}) ) + { + print "error 1 CVSROOT not specified\n"; + cleanupWorkTree(); + exit; + } + if( $ENV{GIT_DIR} ne ($state->{CVSROOT} . '/') ) + { + print "error 1 Internally inconsistent CVSROOT\n"; + cleanupWorkTree(); + exit; + } +} + +# Setup working directory in a work tree with the requested version +# loaded in the index. +sub setupWorkTree +{ + my ($ver) = @_; + + validateGitDir(); + + if( ( defined($work->{state}) && $work->{state} != 1 ) || + defined($work->{tmpDir}) ) + { + $log->warn("Bad work tree state management"); + print "error 1 Internal setup multiple work trees without cleanup\n"; + cleanupWorkTree(); + exit; + } + + $work->{workDir} = tempdir ( DIR => $TEMP_DIR ); + + if( !defined($work->{index}) ) + { + (undef, $work->{index}) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 ); + } + + chdir $work->{workDir} or + die "Unable to chdir to $work->{workDir}\n"; + + $log->info("Setting up GIT_WORK_TREE as '.' in '$work->{workDir}', index file is '$work->{index}'"); + + $ENV{GIT_WORK_TREE} = "."; + $ENV{GIT_INDEX_FILE} = $work->{index}; + $work->{state} = 2; + + if($ver) + { + system("git","read-tree",$ver); + unless ($? == 0) + { + $log->warn("Error running git-read-tree"); + die "Error running git-read-tree $ver in $work->{workDir} $!\n"; + } + } + # else # req_annotate reads tree for each file +} + +# Ensure current directory is in some kind of working directory, +# with a recent version loaded in the index. +sub ensureWorkTree +{ + if( defined($work->{tmpDir}) ) + { + $log->warn("Bad work tree state management [ensureWorkTree()]"); + print "error 1 Internal setup multiple dirs without cleanup\n"; + cleanupWorkTree(); + exit; + } + if( $work->{state} ) + { + return; + } + + validateGitDir(); + + if( !defined($work->{emptyDir}) ) + { + $work->{emptyDir} = tempdir ( DIR => $TEMP_DIR, OPEN => 0); + } + chdir $work->{emptyDir} or + die "Unable to chdir to $work->{emptyDir}\n"; + + my $ver = `git show-ref -s refs/heads/$state->{module}`; + chomp $ver; + if ($ver !~ /^[0-9a-f]{40}$/) + { + $log->warn("Error from git show-ref -s refs/head$state->{module}"); + print "error 1 cannot find the current HEAD of module"; + cleanupWorkTree(); + exit; + } + + if( !defined($work->{index}) ) + { + (undef, $work->{index}) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 ); + } + + $ENV{GIT_WORK_TREE} = "."; + $ENV{GIT_INDEX_FILE} = $work->{index}; + $work->{state} = 1; + + system("git","read-tree",$ver); + unless ($? == 0) + { + die "Error running git-read-tree $ver $!\n"; + } +} + +# Cleanup working directory that is not needed any longer. +sub cleanupWorkTree +{ + if( ! $work->{state} ) + { + return; + } + + chdir "/" or die "Unable to chdir '/'\n"; + + if( defined($work->{workDir}) ) + { + rmtree( $work->{workDir} ); + undef $work->{workDir}; + } + undef $work->{state}; +} + +# Setup a temporary directory (not a working tree), typically for +# merging dirty state as in req_update. +sub setupTmpDir +{ + $work->{tmpDir} = tempdir ( DIR => $TEMP_DIR ); + chdir $work->{tmpDir} or die "Unable to chdir $work->{tmpDir}\n"; + + return $work->{tmpDir}; +} + +# Clean up a previously setupTmpDir. Restore previous work tree if +# appropriate. +sub cleanupTmpDir +{ + if ( !defined($work->{tmpDir}) ) + { + $log->warn("cleanup tmpdir that has not been setup"); + die "Cleanup tmpDir that has not been setup\n"; + } + if( defined($work->{state}) ) + { + if( $work->{state} == 1 ) + { + chdir $work->{emptyDir} or + die "Unable to chdir to $work->{emptyDir}\n"; + } + elsif( $work->{state} == 2 ) + { + chdir $work->{workDir} or + die "Unable to chdir to $work->{emptyDir}\n"; + } + else + { + $log->warn("Inconsistent work dir state"); + die "Inconsistent work dir state\n"; + } + } + else + { + chdir "/" or die "Unable to chdir '/'\n"; + } +} + # Given a path, this function returns a string containing the kopts # that should go into that path's Entries line. For example, a binary # file should get -kb. sub kopts_from_path { - my ($path) = @_; + my ($path, $srcType, $name) = @_; - # Once it exists, the git attributes system should be used to look up - # what attributes apply to this path. + if ( defined ( $cfg->{gitcvs}{usecrlfattr} ) and + $cfg->{gitcvs}{usecrlfattr} =~ /\s*(1|true|yes)\s*$/i ) + { + my ($val) = check_attr( "crlf", $path ); + if ( $val eq "set" ) + { + return ""; + } + elsif ( $val eq "unset" ) + { + return "-kb" + } + else + { + $log->info("Unrecognized check_attr crlf $path : $val"); + } + } - # Until then, take the setting from the config file - unless ( defined ( $cfg->{gitcvs}{allbinary} ) and $cfg->{gitcvs}{allbinary} =~ /^\s*(1|true|yes)\s*$/i ) + if ( defined ( $cfg->{gitcvs}{allbinary} ) ) { - # Return "" to give no special treatment to any path - return ""; - } else { - # Alternatively, to have all files treated as if they are binary (which - # is more like git itself), always return the "-kb" option - return "-kb"; + if( ($cfg->{gitcvs}{allbinary} =~ /^\s*(1|true|yes)\s*$/i) ) + { + return "-kb"; + } + elsif( ($cfg->{gitcvs}{allbinary} =~ /^\s*guess\s*$/i) ) + { + if( $srcType eq "sha1Or-k" && + !defined($name) ) + { + my ($ret)=$state->{entries}{$path}{options}; + if( !defined($ret) ) + { + $ret=$state->{opt}{k}; + if(defined($ret)) + { + $ret="-k$ret"; + } + else + { + $ret=""; + } + } + if( ! ($ret=~/^(|-kb|-kkv|-kkvl|-kk|-ko|-kv)$/) ) + { + print "E Bad -k option\n"; + $log->warn("Bad -k option: $ret"); + die "Error: Bad -k option: $ret\n"; + } + + return $ret; + } + else + { + if( is_binary($srcType,$name) ) + { + $log->debug("... as binary"); + return "-kb"; + } + else + { + $log->debug("... as text"); + } + } + } + } + # Return "" to give no special treatment to any path + return ""; +} + +sub check_attr +{ + my ($attr,$path) = @_; + ensureWorkTree(); + if ( open my $fh, '-|', "git", "check-attr", $attr, "--", $path ) + { + my $val = <$fh>; + close $fh; + $val =~ s/.*: ([^:\r\n]*)\s*$/$1/; + return $val; + } + else + { + return undef; + } +} + +# This should have the same heuristics as convert.c:is_binary() and related. +# Note that the bare CR test is done by callers in convert.c. +sub is_binary +{ + my ($srcType,$name) = @_; + $log->debug("is_binary($srcType,$name)"); + + # Minimize amount of interpreted code run in the inner per-character + # loop for large files, by totalling each character value and + # then analyzing the totals. + my @counts; + my $i; + for($i=0;$i<256;$i++) + { + $counts[$i]=0; + } + + my $fh = open_blob_or_die($srcType,$name); + my $line; + while( defined($line=<$fh>) ) + { + # Any '\0' and bare CR are considered binary. + if( $line =~ /\0|(\r[^\n])/ ) + { + close($fh); + return 1; + } + + # Count up each character in the line: + my $len=length($line); + for($i=0;$i<$len;$i++) + { + $counts[ord(substr($line,$i,1))]++; + } + } + close $fh; + + # Don't count CR and LF as either printable/nonprintable + $counts[ord("\n")]=0; + $counts[ord("\r")]=0; + + # Categorize individual character count into printable and nonprintable: + my $printable=0; + my $nonprintable=0; + for($i=0;$i<256;$i++) + { + if( $i < 32 && + $i != ord("\b") && + $i != ord("\t") && + $i != 033 && # ESC + $i != 014 ) # FF + { + $nonprintable+=$counts[$i]; + } + elsif( $i==127 ) # DEL + { + $nonprintable+=$counts[$i]; + } + else + { + $printable+=$counts[$i]; + } + } + + return ($printable >> 7) < $nonprintable; +} + +# Returns open file handle. Possible invocations: +# - open_blob_or_die("file",$filename); +# - open_blob_or_die("sha1",$filehash); +sub open_blob_or_die +{ + my ($srcType,$name) = @_; + my ($fh); + if( $srcType eq "file" ) + { + if( !open $fh,"<",$name ) + { + $log->warn("Unable to open file $name: $!"); + die "Unable to open file $name: $!\n"; + } + } + elsif( $srcType eq "sha1" || $srcType eq "sha1Or-k" ) + { + unless ( defined ( $name ) and $name =~ /^[a-zA-Z0-9]{40}$/ ) + { + $log->warn("Need filehash"); + die "Need filehash\n"; + } + + my $type = `git cat-file -t $name`; + chomp $type; + + unless ( defined ( $type ) and $type eq "blob" ) + { + $log->warn("Invalid type '$type' for '$name'"); + die ( "Invalid type '$type' (expected 'blob')" ) + } + + my $size = `git cat-file -s $name`; + chomp $size; + + $log->debug("open_blob_or_die($name) size=$size, type=$type"); + + unless( open $fh, '-|', "git", "cat-file", "blob", $name ) + { + $log->warn("Unable to open sha1 $name"); + die "Unable to open sha1 $name\n"; + } + } + else + { + $log->warn("Unknown type of blob source: $srcType"); + die "Unknown type of blob source: $srcType\n"; } + return $fh; } # Generate a CVS author name from Git author information, by taking diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 8aa73712ca..8ee08ff2fd 100755 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -56,9 +56,9 @@ output () { require_clean_work_tree () { # test if working tree is dirty git rev-parse --verify HEAD > /dev/null && - git update-index --refresh && - git diff-files --quiet && - git diff-index --cached --quiet HEAD -- || + git update-index --ignore-submodules --refresh && + git diff-files --quiet --ignore-submodules && + git diff-index --cached --quiet HEAD --ignore-submodules -- || die "Working tree is dirty" } @@ -377,11 +377,12 @@ do # Sanity check git rev-parse --verify HEAD >/dev/null || die "Cannot read HEAD" - git update-index --refresh && git diff-files --quiet || + git update-index --ignore-submodules --refresh && + git diff-files --quiet --ignore-submodules || die "Working tree is dirty" # do we have anything to commit? - if git diff-index --cached --quiet HEAD -- + if git diff-index --cached --quiet --ignore-submodules HEAD -- then : Nothing to commit -- skip this else diff --git a/git-rebase.sh b/git-rebase.sh index 68855c18ae..dd7dfe123c 100755 --- a/git-rebase.sh +++ b/git-rebase.sh @@ -60,7 +60,7 @@ continue_merge () { fi cmt=`cat "$dotest/current"` - if ! git diff-index --quiet HEAD -- + if ! git diff-index --quiet --ignore-submodules HEAD -- then if ! git commit --no-verify -C "$cmt" then @@ -150,7 +150,7 @@ while test $# != 0 do case "$1" in --continue) - git diff-files --quiet || { + git diff-files --quiet --ignore-submodules || { echo "You must edit all merge conflicts and then" echo "mark them as resolved using git add" exit 1 @@ -282,8 +282,8 @@ else fi # The tree must be really really clean. -git update-index --refresh || exit -diff=$(git diff-index --cached --name-status -r HEAD --) +git update-index --ignore-submodules --refresh || exit +diff=$(git diff-index --cached --name-status -r --ignore-submodules HEAD --) case "$diff" in ?*) echo "cannot rebase: your index is not up-to-date" echo "$diff" diff --git a/git-stash.sh b/git-stash.sh index c2b68205a2..4938ade589 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -15,8 +15,8 @@ trap 'rm -f "$TMP-*"' 0 ref_stash=refs/stash no_changes () { - git diff-index --quiet --cached HEAD -- && - git diff-files --quiet + git diff-index --quiet --cached HEAD --ignore-submodules -- && + git diff-files --quiet --ignore-submodules } clear_stash () { @@ -130,7 +130,7 @@ show_stash () { } apply_stash () { - git diff-files --quiet || + git diff-files --quiet --ignore-submodules || die 'Cannot restore on top of a dirty state' unstash_index= diff --git a/git-svn.perl b/git-svn.perl index 2c53f39aef..37976f2505 100755 --- a/git-svn.perl +++ b/git-svn.perl @@ -4,7 +4,7 @@ use warnings; use strict; use vars qw/ $AUTHOR $VERSION - $sha1 $sha1_short $_revision + $sha1 $sha1_short $_revision $_repository $_q $_authors %users/; $AUTHOR = 'Eric Wong <normalperson@yhbt.net>'; $VERSION = '@@GIT_VERSION@@'; @@ -83,6 +83,7 @@ my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent, 'repack-flags|repack-args|repack-opts=s' => \$Git::SVN::_repack_flags, 'use-log-author' => \$Git::SVN::_use_log_author, + 'add-author-from' => \$Git::SVN::_add_author_from, %remote_opts ); my ($_trunk, $_tags, $_branches, $_stdlayout); @@ -221,6 +222,7 @@ unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) { } $ENV{GIT_DIR} = $git_dir; } + $_repository = Git->repository(Repository => $ENV{GIT_DIR}); } my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd); @@ -302,6 +304,7 @@ sub do_git_init_db { } } command_noisy(@init_db); + $_repository = Git->repository(Repository => ".git"); } my $set; my $pfx = "svn-remote.$Git::SVN::default_repo_id"; @@ -318,6 +321,7 @@ sub init_subdir { mkpath([$repo_path]) unless -d $repo_path; chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n"; $ENV{GIT_DIR} = '.git'; + $_repository = Git->repository(Repository => $ENV{GIT_DIR}); } sub cmd_clone { @@ -1012,17 +1016,28 @@ sub get_commit_entry { my ($msg_fh, $ctx) = command_output_pipe('cat-file', $type, $treeish); my $in_msg = 0; + my $author; + my $saw_from = 0; while (<$msg_fh>) { if (!$in_msg) { $in_msg = 1 if (/^\s*$/); + $author = $1 if (/^author (.*>)/); } elsif (/^git-svn-id: /) { # skip this for now, we regenerate the # correct one on re-fetch anyways # TODO: set *:merge properties or like... } else { + if (/^From:/ || /^Signed-off-by:/) { + $saw_from = 1; + } print $log_fh $_ or croak $!; } } + if ($Git::SVN::_add_author_from && defined($author) + && !$saw_from) { + print $log_fh "\nFrom: $author\n" + or croak $!; + } command_close_pipe($msg_fh, $ctx); } close $log_fh or croak $!; @@ -1249,7 +1264,7 @@ use constant rev_map_fmt => 'NH40'; use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent $_repack $_repack_flags $_use_svm_props $_head $_use_svnsync_props $no_reuse_existing $_minimize_url - $_use_log_author/; + $_use_log_author $_add_author_from/; use Carp qw/croak/; use File::Path qw/mkpath/; use File::Copy qw/copy/; @@ -3018,6 +3033,7 @@ use vars qw/@ISA/; use strict; use warnings; use Carp qw/croak/; +use File::Temp qw/tempfile/; use IO::File qw//; # file baton members: path, mode_a, mode_b, pool, fh, blob, base @@ -3173,14 +3189,9 @@ sub apply_textdelta { my $base = IO::File->new_tmpfile; $base->autoflush(1); if ($fb->{blob}) { - defined (my $pid = fork) or croak $!; - if (!$pid) { - open STDOUT, '>&', $base or croak $!; - print STDOUT 'link ' if ($fb->{mode_a} == 120000); - exec qw/git-cat-file blob/, $fb->{blob} or croak $!; - } - waitpid $pid, 0; - croak $? if $?; + print $base 'link ' if ($fb->{mode_a} == 120000); + my $size = $::_repository->cat_blob($fb->{blob}, $base); + die "Failed to read object $fb->{blob}" unless $size; if (defined $exp) { seek $base, 0, 0 or croak $!; @@ -3221,14 +3232,18 @@ sub close_file { sysseek($fh, 0, 0) or croak $!; } } - defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n"; - if (!$pid) { - open STDIN, '<&', $fh or croak $!; - exec qw/git-hash-object -w --stdin/ or croak $!; + + my ($tmp_fh, $tmp_filename) = File::Temp::tempfile(UNLINK => 1); + my $result; + while ($result = sysread($fh, my $string, 1024)) { + syswrite($tmp_fh, $string, $result); } - chomp($hash = do { local $/; <$out> }); - close $out or croak $!; + defined $result or croak $!; + close $tmp_fh or croak $!; + close $fh or croak $!; + + $hash = $::_repository->hash_and_insert_object($tmp_filename); $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n"; close $fb->{base} or croak $!; } else { @@ -3554,13 +3569,8 @@ sub chg_file { } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) { $self->change_file_prop($fbat,'svn:special',undef); } - defined(my $pid = fork) or croak $!; - if (!$pid) { - open STDOUT, '>&', $fh or croak $!; - exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!; - } - waitpid $pid, 0; - croak $? if $?; + my $size = $::_repository->cat_blob($m->{sha1_b}, $fh); + croak "Failed to read object $m->{sha1_b}" unless $size; $fh->flush == 0 or croak $!; seek $fh, 0, 0 or croak $!; @@ -286,6 +286,7 @@ static void handle_internal_command(int argc, const char **argv) { "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE }, { "cherry", cmd_cherry, RUN_SETUP }, { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE }, + { "clone", cmd_clone }, { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE }, { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE }, { "commit-tree", cmd_commit_tree, RUN_SETUP }, diff --git a/gitk-git/gitk b/gitk-git/gitk index 9ab6dbaa46..22bcd18a46 100644 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -495,7 +495,7 @@ proc reloadcommits {} { stop_rev_list $curview } resetvarcs $curview - catch {unset selectedline} + set selectedline {} catch {unset currentid} catch {unset thickerline} catch {unset treediffs} @@ -927,7 +927,7 @@ proc removefakerow {id} { modify_arc $v $a $i if {[info exist currentid] && $id eq $currentid} { unset currentid - unset selectedline + set selectedline {} } if {[info exists targetid] && $targetid eq $id} { set targetid $p @@ -1838,7 +1838,7 @@ proc makewindow {} { pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \ -side left global selectedline - trace add variable selectedline {write unset} selectedline_change + trace add variable selectedline write selectedline_change # Status label and progress bar set statusw .tf.bar.status @@ -2185,7 +2185,7 @@ proc windows_mousewheel_redirector {W X Y D} { proc selectedline_change {n1 n2 op} { global selectedline rownumsel - if {$op eq "unset"} { + if {$selectedline eq {}} { set rownumsel {} } else { set rownumsel [expr {$selectedline + 1}] @@ -3274,7 +3274,7 @@ proc showview {n} { set ytop [expr {[lindex $span 0] * $ymax}] set ybot [expr {[lindex $span 1] * $ymax}] set yscreen [expr {($ybot - $ytop) / 2}] - if {[info exists selectedline]} { + if {$selectedline ne {}} { set selid $currentid set y [yc $selectedline] if {$ytop < $y && $y < $ybot} { @@ -3388,7 +3388,7 @@ proc bolden {row font} { lappend boldrows $row $canv itemconf $linehtag($row) -font $font - if {[info exists selectedline] && $row == $selectedline} { + if {$row == $selectedline} { $canv delete secsel set t [eval $canv create rect [$canv bbox $linehtag($row)] \ -outline {{}} -tags secsel \ @@ -3402,7 +3402,7 @@ proc bolden_name {row font} { lappend boldnamerows $row $canv2 itemconf $linentag($row) -font $font - if {[info exists selectedline] && $row == $selectedline} { + if {$row == $selectedline} { $canv2 delete secsel set t [eval $canv2 create rect [$canv2 bbox $linentag($row)] \ -outline {{}} -tags secsel \ @@ -3831,7 +3831,7 @@ proc askrelhighlight {row id} { global descendent highlight_related iddrawn rhighlights global selectedline ancestor - if {![info exists selectedline]} return + if {$selectedline eq {}} return set isbold 0 if {$highlight_related eq [mc "Descendant"] || $highlight_related eq [mc "Not descendant"]} { @@ -4005,7 +4005,7 @@ proc visiblerows {} { proc layoutmore {} { global commitidx viewcomplete curview - global numcommits pending_select selectedline curview + global numcommits pending_select curview global lastscrollset lastscrollrows commitinterest if {$lastscrollrows < 100 || $viewcomplete($curview) || @@ -4916,7 +4916,7 @@ proc drawcmittext {id row col} { -text $name -font $nfont -tags text] set linedtag($row) [$canv3 create text 3 $y -anchor w -fill $fgcolor \ -text $date -font mainfont -tags text] - if {[info exists selectedline] && $selectedline == $row} { + if {$selectedline == $row} { make_secsel $row } set xr [expr {$xt + [font measure $font $headline]}] @@ -5107,7 +5107,7 @@ proc drawvisible {} { if {$endrow >= $vrowmod($curview)} { update_arcrows $curview } - if {[info exists selectedline] && + if {$selectedline ne {} && $row <= $selectedline && $selectedline <= $endrow} { set targetrow $selectedline } elseif {[info exists targetid]} { @@ -5125,10 +5125,16 @@ proc drawvisible {} { proc clear_display {} { global iddrawn linesegs need_redisplay nrows_drawn global vhighlights fhighlights nhighlights rhighlights + global linehtag linentag linedtag boldrows boldnamerows allcanvs delete all catch {unset iddrawn} catch {unset linesegs} + catch {unset linehtag} + catch {unset linentag} + catch {unset linedtag} + set boldrows {} + set boldnamerows {} catch {unset vhighlights} catch {unset fhighlights} catch {unset nhighlights} @@ -5423,7 +5429,7 @@ proc dofind {{dirn 1} {wrap 1}} { } focus . if {$findstring eq {} || $numcommits == 0} return - if {![info exists selectedline]} { + if {$selectedline eq {}} { set findstartline [lindex [visiblerows] [expr {$dirn < 0}]] } else { set findstartline $selectedline @@ -5619,7 +5625,7 @@ proc markmatches {canv l str tag matches font row} { [expr {$x0+$xlen+2}] $y1 \ -outline {} -tags [list match$l matches] -fill yellow] $canv lower $t - if {[info exists selectedline] && $row == $selectedline} { + if {$row == $selectedline} { $canv raise $t secsel } } @@ -5778,7 +5784,7 @@ proc appendrefs {pos ids var} { proc dispneartags {delay} { global selectedline currentid showneartags tagphase - if {![info exists selectedline] || !$showneartags} return + if {$selectedline eq {} || !$showneartags} return after cancel dispnexttag if {$delay} { after 200 dispnexttag @@ -5792,7 +5798,7 @@ proc dispneartags {delay} { proc dispnexttag {} { global selectedline currentid showneartags tagphase ctext - if {![info exists selectedline] || !$showneartags} return + if {$selectedline eq {} || !$showneartags} return switch -- $tagphase { 0 { set dtags [desctags $currentid] @@ -6014,7 +6020,7 @@ proc sellastline {} { proc selnextline {dir} { global selectedline focus . - if {![info exists selectedline]} return + if {$selectedline eq {}} return set l [expr {$selectedline + $dir}] unmarkmatches selectline $l 1 @@ -6029,7 +6035,7 @@ proc selnextpage {dir} { } allcanvs yview scroll [expr {$dir * $lpp}] units drawvisible - if {![info exists selectedline]} return + if {$selectedline eq {}} return set l [expr {$selectedline + $dir * $lpp}] if {$l < 0} { set l 0 @@ -6043,7 +6049,7 @@ proc selnextpage {dir} { proc unselectline {} { global selectedline currentid - catch {unset selectedline} + set selectedline {} catch {unset currentid} allcanvs delete secsel rhighlight_none @@ -6052,7 +6058,7 @@ proc unselectline {} { proc reselectline {} { global selectedline - if {[info exists selectedline]} { + if {$selectedline ne {}} { selectline $selectedline 0 } } @@ -6864,7 +6870,7 @@ proc redisplay {} { setcanvscroll allcanvs yview moveto [lindex $span 0] drawvisible - if {[info exists selectedline]} { + if {$selectedline ne {}} { selectline $selectedline 0 allcanvs yview moveto [lindex $span 0] } @@ -7185,8 +7191,7 @@ proc rowmenu {x y id} { stopfinding set rowmenuid $id - if {![info exists selectedline] - || [rowofcommit $id] eq $selectedline} { + if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} { set state disabled } else { set state normal @@ -7210,7 +7215,7 @@ proc rowmenu {x y id} { proc diffvssel {dirn} { global rowmenuid selectedline - if {![info exists selectedline]} return + if {$selectedline eq {}} return if {$dirn} { set oldid [commitonrow $selectedline] set newid $rowmenuid @@ -9886,6 +9891,7 @@ set viewperm(0) 0 set viewargs(0) {} set viewargscmd(0) {} +set selectedline {} set numcommits 0 set loginstance 0 set cmdlineok 0 diff --git a/gitk-git/gitk-git/po/es.po b/gitk-git/po/es.po index 2cb1486247..2cb1486247 100644 --- a/gitk-git/gitk-git/po/es.po +++ b/gitk-git/po/es.po diff --git a/hash-object.c b/hash-object.c index 61e7160b36..0a7ac2fe2a 100644 --- a/hash-object.c +++ b/hash-object.c @@ -6,6 +6,7 @@ */ #include "cache.h" #include "blob.h" +#include "quote.h" static void hash_object(const char *path, enum object_type type, int write_object) { @@ -20,6 +21,7 @@ static void hash_object(const char *path, enum object_type type, int write_objec ? "Unable to add %s to database" : "Unable to hash %s", path); printf("%s\n", sha1_to_hex(sha1)); + maybe_flush_or_die(stdout, "hash to stdout"); } static void hash_stdin(const char *type, int write_object) @@ -30,8 +32,27 @@ static void hash_stdin(const char *type, int write_object) printf("%s\n", sha1_to_hex(sha1)); } +static void hash_stdin_paths(const char *type, int write_objects) +{ + struct strbuf buf, nbuf; + + strbuf_init(&buf, 0); + strbuf_init(&nbuf, 0); + while (strbuf_getline(&buf, stdin, '\n') != EOF) { + if (buf.buf[0] == '"') { + strbuf_reset(&nbuf); + if (unquote_c_style(&nbuf, buf.buf, NULL)) + die("line is badly quoted"); + strbuf_swap(&buf, &nbuf); + } + hash_object(buf.buf, type_from_string(type), write_objects); + } + strbuf_release(&buf); + strbuf_release(&nbuf); +} + static const char hash_object_usage[] = -"git-hash-object [-t <type>] [-w] [--stdin] <file>..."; +"git-hash-object [ [-t <type>] [-w] [--stdin] <file>... | --stdin-paths < <list-of-paths> ]"; int main(int argc, char **argv) { @@ -42,6 +63,7 @@ int main(int argc, char **argv) int prefix_length = -1; int no_more_flags = 0; int hashstdin = 0; + int stdin_paths = 0; git_config(git_default_config); @@ -65,7 +87,19 @@ int main(int argc, char **argv) } else if (!strcmp(argv[i], "--help")) usage(hash_object_usage); + else if (!strcmp(argv[i], "--stdin-paths")) { + if (hashstdin) { + error("Can't use --stdin-paths with --stdin"); + usage(hash_object_usage); + } + stdin_paths = 1; + + } else if (!strcmp(argv[i], "--stdin")) { + if (stdin_paths) { + error("Can't use %s with --stdin-paths", argv[i]); + usage(hash_object_usage); + } if (hashstdin) die("Multiple --stdin arguments are not supported"); hashstdin = 1; @@ -76,6 +110,11 @@ int main(int argc, char **argv) else { const char *arg = argv[i]; + if (stdin_paths) { + error("Can't specify files (such as \"%s\") with --stdin-paths", arg); + usage(hash_object_usage); + } + if (hashstdin) { hash_stdin(type, write_object); hashstdin = 0; @@ -87,6 +126,10 @@ int main(int argc, char **argv) no_more_flags = 1; } } + + if (stdin_paths) + hash_stdin_paths(type, write_object); + if (hashstdin) hash_stdin(type, write_object); return 0; diff --git a/http-push.c b/http-push.c index 42727c8a45..f173dcd64f 100644 --- a/http-push.c +++ b/http-push.c @@ -1349,6 +1349,24 @@ static int unlock_remote(struct remote_lock *lock) return rc; } +static void remove_locks(void) +{ + struct remote_lock *lock = remote->locks; + + fprintf(stderr, "Removing remote locks...\n"); + while (lock) { + unlock_remote(lock); + lock = lock->next; + } +} + +static void remove_locks_on_signal(int signo) +{ + remove_locks(); + signal(signo, SIG_DFL); + raise(signo); +} + static void remote_ls(const char *path, int flags, void (*userFunc)(struct remote_ls_ctx *ls), void *userData); @@ -2256,6 +2274,10 @@ int main(int argc, char **argv) goto cleanup; } + signal(SIGINT, remove_locks_on_signal); + signal(SIGHUP, remove_locks_on_signal); + signal(SIGQUIT, remove_locks_on_signal); + /* Check whether the remote has server info files */ remote->can_update_info_refs = 0; remote->has_info_refs = remote_exists("info/refs"); diff --git a/lockfile.c b/lockfile.c index 663f18f9c4..cfc7335347 100644 --- a/lockfile.c +++ b/lockfile.c @@ -24,7 +24,7 @@ static void remove_lock_file(void) static void remove_lock_file_on_signal(int signo) { remove_lock_file(); - signal(SIGINT, SIG_DFL); + signal(signo, SIG_DFL); raise(signo); } @@ -160,6 +160,34 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on return fd; } +int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on_error) +{ + int fd, orig_fd; + + fd = lock_file(lk, path); + if (fd < 0) { + if (die_on_error) + die("unable to create '%s.lock': %s", path, strerror(errno)); + return fd; + } + + orig_fd = open(path, O_RDONLY); + if (orig_fd < 0) { + if (errno != ENOENT) { + if (die_on_error) + die("cannot open '%s' for copying", path); + close(fd); + return error("cannot open '%s' for copying", path); + } + } else if (copy_fd(orig_fd, fd)) { + if (die_on_error) + exit(128); + close(fd); + return -1; + } + return fd; +} + int close_lock_file(struct lock_file *lk) { int fd = lk->fd; diff --git a/perl/Git.pm b/perl/Git.pm index 2e7f896bae..6ba8ee5c0d 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -39,6 +39,10 @@ $VERSION = '0.01'; my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ], STDERR => 0 ); + my $sha1 = $repo->hash_and_insert_object('file.txt'); + my $tempfile = tempfile(); + my $size = $repo->cat_blob($sha1, $tempfile); + =cut @@ -51,6 +55,7 @@ require Exporter; # Methods which can be called as standalone functions as well: @EXPORT_OK = qw(command command_oneline command_noisy command_output_pipe command_input_pipe command_close_pipe + command_bidi_pipe command_close_bidi_pipe version exec_path hash_object git_cmd_try); @@ -92,6 +97,7 @@ increate nonwithstanding). use Carp qw(carp croak); # but croak is bad - throw instead use Error qw(:try); use Cwd qw(abs_path); +use IPC::Open2 qw(open2); } @@ -216,7 +222,6 @@ sub repository { bless $self, $class; } - =back =head1 METHODS @@ -375,6 +380,60 @@ sub command_close_pipe { _cmd_close($fh, $ctx); } +=item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] ) + +Execute the given C<COMMAND> in the same way as command_output_pipe() +does but return both an input pipe filehandle and an output pipe filehandle. + +The function will return return C<($pid, $pipe_in, $pipe_out, $ctx)>. +See C<command_close_bidi_pipe()> for details. + +=cut + +sub command_bidi_pipe { + my ($pid, $in, $out); + $pid = open2($in, $out, 'git', @_); + return ($pid, $in, $out, join(' ', @_)); +} + +=item command_close_bidi_pipe ( PID, PIPE_IN, PIPE_OUT [, CTX] ) + +Close the C<PIPE_IN> and C<PIPE_OUT> as returned from C<command_bidi_pipe()>, +checking whether the command finished successfully. The optional C<CTX> +argument is required if you want to see the command name in the error message, +and it is the fourth value returned by C<command_bidi_pipe()>. The call idiom +is: + + my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --batch-check'); + print "000000000\n" $out; + while (<$in>) { ... } + $r->command_close_bidi_pipe($pid, $in, $out, $ctx); + +Note that you should not rely on whatever actually is in C<CTX>; +currently it is simply the command name but in future the context might +have more complicated structure. + +=cut + +sub command_close_bidi_pipe { + my ($pid, $in, $out, $ctx) = @_; + foreach my $fh ($in, $out) { + unless (close $fh) { + if ($!) { + carp "error closing pipe: $!"; + } elsif ($? >> 8) { + throw Git::Error::Command($ctx, $? >>8); + } + } + } + + waitpid $pid, 0; + + if ($? >> 8) { + throw Git::Error::Command($ctx, $? >>8); + } +} + =item command_noisy ( COMMAND [, ARGUMENTS... ] ) @@ -678,6 +737,147 @@ sub hash_object { } +=item hash_and_insert_object ( FILENAME ) + +Compute the SHA1 object id of the given C<FILENAME> and add the object to the +object database. + +The function returns the SHA1 hash. + +=cut + +# TODO: Support for passing FILEHANDLE instead of FILENAME +sub hash_and_insert_object { + my ($self, $filename) = @_; + + carp "Bad filename \"$filename\"" if $filename =~ /[\r\n]/; + + $self->_open_hash_and_insert_object_if_needed(); + my ($in, $out) = ($self->{hash_object_in}, $self->{hash_object_out}); + + unless (print $out $filename, "\n") { + $self->_close_hash_and_insert_object(); + throw Error::Simple("out pipe went bad"); + } + + chomp(my $hash = <$in>); + unless (defined($hash)) { + $self->_close_hash_and_insert_object(); + throw Error::Simple("in pipe went bad"); + } + + return $hash; +} + +sub _open_hash_and_insert_object_if_needed { + my ($self) = @_; + + return if defined($self->{hash_object_pid}); + + ($self->{hash_object_pid}, $self->{hash_object_in}, + $self->{hash_object_out}, $self->{hash_object_ctx}) = + command_bidi_pipe(qw(hash-object -w --stdin-paths)); +} + +sub _close_hash_and_insert_object { + my ($self) = @_; + + return unless defined($self->{hash_object_pid}); + + my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx); + + command_close_bidi_pipe($self->{@vars}); + delete $self->{@vars}; +} + +=item cat_blob ( SHA1, FILEHANDLE ) + +Prints the contents of the blob identified by C<SHA1> to C<FILEHANDLE> and +returns the number of bytes printed. + +=cut + +sub cat_blob { + my ($self, $sha1, $fh) = @_; + + $self->_open_cat_blob_if_needed(); + my ($in, $out) = ($self->{cat_blob_in}, $self->{cat_blob_out}); + + unless (print $out $sha1, "\n") { + $self->_close_cat_blob(); + throw Error::Simple("out pipe went bad"); + } + + my $description = <$in>; + if ($description =~ / missing$/) { + carp "$sha1 doesn't exist in the repository"; + return 0; + } + + if ($description !~ /^[0-9a-fA-F]{40} \S+ (\d+)$/) { + carp "Unexpected result returned from git cat-file"; + return 0; + } + + my $size = $1; + + my $blob; + my $bytesRead = 0; + + while (1) { + my $bytesLeft = $size - $bytesRead; + last unless $bytesLeft; + + my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024; + my $read = read($in, $blob, $bytesToRead, $bytesRead); + unless (defined($read)) { + $self->_close_cat_blob(); + throw Error::Simple("in pipe went bad"); + } + + $bytesRead += $read; + } + + # Skip past the trailing newline. + my $newline; + my $read = read($in, $newline, 1); + unless (defined($read)) { + $self->_close_cat_blob(); + throw Error::Simple("in pipe went bad"); + } + unless ($read == 1 && $newline eq "\n") { + $self->_close_cat_blob(); + throw Error::Simple("didn't find newline after blob"); + } + + unless (print $fh $blob) { + $self->_close_cat_blob(); + throw Error::Simple("couldn't write to passed in filehandle"); + } + + return $size; +} + +sub _open_cat_blob_if_needed { + my ($self) = @_; + + return if defined($self->{cat_blob_pid}); + + ($self->{cat_blob_pid}, $self->{cat_blob_in}, + $self->{cat_blob_out}, $self->{cat_blob_ctx}) = + command_bidi_pipe(qw(cat-file --batch)); +} + +sub _close_cat_blob { + my ($self) = @_; + + return unless defined($self->{cat_blob_pid}); + + my @vars = map { 'cat_blob_' . $_ } qw(pid in out ctx); + + command_close_bidi_pipe($self->{@vars}); + delete $self->{@vars}; +} =back @@ -895,7 +1095,11 @@ sub _cmd_close { } -sub DESTROY { } +sub DESTROY { + my ($self) = @_; + $self->_close_hash_and_insert_object(); + $self->_close_cat_blob(); +} # Pipe implementation for ActiveState Perl. diff --git a/read-cache.c b/read-cache.c index 8b467f8f41..ac9a8e7e32 100644 --- a/read-cache.c +++ b/read-cache.c @@ -462,12 +462,14 @@ static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_ return new; } -int add_to_index(struct index_state *istate, const char *path, struct stat *st, int verbose) +int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags) { - int size, namelen; + int size, namelen, was_same; mode_t st_mode = st->st_mode; struct cache_entry *ce, *alias; unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY; + int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); + int pretend = flags & ADD_CACHE_PRETEND; if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) return error("%s: can only add regular files, symbolic links or git-directories", path); @@ -509,19 +511,28 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, if (ignore_case && alias && different_name(ce, alias)) ce = create_alias_ce(ce, alias); ce->ce_flags |= CE_ADDED; - if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE)) + + /* It was suspected to be recily clean, but it turns out to be Ok */ + was_same = (alias && + !ce_stage(alias) && + !hashcmp(alias->sha1, ce->sha1) && + ce->ce_mode == alias->ce_mode); + + if (pretend) + ; + else if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE)) return error("unable to add %s to index",path); - if (verbose) + if (verbose && !was_same) printf("add '%s'\n", path); return 0; } -int add_file_to_index(struct index_state *istate, const char *path, int verbose) +int add_file_to_index(struct index_state *istate, const char *path, int flags) { struct stat st; if (lstat(path, &st)) die("%s: unable to stat (%s)", path, strerror(errno)); - return add_to_index(istate, path, &st, verbose); + return add_to_index(istate, path, &st, flags); } struct cache_entry *make_cache_entry(unsigned int mode, @@ -942,6 +953,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p int allow_unmerged = (flags & REFRESH_UNMERGED) != 0; int quiet = (flags & REFRESH_QUIET) != 0; int not_new = (flags & REFRESH_IGNORE_MISSING) != 0; + int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0; unsigned int options = really ? CE_MATCH_IGNORE_VALID : 0; for (i = 0; i < istate->cache_nr; i++) { @@ -949,6 +961,9 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p int cache_errno = 0; ce = istate->cache[i]; + if (ignore_submodules && S_ISGITLINK(ce->ce_mode)) + continue; + if (ce_stage(ce)) { while ((i < istate->cache_nr) && ! strcmp(istate->cache[i]->name, ce->name)) @@ -159,6 +159,8 @@ static struct cached_refs { } cached_refs; static struct ref_list *current_ref; +static struct ref_list *extra_refs; + static void free_ref_list(struct ref_list *list) { struct ref_list *next; @@ -215,6 +217,17 @@ static void read_packed_refs(FILE *f, struct cached_refs *cached_refs) cached_refs->packed = sort_ref_list(list); } +void add_extra_ref(const char *name, const unsigned char *sha1, int flag) +{ + extra_refs = add_ref(name, sha1, flag, extra_refs, NULL); +} + +void clear_extra_refs(void) +{ + free_ref_list(extra_refs); + extra_refs = NULL; +} + static struct ref_list *get_packed_refs(void) { if (!cached_refs.did_packed) { @@ -547,6 +560,11 @@ static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, struct ref_list *packed = get_packed_refs(); struct ref_list *loose = get_loose_refs(); + struct ref_list *extra; + + for (extra = extra_refs; extra; extra = extra->next) + retval = do_one_ref(base, fn, trim, cb_data, extra); + while (packed && loose) { struct ref_list *entry; int cmp = strcmp(packed->name, loose->name); @@ -24,6 +24,15 @@ extern int for_each_tag_ref(each_ref_fn, void *); extern int for_each_branch_ref(each_ref_fn, void *); extern int for_each_remote_ref(each_ref_fn, void *); +/* + * Extra refs will be listed by for_each_ref() before any actual refs + * for the duration of this process or until clear_extra_refs() is + * called. Only extra refs added before for_each_ref() is called will + * be listed on a given call of for_each_ref(). + */ +extern void add_extra_ref(const char *refname, const unsigned char *sha1, int flags); +extern void clear_extra_refs(void); + extern int peel_ref(const char *, unsigned char *); /** Locks a "refs/" ref returning the lock on success and NULL on failure. **/ @@ -2,6 +2,16 @@ #include "remote.h" #include "refs.h" +static struct refspec s_tag_refspec = { + 0, + 1, + 0, + "refs/tags/", + "refs/tags/" +}; + +const struct refspec *tag_refspec = &s_tag_refspec; + struct counted_string { size_t len; const char *s; @@ -53,6 +53,8 @@ struct refspec { char *dst; }; +extern const struct refspec *tag_refspec; + struct ref *alloc_ref(unsigned namelen); struct ref *alloc_ref_from_str(const char* str); diff --git a/sha1_file.c b/sha1_file.c index 141f5a713a..9679040d62 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -176,21 +176,22 @@ char *sha1_file_name(const unsigned char *sha1) return base; } -char *sha1_pack_name(const unsigned char *sha1) +static char *sha1_get_pack_name(const unsigned char *sha1, + char **name, char **base) { static const char hex[] = "0123456789abcdef"; - static char *name, *base, *buf; + char *buf; int i; - if (!base) { + if (!*base) { const char *sha1_file_directory = get_object_directory(); int len = strlen(sha1_file_directory); - base = xmalloc(len + 60); - sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory); - name = base + len + 11; + *base = xmalloc(len + 60); + sprintf(*base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory); + *name = *base + len + 11; } - buf = name; + buf = *name; for (i = 0; i < 20; i++) { unsigned int val = *sha1++; @@ -198,32 +199,21 @@ char *sha1_pack_name(const unsigned char *sha1) *buf++ = hex[val & 0xf]; } - return base; + return *base; } -char *sha1_pack_index_name(const unsigned char *sha1) +char *sha1_pack_name(const unsigned char *sha1) { - static const char hex[] = "0123456789abcdef"; - static char *name, *base, *buf; - int i; - - if (!base) { - const char *sha1_file_directory = get_object_directory(); - int len = strlen(sha1_file_directory); - base = xmalloc(len + 60); - sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory); - name = base + len + 11; - } + static char *name, *base; - buf = name; + return sha1_get_pack_name(sha1, &name, &base); +} - for (i = 0; i < 20; i++) { - unsigned int val = *sha1++; - *buf++ = hex[val >> 4]; - *buf++ = hex[val & 0xf]; - } +char *sha1_pack_index_name(const unsigned char *sha1) +{ + static char *name, *base; - return base; + return sha1_get_pack_name(sha1, &name, &base); } struct alternate_object_database *alt_odb_list; @@ -380,6 +370,18 @@ static void read_info_alternates(const char * relative_base, int depth) munmap(map, mapsz); } +void add_to_alternates_file(const char *reference) +{ + struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); + int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), 1); + char *alt = mkpath("%s/objects\n", reference); + write_or_die(fd, alt, strlen(alt)); + if (commit_lock_file(lock)) + die("could not close alternates file"); + if (alt_odb_tail) + link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0); +} + void prepare_alt_odb(void) { const char *alt; diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh new file mode 100755 index 0000000000..cb1fbe5820 --- /dev/null +++ b/t/t1006-cat-file.sh @@ -0,0 +1,226 @@ +#!/bin/sh + +test_description='git cat-file' + +. ./test-lib.sh + +echo_without_newline () { + printf '%s' "$*" +} + +strlen () { + echo_without_newline "$1" | wc -c | sed -e 's/^ *//' +} + +maybe_remove_timestamp () { + if test -z "$2"; then + echo_without_newline "$1" + else + echo_without_newline "$(printf '%s\n' "$1" | sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//')" + fi +} + +run_tests () { + type=$1 + sha1=$2 + size=$3 + content=$4 + pretty_content=$5 + no_ts=$6 + + batch_output="$sha1 $type $size +$content" + + test_expect_success "$type exists" ' + git cat-file -e $sha1 + ' + + test_expect_success "Type of $type is correct" ' + test $type = "$(git cat-file -t $sha1)" + ' + + test_expect_success "Size of $type is correct" ' + test $size = "$(git cat-file -s $sha1)" + ' + + test -z "$content" || + test_expect_success "Content of $type is correct" ' + expect="$(maybe_remove_timestamp "$content" $no_ts)" + actual="$(maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts)" + + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' + + test_expect_success "Pretty content of $type is correct" ' + expect="$(maybe_remove_timestamp "$pretty_content" $no_ts)" + actual="$(maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts)" + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' + + test -z "$content" || + test_expect_success "--batch output of $type is correct" ' + expect="$(maybe_remove_timestamp "$batch_output" $no_ts)" + actual="$(maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" no_ts)" + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' + + test_expect_success "--batch-check output of $type is correct" ' + expect="$sha1 $type $size" + actual="$(echo_without_newline $sha1 | git cat-file --batch-check)" + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' +} + +hello_content="Hello World" +hello_size=$(strlen "$hello_content") +hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin) + +test_expect_success "setup" ' + echo_without_newline "$hello_content" > hello && + git update-index --add hello +' + +run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content" + +tree_sha1=$(git write-tree) +tree_size=33 +tree_pretty_content="100644 blob $hello_sha1 hello" + +run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content" + +commit_message="Intial commit" +commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1) +commit_size=176 +commit_content="tree $tree_sha1 +author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000 + +$commit_message" + +run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1 + +tag_header_without_timestamp="object $hello_sha1 +type blob +tag hellotag +tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" +tag_description="This is a tag" +tag_content="$tag_header_without_timestamp 0000000000 +0000 + +$tag_description" +tag_pretty_content="$tag_header_without_timestamp Thu Jan 1 00:00:00 1970 +0000 + +$tag_description" + +tag_sha1=$(echo_without_newline "$tag_content" | git mktag) +tag_size=$(strlen "$tag_content") + +run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_pretty_content" 1 + +test_expect_success \ + "Reach a blob from a tag pointing to it" \ + "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\"" + +for batch in batch batch-check +do + for opt in t s e p + do + test_expect_success "Passing -$opt with --$batch fails" ' + test_must_fail git cat-file --$batch -$opt $hello_sha1 + ' + + test_expect_success "Passing --$batch with -$opt fails" ' + test_must_fail git cat-file -$opt --$batch $hello_sha1 + ' + done + + test_expect_success "Passing <type> with --$batch fails" ' + test_must_fail git cat-file --$batch blob $hello_sha1 + ' + + test_expect_success "Passing --$batch with <type> fails" ' + test_must_fail git cat-file blob --$batch $hello_sha1 + ' + + test_expect_success "Passing sha1 with --$batch fails" ' + test_must_fail git cat-file --$batch $hello_sha1 + ' +done + +test_expect_success "--batch-check for a non-existent object" ' + test "deadbeef missing" = \ + "$(echo_without_newline deadbeef | git cat-file --batch-check)" +' + +test_expect_success "--batch-check for an emtpy line" ' + test " missing" = "$(echo | git cat-file --batch-check)" +' + +batch_input="$hello_sha1 +$commit_sha1 +$tag_sha1 +deadbeef + +" + +batch_output="$hello_sha1 blob $hello_size +$hello_content +$commit_sha1 commit $commit_size +$commit_content +$tag_sha1 tag $tag_size +$tag_content +deadbeef missing + missing" + +test_expect_success '--batch with multiple sha1s gives correct format' ' + test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)" +' + +batch_check_input="$hello_sha1 +$tree_sha1 +$commit_sha1 +$tag_sha1 +deadbeef + +" + +batch_check_output="$hello_sha1 blob $hello_size +$tree_sha1 tree $tree_size +$commit_sha1 commit $commit_size +$tag_sha1 tag $tag_size +deadbeef missing + missing" + +test_expect_success "--batch-check with multiple sha1s gives correct format" ' + test "$batch_check_output" = \ + "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)" +' + +test_done diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh new file mode 100755 index 0000000000..05262954ab --- /dev/null +++ b/t/t1007-hash-object.sh @@ -0,0 +1,133 @@ +#!/bin/sh + +test_description=git-hash-object + +. ./test-lib.sh + +echo_without_newline() { + printf '%s' "$*" +} + +test_blob_does_not_exist() { + test_expect_success 'blob does not exist in database' " + test_must_fail git cat-file blob $1 + " +} + +test_blob_exists() { + test_expect_success 'blob exists in database' " + git cat-file blob $1 + " +} + +hello_content="Hello World" +hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689 + +example_content="This is an example" +example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399 + +setup_repo() { + echo_without_newline "$hello_content" > hello + echo_without_newline "$example_content" > example +} + +test_repo=test +push_repo() { + test_create_repo $test_repo + cd $test_repo + + setup_repo +} + +pop_repo() { + cd .. + rm -rf $test_repo +} + +setup_repo + +# Argument checking + +test_expect_success "multiple '--stdin's are rejected" ' + test_must_fail git hash-object --stdin --stdin < example +' + +test_expect_success "Can't use --stdin and --stdin-paths together" ' + test_must_fail git hash-object --stdin --stdin-paths && + test_must_fail git hash-object --stdin-paths --stdin +' + +test_expect_success "Can't pass filenames as arguments with --stdin-paths" ' + test_must_fail git hash-object --stdin-paths hello < example +' + +# Behavior + +push_repo + +test_expect_success 'hash a file' ' + test $hello_sha1 = $(git hash-object hello) +' + +test_blob_does_not_exist $hello_sha1 + +test_expect_success 'hash from stdin' ' + test $example_sha1 = $(git hash-object --stdin < example) +' + +test_blob_does_not_exist $example_sha1 + +test_expect_success 'hash a file and write to database' ' + test $hello_sha1 = $(git hash-object -w hello) +' + +test_blob_exists $hello_sha1 + +test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' ' + echo foo > file1 && + obname0=$(echo bar | git hash-object --stdin) && + obname1=$(git hash-object file1) && + obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && + obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && + test "$obname0" = "$obname0new" && + test "$obname1" = "$obname1new" +' + +pop_repo + +for args in "-w --stdin" "--stdin -w"; do + push_repo + + test_expect_success "hash from stdin and write to database ($args)" ' + test $example_sha1 = $(git hash-object $args < example) + ' + + test_blob_exists $example_sha1 + + pop_repo +done + +filenames="hello +example" + +sha1s="$hello_sha1 +$example_sha1" + +test_expect_success "hash two files with names on stdin" ' + test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)" +' + +for args in "-w --stdin-paths" "--stdin-paths -w"; do + push_repo + + test_expect_success "hash two files with names on stdin and write to database ($args)" ' + test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)" + ' + + test_blob_exists $hello_sha1 + test_blob_exists $example_sha1 + + pop_repo +done + +test_done diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh index b664341926..f57a6e077c 100755 --- a/t/t2200-add-update.sh +++ b/t/t2200-add-update.sh @@ -111,4 +111,21 @@ test_expect_success 'touch and then add explicitly' ' ' +test_expect_success 'add -n -u should not add but just report' ' + + ( + echo "add '\''check'\''" && + echo "remove '\''top'\''" + ) >expect && + before=$(git ls-files -s check top) && + echo changed >>check && + rm -f top && + git add -n -u >actual && + after=$(git ls-files -s check top) && + + test "$before" = "$after" && + test_cmp expect actual + +' + test_done diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh new file mode 100755 index 0000000000..0cfd47cfcf --- /dev/null +++ b/t/t4126-apply-empty.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +test_description='apply empty' + +. ./test-lib.sh + +test_expect_success setup ' + >empty && + git add empty && + test_tick && + git commit -m initial && + for i in a b c d e + do + echo $i + done >empty && + cat empty >expect && + git diff | + sed -e "/^diff --git/d" \ + -e "/^index /d" \ + -e "s|a/empty|empty.orig|" \ + -e "s|b/empty|empty|" >patch0 && + sed -e "s|empty|missing|" patch0 >patch1 && + >empty && + git update-index --refresh +' + +test_expect_success 'apply empty' ' + git reset --hard && + >empty && + rm -f missing && + git apply patch0 && + test_cmp expect empty +' + +test_expect_success 'apply --index empty' ' + git reset --hard && + >empty && + rm -f missing && + git apply --index patch0 && + test_cmp expect empty && + git diff --exit-code +' + +test_expect_success 'apply create' ' + git reset --hard && + >empty && + rm -f missing && + git apply patch1 && + test_cmp expect missing +' + +test_expect_success 'apply --index create' ' + git reset --hard && + >empty && + rm -f missing && + git apply --index patch1 && + test_cmp expect missing && + git diff --exit-code +' + +test_done diff --git a/t/t5303-hash-object.sh b/t/t5303-hash-object.sh deleted file mode 100755 index 543c0784bd..0000000000 --- a/t/t5303-hash-object.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/sh - -test_description=git-hash-object - -. ./test-lib.sh - -test_expect_success \ - 'git hash-object -w --stdin saves the object' \ - 'obname=$(echo foo | git hash-object -w --stdin) && - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && - test -r .git/objects/"$obpath" && - rm -f .git/objects/"$obpath"' - -test_expect_success \ - 'git hash-object --stdin -w saves the object' \ - 'obname=$(echo foo | git hash-object --stdin -w) && - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && - test -r .git/objects/"$obpath" && - rm -f .git/objects/"$obpath"' - -test_expect_success \ - 'git hash-object --stdin file1 <file0 first operates on file0, then file1' \ - 'echo foo > file1 && - obname0=$(echo bar | git hash-object --stdin) && - obname1=$(git hash-object file1) && - obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && - obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && - test "$obname0" = "$obname0new" && - test "$obname1" = "$obname1new"' - -test_expect_success \ - 'git hash-object refuses multiple --stdin arguments' \ - '! git hash-object --stdin --stdin < file1' - -test_done diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh index dc9d63dbf9..593d1a3877 100755 --- a/t/t5601-clone.sh +++ b/t/t5601-clone.sh @@ -23,4 +23,11 @@ test_expect_success 'clone with excess parameters' ' ' +test_expect_success 'clone checks out files' ' + + git clone src dst && + test -f dst/file + +' + test_done diff --git a/t/t5700-clone-reference.sh b/t/t5700-clone-reference.sh index e5619a9f5c..e1ca7303ac 100755 --- a/t/t5700-clone-reference.sh +++ b/t/t5700-clone-reference.sh @@ -8,6 +8,8 @@ test_description='test clone --reference' base_dir=`pwd` +U=$base_dir/UPLOAD_LOG + test_expect_success 'preparing first repository' \ 'test_create_repo A && cd A && echo first > file1 && @@ -50,8 +52,13 @@ diff expected current' cd "$base_dir" +rm -f $U + test_expect_success 'cloning with reference (no -l -s)' \ -'git clone --reference B "file://$(pwd)/A" D' +'GIT_DEBUG_SEND_PACK=3 git clone --reference B "file://$(pwd)/A" D 3>$U' + +test_expect_success 'fetched no objects' \ +'! grep "^want" $U' cd "$base_dir" @@ -113,4 +120,30 @@ diff expected current' cd "$base_dir" +test_expect_success 'preparing alternate repository #1' \ +'test_create_repo F && cd F && +echo first > file1 && +git add file1 && +git commit -m initial' + +cd "$base_dir" + +test_expect_success 'cloning alternate repo #2 and adding changes to repo #1' \ +'git clone F G && cd F && +echo second > file2 && +git add file2 && +git commit -m addition' + +cd "$base_dir" + +test_expect_success 'cloning alternate repo #1, using #2 as reference' \ +'git clone --reference G F H' + +cd "$base_dir" + +test_expect_success 'cloning with reference being subset of source (-l -s)' \ +'git clone -l -s --reference A B E' + +cd "$base_dir" + test_done diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index 933f567983..0626544823 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -126,6 +126,47 @@ test_expect_success 'bisect reset removes packed refs' ' test -z "$(git for-each-ref "refs/heads/bisect")" ' +test_expect_success 'bisect start: back in good branch' ' + git branch > branch.output && + grep "* other" branch.output > /dev/null && + git bisect start $HASH4 $HASH1 -- && + git bisect good && + git bisect start $HASH4 $HASH1 -- && + git bisect bad && + git bisect reset && + git branch > branch.output && + grep "* other" branch.output > /dev/null +' + +test_expect_success 'bisect start: no ".git/BISECT_START" if junk rev' ' + git bisect start $HASH4 $HASH1 -- && + git bisect good && + test_must_fail git bisect start $HASH4 foo -- && + git branch > branch.output && + grep "* other" branch.output > /dev/null && + test_must_fail test -e .git/BISECT_START +' + +test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' ' + git bisect start $HASH4 $HASH1 -- && + git bisect good && + test_must_fail git bisect start $HASH1 $HASH4 -- && + git branch > branch.output && + grep "* other" branch.output > /dev/null && + test_must_fail test -e .git/BISECT_START +' + +test_expect_success 'bisect start: no ".git/BISECT_START" if checkout error' ' + echo "temp stuff" > hello && + test_must_fail git bisect start $HASH4 $HASH1 -- && + git branch && + git branch > branch.output && + grep "* other" branch.output > /dev/null && + test_must_fail test -e .git/BISECT_START && + test -z "$(git for-each-ref "refs/bisect/*")" && + git checkout HEAD hello +' + # $HASH1 is good, $HASH4 is bad, we skip $HASH3 # but $HASH2 is bad, # so we should find $HASH2 as the first bad commit @@ -281,25 +322,6 @@ test_expect_success 'bisect starting with a detached HEAD' ' test $HEAD = $(cat .git/BISECT_START) && git bisect reset && test $HEAD = $(git rev-parse --verify HEAD) - -' - -test_expect_success 'bisect refuses to start if branch bisect exists' ' - git bisect reset && - git branch bisect && - test_must_fail git bisect start && - git branch -d bisect && - git checkout -b bisect && - test_must_fail git bisect start && - git checkout master && - git branch -d bisect -' - -test_expect_success 'bisect refuses to start if branch new-bisect exists' ' - git bisect reset && - git branch new-bisect && - test_must_fail git bisect start && - git branch -d new-bisect ' test_expect_success 'bisect errors out if bad and good are mistaken' ' @@ -309,6 +331,25 @@ test_expect_success 'bisect errors out if bad and good are mistaken' ' git bisect reset ' +test_expect_success 'bisect does not create a "bisect" branch' ' + git bisect reset && + git bisect start $HASH7 $HASH1 && + git branch bisect && + rev_hash4=$(git rev-parse --verify HEAD) && + test "$rev_hash4" = "$HASH4" && + git branch -D bisect && + git bisect good && + git branch bisect && + rev_hash6=$(git rev-parse --verify HEAD) && + test "$rev_hash6" = "$HASH6" && + git bisect good > my_bisect_log.txt && + grep "$HASH7 is first bad commit" my_bisect_log.txt && + git bisect reset && + rev_hash6=$(git rev-parse --verify bisect) && + test "$rev_hash6" = "$HASH6" && + git branch -D bisect +' + # # test_done diff --git a/t/t6031-merge-recursive.sh b/t/t6031-merge-recursive.sh index c8310aee4f..8073e0c3ef 100755 --- a/t/t6031-merge-recursive.sh +++ b/t/t6031-merge-recursive.sh @@ -3,6 +3,9 @@ test_description='merge-recursive: handle file mode' . ./test-lib.sh +# Note that we follow "chmod +x F" with "update-index --chmod=+x F" to +# help filesystems that do not have the executable bit. + test_expect_success 'mode change in one branch: keep changed version' ' : >file1 && git add file1 && @@ -13,7 +16,7 @@ test_expect_success 'mode change in one branch: keep changed version' ' git commit -m a && git checkout -b b1 master && chmod +x file1 && - git add file1 && + git update-index --chmod=+x file1 && git commit -m b1 && git checkout a1 && git merge-recursive master -- a1 b1 && @@ -26,7 +29,7 @@ test_expect_success 'mode change in both branches: expect conflict' ' : >file2 && H=$(git hash-object file2) && chmod +x file2 && - git add file2 && + git update-index --add --chmod=+x file2 && git commit -m a2 && git checkout -b b2 master && : >file2 && diff --git a/t/t7402-submodule-rebase.sh b/t/t7402-submodule-rebase.sh new file mode 100755 index 0000000000..5becb3ec54 --- /dev/null +++ b/t/t7402-submodule-rebase.sh @@ -0,0 +1,92 @@ +#!/bin/sh +# +# Copyright (c) 2008 Johannes Schindelin +# + +test_description='Test rebasing and stashing with dirty submodules' + +. ./test-lib.sh + +test_expect_success setup ' + + echo file > file && + git add file && + test_tick && + git commit -m initial && + git clone . submodule && + git add submodule && + test_tick && + git commit -m submodule && + echo second line >> file && + (cd submodule && git pull) && + test_tick && + git commit -m file-and-submodule -a + +' + +test_expect_success 'rebase with a dirty submodule' ' + + (cd submodule && + echo 3rd line >> file && + test_tick && + git commit -m fork -a) && + echo unrelated >> file2 && + git add file2 && + test_tick && + git commit -m unrelated file2 && + echo other line >> file && + test_tick && + git commit -m update file && + CURRENT=$(cd submodule && git rev-parse HEAD) && + EXPECTED=$(git rev-parse HEAD~2:submodule) && + GIT_TRACE=1 git rebase --onto HEAD~2 HEAD^ && + STORED=$(git rev-parse HEAD:submodule) && + test $EXPECTED = $STORED && + test $CURRENT = $(cd submodule && git rev-parse HEAD) + +' + +cat > fake-editor.sh << \EOF +#!/bin/sh +echo $EDITOR_TEXT +EOF +chmod a+x fake-editor.sh + +test_expect_success 'interactive rebase with a dirty submodule' ' + + test submodule = $(git diff --name-only) && + HEAD=$(git rev-parse HEAD) && + GIT_EDITOR="\"$(pwd)/fake-editor.sh\"" EDITOR_TEXT="pick $HEAD" \ + git rebase -i HEAD^ && + test submodule = $(git diff --name-only) + +' + +test_expect_success 'rebase with dirty file and submodule fails' ' + + echo yet another line >> file && + test_tick && + git commit -m next file && + echo rewrite > file && + test_tick && + git commit -m rewrite file && + echo dirty > file && + ! git rebase --onto HEAD~2 HEAD^ + +' + +test_expect_success 'stash with a dirty submodule' ' + + echo new > file && + CURRENT=$(cd submodule && git rev-parse HEAD) && + git stash && + test new != $(cat file) && + test submodule = $(git diff --name-only) && + test $CURRENT = $(cd submodule && git rev-parse HEAD) && + git stash apply && + test new = $(cat file) && + test $CURRENT = $(cd submodule && git rev-parse HEAD) + +' + +test_done diff --git a/t/t9122-git-svn-author.sh b/t/t9122-git-svn-author.sh new file mode 100755 index 0000000000..8c58f0b8d7 --- /dev/null +++ b/t/t9122-git-svn-author.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +test_description='git svn authorship' +. ./lib-git-svn.sh + +test_expect_success 'setup svn repository' ' + svn checkout "$svnrepo" work.svn && + ( + cd work.svn && + echo >file + svn add file + svn commit -m "first commit" file + ) +' + +test_expect_success 'interact with it via git-svn' ' + mkdir work.git && + ( + cd work.git && + git svn init "$svnrepo" + git svn fetch && + + echo modification >file && + test_tick && + git commit -a -m second && + + test_tick && + git svn dcommit && + + echo "further modification" >file && + test_tick && + git commit -a -m third && + + test_tick && + git svn --add-author-from dcommit && + + echo "yet further modification" >file && + test_tick && + git commit -a -m fourth && + + test_tick && + git svn --add-author-from --use-log-author dcommit && + + git log && + + git show -s HEAD^^ >../actual.2 && + git show -s HEAD^ >../actual.3 && + git show -s HEAD >../actual.4 + + ) && + + # Make sure that --add-author-from without --use-log-author + # did not affect the authorship information + myself=$(grep "^Author: " actual.2) && + unaffected=$(grep "^Author: " actual.3) && + test "z$myself" = "z$unaffected" && + + # Make sure lack of --add-author-from did not add cruft + ! grep "^ From: A U Thor " actual.2 && + + # Make sure --add-author-from added cruft + grep "^ From: A U Thor " actual.3 && + grep "^ From: A U Thor " actual.4 && + + # Make sure --add-author-from with --use-log-author affected + # the authorship information + grep "^Author: A U Thor " actual.4 +' + +test_done diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh index 42b144b1b3..b1dc32d056 100755 --- a/t/t9200-git-cvsexportcommit.sh +++ b/t/t9200-git-cvsexportcommit.sh @@ -297,4 +297,21 @@ test_expect_success 'commit a file with leading spaces in the name' ' ' +test_expect_success 'use the same checkout for Git and CVS' ' + + (mkdir shared && + cd shared && + unset GIT_DIR && + cvs co . && + git init && + git add " space" && + git commit -m "fake initial commit" && + echo Hello >> " space" && + git commit -m "Another change" " space" && + git cvsexportcommit -W -p -u -c HEAD && + grep Hello " space" && + git diff-files) + +' + test_done diff --git a/t/t9401-git-cvsserver-crlf.sh b/t/t9401-git-cvsserver-crlf.sh new file mode 100755 index 0000000000..e27a1c5f85 --- /dev/null +++ b/t/t9401-git-cvsserver-crlf.sh @@ -0,0 +1,337 @@ +#!/bin/sh +# +# Copyright (c) 2008 Matthew Ogilvie +# Parts adapted from other tests. +# + +test_description='git-cvsserver -kb modes + +tests -kb mode for binary files when accessing a git +repository using cvs CLI client via git-cvsserver server' + +. ./test-lib.sh + +q_to_nul () { + perl -pe 'y/Q/\000/' +} + +q_to_cr () { + tr Q '\015' +} + +marked_as () { + foundEntry="$(grep "^/$2/" "$1/CVS/Entries")" + if [ x"$foundEntry" = x"" ] ; then + echo "NOT FOUND: $1 $2 1 $3" >> "${WORKDIR}/marked.log" + return 1 + fi + test x"$(grep "^/$2/" "$1/CVS/Entries" | cut -d/ -f5)" = x"$3" + stat=$? + echo "$1 $2 $stat '$3'" >> "${WORKDIR}/marked.log" + return $stat +} + +not_present() { + foundEntry="$(grep "^/$2/" "$1/CVS/Entries")" + if [ -r "$1/$2" ] ; then + echo "Error: File still exists: $1 $2" >> "${WORKDIR}/marked.log" + return 1; + fi + if [ x"$foundEntry" != x"" ] ; then + echo "Error: should not have found: $1 $2" >> "${WORKDIR}/marked.log" + return 1; + else + echo "Correctly not found: $1 $2" >> "${WORKDIR}/marked.log" + return 0; + fi +} + +cvs >/dev/null 2>&1 +if test $? -ne 1 +then + test_expect_success 'skipping git-cvsserver tests, cvs not found' : + test_done + exit +fi +perl -e 'use DBI; use DBD::SQLite' >/dev/null 2>&1 || { + test_expect_success 'skipping git-cvsserver tests, Perl SQLite interface unavailable' : + test_done + exit +} + +unset GIT_DIR GIT_CONFIG +WORKDIR=$(pwd) +SERVERDIR=$(pwd)/gitcvs.git +git_config="$SERVERDIR/config" +CVSROOT=":fork:$SERVERDIR" +CVSWORK="$(pwd)/cvswork" +CVS_SERVER=git-cvsserver +export CVSROOT CVS_SERVER + +rm -rf "$CVSWORK" "$SERVERDIR" +test_expect_success 'setup' ' + echo "Simple text file" >textfile.c && + echo "File with embedded NUL: Q <- there" | q_to_nul > binfile.bin && + mkdir subdir && + echo "Another text file" > subdir/file.h && + echo "Another binary: Q (this time CR)" | q_to_cr > subdir/withCr.bin && + echo "Mixed up NUL, but marked text: Q <- there" | q_to_nul > mixedUp.c + echo "Unspecified" > subdir/unspecified.other && + echo "/*.bin -crlf" > .gitattributes && + echo "/*.c crlf" >> .gitattributes && + echo "subdir/*.bin -crlf" >> .gitattributes && + echo "subdir/*.c crlf" >> .gitattributes && + echo "subdir/file.h crlf" >> .gitattributes && + git add .gitattributes textfile.c binfile.bin mixedUp.c subdir/* && + git commit -q -m "First Commit" && + git clone -q --local --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 && + GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true && + GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" +' + +test_expect_success 'cvs co (default crlf)' ' + GIT_CONFIG="$git_config" cvs -Q co -d cvswork master >cvs.log 2>&1 && + test x"$(grep '/-k' cvswork/CVS/Entries cvswork/subdir/CVS/Entries)" = x"" +' + +rm -rf cvswork +test_expect_success 'cvs co (allbinary)' ' + GIT_DIR="$SERVERDIR" git config --bool gitcvs.allbinary true && + GIT_CONFIG="$git_config" cvs -Q co -d cvswork master >cvs.log 2>&1 && + marked_as cvswork textfile.c -kb && + marked_as cvswork binfile.bin -kb && + marked_as cvswork .gitattributes -kb && + marked_as cvswork mixedUp.c -kb && + marked_as cvswork/subdir withCr.bin -kb && + marked_as cvswork/subdir file.h -kb && + marked_as cvswork/subdir unspecified.other -kb +' + +rm -rf cvswork cvs.log +test_expect_success 'cvs co (use attributes/allbinary)' ' + GIT_DIR="$SERVERDIR" git config --bool gitcvs.usecrlfattr true && + GIT_CONFIG="$git_config" cvs -Q co -d cvswork master >cvs.log 2>&1 && + marked_as cvswork textfile.c "" && + marked_as cvswork binfile.bin -kb && + marked_as cvswork .gitattributes -kb && + marked_as cvswork mixedUp.c "" && + marked_as cvswork/subdir withCr.bin -kb && + marked_as cvswork/subdir file.h "" && + marked_as cvswork/subdir unspecified.other -kb +' + +rm -rf cvswork +test_expect_success 'cvs co (use attributes)' ' + GIT_DIR="$SERVERDIR" git config --bool gitcvs.allbinary false && + GIT_CONFIG="$git_config" cvs -Q co -d cvswork master >cvs.log 2>&1 && + marked_as cvswork textfile.c "" && + marked_as cvswork binfile.bin -kb && + marked_as cvswork .gitattributes "" && + marked_as cvswork mixedUp.c "" && + marked_as cvswork/subdir withCr.bin -kb && + marked_as cvswork/subdir file.h "" && + marked_as cvswork/subdir unspecified.other "" +' + +test_expect_success 'adding files' ' + cd cvswork/subdir && + echo "more text" > src.c && + GIT_CONFIG="$git_config" cvs -Q add src.c >cvs.log 2>&1 && + marked_as . src.c "" && + echo "psuedo-binary" > temp.bin && + cd .. && + GIT_CONFIG="$git_config" cvs -Q add subdir/temp.bin >cvs.log 2>&1 && + marked_as subdir temp.bin "-kb" && + cd subdir && + GIT_CONFIG="$git_config" cvs -Q ci -m "adding files" >cvs.log 2>&1 && + marked_as . temp.bin "-kb" && + marked_as . src.c "" +' + +cd "$WORKDIR" +test_expect_success 'updating' ' + git pull gitcvs.git && + echo 'hi' > subdir/newfile.bin && + echo 'junk' > subdir/file.h && + echo 'hi' > subdir/newfile.c && + echo 'hello' >> binfile.bin && + git add subdir/newfile.bin subdir/file.h subdir/newfile.c binfile.bin && + git commit -q -m "Add and change some files" && + git push gitcvs.git >/dev/null && + cd cvswork && + GIT_CONFIG="$git_config" cvs -Q update && + cd .. && + marked_as cvswork textfile.c "" && + marked_as cvswork binfile.bin -kb && + marked_as cvswork .gitattributes "" && + marked_as cvswork mixedUp.c "" && + marked_as cvswork/subdir withCr.bin -kb && + marked_as cvswork/subdir file.h "" && + marked_as cvswork/subdir unspecified.other "" && + marked_as cvswork/subdir newfile.bin -kb && + marked_as cvswork/subdir newfile.c "" && + echo "File with embedded NUL: Q <- there" | q_to_nul > tmpExpect1 && + echo "hello" >> tmpExpect1 && + cmp cvswork/binfile.bin tmpExpect1 +' + +rm -rf cvswork +test_expect_success 'cvs co (use attributes/guess)' ' + GIT_DIR="$SERVERDIR" git config gitcvs.allbinary guess && + GIT_CONFIG="$git_config" cvs -Q co -d cvswork master >cvs.log 2>&1 && + marked_as cvswork textfile.c "" && + marked_as cvswork binfile.bin -kb && + marked_as cvswork .gitattributes "" && + marked_as cvswork mixedUp.c "" && + marked_as cvswork/subdir withCr.bin -kb && + marked_as cvswork/subdir file.h "" && + marked_as cvswork/subdir unspecified.other "" && + marked_as cvswork/subdir newfile.bin -kb && + marked_as cvswork/subdir newfile.c "" +' + +test_expect_success 'setup multi-line files' ' + ( echo "line 1" && + echo "line 2" && + echo "line 3" && + echo "line 4 with NUL: Q <-" ) | q_to_nul > multiline.c && + git add multiline.c && + ( echo "line 1" && + echo "line 2" && + echo "line 3" && + echo "line 4" ) | q_to_nul > multilineTxt.c && + git add multilineTxt.c && + git commit -q -m "multiline files" && + git push gitcvs.git >/dev/null +' + +rm -rf cvswork +test_expect_success 'cvs co (guess)' ' + GIT_DIR="$SERVERDIR" git config --bool gitcvs.usecrlfattr false && + GIT_CONFIG="$git_config" cvs -Q co -d cvswork master >cvs.log 2>&1 && + marked_as cvswork textfile.c "" && + marked_as cvswork binfile.bin -kb && + marked_as cvswork .gitattributes "" && + marked_as cvswork mixedUp.c -kb && + marked_as cvswork multiline.c -kb && + marked_as cvswork multilineTxt.c "" && + marked_as cvswork/subdir withCr.bin -kb && + marked_as cvswork/subdir file.h "" && + marked_as cvswork/subdir unspecified.other "" && + marked_as cvswork/subdir newfile.bin "" && + marked_as cvswork/subdir newfile.c "" +' + +test_expect_success 'cvs co another copy (guess)' ' + GIT_CONFIG="$git_config" cvs -Q co -d cvswork2 master >cvs.log 2>&1 && + marked_as cvswork2 textfile.c "" && + marked_as cvswork2 binfile.bin -kb && + marked_as cvswork2 .gitattributes "" && + marked_as cvswork2 mixedUp.c -kb && + marked_as cvswork2 multiline.c -kb && + marked_as cvswork2 multilineTxt.c "" && + marked_as cvswork2/subdir withCr.bin -kb && + marked_as cvswork2/subdir file.h "" && + marked_as cvswork2/subdir unspecified.other "" && + marked_as cvswork2/subdir newfile.bin "" && + marked_as cvswork2/subdir newfile.c "" +' + +test_expect_success 'add text (guess)' ' + cd cvswork && + echo "simpleText" > simpleText.c && + GIT_CONFIG="$git_config" cvs -Q add simpleText.c && + cd .. && + marked_as cvswork simpleText.c "" +' + +test_expect_success 'add bin (guess)' ' + cd cvswork && + echo "simpleBin: NUL: Q <- there" | q_to_nul > simpleBin.bin && + GIT_CONFIG="$git_config" cvs -Q add simpleBin.bin && + cd .. && + marked_as cvswork simpleBin.bin -kb +' + +test_expect_success 'remove files (guess)' ' + cd cvswork && + GIT_CONFIG="$git_config" cvs -Q rm -f subdir/file.h && + cd subdir && + GIT_CONFIG="$git_config" cvs -Q rm -f withCr.bin && + cd ../.. && + marked_as cvswork/subdir withCr.bin -kb && + marked_as cvswork/subdir file.h "" +' + +test_expect_success 'cvs ci (guess)' ' + cd cvswork && + GIT_CONFIG="$git_config" cvs -Q ci -m "add/rm files" >cvs.log 2>&1 && + cd .. && + marked_as cvswork textfile.c "" && + marked_as cvswork binfile.bin -kb && + marked_as cvswork .gitattributes "" && + marked_as cvswork mixedUp.c -kb && + marked_as cvswork multiline.c -kb && + marked_as cvswork multilineTxt.c "" && + not_present cvswork/subdir withCr.bin && + not_present cvswork/subdir file.h && + marked_as cvswork/subdir unspecified.other "" && + marked_as cvswork/subdir newfile.bin "" && + marked_as cvswork/subdir newfile.c "" && + marked_as cvswork simpleBin.bin -kb && + marked_as cvswork simpleText.c "" +' + +test_expect_success 'update subdir of other copy (guess)' ' + cd cvswork2/subdir && + GIT_CONFIG="$git_config" cvs -Q update && + cd ../.. && + marked_as cvswork2 textfile.c "" && + marked_as cvswork2 binfile.bin -kb && + marked_as cvswork2 .gitattributes "" && + marked_as cvswork2 mixedUp.c -kb && + marked_as cvswork2 multiline.c -kb && + marked_as cvswork2 multilineTxt.c "" && + not_present cvswork2/subdir withCr.bin && + not_present cvswork2/subdir file.h && + marked_as cvswork2/subdir unspecified.other "" && + marked_as cvswork2/subdir newfile.bin "" && + marked_as cvswork2/subdir newfile.c "" && + not_present cvswork2 simpleBin.bin && + not_present cvswork2 simpleText.c +' + +echo "starting update/merge" >> "${WORKDIR}/marked.log" +test_expect_success 'update/merge full other copy (guess)' ' + git pull gitcvs.git master && + sed "s/3/replaced_3/" < multilineTxt.c > ml.temp && + mv ml.temp multilineTxt.c && + git add multilineTxt.c && + git commit -q -m "modify multiline file" >> "${WORKDIR}/marked.log" && + git push gitcvs.git >/dev/null && + cd cvswork2 && + sed "s/1/replaced_1/" < multilineTxt.c > ml.temp && + mv ml.temp multilineTxt.c && + GIT_CONFIG="$git_config" cvs update > cvs.log 2>&1 && + cd .. && + marked_as cvswork2 textfile.c "" && + marked_as cvswork2 binfile.bin -kb && + marked_as cvswork2 .gitattributes "" && + marked_as cvswork2 mixedUp.c -kb && + marked_as cvswork2 multiline.c -kb && + marked_as cvswork2 multilineTxt.c "" && + not_present cvswork2/subdir withCr.bin && + not_present cvswork2/subdir file.h && + marked_as cvswork2/subdir unspecified.other "" && + marked_as cvswork2/subdir newfile.bin "" && + marked_as cvswork2/subdir newfile.c "" && + marked_as cvswork2 simpleBin.bin -kb && + marked_as cvswork2 simpleText.c "" && + echo "line replaced_1" > tmpExpect2 && + echo "line 2" >> tmpExpect2 && + echo "line replaced_3" >> tmpExpect2 && + echo "line 4" | q_to_nul >> tmpExpect2 && + cmp cvswork2/multilineTxt.c tmpExpect2 +' + +test_done diff --git a/transport.c b/transport.c index 1bc16f2b65..3ff851935f 100644 --- a/transport.c +++ b/transport.c @@ -203,7 +203,7 @@ static struct ref *get_refs_via_rsync(struct transport *transport) } static int fetch_objs_via_rsync(struct transport *transport, - int nr_objs, struct ref **to_fetch) + int nr_objs, const struct ref **to_fetch) { struct strbuf buf = STRBUF_INIT; struct child_process rsync; @@ -350,7 +350,7 @@ static int rsync_transport_push(struct transport *transport, #ifndef NO_CURL /* http fetch is the only user */ static int fetch_objs_via_walker(struct transport *transport, - int nr_objs, struct ref **to_fetch) + int nr_objs, const struct ref **to_fetch) { char *dest = xstrdup(transport->url); struct walker *walker = transport->data; @@ -517,7 +517,7 @@ static struct ref *get_refs_via_curl(struct transport *transport) } static int fetch_objs_via_curl(struct transport *transport, - int nr_objs, struct ref **to_fetch) + int nr_objs, const struct ref **to_fetch) { if (!transport->data) transport->data = get_http_walker(transport->url, @@ -554,7 +554,7 @@ static struct ref *get_refs_from_bundle(struct transport *transport) } static int fetch_refs_from_bundle(struct transport *transport, - int nr_heads, struct ref **to_fetch) + int nr_heads, const struct ref **to_fetch) { struct bundle_transport_data *data = transport->data; return unbundle(&data->header, data->fd); @@ -628,7 +628,7 @@ static struct ref *get_refs_via_connect(struct transport *transport) } static int fetch_refs_via_pack(struct transport *transport, - int nr_heads, struct ref **to_fetch) + int nr_heads, const struct ref **to_fetch) { struct git_transport_data *data = transport->data; char **heads = xmalloc(nr_heads * sizeof(*heads)); @@ -796,12 +796,12 @@ const struct ref *transport_get_remote_refs(struct transport *transport) return transport->remote_refs; } -int transport_fetch_refs(struct transport *transport, struct ref *refs) +int transport_fetch_refs(struct transport *transport, const struct ref *refs) { int rc; int nr_heads = 0, nr_alloc = 0; - struct ref **heads = NULL; - struct ref *rm; + const struct ref **heads = NULL; + const struct ref *rm; for (rm = refs; rm; rm = rm->next) { if (rm->peer_ref && diff --git a/transport.h b/transport.h index 8abfc0ae60..d0b52053ff 100644 --- a/transport.h +++ b/transport.h @@ -19,7 +19,7 @@ struct transport { const char *value); struct ref *(*get_refs_list)(struct transport *transport); - int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs); + int (*fetch)(struct transport *transport, int refs_nr, const struct ref **refs); int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags); int (*disconnect)(struct transport *connection); @@ -68,7 +68,7 @@ int transport_push(struct transport *connection, const struct ref *transport_get_remote_refs(struct transport *transport); -int transport_fetch_refs(struct transport *transport, struct ref *refs); +int transport_fetch_refs(struct transport *transport, const struct ref *refs); void transport_unlock_pack(struct transport *transport); int transport_disconnect(struct transport *transport); diff --git a/unpack-trees.c b/unpack-trees.c index 1ab28fda45..0de5a31c0b 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -8,6 +8,36 @@ #include "progress.h" #include "refs.h" +/* + * Error messages expected by scripts out of plumbing commands such as + * read-tree. Non-scripted Porcelain is not required to use these messages + * and in fact are encouraged to reword them to better suit their particular + * situation better. See how "git checkout" replaces not_uptodate_file to + * explain why it does not allow switching between branches when you have + * local changes, for example. + */ +static struct unpack_trees_error_msgs unpack_plumbing_errors = { + /* would_overwrite */ + "Entry '%s' would be overwritten by merge. Cannot merge.", + + /* not_uptodate_file */ + "Entry '%s' not uptodate. Cannot merge.", + + /* not_uptodate_dir */ + "Updating '%s' would lose untracked files in it", + + /* would_lose_untracked */ + "Untracked working tree file '%s' would be %s by merge.", + + /* bind_overlap */ + "Entry '%s' overlaps with '%s'. Cannot bind.", +}; + +#define ERRORMSG(o,fld) \ + ( ((o) && (o)->msgs.fld) \ + ? ((o)->msgs.fld) \ + : (unpack_plumbing_errors.fld) ) + static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce, unsigned int set, unsigned int clear) { @@ -383,10 +413,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options /* Here come the merge functions */ -static int reject_merge(struct cache_entry *ce) +static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o) { - return error("Entry '%s' would be overwritten by merge. Cannot merge.", - ce->name); + return error(ERRORMSG(o, would_overwrite), ce->name); } static int same(struct cache_entry *a, struct cache_entry *b) @@ -430,7 +459,7 @@ static int verify_uptodate(struct cache_entry *ce, if (errno == ENOENT) return 0; return o->gently ? -1 : - error("Entry '%s' not uptodate. Cannot merge.", ce->name); + error(ERRORMSG(o, not_uptodate_file), ce->name); } static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o) @@ -517,8 +546,7 @@ static int verify_clean_subdirectory(struct cache_entry *ce, const char *action, i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL); if (i) return o->gently ? -1 : - error("Updating '%s' would lose untracked files in it", - ce->name); + error(ERRORMSG(o, not_uptodate_dir), ce->name); free(pathbuf); return cnt; } @@ -618,8 +646,7 @@ static int verify_absent(struct cache_entry *ce, const char *action, } return o->gently ? -1 : - error("Untracked working tree file '%s' " - "would be %s by merge.", ce->name, action); + error(ERRORMSG(o, would_lose_untracked), ce->name, action); } return 0; } @@ -751,7 +778,7 @@ int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o) /* #14, #14ALT, #2ALT */ if (remote && !df_conflict_head && head_match && !remote_match) { if (index && !same(index, remote) && !same(index, head)) - return o->gently ? -1 : reject_merge(index); + return o->gently ? -1 : reject_merge(index, o); return merged_entry(remote, index, o); } /* @@ -759,7 +786,7 @@ int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o) * make sure that it matches head. */ if (index && !same(index, head)) - return o->gently ? -1 : reject_merge(index); + return o->gently ? -1 : reject_merge(index, o); if (head) { /* #5ALT, #15 */ @@ -901,11 +928,11 @@ int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o) else { /* all other failures */ if (oldtree) - return o->gently ? -1 : reject_merge(oldtree); + return o->gently ? -1 : reject_merge(oldtree, o); if (current) - return o->gently ? -1 : reject_merge(current); + return o->gently ? -1 : reject_merge(current, o); if (newtree) - return o->gently ? -1 : reject_merge(newtree); + return o->gently ? -1 : reject_merge(newtree, o); return -1; } } @@ -931,7 +958,7 @@ int bind_merge(struct cache_entry **src, o->merge_size); if (a && old) return o->gently ? -1 : - error("Entry '%s' overlaps with '%s'. Cannot bind.", a->name, old->name); + error(ERRORMSG(o, bind_overlap), a->name, old->name); if (!a) return keep_entry(old, o); else diff --git a/unpack-trees.h b/unpack-trees.h index d436d6ced9..94e567265a 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -8,6 +8,14 @@ struct unpack_trees_options; typedef int (*merge_fn_t)(struct cache_entry **src, struct unpack_trees_options *options); +struct unpack_trees_error_msgs { + const char *would_overwrite; + const char *not_uptodate_file; + const char *not_uptodate_dir; + const char *would_lose_untracked; + const char *bind_overlap; +}; + struct unpack_trees_options { unsigned int reset:1, merge:1, @@ -23,6 +31,7 @@ struct unpack_trees_options { int pos; struct dir_struct *dir; merge_fn_t fn; + struct unpack_trees_error_msgs msgs; int head_idx; int merge_size; |