diff options
Diffstat (limited to 'builtin')
94 files changed, 47544 insertions, 0 deletions
diff --git a/builtin/add.c b/builtin/add.c new file mode 100644 index 0000000000..075312afcd --- /dev/null +++ b/builtin/add.c @@ -0,0 +1,485 @@ +/* + * "git add" builtin command + * + * Copyright (C) 2006 Linus Torvalds + */ +#include "cache.h" +#include "builtin.h" +#include "dir.h" +#include "exec_cmd.h" +#include "cache-tree.h" +#include "run-command.h" +#include "parse-options.h" +#include "diff.h" +#include "diffcore.h" +#include "revision.h" +#include "bulk-checkin.h" + +static const char * const builtin_add_usage[] = { + N_("git add [options] [--] <filepattern>..."), + NULL +}; +static int patch_interactive, add_interactive, edit_interactive; +static int take_worktree_changes; + +struct update_callback_data { + int flags; + int add_errors; +}; + +static int fix_unmerged_status(struct diff_filepair *p, + struct update_callback_data *data) +{ + if (p->status != DIFF_STATUS_UNMERGED) + return p->status; + if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode) + /* + * This is not an explicit add request, and the + * path is missing from the working tree (deleted) + */ + return DIFF_STATUS_DELETED; + else + /* + * Either an explicit add request, or path exists + * in the working tree. An attempt to explicitly + * add a path that does not exist in the working tree + * will be caught as an error by the caller immediately. + */ + return DIFF_STATUS_MODIFIED; +} + +static void update_callback(struct diff_queue_struct *q, + struct diff_options *opt, void *cbdata) +{ + int i; + struct update_callback_data *data = cbdata; + + for (i = 0; i < q->nr; i++) { + struct diff_filepair *p = q->queue[i]; + const char *path = p->one->path; + switch (fix_unmerged_status(p, data)) { + default: + die(_("unexpected diff status %c"), p->status); + case DIFF_STATUS_MODIFIED: + case DIFF_STATUS_TYPE_CHANGED: + if (add_file_to_index(&the_index, path, data->flags)) { + if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) + die(_("updating files failed")); + data->add_errors++; + } + break; + case DIFF_STATUS_DELETED: + if (data->flags & ADD_CACHE_IGNORE_REMOVAL) + break; + if (!(data->flags & ADD_CACHE_PRETEND)) + remove_file_from_index(&the_index, path); + if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) + printf(_("remove '%s'\n"), path); + break; + } + } +} + +int add_files_to_cache(const char *prefix, const char **pathspec, int flags) +{ + struct update_callback_data data; + struct rev_info rev; + init_revisions(&rev, prefix); + setup_revisions(0, NULL, &rev, NULL); + init_pathspec(&rev.prune_data, pathspec); + rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; + rev.diffopt.format_callback = update_callback; + data.flags = flags; + data.add_errors = 0; + rev.diffopt.format_callback_data = &data; + rev.max_count = 0; /* do not compare unmerged paths with stage #2 */ + run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); + return !!data.add_errors; +} + +static void fill_pathspec_matches(const char **pathspec, char *seen, int specs) +{ + int num_unmatched = 0, i; + + /* + * Since we are walking the index as if we were walking the directory, + * we have to mark the matched pathspec as seen; otherwise we will + * mistakenly think that the user gave a pathspec that did not match + * anything. + */ + for (i = 0; i < specs; i++) + if (!seen[i]) + num_unmatched++; + if (!num_unmatched) + return; + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen); + } +} + +static char *find_used_pathspec(const char **pathspec) +{ + char *seen; + int i; + + for (i = 0; pathspec[i]; i++) + ; /* just counting */ + seen = xcalloc(i, 1); + fill_pathspec_matches(pathspec, seen, i); + return seen; +} + +static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix) +{ + char *seen; + int i, specs; + struct dir_entry **src, **dst; + + for (specs = 0; pathspec[specs]; specs++) + /* nothing */; + seen = xcalloc(specs, 1); + + src = dst = dir->entries; + i = dir->nr; + while (--i >= 0) { + struct dir_entry *entry = *src++; + if (match_pathspec(pathspec, entry->name, entry->len, + prefix, seen)) + *dst++ = entry; + } + dir->nr = dst - dir->entries; + fill_pathspec_matches(pathspec, seen, specs); + return seen; +} + +static void treat_gitlinks(const char **pathspec) +{ + int i; + + if (!pathspec || !*pathspec) + return; + + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + if (S_ISGITLINK(ce->ce_mode)) { + int len = ce_namelen(ce), j; + for (j = 0; pathspec[j]; j++) { + int len2 = strlen(pathspec[j]); + if (len2 <= len || pathspec[j][len] != '/' || + memcmp(ce->name, pathspec[j], len)) + continue; + if (len2 == len + 1) + /* strip trailing slash */ + pathspec[j] = xstrndup(ce->name, len); + else + die (_("Path '%s' is in submodule '%.*s'"), + pathspec[j], len, ce->name); + } + } + } +} + +static void refresh(int verbose, const char **pathspec) +{ + char *seen; + int i, specs; + + for (specs = 0; pathspec[specs]; specs++) + /* nothing */; + seen = xcalloc(specs, 1); + refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET, + pathspec, seen, _("Unstaged changes after refreshing the index:")); + for (i = 0; i < specs; i++) { + if (!seen[i]) + die(_("pathspec '%s' did not match any files"), pathspec[i]); + } + free(seen); +} + +static const char **validate_pathspec(int argc, const char **argv, const char *prefix) +{ + const char **pathspec = get_pathspec(prefix, argv); + + if (pathspec) { + const char **p; + for (p = pathspec; *p; p++) { + if (has_symlink_leading_path(*p, strlen(*p))) { + int len = prefix ? strlen(prefix) : 0; + die(_("'%s' is beyond a symbolic link"), *p + len); + } + } + } + + return pathspec; +} + +int run_add_interactive(const char *revision, const char *patch_mode, + const char **pathspec) +{ + int status, ac, pc = 0; + const char **args; + + if (pathspec) + while (pathspec[pc]) + pc++; + + args = xcalloc(sizeof(const char *), (pc + 5)); + ac = 0; + args[ac++] = "add--interactive"; + if (patch_mode) + args[ac++] = patch_mode; + if (revision) + args[ac++] = revision; + args[ac++] = "--"; + if (pc) { + memcpy(&(args[ac]), pathspec, sizeof(const char *) * pc); + ac += pc; + } + args[ac] = NULL; + + status = run_command_v_opt(args, RUN_GIT_CMD); + free(args); + return status; +} + +int interactive_add(int argc, const char **argv, const char *prefix, int patch) +{ + const char **pathspec = NULL; + + if (argc) { + pathspec = validate_pathspec(argc, argv, prefix); + if (!pathspec) + return -1; + } + + return run_add_interactive(NULL, + patch ? "--patch" : NULL, + pathspec); +} + +static int edit_patch(int argc, const char **argv, const char *prefix) +{ + char *file = git_pathdup("ADD_EDIT.patch"); + const char *apply_argv[] = { "apply", "--recount", "--cached", + NULL, NULL }; + struct child_process child; + struct rev_info rev; + int out; + struct stat st; + + apply_argv[3] = file; + + git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ + + if (read_cache() < 0) + die (_("Could not read the index")); + + init_revisions(&rev, prefix); + rev.diffopt.context = 7; + + argc = setup_revisions(argc, argv, &rev, NULL); + rev.diffopt.output_format = DIFF_FORMAT_PATCH; + DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES); + out = open(file, O_CREAT | O_WRONLY, 0666); + if (out < 0) + die (_("Could not open '%s' for writing."), file); + rev.diffopt.file = xfdopen(out, "w"); + rev.diffopt.close_file = 1; + if (run_diff_files(&rev, 0)) + die (_("Could not write patch")); + + launch_editor(file, NULL, NULL); + + if (stat(file, &st)) + die_errno(_("Could not stat '%s'"), file); + if (!st.st_size) + die(_("Empty patch. Aborted.")); + + memset(&child, 0, sizeof(child)); + child.git_cmd = 1; + child.argv = apply_argv; + if (run_command(&child)) + die (_("Could not apply '%s'"), file); + + unlink(file); + free(file); + return 0; +} + +static struct lock_file lock_file; + +static const char ignore_error[] = +N_("The following paths are ignored by one of your .gitignore files:\n"); + +static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; +static int ignore_add_errors, addremove, intent_to_add, ignore_missing = 0; + +static struct option builtin_add_options[] = { + OPT__DRY_RUN(&show_only, N_("dry run")), + OPT__VERBOSE(&verbose, N_("be verbose")), + OPT_GROUP(""), + OPT_BOOLEAN('i', "interactive", &add_interactive, N_("interactive picking")), + OPT_BOOLEAN('p', "patch", &patch_interactive, N_("select hunks interactively")), + OPT_BOOLEAN('e', "edit", &edit_interactive, N_("edit current diff and apply")), + OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")), + OPT_BOOLEAN('u', "update", &take_worktree_changes, N_("update tracked files")), + OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")), + OPT_BOOLEAN('A', "all", &addremove, N_("add changes from all tracked and untracked files")), + OPT_BOOLEAN( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")), + OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")), + OPT_BOOLEAN( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")), + OPT_END(), +}; + +static int add_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "add.ignoreerrors") || + !strcmp(var, "add.ignore-errors")) { + ignore_add_errors = git_config_bool(var, value); + return 0; + } + return git_default_config(var, value, cb); +} + +static int add_files(struct dir_struct *dir, int flags) +{ + int i, exit_status = 0; + + if (dir->ignored_nr) { + fprintf(stderr, _(ignore_error)); + for (i = 0; i < dir->ignored_nr; i++) + fprintf(stderr, "%s\n", dir->ignored[i]->name); + fprintf(stderr, _("Use -f if you really want to add them.\n")); + die(_("no files added")); + } + + for (i = 0; i < dir->nr; i++) + if (add_file_to_cache(dir->entries[i]->name, flags)) { + if (!ignore_add_errors) + die(_("adding files failed")); + exit_status = 1; + } + return exit_status; +} + +int cmd_add(int argc, const char **argv, const char *prefix) +{ + int exit_status = 0; + int newfd; + const char **pathspec; + struct dir_struct dir; + int flags; + int add_new_files; + int require_pathspec; + char *seen = NULL; + + git_config(add_config, NULL); + + argc = parse_options(argc, argv, prefix, builtin_add_options, + builtin_add_usage, PARSE_OPT_KEEP_ARGV0); + if (patch_interactive) + add_interactive = 1; + if (add_interactive) + exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive)); + + if (edit_interactive) + return(edit_patch(argc, argv, prefix)); + argc--; + argv++; + + if (addremove && take_worktree_changes) + die(_("-A and -u are mutually incompatible")); + if (!show_only && ignore_missing) + die(_("Option --ignore-missing can only be used together with --dry-run")); + if ((addremove || take_worktree_changes) && !argc) { + static const char *here[2] = { ".", NULL }; + argc = 1; + argv = here; + } + + add_new_files = !take_worktree_changes && !refresh_only; + require_pathspec = !take_worktree_changes; + + newfd = hold_locked_index(&lock_file, 1); + + flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | + (show_only ? ADD_CACHE_PRETEND : 0) | + (intent_to_add ? ADD_CACHE_INTENT : 0) | + (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) | + (!(addremove || take_worktree_changes) + ? ADD_CACHE_IGNORE_REMOVAL : 0)); + + if (require_pathspec && argc == 0) { + fprintf(stderr, _("Nothing specified, nothing added.\n")); + fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n")); + return 0; + } + pathspec = validate_pathspec(argc, argv, prefix); + + if (read_cache() < 0) + die(_("index file corrupt")); + treat_gitlinks(pathspec); + + if (add_new_files) { + int baselen; + + /* Set up the default git porcelain excludes */ + memset(&dir, 0, sizeof(dir)); + if (!ignored_too) { + dir.flags |= DIR_COLLECT_IGNORED; + setup_standard_excludes(&dir); + } + + /* This picks up the paths that are not tracked */ + baselen = fill_directory(&dir, pathspec); + if (pathspec) + seen = prune_directory(&dir, pathspec, baselen); + } + + if (refresh_only) { + refresh(verbose, pathspec); + goto finish; + } + + if (pathspec) { + int i; + struct path_exclude_check check; + + path_exclude_check_init(&check, &dir); + if (!seen) + seen = find_used_pathspec(pathspec); + for (i = 0; pathspec[i]; i++) { + if (!seen[i] && pathspec[i][0] + && !file_exists(pathspec[i])) { + if (ignore_missing) { + int dtype = DT_UNKNOWN; + if (is_path_excluded(&check, pathspec[i], -1, &dtype)) + dir_add_ignored(&dir, pathspec[i], strlen(pathspec[i])); + } else + die(_("pathspec '%s' did not match any files"), + pathspec[i]); + } + } + free(seen); + path_exclude_check_clear(&check); + } + + plug_bulk_checkin(); + + exit_status |= add_files_to_cache(prefix, pathspec, flags); + + if (add_new_files) + exit_status |= add_files(&dir, flags); + + unplug_bulk_checkin(); + + finish: + if (active_cache_changed) { + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(&lock_file)) + die(_("Unable to write new index file")); + } + + return exit_status; +} diff --git a/builtin/annotate.c b/builtin/annotate.c new file mode 100644 index 0000000000..fc43eed36b --- /dev/null +++ b/builtin/annotate.c @@ -0,0 +1,24 @@ +/* + * "git annotate" builtin alias + * + * Copyright (C) 2006 Ryan Anderson + */ +#include "git-compat-util.h" +#include "builtin.h" + +int cmd_annotate(int argc, const char **argv, const char *prefix) +{ + const char **nargv; + int i; + nargv = xmalloc(sizeof(char *) * (argc + 2)); + + nargv[0] = "annotate"; + nargv[1] = "-c"; + + for (i = 1; i < argc; i++) { + nargv[i+1] = argv[i]; + } + nargv[argc + 1] = NULL; + + return cmd_blame(argc + 1, nargv, prefix); +} diff --git a/builtin/apply.c b/builtin/apply.c new file mode 100644 index 0000000000..6c11e8bc73 --- /dev/null +++ b/builtin/apply.c @@ -0,0 +1,4465 @@ +/* + * apply.c + * + * Copyright (C) Linus Torvalds, 2005 + * + * This applies patches on top of some (arbitrary) version of the SCM. + * + */ +#include "cache.h" +#include "cache-tree.h" +#include "quote.h" +#include "blob.h" +#include "delta.h" +#include "builtin.h" +#include "string-list.h" +#include "dir.h" +#include "diff.h" +#include "parse-options.h" +#include "xdiff-interface.h" +#include "ll-merge.h" +#include "rerere.h" + +/* + * --check turns on checking that the working tree matches the + * files that are being modified, but doesn't apply the patch + * --stat does just a diffstat, and doesn't actually apply + * --numstat does numeric diffstat, and doesn't actually apply + * --index-info shows the old and new index info for paths if available. + * --index updates the cache as well. + * --cached updates only the cache without ever touching the working tree. + */ +static const char *prefix; +static int prefix_length = -1; +static int newfd = -1; + +static int unidiff_zero; +static int p_value = 1; +static int p_value_known; +static int check_index; +static int update_index; +static int cached; +static int diffstat; +static int numstat; +static int summary; +static int check; +static int apply = 1; +static int apply_in_reverse; +static int apply_with_reject; +static int apply_verbosely; +static int allow_overlap; +static int no_add; +static int threeway; +static const char *fake_ancestor; +static int line_termination = '\n'; +static unsigned int p_context = UINT_MAX; +static const char * const apply_usage[] = { + N_("git apply [options] [<patch>...]"), + NULL +}; + +static enum ws_error_action { + nowarn_ws_error, + warn_on_ws_error, + die_on_ws_error, + correct_ws_error +} ws_error_action = warn_on_ws_error; +static int whitespace_error; +static int squelch_whitespace_errors = 5; +static int applied_after_fixing_ws; + +static enum ws_ignore { + ignore_ws_none, + ignore_ws_change +} ws_ignore_action = ignore_ws_none; + + +static const char *patch_input_file; +static const char *root; +static int root_len; +static int read_stdin = 1; +static int options; + +static void parse_whitespace_option(const char *option) +{ + if (!option) { + ws_error_action = warn_on_ws_error; + return; + } + if (!strcmp(option, "warn")) { + ws_error_action = warn_on_ws_error; + return; + } + if (!strcmp(option, "nowarn")) { + ws_error_action = nowarn_ws_error; + return; + } + if (!strcmp(option, "error")) { + ws_error_action = die_on_ws_error; + return; + } + if (!strcmp(option, "error-all")) { + ws_error_action = die_on_ws_error; + squelch_whitespace_errors = 0; + return; + } + if (!strcmp(option, "strip") || !strcmp(option, "fix")) { + ws_error_action = correct_ws_error; + return; + } + die(_("unrecognized whitespace option '%s'"), option); +} + +static void parse_ignorewhitespace_option(const char *option) +{ + if (!option || !strcmp(option, "no") || + !strcmp(option, "false") || !strcmp(option, "never") || + !strcmp(option, "none")) { + ws_ignore_action = ignore_ws_none; + return; + } + if (!strcmp(option, "change")) { + ws_ignore_action = ignore_ws_change; + return; + } + die(_("unrecognized whitespace ignore option '%s'"), option); +} + +static void set_default_whitespace_mode(const char *whitespace_option) +{ + if (!whitespace_option && !apply_default_whitespace) + ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error); +} + +/* + * For "diff-stat" like behaviour, we keep track of the biggest change + * we've seen, and the longest filename. That allows us to do simple + * scaling. + */ +static int max_change, max_len; + +/* + * Various "current state", notably line numbers and what + * file (and how) we're patching right now.. The "is_xxxx" + * things are flags, where -1 means "don't know yet". + */ +static int linenr = 1; + +/* + * This represents one "hunk" from a patch, starting with + * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The + * patch text is pointed at by patch, and its byte length + * is stored in size. leading and trailing are the number + * of context lines. + */ +struct fragment { + unsigned long leading, trailing; + unsigned long oldpos, oldlines; + unsigned long newpos, newlines; + /* + * 'patch' is usually borrowed from buf in apply_patch(), + * but some codepaths store an allocated buffer. + */ + const char *patch; + unsigned free_patch:1, + rejected:1; + int size; + int linenr; + struct fragment *next; +}; + +/* + * When dealing with a binary patch, we reuse "leading" field + * to store the type of the binary hunk, either deflated "delta" + * or deflated "literal". + */ +#define binary_patch_method leading +#define BINARY_DELTA_DEFLATED 1 +#define BINARY_LITERAL_DEFLATED 2 + +/* + * This represents a "patch" to a file, both metainfo changes + * such as creation/deletion, filemode and content changes represented + * as a series of fragments. + */ +struct patch { + char *new_name, *old_name, *def_name; + unsigned int old_mode, new_mode; + int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */ + int rejected; + unsigned ws_rule; + int lines_added, lines_deleted; + int score; + unsigned int is_toplevel_relative:1; + unsigned int inaccurate_eof:1; + unsigned int is_binary:1; + unsigned int is_copy:1; + unsigned int is_rename:1; + unsigned int recount:1; + unsigned int conflicted_threeway:1; + unsigned int direct_to_threeway:1; + struct fragment *fragments; + char *result; + size_t resultsize; + char old_sha1_prefix[41]; + char new_sha1_prefix[41]; + struct patch *next; + + /* three-way fallback result */ + unsigned char threeway_stage[3][20]; +}; + +static void free_fragment_list(struct fragment *list) +{ + while (list) { + struct fragment *next = list->next; + if (list->free_patch) + free((char *)list->patch); + free(list); + list = next; + } +} + +static void free_patch(struct patch *patch) +{ + free_fragment_list(patch->fragments); + free(patch->def_name); + free(patch->old_name); + free(patch->new_name); + free(patch->result); + free(patch); +} + +static void free_patch_list(struct patch *list) +{ + while (list) { + struct patch *next = list->next; + free_patch(list); + list = next; + } +} + +/* + * A line in a file, len-bytes long (includes the terminating LF, + * except for an incomplete line at the end if the file ends with + * one), and its contents hashes to 'hash'. + */ +struct line { + size_t len; + unsigned hash : 24; + unsigned flag : 8; +#define LINE_COMMON 1 +#define LINE_PATCHED 2 +}; + +/* + * This represents a "file", which is an array of "lines". + */ +struct image { + char *buf; + size_t len; + size_t nr; + size_t alloc; + struct line *line_allocated; + struct line *line; +}; + +/* + * Records filenames that have been touched, in order to handle + * the case where more than one patches touch the same file. + */ + +static struct string_list fn_table; + +static uint32_t hash_line(const char *cp, size_t len) +{ + size_t i; + uint32_t h; + for (i = 0, h = 0; i < len; i++) { + if (!isspace(cp[i])) { + h = h * 3 + (cp[i] & 0xff); + } + } + return h; +} + +/* + * Compare lines s1 of length n1 and s2 of length n2, ignoring + * whitespace difference. Returns 1 if they match, 0 otherwise + */ +static int fuzzy_matchlines(const char *s1, size_t n1, + const char *s2, size_t n2) +{ + const char *last1 = s1 + n1 - 1; + const char *last2 = s2 + n2 - 1; + int result = 0; + + /* ignore line endings */ + while ((*last1 == '\r') || (*last1 == '\n')) + last1--; + while ((*last2 == '\r') || (*last2 == '\n')) + last2--; + + /* skip leading whitespace */ + while (isspace(*s1) && (s1 <= last1)) + s1++; + while (isspace(*s2) && (s2 <= last2)) + s2++; + /* early return if both lines are empty */ + if ((s1 > last1) && (s2 > last2)) + return 1; + while (!result) { + result = *s1++ - *s2++; + /* + * Skip whitespace inside. We check for whitespace on + * both buffers because we don't want "a b" to match + * "ab" + */ + if (isspace(*s1) && isspace(*s2)) { + while (isspace(*s1) && s1 <= last1) + s1++; + while (isspace(*s2) && s2 <= last2) + s2++; + } + /* + * If we reached the end on one side only, + * lines don't match + */ + if ( + ((s2 > last2) && (s1 <= last1)) || + ((s1 > last1) && (s2 <= last2))) + return 0; + if ((s1 > last1) && (s2 > last2)) + break; + } + + return !result; +} + +static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag) +{ + ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc); + img->line_allocated[img->nr].len = len; + img->line_allocated[img->nr].hash = hash_line(bol, len); + img->line_allocated[img->nr].flag = flag; + img->nr++; +} + +/* + * "buf" has the file contents to be patched (read from various sources). + * attach it to "image" and add line-based index to it. + * "image" now owns the "buf". + */ +static void prepare_image(struct image *image, char *buf, size_t len, + int prepare_linetable) +{ + const char *cp, *ep; + + memset(image, 0, sizeof(*image)); + image->buf = buf; + image->len = len; + + if (!prepare_linetable) + return; + + ep = image->buf + image->len; + cp = image->buf; + while (cp < ep) { + const char *next; + for (next = cp; next < ep && *next != '\n'; next++) + ; + if (next < ep) + next++; + add_line_info(image, cp, next - cp, 0); + cp = next; + } + image->line = image->line_allocated; +} + +static void clear_image(struct image *image) +{ + free(image->buf); + free(image->line_allocated); + memset(image, 0, sizeof(*image)); +} + +/* fmt must contain _one_ %s and no other substitution */ +static void say_patch_name(FILE *output, const char *fmt, struct patch *patch) +{ + struct strbuf sb = STRBUF_INIT; + + if (patch->old_name && patch->new_name && + strcmp(patch->old_name, patch->new_name)) { + quote_c_style(patch->old_name, &sb, NULL, 0); + strbuf_addstr(&sb, " => "); + quote_c_style(patch->new_name, &sb, NULL, 0); + } else { + const char *n = patch->new_name; + if (!n) + n = patch->old_name; + quote_c_style(n, &sb, NULL, 0); + } + fprintf(output, fmt, sb.buf); + fputc('\n', output); + strbuf_release(&sb); +} + +#define SLOP (16) + +static void read_patch_file(struct strbuf *sb, int fd) +{ + if (strbuf_read(sb, fd, 0) < 0) + die_errno("git apply: failed to read"); + + /* + * Make sure that we have some slop in the buffer + * so that we can do speculative "memcmp" etc, and + * see to it that it is NUL-filled. + */ + strbuf_grow(sb, SLOP); + memset(sb->buf + sb->len, 0, SLOP); +} + +static unsigned long linelen(const char *buffer, unsigned long size) +{ + unsigned long len = 0; + while (size--) { + len++; + if (*buffer++ == '\n') + break; + } + return len; +} + +static int is_dev_null(const char *str) +{ + return !memcmp("/dev/null", str, 9) && isspace(str[9]); +} + +#define TERM_SPACE 1 +#define TERM_TAB 2 + +static int name_terminate(const char *name, int namelen, int c, int terminate) +{ + if (c == ' ' && !(terminate & TERM_SPACE)) + return 0; + if (c == '\t' && !(terminate & TERM_TAB)) + return 0; + + return 1; +} + +/* remove double slashes to make --index work with such filenames */ +static char *squash_slash(char *name) +{ + int i = 0, j = 0; + + if (!name) + return NULL; + + while (name[i]) { + if ((name[j++] = name[i++]) == '/') + while (name[i] == '/') + i++; + } + name[j] = '\0'; + return name; +} + +static char *find_name_gnu(const char *line, const char *def, int p_value) +{ + struct strbuf name = STRBUF_INIT; + char *cp; + + /* + * Proposed "new-style" GNU patch/diff format; see + * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2 + */ + if (unquote_c_style(&name, line, NULL)) { + strbuf_release(&name); + return NULL; + } + + for (cp = name.buf; p_value; p_value--) { + cp = strchr(cp, '/'); + if (!cp) { + strbuf_release(&name); + return NULL; + } + cp++; + } + + strbuf_remove(&name, 0, cp - name.buf); + if (root) + strbuf_insert(&name, 0, root, root_len); + return squash_slash(strbuf_detach(&name, NULL)); +} + +static size_t sane_tz_len(const char *line, size_t len) +{ + const char *tz, *p; + + if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ') + return 0; + tz = line + len - strlen(" +0500"); + + if (tz[1] != '+' && tz[1] != '-') + return 0; + + for (p = tz + 2; p != line + len; p++) + if (!isdigit(*p)) + return 0; + + return line + len - tz; +} + +static size_t tz_with_colon_len(const char *line, size_t len) +{ + const char *tz, *p; + + if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':') + return 0; + tz = line + len - strlen(" +08:00"); + + if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-')) + return 0; + p = tz + 2; + if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || + !isdigit(*p++) || !isdigit(*p++)) + return 0; + + return line + len - tz; +} + +static size_t date_len(const char *line, size_t len) +{ + const char *date, *p; + + if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-') + return 0; + p = date = line + len - strlen("72-02-05"); + + if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' || + !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' || + !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */ + return 0; + + if (date - line >= strlen("19") && + isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */ + date -= strlen("19"); + + return line + len - date; +} + +static size_t short_time_len(const char *line, size_t len) +{ + const char *time, *p; + + if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':') + return 0; + p = time = line + len - strlen(" 07:01:32"); + + /* Permit 1-digit hours? */ + if (*p++ != ' ' || + !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || + !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || + !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */ + return 0; + + return line + len - time; +} + +static size_t fractional_time_len(const char *line, size_t len) +{ + const char *p; + size_t n; + + /* Expected format: 19:41:17.620000023 */ + if (!len || !isdigit(line[len - 1])) + return 0; + p = line + len - 1; + + /* Fractional seconds. */ + while (p > line && isdigit(*p)) + p--; + if (*p != '.') + return 0; + + /* Hours, minutes, and whole seconds. */ + n = short_time_len(line, p - line); + if (!n) + return 0; + + return line + len - p + n; +} + +static size_t trailing_spaces_len(const char *line, size_t len) +{ + const char *p; + + /* Expected format: ' ' x (1 or more) */ + if (!len || line[len - 1] != ' ') + return 0; + + p = line + len; + while (p != line) { + p--; + if (*p != ' ') + return line + len - (p + 1); + } + + /* All spaces! */ + return len; +} + +static size_t diff_timestamp_len(const char *line, size_t len) +{ + const char *end = line + len; + size_t n; + + /* + * Posix: 2010-07-05 19:41:17 + * GNU: 2010-07-05 19:41:17.620000023 -0500 + */ + + if (!isdigit(end[-1])) + return 0; + + n = sane_tz_len(line, end - line); + if (!n) + n = tz_with_colon_len(line, end - line); + end -= n; + + n = short_time_len(line, end - line); + if (!n) + n = fractional_time_len(line, end - line); + end -= n; + + n = date_len(line, end - line); + if (!n) /* No date. Too bad. */ + return 0; + end -= n; + + if (end == line) /* No space before date. */ + return 0; + if (end[-1] == '\t') { /* Success! */ + end--; + return line + len - end; + } + if (end[-1] != ' ') /* No space before date. */ + return 0; + + /* Whitespace damage. */ + end -= trailing_spaces_len(line, end - line); + return line + len - end; +} + +static char *null_strdup(const char *s) +{ + return s ? xstrdup(s) : NULL; +} + +static char *find_name_common(const char *line, const char *def, + int p_value, const char *end, int terminate) +{ + int len; + const char *start = NULL; + + if (p_value == 0) + start = line; + while (line != end) { + char c = *line; + + if (!end && isspace(c)) { + if (c == '\n') + break; + if (name_terminate(start, line-start, c, terminate)) + break; + } + line++; + if (c == '/' && !--p_value) + start = line; + } + if (!start) + return squash_slash(null_strdup(def)); + len = line - start; + if (!len) + return squash_slash(null_strdup(def)); + + /* + * Generally we prefer the shorter name, especially + * if the other one is just a variation of that with + * something else tacked on to the end (ie "file.orig" + * or "file~"). + */ + if (def) { + int deflen = strlen(def); + if (deflen < len && !strncmp(start, def, deflen)) + return squash_slash(xstrdup(def)); + } + + if (root) { + char *ret = xmalloc(root_len + len + 1); + strcpy(ret, root); + memcpy(ret + root_len, start, len); + ret[root_len + len] = '\0'; + return squash_slash(ret); + } + + return squash_slash(xmemdupz(start, len)); +} + +static char *find_name(const char *line, char *def, int p_value, int terminate) +{ + if (*line == '"') { + char *name = find_name_gnu(line, def, p_value); + if (name) + return name; + } + + return find_name_common(line, def, p_value, NULL, terminate); +} + +static char *find_name_traditional(const char *line, char *def, int p_value) +{ + size_t len = strlen(line); + size_t date_len; + + if (*line == '"') { + char *name = find_name_gnu(line, def, p_value); + if (name) + return name; + } + + len = strchrnul(line, '\n') - line; + date_len = diff_timestamp_len(line, len); + if (!date_len) + return find_name_common(line, def, p_value, NULL, TERM_TAB); + len -= date_len; + + return find_name_common(line, def, p_value, line + len, 0); +} + +static int count_slashes(const char *cp) +{ + int cnt = 0; + char ch; + + while ((ch = *cp++)) + if (ch == '/') + cnt++; + return cnt; +} + +/* + * Given the string after "--- " or "+++ ", guess the appropriate + * p_value for the given patch. + */ +static int guess_p_value(const char *nameline) +{ + char *name, *cp; + int val = -1; + + if (is_dev_null(nameline)) + return -1; + name = find_name_traditional(nameline, NULL, 0); + if (!name) + return -1; + cp = strchr(name, '/'); + if (!cp) + val = 0; + else if (prefix) { + /* + * Does it begin with "a/$our-prefix" and such? Then this is + * very likely to apply to our directory. + */ + if (!strncmp(name, prefix, prefix_length)) + val = count_slashes(prefix); + else { + cp++; + if (!strncmp(cp, prefix, prefix_length)) + val = count_slashes(prefix) + 1; + } + } + free(name); + return val; +} + +/* + * Does the ---/+++ line has the POSIX timestamp after the last HT? + * GNU diff puts epoch there to signal a creation/deletion event. Is + * this such a timestamp? + */ +static int has_epoch_timestamp(const char *nameline) +{ + /* + * We are only interested in epoch timestamp; any non-zero + * fraction cannot be one, hence "(\.0+)?" in the regexp below. + * For the same reason, the date must be either 1969-12-31 or + * 1970-01-01, and the seconds part must be "00". + */ + const char stamp_regexp[] = + "^(1969-12-31|1970-01-01)" + " " + "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?" + " " + "([-+][0-2][0-9]:?[0-5][0-9])\n"; + const char *timestamp = NULL, *cp, *colon; + static regex_t *stamp; + regmatch_t m[10]; + int zoneoffset; + int hourminute; + int status; + + for (cp = nameline; *cp != '\n'; cp++) { + if (*cp == '\t') + timestamp = cp + 1; + } + if (!timestamp) + return 0; + if (!stamp) { + stamp = xmalloc(sizeof(*stamp)); + if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) { + warning(_("Cannot prepare timestamp regexp %s"), + stamp_regexp); + return 0; + } + } + + status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0); + if (status) { + if (status != REG_NOMATCH) + warning(_("regexec returned %d for input: %s"), + status, timestamp); + return 0; + } + + zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10); + if (*colon == ':') + zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10); + else + zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100); + if (timestamp[m[3].rm_so] == '-') + zoneoffset = -zoneoffset; + + /* + * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31 + * (west of GMT) or 1970-01-01 (east of GMT) + */ + if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) || + (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10))) + return 0; + + hourminute = (strtol(timestamp + 11, NULL, 10) * 60 + + strtol(timestamp + 14, NULL, 10) - + zoneoffset); + + return ((zoneoffset < 0 && hourminute == 1440) || + (0 <= zoneoffset && !hourminute)); +} + +/* + * 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 + * new file we should try to match whatever "patch" does. I have no idea. + */ +static void parse_traditional_patch(const char *first, const char *second, struct patch *patch) +{ + char *name; + + first += 4; /* skip "--- " */ + second += 4; /* skip "+++ " */ + if (!p_value_known) { + int p, q; + p = guess_p_value(first); + q = guess_p_value(second); + if (p < 0) p = q; + if (0 <= p && p == q) { + p_value = p; + p_value_known = 1; + } + } + if (is_dev_null(first)) { + patch->is_new = 1; + patch->is_delete = 0; + name = find_name_traditional(second, NULL, p_value); + patch->new_name = name; + } else if (is_dev_null(second)) { + patch->is_new = 0; + patch->is_delete = 1; + name = find_name_traditional(first, NULL, p_value); + patch->old_name = name; + } else { + char *first_name; + first_name = find_name_traditional(first, NULL, p_value); + name = find_name_traditional(second, first_name, p_value); + free(first_name); + if (has_epoch_timestamp(first)) { + patch->is_new = 1; + patch->is_delete = 0; + patch->new_name = name; + } else if (has_epoch_timestamp(second)) { + patch->is_new = 0; + patch->is_delete = 1; + patch->old_name = name; + } else { + patch->old_name = name; + patch->new_name = xstrdup(name); + } + } + if (!name) + die(_("unable to find filename in patch at line %d"), linenr); +} + +static int gitdiff_hdrend(const char *line, struct patch *patch) +{ + return -1; +} + +/* + * We're anal about diff header consistency, to make + * sure that we don't end up having strange ambiguous + * patches floating around. + * + * As a result, gitdiff_{old|new}name() will check + * their names against any previous information, just + * to make sure.. + */ +#define DIFF_OLD_NAME 0 +#define DIFF_NEW_NAME 1 + +static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, int side) +{ + if (!orig_name && !isnull) + return find_name(line, NULL, p_value, TERM_TAB); + + if (orig_name) { + int len; + const char *name; + char *another; + name = orig_name; + len = strlen(name); + if (isnull) + die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), name, linenr); + another = find_name(line, NULL, p_value, TERM_TAB); + if (!another || memcmp(another, name, len + 1)) + die((side == DIFF_NEW_NAME) ? + _("git apply: bad git-diff - inconsistent new filename on line %d") : + _("git apply: bad git-diff - inconsistent old filename on line %d"), linenr); + free(another); + return orig_name; + } + else { + /* expect "/dev/null" */ + if (memcmp("/dev/null", line, 9) || line[9] != '\n') + die(_("git apply: bad git-diff - expected /dev/null on line %d"), linenr); + return NULL; + } +} + +static int gitdiff_oldname(const char *line, struct patch *patch) +{ + char *orig = patch->old_name; + patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, + DIFF_OLD_NAME); + if (orig != patch->old_name) + free(orig); + return 0; +} + +static int gitdiff_newname(const char *line, struct patch *patch) +{ + char *orig = patch->new_name; + patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, + DIFF_NEW_NAME); + if (orig != patch->new_name) + free(orig); + return 0; +} + +static int gitdiff_oldmode(const char *line, struct patch *patch) +{ + patch->old_mode = strtoul(line, NULL, 8); + return 0; +} + +static int gitdiff_newmode(const char *line, struct patch *patch) +{ + patch->new_mode = strtoul(line, NULL, 8); + return 0; +} + +static int gitdiff_delete(const char *line, struct patch *patch) +{ + patch->is_delete = 1; + free(patch->old_name); + patch->old_name = null_strdup(patch->def_name); + return gitdiff_oldmode(line, patch); +} + +static int gitdiff_newfile(const char *line, struct patch *patch) +{ + patch->is_new = 1; + free(patch->new_name); + patch->new_name = null_strdup(patch->def_name); + return gitdiff_newmode(line, patch); +} + +static int gitdiff_copysrc(const char *line, struct patch *patch) +{ + patch->is_copy = 1; + free(patch->old_name); + patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); + return 0; +} + +static int gitdiff_copydst(const char *line, struct patch *patch) +{ + patch->is_copy = 1; + free(patch->new_name); + patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); + return 0; +} + +static int gitdiff_renamesrc(const char *line, struct patch *patch) +{ + patch->is_rename = 1; + free(patch->old_name); + patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); + return 0; +} + +static int gitdiff_renamedst(const char *line, struct patch *patch) +{ + patch->is_rename = 1; + free(patch->new_name); + patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); + return 0; +} + +static int gitdiff_similarity(const char *line, struct patch *patch) +{ + if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) + patch->score = 0; + return 0; +} + +static int gitdiff_dissimilarity(const char *line, struct patch *patch) +{ + if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) + patch->score = 0; + return 0; +} + +static int gitdiff_index(const char *line, struct patch *patch) +{ + /* + * index line is N hexadecimal, "..", N hexadecimal, + * and optional space with octal mode. + */ + const char *ptr, *eol; + int len; + + ptr = strchr(line, '.'); + if (!ptr || ptr[1] != '.' || 40 < ptr - line) + return 0; + len = ptr - line; + memcpy(patch->old_sha1_prefix, line, len); + patch->old_sha1_prefix[len] = 0; + + line = ptr + 2; + ptr = strchr(line, ' '); + eol = strchr(line, '\n'); + + if (!ptr || eol < ptr) + ptr = eol; + len = ptr - line; + + if (40 < len) + return 0; + memcpy(patch->new_sha1_prefix, line, len); + patch->new_sha1_prefix[len] = 0; + if (*ptr == ' ') + patch->old_mode = strtoul(ptr+1, NULL, 8); + return 0; +} + +/* + * This is normal for a diff that doesn't change anything: we'll fall through + * into the next diff. Tell the parser to break out. + */ +static int gitdiff_unrecognized(const char *line, struct patch *patch) +{ + return -1; +} + +/* + * Skip p_value leading components from "line"; as we do not accept + * absolute paths, return NULL in that case. + */ +static const char *skip_tree_prefix(const char *line, int llen) +{ + int nslash; + int i; + + if (!p_value) + return (llen && line[0] == '/') ? NULL : line; + + nslash = p_value; + for (i = 0; i < llen; i++) { + int ch = line[i]; + if (ch == '/' && --nslash <= 0) + return (i == 0) ? NULL : &line[i + 1]; + } + return NULL; +} + +/* + * This is to extract the same name that appears on "diff --git" + * line. We do not find and return anything if it is a rename + * patch, and it is OK because we will find the name elsewhere. + * We need to reliably find name only when it is mode-change only, + * creation or deletion of an empty file. In any of these cases, + * both sides are the same name under a/ and b/ respectively. + */ +static char *git_header_name(const char *line, int llen) +{ + const char *name; + const char *second = NULL; + size_t len, line_len; + + line += strlen("diff --git "); + llen -= strlen("diff --git "); + + if (*line == '"') { + const char *cp; + struct strbuf first = STRBUF_INIT; + struct strbuf sp = STRBUF_INIT; + + if (unquote_c_style(&first, line, &second)) + goto free_and_fail1; + + /* strip the a/b prefix including trailing slash */ + cp = skip_tree_prefix(first.buf, first.len); + if (!cp) + goto free_and_fail1; + strbuf_remove(&first, 0, cp - first.buf); + + /* + * second points at one past closing dq of name. + * find the second name. + */ + while ((second < line + llen) && isspace(*second)) + second++; + + if (line + llen <= second) + goto free_and_fail1; + if (*second == '"') { + if (unquote_c_style(&sp, second, NULL)) + goto free_and_fail1; + cp = skip_tree_prefix(sp.buf, sp.len); + if (!cp) + goto free_and_fail1; + /* They must match, otherwise ignore */ + if (strcmp(cp, first.buf)) + goto free_and_fail1; + strbuf_release(&sp); + return strbuf_detach(&first, NULL); + } + + /* unquoted second */ + cp = skip_tree_prefix(second, line + llen - second); + if (!cp) + goto free_and_fail1; + if (line + llen - cp != first.len || + memcmp(first.buf, cp, first.len)) + goto free_and_fail1; + return strbuf_detach(&first, NULL); + + free_and_fail1: + strbuf_release(&first); + strbuf_release(&sp); + return NULL; + } + + /* unquoted first name */ + name = skip_tree_prefix(line, llen); + if (!name) + return NULL; + + /* + * since the first name is unquoted, a dq if exists must be + * the beginning of the second name. + */ + for (second = name; second < line + llen; second++) { + if (*second == '"') { + struct strbuf sp = STRBUF_INIT; + const char *np; + + if (unquote_c_style(&sp, second, NULL)) + goto free_and_fail2; + + np = skip_tree_prefix(sp.buf, sp.len); + if (!np) + goto free_and_fail2; + + len = sp.buf + sp.len - np; + if (len < second - name && + !strncmp(np, name, len) && + isspace(name[len])) { + /* Good */ + strbuf_remove(&sp, 0, np - sp.buf); + return strbuf_detach(&sp, NULL); + } + + free_and_fail2: + strbuf_release(&sp); + return NULL; + } + } + + /* + * Accept a name only if it shows up twice, exactly the same + * form. + */ + second = strchr(name, '\n'); + if (!second) + return NULL; + line_len = second - name; + for (len = 0 ; ; len++) { + switch (name[len]) { + default: + continue; + case '\n': + return NULL; + case '\t': case ' ': + /* + * Is this the separator between the preimage + * and the postimage pathname? Again, we are + * only interested in the case where there is + * no rename, as this is only to set def_name + * and a rename patch has the names elsewhere + * in an unambiguous form. + */ + if (!name[len + 1]) + return NULL; /* no postimage name */ + second = skip_tree_prefix(name + len + 1, + line_len - (len + 1)); + if (!second) + return NULL; + /* + * Does len bytes starting at "name" and "second" + * (that are separated by one HT or SP we just + * found) exactly match? + */ + if (second[len] == '\n' && !strncmp(name, second, len)) + return xmemdupz(name, len); + } + } +} + +/* Verify that we recognize the lines following a git header */ +static int parse_git_header(const char *line, int len, unsigned int size, struct patch *patch) +{ + unsigned long offset; + + /* A git diff has explicit new/delete information, so we don't guess */ + patch->is_new = 0; + patch->is_delete = 0; + + /* + * Some things may not have the old name in the + * rest of the headers anywhere (pure mode changes, + * or removing or adding empty files), so we get + * the default name from the header. + */ + patch->def_name = git_header_name(line, len); + if (patch->def_name && root) { + char *s = xmalloc(root_len + strlen(patch->def_name) + 1); + strcpy(s, root); + strcpy(s + root_len, patch->def_name); + free(patch->def_name); + patch->def_name = s; + } + + line += len; + size -= len; + linenr++; + for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) { + static const struct opentry { + const char *str; + int (*fn)(const char *, struct patch *); + } optable[] = { + { "@@ -", gitdiff_hdrend }, + { "--- ", gitdiff_oldname }, + { "+++ ", gitdiff_newname }, + { "old mode ", gitdiff_oldmode }, + { "new mode ", gitdiff_newmode }, + { "deleted file mode ", gitdiff_delete }, + { "new file mode ", gitdiff_newfile }, + { "copy from ", gitdiff_copysrc }, + { "copy to ", gitdiff_copydst }, + { "rename old ", gitdiff_renamesrc }, + { "rename new ", gitdiff_renamedst }, + { "rename from ", gitdiff_renamesrc }, + { "rename to ", gitdiff_renamedst }, + { "similarity index ", gitdiff_similarity }, + { "dissimilarity index ", gitdiff_dissimilarity }, + { "index ", gitdiff_index }, + { "", gitdiff_unrecognized }, + }; + int i; + + len = linelen(line, size); + if (!len || line[len-1] != '\n') + break; + for (i = 0; i < ARRAY_SIZE(optable); i++) { + const struct opentry *p = optable + i; + int oplen = strlen(p->str); + if (len < oplen || memcmp(p->str, line, oplen)) + continue; + if (p->fn(line + oplen, patch) < 0) + return offset; + break; + } + } + + return offset; +} + +static int parse_num(const char *line, unsigned long *p) +{ + char *ptr; + + if (!isdigit(*line)) + return 0; + *p = strtoul(line, &ptr, 10); + return ptr - line; +} + +static int parse_range(const char *line, int len, int offset, const char *expect, + unsigned long *p1, unsigned long *p2) +{ + int digits, ex; + + if (offset < 0 || offset >= len) + return -1; + line += offset; + len -= offset; + + digits = parse_num(line, p1); + if (!digits) + return -1; + + offset += digits; + line += digits; + len -= digits; + + *p2 = 1; + if (*line == ',') { + digits = parse_num(line+1, p2); + if (!digits) + return -1; + + offset += digits+1; + line += digits+1; + len -= digits+1; + } + + ex = strlen(expect); + if (ex > len) + return -1; + if (memcmp(line, expect, ex)) + return -1; + + return offset + ex; +} + +static void recount_diff(const char *line, int size, struct fragment *fragment) +{ + int oldlines = 0, newlines = 0, ret = 0; + + if (size < 1) { + warning("recount: ignore empty hunk"); + return; + } + + for (;;) { + int len = linelen(line, size); + size -= len; + line += len; + + if (size < 1) + break; + + switch (*line) { + case ' ': case '\n': + newlines++; + /* fall through */ + case '-': + oldlines++; + continue; + case '+': + newlines++; + continue; + case '\\': + continue; + case '@': + ret = size < 3 || prefixcmp(line, "@@ "); + break; + case 'd': + ret = size < 5 || prefixcmp(line, "diff "); + break; + default: + ret = -1; + break; + } + if (ret) { + warning(_("recount: unexpected line: %.*s"), + (int)linelen(line, size), line); + return; + } + break; + } + fragment->oldlines = oldlines; + fragment->newlines = newlines; +} + +/* + * Parse a unified diff fragment header of the + * form "@@ -a,b +c,d @@" + */ +static int parse_fragment_header(const char *line, int len, struct fragment *fragment) +{ + int offset; + + if (!len || line[len-1] != '\n') + return -1; + + /* Figure out the number of lines in a fragment */ + offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines); + offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines); + + return offset; +} + +static int find_header(const char *line, unsigned long size, int *hdrsize, struct patch *patch) +{ + unsigned long offset, len; + + patch->is_toplevel_relative = 0; + patch->is_rename = patch->is_copy = 0; + patch->is_new = patch->is_delete = -1; + patch->old_mode = patch->new_mode = 0; + patch->old_name = patch->new_name = NULL; + for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) { + unsigned long nextlen; + + len = linelen(line, size); + if (!len) + break; + + /* Testing this early allows us to take a few shortcuts.. */ + if (len < 6) + continue; + + /* + * Make sure we don't find any unconnected patch fragments. + * That's a sign that we didn't find a header, and that a + * patch has become corrupted/broken up. + */ + if (!memcmp("@@ -", line, 4)) { + struct fragment dummy; + if (parse_fragment_header(line, len, &dummy) < 0) + continue; + die(_("patch fragment without header at line %d: %.*s"), + linenr, (int)len-1, line); + } + + if (size < len + 6) + break; + + /* + * Git patch? It might not have a real patch, just a rename + * or mode change, so we handle that specially + */ + if (!memcmp("diff --git ", line, 11)) { + int git_hdr_len = parse_git_header(line, len, size, patch); + if (git_hdr_len <= len) + continue; + if (!patch->old_name && !patch->new_name) { + if (!patch->def_name) + die(Q_("git diff header lacks filename information when removing " + "%d leading pathname component (line %d)", + "git diff header lacks filename information when removing " + "%d leading pathname components (line %d)", + p_value), + p_value, linenr); + patch->old_name = xstrdup(patch->def_name); + patch->new_name = xstrdup(patch->def_name); + } + if (!patch->is_delete && !patch->new_name) + die("git diff header lacks filename information " + "(line %d)", linenr); + patch->is_toplevel_relative = 1; + *hdrsize = git_hdr_len; + return offset; + } + + /* --- followed by +++ ? */ + if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4)) + continue; + + /* + * We only accept unified patches, so we want it to + * at least have "@@ -a,b +c,d @@\n", which is 14 chars + * minimum ("@@ -0,0 +1 @@\n" is the shortest). + */ + nextlen = linelen(line + len, size - len); + if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4)) + continue; + + /* Ok, we'll consider it a patch */ + parse_traditional_patch(line, line+len, patch); + *hdrsize = len + nextlen; + linenr += 2; + return offset; + } + return -1; +} + +static void record_ws_error(unsigned result, const char *line, int len, int linenr) +{ + char *err; + + if (!result) + return; + + whitespace_error++; + if (squelch_whitespace_errors && + squelch_whitespace_errors < whitespace_error) + return; + + err = whitespace_error_string(result); + fprintf(stderr, "%s:%d: %s.\n%.*s\n", + patch_input_file, linenr, err, len, line); + free(err); +} + +static void check_whitespace(const char *line, int len, unsigned ws_rule) +{ + unsigned result = ws_check(line + 1, len - 1, ws_rule); + + record_ws_error(result, line + 1, len - 2, linenr); +} + +/* + * Parse a unified diff. Note that this really needs to parse each + * fragment separately, since the only way to know the difference + * between a "---" that is part of a patch, and a "---" that starts + * the next patch is to look at the line counts.. + */ +static int parse_fragment(const char *line, unsigned long size, + struct patch *patch, struct fragment *fragment) +{ + int added, deleted; + int len = linelen(line, size), offset; + unsigned long oldlines, newlines; + unsigned long leading, trailing; + + offset = parse_fragment_header(line, len, fragment); + if (offset < 0) + return -1; + if (offset > 0 && patch->recount) + recount_diff(line + offset, size - offset, fragment); + oldlines = fragment->oldlines; + newlines = fragment->newlines; + leading = 0; + trailing = 0; + + /* Parse the thing.. */ + line += len; + size -= len; + linenr++; + added = deleted = 0; + for (offset = len; + 0 < size; + offset += len, size -= len, line += len, linenr++) { + if (!oldlines && !newlines) + break; + len = linelen(line, size); + if (!len || line[len-1] != '\n') + return -1; + switch (*line) { + default: + return -1; + case '\n': /* newer GNU diff, an empty context line */ + case ' ': + oldlines--; + newlines--; + if (!deleted && !added) + leading++; + trailing++; + break; + case '-': + if (apply_in_reverse && + ws_error_action != nowarn_ws_error) + check_whitespace(line, len, patch->ws_rule); + deleted++; + oldlines--; + trailing = 0; + break; + case '+': + if (!apply_in_reverse && + ws_error_action != nowarn_ws_error) + check_whitespace(line, len, patch->ws_rule); + added++; + newlines--; + trailing = 0; + break; + + /* + * We allow "\ No newline at end of file". Depending + * on locale settings when the patch was produced we + * don't know what this line looks like. The only + * thing we do know is that it begins with "\ ". + * Checking for 12 is just for sanity check -- any + * l10n of "\ No newline..." is at least that long. + */ + case '\\': + if (len < 12 || memcmp(line, "\\ ", 2)) + return -1; + break; + } + } + if (oldlines || newlines) + return -1; + fragment->leading = leading; + fragment->trailing = trailing; + + /* + * If a fragment ends with an incomplete line, we failed to include + * it in the above loop because we hit oldlines == newlines == 0 + * before seeing it. + */ + if (12 < size && !memcmp(line, "\\ ", 2)) + offset += linelen(line, size); + + patch->lines_added += added; + patch->lines_deleted += deleted; + + if (0 < patch->is_new && oldlines) + return error(_("new file depends on old contents")); + if (0 < patch->is_delete && newlines) + return error(_("deleted file still has contents")); + return offset; +} + +/* + * We have seen "diff --git a/... b/..." header (or a traditional patch + * header). Read hunks that belong to this patch into fragments and hang + * them to the given patch structure. + * + * The (fragment->patch, fragment->size) pair points into the memory given + * by the caller, not a copy, when we return. + */ +static int parse_single_patch(const char *line, unsigned long size, struct patch *patch) +{ + unsigned long offset = 0; + unsigned long oldlines = 0, newlines = 0, context = 0; + struct fragment **fragp = &patch->fragments; + + while (size > 4 && !memcmp(line, "@@ -", 4)) { + struct fragment *fragment; + int len; + + fragment = xcalloc(1, sizeof(*fragment)); + fragment->linenr = linenr; + len = parse_fragment(line, size, patch, fragment); + if (len <= 0) + die(_("corrupt patch at line %d"), linenr); + fragment->patch = line; + fragment->size = len; + oldlines += fragment->oldlines; + newlines += fragment->newlines; + context += fragment->leading + fragment->trailing; + + *fragp = fragment; + fragp = &fragment->next; + + offset += len; + line += len; + size -= len; + } + + /* + * If something was removed (i.e. we have old-lines) it cannot + * be creation, and if something was added it cannot be + * deletion. However, the reverse is not true; --unified=0 + * patches that only add are not necessarily creation even + * though they do not have any old lines, and ones that only + * delete are not necessarily deletion. + * + * Unfortunately, a real creation/deletion patch do _not_ have + * any context line by definition, so we cannot safely tell it + * apart with --unified=0 insanity. At least if the patch has + * more than one hunk it is not creation or deletion. + */ + if (patch->is_new < 0 && + (oldlines || (patch->fragments && patch->fragments->next))) + patch->is_new = 0; + if (patch->is_delete < 0 && + (newlines || (patch->fragments && patch->fragments->next))) + patch->is_delete = 0; + + if (0 < patch->is_new && oldlines) + die(_("new file %s depends on old contents"), patch->new_name); + if (0 < patch->is_delete && newlines) + die(_("deleted file %s still has contents"), patch->old_name); + if (!patch->is_delete && !newlines && context) + fprintf_ln(stderr, + _("** warning: " + "file %s becomes empty but is not deleted"), + patch->new_name); + + return offset; +} + +static inline int metadata_changes(struct patch *patch) +{ + return patch->is_rename > 0 || + patch->is_copy > 0 || + patch->is_new > 0 || + patch->is_delete || + (patch->old_mode && patch->new_mode && + patch->old_mode != patch->new_mode); +} + +static char *inflate_it(const void *data, unsigned long size, + unsigned long inflated_size) +{ + git_zstream stream; + void *out; + int st; + + memset(&stream, 0, sizeof(stream)); + + stream.next_in = (unsigned char *)data; + stream.avail_in = size; + stream.next_out = out = xmalloc(inflated_size); + stream.avail_out = inflated_size; + git_inflate_init(&stream); + st = git_inflate(&stream, Z_FINISH); + git_inflate_end(&stream); + if ((st != Z_STREAM_END) || stream.total_out != inflated_size) { + free(out); + return NULL; + } + return out; +} + +/* + * Read a binary hunk and return a new fragment; fragment->patch + * points at an allocated memory that the caller must free, so + * it is marked as "->free_patch = 1". + */ +static struct fragment *parse_binary_hunk(char **buf_p, + unsigned long *sz_p, + int *status_p, + int *used_p) +{ + /* + * Expect a line that begins with binary patch method ("literal" + * or "delta"), followed by the length of data before deflating. + * a sequence of 'length-byte' followed by base-85 encoded data + * should follow, terminated by a newline. + * + * Each 5-byte sequence of base-85 encodes up to 4 bytes, + * and we would limit the patch line to 66 characters, + * so one line can fit up to 13 groups that would decode + * to 52 bytes max. The length byte 'A'-'Z' corresponds + * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes. + */ + int llen, used; + unsigned long size = *sz_p; + char *buffer = *buf_p; + int patch_method; + unsigned long origlen; + char *data = NULL; + int hunk_size = 0; + struct fragment *frag; + + llen = linelen(buffer, size); + used = llen; + + *status_p = 0; + + if (!prefixcmp(buffer, "delta ")) { + patch_method = BINARY_DELTA_DEFLATED; + origlen = strtoul(buffer + 6, NULL, 10); + } + else if (!prefixcmp(buffer, "literal ")) { + patch_method = BINARY_LITERAL_DEFLATED; + origlen = strtoul(buffer + 8, NULL, 10); + } + else + return NULL; + + linenr++; + buffer += llen; + while (1) { + int byte_length, max_byte_length, newsize; + llen = linelen(buffer, size); + used += llen; + linenr++; + if (llen == 1) { + /* consume the blank line */ + buffer++; + size--; + break; + } + /* + * Minimum line is "A00000\n" which is 7-byte long, + * and the line length must be multiple of 5 plus 2. + */ + if ((llen < 7) || (llen-2) % 5) + goto corrupt; + max_byte_length = (llen - 2) / 5 * 4; + byte_length = *buffer; + if ('A' <= byte_length && byte_length <= 'Z') + byte_length = byte_length - 'A' + 1; + else if ('a' <= byte_length && byte_length <= 'z') + byte_length = byte_length - 'a' + 27; + else + goto corrupt; + /* if the input length was not multiple of 4, we would + * have filler at the end but the filler should never + * exceed 3 bytes + */ + if (max_byte_length < byte_length || + byte_length <= max_byte_length - 4) + goto corrupt; + newsize = hunk_size + byte_length; + data = xrealloc(data, newsize); + if (decode_85(data + hunk_size, buffer + 1, byte_length)) + goto corrupt; + hunk_size = newsize; + buffer += llen; + size -= llen; + } + + frag = xcalloc(1, sizeof(*frag)); + frag->patch = inflate_it(data, hunk_size, origlen); + frag->free_patch = 1; + if (!frag->patch) + goto corrupt; + free(data); + frag->size = origlen; + *buf_p = buffer; + *sz_p = size; + *used_p = used; + frag->binary_patch_method = patch_method; + return frag; + + corrupt: + free(data); + *status_p = -1; + error(_("corrupt binary patch at line %d: %.*s"), + linenr-1, llen-1, buffer); + return NULL; +} + +static int parse_binary(char *buffer, unsigned long size, struct patch *patch) +{ + /* + * We have read "GIT binary patch\n"; what follows is a line + * that says the patch method (currently, either "literal" or + * "delta") and the length of data before deflating; a + * sequence of 'length-byte' followed by base-85 encoded data + * follows. + * + * When a binary patch is reversible, there is another binary + * hunk in the same format, starting with patch method (either + * "literal" or "delta") with the length of data, and a sequence + * of length-byte + base-85 encoded data, terminated with another + * empty line. This data, when applied to the postimage, produces + * the preimage. + */ + struct fragment *forward; + struct fragment *reverse; + int status; + int used, used_1; + + forward = parse_binary_hunk(&buffer, &size, &status, &used); + if (!forward && !status) + /* there has to be one hunk (forward hunk) */ + return error(_("unrecognized binary patch at line %d"), linenr-1); + if (status) + /* otherwise we already gave an error message */ + return status; + + reverse = parse_binary_hunk(&buffer, &size, &status, &used_1); + if (reverse) + used += used_1; + else if (status) { + /* + * Not having reverse hunk is not an error, but having + * a corrupt reverse hunk is. + */ + free((void*) forward->patch); + free(forward); + return status; + } + forward->next = reverse; + patch->fragments = forward; + patch->is_binary = 1; + return used; +} + +/* + * Read the patch text in "buffer" taht extends for "size" bytes; stop + * reading after seeing a single patch (i.e. changes to a single file). + * Create fragments (i.e. patch hunks) and hang them to the given patch. + * Return the number of bytes consumed, so that the caller can call us + * again for the next patch. + */ +static int parse_chunk(char *buffer, unsigned long size, struct patch *patch) +{ + int hdrsize, patchsize; + int offset = find_header(buffer, size, &hdrsize, patch); + + if (offset < 0) + return offset; + + patch->ws_rule = whitespace_rule(patch->new_name + ? patch->new_name + : patch->old_name); + + patchsize = parse_single_patch(buffer + offset + hdrsize, + size - offset - hdrsize, patch); + + if (!patchsize) { + static const char *binhdr[] = { + "Binary files ", + "Files ", + NULL, + }; + static const char git_binary[] = "GIT binary patch\n"; + int i; + int hd = hdrsize + offset; + unsigned long llen = linelen(buffer + hd, size - hd); + + if (llen == sizeof(git_binary) - 1 && + !memcmp(git_binary, buffer + hd, llen)) { + int used; + linenr++; + used = parse_binary(buffer + hd + llen, + size - hd - llen, patch); + if (used) + patchsize = used + llen; + else + patchsize = 0; + } + else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) { + for (i = 0; binhdr[i]; i++) { + int len = strlen(binhdr[i]); + if (len < size - hd && + !memcmp(binhdr[i], buffer + hd, len)) { + linenr++; + patch->is_binary = 1; + patchsize = llen; + break; + } + } + } + + /* Empty patch cannot be applied if it is a text patch + * without metadata change. A binary patch appears + * empty to us here. + */ + if ((apply || check) && + (!patch->is_binary && !metadata_changes(patch))) + die(_("patch with only garbage at line %d"), linenr); + } + + return offset + hdrsize + patchsize; +} + +#define swap(a,b) myswap((a),(b),sizeof(a)) + +#define myswap(a, b, size) do { \ + unsigned char mytmp[size]; \ + memcpy(mytmp, &a, size); \ + memcpy(&a, &b, size); \ + memcpy(&b, mytmp, size); \ +} while (0) + +static void reverse_patches(struct patch *p) +{ + for (; p; p = p->next) { + struct fragment *frag = p->fragments; + + swap(p->new_name, p->old_name); + swap(p->new_mode, p->old_mode); + swap(p->is_new, p->is_delete); + swap(p->lines_added, p->lines_deleted); + swap(p->old_sha1_prefix, p->new_sha1_prefix); + + for (; frag; frag = frag->next) { + swap(frag->newpos, frag->oldpos); + swap(frag->newlines, frag->oldlines); + } + } +} + +static const char pluses[] = +"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; +static const char minuses[]= +"----------------------------------------------------------------------"; + +static void show_stats(struct patch *patch) +{ + struct strbuf qname = STRBUF_INIT; + char *cp = patch->new_name ? patch->new_name : patch->old_name; + int max, add, del; + + quote_c_style(cp, &qname, NULL, 0); + + /* + * "scale" the filename + */ + max = max_len; + if (max > 50) + max = 50; + + if (qname.len > max) { + cp = strchr(qname.buf + qname.len + 3 - max, '/'); + if (!cp) + cp = qname.buf + qname.len + 3 - max; + strbuf_splice(&qname, 0, cp - qname.buf, "...", 3); + } + + if (patch->is_binary) { + printf(" %-*s | Bin\n", max, qname.buf); + strbuf_release(&qname); + return; + } + + printf(" %-*s |", max, qname.buf); + strbuf_release(&qname); + + /* + * scale the add/delete + */ + max = max + max_change > 70 ? 70 - max : max_change; + add = patch->lines_added; + del = patch->lines_deleted; + + if (max_change > 0) { + int total = ((add + del) * max + max_change / 2) / max_change; + add = (add * max + max_change / 2) / max_change; + del = total - add; + } + printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted, + add, pluses, del, minuses); +} + +static int read_old_data(struct stat *st, const char *path, struct strbuf *buf) +{ + switch (st->st_mode & S_IFMT) { + case S_IFLNK: + if (strbuf_readlink(buf, path, st->st_size) < 0) + return error(_("unable to read symlink %s"), path); + return 0; + case S_IFREG: + if (strbuf_read_file(buf, path, st->st_size) != st->st_size) + return error(_("unable to open or read %s"), path); + convert_to_git(path, buf->buf, buf->len, buf, 0); + return 0; + default: + return -1; + } +} + +/* + * Update the preimage, and the common lines in postimage, + * from buffer buf of length len. If postlen is 0 the postimage + * is updated in place, otherwise it's updated on a new buffer + * of length postlen + */ + +static void update_pre_post_images(struct image *preimage, + struct image *postimage, + char *buf, + size_t len, size_t postlen) +{ + int i, ctx, reduced; + char *new, *old, *fixed; + struct image fixed_preimage; + + /* + * Update the preimage with whitespace fixes. Note that we + * are not losing preimage->buf -- apply_one_fragment() will + * free "oldlines". + */ + prepare_image(&fixed_preimage, buf, len, 1); + assert(postlen + ? fixed_preimage.nr == preimage->nr + : fixed_preimage.nr <= preimage->nr); + for (i = 0; i < fixed_preimage.nr; i++) + fixed_preimage.line[i].flag = preimage->line[i].flag; + free(preimage->line_allocated); + *preimage = fixed_preimage; + + /* + * Adjust the common context lines in postimage. This can be + * done in-place when we are just doing whitespace fixing, + * which does not make the string grow, but needs a new buffer + * when ignoring whitespace causes the update, since in this case + * we could have e.g. tabs converted to multiple spaces. + * We trust the caller to tell us if the update can be done + * in place (postlen==0) or not. + */ + old = postimage->buf; + if (postlen) + new = postimage->buf = xmalloc(postlen); + else + new = old; + fixed = preimage->buf; + + for (i = reduced = ctx = 0; i < postimage->nr; i++) { + size_t len = postimage->line[i].len; + if (!(postimage->line[i].flag & LINE_COMMON)) { + /* an added line -- no counterparts in preimage */ + memmove(new, old, len); + old += len; + new += len; + continue; + } + + /* a common context -- skip it in the original postimage */ + old += len; + + /* and find the corresponding one in the fixed preimage */ + while (ctx < preimage->nr && + !(preimage->line[ctx].flag & LINE_COMMON)) { + fixed += preimage->line[ctx].len; + ctx++; + } + + /* + * preimage is expected to run out, if the caller + * fixed addition of trailing blank lines. + */ + if (preimage->nr <= ctx) { + reduced++; + continue; + } + + /* and copy it in, while fixing the line length */ + len = preimage->line[ctx].len; + memcpy(new, fixed, len); + new += len; + fixed += len; + postimage->line[i].len = len; + ctx++; + } + + /* Fix the length of the whole thing */ + postimage->len = new - postimage->buf; + postimage->nr -= reduced; +} + +static int match_fragment(struct image *img, + struct image *preimage, + struct image *postimage, + unsigned long try, + int try_lno, + unsigned ws_rule, + int match_beginning, int match_end) +{ + int i; + char *fixed_buf, *buf, *orig, *target; + struct strbuf fixed; + size_t fixed_len; + int preimage_limit; + + if (preimage->nr + try_lno <= img->nr) { + /* + * The hunk falls within the boundaries of img. + */ + preimage_limit = preimage->nr; + if (match_end && (preimage->nr + try_lno != img->nr)) + return 0; + } else if (ws_error_action == correct_ws_error && + (ws_rule & WS_BLANK_AT_EOF)) { + /* + * This hunk extends beyond the end of img, and we are + * removing blank lines at the end of the file. This + * many lines from the beginning of the preimage must + * match with img, and the remainder of the preimage + * must be blank. + */ + preimage_limit = img->nr - try_lno; + } else { + /* + * The hunk extends beyond the end of the img and + * we are not removing blanks at the end, so we + * should reject the hunk at this position. + */ + return 0; + } + + if (match_beginning && try_lno) + return 0; + + /* Quick hash check */ + for (i = 0; i < preimage_limit; i++) + if ((img->line[try_lno + i].flag & LINE_PATCHED) || + (preimage->line[i].hash != img->line[try_lno + i].hash)) + return 0; + + if (preimage_limit == preimage->nr) { + /* + * Do we have an exact match? If we were told to match + * at the end, size must be exactly at try+fragsize, + * otherwise try+fragsize must be still within the preimage, + * and either case, the old piece should match the preimage + * exactly. + */ + if ((match_end + ? (try + preimage->len == img->len) + : (try + preimage->len <= img->len)) && + !memcmp(img->buf + try, preimage->buf, preimage->len)) + return 1; + } else { + /* + * The preimage extends beyond the end of img, so + * there cannot be an exact match. + * + * There must be one non-blank context line that match + * a line before the end of img. + */ + char *buf_end; + + buf = preimage->buf; + buf_end = buf; + for (i = 0; i < preimage_limit; i++) + buf_end += preimage->line[i].len; + + for ( ; buf < buf_end; buf++) + if (!isspace(*buf)) + break; + if (buf == buf_end) + return 0; + } + + /* + * No exact match. If we are ignoring whitespace, run a line-by-line + * fuzzy matching. We collect all the line length information because + * we need it to adjust whitespace if we match. + */ + if (ws_ignore_action == ignore_ws_change) { + size_t imgoff = 0; + size_t preoff = 0; + size_t postlen = postimage->len; + size_t extra_chars; + char *preimage_eof; + char *preimage_end; + for (i = 0; i < preimage_limit; i++) { + size_t prelen = preimage->line[i].len; + size_t imglen = img->line[try_lno+i].len; + + if (!fuzzy_matchlines(img->buf + try + imgoff, imglen, + preimage->buf + preoff, prelen)) + return 0; + if (preimage->line[i].flag & LINE_COMMON) + postlen += imglen - prelen; + imgoff += imglen; + preoff += prelen; + } + + /* + * Ok, the preimage matches with whitespace fuzz. + * + * imgoff now holds the true length of the target that + * matches the preimage before the end of the file. + * + * Count the number of characters in the preimage that fall + * beyond the end of the file and make sure that all of them + * are whitespace characters. (This can only happen if + * we are removing blank lines at the end of the file.) + */ + buf = preimage_eof = preimage->buf + preoff; + for ( ; i < preimage->nr; i++) + preoff += preimage->line[i].len; + preimage_end = preimage->buf + preoff; + for ( ; buf < preimage_end; buf++) + if (!isspace(*buf)) + return 0; + + /* + * Update the preimage and the common postimage context + * lines to use the same whitespace as the target. + * If whitespace is missing in the target (i.e. + * if the preimage extends beyond the end of the file), + * use the whitespace from the preimage. + */ + extra_chars = preimage_end - preimage_eof; + strbuf_init(&fixed, imgoff + extra_chars); + strbuf_add(&fixed, img->buf + try, imgoff); + strbuf_add(&fixed, preimage_eof, extra_chars); + fixed_buf = strbuf_detach(&fixed, &fixed_len); + update_pre_post_images(preimage, postimage, + fixed_buf, fixed_len, postlen); + return 1; + } + + if (ws_error_action != correct_ws_error) + return 0; + + /* + * The hunk does not apply byte-by-byte, but the hash says + * it might with whitespace fuzz. We haven't been asked to + * ignore whitespace, we were asked to correct whitespace + * errors, so let's try matching after whitespace correction. + * + * The preimage may extend beyond the end of the file, + * but in this loop we will only handle the part of the + * preimage that falls within the file. + */ + strbuf_init(&fixed, preimage->len + 1); + orig = preimage->buf; + target = img->buf + try; + for (i = 0; i < preimage_limit; i++) { + size_t oldlen = preimage->line[i].len; + size_t tgtlen = img->line[try_lno + i].len; + size_t fixstart = fixed.len; + struct strbuf tgtfix; + int match; + + /* Try fixing the line in the preimage */ + ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL); + + /* Try fixing the line in the target */ + strbuf_init(&tgtfix, tgtlen); + ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL); + + /* + * If they match, either the preimage was based on + * a version before our tree fixed whitespace breakage, + * or we are lacking a whitespace-fix patch the tree + * the preimage was based on already had (i.e. target + * has whitespace breakage, the preimage doesn't). + * In either case, we are fixing the whitespace breakages + * so we might as well take the fix together with their + * real change. + */ + match = (tgtfix.len == fixed.len - fixstart && + !memcmp(tgtfix.buf, fixed.buf + fixstart, + fixed.len - fixstart)); + + strbuf_release(&tgtfix); + if (!match) + goto unmatch_exit; + + orig += oldlen; + target += tgtlen; + } + + + /* + * Now handle the lines in the preimage that falls beyond the + * end of the file (if any). They will only match if they are + * empty or only contain whitespace (if WS_BLANK_AT_EOL is + * false). + */ + for ( ; i < preimage->nr; i++) { + size_t fixstart = fixed.len; /* start of the fixed preimage */ + size_t oldlen = preimage->line[i].len; + int j; + + /* Try fixing the line in the preimage */ + ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL); + + for (j = fixstart; j < fixed.len; j++) + if (!isspace(fixed.buf[j])) + goto unmatch_exit; + + orig += oldlen; + } + + /* + * Yes, the preimage is based on an older version that still + * has whitespace breakages unfixed, and fixing them makes the + * hunk match. Update the context lines in the postimage. + */ + fixed_buf = strbuf_detach(&fixed, &fixed_len); + update_pre_post_images(preimage, postimage, + fixed_buf, fixed_len, 0); + return 1; + + unmatch_exit: + strbuf_release(&fixed); + return 0; +} + +static int find_pos(struct image *img, + struct image *preimage, + struct image *postimage, + int line, + unsigned ws_rule, + int match_beginning, int match_end) +{ + int i; + unsigned long backwards, forwards, try; + int backwards_lno, forwards_lno, try_lno; + + /* + * If match_beginning or match_end is specified, there is no + * point starting from a wrong line that will never match and + * wander around and wait for a match at the specified end. + */ + if (match_beginning) + line = 0; + else if (match_end) + line = img->nr - preimage->nr; + + /* + * Because the comparison is unsigned, the following test + * will also take care of a negative line number that can + * result when match_end and preimage is larger than the target. + */ + if ((size_t) line > img->nr) + line = img->nr; + + try = 0; + for (i = 0; i < line; i++) + try += img->line[i].len; + + /* + * There's probably some smart way to do this, but I'll leave + * that to the smart and beautiful people. I'm simple and stupid. + */ + backwards = try; + backwards_lno = line; + forwards = try; + forwards_lno = line; + try_lno = line; + + for (i = 0; ; i++) { + if (match_fragment(img, preimage, postimage, + try, try_lno, ws_rule, + match_beginning, match_end)) + return try_lno; + + again: + if (backwards_lno == 0 && forwards_lno == img->nr) + break; + + if (i & 1) { + if (backwards_lno == 0) { + i++; + goto again; + } + backwards_lno--; + backwards -= img->line[backwards_lno].len; + try = backwards; + try_lno = backwards_lno; + } else { + if (forwards_lno == img->nr) { + i++; + goto again; + } + forwards += img->line[forwards_lno].len; + forwards_lno++; + try = forwards; + try_lno = forwards_lno; + } + + } + return -1; +} + +static void remove_first_line(struct image *img) +{ + img->buf += img->line[0].len; + img->len -= img->line[0].len; + img->line++; + img->nr--; +} + +static void remove_last_line(struct image *img) +{ + img->len -= img->line[--img->nr].len; +} + +/* + * The change from "preimage" and "postimage" has been found to + * apply at applied_pos (counts in line numbers) in "img". + * Update "img" to remove "preimage" and replace it with "postimage". + */ +static void update_image(struct image *img, + int applied_pos, + struct image *preimage, + struct image *postimage) +{ + /* + * remove the copy of preimage at offset in img + * and replace it with postimage + */ + int i, nr; + size_t remove_count, insert_count, applied_at = 0; + char *result; + int preimage_limit; + + /* + * If we are removing blank lines at the end of img, + * the preimage may extend beyond the end. + * If that is the case, we must be careful only to + * remove the part of the preimage that falls within + * the boundaries of img. Initialize preimage_limit + * to the number of lines in the preimage that falls + * within the boundaries. + */ + preimage_limit = preimage->nr; + if (preimage_limit > img->nr - applied_pos) + preimage_limit = img->nr - applied_pos; + + for (i = 0; i < applied_pos; i++) + applied_at += img->line[i].len; + + remove_count = 0; + for (i = 0; i < preimage_limit; i++) + remove_count += img->line[applied_pos + i].len; + insert_count = postimage->len; + + /* Adjust the contents */ + result = xmalloc(img->len + insert_count - remove_count + 1); + memcpy(result, img->buf, applied_at); + memcpy(result + applied_at, postimage->buf, postimage->len); + memcpy(result + applied_at + postimage->len, + img->buf + (applied_at + remove_count), + img->len - (applied_at + remove_count)); + free(img->buf); + img->buf = result; + img->len += insert_count - remove_count; + result[img->len] = '\0'; + + /* Adjust the line table */ + nr = img->nr + postimage->nr - preimage_limit; + if (preimage_limit < postimage->nr) { + /* + * NOTE: this knows that we never call remove_first_line() + * on anything other than pre/post image. + */ + img->line = xrealloc(img->line, nr * sizeof(*img->line)); + img->line_allocated = img->line; + } + if (preimage_limit != postimage->nr) + memmove(img->line + applied_pos + postimage->nr, + img->line + applied_pos + preimage_limit, + (img->nr - (applied_pos + preimage_limit)) * + sizeof(*img->line)); + memcpy(img->line + applied_pos, + postimage->line, + postimage->nr * sizeof(*img->line)); + if (!allow_overlap) + for (i = 0; i < postimage->nr; i++) + img->line[applied_pos + i].flag |= LINE_PATCHED; + img->nr = nr; +} + +/* + * Use the patch-hunk text in "frag" to prepare two images (preimage and + * postimage) for the hunk. Find lines that match "preimage" in "img" and + * replace the part of "img" with "postimage" text. + */ +static int apply_one_fragment(struct image *img, struct fragment *frag, + int inaccurate_eof, unsigned ws_rule, + int nth_fragment) +{ + int match_beginning, match_end; + const char *patch = frag->patch; + int size = frag->size; + char *old, *oldlines; + struct strbuf newlines; + int new_blank_lines_at_end = 0; + int found_new_blank_lines_at_end = 0; + int hunk_linenr = frag->linenr; + unsigned long leading, trailing; + int pos, applied_pos; + struct image preimage; + struct image postimage; + + memset(&preimage, 0, sizeof(preimage)); + memset(&postimage, 0, sizeof(postimage)); + oldlines = xmalloc(size); + strbuf_init(&newlines, size); + + old = oldlines; + while (size > 0) { + char first; + int len = linelen(patch, size); + int plen; + int added_blank_line = 0; + int is_blank_context = 0; + size_t start; + + if (!len) + break; + + /* + * "plen" is how much of the line we should use for + * the actual patch data. Normally we just remove the + * first character on the line, but if the line is + * followed by "\ No newline", then we also remove the + * last one (which is the newline, of course). + */ + plen = len - 1; + if (len < size && patch[len] == '\\') + plen--; + first = *patch; + if (apply_in_reverse) { + if (first == '-') + first = '+'; + else if (first == '+') + first = '-'; + } + + switch (first) { + case '\n': + /* Newer GNU diff, empty context line */ + if (plen < 0) + /* ... followed by '\No newline'; nothing */ + break; + *old++ = '\n'; + strbuf_addch(&newlines, '\n'); + add_line_info(&preimage, "\n", 1, LINE_COMMON); + add_line_info(&postimage, "\n", 1, LINE_COMMON); + is_blank_context = 1; + break; + case ' ': + if (plen && (ws_rule & WS_BLANK_AT_EOF) && + ws_blank_line(patch + 1, plen, ws_rule)) + is_blank_context = 1; + case '-': + memcpy(old, patch + 1, plen); + add_line_info(&preimage, old, plen, + (first == ' ' ? LINE_COMMON : 0)); + old += plen; + if (first == '-') + break; + /* Fall-through for ' ' */ + case '+': + /* --no-add does not add new lines */ + if (first == '+' && no_add) + break; + + start = newlines.len; + if (first != '+' || + !whitespace_error || + ws_error_action != correct_ws_error) { + strbuf_add(&newlines, patch + 1, plen); + } + else { + ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &applied_after_fixing_ws); + } + add_line_info(&postimage, newlines.buf + start, newlines.len - start, + (first == '+' ? 0 : LINE_COMMON)); + if (first == '+' && + (ws_rule & WS_BLANK_AT_EOF) && + ws_blank_line(patch + 1, plen, ws_rule)) + added_blank_line = 1; + break; + case '@': case '\\': + /* Ignore it, we already handled it */ + break; + default: + if (apply_verbosely) + error(_("invalid start of line: '%c'"), first); + return -1; + } + if (added_blank_line) { + if (!new_blank_lines_at_end) + found_new_blank_lines_at_end = hunk_linenr; + new_blank_lines_at_end++; + } + else if (is_blank_context) + ; + else + new_blank_lines_at_end = 0; + patch += len; + size -= len; + hunk_linenr++; + } + if (inaccurate_eof && + old > oldlines && old[-1] == '\n' && + newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') { + old--; + strbuf_setlen(&newlines, newlines.len - 1); + } + + leading = frag->leading; + trailing = frag->trailing; + + /* + * A hunk to change lines at the beginning would begin with + * @@ -1,L +N,M @@ + * but we need to be careful. -U0 that inserts before the second + * line also has this pattern. + * + * And a hunk to add to an empty file would begin with + * @@ -0,0 +N,M @@ + * + * In other words, a hunk that is (frag->oldpos <= 1) with or + * without leading context must match at the beginning. + */ + match_beginning = (!frag->oldpos || + (frag->oldpos == 1 && !unidiff_zero)); + + /* + * A hunk without trailing lines must match at the end. + * However, we simply cannot tell if a hunk must match end + * from the lack of trailing lines if the patch was generated + * with unidiff without any context. + */ + match_end = !unidiff_zero && !trailing; + + pos = frag->newpos ? (frag->newpos - 1) : 0; + preimage.buf = oldlines; + preimage.len = old - oldlines; + postimage.buf = newlines.buf; + postimage.len = newlines.len; + preimage.line = preimage.line_allocated; + postimage.line = postimage.line_allocated; + + for (;;) { + + applied_pos = find_pos(img, &preimage, &postimage, pos, + ws_rule, match_beginning, match_end); + + if (applied_pos >= 0) + break; + + /* Am I at my context limits? */ + if ((leading <= p_context) && (trailing <= p_context)) + break; + if (match_beginning || match_end) { + match_beginning = match_end = 0; + continue; + } + + /* + * Reduce the number of context lines; reduce both + * leading and trailing if they are equal otherwise + * just reduce the larger context. + */ + if (leading >= trailing) { + remove_first_line(&preimage); + remove_first_line(&postimage); + pos--; + leading--; + } + if (trailing > leading) { + remove_last_line(&preimage); + remove_last_line(&postimage); + trailing--; + } + } + + if (applied_pos >= 0) { + if (new_blank_lines_at_end && + preimage.nr + applied_pos >= img->nr && + (ws_rule & WS_BLANK_AT_EOF) && + ws_error_action != nowarn_ws_error) { + record_ws_error(WS_BLANK_AT_EOF, "+", 1, + found_new_blank_lines_at_end); + if (ws_error_action == correct_ws_error) { + while (new_blank_lines_at_end--) + remove_last_line(&postimage); + } + /* + * We would want to prevent write_out_results() + * from taking place in apply_patch() that follows + * the callchain led us here, which is: + * apply_patch->check_patch_list->check_patch-> + * apply_data->apply_fragments->apply_one_fragment + */ + if (ws_error_action == die_on_ws_error) + apply = 0; + } + + if (apply_verbosely && applied_pos != pos) { + int offset = applied_pos - pos; + if (apply_in_reverse) + offset = 0 - offset; + fprintf_ln(stderr, + Q_("Hunk #%d succeeded at %d (offset %d line).", + "Hunk #%d succeeded at %d (offset %d lines).", + offset), + nth_fragment, applied_pos + 1, offset); + } + + /* + * Warn if it was necessary to reduce the number + * of context lines. + */ + if ((leading != frag->leading) || + (trailing != frag->trailing)) + fprintf_ln(stderr, _("Context reduced to (%ld/%ld)" + " to apply fragment at %d"), + leading, trailing, applied_pos+1); + update_image(img, applied_pos, &preimage, &postimage); + } else { + if (apply_verbosely) + error(_("while searching for:\n%.*s"), + (int)(old - oldlines), oldlines); + } + + free(oldlines); + strbuf_release(&newlines); + free(preimage.line_allocated); + free(postimage.line_allocated); + + return (applied_pos < 0); +} + +static int apply_binary_fragment(struct image *img, struct patch *patch) +{ + struct fragment *fragment = patch->fragments; + unsigned long len; + void *dst; + + if (!fragment) + return error(_("missing binary patch data for '%s'"), + patch->new_name ? + patch->new_name : + patch->old_name); + + /* Binary patch is irreversible without the optional second hunk */ + if (apply_in_reverse) { + if (!fragment->next) + return error("cannot reverse-apply a binary patch " + "without the reverse hunk to '%s'", + patch->new_name + ? patch->new_name : patch->old_name); + fragment = fragment->next; + } + switch (fragment->binary_patch_method) { + case BINARY_DELTA_DEFLATED: + dst = patch_delta(img->buf, img->len, fragment->patch, + fragment->size, &len); + if (!dst) + return -1; + clear_image(img); + img->buf = dst; + img->len = len; + return 0; + case BINARY_LITERAL_DEFLATED: + clear_image(img); + img->len = fragment->size; + img->buf = xmalloc(img->len+1); + memcpy(img->buf, fragment->patch, img->len); + img->buf[img->len] = '\0'; + return 0; + } + return -1; +} + +/* + * Replace "img" with the result of applying the binary patch. + * The binary patch data itself in patch->fragment is still kept + * but the preimage prepared by the caller in "img" is freed here + * or in the helper function apply_binary_fragment() this calls. + */ +static int apply_binary(struct image *img, struct patch *patch) +{ + const char *name = patch->old_name ? patch->old_name : patch->new_name; + unsigned char sha1[20]; + + /* + * For safety, we require patch index line to contain + * full 40-byte textual SHA1 for old and new, at least for now. + */ + if (strlen(patch->old_sha1_prefix) != 40 || + strlen(patch->new_sha1_prefix) != 40 || + get_sha1_hex(patch->old_sha1_prefix, sha1) || + get_sha1_hex(patch->new_sha1_prefix, sha1)) + return error("cannot apply binary patch to '%s' " + "without full index line", name); + + if (patch->old_name) { + /* + * See if the old one matches what the patch + * applies to. + */ + hash_sha1_file(img->buf, img->len, blob_type, sha1); + if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix)) + return error("the patch applies to '%s' (%s), " + "which does not match the " + "current contents.", + name, sha1_to_hex(sha1)); + } + else { + /* Otherwise, the old one must be empty. */ + if (img->len) + return error("the patch applies to an empty " + "'%s' but it is not empty", name); + } + + get_sha1_hex(patch->new_sha1_prefix, sha1); + if (is_null_sha1(sha1)) { + clear_image(img); + return 0; /* deletion patch */ + } + + if (has_sha1_file(sha1)) { + /* We already have the postimage */ + enum object_type type; + unsigned long size; + char *result; + + result = read_sha1_file(sha1, &type, &size); + if (!result) + return error("the necessary postimage %s for " + "'%s' cannot be read", + patch->new_sha1_prefix, name); + clear_image(img); + img->buf = result; + img->len = size; + } else { + /* + * We have verified buf matches the preimage; + * apply the patch data to it, which is stored + * in the patch->fragments->{patch,size}. + */ + if (apply_binary_fragment(img, patch)) + return error(_("binary patch does not apply to '%s'"), + name); + + /* verify that the result matches */ + hash_sha1_file(img->buf, img->len, blob_type, sha1); + if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix)) + return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"), + name, patch->new_sha1_prefix, sha1_to_hex(sha1)); + } + + return 0; +} + +static int apply_fragments(struct image *img, struct patch *patch) +{ + struct fragment *frag = patch->fragments; + const char *name = patch->old_name ? patch->old_name : patch->new_name; + unsigned ws_rule = patch->ws_rule; + unsigned inaccurate_eof = patch->inaccurate_eof; + int nth = 0; + + if (patch->is_binary) + return apply_binary(img, patch); + + while (frag) { + nth++; + if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule, nth)) { + error(_("patch failed: %s:%ld"), name, frag->oldpos); + if (!apply_with_reject) + return -1; + frag->rejected = 1; + } + frag = frag->next; + } + return 0; +} + +static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode) +{ + if (S_ISGITLINK(mode)) { + strbuf_grow(buf, 100); + strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1)); + } else { + enum object_type type; + unsigned long sz; + char *result; + + result = read_sha1_file(sha1, &type, &sz); + if (!result) + return -1; + /* XXX read_sha1_file NUL-terminates */ + strbuf_attach(buf, result, sz, sz + 1); + } + return 0; +} + +static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf) +{ + if (!ce) + return 0; + return read_blob_object(buf, ce->sha1, ce->ce_mode); +} + +static struct patch *in_fn_table(const char *name) +{ + struct string_list_item *item; + + if (name == NULL) + return NULL; + + item = string_list_lookup(&fn_table, name); + if (item != NULL) + return (struct patch *)item->util; + + return NULL; +} + +/* + * item->util in the filename table records the status of the path. + * Usually it points at a patch (whose result records the contents + * of it after applying it), but it could be PATH_WAS_DELETED for a + * path that a previously applied patch has already removed, or + * PATH_TO_BE_DELETED for a path that a later patch would remove. + * + * The latter is needed to deal with a case where two paths A and B + * are swapped by first renaming A to B and then renaming B to A; + * moving A to B should not be prevented due to presense of B as we + * will remove it in a later patch. + */ +#define PATH_TO_BE_DELETED ((struct patch *) -2) +#define PATH_WAS_DELETED ((struct patch *) -1) + +static int to_be_deleted(struct patch *patch) +{ + return patch == PATH_TO_BE_DELETED; +} + +static int was_deleted(struct patch *patch) +{ + return patch == PATH_WAS_DELETED; +} + +static void add_to_fn_table(struct patch *patch) +{ + struct string_list_item *item; + + /* + * Always add new_name unless patch is a deletion + * This should cover the cases for normal diffs, + * file creations and copies + */ + if (patch->new_name != NULL) { + item = string_list_insert(&fn_table, patch->new_name); + item->util = patch; + } + + /* + * store a failure on rename/deletion cases because + * later chunks shouldn't patch old names + */ + if ((patch->new_name == NULL) || (patch->is_rename)) { + item = string_list_insert(&fn_table, patch->old_name); + item->util = PATH_WAS_DELETED; + } +} + +static void prepare_fn_table(struct patch *patch) +{ + /* + * store information about incoming file deletion + */ + while (patch) { + if ((patch->new_name == NULL) || (patch->is_rename)) { + struct string_list_item *item; + item = string_list_insert(&fn_table, patch->old_name); + item->util = PATH_TO_BE_DELETED; + } + patch = patch->next; + } +} + +static int checkout_target(struct cache_entry *ce, struct stat *st) +{ + struct checkout costate; + + memset(&costate, 0, sizeof(costate)); + costate.base_dir = ""; + costate.refresh_cache = 1; + if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st)) + return error(_("cannot checkout %s"), ce->name); + return 0; +} + +static struct patch *previous_patch(struct patch *patch, int *gone) +{ + struct patch *previous; + + *gone = 0; + if (patch->is_copy || patch->is_rename) + return NULL; /* "git" patches do not depend on the order */ + + previous = in_fn_table(patch->old_name); + if (!previous) + return NULL; + + if (to_be_deleted(previous)) + return NULL; /* the deletion hasn't happened yet */ + + if (was_deleted(previous)) + *gone = 1; + + return previous; +} + +static int verify_index_match(struct cache_entry *ce, struct stat *st) +{ + if (S_ISGITLINK(ce->ce_mode)) { + if (!S_ISDIR(st->st_mode)) + return -1; + return 0; + } + return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE); +} + +#define SUBMODULE_PATCH_WITHOUT_INDEX 1 + +static int load_patch_target(struct strbuf *buf, + struct cache_entry *ce, + struct stat *st, + const char *name, + unsigned expected_mode) +{ + if (cached) { + if (read_file_or_gitlink(ce, buf)) + return error(_("read of %s failed"), name); + } else if (name) { + if (S_ISGITLINK(expected_mode)) { + if (ce) + return read_file_or_gitlink(ce, buf); + else + return SUBMODULE_PATCH_WITHOUT_INDEX; + } else { + if (read_old_data(st, name, buf)) + return error(_("read of %s failed"), name); + } + } + return 0; +} + +/* + * We are about to apply "patch"; populate the "image" with the + * current version we have, from the working tree or from the index, + * depending on the situation e.g. --cached/--index. If we are + * applying a non-git patch that incrementally updates the tree, + * we read from the result of a previous diff. + */ +static int load_preimage(struct image *image, + struct patch *patch, struct stat *st, struct cache_entry *ce) +{ + struct strbuf buf = STRBUF_INIT; + size_t len; + char *img; + struct patch *previous; + int status; + + previous = previous_patch(patch, &status); + if (status) + return error(_("path %s has been renamed/deleted"), + patch->old_name); + if (previous) { + /* We have a patched copy in memory; use that. */ + strbuf_add(&buf, previous->result, previous->resultsize); + } else { + status = load_patch_target(&buf, ce, st, + patch->old_name, patch->old_mode); + if (status < 0) + return status; + else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) { + /* + * There is no way to apply subproject + * patch without looking at the index. + * NEEDSWORK: shouldn't this be flagged + * as an error??? + */ + free_fragment_list(patch->fragments); + patch->fragments = NULL; + } else if (status) { + return error(_("read of %s failed"), patch->old_name); + } + } + + img = strbuf_detach(&buf, &len); + prepare_image(image, img, len, !patch->is_binary); + return 0; +} + +static int three_way_merge(struct image *image, + char *path, + const unsigned char *base, + const unsigned char *ours, + const unsigned char *theirs) +{ + mmfile_t base_file, our_file, their_file; + mmbuffer_t result = { NULL }; + int status; + + read_mmblob(&base_file, base); + read_mmblob(&our_file, ours); + read_mmblob(&their_file, theirs); + status = ll_merge(&result, path, + &base_file, "base", + &our_file, "ours", + &their_file, "theirs", NULL); + free(base_file.ptr); + free(our_file.ptr); + free(their_file.ptr); + if (status < 0 || !result.ptr) { + free(result.ptr); + return -1; + } + clear_image(image); + image->buf = result.ptr; + image->len = result.size; + + return status; +} + +/* + * When directly falling back to add/add three-way merge, we read from + * the current contents of the new_name. In no cases other than that + * this function will be called. + */ +static int load_current(struct image *image, struct patch *patch) +{ + struct strbuf buf = STRBUF_INIT; + int status, pos; + size_t len; + char *img; + struct stat st; + struct cache_entry *ce; + char *name = patch->new_name; + unsigned mode = patch->new_mode; + + if (!patch->is_new) + die("BUG: patch to %s is not a creation", patch->old_name); + + pos = cache_name_pos(name, strlen(name)); + if (pos < 0) + return error(_("%s: does not exist in index"), name); + ce = active_cache[pos]; + if (lstat(name, &st)) { + if (errno != ENOENT) + return error(_("%s: %s"), name, strerror(errno)); + if (checkout_target(ce, &st)) + return -1; + } + if (verify_index_match(ce, &st)) + return error(_("%s: does not match index"), name); + + status = load_patch_target(&buf, ce, &st, name, mode); + if (status < 0) + return status; + else if (status) + return -1; + img = strbuf_detach(&buf, &len); + prepare_image(image, img, len, !patch->is_binary); + return 0; +} + +static int try_threeway(struct image *image, struct patch *patch, + struct stat *st, struct cache_entry *ce) +{ + unsigned char pre_sha1[20], post_sha1[20], our_sha1[20]; + struct strbuf buf = STRBUF_INIT; + size_t len; + int status; + char *img; + struct image tmp_image; + + /* No point falling back to 3-way merge in these cases */ + if (patch->is_delete || + S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode)) + return -1; + + /* Preimage the patch was prepared for */ + if (patch->is_new) + write_sha1_file("", 0, blob_type, pre_sha1); + else if (get_sha1(patch->old_sha1_prefix, pre_sha1) || + read_blob_object(&buf, pre_sha1, patch->old_mode)) + return error("repository lacks the necessary blob to fall back on 3-way merge."); + + fprintf(stderr, "Falling back to three-way merge...\n"); + + img = strbuf_detach(&buf, &len); + prepare_image(&tmp_image, img, len, 1); + /* Apply the patch to get the post image */ + if (apply_fragments(&tmp_image, patch) < 0) { + clear_image(&tmp_image); + return -1; + } + /* post_sha1[] is theirs */ + write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1); + clear_image(&tmp_image); + + /* our_sha1[] is ours */ + if (patch->is_new) { + if (load_current(&tmp_image, patch)) + return error("cannot read the current contents of '%s'", + patch->new_name); + } else { + if (load_preimage(&tmp_image, patch, st, ce)) + return error("cannot read the current contents of '%s'", + patch->old_name); + } + write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1); + clear_image(&tmp_image); + + /* in-core three-way merge between post and our using pre as base */ + status = three_way_merge(image, patch->new_name, + pre_sha1, our_sha1, post_sha1); + if (status < 0) { + fprintf(stderr, "Failed to fall back on three-way merge...\n"); + return status; + } + + if (status) { + patch->conflicted_threeway = 1; + if (patch->is_new) + hashclr(patch->threeway_stage[0]); + else + hashcpy(patch->threeway_stage[0], pre_sha1); + hashcpy(patch->threeway_stage[1], our_sha1); + hashcpy(patch->threeway_stage[2], post_sha1); + fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name); + } else { + fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name); + } + return 0; +} + +static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce) +{ + struct image image; + + if (load_preimage(&image, patch, st, ce) < 0) + return -1; + + if (patch->direct_to_threeway || + apply_fragments(&image, patch) < 0) { + /* Note: with --reject, apply_fragments() returns 0 */ + if (!threeway || try_threeway(&image, patch, st, ce) < 0) + return -1; + } + patch->result = image.buf; + patch->resultsize = image.len; + add_to_fn_table(patch); + free(image.line_allocated); + + if (0 < patch->is_delete && patch->resultsize) + return error(_("removal patch leaves file contents")); + + return 0; +} + +/* + * If "patch" that we are looking at modifies or deletes what we have, + * we would want it not to lose any local modification we have, either + * in the working tree or in the index. + * + * This also decides if a non-git patch is a creation patch or a + * modification to an existing empty file. We do not check the state + * of the current tree for a creation patch in this function; the caller + * check_patch() separately makes sure (and errors out otherwise) that + * the path the patch creates does not exist in the current tree. + */ +static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st) +{ + const char *old_name = patch->old_name; + struct patch *previous = NULL; + int stat_ret = 0, status; + unsigned st_mode = 0; + + if (!old_name) + return 0; + + assert(patch->is_new <= 0); + previous = previous_patch(patch, &status); + + if (status) + return error(_("path %s has been renamed/deleted"), old_name); + if (previous) { + st_mode = previous->new_mode; + } else if (!cached) { + stat_ret = lstat(old_name, st); + if (stat_ret && errno != ENOENT) + return error(_("%s: %s"), old_name, strerror(errno)); + } + + if (check_index && !previous) { + 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) { + if (checkout_target(*ce, 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) + goto is_new; + return error(_("%s: %s"), old_name, strerror(errno)); + } + + if (!cached && !previous) + 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) + warning(_("%s has type %o, expected %o"), + old_name, st_mode, patch->old_mode); + if (!patch->new_mode && !patch->is_delete) + patch->new_mode = st_mode; + return 0; + + is_new: + patch->is_new = 1; + patch->is_delete = 0; + free(patch->old_name); + patch->old_name = NULL; + return 0; +} + + +#define EXISTS_IN_INDEX 1 +#define EXISTS_IN_WORKTREE 2 + +static int check_to_create(const char *new_name, int ok_if_exists) +{ + struct stat nst; + + if (check_index && + cache_name_pos(new_name, strlen(new_name)) >= 0 && + !ok_if_exists) + return EXISTS_IN_INDEX; + if (cached) + return 0; + + if (!lstat(new_name, &nst)) { + if (S_ISDIR(nst.st_mode) || ok_if_exists) + return 0; + /* + * A leading component of new_name might be a symlink + * that is going to be removed with this patch, but + * still pointing at somewhere that has the path. + * In such a case, path "new_name" does not exist as + * far as git is concerned. + */ + if (has_symlink_leading_path(new_name, strlen(new_name))) + return 0; + + return EXISTS_IN_WORKTREE; + } else if ((errno != ENOENT) && (errno != ENOTDIR)) { + return error("%s: %s", new_name, strerror(errno)); + } + return 0; +} + +/* + * Check and apply the patch in-core; leave the result in patch->result + * for the caller to write it out to the final destination. + */ +static int check_patch(struct patch *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; + struct patch *tpatch; + 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; + + /* + * A type-change diff is always split into a patch to delete + * old, immediately followed by a patch to create new (see + * diff.c::run_diff()); in such a case it is Ok that the entry + * to be deleted by the previous patch is still in the working + * tree and in the index. + * + * A patch to swap-rename between A and B would first rename A + * to B and then rename B to A. While applying the first one, + * the presense of B should not stop A from getting renamed to + * B; ask to_be_deleted() about the later rename. Removal of + * B and rename from A to B is handled the same way by asking + * was_deleted(). + */ + if ((tpatch = in_fn_table(new_name)) && + (was_deleted(tpatch) || to_be_deleted(tpatch))) + ok_if_exists = 1; + else + ok_if_exists = 0; + + if (new_name && + ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) { + int err = check_to_create(new_name, ok_if_exists); + + if (err && threeway) { + patch->direct_to_threeway = 1; + } else switch (err) { + case 0: + break; /* happy */ + case EXISTS_IN_INDEX: + return error(_("%s: already exists in index"), new_name); + break; + case EXISTS_IN_WORKTREE: + return error(_("%s: already exists in working directory"), + new_name); + default: + return err; + } + + if (!patch->new_mode) { + if (0 < patch->is_new) + patch->new_mode = S_IFREG | 0644; + else + patch->new_mode = patch->old_mode; + } + } + + if (new_name && old_name) { + int same = !strcmp(old_name, new_name); + if (!patch->new_mode) + patch->new_mode = patch->old_mode; + if ((patch->old_mode ^ patch->new_mode) & S_IFMT) { + if (same) + return error(_("new mode (%o) of %s does not " + "match old mode (%o)"), + patch->new_mode, new_name, + patch->old_mode); + else + return error(_("new mode (%o) of %s does not " + "match old mode (%o) of %s"), + patch->new_mode, new_name, + patch->old_mode, old_name); + } + } + + if (apply_data(patch, &st, ce) < 0) + return error(_("%s: patch does not apply"), name); + patch->rejected = 0; + return 0; +} + +static int check_patch_list(struct patch *patch) +{ + int err = 0; + + prepare_fn_table(patch); + while (patch) { + if (apply_verbosely) + say_patch_name(stderr, + _("Checking patch %s..."), patch); + err |= check_patch(patch); + patch = patch->next; + } + return err; +} + +/* This function tries to read the sha1 from the current index */ +static int get_current_sha1(const char *path, unsigned char *sha1) +{ + int pos; + + if (read_cache() < 0) + return -1; + pos = cache_name_pos(path, strlen(path)); + if (pos < 0) + return -1; + hashcpy(sha1, active_cache[pos]->sha1); + return 0; +} + +/* Build an index that contains the just the files needed for a 3way merge */ +static void build_fake_ancestor(struct patch *list, const char *filename) +{ + struct patch *patch; + struct index_state result = { NULL }; + int fd; + + /* Once we start supporting the reverse patch, it may be + * worth showing the new sha1 prefix, but until then... + */ + for (patch = list; patch; patch = patch->next) { + const unsigned char *sha1_ptr; + unsigned char sha1[20]; + struct cache_entry *ce; + const char *name; + + name = patch->old_name ? patch->old_name : patch->new_name; + if (0 < patch->is_new) + continue; + else if (get_sha1_blob(patch->old_sha1_prefix, sha1)) + /* git diff has no index line for mode/type changes */ + if (!patch->lines_added && !patch->lines_deleted) { + if (get_current_sha1(patch->old_name, sha1)) + die("mode change for %s, which is not " + "in current HEAD", name); + sha1_ptr = sha1; + } else + die("sha1 information is lacking or useless " + "(%s).", name); + else + sha1_ptr = sha1; + + ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0); + if (!ce) + die(_("make_cache_entry failed for path '%s'"), name); + if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) + die ("Could not add %s to temporary index", name); + } + + fd = open(filename, O_WRONLY | O_CREAT, 0666); + if (fd < 0 || write_index(&result, fd) || close(fd)) + die ("Could not write temporary index to %s", filename); + + discard_index(&result); +} + +static void stat_patch_list(struct patch *patch) +{ + int files, adds, dels; + + for (files = adds = dels = 0 ; patch ; patch = patch->next) { + files++; + adds += patch->lines_added; + dels += patch->lines_deleted; + show_stats(patch); + } + + print_stat_summary(stdout, files, adds, dels); +} + +static void numstat_patch_list(struct patch *patch) +{ + for ( ; patch; patch = patch->next) { + const char *name; + name = patch->new_name ? patch->new_name : patch->old_name; + if (patch->is_binary) + printf("-\t-\t"); + else + printf("%d\t%d\t", patch->lines_added, patch->lines_deleted); + write_name_quoted(name, stdout, line_termination); + } +} + +static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name) +{ + if (mode) + printf(" %s mode %06o %s\n", newdelete, mode, name); + else + printf(" %s %s\n", newdelete, name); +} + +static void show_mode_change(struct patch *p, int show_name) +{ + if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) { + if (show_name) + printf(" mode change %06o => %06o %s\n", + p->old_mode, p->new_mode, p->new_name); + else + printf(" mode change %06o => %06o\n", + p->old_mode, p->new_mode); + } +} + +static void show_rename_copy(struct patch *p) +{ + const char *renamecopy = p->is_rename ? "rename" : "copy"; + const char *old, *new; + + /* Find common prefix */ + old = p->old_name; + new = p->new_name; + while (1) { + const char *slash_old, *slash_new; + slash_old = strchr(old, '/'); + slash_new = strchr(new, '/'); + if (!slash_old || + !slash_new || + slash_old - old != slash_new - new || + memcmp(old, new, slash_new - new)) + break; + old = slash_old + 1; + new = slash_new + 1; + } + /* p->old_name thru old is the common prefix, and old and new + * through the end of names are renames + */ + if (old != p->old_name) + printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy, + (int)(old - p->old_name), p->old_name, + old, new, p->score); + else + printf(" %s %s => %s (%d%%)\n", renamecopy, + p->old_name, p->new_name, p->score); + show_mode_change(p, 0); +} + +static void summary_patch_list(struct patch *patch) +{ + struct patch *p; + + for (p = patch; p; p = p->next) { + if (p->is_new) + show_file_mode_name("create", p->new_mode, p->new_name); + else if (p->is_delete) + show_file_mode_name("delete", p->old_mode, p->old_name); + else { + if (p->is_rename || p->is_copy) + show_rename_copy(p); + else { + if (p->score) { + printf(" rewrite %s (%d%%)\n", + p->new_name, p->score); + show_mode_change(p, 0); + } + else + show_mode_change(p, 1); + } + } + } +} + +static void patch_stats(struct patch *patch) +{ + int lines = patch->lines_added + patch->lines_deleted; + + if (lines > max_change) + max_change = lines; + if (patch->old_name) { + int len = quote_c_style(patch->old_name, NULL, NULL, 0); + if (!len) + len = strlen(patch->old_name); + if (len > max_len) + max_len = len; + } + if (patch->new_name) { + int len = quote_c_style(patch->new_name, NULL, NULL, 0); + if (!len) + len = strlen(patch->new_name); + if (len > max_len) + max_len = len; + } +} + +static void remove_file(struct patch *patch, int rmdir_empty) +{ + if (update_index) { + if (remove_file_from_cache(patch->old_name) < 0) + die(_("unable to remove %s from index"), patch->old_name); + } + if (!cached) { + if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) { + remove_path(patch->old_name); + } + } +} + +static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size) +{ + struct stat st; + struct cache_entry *ce; + int namelen = strlen(path); + unsigned ce_size = cache_entry_size(namelen); + + if (!update_index) + return; + + ce = xcalloc(1, ce_size); + memcpy(ce->name, path, namelen); + ce->ce_mode = create_ce_mode(mode); + ce->ce_flags = create_ce_flags(0); + ce->ce_namelen = namelen; + if (S_ISGITLINK(mode)) { + const char *s = buf; + + if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1)) + die(_("corrupt patch for subproject %s"), path); + } else { + if (!cached) { + if (lstat(path, &st) < 0) + die_errno(_("unable to stat newly created file '%s'"), + path); + fill_stat_cache_info(ce, &st); + } + if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) + die(_("unable to create backing store for newly created file %s"), path); + } + if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) + die(_("unable to add cache entry for %s"), path); +} + +static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) +{ + int fd; + struct strbuf nbuf = STRBUF_INIT; + + if (S_ISGITLINK(mode)) { + struct stat st; + if (!lstat(path, &st) && S_ISDIR(st.st_mode)) + return 0; + return mkdir(path, 0777); + } + + if (has_symlinks && S_ISLNK(mode)) + /* Although buf:size is counted string, it also is NUL + * terminated. + */ + return symlink(buf, path); + + fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666); + if (fd < 0) + return -1; + + if (convert_to_working_tree(path, buf, size, &nbuf)) { + size = nbuf.len; + buf = nbuf.buf; + } + write_or_die(fd, buf, size); + strbuf_release(&nbuf); + + if (close(fd) < 0) + die_errno(_("closing file '%s'"), path); + return 0; +} + +/* + * We optimistically assume that the directories exist, + * which is true 99% of the time anyway. If they don't, + * we create them and try again. + */ +static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size) +{ + if (cached) + return; + if (!try_create_file(path, mode, buf, size)) + return; + + if (errno == ENOENT) { + if (safe_create_leading_directories(path)) + return; + if (!try_create_file(path, mode, buf, size)) + return; + } + + if (errno == EEXIST || errno == EACCES) { + /* We may be trying to create a file where a directory + * used to be. + */ + struct stat st; + if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path))) + errno = EEXIST; + } + + if (errno == EEXIST) { + unsigned int nr = getpid(); + + for (;;) { + char newpath[PATH_MAX]; + mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr); + if (!try_create_file(newpath, mode, buf, size)) { + if (!rename(newpath, path)) + return; + unlink_or_warn(newpath); + break; + } + if (errno != EEXIST) + break; + ++nr; + } + } + die_errno(_("unable to write file '%s' mode %o"), path, mode); +} + +static void add_conflicted_stages_file(struct patch *patch) +{ + int stage, namelen; + unsigned ce_size, mode; + struct cache_entry *ce; + + if (!update_index) + return; + namelen = strlen(patch->new_name); + ce_size = cache_entry_size(namelen); + mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644); + + remove_file_from_cache(patch->new_name); + for (stage = 1; stage < 4; stage++) { + if (is_null_sha1(patch->threeway_stage[stage - 1])) + continue; + ce = xcalloc(1, ce_size); + memcpy(ce->name, patch->new_name, namelen); + ce->ce_mode = create_ce_mode(mode); + ce->ce_flags = create_ce_flags(stage); + ce->ce_namelen = namelen; + hashcpy(ce->sha1, patch->threeway_stage[stage - 1]); + if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) + die(_("unable to add cache entry for %s"), patch->new_name); + } +} + +static void create_file(struct patch *patch) +{ + char *path = patch->new_name; + unsigned mode = patch->new_mode; + unsigned long size = patch->resultsize; + char *buf = patch->result; + + if (!mode) + mode = S_IFREG | 0644; + create_one_file(path, mode, buf, size); + + if (patch->conflicted_threeway) + add_conflicted_stages_file(patch); + else + add_index_file(path, mode, buf, size); +} + +/* phase zero is to remove, phase one is to create */ +static void write_out_one_result(struct patch *patch, int phase) +{ + if (patch->is_delete > 0) { + if (phase == 0) + remove_file(patch, 1); + return; + } + if (patch->is_new > 0 || patch->is_copy) { + if (phase == 1) + create_file(patch); + return; + } + /* + * Rename or modification boils down to the same + * thing: remove the old, write the new + */ + if (phase == 0) + remove_file(patch, patch->is_rename); + if (phase == 1) + create_file(patch); +} + +static int write_out_one_reject(struct patch *patch) +{ + FILE *rej; + char namebuf[PATH_MAX]; + struct fragment *frag; + int cnt = 0; + struct strbuf sb = STRBUF_INIT; + + for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) { + if (!frag->rejected) + continue; + cnt++; + } + + if (!cnt) { + if (apply_verbosely) + say_patch_name(stderr, + _("Applied patch %s cleanly."), patch); + return 0; + } + + /* This should not happen, because a removal patch that leaves + * contents are marked "rejected" at the patch level. + */ + if (!patch->new_name) + die(_("internal error")); + + /* Say this even without --verbose */ + strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...", + "Applying patch %%s with %d rejects...", + cnt), + cnt); + say_patch_name(stderr, sb.buf, patch); + strbuf_release(&sb); + + cnt = strlen(patch->new_name); + if (ARRAY_SIZE(namebuf) <= cnt + 5) { + cnt = ARRAY_SIZE(namebuf) - 5; + warning(_("truncating .rej filename to %.*s.rej"), + cnt - 1, patch->new_name); + } + memcpy(namebuf, patch->new_name, cnt); + memcpy(namebuf + cnt, ".rej", 5); + + rej = fopen(namebuf, "w"); + if (!rej) + return error(_("cannot open %s: %s"), namebuf, strerror(errno)); + + /* Normal git tools never deal with .rej, so do not pretend + * this is a git patch by saying --git nor give extended + * headers. While at it, maybe please "kompare" that wants + * the trailing TAB and some garbage at the end of line ;-). + */ + fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n", + patch->new_name, patch->new_name); + for (cnt = 1, frag = patch->fragments; + frag; + cnt++, frag = frag->next) { + if (!frag->rejected) { + fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt); + continue; + } + fprintf_ln(stderr, _("Rejected hunk #%d."), cnt); + fprintf(rej, "%.*s", frag->size, frag->patch); + if (frag->patch[frag->size-1] != '\n') + fputc('\n', rej); + } + fclose(rej); + return -1; +} + +static int write_out_results(struct patch *list) +{ + int phase; + int errs = 0; + struct patch *l; + struct string_list cpath = STRING_LIST_INIT_DUP; + + for (phase = 0; phase < 2; phase++) { + l = list; + while (l) { + if (l->rejected) + errs = 1; + else { + write_out_one_result(l, phase); + if (phase == 1) { + if (write_out_one_reject(l)) + errs = 1; + if (l->conflicted_threeway) { + string_list_append(&cpath, l->new_name); + errs = 1; + } + } + } + l = l->next; + } + } + + if (cpath.nr) { + struct string_list_item *item; + + sort_string_list(&cpath); + for_each_string_list_item(item, &cpath) + fprintf(stderr, "U %s\n", item->string); + string_list_clear(&cpath, 0); + + rerere(0); + } + + return errs; +} + +static struct lock_file lock_file; + +static struct string_list limit_by_name; +static int has_include; +static void add_name_limit(const char *name, int exclude) +{ + struct string_list_item *it; + + it = string_list_append(&limit_by_name, name); + it->util = exclude ? NULL : (void *) 1; +} + +static int use_patch(struct patch *p) +{ + const char *pathname = p->new_name ? p->new_name : p->old_name; + int i; + + /* Paths outside are not touched regardless of "--include" */ + if (0 < prefix_length) { + int pathlen = strlen(pathname); + if (pathlen <= prefix_length || + memcmp(prefix, pathname, prefix_length)) + return 0; + } + + /* See if it matches any of exclude/include rule */ + for (i = 0; i < limit_by_name.nr; i++) { + struct string_list_item *it = &limit_by_name.items[i]; + if (!fnmatch(it->string, pathname, 0)) + return (it->util != NULL); + } + + /* + * If we had any include, a path that does not match any rule is + * not used. Otherwise, we saw bunch of exclude rules (or none) + * and such a path is used. + */ + return !has_include; +} + + +static void prefix_one(char **name) +{ + char *old_name = *name; + if (!old_name) + return; + *name = xstrdup(prefix_filename(prefix, prefix_length, *name)); + free(old_name); +} + +static void prefix_patches(struct patch *p) +{ + if (!prefix || p->is_toplevel_relative) + return; + for ( ; p; p = p->next) { + prefix_one(&p->new_name); + prefix_one(&p->old_name); + } +} + +#define INACCURATE_EOF (1<<0) +#define RECOUNT (1<<1) + +static int apply_patch(int fd, const char *filename, int options) +{ + size_t offset; + struct strbuf buf = STRBUF_INIT; /* owns the patch text */ + struct patch *list = NULL, **listp = &list; + int skipped_patch = 0; + + patch_input_file = filename; + read_patch_file(&buf, fd); + offset = 0; + while (offset < buf.len) { + struct patch *patch; + int nr; + + patch = xcalloc(1, sizeof(*patch)); + patch->inaccurate_eof = !!(options & INACCURATE_EOF); + patch->recount = !!(options & RECOUNT); + nr = parse_chunk(buf.buf + offset, buf.len - offset, patch); + if (nr < 0) + break; + if (apply_in_reverse) + reverse_patches(patch); + if (prefix) + prefix_patches(patch); + if (use_patch(patch)) { + patch_stats(patch); + *listp = patch; + listp = &patch->next; + } + else { + free_patch(patch); + skipped_patch++; + } + offset += nr; + } + + if (!list && !skipped_patch) + die(_("unrecognized input")); + + if (whitespace_error && (ws_error_action == die_on_ws_error)) + apply = 0; + + update_index = check_index && apply; + if (update_index && newfd < 0) + newfd = hold_locked_index(&lock_file, 1); + + if (check_index) { + if (read_cache() < 0) + die(_("unable to read index file")); + } + + if ((check || apply) && + check_patch_list(list) < 0 && + !apply_with_reject) + exit(1); + + if (apply && write_out_results(list)) { + if (apply_with_reject) + exit(1); + /* with --3way, we still need to write the index out */ + return 1; + } + + if (fake_ancestor) + build_fake_ancestor(list, fake_ancestor); + + if (diffstat) + stat_patch_list(list); + + if (numstat) + numstat_patch_list(list); + + if (summary) + summary_patch_list(list); + + free_patch_list(list); + strbuf_release(&buf); + string_list_clear(&fn_table, 0); + return 0; +} + +static int git_apply_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "apply.whitespace")) + return git_config_string(&apply_default_whitespace, var, value); + else if (!strcmp(var, "apply.ignorewhitespace")) + return git_config_string(&apply_default_ignorewhitespace, var, value); + return git_default_config(var, value, cb); +} + +static int option_parse_exclude(const struct option *opt, + const char *arg, int unset) +{ + add_name_limit(arg, 1); + return 0; +} + +static int option_parse_include(const struct option *opt, + const char *arg, int unset) +{ + add_name_limit(arg, 0); + has_include = 1; + return 0; +} + +static int option_parse_p(const struct option *opt, + const char *arg, int unset) +{ + p_value = atoi(arg); + p_value_known = 1; + return 0; +} + +static int option_parse_z(const struct option *opt, + const char *arg, int unset) +{ + if (unset) + line_termination = '\n'; + else + line_termination = 0; + return 0; +} + +static int option_parse_space_change(const struct option *opt, + const char *arg, int unset) +{ + if (unset) + ws_ignore_action = ignore_ws_none; + else + ws_ignore_action = ignore_ws_change; + return 0; +} + +static int option_parse_whitespace(const struct option *opt, + const char *arg, int unset) +{ + const char **whitespace_option = opt->value; + + *whitespace_option = arg; + parse_whitespace_option(arg); + return 0; +} + +static int option_parse_directory(const struct option *opt, + const char *arg, int unset) +{ + root_len = strlen(arg); + if (root_len && arg[root_len - 1] != '/') { + char *new_root; + root = new_root = xmalloc(root_len + 2); + strcpy(new_root, arg); + strcpy(new_root + root_len++, "/"); + } else + root = arg; + return 0; +} + +int cmd_apply(int argc, const char **argv, const char *prefix_) +{ + int i; + int errs = 0; + int is_not_gitdir = !startup_info->have_repository; + int force_apply = 0; + + const char *whitespace_option = NULL; + + struct option builtin_apply_options[] = { + { OPTION_CALLBACK, 0, "exclude", NULL, N_("path"), + N_("don't apply changes matching the given path"), + 0, option_parse_exclude }, + { OPTION_CALLBACK, 0, "include", NULL, N_("path"), + N_("apply changes matching the given path"), + 0, option_parse_include }, + { OPTION_CALLBACK, 'p', NULL, NULL, N_("num"), + N_("remove <num> leading slashes from traditional diff paths"), + 0, option_parse_p }, + OPT_BOOLEAN(0, "no-add", &no_add, + N_("ignore additions made by the patch")), + OPT_BOOLEAN(0, "stat", &diffstat, + N_("instead of applying the patch, output diffstat for the input")), + OPT_NOOP_NOARG(0, "allow-binary-replacement"), + OPT_NOOP_NOARG(0, "binary"), + OPT_BOOLEAN(0, "numstat", &numstat, + N_("show number of added and deleted lines in decimal notation")), + OPT_BOOLEAN(0, "summary", &summary, + N_("instead of applying the patch, output a summary for the input")), + OPT_BOOLEAN(0, "check", &check, + N_("instead of applying the patch, see if the patch is applicable")), + OPT_BOOLEAN(0, "index", &check_index, + N_("make sure the patch is applicable to the current index")), + OPT_BOOLEAN(0, "cached", &cached, + N_("apply a patch without touching the working tree")), + OPT_BOOLEAN(0, "apply", &force_apply, + N_("also apply the patch (use with --stat/--summary/--check)")), + OPT_BOOL('3', "3way", &threeway, + N_( "attempt three-way merge if a patch does not apply")), + OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor, + N_("build a temporary index based on embedded index information")), + { OPTION_CALLBACK, 'z', NULL, NULL, NULL, + N_("paths are separated with NUL character"), + PARSE_OPT_NOARG, option_parse_z }, + OPT_INTEGER('C', NULL, &p_context, + N_("ensure at least <n> lines of context match")), + { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"), + N_("detect new or modified lines that have whitespace errors"), + 0, option_parse_whitespace }, + { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL, + N_("ignore changes in whitespace when finding context"), + PARSE_OPT_NOARG, option_parse_space_change }, + { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL, + N_("ignore changes in whitespace when finding context"), + PARSE_OPT_NOARG, option_parse_space_change }, + OPT_BOOLEAN('R', "reverse", &apply_in_reverse, + N_("apply the patch in reverse")), + OPT_BOOLEAN(0, "unidiff-zero", &unidiff_zero, + N_("don't expect at least one line of context")), + OPT_BOOLEAN(0, "reject", &apply_with_reject, + N_("leave the rejected hunks in corresponding *.rej files")), + OPT_BOOLEAN(0, "allow-overlap", &allow_overlap, + N_("allow overlapping hunks")), + OPT__VERBOSE(&apply_verbosely, N_("be verbose")), + OPT_BIT(0, "inaccurate-eof", &options, + N_("tolerate incorrectly detected missing new-line at the end of file"), + INACCURATE_EOF), + OPT_BIT(0, "recount", &options, + N_("do not trust the line counts in the hunk headers"), + RECOUNT), + { OPTION_CALLBACK, 0, "directory", NULL, N_("root"), + N_("prepend <root> to all filenames"), + 0, option_parse_directory }, + OPT_END() + }; + + prefix = prefix_; + prefix_length = prefix ? strlen(prefix) : 0; + git_config(git_apply_config, NULL); + if (apply_default_whitespace) + parse_whitespace_option(apply_default_whitespace); + if (apply_default_ignorewhitespace) + parse_ignorewhitespace_option(apply_default_ignorewhitespace); + + argc = parse_options(argc, argv, prefix, builtin_apply_options, + apply_usage, 0); + + if (apply_with_reject && threeway) + die("--reject and --3way cannot be used together."); + if (cached && threeway) + die("--cached and --3way cannot be used together."); + if (threeway) { + if (is_not_gitdir) + die(_("--3way outside a repository")); + check_index = 1; + } + if (apply_with_reject) + apply = apply_verbosely = 1; + if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor)) + apply = 0; + if (check_index && is_not_gitdir) + die(_("--index outside a repository")); + if (cached) { + if (is_not_gitdir) + die(_("--cached outside a repository")); + check_index = 1; + } + for (i = 0; i < argc; i++) { + const char *arg = argv[i]; + int fd; + + if (!strcmp(arg, "-")) { + errs |= apply_patch(0, "<stdin>", options); + read_stdin = 0; + continue; + } else if (0 < prefix_length) + arg = prefix_filename(prefix, prefix_length, arg); + + fd = open(arg, O_RDONLY); + if (fd < 0) + die_errno(_("can't open patch '%s'"), arg); + read_stdin = 0; + set_default_whitespace_mode(whitespace_option); + errs |= apply_patch(fd, arg, options); + close(fd); + } + set_default_whitespace_mode(whitespace_option); + if (read_stdin) + errs |= apply_patch(0, "<stdin>", options); + if (whitespace_error) { + if (squelch_whitespace_errors && + squelch_whitespace_errors < whitespace_error) { + int squelched = + whitespace_error - squelch_whitespace_errors; + warning(Q_("squelched %d whitespace error", + "squelched %d whitespace errors", + squelched), + squelched); + } + if (ws_error_action == die_on_ws_error) + die(Q_("%d line adds whitespace errors.", + "%d lines add whitespace errors.", + whitespace_error), + whitespace_error); + if (applied_after_fixing_ws && apply) + warning("%d line%s applied after" + " fixing whitespace errors.", + applied_after_fixing_ws, + applied_after_fixing_ws == 1 ? "" : "s"); + else if (whitespace_error) + warning(Q_("%d line adds whitespace errors.", + "%d lines add whitespace errors.", + whitespace_error), + whitespace_error); + } + + if (update_index) { + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(&lock_file)) + die(_("Unable to write new index file")); + } + + return !!errs; +} diff --git a/builtin/archive.c b/builtin/archive.c new file mode 100644 index 0000000000..9a1cfd3dac --- /dev/null +++ b/builtin/archive.c @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2006 Franck Bui-Huu + * Copyright (c) 2006 Rene Scharfe + */ +#include "cache.h" +#include "builtin.h" +#include "archive.h" +#include "transport.h" +#include "parse-options.h" +#include "pkt-line.h" +#include "sideband.h" + +static void create_output_file(const char *output_file) +{ + int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666); + if (output_fd < 0) + die_errno(_("could not create archive file '%s'"), output_file); + if (output_fd != 1) { + if (dup2(output_fd, 1) < 0) + die_errno(_("could not redirect output")); + else + close(output_fd); + } +} + +static int run_remote_archiver(int argc, const char **argv, + const char *remote, const char *exec, + const char *name_hint) +{ + char buf[LARGE_PACKET_MAX]; + int fd[2], i, len, rv; + struct transport *transport; + struct remote *_remote; + + _remote = remote_get(remote); + if (!_remote->url[0]) + die(_("git archive: Remote with no URL")); + transport = transport_get(_remote, _remote->url[0]); + transport_connect(transport, "git-upload-archive", exec, fd); + + /* + * Inject a fake --format field at the beginning of the + * arguments, with the format inferred from our output + * filename. This way explicit --format options can override + * it. + */ + if (name_hint) { + const char *format = archive_format_from_filename(name_hint); + if (format) + packet_write(fd[1], "argument --format=%s\n", format); + } + for (i = 1; i < argc; i++) + packet_write(fd[1], "argument %s\n", argv[i]); + packet_flush(fd[1]); + + len = packet_read_line(fd[0], buf, sizeof(buf)); + if (!len) + die(_("git archive: expected ACK/NAK, got EOF")); + if (buf[len-1] == '\n') + buf[--len] = 0; + if (strcmp(buf, "ACK")) { + if (len > 5 && !prefixcmp(buf, "NACK ")) + die(_("git archive: NACK %s"), buf + 5); + if (len > 4 && !prefixcmp(buf, "ERR ")) + die(_("remote error: %s"), buf + 4); + die(_("git archive: protocol error")); + } + + len = packet_read_line(fd[0], buf, sizeof(buf)); + if (len) + die(_("git archive: expected a flush")); + + /* Now, start reading from fd[0] and spit it out to stdout */ + rv = recv_sideband("archive", fd[0], 1); + rv |= transport_disconnect(transport); + + return !!rv; +} + +#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \ + PARSE_OPT_KEEP_ARGV0 | \ + PARSE_OPT_KEEP_UNKNOWN | \ + PARSE_OPT_NO_INTERNAL_HELP ) + +int cmd_archive(int argc, const char **argv, const char *prefix) +{ + const char *exec = "git-upload-archive"; + const char *output = NULL; + const char *remote = NULL; + struct option local_opts[] = { + OPT_STRING('o', "output", &output, N_("file"), + N_("write the archive to this file")), + OPT_STRING(0, "remote", &remote, N_("repo"), + N_("retrieve the archive from remote repository <repo>")), + OPT_STRING(0, "exec", &exec, N_("command"), + N_("path to the remote git-upload-archive command")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, local_opts, NULL, + PARSE_OPT_KEEP_ALL); + + if (output) + create_output_file(output); + + if (remote) + return run_remote_archiver(argc, argv, remote, exec, output); + + setvbuf(stderr, NULL, _IOLBF, BUFSIZ); + + return write_archive(argc, argv, prefix, 1, output, 0); +} diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c new file mode 100644 index 0000000000..e3884e3bb6 --- /dev/null +++ b/builtin/bisect--helper.c @@ -0,0 +1,31 @@ +#include "builtin.h" +#include "cache.h" +#include "parse-options.h" +#include "bisect.h" + +static const char * const git_bisect_helper_usage[] = { + N_("git bisect--helper --next-all [--no-checkout]"), + NULL +}; + +int cmd_bisect__helper(int argc, const char **argv, const char *prefix) +{ + int next_all = 0; + int no_checkout = 0; + struct option options[] = { + OPT_BOOLEAN(0, "next-all", &next_all, + N_("perform 'git bisect next'")), + OPT_BOOLEAN(0, "no-checkout", &no_checkout, + N_("update BISECT_HEAD instead of checking out the current commit")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_bisect_helper_usage, 0); + + if (!next_all) + usage_with_options(git_bisect_helper_usage, options); + + /* next-all */ + return bisect_next_all(prefix, no_checkout); +} diff --git a/builtin/blame.c b/builtin/blame.c new file mode 100644 index 0000000000..b431ba3209 --- /dev/null +++ b/builtin/blame.c @@ -0,0 +1,2636 @@ +/* + * Blame + * + * Copyright (c) 2006, Junio C Hamano + */ + +#include "cache.h" +#include "builtin.h" +#include "blob.h" +#include "commit.h" +#include "tag.h" +#include "tree-walk.h" +#include "diff.h" +#include "diffcore.h" +#include "revision.h" +#include "quote.h" +#include "xdiff-interface.h" +#include "cache-tree.h" +#include "string-list.h" +#include "mailmap.h" +#include "parse-options.h" +#include "utf8.h" +#include "userdiff.h" + +static char blame_usage[] = N_("git blame [options] [rev-opts] [rev] [--] file"); + +static const char *blame_opt_usage[] = { + blame_usage, + "", + N_("[rev-opts] are documented in git-rev-list(1)"), + NULL +}; + +static int longest_file; +static int longest_author; +static int max_orig_digits; +static int max_digits; +static int max_score_digits; +static int show_root; +static int reverse; +static int blank_boundary; +static int incremental; +static int xdl_opts; +static int abbrev = -1; +static int no_whole_file_rename; + +static enum date_mode blame_date_mode = DATE_ISO8601; +static size_t blame_date_width; + +static struct string_list mailmap; + +#ifndef DEBUG +#define DEBUG 0 +#endif + +/* stats */ +static int num_read_blob; +static int num_get_patch; +static int num_commits; + +#define PICKAXE_BLAME_MOVE 01 +#define PICKAXE_BLAME_COPY 02 +#define PICKAXE_BLAME_COPY_HARDER 04 +#define PICKAXE_BLAME_COPY_HARDEST 010 + +/* + * blame for a blame_entry with score lower than these thresholds + * is not passed to the parent using move/copy logic. + */ +static unsigned blame_move_score; +static unsigned blame_copy_score; +#define BLAME_DEFAULT_MOVE_SCORE 20 +#define BLAME_DEFAULT_COPY_SCORE 40 + +/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ +#define METAINFO_SHOWN (1u<<12) +#define MORE_THAN_ONE_PATH (1u<<13) + +/* + * One blob in a commit that is being suspected + */ +struct origin { + int refcnt; + struct origin *previous; + struct commit *commit; + mmfile_t file; + unsigned char blob_sha1[20]; + unsigned mode; + char path[FLEX_ARRAY]; +}; + +static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen, + xdl_emit_hunk_consume_func_t hunk_func, void *cb_data) +{ + xpparam_t xpp = {0}; + xdemitconf_t xecfg = {0}; + xdemitcb_t ecb = {NULL}; + + xpp.flags = xdl_opts; + xecfg.ctxlen = ctxlen; + xecfg.hunk_func = hunk_func; + ecb.priv = cb_data; + return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); +} + +/* + * Prepare diff_filespec and convert it using diff textconv API + * if the textconv driver exists. + * Return 1 if the conversion succeeds, 0 otherwise. + */ +int textconv_object(const char *path, + unsigned mode, + const unsigned char *sha1, + int sha1_valid, + char **buf, + unsigned long *buf_size) +{ + struct diff_filespec *df; + struct userdiff_driver *textconv; + + df = alloc_filespec(path); + fill_filespec(df, sha1, sha1_valid, mode); + textconv = get_textconv(df); + if (!textconv) { + free_filespec(df); + return 0; + } + + *buf_size = fill_textconv(textconv, df, buf); + free_filespec(df); + return 1; +} + +/* + * Given an origin, prepare mmfile_t structure to be used by the + * diff machinery + */ +static void fill_origin_blob(struct diff_options *opt, + struct origin *o, mmfile_t *file) +{ + if (!o->file.ptr) { + enum object_type type; + unsigned long file_size; + + num_read_blob++; + if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) && + textconv_object(o->path, o->mode, o->blob_sha1, 1, &file->ptr, &file_size)) + ; + else + file->ptr = read_sha1_file(o->blob_sha1, &type, &file_size); + file->size = file_size; + + if (!file->ptr) + die("Cannot read blob %s for path %s", + sha1_to_hex(o->blob_sha1), + o->path); + o->file = *file; + } + else + *file = o->file; +} + +/* + * Origin is refcounted and usually we keep the blob contents to be + * reused. + */ +static inline struct origin *origin_incref(struct origin *o) +{ + if (o) + o->refcnt++; + return o; +} + +static void origin_decref(struct origin *o) +{ + if (o && --o->refcnt <= 0) { + if (o->previous) + origin_decref(o->previous); + free(o->file.ptr); + free(o); + } +} + +static void drop_origin_blob(struct origin *o) +{ + if (o->file.ptr) { + free(o->file.ptr); + o->file.ptr = NULL; + } +} + +/* + * Each group of lines is described by a blame_entry; it can be split + * as we pass blame to the parents. They form a linked list in the + * scoreboard structure, sorted by the target line number. + */ +struct blame_entry { + struct blame_entry *prev; + struct blame_entry *next; + + /* the first line of this group in the final image; + * internally all line numbers are 0 based. + */ + int lno; + + /* how many lines this group has */ + int num_lines; + + /* the commit that introduced this group into the final image */ + struct origin *suspect; + + /* true if the suspect is truly guilty; false while we have not + * checked if the group came from one of its parents. + */ + char guilty; + + /* true if the entry has been scanned for copies in the current parent + */ + char scanned; + + /* the line number of the first line of this group in the + * suspect's file; internally all line numbers are 0 based. + */ + int s_lno; + + /* how significant this entry is -- cached to avoid + * scanning the lines over and over. + */ + unsigned score; +}; + +/* + * The current state of the blame assignment. + */ +struct scoreboard { + /* the final commit (i.e. where we started digging from) */ + struct commit *final; + struct rev_info *revs; + const char *path; + + /* + * The contents in the final image. + * Used by many functions to obtain contents of the nth line, + * indexed with scoreboard.lineno[blame_entry.lno]. + */ + const char *final_buf; + unsigned long final_buf_size; + + /* linked list of blames */ + struct blame_entry *ent; + + /* look-up a line in the final buffer */ + int num_lines; + int *lineno; +}; + +static inline int same_suspect(struct origin *a, struct origin *b) +{ + if (a == b) + return 1; + if (a->commit != b->commit) + return 0; + return !strcmp(a->path, b->path); +} + +static void sanity_check_refcnt(struct scoreboard *); + +/* + * If two blame entries that are next to each other came from + * contiguous lines in the same origin (i.e. <commit, path> pair), + * merge them together. + */ +static void coalesce(struct scoreboard *sb) +{ + struct blame_entry *ent, *next; + + for (ent = sb->ent; ent && (next = ent->next); ent = next) { + if (same_suspect(ent->suspect, next->suspect) && + ent->guilty == next->guilty && + ent->s_lno + ent->num_lines == next->s_lno) { + ent->num_lines += next->num_lines; + ent->next = next->next; + if (ent->next) + ent->next->prev = ent; + origin_decref(next->suspect); + free(next); + ent->score = 0; + next = ent; /* again */ + } + } + + if (DEBUG) /* sanity */ + sanity_check_refcnt(sb); +} + +/* + * Given a commit and a path in it, create a new origin structure. + * The callers that add blame to the scoreboard should use + * get_origin() to obtain shared, refcounted copy instead of calling + * this function directly. + */ +static struct origin *make_origin(struct commit *commit, const char *path) +{ + struct origin *o; + o = xcalloc(1, sizeof(*o) + strlen(path) + 1); + o->commit = commit; + o->refcnt = 1; + strcpy(o->path, path); + return o; +} + +/* + * Locate an existing origin or create a new one. + */ +static struct origin *get_origin(struct scoreboard *sb, + struct commit *commit, + const char *path) +{ + struct blame_entry *e; + + for (e = sb->ent; e; e = e->next) { + if (e->suspect->commit == commit && + !strcmp(e->suspect->path, path)) + return origin_incref(e->suspect); + } + return make_origin(commit, path); +} + +/* + * Fill the blob_sha1 field of an origin if it hasn't, so that later + * call to fill_origin_blob() can use it to locate the data. blob_sha1 + * for an origin is also used to pass the blame for the entire file to + * the parent to detect the case where a child's blob is identical to + * that of its parent's. + * + * This also fills origin->mode for corresponding tree path. + */ +static int fill_blob_sha1_and_mode(struct origin *origin) +{ + if (!is_null_sha1(origin->blob_sha1)) + return 0; + if (get_tree_entry(origin->commit->object.sha1, + origin->path, + origin->blob_sha1, &origin->mode)) + goto error_out; + if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) + goto error_out; + return 0; + error_out: + hashclr(origin->blob_sha1); + origin->mode = S_IFINVALID; + return -1; +} + +/* + * We have an origin -- check if the same path exists in the + * parent and return an origin structure to represent it. + */ +static struct origin *find_origin(struct scoreboard *sb, + struct commit *parent, + struct origin *origin) +{ + struct origin *porigin = NULL; + struct diff_options diff_opts; + const char *paths[2]; + + if (parent->util) { + /* + * Each commit object can cache one origin in that + * commit. This is a freestanding copy of origin and + * not refcounted. + */ + struct origin *cached = parent->util; + if (!strcmp(cached->path, origin->path)) { + /* + * The same path between origin and its parent + * without renaming -- the most common case. + */ + porigin = get_origin(sb, parent, cached->path); + + /* + * If the origin was newly created (i.e. get_origin + * would call make_origin if none is found in the + * scoreboard), it does not know the blob_sha1/mode, + * so copy it. Otherwise porigin was in the + * scoreboard and already knows blob_sha1/mode. + */ + if (porigin->refcnt == 1) { + hashcpy(porigin->blob_sha1, cached->blob_sha1); + porigin->mode = cached->mode; + } + return porigin; + } + /* otherwise it was not very useful; free it */ + free(parent->util); + parent->util = NULL; + } + + /* See if the origin->path is different between parent + * and origin first. Most of the time they are the + * same and diff-tree is fairly efficient about this. + */ + diff_setup(&diff_opts); + DIFF_OPT_SET(&diff_opts, RECURSIVE); + diff_opts.detect_rename = 0; + diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; + paths[0] = origin->path; + paths[1] = NULL; + + diff_tree_setup_paths(paths, &diff_opts); + diff_setup_done(&diff_opts); + + if (is_null_sha1(origin->commit->object.sha1)) + do_diff_cache(parent->tree->object.sha1, &diff_opts); + else + diff_tree_sha1(parent->tree->object.sha1, + origin->commit->tree->object.sha1, + "", &diff_opts); + diffcore_std(&diff_opts); + + if (!diff_queued_diff.nr) { + /* The path is the same as parent */ + porigin = get_origin(sb, parent, origin->path); + hashcpy(porigin->blob_sha1, origin->blob_sha1); + porigin->mode = origin->mode; + } else { + /* + * Since origin->path is a pathspec, if the parent + * commit had it as a directory, we will see a whole + * bunch of deletion of files in the directory that we + * do not care about. + */ + int i; + struct diff_filepair *p = NULL; + for (i = 0; i < diff_queued_diff.nr; i++) { + const char *name; + p = diff_queued_diff.queue[i]; + name = p->one->path ? p->one->path : p->two->path; + if (!strcmp(name, origin->path)) + break; + } + if (!p) + die("internal error in blame::find_origin"); + switch (p->status) { + default: + die("internal error in blame::find_origin (%c)", + p->status); + case 'M': + porigin = get_origin(sb, parent, origin->path); + hashcpy(porigin->blob_sha1, p->one->sha1); + porigin->mode = p->one->mode; + break; + case 'A': + case 'T': + /* Did not exist in parent, or type changed */ + break; + } + } + diff_flush(&diff_opts); + diff_tree_release_paths(&diff_opts); + if (porigin) { + /* + * Create a freestanding copy that is not part of + * the refcounted origin found in the scoreboard, and + * cache it in the commit. + */ + struct origin *cached; + + cached = make_origin(porigin->commit, porigin->path); + hashcpy(cached->blob_sha1, porigin->blob_sha1); + cached->mode = porigin->mode; + parent->util = cached; + } + return porigin; +} + +/* + * We have an origin -- find the path that corresponds to it in its + * parent and return an origin structure to represent it. + */ +static struct origin *find_rename(struct scoreboard *sb, + struct commit *parent, + struct origin *origin) +{ + struct origin *porigin = NULL; + struct diff_options diff_opts; + int i; + const char *paths[2]; + + diff_setup(&diff_opts); + DIFF_OPT_SET(&diff_opts, RECURSIVE); + diff_opts.detect_rename = DIFF_DETECT_RENAME; + diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; + diff_opts.single_follow = origin->path; + paths[0] = NULL; + diff_tree_setup_paths(paths, &diff_opts); + diff_setup_done(&diff_opts); + + if (is_null_sha1(origin->commit->object.sha1)) + do_diff_cache(parent->tree->object.sha1, &diff_opts); + else + diff_tree_sha1(parent->tree->object.sha1, + origin->commit->tree->object.sha1, + "", &diff_opts); + diffcore_std(&diff_opts); + + for (i = 0; i < diff_queued_diff.nr; i++) { + struct diff_filepair *p = diff_queued_diff.queue[i]; + if ((p->status == 'R' || p->status == 'C') && + !strcmp(p->two->path, origin->path)) { + porigin = get_origin(sb, parent, p->one->path); + hashcpy(porigin->blob_sha1, p->one->sha1); + porigin->mode = p->one->mode; + break; + } + } + diff_flush(&diff_opts); + diff_tree_release_paths(&diff_opts); + return porigin; +} + +/* + * Link in a new blame entry to the scoreboard. Entries that cover the + * same line range have been removed from the scoreboard previously. + */ +static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) +{ + struct blame_entry *ent, *prev = NULL; + + origin_incref(e->suspect); + + for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) + prev = ent; + + /* prev, if not NULL, is the last one that is below e */ + e->prev = prev; + if (prev) { + e->next = prev->next; + prev->next = e; + } + else { + e->next = sb->ent; + sb->ent = e; + } + if (e->next) + e->next->prev = e; +} + +/* + * src typically is on-stack; we want to copy the information in it to + * a malloced blame_entry that is already on the linked list of the + * scoreboard. The origin of dst loses a refcnt while the origin of src + * gains one. + */ +static void dup_entry(struct blame_entry *dst, struct blame_entry *src) +{ + struct blame_entry *p, *n; + + p = dst->prev; + n = dst->next; + origin_incref(src->suspect); + origin_decref(dst->suspect); + memcpy(dst, src, sizeof(*src)); + dst->prev = p; + dst->next = n; + dst->score = 0; +} + +static const char *nth_line(struct scoreboard *sb, int lno) +{ + return sb->final_buf + sb->lineno[lno]; +} + +/* + * It is known that lines between tlno to same came from parent, and e + * has an overlap with that range. it also is known that parent's + * line plno corresponds to e's line tlno. + * + * <---- e -----> + * <------> + * <------------> + * <------------> + * <------------------> + * + * Split e into potentially three parts; before this chunk, the chunk + * to be blamed for the parent, and after that portion. + */ +static void split_overlap(struct blame_entry *split, + struct blame_entry *e, + int tlno, int plno, int same, + struct origin *parent) +{ + int chunk_end_lno; + memset(split, 0, sizeof(struct blame_entry [3])); + + if (e->s_lno < tlno) { + /* there is a pre-chunk part not blamed on parent */ + split[0].suspect = origin_incref(e->suspect); + split[0].lno = e->lno; + split[0].s_lno = e->s_lno; + split[0].num_lines = tlno - e->s_lno; + split[1].lno = e->lno + tlno - e->s_lno; + split[1].s_lno = plno; + } + else { + split[1].lno = e->lno; + split[1].s_lno = plno + (e->s_lno - tlno); + } + + if (same < e->s_lno + e->num_lines) { + /* there is a post-chunk part not blamed on parent */ + split[2].suspect = origin_incref(e->suspect); + split[2].lno = e->lno + (same - e->s_lno); + split[2].s_lno = e->s_lno + (same - e->s_lno); + split[2].num_lines = e->s_lno + e->num_lines - same; + chunk_end_lno = split[2].lno; + } + else + chunk_end_lno = e->lno + e->num_lines; + split[1].num_lines = chunk_end_lno - split[1].lno; + + /* + * if it turns out there is nothing to blame the parent for, + * forget about the splitting. !split[1].suspect signals this. + */ + if (split[1].num_lines < 1) + return; + split[1].suspect = origin_incref(parent); +} + +/* + * split_overlap() divided an existing blame e into up to three parts + * in split. Adjust the linked list of blames in the scoreboard to + * reflect the split. + */ +static void split_blame(struct scoreboard *sb, + struct blame_entry *split, + struct blame_entry *e) +{ + struct blame_entry *new_entry; + + if (split[0].suspect && split[2].suspect) { + /* The first part (reuse storage for the existing entry e) */ + dup_entry(e, &split[0]); + + /* The last part -- me */ + new_entry = xmalloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + + /* ... and the middle part -- parent */ + new_entry = xmalloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + } + else if (!split[0].suspect && !split[2].suspect) + /* + * The parent covers the entire area; reuse storage for + * e and replace it with the parent. + */ + dup_entry(e, &split[1]); + else if (split[0].suspect) { + /* me and then parent */ + dup_entry(e, &split[0]); + + new_entry = xmalloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + } + else { + /* parent and then me */ + dup_entry(e, &split[1]); + + new_entry = xmalloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + } + + if (DEBUG) { /* sanity */ + struct blame_entry *ent; + int lno = sb->ent->lno, corrupt = 0; + + for (ent = sb->ent; ent; ent = ent->next) { + if (lno != ent->lno) + corrupt = 1; + if (ent->s_lno < 0) + corrupt = 1; + lno += ent->num_lines; + } + if (corrupt) { + lno = sb->ent->lno; + for (ent = sb->ent; ent; ent = ent->next) { + printf("L %8d l %8d n %8d\n", + lno, ent->lno, ent->num_lines); + lno = ent->lno + ent->num_lines; + } + die("oops"); + } + } +} + +/* + * After splitting the blame, the origins used by the + * on-stack blame_entry should lose one refcnt each. + */ +static void decref_split(struct blame_entry *split) +{ + int i; + + for (i = 0; i < 3; i++) + origin_decref(split[i].suspect); +} + +/* + * Helper for blame_chunk(). blame_entry e is known to overlap with + * the patch hunk; split it and pass blame to the parent. + */ +static void blame_overlap(struct scoreboard *sb, struct blame_entry *e, + int tlno, int plno, int same, + struct origin *parent) +{ + struct blame_entry split[3]; + + split_overlap(split, e, tlno, plno, same, parent); + if (split[1].suspect) + split_blame(sb, split, e); + decref_split(split); +} + +/* + * Find the line number of the last line the target is suspected for. + */ +static int find_last_in_target(struct scoreboard *sb, struct origin *target) +{ + struct blame_entry *e; + int last_in_target = -1; + + for (e = sb->ent; e; e = e->next) { + if (e->guilty || !same_suspect(e->suspect, target)) + continue; + if (last_in_target < e->s_lno + e->num_lines) + last_in_target = e->s_lno + e->num_lines; + } + return last_in_target; +} + +/* + * Process one hunk from the patch between the current suspect for + * blame_entry e and its parent. Find and split the overlap, and + * pass blame to the overlapping part to the parent. + */ +static void blame_chunk(struct scoreboard *sb, + int tlno, int plno, int same, + struct origin *target, struct origin *parent) +{ + struct blame_entry *e; + + for (e = sb->ent; e; e = e->next) { + if (e->guilty || !same_suspect(e->suspect, target)) + continue; + if (same <= e->s_lno) + continue; + if (tlno < e->s_lno + e->num_lines) + blame_overlap(sb, e, tlno, plno, same, parent); + } +} + +struct blame_chunk_cb_data { + struct scoreboard *sb; + struct origin *target; + struct origin *parent; + long plno; + long tlno; +}; + +static int blame_chunk_cb(long start_a, long count_a, + long start_b, long count_b, void *data) +{ + struct blame_chunk_cb_data *d = data; + blame_chunk(d->sb, d->tlno, d->plno, start_b, d->target, d->parent); + d->plno = start_a + count_a; + d->tlno = start_b + count_b; + return 0; +} + +/* + * We are looking at the origin 'target' and aiming to pass blame + * for the lines it is suspected to its parent. Run diff to find + * which lines came from parent and pass blame for them. + */ +static int pass_blame_to_parent(struct scoreboard *sb, + struct origin *target, + struct origin *parent) +{ + int last_in_target; + mmfile_t file_p, file_o; + struct blame_chunk_cb_data d; + + memset(&d, 0, sizeof(d)); + d.sb = sb; d.target = target; d.parent = parent; + last_in_target = find_last_in_target(sb, target); + if (last_in_target < 0) + return 1; /* nothing remains for this target */ + + fill_origin_blob(&sb->revs->diffopt, parent, &file_p); + fill_origin_blob(&sb->revs->diffopt, target, &file_o); + num_get_patch++; + + diff_hunks(&file_p, &file_o, 0, blame_chunk_cb, &d); + /* The rest (i.e. anything after tlno) are the same as the parent */ + blame_chunk(sb, d.tlno, d.plno, last_in_target, target, parent); + + return 0; +} + +/* + * The lines in blame_entry after splitting blames many times can become + * very small and trivial, and at some point it becomes pointless to + * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any + * ordinary C program, and it is not worth to say it was copied from + * totally unrelated file in the parent. + * + * Compute how trivial the lines in the blame_entry are. + */ +static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e) +{ + unsigned score; + const char *cp, *ep; + + if (e->score) + return e->score; + + score = 1; + cp = nth_line(sb, e->lno); + ep = nth_line(sb, e->lno + e->num_lines); + while (cp < ep) { + unsigned ch = *((unsigned char *)cp); + if (isalnum(ch)) + score++; + cp++; + } + e->score = score; + return score; +} + +/* + * best_so_far[] and this[] are both a split of an existing blame_entry + * that passes blame to the parent. Maintain best_so_far the best split + * so far, by comparing this and best_so_far and copying this into + * bst_so_far as needed. + */ +static void copy_split_if_better(struct scoreboard *sb, + struct blame_entry *best_so_far, + struct blame_entry *this) +{ + int i; + + if (!this[1].suspect) + return; + if (best_so_far[1].suspect) { + if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1])) + return; + } + + for (i = 0; i < 3; i++) + origin_incref(this[i].suspect); + decref_split(best_so_far); + memcpy(best_so_far, this, sizeof(struct blame_entry [3])); +} + +/* + * We are looking at a part of the final image represented by + * ent (tlno and same are offset by ent->s_lno). + * tlno is where we are looking at in the final image. + * up to (but not including) same match preimage. + * plno is where we are looking at in the preimage. + * + * <-------------- final image ----------------------> + * <------ent------> + * ^tlno ^same + * <---------preimage-----> + * ^plno + * + * All line numbers are 0-based. + */ +static void handle_split(struct scoreboard *sb, + struct blame_entry *ent, + int tlno, int plno, int same, + struct origin *parent, + struct blame_entry *split) +{ + if (ent->num_lines <= tlno) + return; + if (tlno < same) { + struct blame_entry this[3]; + tlno += ent->s_lno; + same += ent->s_lno; + split_overlap(this, ent, tlno, plno, same, parent); + copy_split_if_better(sb, split, this); + decref_split(this); + } +} + +struct handle_split_cb_data { + struct scoreboard *sb; + struct blame_entry *ent; + struct origin *parent; + struct blame_entry *split; + long plno; + long tlno; +}; + +static int handle_split_cb(long start_a, long count_a, + long start_b, long count_b, void *data) +{ + struct handle_split_cb_data *d = data; + handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent, + d->split); + d->plno = start_a + count_a; + d->tlno = start_b + count_b; + return 0; +} + +/* + * Find the lines from parent that are the same as ent so that + * we can pass blames to it. file_p has the blob contents for + * the parent. + */ +static void find_copy_in_blob(struct scoreboard *sb, + struct blame_entry *ent, + struct origin *parent, + struct blame_entry *split, + mmfile_t *file_p) +{ + const char *cp; + int cnt; + mmfile_t file_o; + struct handle_split_cb_data d; + + memset(&d, 0, sizeof(d)); + d.sb = sb; d.ent = ent; d.parent = parent; d.split = split; + /* + * Prepare mmfile that contains only the lines in ent. + */ + cp = nth_line(sb, ent->lno); + file_o.ptr = (char *) cp; + cnt = ent->num_lines; + + while (cnt && cp < sb->final_buf + sb->final_buf_size) { + if (*cp++ == '\n') + cnt--; + } + file_o.size = cp - file_o.ptr; + + /* + * file_o is a part of final image we are annotating. + * file_p partially may match that image. + */ + memset(split, 0, sizeof(struct blame_entry [3])); + diff_hunks(file_p, &file_o, 1, handle_split_cb, &d); + /* remainder, if any, all match the preimage */ + handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split); +} + +/* + * See if lines currently target is suspected for can be attributed to + * parent. + */ +static int find_move_in_parent(struct scoreboard *sb, + struct origin *target, + struct origin *parent) +{ + int last_in_target, made_progress; + struct blame_entry *e, split[3]; + mmfile_t file_p; + + last_in_target = find_last_in_target(sb, target); + if (last_in_target < 0) + return 1; /* nothing remains for this target */ + + fill_origin_blob(&sb->revs->diffopt, parent, &file_p); + if (!file_p.ptr) + return 0; + + made_progress = 1; + while (made_progress) { + made_progress = 0; + for (e = sb->ent; e; e = e->next) { + if (e->guilty || !same_suspect(e->suspect, target) || + ent_score(sb, e) < blame_move_score) + continue; + find_copy_in_blob(sb, e, parent, split, &file_p); + if (split[1].suspect && + blame_move_score < ent_score(sb, &split[1])) { + split_blame(sb, split, e); + made_progress = 1; + } + decref_split(split); + } + } + return 0; +} + +struct blame_list { + struct blame_entry *ent; + struct blame_entry split[3]; +}; + +/* + * Count the number of entries the target is suspected for, + * and prepare a list of entry and the best split. + */ +static struct blame_list *setup_blame_list(struct scoreboard *sb, + struct origin *target, + int min_score, + int *num_ents_p) +{ + struct blame_entry *e; + int num_ents, i; + struct blame_list *blame_list = NULL; + + for (e = sb->ent, num_ents = 0; e; e = e->next) + if (!e->scanned && !e->guilty && + same_suspect(e->suspect, target) && + min_score < ent_score(sb, e)) + num_ents++; + if (num_ents) { + blame_list = xcalloc(num_ents, sizeof(struct blame_list)); + for (e = sb->ent, i = 0; e; e = e->next) + if (!e->scanned && !e->guilty && + same_suspect(e->suspect, target) && + min_score < ent_score(sb, e)) + blame_list[i++].ent = e; + } + *num_ents_p = num_ents; + return blame_list; +} + +/* + * Reset the scanned status on all entries. + */ +static void reset_scanned_flag(struct scoreboard *sb) +{ + struct blame_entry *e; + for (e = sb->ent; e; e = e->next) + e->scanned = 0; +} + +/* + * For lines target is suspected for, see if we can find code movement + * across file boundary from the parent commit. porigin is the path + * in the parent we already tried. + */ +static int find_copy_in_parent(struct scoreboard *sb, + struct origin *target, + struct commit *parent, + struct origin *porigin, + int opt) +{ + struct diff_options diff_opts; + const char *paths[1]; + int i, j; + int retval; + struct blame_list *blame_list; + int num_ents; + + blame_list = setup_blame_list(sb, target, blame_copy_score, &num_ents); + if (!blame_list) + return 1; /* nothing remains for this target */ + + diff_setup(&diff_opts); + DIFF_OPT_SET(&diff_opts, RECURSIVE); + diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; + + paths[0] = NULL; + diff_tree_setup_paths(paths, &diff_opts); + diff_setup_done(&diff_opts); + + /* Try "find copies harder" on new path if requested; + * we do not want to use diffcore_rename() actually to + * match things up; find_copies_harder is set only to + * force diff_tree_sha1() to feed all filepairs to diff_queue, + * and this code needs to be after diff_setup_done(), which + * usually makes find-copies-harder imply copy detection. + */ + if ((opt & PICKAXE_BLAME_COPY_HARDEST) + || ((opt & PICKAXE_BLAME_COPY_HARDER) + && (!porigin || strcmp(target->path, porigin->path)))) + DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); + + if (is_null_sha1(target->commit->object.sha1)) + do_diff_cache(parent->tree->object.sha1, &diff_opts); + else + diff_tree_sha1(parent->tree->object.sha1, + target->commit->tree->object.sha1, + "", &diff_opts); + + if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER)) + diffcore_std(&diff_opts); + + retval = 0; + while (1) { + int made_progress = 0; + + for (i = 0; i < diff_queued_diff.nr; i++) { + struct diff_filepair *p = diff_queued_diff.queue[i]; + struct origin *norigin; + mmfile_t file_p; + struct blame_entry this[3]; + + if (!DIFF_FILE_VALID(p->one)) + continue; /* does not exist in parent */ + if (S_ISGITLINK(p->one->mode)) + continue; /* ignore git links */ + if (porigin && !strcmp(p->one->path, porigin->path)) + /* find_move already dealt with this path */ + continue; + + norigin = get_origin(sb, parent, p->one->path); + hashcpy(norigin->blob_sha1, p->one->sha1); + norigin->mode = p->one->mode; + fill_origin_blob(&sb->revs->diffopt, norigin, &file_p); + if (!file_p.ptr) + continue; + + for (j = 0; j < num_ents; j++) { + find_copy_in_blob(sb, blame_list[j].ent, + norigin, this, &file_p); + copy_split_if_better(sb, blame_list[j].split, + this); + decref_split(this); + } + origin_decref(norigin); + } + + for (j = 0; j < num_ents; j++) { + struct blame_entry *split = blame_list[j].split; + if (split[1].suspect && + blame_copy_score < ent_score(sb, &split[1])) { + split_blame(sb, split, blame_list[j].ent); + made_progress = 1; + } + else + blame_list[j].ent->scanned = 1; + decref_split(split); + } + free(blame_list); + + if (!made_progress) + break; + blame_list = setup_blame_list(sb, target, blame_copy_score, &num_ents); + if (!blame_list) { + retval = 1; + break; + } + } + reset_scanned_flag(sb); + diff_flush(&diff_opts); + diff_tree_release_paths(&diff_opts); + return retval; +} + +/* + * The blobs of origin and porigin exactly match, so everything + * origin is suspected for can be blamed on the parent. + */ +static void pass_whole_blame(struct scoreboard *sb, + struct origin *origin, struct origin *porigin) +{ + struct blame_entry *e; + + if (!porigin->file.ptr && origin->file.ptr) { + /* Steal its file */ + porigin->file = origin->file; + origin->file.ptr = NULL; + } + for (e = sb->ent; e; e = e->next) { + if (!same_suspect(e->suspect, origin)) + continue; + origin_incref(porigin); + origin_decref(e->suspect); + e->suspect = porigin; + } +} + +/* + * We pass blame from the current commit to its parents. We keep saying + * "parent" (and "porigin"), but what we mean is to find scapegoat to + * exonerate ourselves. + */ +static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit) +{ + if (!reverse) + return commit->parents; + return lookup_decoration(&revs->children, &commit->object); +} + +static int num_scapegoats(struct rev_info *revs, struct commit *commit) +{ + int cnt; + struct commit_list *l = first_scapegoat(revs, commit); + for (cnt = 0; l; l = l->next) + cnt++; + return cnt; +} + +#define MAXSG 16 + +static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt) +{ + struct rev_info *revs = sb->revs; + int i, pass, num_sg; + struct commit *commit = origin->commit; + struct commit_list *sg; + struct origin *sg_buf[MAXSG]; + struct origin *porigin, **sg_origin = sg_buf; + + num_sg = num_scapegoats(revs, commit); + if (!num_sg) + goto finish; + else if (num_sg < ARRAY_SIZE(sg_buf)) + memset(sg_buf, 0, sizeof(sg_buf)); + else + sg_origin = xcalloc(num_sg, sizeof(*sg_origin)); + + /* + * The first pass looks for unrenamed path to optimize for + * common cases, then we look for renames in the second pass. + */ + for (pass = 0; pass < 2 - no_whole_file_rename; pass++) { + struct origin *(*find)(struct scoreboard *, + struct commit *, struct origin *); + find = pass ? find_rename : find_origin; + + for (i = 0, sg = first_scapegoat(revs, commit); + i < num_sg && sg; + sg = sg->next, i++) { + struct commit *p = sg->item; + int j, same; + + if (sg_origin[i]) + continue; + if (parse_commit(p)) + continue; + porigin = find(sb, p, origin); + if (!porigin) + continue; + if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) { + pass_whole_blame(sb, origin, porigin); + origin_decref(porigin); + goto finish; + } + for (j = same = 0; j < i; j++) + if (sg_origin[j] && + !hashcmp(sg_origin[j]->blob_sha1, + porigin->blob_sha1)) { + same = 1; + break; + } + if (!same) + sg_origin[i] = porigin; + else + origin_decref(porigin); + } + } + + num_commits++; + for (i = 0, sg = first_scapegoat(revs, commit); + i < num_sg && sg; + sg = sg->next, i++) { + struct origin *porigin = sg_origin[i]; + if (!porigin) + continue; + if (!origin->previous) { + origin_incref(porigin); + origin->previous = porigin; + } + if (pass_blame_to_parent(sb, origin, porigin)) + goto finish; + } + + /* + * Optionally find moves in parents' files. + */ + if (opt & PICKAXE_BLAME_MOVE) + for (i = 0, sg = first_scapegoat(revs, commit); + i < num_sg && sg; + sg = sg->next, i++) { + struct origin *porigin = sg_origin[i]; + if (!porigin) + continue; + if (find_move_in_parent(sb, origin, porigin)) + goto finish; + } + + /* + * Optionally find copies from parents' files. + */ + if (opt & PICKAXE_BLAME_COPY) + for (i = 0, sg = first_scapegoat(revs, commit); + i < num_sg && sg; + sg = sg->next, i++) { + struct origin *porigin = sg_origin[i]; + if (find_copy_in_parent(sb, origin, sg->item, + porigin, opt)) + goto finish; + } + + finish: + for (i = 0; i < num_sg; i++) { + if (sg_origin[i]) { + drop_origin_blob(sg_origin[i]); + origin_decref(sg_origin[i]); + } + } + drop_origin_blob(origin); + if (sg_buf != sg_origin) + free(sg_origin); +} + +/* + * Information on commits, used for output. + */ +struct commit_info { + struct strbuf author; + struct strbuf author_mail; + unsigned long author_time; + struct strbuf author_tz; + + /* filled only when asked for details */ + struct strbuf committer; + struct strbuf committer_mail; + unsigned long committer_time; + struct strbuf committer_tz; + + struct strbuf summary; +}; + +/* + * Parse author/committer line in the commit object buffer + */ +static void get_ac_line(const char *inbuf, const char *what, + struct strbuf *name, struct strbuf *mail, + unsigned long *time, struct strbuf *tz) +{ + struct ident_split ident; + size_t len, maillen, namelen; + char *tmp, *endp; + const char *namebuf, *mailbuf; + + tmp = strstr(inbuf, what); + if (!tmp) + goto error_out; + tmp += strlen(what); + endp = strchr(tmp, '\n'); + if (!endp) + len = strlen(tmp); + else + len = endp - tmp; + + if (split_ident_line(&ident, tmp, len)) { + error_out: + /* Ugh */ + tmp = "(unknown)"; + strbuf_addstr(name, tmp); + strbuf_addstr(mail, tmp); + strbuf_addstr(tz, tmp); + *time = 0; + return; + } + + namelen = ident.name_end - ident.name_begin; + namebuf = ident.name_begin; + + maillen = ident.mail_end - ident.mail_begin; + mailbuf = ident.mail_begin; + + *time = strtoul(ident.date_begin, NULL, 10); + + len = ident.tz_end - ident.tz_begin; + strbuf_add(tz, ident.tz_begin, len); + + /* + * Now, convert both name and e-mail using mailmap + */ + map_user(&mailmap, &mailbuf, &maillen, + &namebuf, &namelen); + + strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf); + strbuf_add(name, namebuf, namelen); +} + +static void commit_info_init(struct commit_info *ci) +{ + + strbuf_init(&ci->author, 0); + strbuf_init(&ci->author_mail, 0); + strbuf_init(&ci->author_tz, 0); + strbuf_init(&ci->committer, 0); + strbuf_init(&ci->committer_mail, 0); + strbuf_init(&ci->committer_tz, 0); + strbuf_init(&ci->summary, 0); +} + +static void commit_info_destroy(struct commit_info *ci) +{ + + strbuf_release(&ci->author); + strbuf_release(&ci->author_mail); + strbuf_release(&ci->author_tz); + strbuf_release(&ci->committer); + strbuf_release(&ci->committer_mail); + strbuf_release(&ci->committer_tz); + strbuf_release(&ci->summary); +} + +static void get_commit_info(struct commit *commit, + struct commit_info *ret, + int detailed) +{ + int len; + const char *subject, *encoding; + char *reencoded, *message; + + commit_info_init(ret); + + /* + * We've operated without save_commit_buffer, so + * we now need to populate them for output. + */ + if (!commit->buffer) { + enum object_type type; + unsigned long size; + commit->buffer = + read_sha1_file(commit->object.sha1, &type, &size); + if (!commit->buffer) + die("Cannot read commit %s", + sha1_to_hex(commit->object.sha1)); + } + encoding = get_log_output_encoding(); + reencoded = logmsg_reencode(commit, encoding); + message = reencoded ? reencoded : commit->buffer; + get_ac_line(message, "\nauthor ", + &ret->author, &ret->author_mail, + &ret->author_time, &ret->author_tz); + + if (!detailed) { + free(reencoded); + return; + } + + get_ac_line(message, "\ncommitter ", + &ret->committer, &ret->committer_mail, + &ret->committer_time, &ret->committer_tz); + + len = find_commit_subject(message, &subject); + if (len) + strbuf_add(&ret->summary, subject, len); + else + strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1)); + + free(reencoded); +} + +/* + * To allow LF and other nonportable characters in pathnames, + * they are c-style quoted as needed. + */ +static void write_filename_info(const char *path) +{ + printf("filename "); + write_name_quoted(path, stdout, '\n'); +} + +/* + * Porcelain/Incremental format wants to show a lot of details per + * commit. Instead of repeating this every line, emit it only once, + * the first time each commit appears in the output (unless the + * user has specifically asked for us to repeat). + */ +static int emit_one_suspect_detail(struct origin *suspect, int repeat) +{ + struct commit_info ci; + + if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN)) + return 0; + + suspect->commit->object.flags |= METAINFO_SHOWN; + get_commit_info(suspect->commit, &ci, 1); + printf("author %s\n", ci.author.buf); + printf("author-mail %s\n", ci.author_mail.buf); + printf("author-time %lu\n", ci.author_time); + printf("author-tz %s\n", ci.author_tz.buf); + printf("committer %s\n", ci.committer.buf); + printf("committer-mail %s\n", ci.committer_mail.buf); + printf("committer-time %lu\n", ci.committer_time); + printf("committer-tz %s\n", ci.committer_tz.buf); + printf("summary %s\n", ci.summary.buf); + if (suspect->commit->object.flags & UNINTERESTING) + printf("boundary\n"); + if (suspect->previous) { + struct origin *prev = suspect->previous; + printf("previous %s ", sha1_to_hex(prev->commit->object.sha1)); + write_name_quoted(prev->path, stdout, '\n'); + } + + commit_info_destroy(&ci); + + return 1; +} + +/* + * The blame_entry is found to be guilty for the range. Mark it + * as such, and show it in incremental output. + */ +static void found_guilty_entry(struct blame_entry *ent) +{ + if (ent->guilty) + return; + ent->guilty = 1; + if (incremental) { + struct origin *suspect = ent->suspect; + + printf("%s %d %d %d\n", + sha1_to_hex(suspect->commit->object.sha1), + ent->s_lno + 1, ent->lno + 1, ent->num_lines); + emit_one_suspect_detail(suspect, 0); + write_filename_info(suspect->path); + maybe_flush_or_die(stdout, "stdout"); + } +} + +/* + * The main loop -- while the scoreboard has lines whose true origin + * is still unknown, pick one blame_entry, and allow its current + * suspect to pass blames to its parents. + */ +static void assign_blame(struct scoreboard *sb, int opt) +{ + struct rev_info *revs = sb->revs; + + while (1) { + struct blame_entry *ent; + struct commit *commit; + struct origin *suspect = NULL; + + /* find one suspect to break down */ + for (ent = sb->ent; !suspect && ent; ent = ent->next) + if (!ent->guilty) + suspect = ent->suspect; + if (!suspect) + return; /* all done */ + + /* + * We will use this suspect later in the loop, + * so hold onto it in the meantime. + */ + origin_incref(suspect); + commit = suspect->commit; + if (!commit->object.parsed) + parse_commit(commit); + if (reverse || + (!(commit->object.flags & UNINTERESTING) && + !(revs->max_age != -1 && commit->date < revs->max_age))) + pass_blame(sb, suspect, opt); + else { + commit->object.flags |= UNINTERESTING; + if (commit->object.parsed) + mark_parents_uninteresting(commit); + } + /* treat root commit as boundary */ + if (!commit->parents && !show_root) + commit->object.flags |= UNINTERESTING; + + /* Take responsibility for the remaining entries */ + for (ent = sb->ent; ent; ent = ent->next) + if (same_suspect(ent->suspect, suspect)) + found_guilty_entry(ent); + origin_decref(suspect); + + if (DEBUG) /* sanity */ + sanity_check_refcnt(sb); + } +} + +static const char *format_time(unsigned long time, const char *tz_str, + int show_raw_time) +{ + static char time_buf[128]; + const char *time_str; + int time_len; + int tz; + + if (show_raw_time) { + snprintf(time_buf, sizeof(time_buf), "%lu %s", time, tz_str); + } + else { + tz = atoi(tz_str); + time_str = show_date(time, tz, blame_date_mode); + time_len = strlen(time_str); + memcpy(time_buf, time_str, time_len); + memset(time_buf + time_len, ' ', blame_date_width - time_len); + } + return time_buf; +} + +#define OUTPUT_ANNOTATE_COMPAT 001 +#define OUTPUT_LONG_OBJECT_NAME 002 +#define OUTPUT_RAW_TIMESTAMP 004 +#define OUTPUT_PORCELAIN 010 +#define OUTPUT_SHOW_NAME 020 +#define OUTPUT_SHOW_NUMBER 040 +#define OUTPUT_SHOW_SCORE 0100 +#define OUTPUT_NO_AUTHOR 0200 +#define OUTPUT_SHOW_EMAIL 0400 +#define OUTPUT_LINE_PORCELAIN 01000 + +static void emit_porcelain_details(struct origin *suspect, int repeat) +{ + if (emit_one_suspect_detail(suspect, repeat) || + (suspect->commit->object.flags & MORE_THAN_ONE_PATH)) + write_filename_info(suspect->path); +} + +static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent, + int opt) +{ + int repeat = opt & OUTPUT_LINE_PORCELAIN; + int cnt; + const char *cp; + struct origin *suspect = ent->suspect; + char hex[41]; + + strcpy(hex, sha1_to_hex(suspect->commit->object.sha1)); + printf("%s%c%d %d %d\n", + hex, + ent->guilty ? ' ' : '*', /* purely for debugging */ + ent->s_lno + 1, + ent->lno + 1, + ent->num_lines); + emit_porcelain_details(suspect, repeat); + + cp = nth_line(sb, ent->lno); + for (cnt = 0; cnt < ent->num_lines; cnt++) { + char ch; + if (cnt) { + printf("%s %d %d\n", hex, + ent->s_lno + 1 + cnt, + ent->lno + 1 + cnt); + if (repeat) + emit_porcelain_details(suspect, 1); + } + putchar('\t'); + do { + ch = *cp++; + putchar(ch); + } while (ch != '\n' && + cp < sb->final_buf + sb->final_buf_size); + } + + if (sb->final_buf_size && cp[-1] != '\n') + putchar('\n'); +} + +static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt) +{ + int cnt; + const char *cp; + struct origin *suspect = ent->suspect; + struct commit_info ci; + char hex[41]; + int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP); + + get_commit_info(suspect->commit, &ci, 1); + strcpy(hex, sha1_to_hex(suspect->commit->object.sha1)); + + cp = nth_line(sb, ent->lno); + for (cnt = 0; cnt < ent->num_lines; cnt++) { + char ch; + int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : abbrev; + + if (suspect->commit->object.flags & UNINTERESTING) { + if (blank_boundary) + memset(hex, ' ', length); + else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) { + length--; + putchar('^'); + } + } + + printf("%.*s", length, hex); + if (opt & OUTPUT_ANNOTATE_COMPAT) { + const char *name; + if (opt & OUTPUT_SHOW_EMAIL) + name = ci.author_mail.buf; + else + name = ci.author.buf; + printf("\t(%10s\t%10s\t%d)", name, + format_time(ci.author_time, ci.author_tz.buf, + show_raw_time), + ent->lno + 1 + cnt); + } else { + if (opt & OUTPUT_SHOW_SCORE) + printf(" %*d %02d", + max_score_digits, ent->score, + ent->suspect->refcnt); + if (opt & OUTPUT_SHOW_NAME) + printf(" %-*.*s", longest_file, longest_file, + suspect->path); + if (opt & OUTPUT_SHOW_NUMBER) + printf(" %*d", max_orig_digits, + ent->s_lno + 1 + cnt); + + if (!(opt & OUTPUT_NO_AUTHOR)) { + const char *name; + int pad; + if (opt & OUTPUT_SHOW_EMAIL) + name = ci.author_mail.buf; + else + name = ci.author.buf; + pad = longest_author - utf8_strwidth(name); + printf(" (%s%*s %10s", + name, pad, "", + format_time(ci.author_time, + ci.author_tz.buf, + show_raw_time)); + } + printf(" %*d) ", + max_digits, ent->lno + 1 + cnt); + } + do { + ch = *cp++; + putchar(ch); + } while (ch != '\n' && + cp < sb->final_buf + sb->final_buf_size); + } + + if (sb->final_buf_size && cp[-1] != '\n') + putchar('\n'); + + commit_info_destroy(&ci); +} + +static void output(struct scoreboard *sb, int option) +{ + struct blame_entry *ent; + + if (option & OUTPUT_PORCELAIN) { + for (ent = sb->ent; ent; ent = ent->next) { + struct blame_entry *oth; + struct origin *suspect = ent->suspect; + struct commit *commit = suspect->commit; + if (commit->object.flags & MORE_THAN_ONE_PATH) + continue; + for (oth = ent->next; oth; oth = oth->next) { + if ((oth->suspect->commit != commit) || + !strcmp(oth->suspect->path, suspect->path)) + continue; + commit->object.flags |= MORE_THAN_ONE_PATH; + break; + } + } + } + + for (ent = sb->ent; ent; ent = ent->next) { + if (option & OUTPUT_PORCELAIN) + emit_porcelain(sb, ent, option); + else { + emit_other(sb, ent, option); + } + } +} + +/* + * To allow quick access to the contents of nth line in the + * final image, prepare an index in the scoreboard. + */ +static int prepare_lines(struct scoreboard *sb) +{ + const char *buf = sb->final_buf; + unsigned long len = sb->final_buf_size; + int num = 0, incomplete = 0, bol = 1; + + if (len && buf[len-1] != '\n') + incomplete++; /* incomplete line at the end */ + while (len--) { + if (bol) { + sb->lineno = xrealloc(sb->lineno, + sizeof(int *) * (num + 1)); + sb->lineno[num] = buf - sb->final_buf; + bol = 0; + } + if (*buf++ == '\n') { + num++; + bol = 1; + } + } + sb->lineno = xrealloc(sb->lineno, + sizeof(int *) * (num + incomplete + 1)); + sb->lineno[num + incomplete] = buf - sb->final_buf; + sb->num_lines = num + incomplete; + return sb->num_lines; +} + +/* + * Add phony grafts for use with -S; this is primarily to + * support git's cvsserver that wants to give a linear history + * to its clients. + */ +static int read_ancestry(const char *graft_file) +{ + FILE *fp = fopen(graft_file, "r"); + char buf[1024]; + if (!fp) + return -1; + while (fgets(buf, sizeof(buf), fp)) { + /* The format is just "Commit Parent1 Parent2 ...\n" */ + int len = strlen(buf); + struct commit_graft *graft = read_graft_line(buf, len); + if (graft) + register_commit_graft(graft, 0); + } + fclose(fp); + return 0; +} + +static int update_auto_abbrev(int auto_abbrev, struct origin *suspect) +{ + const char *uniq = find_unique_abbrev(suspect->commit->object.sha1, + auto_abbrev); + int len = strlen(uniq); + if (auto_abbrev < len) + return len; + return auto_abbrev; +} + +/* + * How many columns do we need to show line numbers, authors, + * and filenames? + */ +static void find_alignment(struct scoreboard *sb, int *option) +{ + int longest_src_lines = 0; + int longest_dst_lines = 0; + unsigned largest_score = 0; + struct blame_entry *e; + int compute_auto_abbrev = (abbrev < 0); + int auto_abbrev = default_abbrev; + + for (e = sb->ent; e; e = e->next) { + struct origin *suspect = e->suspect; + struct commit_info ci; + int num; + + if (compute_auto_abbrev) + auto_abbrev = update_auto_abbrev(auto_abbrev, suspect); + if (strcmp(suspect->path, sb->path)) + *option |= OUTPUT_SHOW_NAME; + num = strlen(suspect->path); + if (longest_file < num) + longest_file = num; + if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { + suspect->commit->object.flags |= METAINFO_SHOWN; + get_commit_info(suspect->commit, &ci, 1); + if (*option & OUTPUT_SHOW_EMAIL) + num = utf8_strwidth(ci.author_mail.buf); + else + num = utf8_strwidth(ci.author.buf); + if (longest_author < num) + longest_author = num; + } + num = e->s_lno + e->num_lines; + if (longest_src_lines < num) + longest_src_lines = num; + num = e->lno + e->num_lines; + if (longest_dst_lines < num) + longest_dst_lines = num; + if (largest_score < ent_score(sb, e)) + largest_score = ent_score(sb, e); + + commit_info_destroy(&ci); + } + max_orig_digits = decimal_width(longest_src_lines); + max_digits = decimal_width(longest_dst_lines); + max_score_digits = decimal_width(largest_score); + + if (compute_auto_abbrev) + /* one more abbrev length is needed for the boundary commit */ + abbrev = auto_abbrev + 1; +} + +/* + * For debugging -- origin is refcounted, and this asserts that + * we do not underflow. + */ +static void sanity_check_refcnt(struct scoreboard *sb) +{ + int baa = 0; + struct blame_entry *ent; + + for (ent = sb->ent; ent; ent = ent->next) { + /* Nobody should have zero or negative refcnt */ + if (ent->suspect->refcnt <= 0) { + fprintf(stderr, "%s in %s has negative refcnt %d\n", + ent->suspect->path, + sha1_to_hex(ent->suspect->commit->object.sha1), + ent->suspect->refcnt); + baa = 1; + } + } + if (baa) { + int opt = 0160; + find_alignment(sb, &opt); + output(sb, opt); + die("Baa %d!", baa); + } +} + +/* + * Used for the command line parsing; check if the path exists + * in the working tree. + */ +static int has_string_in_work_tree(const char *path) +{ + struct stat st; + return !lstat(path, &st); +} + +static unsigned parse_score(const char *arg) +{ + char *end; + unsigned long score = strtoul(arg, &end, 10); + if (*end) + return 0; + return score; +} + +static const char *add_prefix(const char *prefix, const char *path) +{ + return prefix_path(prefix, prefix ? strlen(prefix) : 0, path); +} + +/* + * Parsing of (comma separated) one item in the -L option + */ +static const char *parse_loc(const char *spec, + struct scoreboard *sb, long lno, + long begin, long *ret) +{ + char *term; + const char *line; + long num; + int reg_error; + regex_t regexp; + regmatch_t match[1]; + + /* Allow "-L <something>,+20" to mean starting at <something> + * for 20 lines, or "-L <something>,-5" for 5 lines ending at + * <something>. + */ + if (1 < begin && (spec[0] == '+' || spec[0] == '-')) { + num = strtol(spec + 1, &term, 10); + if (term != spec + 1) { + if (spec[0] == '-') + num = 0 - num; + if (0 < num) + *ret = begin + num - 2; + else if (!num) + *ret = begin; + else + *ret = begin + num; + return term; + } + return spec; + } + num = strtol(spec, &term, 10); + if (term != spec) { + *ret = num; + return term; + } + if (spec[0] != '/') + return spec; + + /* it could be a regexp of form /.../ */ + for (term = (char *) spec + 1; *term && *term != '/'; term++) { + if (*term == '\\') + term++; + } + if (*term != '/') + return spec; + + /* try [spec+1 .. term-1] as regexp */ + *term = 0; + begin--; /* input is in human terms */ + line = nth_line(sb, begin); + + if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) && + !(reg_error = regexec(®exp, line, 1, match, 0))) { + const char *cp = line + match[0].rm_so; + const char *nline; + + while (begin++ < lno) { + nline = nth_line(sb, begin); + if (line <= cp && cp < nline) + break; + line = nline; + } + *ret = begin; + regfree(®exp); + *term++ = '/'; + return term; + } + else { + char errbuf[1024]; + regerror(reg_error, ®exp, errbuf, 1024); + die("-L parameter '%s': %s", spec + 1, errbuf); + } +} + +/* + * Parsing of -L option + */ +static void prepare_blame_range(struct scoreboard *sb, + const char *bottomtop, + long lno, + long *bottom, long *top) +{ + const char *term; + + term = parse_loc(bottomtop, sb, lno, 1, bottom); + if (*term == ',') { + term = parse_loc(term + 1, sb, lno, *bottom + 1, top); + if (*term) + usage(blame_usage); + } + if (*term) + usage(blame_usage); +} + +static int git_blame_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "blame.showroot")) { + show_root = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "blame.blankboundary")) { + blank_boundary = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "blame.date")) { + if (!value) + return config_error_nonbool(var); + blame_date_mode = parse_date_format(value); + return 0; + } + + if (userdiff_config(var, value) < 0) + return -1; + + return git_default_config(var, value, cb); +} + +static void verify_working_tree_path(struct commit *work_tree, const char *path) +{ + struct commit_list *parents; + + for (parents = work_tree->parents; parents; parents = parents->next) { + const unsigned char *commit_sha1 = parents->item->object.sha1; + unsigned char blob_sha1[20]; + unsigned mode; + + if (!get_tree_entry(commit_sha1, path, blob_sha1, &mode) && + sha1_object_info(blob_sha1, NULL) == OBJ_BLOB) + return; + } + die("no such path '%s' in HEAD", path); +} + +static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1) +{ + struct commit *parent; + + parent = lookup_commit_reference(sha1); + if (!parent) + die("no such commit %s", sha1_to_hex(sha1)); + return &commit_list_insert(parent, tail)->next; +} + +static void append_merge_parents(struct commit_list **tail) +{ + int merge_head; + const char *merge_head_file = git_path("MERGE_HEAD"); + struct strbuf line = STRBUF_INIT; + + merge_head = open(merge_head_file, O_RDONLY); + if (merge_head < 0) { + if (errno == ENOENT) + return; + die("cannot open '%s' for reading", merge_head_file); + } + + while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) { + unsigned char sha1[20]; + if (line.len < 40 || get_sha1_hex(line.buf, sha1)) + die("unknown line in '%s': %s", merge_head_file, line.buf); + tail = append_parent(tail, sha1); + } + close(merge_head); + strbuf_release(&line); +} + +/* + * Prepare a dummy commit that represents the work tree (or staged) item. + * Note that annotating work tree item never works in the reverse. + */ +static struct commit *fake_working_tree_commit(struct diff_options *opt, + const char *path, + const char *contents_from) +{ + struct commit *commit; + struct origin *origin; + struct commit_list **parent_tail, *parent; + unsigned char head_sha1[20]; + struct strbuf buf = STRBUF_INIT; + const char *ident; + time_t now; + int size, len; + struct cache_entry *ce; + unsigned mode; + struct strbuf msg = STRBUF_INIT; + + time(&now); + commit = xcalloc(1, sizeof(*commit)); + commit->object.parsed = 1; + commit->date = now; + commit->object.type = OBJ_COMMIT; + parent_tail = &commit->parents; + + if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL)) + die("no such ref: HEAD"); + + parent_tail = append_parent(parent_tail, head_sha1); + append_merge_parents(parent_tail); + verify_working_tree_path(commit, path); + + origin = make_origin(commit, path); + + ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0); + strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n"); + for (parent = commit->parents; parent; parent = parent->next) + strbuf_addf(&msg, "parent %s\n", + sha1_to_hex(parent->item->object.sha1)); + strbuf_addf(&msg, + "author %s\n" + "committer %s\n\n" + "Version of %s from %s\n", + ident, ident, path, + (!contents_from ? path : + (!strcmp(contents_from, "-") ? "standard input" : contents_from))); + commit->buffer = strbuf_detach(&msg, NULL); + + if (!contents_from || strcmp("-", contents_from)) { + struct stat st; + const char *read_from; + char *buf_ptr; + unsigned long buf_len; + + if (contents_from) { + if (stat(contents_from, &st) < 0) + die_errno("Cannot stat '%s'", contents_from); + read_from = contents_from; + } + else { + if (lstat(path, &st) < 0) + die_errno("Cannot lstat '%s'", path); + read_from = path; + } + mode = canon_mode(st.st_mode); + + switch (st.st_mode & S_IFMT) { + case S_IFREG: + if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) && + textconv_object(read_from, mode, null_sha1, 0, &buf_ptr, &buf_len)) + strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1); + else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) + die_errno("cannot open or read '%s'", read_from); + break; + case S_IFLNK: + if (strbuf_readlink(&buf, read_from, st.st_size) < 0) + die_errno("cannot readlink '%s'", read_from); + break; + default: + die("unsupported file type %s", read_from); + } + } + else { + /* Reading from stdin */ + mode = 0; + if (strbuf_read(&buf, 0, 0) < 0) + die_errno("failed to read from stdin"); + } + convert_to_git(path, buf.buf, buf.len, &buf, 0); + origin->file.ptr = buf.buf; + origin->file.size = buf.len; + pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1); + commit->util = origin; + + /* + * Read the current index, replace the path entry with + * origin->blob_sha1 without mucking with its mode or type + * bits; we are not going to write this index out -- we just + * want to run "diff-index --cached". + */ + discard_cache(); + read_cache(); + + len = strlen(path); + if (!mode) { + int pos = cache_name_pos(path, len); + if (0 <= pos) + mode = active_cache[pos]->ce_mode; + else + /* Let's not bother reading from HEAD tree */ + mode = S_IFREG | 0644; + } + size = cache_entry_size(len); + ce = xcalloc(1, size); + hashcpy(ce->sha1, origin->blob_sha1); + memcpy(ce->name, path, len); + ce->ce_flags = create_ce_flags(0); + ce->ce_namelen = len; + ce->ce_mode = create_ce_mode(mode); + add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); + + /* + * We are not going to write this out, so this does not matter + * right now, but someday we might optimize diff-index --cached + * with cache-tree information. + */ + cache_tree_invalidate_path(active_cache_tree, path); + + return commit; +} + +static const char *prepare_final(struct scoreboard *sb) +{ + int i; + const char *final_commit_name = NULL; + struct rev_info *revs = sb->revs; + + /* + * There must be one and only one positive commit in the + * revs->pending array. + */ + for (i = 0; i < revs->pending.nr; i++) { + struct object *obj = revs->pending.objects[i].item; + if (obj->flags & UNINTERESTING) + continue; + while (obj->type == OBJ_TAG) + obj = deref_tag(obj, NULL, 0); + if (obj->type != OBJ_COMMIT) + die("Non commit %s?", revs->pending.objects[i].name); + if (sb->final) + die("More than one commit to dig from %s and %s?", + revs->pending.objects[i].name, + final_commit_name); + sb->final = (struct commit *) obj; + final_commit_name = revs->pending.objects[i].name; + } + return final_commit_name; +} + +static const char *prepare_initial(struct scoreboard *sb) +{ + int i; + const char *final_commit_name = NULL; + struct rev_info *revs = sb->revs; + + /* + * There must be one and only one negative commit, and it must be + * the boundary. + */ + for (i = 0; i < revs->pending.nr; i++) { + struct object *obj = revs->pending.objects[i].item; + if (!(obj->flags & UNINTERESTING)) + continue; + while (obj->type == OBJ_TAG) + obj = deref_tag(obj, NULL, 0); + if (obj->type != OBJ_COMMIT) + die("Non commit %s?", revs->pending.objects[i].name); + if (sb->final) + die("More than one commit to dig down to %s and %s?", + revs->pending.objects[i].name, + final_commit_name); + sb->final = (struct commit *) obj; + final_commit_name = revs->pending.objects[i].name; + } + if (!final_commit_name) + die("No commit to dig down to?"); + return final_commit_name; +} + +static int blame_copy_callback(const struct option *option, const char *arg, int unset) +{ + int *opt = option->value; + + /* + * -C enables copy from removed files; + * -C -C enables copy from existing files, but only + * when blaming a new file; + * -C -C -C enables copy from existing files for + * everybody + */ + if (*opt & PICKAXE_BLAME_COPY_HARDER) + *opt |= PICKAXE_BLAME_COPY_HARDEST; + if (*opt & PICKAXE_BLAME_COPY) + *opt |= PICKAXE_BLAME_COPY_HARDER; + *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE; + + if (arg) + blame_copy_score = parse_score(arg); + return 0; +} + +static int blame_move_callback(const struct option *option, const char *arg, int unset) +{ + int *opt = option->value; + + *opt |= PICKAXE_BLAME_MOVE; + + if (arg) + blame_move_score = parse_score(arg); + return 0; +} + +static int blame_bottomtop_callback(const struct option *option, const char *arg, int unset) +{ + const char **bottomtop = option->value; + if (!arg) + return -1; + if (*bottomtop) + die("More than one '-L n,m' option given"); + *bottomtop = arg; + return 0; +} + +int cmd_blame(int argc, const char **argv, const char *prefix) +{ + struct rev_info revs; + const char *path; + struct scoreboard sb; + struct origin *o; + struct blame_entry *ent; + long dashdash_pos, bottom, top, lno; + const char *final_commit_name = NULL; + enum object_type type; + + static const char *bottomtop = NULL; + static int output_option = 0, opt = 0; + static int show_stats = 0; + static const char *revs_file = NULL; + static const char *contents_from = NULL; + static const struct option options[] = { + OPT_BOOLEAN(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")), + OPT_BOOLEAN('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")), + OPT_BOOLEAN(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")), + OPT_BOOLEAN(0, "show-stats", &show_stats, N_("Show work cost statistics")), + OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE), + OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME), + OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER), + OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN), + OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN), + OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT), + OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP), + OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME), + OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR), + OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL), + OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE), + OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL), + OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")), + OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")), + { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback }, + { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback }, + OPT_CALLBACK('L', NULL, &bottomtop, N_("n,m"), N_("Process only line range n,m, counting from 1"), blame_bottomtop_callback), + OPT__ABBREV(&abbrev), + OPT_END() + }; + + struct parse_opt_ctx_t ctx; + int cmd_is_annotate = !strcmp(argv[0], "annotate"); + + git_config(git_blame_config, NULL); + init_revisions(&revs, NULL); + revs.date_mode = blame_date_mode; + DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV); + DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES); + + save_commit_buffer = 0; + dashdash_pos = 0; + + parse_options_start(&ctx, argc, argv, prefix, options, + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); + for (;;) { + switch (parse_options_step(&ctx, options, blame_opt_usage)) { + case PARSE_OPT_HELP: + exit(129); + case PARSE_OPT_DONE: + if (ctx.argv[0]) + dashdash_pos = ctx.cpidx; + goto parse_done; + } + + if (!strcmp(ctx.argv[0], "--reverse")) { + ctx.argv[0] = "--children"; + reverse = 1; + } + parse_revision_opt(&revs, &ctx, options, blame_opt_usage); + } +parse_done: + no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES); + DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES); + argc = parse_options_end(&ctx); + + if (0 < abbrev) + /* one more abbrev length is needed for the boundary commit */ + abbrev++; + + if (revs_file && read_ancestry(revs_file)) + die_errno("reading graft file '%s' failed", revs_file); + + if (cmd_is_annotate) { + output_option |= OUTPUT_ANNOTATE_COMPAT; + blame_date_mode = DATE_ISO8601; + } else { + blame_date_mode = revs.date_mode; + } + + /* The maximum width used to show the dates */ + switch (blame_date_mode) { + case DATE_RFC2822: + blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700"); + break; + case DATE_ISO8601: + blame_date_width = sizeof("2006-10-19 16:00:04 -0700"); + break; + case DATE_RAW: + blame_date_width = sizeof("1161298804 -0700"); + break; + case DATE_SHORT: + blame_date_width = sizeof("2006-10-19"); + break; + case DATE_RELATIVE: + /* "normal" is used as the fallback for "relative" */ + case DATE_LOCAL: + case DATE_NORMAL: + blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700"); + break; + } + blame_date_width -= 1; /* strip the null */ + + if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER)) + opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE | + PICKAXE_BLAME_COPY_HARDER); + + if (!blame_move_score) + blame_move_score = BLAME_DEFAULT_MOVE_SCORE; + if (!blame_copy_score) + blame_copy_score = BLAME_DEFAULT_COPY_SCORE; + + /* + * We have collected options unknown to us in argv[1..unk] + * which are to be passed to revision machinery if we are + * going to do the "bottom" processing. + * + * The remaining are: + * + * (1) if dashdash_pos != 0, it is either + * "blame [revisions] -- <path>" or + * "blame -- <path> <rev>" + * + * (2) otherwise, it is one of the two: + * "blame [revisions] <path>" + * "blame <path> <rev>" + * + * Note that we must strip out <path> from the arguments: we do not + * want the path pruning but we may want "bottom" processing. + */ + if (dashdash_pos) { + switch (argc - dashdash_pos - 1) { + case 2: /* (1b) */ + if (argc != 4) + usage_with_options(blame_opt_usage, options); + /* reorder for the new way: <rev> -- <path> */ + argv[1] = argv[3]; + argv[3] = argv[2]; + argv[2] = "--"; + /* FALLTHROUGH */ + case 1: /* (1a) */ + path = add_prefix(prefix, argv[--argc]); + argv[argc] = NULL; + break; + default: + usage_with_options(blame_opt_usage, options); + } + } else { + if (argc < 2) + usage_with_options(blame_opt_usage, options); + path = add_prefix(prefix, argv[argc - 1]); + if (argc == 3 && !has_string_in_work_tree(path)) { /* (2b) */ + path = add_prefix(prefix, argv[1]); + argv[1] = argv[2]; + } + argv[argc - 1] = "--"; + + setup_work_tree(); + if (!has_string_in_work_tree(path)) + die_errno("cannot stat path '%s'", path); + } + + revs.disable_stdin = 1; + setup_revisions(argc, argv, &revs, NULL); + memset(&sb, 0, sizeof(sb)); + + sb.revs = &revs; + if (!reverse) + final_commit_name = prepare_final(&sb); + else if (contents_from) + die("--contents and --children do not blend well."); + else + final_commit_name = prepare_initial(&sb); + + if (!sb.final) { + /* + * "--not A B -- path" without anything positive; + * do not default to HEAD, but use the working tree + * or "--contents". + */ + setup_work_tree(); + sb.final = fake_working_tree_commit(&sb.revs->diffopt, + path, contents_from); + add_pending_object(&revs, &(sb.final->object), ":"); + } + else if (contents_from) + die("Cannot use --contents with final commit object name"); + + /* + * If we have bottom, this will mark the ancestors of the + * bottom commits we would reach while traversing as + * uninteresting. + */ + if (prepare_revision_walk(&revs)) + die("revision walk setup failed"); + + if (is_null_sha1(sb.final->object.sha1)) { + char *buf; + o = sb.final->util; + buf = xmalloc(o->file.size + 1); + memcpy(buf, o->file.ptr, o->file.size + 1); + sb.final_buf = buf; + sb.final_buf_size = o->file.size; + } + else { + o = get_origin(&sb, sb.final, path); + if (fill_blob_sha1_and_mode(o)) + die("no such path %s in %s", path, final_commit_name); + + if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) && + textconv_object(path, o->mode, o->blob_sha1, 1, (char **) &sb.final_buf, + &sb.final_buf_size)) + ; + else + sb.final_buf = read_sha1_file(o->blob_sha1, &type, + &sb.final_buf_size); + + if (!sb.final_buf) + die("Cannot read blob %s for path %s", + sha1_to_hex(o->blob_sha1), + path); + } + num_read_blob++; + lno = prepare_lines(&sb); + + bottom = top = 0; + if (bottomtop) + prepare_blame_range(&sb, bottomtop, lno, &bottom, &top); + if (bottom && top && top < bottom) { + long tmp; + tmp = top; top = bottom; bottom = tmp; + } + if (bottom < 1) + bottom = 1; + if (top < 1) + top = lno; + bottom--; + if (lno < top || lno < bottom) + die("file %s has only %lu lines", path, lno); + + ent = xcalloc(1, sizeof(*ent)); + ent->lno = bottom; + ent->num_lines = top - bottom; + ent->suspect = o; + ent->s_lno = bottom; + + sb.ent = ent; + sb.path = path; + + read_mailmap(&mailmap, NULL); + + if (!incremental) + setup_pager(); + + assign_blame(&sb, opt); + + if (incremental) + return 0; + + coalesce(&sb); + + if (!(output_option & OUTPUT_PORCELAIN)) + find_alignment(&sb, &output_option); + + output(&sb, output_option); + free((void *)sb.final_buf); + for (ent = sb.ent; ent; ) { + struct blame_entry *e = ent->next; + free(ent); + ent = e; + } + + if (show_stats) { + printf("num read blob: %d\n", num_read_blob); + printf("num get patch: %d\n", num_get_patch); + printf("num commits: %d\n", num_commits); + } + return 0; +} diff --git a/builtin/branch.c b/builtin/branch.c new file mode 100644 index 0000000000..873f624d1c --- /dev/null +++ b/builtin/branch.c @@ -0,0 +1,943 @@ +/* + * Builtin "git branch" + * + * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com> + * Based on git-branch.sh by Junio C Hamano. + */ + +#include "cache.h" +#include "color.h" +#include "refs.h" +#include "commit.h" +#include "builtin.h" +#include "remote.h" +#include "parse-options.h" +#include "branch.h" +#include "diff.h" +#include "revision.h" +#include "string-list.h" +#include "column.h" +#include "utf8.h" + +static const char * const builtin_branch_usage[] = { + N_("git branch [options] [-r | -a] [--merged | --no-merged]"), + N_("git branch [options] [-l] [-f] <branchname> [<start-point>]"), + N_("git branch [options] [-r] (-d | -D) <branchname>..."), + N_("git branch [options] (-m | -M) [<oldbranch>] <newbranch>"), + NULL +}; + +#define REF_LOCAL_BRANCH 0x01 +#define REF_REMOTE_BRANCH 0x02 + +static const char *head; +static unsigned char head_sha1[20]; + +static int branch_use_color = -1; +static char branch_colors[][COLOR_MAXLEN] = { + GIT_COLOR_RESET, + GIT_COLOR_NORMAL, /* PLAIN */ + GIT_COLOR_RED, /* REMOTE */ + GIT_COLOR_NORMAL, /* LOCAL */ + GIT_COLOR_GREEN, /* CURRENT */ +}; +enum color_branch { + BRANCH_COLOR_RESET = 0, + BRANCH_COLOR_PLAIN = 1, + BRANCH_COLOR_REMOTE = 2, + BRANCH_COLOR_LOCAL = 3, + BRANCH_COLOR_CURRENT = 4 +}; + +static enum merge_filter { + NO_FILTER = 0, + SHOW_NOT_MERGED, + SHOW_MERGED +} merge_filter; +static unsigned char merge_filter_ref[20]; + +static struct string_list output = STRING_LIST_INIT_DUP; +static unsigned int colopts; + +static int parse_branch_color_slot(const char *var, int ofs) +{ + if (!strcasecmp(var+ofs, "plain")) + return BRANCH_COLOR_PLAIN; + if (!strcasecmp(var+ofs, "reset")) + return BRANCH_COLOR_RESET; + if (!strcasecmp(var+ofs, "remote")) + return BRANCH_COLOR_REMOTE; + if (!strcasecmp(var+ofs, "local")) + return BRANCH_COLOR_LOCAL; + if (!strcasecmp(var+ofs, "current")) + return BRANCH_COLOR_CURRENT; + return -1; +} + +static int git_branch_config(const char *var, const char *value, void *cb) +{ + if (!prefixcmp(var, "column.")) + return git_column_config(var, value, "branch", &colopts); + if (!strcmp(var, "color.branch")) { + branch_use_color = git_config_colorbool(var, value); + return 0; + } + if (!prefixcmp(var, "color.branch.")) { + int slot = parse_branch_color_slot(var, 13); + if (slot < 0) + return 0; + if (!value) + return config_error_nonbool(var); + color_parse(value, var, branch_colors[slot]); + return 0; + } + return git_color_default_config(var, value, cb); +} + +static const char *branch_get_color(enum color_branch ix) +{ + if (want_color(branch_use_color)) + return branch_colors[ix]; + return ""; +} + +static int branch_merged(int kind, const char *name, + struct commit *rev, struct commit *head_rev) +{ + /* + * This checks whether the merge bases of branch and HEAD (or + * the other branch this branch builds upon) contains the + * branch, which means that the branch has already been merged + * safely to HEAD (or the other branch). + */ + struct commit *reference_rev = NULL; + const char *reference_name = NULL; + void *reference_name_to_free = NULL; + int merged; + + if (kind == REF_LOCAL_BRANCH) { + struct branch *branch = branch_get(name); + unsigned char sha1[20]; + + if (branch && + branch->merge && + branch->merge[0] && + branch->merge[0]->dst && + (reference_name = reference_name_to_free = + resolve_refdup(branch->merge[0]->dst, sha1, 1, NULL)) != NULL) + reference_rev = lookup_commit_reference(sha1); + } + if (!reference_rev) + reference_rev = head_rev; + + merged = in_merge_bases(rev, reference_rev); + + /* + * After the safety valve is fully redefined to "check with + * upstream, if any, otherwise with HEAD", we should just + * return the result of the in_merge_bases() above without + * any of the following code, but during the transition period, + * a gentle reminder is in order. + */ + if ((head_rev != reference_rev) && + in_merge_bases(rev, head_rev) != merged) { + if (merged) + warning(_("deleting branch '%s' that has been merged to\n" + " '%s', but not yet merged to HEAD."), + name, reference_name); + else + warning(_("not deleting branch '%s' that is not yet merged to\n" + " '%s', even though it is merged to HEAD."), + name, reference_name); + } + free(reference_name_to_free); + return merged; +} + +static int check_branch_commit(const char *branchname, const char *refname, + unsigned char *sha1, struct commit *head_rev, + int kinds, int force) +{ + struct commit *rev = lookup_commit_reference(sha1); + if (!rev) { + error(_("Couldn't look up commit object for '%s'"), refname); + return -1; + } + if (!force && !branch_merged(kinds, branchname, rev, head_rev)) { + error(_("The branch '%s' is not fully merged.\n" + "If you are sure you want to delete it, " + "run 'git branch -D %s'."), branchname, branchname); + return -1; + } + return 0; +} + +static void delete_branch_config(const char *branchname) +{ + struct strbuf buf = STRBUF_INIT; + strbuf_addf(&buf, "branch.%s", branchname); + if (git_config_rename_section(buf.buf, NULL) < 0) + warning(_("Update of config-file failed")); + strbuf_release(&buf); +} + +static int delete_branches(int argc, const char **argv, int force, int kinds, + int quiet) +{ + struct commit *head_rev = NULL; + unsigned char sha1[20]; + char *name = NULL; + const char *fmt; + int i; + int ret = 0; + int remote_branch = 0; + struct strbuf bname = STRBUF_INIT; + + switch (kinds) { + case REF_REMOTE_BRANCH: + fmt = "refs/remotes/%s"; + /* For subsequent UI messages */ + remote_branch = 1; + + force = 1; + break; + case REF_LOCAL_BRANCH: + fmt = "refs/heads/%s"; + break; + default: + die(_("cannot use -a with -d")); + } + + if (!force) { + head_rev = lookup_commit_reference(head_sha1); + if (!head_rev) + die(_("Couldn't look up commit object for HEAD")); + } + for (i = 0; i < argc; i++, strbuf_release(&bname)) { + const char *target; + int flags = 0; + + strbuf_branchname(&bname, argv[i]); + if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) { + error(_("Cannot delete the branch '%s' " + "which you are currently on."), bname.buf); + ret = 1; + continue; + } + + free(name); + + name = mkpathdup(fmt, bname.buf); + target = resolve_ref_unsafe(name, sha1, 0, &flags); + if (!target || + (!(flags & REF_ISSYMREF) && is_null_sha1(sha1))) { + error(remote_branch + ? _("remote branch '%s' not found.") + : _("branch '%s' not found."), bname.buf); + ret = 1; + continue; + } + + if (!(flags & REF_ISSYMREF) && + check_branch_commit(bname.buf, name, sha1, head_rev, kinds, + force)) { + ret = 1; + continue; + } + + if (delete_ref(name, sha1, REF_NODEREF)) { + error(remote_branch + ? _("Error deleting remote branch '%s'") + : _("Error deleting branch '%s'"), + bname.buf); + ret = 1; + continue; + } + if (!quiet) { + printf(remote_branch + ? _("Deleted remote branch %s (was %s).\n") + : _("Deleted branch %s (was %s).\n"), + bname.buf, + (flags & REF_ISSYMREF) + ? target + : find_unique_abbrev(sha1, DEFAULT_ABBREV)); + } + delete_branch_config(bname.buf); + } + + free(name); + + return(ret); +} + +struct ref_item { + char *name; + char *dest; + unsigned int kind, width; + struct commit *commit; +}; + +struct ref_list { + struct rev_info revs; + int index, alloc, maxwidth, verbose, abbrev; + struct ref_item *list; + struct commit_list *with_commit; + int kinds; +}; + +static char *resolve_symref(const char *src, const char *prefix) +{ + unsigned char sha1[20]; + int flag; + const char *dst, *cp; + + dst = resolve_ref_unsafe(src, sha1, 0, &flag); + if (!(dst && (flag & REF_ISSYMREF))) + return NULL; + if (prefix && (cp = skip_prefix(dst, prefix))) + dst = cp; + return xstrdup(dst); +} + +struct append_ref_cb { + struct ref_list *ref_list; + const char **pattern; + int ret; +}; + +static int match_patterns(const char **pattern, const char *refname) +{ + if (!*pattern) + return 1; /* no pattern always matches */ + while (*pattern) { + if (!fnmatch(*pattern, refname, 0)) + return 1; + pattern++; + } + return 0; +} + +static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data) +{ + struct append_ref_cb *cb = (struct append_ref_cb *)(cb_data); + struct ref_list *ref_list = cb->ref_list; + struct ref_item *newitem; + struct commit *commit; + int kind, i; + const char *prefix, *orig_refname = refname; + + static struct { + int kind; + const char *prefix; + int pfxlen; + } ref_kind[] = { + { REF_LOCAL_BRANCH, "refs/heads/", 11 }, + { REF_REMOTE_BRANCH, "refs/remotes/", 13 }, + }; + + /* Detect kind */ + for (i = 0; i < ARRAY_SIZE(ref_kind); i++) { + prefix = ref_kind[i].prefix; + if (strncmp(refname, prefix, ref_kind[i].pfxlen)) + continue; + kind = ref_kind[i].kind; + refname += ref_kind[i].pfxlen; + break; + } + if (ARRAY_SIZE(ref_kind) <= i) + return 0; + + /* Don't add types the caller doesn't want */ + if ((kind & ref_list->kinds) == 0) + return 0; + + if (!match_patterns(cb->pattern, refname)) + return 0; + + commit = NULL; + if (ref_list->verbose || ref_list->with_commit || merge_filter != NO_FILTER) { + commit = lookup_commit_reference_gently(sha1, 1); + if (!commit) { + cb->ret = error(_("branch '%s' does not point at a commit"), refname); + return 0; + } + + /* Filter with with_commit if specified */ + if (!is_descendant_of(commit, ref_list->with_commit)) + return 0; + + if (merge_filter != NO_FILTER) + add_pending_object(&ref_list->revs, + (struct object *)commit, refname); + } + + ALLOC_GROW(ref_list->list, ref_list->index + 1, ref_list->alloc); + + /* Record the new item */ + newitem = &(ref_list->list[ref_list->index++]); + newitem->name = xstrdup(refname); + newitem->kind = kind; + newitem->commit = commit; + newitem->width = utf8_strwidth(refname); + newitem->dest = resolve_symref(orig_refname, prefix); + /* adjust for "remotes/" */ + if (newitem->kind == REF_REMOTE_BRANCH && + ref_list->kinds != REF_REMOTE_BRANCH) + newitem->width += 8; + if (newitem->width > ref_list->maxwidth) + ref_list->maxwidth = newitem->width; + + return 0; +} + +static void free_ref_list(struct ref_list *ref_list) +{ + int i; + + for (i = 0; i < ref_list->index; i++) { + free(ref_list->list[i].name); + free(ref_list->list[i].dest); + } + free(ref_list->list); +} + +static int ref_cmp(const void *r1, const void *r2) +{ + struct ref_item *c1 = (struct ref_item *)(r1); + struct ref_item *c2 = (struct ref_item *)(r2); + + if (c1->kind != c2->kind) + return c1->kind - c2->kind; + return strcmp(c1->name, c2->name); +} + +static void fill_tracking_info(struct strbuf *stat, const char *branch_name, + int show_upstream_ref) +{ + int ours, theirs; + char *ref = NULL; + struct branch *branch = branch_get(branch_name); + + if (!stat_tracking_info(branch, &ours, &theirs)) { + if (branch && branch->merge && branch->merge[0]->dst && + show_upstream_ref) + strbuf_addf(stat, "[%s] ", + shorten_unambiguous_ref(branch->merge[0]->dst, 0)); + return; + } + + if (show_upstream_ref) + ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0); + if (!ours) { + if (ref) + strbuf_addf(stat, _("[%s: behind %d]"), ref, theirs); + else + strbuf_addf(stat, _("[behind %d]"), theirs); + + } else if (!theirs) { + if (ref) + strbuf_addf(stat, _("[%s: ahead %d]"), ref, ours); + else + strbuf_addf(stat, _("[ahead %d]"), ours); + } else { + if (ref) + strbuf_addf(stat, _("[%s: ahead %d, behind %d]"), + ref, ours, theirs); + else + strbuf_addf(stat, _("[ahead %d, behind %d]"), + ours, theirs); + } + strbuf_addch(stat, ' '); + free(ref); +} + +static int matches_merge_filter(struct commit *commit) +{ + int is_merged; + + if (merge_filter == NO_FILTER) + return 1; + + is_merged = !!(commit->object.flags & UNINTERESTING); + return (is_merged == (merge_filter == SHOW_MERGED)); +} + +static void add_verbose_info(struct strbuf *out, struct ref_item *item, + int verbose, int abbrev) +{ + struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT; + const char *sub = " **** invalid ref ****"; + struct commit *commit = item->commit; + + if (commit && !parse_commit(commit)) { + pp_commit_easy(CMIT_FMT_ONELINE, commit, &subject); + sub = subject.buf; + } + + if (item->kind == REF_LOCAL_BRANCH) + fill_tracking_info(&stat, item->name, verbose > 1); + + strbuf_addf(out, " %s %s%s", + find_unique_abbrev(item->commit->object.sha1, abbrev), + stat.buf, sub); + strbuf_release(&stat); + strbuf_release(&subject); +} + +static void print_ref_item(struct ref_item *item, int maxwidth, int verbose, + int abbrev, int current, char *prefix) +{ + char c; + int color; + struct commit *commit = item->commit; + struct strbuf out = STRBUF_INIT, name = STRBUF_INIT; + + if (!matches_merge_filter(commit)) + return; + + switch (item->kind) { + case REF_LOCAL_BRANCH: + color = BRANCH_COLOR_LOCAL; + break; + case REF_REMOTE_BRANCH: + color = BRANCH_COLOR_REMOTE; + break; + default: + color = BRANCH_COLOR_PLAIN; + break; + } + + c = ' '; + if (current) { + c = '*'; + color = BRANCH_COLOR_CURRENT; + } + + strbuf_addf(&name, "%s%s", prefix, item->name); + if (verbose) { + int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf); + strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color), + maxwidth + utf8_compensation, name.buf, + branch_get_color(BRANCH_COLOR_RESET)); + } else + strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color), + name.buf, branch_get_color(BRANCH_COLOR_RESET)); + + if (item->dest) + strbuf_addf(&out, " -> %s", item->dest); + else if (verbose) + /* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */ + add_verbose_info(&out, item, verbose, abbrev); + if (column_active(colopts)) { + assert(!verbose && "--column and --verbose are incompatible"); + string_list_append(&output, out.buf); + } else { + printf("%s\n", out.buf); + } + strbuf_release(&name); + strbuf_release(&out); +} + +static int calc_maxwidth(struct ref_list *refs) +{ + int i, w = 0; + for (i = 0; i < refs->index; i++) { + if (!matches_merge_filter(refs->list[i].commit)) + continue; + if (refs->list[i].width > w) + w = refs->list[i].width; + } + return w; +} + + +static void show_detached(struct ref_list *ref_list) +{ + struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1); + + if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) { + struct ref_item item; + item.name = xstrdup(_("(no branch)")); + item.width = utf8_strwidth(item.name); + item.kind = REF_LOCAL_BRANCH; + item.dest = NULL; + item.commit = head_commit; + if (item.width > ref_list->maxwidth) + ref_list->maxwidth = item.width; + print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, ""); + free(item.name); + } +} + +static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit, const char **pattern) +{ + int i; + struct append_ref_cb cb; + struct ref_list ref_list; + + memset(&ref_list, 0, sizeof(ref_list)); + ref_list.kinds = kinds; + ref_list.verbose = verbose; + ref_list.abbrev = abbrev; + ref_list.with_commit = with_commit; + if (merge_filter != NO_FILTER) + init_revisions(&ref_list.revs, NULL); + cb.ref_list = &ref_list; + cb.pattern = pattern; + cb.ret = 0; + for_each_rawref(append_ref, &cb); + if (merge_filter != NO_FILTER) { + struct commit *filter; + filter = lookup_commit_reference_gently(merge_filter_ref, 0); + if (!filter) + die("object '%s' does not point to a commit", + sha1_to_hex(merge_filter_ref)); + + filter->object.flags |= UNINTERESTING; + add_pending_object(&ref_list.revs, + (struct object *) filter, ""); + ref_list.revs.limited = 1; + prepare_revision_walk(&ref_list.revs); + if (verbose) + ref_list.maxwidth = calc_maxwidth(&ref_list); + } + + qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp); + + detached = (detached && (kinds & REF_LOCAL_BRANCH)); + if (detached && match_patterns(pattern, "HEAD")) + show_detached(&ref_list); + + for (i = 0; i < ref_list.index; i++) { + int current = !detached && + (ref_list.list[i].kind == REF_LOCAL_BRANCH) && + !strcmp(ref_list.list[i].name, head); + char *prefix = (kinds != REF_REMOTE_BRANCH && + ref_list.list[i].kind == REF_REMOTE_BRANCH) + ? "remotes/" : ""; + print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose, + abbrev, current, prefix); + } + + free_ref_list(&ref_list); + + if (cb.ret) + error(_("some refs could not be read")); + + return cb.ret; +} + +static void rename_branch(const char *oldname, const char *newname, int force) +{ + struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; + struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; + int recovery = 0; + int clobber_head_ok; + + if (!oldname) + die(_("cannot rename the current branch while not on any.")); + + if (strbuf_check_branch_ref(&oldref, oldname)) { + /* + * Bad name --- this could be an attempt to rename a + * ref that we used to allow to be created by accident. + */ + if (ref_exists(oldref.buf)) + recovery = 1; + else + die(_("Invalid branch name: '%s'"), oldname); + } + + /* + * A command like "git branch -M currentbranch currentbranch" cannot + * cause the worktree to become inconsistent with HEAD, so allow it. + */ + clobber_head_ok = !strcmp(oldname, newname); + + validate_new_branchname(newname, &newref, force, clobber_head_ok); + + strbuf_addf(&logmsg, "Branch: renamed %s to %s", + oldref.buf, newref.buf); + + if (rename_ref(oldref.buf, newref.buf, logmsg.buf)) + die(_("Branch rename failed")); + strbuf_release(&logmsg); + + if (recovery) + warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11); + + /* no need to pass logmsg here as HEAD didn't really move */ + if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL)) + die(_("Branch renamed to %s, but HEAD is not updated!"), newname); + + strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11); + strbuf_release(&oldref); + strbuf_addf(&newsection, "branch.%s", newref.buf + 11); + strbuf_release(&newref); + if (git_config_rename_section(oldsection.buf, newsection.buf) < 0) + die(_("Branch is renamed, but update of config-file failed")); + strbuf_release(&oldsection); + strbuf_release(&newsection); +} + +static int opt_parse_merge_filter(const struct option *opt, const char *arg, int unset) +{ + merge_filter = ((opt->long_name[0] == 'n') + ? SHOW_NOT_MERGED + : SHOW_MERGED); + if (unset) + merge_filter = SHOW_NOT_MERGED; /* b/c for --no-merged */ + if (!arg) + arg = "HEAD"; + if (get_sha1(arg, merge_filter_ref)) + die(_("malformed object name %s"), arg); + return 0; +} + +static const char edit_description[] = "BRANCH_DESCRIPTION"; + +static int edit_branch_description(const char *branch_name) +{ + FILE *fp; + int status; + struct strbuf buf = STRBUF_INIT; + struct strbuf name = STRBUF_INIT; + + read_branch_desc(&buf, branch_name); + if (!buf.len || buf.buf[buf.len-1] != '\n') + strbuf_addch(&buf, '\n'); + strbuf_addf(&buf, + "# Please edit the description for the branch\n" + "# %s\n" + "# Lines starting with '#' will be stripped.\n", + branch_name); + fp = fopen(git_path(edit_description), "w"); + if ((fwrite(buf.buf, 1, buf.len, fp) < buf.len) || fclose(fp)) { + strbuf_release(&buf); + return error(_("could not write branch description template: %s"), + strerror(errno)); + } + strbuf_reset(&buf); + if (launch_editor(git_path(edit_description), &buf, NULL)) { + strbuf_release(&buf); + return -1; + } + stripspace(&buf, 1); + + strbuf_addf(&name, "branch.%s.description", branch_name); + status = git_config_set(name.buf, buf.len ? buf.buf : NULL); + strbuf_release(&name); + strbuf_release(&buf); + + return status; +} + +int cmd_branch(int argc, const char **argv, const char *prefix) +{ + int delete = 0, rename = 0, force_create = 0, list = 0; + int verbose = 0, abbrev = -1, detached = 0; + int reflog = 0, edit_description = 0; + int quiet = 0, unset_upstream = 0; + const char *new_upstream = NULL; + enum branch_track track; + int kinds = REF_LOCAL_BRANCH; + struct commit_list *with_commit = NULL; + + struct option options[] = { + OPT_GROUP(N_("Generic options")), + OPT__VERBOSE(&verbose, + N_("show hash and subject, give twice for upstream branch")), + OPT__QUIET(&quiet, N_("suppress informational messages")), + OPT_SET_INT('t', "track", &track, N_("set up tracking mode (see git-pull(1))"), + BRANCH_TRACK_EXPLICIT), + OPT_SET_INT( 0, "set-upstream", &track, N_("change upstream info"), + BRANCH_TRACK_OVERRIDE), + OPT_STRING('u', "set-upstream-to", &new_upstream, "upstream", "change the upstream info"), + OPT_BOOLEAN(0, "unset-upstream", &unset_upstream, "Unset the upstream info"), + OPT__COLOR(&branch_use_color, N_("use colored output")), + OPT_SET_INT('r', "remotes", &kinds, N_("act on remote-tracking branches"), + REF_REMOTE_BRANCH), + { + OPTION_CALLBACK, 0, "contains", &with_commit, N_("commit"), + N_("print only branches that contain the commit"), + PARSE_OPT_LASTARG_DEFAULT, + parse_opt_with_commit, (intptr_t)"HEAD", + }, + { + OPTION_CALLBACK, 0, "with", &with_commit, N_("commit"), + N_("print only branches that contain the commit"), + PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT, + parse_opt_with_commit, (intptr_t) "HEAD", + }, + OPT__ABBREV(&abbrev), + + OPT_GROUP(N_("Specific git-branch actions:")), + OPT_SET_INT('a', "all", &kinds, N_("list both remote-tracking and local branches"), + REF_REMOTE_BRANCH | REF_LOCAL_BRANCH), + OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1), + OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2), + OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1), + OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2), + OPT_BOOLEAN(0, "list", &list, N_("list branch names")), + OPT_BOOLEAN('l', "create-reflog", &reflog, N_("create the branch's reflog")), + OPT_BOOLEAN(0, "edit-description", &edit_description, + N_("edit the description for the branch")), + OPT__FORCE(&force_create, N_("force creation (when already exists)")), + { + OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref, + N_("commit"), N_("print only not merged branches"), + PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, + opt_parse_merge_filter, (intptr_t) "HEAD", + }, + { + OPTION_CALLBACK, 0, "merged", &merge_filter_ref, + N_("commit"), N_("print only merged branches"), + PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, + opt_parse_merge_filter, (intptr_t) "HEAD", + }, + OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")), + OPT_END(), + }; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(builtin_branch_usage, options); + + git_config(git_branch_config, NULL); + + track = git_branch_track; + + head = resolve_refdup("HEAD", head_sha1, 0, NULL); + if (!head) + die(_("Failed to resolve HEAD as a valid ref.")); + if (!strcmp(head, "HEAD")) { + detached = 1; + } else { + if (prefixcmp(head, "refs/heads/")) + die(_("HEAD not found below refs/heads!")); + head += 11; + } + hashcpy(merge_filter_ref, head_sha1); + + + argc = parse_options(argc, argv, prefix, options, builtin_branch_usage, + 0); + + if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0) + list = 1; + + if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1) + usage_with_options(builtin_branch_usage, options); + + if (abbrev == -1) + abbrev = DEFAULT_ABBREV; + finalize_colopts(&colopts, -1); + if (verbose) { + if (explicitly_enable_column(colopts)) + die(_("--column and --verbose are incompatible")); + colopts = 0; + } + + if (delete) + return delete_branches(argc, argv, delete > 1, kinds, quiet); + else if (list) { + int ret = print_ref_list(kinds, detached, verbose, abbrev, + with_commit, argv); + print_columns(&output, colopts, NULL); + string_list_clear(&output, 0); + return ret; + } + else if (edit_description) { + const char *branch_name; + struct strbuf branch_ref = STRBUF_INIT; + + if (detached) + die("Cannot give description to detached HEAD"); + if (!argc) + branch_name = head; + else if (argc == 1) + branch_name = argv[0]; + else + usage_with_options(builtin_branch_usage, options); + + strbuf_addf(&branch_ref, "refs/heads/%s", branch_name); + if (!ref_exists(branch_ref.buf)) { + strbuf_release(&branch_ref); + + if (!argc) + return error("No commit on branch '%s' yet.", + branch_name); + else + return error("No such branch '%s'.", branch_name); + } + strbuf_release(&branch_ref); + + if (edit_branch_description(branch_name)) + return 1; + } else if (rename) { + if (argc == 1) + rename_branch(head, argv[0], rename > 1); + else if (argc == 2) + rename_branch(argv[0], argv[1], rename > 1); + else + usage_with_options(builtin_branch_usage, options); + } else if (new_upstream) { + struct branch *branch = branch_get(argv[0]); + + if (!ref_exists(branch->refname)) + die(_("branch '%s' does not exist"), branch->name); + + /* + * create_branch takes care of setting up the tracking + * info and making sure new_upstream is correct + */ + create_branch(head, branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE); + } else if (unset_upstream) { + struct branch *branch = branch_get(argv[0]); + struct strbuf buf = STRBUF_INIT; + + if (!branch_has_merge_config(branch)) { + die(_("Branch '%s' has no upstream information"), branch->name); + } + + strbuf_addf(&buf, "branch.%s.remote", branch->name); + git_config_set_multivar(buf.buf, NULL, NULL, 1); + strbuf_reset(&buf); + strbuf_addf(&buf, "branch.%s.merge", branch->name); + git_config_set_multivar(buf.buf, NULL, NULL, 1); + strbuf_release(&buf); + } else if (argc > 0 && argc <= 2) { + struct branch *branch = branch_get(argv[0]); + int branch_existed = 0, remote_tracking = 0; + struct strbuf buf = STRBUF_INIT; + + if (kinds != REF_LOCAL_BRANCH) + die(_("-a and -r options to 'git branch' do not make sense with a branch name")); + + if (track == BRANCH_TRACK_OVERRIDE) + fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n")); + + strbuf_addf(&buf, "refs/remotes/%s", branch->name); + remote_tracking = ref_exists(buf.buf); + strbuf_release(&buf); + + branch_existed = ref_exists(branch->refname); + create_branch(head, argv[0], (argc == 2) ? argv[1] : head, + force_create, reflog, 0, quiet, track); + + /* + * We only show the instructions if the user gave us + * one branch which doesn't exist locally, but is the + * name of a remote-tracking branch. + */ + if (argc == 1 && track == BRANCH_TRACK_OVERRIDE && + !branch_existed && remote_tracking) { + fprintf(stderr, _("\nIf you wanted to make '%s' track '%s', do this:\n\n"), head, branch->name); + fprintf(stderr, _(" git branch -d %s\n"), branch->name); + fprintf(stderr, _(" git branch --set-upstream-to %s\n"), branch->name); + } + + } else + usage_with_options(builtin_branch_usage, options); + + return 0; +} diff --git a/builtin/bundle.c b/builtin/bundle.c new file mode 100644 index 0000000000..92a8a6026a --- /dev/null +++ b/builtin/bundle.c @@ -0,0 +1,65 @@ +#include "builtin.h" +#include "cache.h" +#include "bundle.h" + +/* + * Basic handler for bundle files to connect repositories via sneakernet. + * Invocation must include action. + * This function can create a bundle or provide information on an existing + * bundle supporting "fetch", "pull", and "ls-remote". + */ + +static const char builtin_bundle_usage[] = + "git bundle create <file> <git-rev-list args>\n" + " or: git bundle verify <file>\n" + " or: git bundle list-heads <file> [<refname>...]\n" + " or: git bundle unbundle <file> [<refname>...]"; + +int cmd_bundle(int argc, const char **argv, const char *prefix) +{ + struct bundle_header header; + const char *cmd, *bundle_file; + int bundle_fd = -1; + char buffer[PATH_MAX]; + + if (argc < 3) + usage(builtin_bundle_usage); + + cmd = argv[1]; + bundle_file = argv[2]; + argc -= 2; + argv += 2; + + if (prefix && bundle_file[0] != '/') { + snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file); + bundle_file = buffer; + } + + memset(&header, 0, sizeof(header)); + if (strcmp(cmd, "create") && (bundle_fd = + read_bundle_header(bundle_file, &header)) < 0) + return 1; + + if (!strcmp(cmd, "verify")) { + close(bundle_fd); + if (verify_bundle(&header, 1)) + return 1; + fprintf(stderr, _("%s is okay\n"), bundle_file); + return 0; + } + if (!strcmp(cmd, "list-heads")) { + close(bundle_fd); + return !!list_bundle_refs(&header, argc, argv); + } + if (!strcmp(cmd, "create")) { + if (!startup_info->have_repository) + die(_("Need a repository to create a bundle.")); + return !!create_bundle(&header, bundle_file, argc, argv); + } else if (!strcmp(cmd, "unbundle")) { + if (!startup_info->have_repository) + die(_("Need a repository to unbundle.")); + return !!unbundle(&header, bundle_fd, 0) || + list_bundle_refs(&header, argc, argv); + } else + usage(builtin_bundle_usage); +} diff --git a/builtin/cat-file.c b/builtin/cat-file.c new file mode 100644 index 0000000000..00528ddc38 --- /dev/null +++ b/builtin/cat-file.c @@ -0,0 +1,311 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "exec_cmd.h" +#include "tag.h" +#include "tree.h" +#include "builtin.h" +#include "parse-options.h" +#include "diff.h" +#include "userdiff.h" +#include "streaming.h" + +#define BATCH 1 +#define BATCH_CHECK 2 + +static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size) +{ + /* the parser in tag.c is useless here. */ + const char *endp = buf + size; + const char *cp = buf; + + while (cp < endp) { + char c = *cp++; + if (c != '\n') + continue; + if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) { + const char *tagger = cp; + + /* Found the tagger line. Copy out the contents + * of the buffer so far. + */ + write_or_die(1, buf, cp - buf); + + /* + * Do something intelligent, like pretty-printing + * the date. + */ + while (cp < endp) { + if (*cp++ == '\n') { + /* tagger to cp is a line + * that has ident and time. + */ + const char *sp = tagger; + char *ep; + unsigned long date; + long tz; + while (sp < cp && *sp != '>') + sp++; + if (sp == cp) { + /* give up */ + write_or_die(1, tagger, + cp - tagger); + break; + } + while (sp < cp && + !('0' <= *sp && *sp <= '9')) + sp++; + write_or_die(1, tagger, sp - tagger); + date = strtoul(sp, &ep, 10); + tz = strtol(ep, NULL, 10); + sp = show_date(date, tz, 0); + write_or_die(1, sp, strlen(sp)); + xwrite(1, "\n", 1); + break; + } + } + break; + } + if (cp < endp && *cp == '\n') + /* end of header */ + break; + } + /* At this point, we have copied out the header up to the end of + * the tagger line and cp points at one past \n. It could be the + * next header line after the tagger line, or it could be another + * \n that marks the end of the headers. We need to copy out the + * remainder as is. + */ + if (cp < endp) + write_or_die(1, cp, endp - cp); +} + +static int cat_one_file(int opt, const char *exp_type, const char *obj_name) +{ + unsigned char sha1[20]; + enum object_type type; + char *buf; + unsigned long size; + struct object_context obj_context; + + if (get_sha1_with_context(obj_name, 0, sha1, &obj_context)) + die("Not a valid object name %s", obj_name); + + buf = NULL; + switch (opt) { + case 't': + type = sha1_object_info(sha1, NULL); + if (type > 0) { + printf("%s\n", typename(type)); + return 0; + } + break; + + case 's': + type = sha1_object_info(sha1, &size); + if (type > 0) { + printf("%lu\n", size); + return 0; + } + break; + + case 'e': + return !has_sha1_file(sha1); + + case 'p': + type = sha1_object_info(sha1, NULL); + if (type < 0) + die("Not a valid object name %s", obj_name); + + /* custom pretty-print here */ + if (type == OBJ_TREE) { + const char *ls_args[3] = { NULL }; + ls_args[0] = "ls-tree"; + ls_args[1] = obj_name; + return cmd_ls_tree(2, ls_args, NULL); + } + + if (type == OBJ_BLOB) + return stream_blob_to_fd(1, sha1, NULL, 0); + buf = read_sha1_file(sha1, &type, &size); + if (!buf) + die("Cannot read object %s", obj_name); + if (type == OBJ_TAG) { + pprint_tag(sha1, buf, size); + return 0; + } + + /* otherwise just spit out the data */ + break; + + case 'c': + if (!obj_context.path[0]) + die("git cat-file --textconv %s: <object> must be <sha1:path>", + obj_name); + + if (!textconv_object(obj_context.path, obj_context.mode, sha1, 1, &buf, &size)) + die("git cat-file --textconv: unable to run textconv on %s", + obj_name); + break; + + case 0: + if (type_from_string(exp_type) == OBJ_BLOB) { + unsigned char blob_sha1[20]; + if (sha1_object_info(sha1, NULL) == OBJ_TAG) { + enum object_type type; + unsigned long size; + char *buffer = read_sha1_file(sha1, &type, &size); + if (memcmp(buffer, "object ", 7) || + get_sha1_hex(buffer + 7, blob_sha1)) + die("%s not a valid tag", sha1_to_hex(sha1)); + free(buffer); + } else + hashcpy(blob_sha1, sha1); + + if (sha1_object_info(blob_sha1, NULL) == OBJ_BLOB) + return stream_blob_to_fd(1, blob_sha1, NULL, 0); + /* + * we attempted to dereference a tag to a blob + * and failed; there may be new dereference + * mechanisms this code is not aware of. + * fall-back to the usual case. + */ + } + buf = read_object_with_reference(sha1, exp_type, &size, NULL); + break; + + default: + die("git cat-file: unknown option: %s", exp_type); + } + + if (!buf) + die("git cat-file %s: bad file", obj_name); + + 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 = 0; + unsigned long size; + void *contents = contents; + + if (!obj_name) + return 1; + + if (get_sha1(obj_name, sha1)) { + printf("%s missing\n", obj_name); + fflush(stdout); + return 0; + } + + if (print_contents == BATCH) + contents = read_sha1_file(sha1, &type, &size); + else + type = sha1_object_info(sha1, &size); + + if (type <= 0) { + printf("%s missing\n", obj_name); + fflush(stdout); + if (print_contents == BATCH) + free(contents); + return 0; + } + + 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); + free(contents); + } + + return 0; +} + +static int batch_objects(int print_contents) +{ + struct strbuf buf = STRBUF_INIT; + + 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[] = { + N_("git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>"), + N_("git cat-file (--batch|--batch-check) < <list_of_objects>"), + NULL +}; + +static int git_cat_file_config(const char *var, const char *value, void *cb) +{ + if (userdiff_config(var, value) < 0) + return -1; + + return git_default_config(var, value, cb); +} + +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(N_("<type> can be one of: blob, tree, commit, tag")), + OPT_SET_INT('t', NULL, &opt, N_("show object type"), 't'), + OPT_SET_INT('s', NULL, &opt, N_("show object size"), 's'), + OPT_SET_INT('e', NULL, &opt, + N_("exit with zero when there's no error"), 'e'), + OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'), + OPT_SET_INT(0, "textconv", &opt, + N_("for blob objects, run textconv on object's content"), 'c'), + OPT_SET_INT(0, "batch", &batch, + N_("show info and content of objects fed from the standard input"), + BATCH), + OPT_SET_INT(0, "batch-check", &batch, + N_("show info about objects fed from the standard input"), + BATCH_CHECK), + OPT_END() + }; + + git_config(git_cat_file_config, NULL); + + if (argc != 3 && argc != 2) + usage_with_options(cat_file_usage, options); + + argc = parse_options(argc, argv, prefix, 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/check-attr.c b/builtin/check-attr.c new file mode 100644 index 0000000000..075d01d30c --- /dev/null +++ b/builtin/check-attr.c @@ -0,0 +1,175 @@ +#include "builtin.h" +#include "cache.h" +#include "attr.h" +#include "quote.h" +#include "parse-options.h" + +static int all_attrs; +static int cached_attrs; +static int stdin_paths; +static const char * const check_attr_usage[] = { +N_("git check-attr [-a | --all | attr...] [--] pathname..."), +N_("git check-attr --stdin [-z] [-a | --all | attr...] < <list-of-paths>"), +NULL +}; + +static int null_term_line; + +static const struct option check_attr_options[] = { + OPT_BOOLEAN('a', "all", &all_attrs, N_("report all attributes set on file")), + OPT_BOOLEAN(0, "cached", &cached_attrs, N_("use .gitattributes only from the index")), + OPT_BOOLEAN(0 , "stdin", &stdin_paths, N_("read file names from stdin")), + OPT_BOOLEAN('z', NULL, &null_term_line, + N_("input paths are terminated by a null character")), + OPT_END() +}; + +static void output_attr(int cnt, struct git_attr_check *check, + const char *file) +{ + int j; + for (j = 0; j < cnt; j++) { + const char *value = check[j].value; + + if (ATTR_TRUE(value)) + value = "set"; + else if (ATTR_FALSE(value)) + value = "unset"; + else if (ATTR_UNSET(value)) + value = "unspecified"; + + quote_c_style(file, NULL, stdout, 0); + printf(": %s: %s\n", git_attr_name(check[j].attr), value); + } +} + +static void check_attr(const char *prefix, int cnt, + struct git_attr_check *check, const char *file) +{ + char *full_path = + prefix_path(prefix, prefix ? strlen(prefix) : 0, file); + if (check != NULL) { + if (git_check_attr(full_path, cnt, check)) + die("git_check_attr died"); + output_attr(cnt, check, file); + } else { + if (git_all_attrs(full_path, &cnt, &check)) + die("git_all_attrs died"); + output_attr(cnt, check, file); + free(check); + } + free(full_path); +} + +static void check_attr_stdin_paths(const char *prefix, int cnt, + struct git_attr_check *check) +{ + struct strbuf buf, nbuf; + int line_termination = null_term_line ? 0 : '\n'; + + strbuf_init(&buf, 0); + strbuf_init(&nbuf, 0); + while (strbuf_getline(&buf, stdin, line_termination) != EOF) { + if (line_termination && buf.buf[0] == '"') { + strbuf_reset(&nbuf); + if (unquote_c_style(&nbuf, buf.buf, NULL)) + die("line is badly quoted"); + strbuf_swap(&buf, &nbuf); + } + check_attr(prefix, cnt, check, buf.buf); + maybe_flush_or_die(stdout, "attribute to stdout"); + } + strbuf_release(&buf); + strbuf_release(&nbuf); +} + +static NORETURN void error_with_usage(const char *msg) +{ + error("%s", msg); + usage_with_options(check_attr_usage, check_attr_options); +} + +int cmd_check_attr(int argc, const char **argv, const char *prefix) +{ + struct git_attr_check *check; + int cnt, i, doubledash, filei; + + git_config(git_default_config, NULL); + + argc = parse_options(argc, argv, prefix, check_attr_options, + check_attr_usage, PARSE_OPT_KEEP_DASHDASH); + + if (read_cache() < 0) { + die("invalid cache"); + } + + if (cached_attrs) + git_attr_set_direction(GIT_ATTR_INDEX, NULL); + + doubledash = -1; + for (i = 0; doubledash < 0 && i < argc; i++) { + if (!strcmp(argv[i], "--")) + doubledash = i; + } + + /* Process --all and/or attribute arguments: */ + if (all_attrs) { + if (doubledash >= 1) + error_with_usage("Attributes and --all both specified"); + + cnt = 0; + filei = doubledash + 1; + } else if (doubledash == 0) { + error_with_usage("No attribute specified"); + } else if (doubledash < 0) { + if (!argc) + error_with_usage("No attribute specified"); + + if (stdin_paths) { + /* Treat all arguments as attribute names. */ + cnt = argc; + filei = argc; + } else { + /* Treat exactly one argument as an attribute name. */ + cnt = 1; + filei = 1; + } + } else { + cnt = doubledash; + filei = doubledash + 1; + } + + /* Check file argument(s): */ + if (stdin_paths) { + if (filei < argc) + error_with_usage("Can't specify files with --stdin"); + } else { + if (filei >= argc) + error_with_usage("No file specified"); + } + + if (all_attrs) { + check = NULL; + } else { + check = xcalloc(cnt, sizeof(*check)); + for (i = 0; i < cnt; i++) { + const char *name; + struct git_attr *a; + name = argv[i]; + a = git_attr(name); + if (!a) + return error("%s: not a valid attribute name", + name); + check[i].attr = a; + } + } + + if (stdin_paths) + check_attr_stdin_paths(prefix, cnt, check); + else { + for (i = filei; i < argc; i++) + check_attr(prefix, cnt, check, argv[i]); + maybe_flush_or_die(stdout, "attribute to stdout"); + } + return 0; +} diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c new file mode 100644 index 0000000000..28a7320271 --- /dev/null +++ b/builtin/check-ref-format.c @@ -0,0 +1,88 @@ +/* + * GIT - The information manager from hell + */ + +#include "cache.h" +#include "refs.h" +#include "builtin.h" +#include "strbuf.h" + +static const char builtin_check_ref_format_usage[] = +"git check-ref-format [--normalize] [options] <refname>\n" +" or: git check-ref-format --branch <branchname-shorthand>"; + +/* + * Return a copy of refname but with leading slashes removed and runs + * of adjacent slashes replaced with single slashes. + * + * This function is similar to normalize_path_copy(), but stripped down + * to meet check_ref_format's simpler needs. + */ +static char *collapse_slashes(const char *refname) +{ + char *ret = xmalloc(strlen(refname) + 1); + char ch; + char prev = '/'; + char *cp = ret; + + while ((ch = *refname++) != '\0') { + if (prev == '/' && ch == prev) + continue; + + *cp++ = ch; + prev = ch; + } + *cp = '\0'; + return ret; +} + +static int check_ref_format_branch(const char *arg) +{ + struct strbuf sb = STRBUF_INIT; + int nongit; + + setup_git_directory_gently(&nongit); + if (strbuf_check_branch_ref(&sb, arg)) + die("'%s' is not a valid branch name", arg); + printf("%s\n", sb.buf + 11); + return 0; +} + +int cmd_check_ref_format(int argc, const char **argv, const char *prefix) +{ + int i; + int normalize = 0; + int flags = 0; + const char *refname; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage(builtin_check_ref_format_usage); + + if (argc == 3 && !strcmp(argv[1], "--branch")) + return check_ref_format_branch(argv[2]); + + for (i = 1; i < argc && argv[i][0] == '-'; i++) { + if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print")) + normalize = 1; + else if (!strcmp(argv[i], "--allow-onelevel")) + flags |= REFNAME_ALLOW_ONELEVEL; + else if (!strcmp(argv[i], "--no-allow-onelevel")) + flags &= ~REFNAME_ALLOW_ONELEVEL; + else if (!strcmp(argv[i], "--refspec-pattern")) + flags |= REFNAME_REFSPEC_PATTERN; + else + usage(builtin_check_ref_format_usage); + } + if (! (i == argc - 1)) + usage(builtin_check_ref_format_usage); + + refname = argv[i]; + if (normalize) + refname = collapse_slashes(refname); + if (check_refname_format(refname, flags)) + return 1; + if (normalize) + printf("%s\n", refname); + + return 0; +} diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c new file mode 100644 index 0000000000..b1feda7d5e --- /dev/null +++ b/builtin/checkout-index.c @@ -0,0 +1,286 @@ +/* + * Check-out files from the "current cache directory" + * + * Copyright (C) 2005 Linus Torvalds + * + */ +#include "builtin.h" +#include "cache.h" +#include "quote.h" +#include "cache-tree.h" +#include "parse-options.h" + +#define CHECKOUT_ALL 4 +static int line_termination = '\n'; +static int checkout_stage; /* default to checkout stage0 */ +static int to_tempfile; +static char topath[4][PATH_MAX + 1]; + +static struct checkout state; + +static void write_tempfile_record(const char *name, int prefix_length) +{ + int i; + + if (CHECKOUT_ALL == checkout_stage) { + for (i = 1; i < 4; i++) { + if (i > 1) + putchar(' '); + if (topath[i][0]) + fputs(topath[i], stdout); + else + putchar('.'); + } + } else + fputs(topath[checkout_stage], stdout); + + putchar('\t'); + write_name_quoted(name + prefix_length, stdout, line_termination); + + for (i = 0; i < 4; i++) { + topath[i][0] = 0; + } +} + +static int checkout_file(const char *name, int prefix_length) +{ + int namelen = strlen(name); + int pos = cache_name_pos(name, namelen); + int has_same_name = 0; + int did_checkout = 0; + int errs = 0; + + if (pos < 0) + pos = -pos - 1; + + while (pos < active_nr) { + struct cache_entry *ce = active_cache[pos]; + if (ce_namelen(ce) != namelen || + memcmp(ce->name, name, namelen)) + break; + has_same_name = 1; + pos++; + if (ce_stage(ce) != checkout_stage + && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) + continue; + did_checkout = 1; + if (checkout_entry(ce, &state, + to_tempfile ? topath[ce_stage(ce)] : NULL) < 0) + errs++; + } + + if (did_checkout) { + if (to_tempfile) + write_tempfile_record(name, prefix_length); + return errs > 0 ? -1 : 0; + } + + if (!state.quiet) { + fprintf(stderr, "git checkout-index: %s ", name); + if (!has_same_name) + fprintf(stderr, "is not in the cache"); + else if (checkout_stage) + fprintf(stderr, "does not exist at stage %d", + checkout_stage); + else + fprintf(stderr, "is unmerged"); + fputc('\n', stderr); + } + return -1; +} + +static void checkout_all(const char *prefix, int prefix_length) +{ + int i, errs = 0; + struct cache_entry *last_ce = NULL; + + for (i = 0; i < active_nr ; i++) { + struct cache_entry *ce = active_cache[i]; + if (ce_stage(ce) != checkout_stage + && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) + continue; + if (prefix && *prefix && + (ce_namelen(ce) <= prefix_length || + memcmp(prefix, ce->name, prefix_length))) + continue; + if (last_ce && to_tempfile) { + if (ce_namelen(last_ce) != ce_namelen(ce) + || memcmp(last_ce->name, ce->name, ce_namelen(ce))) + write_tempfile_record(last_ce->name, prefix_length); + } + if (checkout_entry(ce, &state, + to_tempfile ? topath[ce_stage(ce)] : NULL) < 0) + errs++; + last_ce = ce; + } + if (last_ce && to_tempfile) + write_tempfile_record(last_ce->name, prefix_length); + if (errs) + /* we have already done our error reporting. + * exit with the same code as die(). + */ + exit(128); +} + +static const char * const builtin_checkout_index_usage[] = { + N_("git checkout-index [options] [--] [<file>...]"), + NULL +}; + +static struct lock_file lock_file; + +static int option_parse_u(const struct option *opt, + const char *arg, int unset) +{ + int *newfd = opt->value; + + state.refresh_cache = 1; + if (*newfd < 0) + *newfd = hold_locked_index(&lock_file, 1); + return 0; +} + +static int option_parse_z(const struct option *opt, + const char *arg, int unset) +{ + if (unset) + line_termination = '\n'; + else + line_termination = 0; + return 0; +} + +static int option_parse_prefix(const struct option *opt, + const char *arg, int unset) +{ + state.base_dir = arg; + state.base_dir_len = strlen(arg); + return 0; +} + +static int option_parse_stage(const struct option *opt, + const char *arg, int unset) +{ + if (!strcmp(arg, "all")) { + to_tempfile = 1; + checkout_stage = CHECKOUT_ALL; + } else { + int ch = arg[0]; + if ('1' <= ch && ch <= '3') + checkout_stage = arg[0] - '0'; + else + die("stage should be between 1 and 3 or all"); + } + return 0; +} + +int cmd_checkout_index(int argc, const char **argv, const char *prefix) +{ + int i; + int newfd = -1; + int all = 0; + int read_from_stdin = 0; + int prefix_length; + int force = 0, quiet = 0, not_new = 0; + struct option builtin_checkout_index_options[] = { + OPT_BOOLEAN('a', "all", &all, + N_("check out all files in the index")), + OPT__FORCE(&force, N_("force overwrite of existing files")), + OPT__QUIET(&quiet, + N_("no warning for existing files and files not in index")), + OPT_BOOLEAN('n', "no-create", ¬_new, + N_("don't checkout new files")), + { OPTION_CALLBACK, 'u', "index", &newfd, NULL, + N_("update stat information in the index file"), + PARSE_OPT_NOARG, option_parse_u }, + { OPTION_CALLBACK, 'z', NULL, NULL, NULL, + N_("paths are separated with NUL character"), + PARSE_OPT_NOARG, option_parse_z }, + OPT_BOOLEAN(0, "stdin", &read_from_stdin, + N_("read list of paths from the standard input")), + OPT_BOOLEAN(0, "temp", &to_tempfile, + N_("write the content to temporary files")), + OPT_CALLBACK(0, "prefix", NULL, N_("string"), + N_("when creating files, prepend <string>"), + option_parse_prefix), + OPT_CALLBACK(0, "stage", NULL, NULL, + N_("copy out the files from named stage"), + option_parse_stage), + OPT_END() + }; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(builtin_checkout_index_usage, + builtin_checkout_index_options); + git_config(git_default_config, NULL); + state.base_dir = ""; + prefix_length = prefix ? strlen(prefix) : 0; + + if (read_cache() < 0) { + die("invalid cache"); + } + + argc = parse_options(argc, argv, prefix, builtin_checkout_index_options, + builtin_checkout_index_usage, 0); + state.force = force; + state.quiet = quiet; + state.not_new = not_new; + + if (state.base_dir_len || to_tempfile) { + /* when --prefix is specified we do not + * want to update cache. + */ + if (state.refresh_cache) { + rollback_lock_file(&lock_file); + newfd = -1; + } + state.refresh_cache = 0; + } + + /* Check out named files first */ + for (i = 0; i < argc; i++) { + const char *arg = argv[i]; + const char *p; + + if (all) + die("git checkout-index: don't mix '--all' and explicit filenames"); + if (read_from_stdin) + die("git checkout-index: don't mix '--stdin' and explicit filenames"); + p = prefix_path(prefix, prefix_length, arg); + checkout_file(p, prefix_length); + if (p < arg || p > arg + strlen(arg)) + free((char *)p); + } + + if (read_from_stdin) { + struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; + + if (all) + die("git checkout-index: don't mix '--all' and '--stdin'"); + + while (strbuf_getline(&buf, stdin, line_termination) != EOF) { + const char *p; + if (line_termination && buf.buf[0] == '"') { + strbuf_reset(&nbuf); + if (unquote_c_style(&nbuf, buf.buf, NULL)) + die("line is badly quoted"); + strbuf_swap(&buf, &nbuf); + } + p = prefix_path(prefix, prefix_length, buf.buf); + checkout_file(p, prefix_length); + if (p < buf.buf || p > buf.buf + buf.len) + free((char *)p); + } + strbuf_release(&nbuf); + strbuf_release(&buf); + } + + if (all) + checkout_all(prefix, prefix_length); + + if (0 <= newfd && + (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(&lock_file))) + die("Unable to write new index file"); + return 0; +} diff --git a/builtin/checkout.c b/builtin/checkout.c new file mode 100644 index 0000000000..a9c1b5a95f --- /dev/null +++ b/builtin/checkout.c @@ -0,0 +1,1151 @@ +#include "cache.h" +#include "builtin.h" +#include "parse-options.h" +#include "refs.h" +#include "commit.h" +#include "tree.h" +#include "tree-walk.h" +#include "cache-tree.h" +#include "unpack-trees.h" +#include "dir.h" +#include "run-command.h" +#include "merge-recursive.h" +#include "branch.h" +#include "diff.h" +#include "revision.h" +#include "remote.h" +#include "blob.h" +#include "xdiff-interface.h" +#include "ll-merge.h" +#include "resolve-undo.h" +#include "submodule.h" +#include "argv-array.h" + +static const char * const checkout_usage[] = { + N_("git checkout [options] <branch>"), + N_("git checkout [options] [<branch>] -- <file>..."), + NULL, +}; + +struct checkout_opts { + int patch_mode; + int quiet; + int merge; + int force; + int force_detach; + int writeout_stage; + int overwrite_ignore; + + const char *new_branch; + const char *new_branch_force; + const char *new_orphan_branch; + int new_branch_log; + enum branch_track track; + struct diff_options diff_options; + + int branch_exists; + const char *prefix; + const char **pathspec; + struct tree *source_tree; +}; + +static int post_checkout_hook(struct commit *old, struct commit *new, + int changed) +{ + return run_hook(NULL, "post-checkout", + sha1_to_hex(old ? old->object.sha1 : null_sha1), + sha1_to_hex(new ? new->object.sha1 : null_sha1), + changed ? "1" : "0", NULL); + /* "new" can be NULL when checking out from the index before + a commit exists. */ + +} + +static int update_some(const unsigned char *sha1, const char *base, int baselen, + const char *pathname, unsigned mode, int stage, void *context) +{ + int len; + struct cache_entry *ce; + + if (S_ISDIR(mode)) + return READ_TREE_RECURSIVE; + + len = baselen + strlen(pathname); + ce = xcalloc(1, cache_entry_size(len)); + hashcpy(ce->sha1, sha1); + memcpy(ce->name, base, baselen); + memcpy(ce->name + baselen, pathname, len - baselen); + ce->ce_flags = create_ce_flags(0) | CE_UPDATE; + ce->ce_namelen = len; + ce->ce_mode = create_ce_mode(mode); + add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); + return 0; +} + +static int read_tree_some(struct tree *tree, const char **pathspec) +{ + struct pathspec ps; + init_pathspec(&ps, pathspec); + read_tree_recursive(tree, "", 0, 0, &ps, update_some, NULL); + free_pathspec(&ps); + + /* update the index with the given tree's info + * for all args, expanding wildcards, and exit + * with any non-zero return code. + */ + return 0; +} + +static int skip_same_name(struct cache_entry *ce, int pos) +{ + while (++pos < active_nr && + !strcmp(active_cache[pos]->name, ce->name)) + ; /* skip */ + return pos; +} + +static int check_stage(int stage, struct cache_entry *ce, int pos) +{ + while (pos < active_nr && + !strcmp(active_cache[pos]->name, ce->name)) { + if (ce_stage(active_cache[pos]) == stage) + return 0; + pos++; + } + if (stage == 2) + return error(_("path '%s' does not have our version"), ce->name); + else + return error(_("path '%s' does not have their version"), ce->name); +} + +static int check_stages(unsigned stages, struct cache_entry *ce, int pos) +{ + unsigned seen = 0; + const char *name = ce->name; + + while (pos < active_nr) { + ce = active_cache[pos]; + if (strcmp(name, ce->name)) + break; + seen |= (1 << ce_stage(ce)); + pos++; + } + if ((stages & seen) != stages) + return error(_("path '%s' does not have all necessary versions"), + name); + return 0; +} + +static int checkout_stage(int stage, struct cache_entry *ce, int pos, + struct checkout *state) +{ + while (pos < active_nr && + !strcmp(active_cache[pos]->name, ce->name)) { + if (ce_stage(active_cache[pos]) == stage) + return checkout_entry(active_cache[pos], state, NULL); + pos++; + } + if (stage == 2) + return error(_("path '%s' does not have our version"), ce->name); + else + return error(_("path '%s' does not have their version"), ce->name); +} + +static int checkout_merged(int pos, struct checkout *state) +{ + struct cache_entry *ce = active_cache[pos]; + const char *path = ce->name; + mmfile_t ancestor, ours, theirs; + int status; + unsigned char sha1[20]; + mmbuffer_t result_buf; + unsigned char threeway[3][20]; + unsigned mode = 0; + + memset(threeway, 0, sizeof(threeway)); + while (pos < active_nr) { + int stage; + stage = ce_stage(ce); + if (!stage || strcmp(path, ce->name)) + break; + hashcpy(threeway[stage - 1], ce->sha1); + if (stage == 2) + mode = create_ce_mode(ce->ce_mode); + pos++; + ce = active_cache[pos]; + } + if (is_null_sha1(threeway[1]) || is_null_sha1(threeway[2])) + return error(_("path '%s' does not have necessary versions"), path); + + read_mmblob(&ancestor, threeway[0]); + read_mmblob(&ours, threeway[1]); + read_mmblob(&theirs, threeway[2]); + + /* + * NEEDSWORK: re-create conflicts from merges with + * merge.renormalize set, too + */ + status = ll_merge(&result_buf, path, &ancestor, "base", + &ours, "ours", &theirs, "theirs", NULL); + free(ancestor.ptr); + free(ours.ptr); + free(theirs.ptr); + if (status < 0 || !result_buf.ptr) { + free(result_buf.ptr); + return error(_("path '%s': cannot merge"), path); + } + + /* + * NEEDSWORK: + * There is absolutely no reason to write this as a blob object + * and create a phony cache entry just to leak. This hack is + * primarily to get to the write_entry() machinery that massages + * the contents to work-tree format and writes out which only + * allows it for a cache entry. The code in write_entry() needs + * to be refactored to allow us to feed a <buffer, size, mode> + * instead of a cache entry. Such a refactoring would help + * merge_recursive as well (it also writes the merge result to the + * object database even when it may contain conflicts). + */ + if (write_sha1_file(result_buf.ptr, result_buf.size, + blob_type, sha1)) + die(_("Unable to add merge result for '%s'"), path); + ce = make_cache_entry(mode, sha1, path, 2, 0); + if (!ce) + die(_("make_cache_entry failed for path '%s'"), path); + status = checkout_entry(ce, state, NULL); + return status; +} + +static int checkout_paths(const struct checkout_opts *opts, + const char *revision) +{ + int pos; + struct checkout state; + static char *ps_matched; + unsigned char rev[20]; + int flag; + struct commit *head; + int errs = 0; + int stage = opts->writeout_stage; + int merge = opts->merge; + int newfd; + struct lock_file *lock_file; + + if (opts->track != BRANCH_TRACK_UNSPECIFIED) + die(_("'%s' cannot be used with updating paths"), "--track"); + + if (opts->new_branch_log) + die(_("'%s' cannot be used with updating paths"), "-l"); + + if (opts->force && opts->patch_mode) + die(_("'%s' cannot be used with updating paths"), "-f"); + + if (opts->force_detach) + die(_("'%s' cannot be used with updating paths"), "--detach"); + + if (opts->merge && opts->patch_mode) + die(_("'%s' cannot be used with %s"), "--merge", "--patch"); + + if (opts->force && opts->merge) + die(_("'%s' cannot be used with %s"), "-f", "-m"); + + if (opts->new_branch) + die(_("Cannot update paths and switch to branch '%s' at the same time."), + opts->new_branch); + + if (opts->patch_mode) + return run_add_interactive(revision, "--patch=checkout", + opts->pathspec); + + lock_file = xcalloc(1, sizeof(struct lock_file)); + + newfd = hold_locked_index(lock_file, 1); + if (read_cache_preload(opts->pathspec) < 0) + return error(_("corrupt index file")); + + if (opts->source_tree) + read_tree_some(opts->source_tree, opts->pathspec); + + for (pos = 0; opts->pathspec[pos]; pos++) + ; + ps_matched = xcalloc(1, pos); + + for (pos = 0; pos < active_nr; pos++) { + struct cache_entry *ce = active_cache[pos]; + if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) + continue; + match_pathspec(opts->pathspec, ce->name, ce_namelen(ce), 0, ps_matched); + } + + if (report_path_error(ps_matched, opts->pathspec, opts->prefix)) + return 1; + + /* "checkout -m path" to recreate conflicted state */ + if (opts->merge) + unmerge_cache(opts->pathspec); + + /* Any unmerged paths? */ + for (pos = 0; pos < active_nr; pos++) { + struct cache_entry *ce = active_cache[pos]; + if (match_pathspec(opts->pathspec, ce->name, ce_namelen(ce), 0, NULL)) { + if (!ce_stage(ce)) + continue; + if (opts->force) { + warning(_("path '%s' is unmerged"), ce->name); + } else if (stage) { + errs |= check_stage(stage, ce, pos); + } else if (opts->merge) { + errs |= check_stages((1<<2) | (1<<3), ce, pos); + } else { + errs = 1; + error(_("path '%s' is unmerged"), ce->name); + } + pos = skip_same_name(ce, pos) - 1; + } + } + if (errs) + return 1; + + /* Now we are committed to check them out */ + memset(&state, 0, sizeof(state)); + state.force = 1; + state.refresh_cache = 1; + for (pos = 0; pos < active_nr; pos++) { + struct cache_entry *ce = active_cache[pos]; + if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) + continue; + if (match_pathspec(opts->pathspec, ce->name, ce_namelen(ce), 0, NULL)) { + if (!ce_stage(ce)) { + errs |= checkout_entry(ce, &state, NULL); + continue; + } + if (stage) + errs |= checkout_stage(stage, ce, pos, &state); + else if (merge) + errs |= checkout_merged(pos, &state); + pos = skip_same_name(ce, pos) - 1; + } + } + + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(lock_file)) + die(_("unable to write new index file")); + + read_ref_full("HEAD", rev, 0, &flag); + head = lookup_commit_reference_gently(rev, 1); + + errs |= post_checkout_hook(head, head, 0); + return errs; +} + +static void show_local_changes(struct object *head, + const struct diff_options *opts) +{ + struct rev_info rev; + /* I think we want full paths, even if we're in a subdirectory. */ + init_revisions(&rev, NULL); + rev.diffopt.flags = opts->flags; + rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; + diff_setup_done(&rev.diffopt); + add_pending_object(&rev, head, NULL); + run_diff_index(&rev, 0); +} + +static void describe_detached_head(const char *msg, struct commit *commit) +{ + struct strbuf sb = STRBUF_INIT; + parse_commit(commit); + pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); + fprintf(stderr, "%s %s... %s\n", msg, + find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf); + strbuf_release(&sb); +} + +static int reset_tree(struct tree *tree, const struct checkout_opts *o, + int worktree, int *writeout_error) +{ + struct unpack_trees_options opts; + struct tree_desc tree_desc; + + memset(&opts, 0, sizeof(opts)); + opts.head_idx = -1; + opts.update = worktree; + opts.skip_unmerged = !worktree; + opts.reset = 1; + opts.merge = 1; + opts.fn = oneway_merge; + opts.verbose_update = !o->quiet && isatty(2); + opts.src_index = &the_index; + opts.dst_index = &the_index; + parse_tree(tree); + init_tree_desc(&tree_desc, tree->buffer, tree->size); + switch (unpack_trees(1, &tree_desc, &opts)) { + case -2: + *writeout_error = 1; + /* + * We return 0 nevertheless, as the index is all right + * and more importantly we have made best efforts to + * update paths in the work tree, and we cannot revert + * them. + */ + case 0: + return 0; + default: + return 128; + } +} + +struct branch_info { + const char *name; /* The short name used */ + const char *path; /* The full name of a real branch */ + struct commit *commit; /* The named commit */ +}; + +static void setup_branch_path(struct branch_info *branch) +{ + struct strbuf buf = STRBUF_INIT; + + strbuf_branchname(&buf, branch->name); + if (strcmp(buf.buf, branch->name)) + branch->name = xstrdup(buf.buf); + strbuf_splice(&buf, 0, 0, "refs/heads/", 11); + branch->path = strbuf_detach(&buf, NULL); +} + +static int merge_working_tree(const struct checkout_opts *opts, + struct branch_info *old, + struct branch_info *new, + int *writeout_error) +{ + int ret; + struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); + int newfd = hold_locked_index(lock_file, 1); + + if (read_cache_preload(NULL) < 0) + return error(_("corrupt index file")); + + resolve_undo_clear(); + if (opts->force) { + ret = reset_tree(new->commit->tree, opts, 1, writeout_error); + if (ret) + return ret; + } else { + struct tree_desc trees[2]; + struct tree *tree; + struct unpack_trees_options topts; + + memset(&topts, 0, sizeof(topts)); + topts.head_idx = -1; + topts.src_index = &the_index; + topts.dst_index = &the_index; + + setup_unpack_trees_porcelain(&topts, "checkout"); + + refresh_cache(REFRESH_QUIET); + + if (unmerged_cache()) { + error(_("you need to resolve your current index first")); + return 1; + } + + /* 2-way merge to the new branch */ + topts.initial_checkout = is_cache_unborn(); + topts.update = 1; + topts.merge = 1; + topts.gently = opts->merge && old->commit; + topts.verbose_update = !opts->quiet && isatty(2); + topts.fn = twoway_merge; + if (opts->overwrite_ignore) { + topts.dir = xcalloc(1, sizeof(*topts.dir)); + topts.dir->flags |= DIR_SHOW_IGNORED; + setup_standard_excludes(topts.dir); + } + tree = parse_tree_indirect(old->commit ? + old->commit->object.sha1 : + EMPTY_TREE_SHA1_BIN); + init_tree_desc(&trees[0], tree->buffer, tree->size); + tree = parse_tree_indirect(new->commit->object.sha1); + init_tree_desc(&trees[1], tree->buffer, tree->size); + + ret = unpack_trees(2, trees, &topts); + if (ret == -1) { + /* + * Unpack couldn't do a trivial merge; either + * give up or do a real merge, depending on + * whether the merge flag was used. + */ + struct tree *result; + struct tree *work; + struct merge_options o; + if (!opts->merge) + return 1; + + /* + * Without old->commit, the below is the same as + * the two-tree unpack we already tried and failed. + */ + if (!old->commit) + return 1; + + /* Do more real merge */ + + /* + * We update the index fully, then write the + * tree from the index, then merge the new + * branch with the current tree, with the old + * branch as the base. Then we reset the index + * (but not the working tree) to the new + * branch, leaving the working tree as the + * merged version, but skipping unmerged + * entries in the index. + */ + + add_files_to_cache(NULL, NULL, 0); + /* + * NEEDSWORK: carrying over local changes + * when branches have different end-of-line + * normalization (or clean+smudge rules) is + * a pain; plumb in an option to set + * o.renormalize? + */ + init_merge_options(&o); + o.verbosity = 0; + work = write_tree_from_memory(&o); + + ret = reset_tree(new->commit->tree, opts, 1, + writeout_error); + if (ret) + return ret; + o.ancestor = old->name; + o.branch1 = new->name; + o.branch2 = "local"; + merge_trees(&o, new->commit->tree, work, + old->commit->tree, &result); + ret = reset_tree(new->commit->tree, opts, 0, + writeout_error); + if (ret) + return ret; + } + } + + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(lock_file)) + die(_("unable to write new index file")); + + if (!opts->force && !opts->quiet) + show_local_changes(&new->commit->object, &opts->diff_options); + + return 0; +} + +static void report_tracking(struct branch_info *new) +{ + struct strbuf sb = STRBUF_INIT; + struct branch *branch = branch_get(new->name); + + if (!format_tracking_info(branch, &sb)) + return; + fputs(sb.buf, stdout); + strbuf_release(&sb); +} + +static void update_refs_for_switch(const struct checkout_opts *opts, + struct branch_info *old, + struct branch_info *new) +{ + struct strbuf msg = STRBUF_INIT; + const char *old_desc; + if (opts->new_branch) { + if (opts->new_orphan_branch) { + if (opts->new_branch_log && !log_all_ref_updates) { + int temp; + char log_file[PATH_MAX]; + char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch); + + temp = log_all_ref_updates; + log_all_ref_updates = 1; + if (log_ref_setup(ref_name, log_file, sizeof(log_file))) { + fprintf(stderr, _("Can not do reflog for '%s'\n"), + opts->new_orphan_branch); + log_all_ref_updates = temp; + return; + } + log_all_ref_updates = temp; + } + } + else + create_branch(old->name, opts->new_branch, new->name, + opts->new_branch_force ? 1 : 0, + opts->new_branch_log, + opts->new_branch_force ? 1 : 0, + opts->quiet, + opts->track); + new->name = opts->new_branch; + setup_branch_path(new); + } + + old_desc = old->name; + if (!old_desc && old->commit) + old_desc = sha1_to_hex(old->commit->object.sha1); + strbuf_addf(&msg, "checkout: moving from %s to %s", + old_desc ? old_desc : "(invalid)", new->name); + + if (!strcmp(new->name, "HEAD") && !new->path && !opts->force_detach) { + /* Nothing to do. */ + } else if (opts->force_detach || !new->path) { /* No longer on any branch. */ + update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL, + REF_NODEREF, DIE_ON_ERR); + if (!opts->quiet) { + if (old->path && advice_detached_head) + detach_advice(new->name); + describe_detached_head(_("HEAD is now at"), new->commit); + } + } else if (new->path) { /* Switch branches. */ + create_symref("HEAD", new->path, msg.buf); + if (!opts->quiet) { + if (old->path && !strcmp(new->path, old->path)) { + if (opts->new_branch_force) + fprintf(stderr, _("Reset branch '%s'\n"), + new->name); + else + fprintf(stderr, _("Already on '%s'\n"), + new->name); + } else if (opts->new_branch) { + if (opts->branch_exists) + fprintf(stderr, _("Switched to and reset branch '%s'\n"), new->name); + else + fprintf(stderr, _("Switched to a new branch '%s'\n"), new->name); + } else { + fprintf(stderr, _("Switched to branch '%s'\n"), + new->name); + } + } + if (old->path && old->name) { + char log_file[PATH_MAX], ref_file[PATH_MAX]; + + git_snpath(log_file, sizeof(log_file), "logs/%s", old->path); + git_snpath(ref_file, sizeof(ref_file), "%s", old->path); + if (!file_exists(ref_file) && file_exists(log_file)) + remove_path(log_file); + } + } + remove_branch_state(); + strbuf_release(&msg); + if (!opts->quiet && + (new->path || (!opts->force_detach && !strcmp(new->name, "HEAD")))) + report_tracking(new); +} + +static int add_pending_uninteresting_ref(const char *refname, + const unsigned char *sha1, + int flags, void *cb_data) +{ + add_pending_sha1(cb_data, refname, sha1, UNINTERESTING); + return 0; +} + +static void describe_one_orphan(struct strbuf *sb, struct commit *commit) +{ + parse_commit(commit); + strbuf_addstr(sb, " "); + strbuf_addstr(sb, + find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); + strbuf_addch(sb, ' '); + pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); + strbuf_addch(sb, '\n'); +} + +#define ORPHAN_CUTOFF 4 +static void suggest_reattach(struct commit *commit, struct rev_info *revs) +{ + struct commit *c, *last = NULL; + struct strbuf sb = STRBUF_INIT; + int lost = 0; + while ((c = get_revision(revs)) != NULL) { + if (lost < ORPHAN_CUTOFF) + describe_one_orphan(&sb, c); + last = c; + lost++; + } + if (ORPHAN_CUTOFF < lost) { + int more = lost - ORPHAN_CUTOFF; + if (more == 1) + describe_one_orphan(&sb, last); + else + strbuf_addf(&sb, _(" ... and %d more.\n"), more); + } + + fprintf(stderr, + Q_( + /* The singular version */ + "Warning: you are leaving %d commit behind, " + "not connected to\n" + "any of your branches:\n\n" + "%s\n", + /* The plural version */ + "Warning: you are leaving %d commits behind, " + "not connected to\n" + "any of your branches:\n\n" + "%s\n", + /* Give ngettext() the count */ + lost), + lost, + sb.buf); + strbuf_release(&sb); + + if (advice_detached_head) + fprintf(stderr, + _( + "If you want to keep them by creating a new branch, " + "this may be a good time\nto do so with:\n\n" + " git branch new_branch_name %s\n\n"), + sha1_to_hex(commit->object.sha1)); +} + +/* + * We are about to leave commit that was at the tip of a detached + * HEAD. If it is not reachable from any ref, this is the last chance + * for the user to do so without resorting to reflog. + */ +static void orphaned_commit_warning(struct commit *old, struct commit *new) +{ + struct rev_info revs; + struct object *object = &old->object; + struct object_array refs; + + init_revisions(&revs, NULL); + setup_revisions(0, NULL, &revs, NULL); + + object->flags &= ~UNINTERESTING; + add_pending_object(&revs, object, sha1_to_hex(object->sha1)); + + for_each_ref(add_pending_uninteresting_ref, &revs); + add_pending_sha1(&revs, "HEAD", new->object.sha1, UNINTERESTING); + + refs = revs.pending; + revs.leak_pending = 1; + + if (prepare_revision_walk(&revs)) + die(_("internal error in revision walk")); + if (!(old->object.flags & UNINTERESTING)) + suggest_reattach(old, &revs); + else + describe_detached_head(_("Previous HEAD position was"), old); + + clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS); + free(refs.objects); +} + +static int switch_branches(const struct checkout_opts *opts, + struct branch_info *new) +{ + int ret = 0; + struct branch_info old; + void *path_to_free; + unsigned char rev[20]; + int flag, writeout_error = 0; + memset(&old, 0, sizeof(old)); + old.path = path_to_free = resolve_refdup("HEAD", rev, 0, &flag); + old.commit = lookup_commit_reference_gently(rev, 1); + if (!(flag & REF_ISSYMREF)) + old.path = NULL; + + if (old.path && !prefixcmp(old.path, "refs/heads/")) + old.name = old.path + strlen("refs/heads/"); + + if (!new->name) { + new->name = "HEAD"; + new->commit = old.commit; + if (!new->commit) + die(_("You are on a branch yet to be born")); + parse_commit(new->commit); + } + + ret = merge_working_tree(opts, &old, new, &writeout_error); + if (ret) { + free(path_to_free); + return ret; + } + + if (!opts->quiet && !old.path && old.commit && new->commit != old.commit) + orphaned_commit_warning(old.commit, new->commit); + + update_refs_for_switch(opts, &old, new); + + ret = post_checkout_hook(old.commit, new->commit, 1); + free(path_to_free); + return ret || writeout_error; +} + +static int git_checkout_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "diff.ignoresubmodules")) { + struct checkout_opts *opts = cb; + handle_ignore_submodules_arg(&opts->diff_options, value); + return 0; + } + + if (!prefixcmp(var, "submodule.")) + return parse_submodule_config_option(var, value); + + return git_xmerge_config(var, value, NULL); +} + +struct tracking_name_data { + const char *name; + char *remote; + int unique; +}; + +static int check_tracking_name(const char *refname, const unsigned char *sha1, + int flags, void *cb_data) +{ + struct tracking_name_data *cb = cb_data; + const char *slash; + + if (prefixcmp(refname, "refs/remotes/")) + return 0; + slash = strchr(refname + 13, '/'); + if (!slash || strcmp(slash + 1, cb->name)) + return 0; + if (cb->remote) { + cb->unique = 0; + return 0; + } + cb->remote = xstrdup(refname); + return 0; +} + +static const char *unique_tracking_name(const char *name) +{ + struct tracking_name_data cb_data = { NULL, NULL, 1 }; + cb_data.name = name; + for_each_ref(check_tracking_name, &cb_data); + if (cb_data.unique) + return cb_data.remote; + free(cb_data.remote); + return NULL; +} + +static int parse_branchname_arg(int argc, const char **argv, + int dwim_new_local_branch_ok, + struct branch_info *new, + struct tree **source_tree, + unsigned char rev[20], + const char **new_branch) +{ + int argcount = 0; + unsigned char branch_rev[20]; + const char *arg; + int has_dash_dash; + + /* + * case 1: git checkout <ref> -- [<paths>] + * + * <ref> must be a valid tree, everything after the '--' must be + * a path. + * + * case 2: git checkout -- [<paths>] + * + * everything after the '--' must be paths. + * + * case 3: git checkout <something> [<paths>] + * + * With no paths, if <something> is a commit, that is to + * switch to the branch or detach HEAD at it. As a special case, + * if <something> is A...B (missing A or B means HEAD but you can + * omit at most one side), and if there is a unique merge base + * between A and B, A...B names that merge base. + * + * With no paths, if <something> is _not_ a commit, no -t nor -b + * was given, and there is a tracking branch whose name is + * <something> in one and only one remote, then this is a short-hand + * to fork local <something> from that remote-tracking branch. + * + * Otherwise <something> shall not be ambiguous. + * - If it's *only* a reference, treat it like case (1). + * - If it's only a path, treat it like case (2). + * - else: fail. + * + */ + if (!argc) + return 0; + + if (!strcmp(argv[0], "--")) /* case (2) */ + return 1; + + arg = argv[0]; + has_dash_dash = (argc > 1) && !strcmp(argv[1], "--"); + + if (!strcmp(arg, "-")) + arg = "@{-1}"; + + if (get_sha1_mb(arg, rev)) { + if (has_dash_dash) /* case (1) */ + die(_("invalid reference: %s"), arg); + if (dwim_new_local_branch_ok && + !check_filename(NULL, arg) && + argc == 1) { + const char *remote = unique_tracking_name(arg); + if (!remote || get_sha1(remote, rev)) + return argcount; + *new_branch = arg; + arg = remote; + /* DWIMmed to create local branch */ + } else { + return argcount; + } + } + + /* we can't end up being in (2) anymore, eat the argument */ + argcount++; + argv++; + argc--; + + new->name = arg; + setup_branch_path(new); + + if (!check_refname_format(new->path, 0) && + !read_ref(new->path, branch_rev)) + hashcpy(rev, branch_rev); + else + new->path = NULL; /* not an existing branch */ + + new->commit = lookup_commit_reference_gently(rev, 1); + if (!new->commit) { + /* not a commit */ + *source_tree = parse_tree_indirect(rev); + } else { + parse_commit(new->commit); + *source_tree = new->commit->tree; + } + + if (!*source_tree) /* case (1): want a tree */ + die(_("reference is not a tree: %s"), arg); + if (!has_dash_dash) {/* case (3 -> 1) */ + /* + * Do not complain the most common case + * git checkout branch + * even if there happen to be a file called 'branch'; + * it would be extremely annoying. + */ + if (argc) + verify_non_filename(NULL, arg); + } else { + argcount++; + argv++; + argc--; + } + + return argcount; +} + +static int switch_unborn_to_new_branch(const struct checkout_opts *opts) +{ + int status; + struct strbuf branch_ref = STRBUF_INIT; + + if (!opts->new_branch) + die(_("You are on a branch yet to be born")); + strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch); + status = create_symref("HEAD", branch_ref.buf, "checkout -b"); + strbuf_release(&branch_ref); + if (!opts->quiet) + fprintf(stderr, _("Switched to a new branch '%s'\n"), + opts->new_branch); + return status; +} + +static int checkout_branch(struct checkout_opts *opts, + struct branch_info *new) +{ + if (opts->pathspec) + die(_("paths cannot be used with switching branches")); + + if (opts->patch_mode) + die(_("'%s' cannot be used with switching branches"), + "--patch"); + + if (opts->writeout_stage) + die(_("'%s' cannot be used with switching branches"), + "--ours/--theirs"); + + if (opts->force && opts->merge) + die(_("'%s' cannot be used with '%s'"), "-f", "-m"); + + if (opts->force_detach && opts->new_branch) + die(_("'%s' cannot be used with '%s'"), + "--detach", "-b/-B/--orphan"); + + if (opts->new_orphan_branch) { + if (opts->track != BRANCH_TRACK_UNSPECIFIED) + die(_("'%s' cannot be used with '%s'"), "--orphan", "-t"); + } else if (opts->force_detach) { + if (opts->track != BRANCH_TRACK_UNSPECIFIED) + die(_("'%s' cannot be used with '%s'"), "--detach", "-t"); + } else if (opts->track == BRANCH_TRACK_UNSPECIFIED) + opts->track = git_branch_track; + + if (new->name && !new->commit) + die(_("Cannot switch branch to a non-commit '%s'"), + new->name); + + if (!new->commit && opts->new_branch) { + unsigned char rev[20]; + int flag; + + if (!read_ref_full("HEAD", rev, 0, &flag) && + (flag & REF_ISSYMREF) && is_null_sha1(rev)) + return switch_unborn_to_new_branch(opts); + } + return switch_branches(opts, new); +} + +int cmd_checkout(int argc, const char **argv, const char *prefix) +{ + struct checkout_opts opts; + struct branch_info new; + char *conflict_style = NULL; + int dwim_new_local_branch = 1; + struct option options[] = { + OPT__QUIET(&opts.quiet, N_("suppress progress reporting")), + OPT_STRING('b', NULL, &opts.new_branch, N_("branch"), + N_("create and checkout a new branch")), + OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"), + N_("create/reset and checkout a branch")), + OPT_BOOLEAN('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")), + OPT_BOOLEAN(0, "detach", &opts.force_detach, N_("detach the HEAD at named commit")), + OPT_SET_INT('t', "track", &opts.track, N_("set upstream info for new branch"), + BRANCH_TRACK_EXPLICIT), + OPT_STRING(0, "orphan", &opts.new_orphan_branch, N_("new branch"), N_("new unparented branch")), + OPT_SET_INT('2', "ours", &opts.writeout_stage, N_("checkout our version for unmerged files"), + 2), + OPT_SET_INT('3', "theirs", &opts.writeout_stage, N_("checkout their version for unmerged files"), + 3), + OPT__FORCE(&opts.force, N_("force checkout (throw away local modifications)")), + OPT_BOOLEAN('m', "merge", &opts.merge, N_("perform a 3-way merge with the new branch")), + OPT_BOOLEAN(0, "overwrite-ignore", &opts.overwrite_ignore, N_("update ignored files (default)")), + OPT_STRING(0, "conflict", &conflict_style, N_("style"), + N_("conflict style (merge or diff3)")), + OPT_BOOLEAN('p', "patch", &opts.patch_mode, N_("select hunks interactively")), + { OPTION_BOOLEAN, 0, "guess", &dwim_new_local_branch, NULL, + N_("second guess 'git checkout no-such-branch'"), + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, + OPT_END(), + }; + + memset(&opts, 0, sizeof(opts)); + memset(&new, 0, sizeof(new)); + opts.overwrite_ignore = 1; + opts.prefix = prefix; + + gitmodules_config(); + git_config(git_checkout_config, &opts); + + opts.track = BRANCH_TRACK_UNSPECIFIED; + + argc = parse_options(argc, argv, prefix, options, checkout_usage, + PARSE_OPT_KEEP_DASHDASH); + + if (conflict_style) { + opts.merge = 1; /* implied */ + git_xmerge_config("merge.conflictstyle", conflict_style, NULL); + } + + if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1) + die(_("-b, -B and --orphan are mutually exclusive")); + + /* + * From here on, new_branch will contain the branch to be checked out, + * and new_branch_force and new_orphan_branch will tell us which one of + * -b/-B/--orphan is being used. + */ + if (opts.new_branch_force) + opts.new_branch = opts.new_branch_force; + + if (opts.new_orphan_branch) + opts.new_branch = opts.new_orphan_branch; + + /* --track without -b/-B/--orphan should DWIM */ + if (opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) { + const char *argv0 = argv[0]; + if (!argc || !strcmp(argv0, "--")) + die (_("--track needs a branch name")); + if (!prefixcmp(argv0, "refs/")) + argv0 += 5; + if (!prefixcmp(argv0, "remotes/")) + argv0 += 8; + argv0 = strchr(argv0, '/'); + if (!argv0 || !argv0[1]) + die (_("Missing branch name; try -b")); + opts.new_branch = argv0 + 1; + } + + /* + * Extract branch name from command line arguments, so + * all that is left is pathspecs. + * + * Handle + * + * 1) git checkout <tree> -- [<paths>] + * 2) git checkout -- [<paths>] + * 3) git checkout <something> [<paths>] + * + * including "last branch" syntax and DWIM-ery for names of + * remote branches, erroring out for invalid or ambiguous cases. + */ + if (argc) { + unsigned char rev[20]; + int dwim_ok = + !opts.patch_mode && + dwim_new_local_branch && + opts.track == BRANCH_TRACK_UNSPECIFIED && + !opts.new_branch; + int n = parse_branchname_arg(argc, argv, dwim_ok, + &new, &opts.source_tree, + rev, &opts.new_branch); + argv += n; + argc -= n; + } + + if (argc) { + opts.pathspec = get_pathspec(prefix, argv); + + if (!opts.pathspec) + die(_("invalid path specification")); + + /* + * Try to give more helpful suggestion. + * new_branch && argc > 1 will be caught later. + */ + if (opts.new_branch && argc == 1) + die(_("Cannot update paths and switch to branch '%s' at the same time.\n" + "Did you intend to checkout '%s' which can not be resolved as commit?"), + opts.new_branch, argv[0]); + + if (opts.force_detach) + die(_("git checkout: --detach does not take a path argument '%s'"), + argv[0]); + + if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge) + die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" + "checking out of the index.")); + } + + if (opts.new_branch) { + struct strbuf buf = STRBUF_INIT; + + opts.branch_exists = + validate_new_branchname(opts.new_branch, &buf, + !!opts.new_branch_force, + !!opts.new_branch_force); + + strbuf_release(&buf); + } + + if (opts.patch_mode || opts.pathspec) + return checkout_paths(&opts, new.name); + else + return checkout_branch(&opts, &new); +} diff --git a/builtin/clean.c b/builtin/clean.c new file mode 100644 index 0000000000..f4b760bf3d --- /dev/null +++ b/builtin/clean.c @@ -0,0 +1,288 @@ +/* + * "git clean" builtin command + * + * Copyright (C) 2007 Shawn Bohrer + * + * Based on git-clean.sh by Pavel Roskin + */ + +#include "builtin.h" +#include "cache.h" +#include "dir.h" +#include "parse-options.h" +#include "refs.h" +#include "string-list.h" +#include "quote.h" + +static int force = -1; /* unset */ + +static const char *const builtin_clean_usage[] = { + N_("git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."), + NULL +}; + +static const char *msg_remove = N_("Removing %s\n"); +static const char *msg_would_remove = N_("Would remove %s\n"); +static const char *msg_skip_git_dir = N_("Skipping repository %s\n"); +static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n"); +static const char *msg_warn_remove_failed = N_("failed to remove %s"); + +static int git_clean_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "clean.requireforce")) + force = !git_config_bool(var, value); + return git_default_config(var, value, cb); +} + +static int exclude_cb(const struct option *opt, const char *arg, int unset) +{ + struct string_list *exclude_list = opt->value; + string_list_append(exclude_list, arg); + return 0; +} + +static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, + int dry_run, int quiet, int *dir_gone) +{ + DIR *dir; + struct strbuf quoted = STRBUF_INIT; + struct dirent *e; + int res = 0, ret = 0, gone = 1, original_len = path->len, len, i; + unsigned char submodule_head[20]; + struct string_list dels = STRING_LIST_INIT_DUP; + + *dir_gone = 1; + + if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && + !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) { + if (!quiet) { + quote_path_relative(path->buf, strlen(path->buf), "ed, prefix); + printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir), + quoted.buf); + } + + *dir_gone = 0; + return 0; + } + + dir = opendir(path->buf); + if (!dir) { + /* an empty dir could be removed even if it is unreadble */ + res = dry_run ? 0 : rmdir(path->buf); + if (res) { + quote_path_relative(path->buf, strlen(path->buf), "ed, prefix); + warning(_(msg_warn_remove_failed), quoted.buf); + *dir_gone = 0; + } + return res; + } + + if (path->buf[original_len - 1] != '/') + strbuf_addch(path, '/'); + + len = path->len; + while ((e = readdir(dir)) != NULL) { + struct stat st; + if (is_dot_or_dotdot(e->d_name)) + continue; + + strbuf_setlen(path, len); + strbuf_addstr(path, e->d_name); + if (lstat(path->buf, &st)) + ; /* fall thru */ + else if (S_ISDIR(st.st_mode)) { + if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone)) + ret = 1; + if (gone) { + quote_path_relative(path->buf, strlen(path->buf), "ed, prefix); + string_list_append(&dels, quoted.buf); + } else + *dir_gone = 0; + continue; + } else { + res = dry_run ? 0 : unlink(path->buf); + if (!res) { + quote_path_relative(path->buf, strlen(path->buf), "ed, prefix); + string_list_append(&dels, quoted.buf); + } else { + quote_path_relative(path->buf, strlen(path->buf), "ed, prefix); + warning(_(msg_warn_remove_failed), quoted.buf); + *dir_gone = 0; + ret = 1; + } + continue; + } + + /* path too long, stat fails, or non-directory still exists */ + *dir_gone = 0; + ret = 1; + break; + } + closedir(dir); + + strbuf_setlen(path, original_len); + + if (*dir_gone) { + res = dry_run ? 0 : rmdir(path->buf); + if (!res) + *dir_gone = 1; + else { + quote_path_relative(path->buf, strlen(path->buf), "ed, prefix); + warning(_(msg_warn_remove_failed), quoted.buf); + *dir_gone = 0; + ret = 1; + } + } + + if (!*dir_gone && !quiet) { + for (i = 0; i < dels.nr; i++) + printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string); + } + string_list_clear(&dels, 0); + return ret; +} + +int cmd_clean(int argc, const char **argv, const char *prefix) +{ + int i, res; + int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0; + int ignored_only = 0, config_set = 0, errors = 0, gone = 1; + int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT; + struct strbuf directory = STRBUF_INIT; + struct dir_struct dir; + static const char **pathspec; + struct strbuf buf = STRBUF_INIT; + struct string_list exclude_list = STRING_LIST_INIT_NODUP; + const char *qname; + char *seen = NULL; + struct option options[] = { + OPT__QUIET(&quiet, N_("do not print names of files removed")), + OPT__DRY_RUN(&dry_run, N_("dry run")), + OPT__FORCE(&force, N_("force")), + OPT_BOOLEAN('d', NULL, &remove_directories, + N_("remove whole directories")), + { OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"), + N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb }, + OPT_BOOLEAN('x', NULL, &ignored, N_("remove ignored files, too")), + OPT_BOOLEAN('X', NULL, &ignored_only, + N_("remove only ignored files")), + OPT_END() + }; + + git_config(git_clean_config, NULL); + if (force < 0) + force = 0; + else + config_set = 1; + + argc = parse_options(argc, argv, prefix, options, builtin_clean_usage, + 0); + + memset(&dir, 0, sizeof(dir)); + if (ignored_only) + dir.flags |= DIR_SHOW_IGNORED; + + if (ignored && ignored_only) + die(_("-x and -X cannot be used together")); + + if (!dry_run && !force) { + if (config_set) + die(_("clean.requireForce set to true and neither -n nor -f given; " + "refusing to clean")); + else + die(_("clean.requireForce defaults to true and neither -n nor -f given; " + "refusing to clean")); + } + + if (force > 1) + rm_flags = 0; + + dir.flags |= DIR_SHOW_OTHER_DIRECTORIES; + + if (read_cache() < 0) + die(_("index file corrupt")); + + if (!ignored) + setup_standard_excludes(&dir); + + for (i = 0; i < exclude_list.nr; i++) + add_exclude(exclude_list.items[i].string, "", 0, + &dir.exclude_list[EXC_CMDL]); + + pathspec = get_pathspec(prefix, argv); + + fill_directory(&dir, pathspec); + + if (pathspec) + seen = xmalloc(argc > 0 ? argc : 1); + + for (i = 0; i < dir.nr; i++) { + struct dir_entry *ent = dir.entries[i]; + int len, pos; + int matches = 0; + struct cache_entry *ce; + struct stat st; + + /* + * Remove the '/' at the end that directory + * walking adds for directory entries. + */ + len = ent->len; + if (len && ent->name[len-1] == '/') + len--; + pos = cache_name_pos(ent->name, len); + if (0 <= pos) + continue; /* exact match */ + pos = -pos - 1; + if (pos < active_nr) { + ce = active_cache[pos]; + if (ce_namelen(ce) == len && + !memcmp(ce->name, ent->name, len)) + continue; /* Yup, this one exists unmerged */ + } + + /* + * we might have removed this as part of earlier + * recursive directory removal, so lstat() here could + * fail with ENOENT. + */ + if (lstat(ent->name, &st)) + continue; + + if (pathspec) { + memset(seen, 0, argc > 0 ? argc : 1); + matches = match_pathspec(pathspec, ent->name, len, + 0, seen); + } + + if (S_ISDIR(st.st_mode)) { + strbuf_addstr(&directory, ent->name); + if (remove_directories || (matches == MATCHED_EXACTLY)) { + if (remove_dirs(&directory, prefix, rm_flags, dry_run, quiet, &gone)) + errors++; + if (gone && !quiet) { + qname = quote_path_relative(directory.buf, directory.len, &buf, prefix); + printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); + } + } + strbuf_reset(&directory); + } else { + if (pathspec && !matches) + continue; + res = dry_run ? 0 : unlink(ent->name); + if (res) { + qname = quote_path_relative(ent->name, -1, &buf, prefix); + warning(_(msg_warn_remove_failed), qname); + errors++; + } else if (!quiet) { + qname = quote_path_relative(ent->name, -1, &buf, prefix); + printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); + } + } + } + free(seen); + + strbuf_release(&directory); + string_list_clear(&exclude_list, 0); + return (errors != 0); +} diff --git a/builtin/clone.c b/builtin/clone.c new file mode 100644 index 0000000000..36ec99db3f --- /dev/null +++ b/builtin/clone.c @@ -0,0 +1,918 @@ +/* + * 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 "builtin.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" +#include "pack-refs.h" +#include "sigchain.h" +#include "branch.h" +#include "remote.h" +#include "run-command.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[] = { + N_("git clone [options] [--] <repo> [<dir>]"), + NULL +}; + +static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1; +static int option_local = -1, option_no_hardlinks, option_shared, option_recursive; +static char *option_template, *option_depth; +static char *option_origin = NULL; +static char *option_branch = NULL; +static const char *real_git_dir; +static char *option_upload_pack = "git-upload-pack"; +static int option_verbosity; +static int option_progress = -1; +static struct string_list option_config; +static struct string_list option_reference; + +static int opt_parse_reference(const struct option *opt, const char *arg, int unset) +{ + struct string_list *option_reference = opt->value; + if (!arg) + return -1; + string_list_append(option_reference, arg); + return 0; +} + +static struct option builtin_clone_options[] = { + OPT__VERBOSITY(&option_verbosity), + OPT_BOOL(0, "progress", &option_progress, + N_("force progress reporting")), + OPT_BOOLEAN('n', "no-checkout", &option_no_checkout, + N_("don't create a checkout")), + OPT_BOOLEAN(0, "bare", &option_bare, N_("create a bare repository")), + { OPTION_BOOLEAN, 0, "naked", &option_bare, NULL, + N_("create a bare repository"), + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, + OPT_BOOLEAN(0, "mirror", &option_mirror, + N_("create a mirror repository (implies bare)")), + OPT_BOOL('l', "local", &option_local, + N_("to clone from a local repository")), + OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks, + N_("don't use local hardlinks, always copy")), + OPT_BOOLEAN('s', "shared", &option_shared, + N_("setup as shared repository")), + OPT_BOOLEAN(0, "recursive", &option_recursive, + N_("initialize submodules in the clone")), + OPT_BOOLEAN(0, "recurse-submodules", &option_recursive, + N_("initialize submodules in the clone")), + OPT_STRING(0, "template", &option_template, N_("template-directory"), + N_("directory from which templates will be used")), + OPT_CALLBACK(0 , "reference", &option_reference, N_("repo"), + N_("reference repository"), &opt_parse_reference), + OPT_STRING('o', "origin", &option_origin, N_("name"), + N_("use <name> instead of 'origin' to track upstream")), + OPT_STRING('b', "branch", &option_branch, N_("branch"), + N_("checkout <branch> instead of the remote's HEAD")), + OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"), + N_("path to git-upload-pack on the remote")), + OPT_STRING(0, "depth", &option_depth, N_("depth"), + N_("create a shallow clone of that depth")), + OPT_BOOL(0, "single-branch", &option_single_branch, + N_("clone only one branch, HEAD or --branch")), + OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), + N_("separate git dir from working tree")), + OPT_STRING_LIST('c', "config", &option_config, N_("key=value"), + N_("set config inside the new repository")), + OPT_END() +}; + +static const char *argv_submodule[] = { + "submodule", "update", "--init", "--recursive", NULL +}; + +static char *get_repo_path(const char *repo, int *is_bundle) +{ + static char *suffix[] = { "/.git", "", ".git/.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)) + continue; + if (S_ISDIR(st.st_mode) && is_git_directory(path)) { + *is_bundle = 0; + return xstrdup(absolute_path(path)); + } else if (S_ISREG(st.st_mode) && st.st_size > 8) { + /* Is it a "gitfile"? */ + char signature[8]; + int len, fd = open(path, O_RDONLY); + if (fd < 0) + continue; + len = read_in_full(fd, signature, 8); + close(fd); + if (len != 8 || strncmp(signature, "gitdir: ", 8)) + continue; + path = read_gitfile(path); + if (path) { + *is_bundle = 0; + return xstrdup(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(absolute_path(path)); + } + } + + return NULL; +} + +static char *guess_dir_name(const char *repo, int is_bundle, int is_bare) +{ + const char *end = repo + strlen(repo), *start; + char *dir; + + /* + * Strip trailing spaces, slashes and /.git + */ + while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1]))) + end--; + if (end - repo > 5 && is_dir_sep(end[-5]) && + !strncmp(end - 4, ".git", 4)) { + end -= 5; + while (repo < end && is_dir_sep(end[-1])) + end--; + } + + /* + * Find last component, but be prepared that repo could have + * the form "remote.example.com:foo.git", i.e. no slash + * in the directory part. + */ + start = end; + while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':') + start--; + + /* + * Strip .{bundle,git}. + */ + if (is_bundle) { + if (end - start > 7 && !strncmp(end - 7, ".bundle", 7)) + end -= 7; + } else { + if (end - start > 4 && !strncmp(end - 4, ".git", 4)) + end -= 4; + } + + if (is_bare) { + struct strbuf result = STRBUF_INIT; + strbuf_addf(&result, "%.*s.git", (int)(end - start), start); + dir = strbuf_detach(&result, NULL); + } else + dir = xstrndup(start, end - start); + /* + * Replace sequences of 'control' characters and whitespace + * with one ascii space, remove leading and trailing spaces. + */ + if (*dir) { + char *out = dir; + int prev_space = 1 /* strip leading whitespace */; + for (end = dir; *end; ++end) { + char ch = *end; + if ((unsigned char)ch < '\x20') + ch = '\x20'; + if (isspace(ch)) { + if (prev_space) + continue; + prev_space = 1; + } else + prev_space = 0; + *out++ = ch; + } + *out = '\0'; + if (out > dir && prev_space) + out[-1] = '\0'; + } + return dir; +} + +static void strip_trailing_slashes(char *dir) +{ + char *end = dir + strlen(dir); + + while (dir < end - 1 && is_dir_sep(end[-1])) + end--; + *end = '\0'; +} + +static int add_one_reference(struct string_list_item *item, void *cb_data) +{ + char *ref_git; + struct strbuf alternate = STRBUF_INIT; + + /* Beware: real_path() and mkpath() return static buffer */ + ref_git = xstrdup(real_path(item->string)); + if (is_directory(mkpath("%s/.git/objects", ref_git))) { + char *ref_git_git = mkpathdup("%s/.git", ref_git); + free(ref_git); + ref_git = ref_git_git; + } else if (!is_directory(mkpath("%s/objects", ref_git))) + die(_("reference repository '%s' is not a local directory."), + item->string); + + strbuf_addf(&alternate, "%s/objects", ref_git); + add_to_alternates_file(alternate.buf); + strbuf_release(&alternate); + free(ref_git); + return 0; +} + +static void setup_reference(void) +{ + for_each_string_list(&option_reference, add_one_reference, NULL); +} + +static void copy_alternates(struct strbuf *src, struct strbuf *dst, + const char *src_repo) +{ + /* + * Read from the source objects/info/alternates file + * and copy the entries to corresponding file in the + * destination repository with add_to_alternates_file(). + * Both src and dst have "$path/objects/info/alternates". + * + * Instead of copying bit-for-bit from the original, + * we need to append to existing one so that the already + * created entry via "clone -s" is not lost, and also + * to turn entries with paths relative to the original + * absolute, so that they can be used in the new repository. + */ + FILE *in = fopen(src->buf, "r"); + struct strbuf line = STRBUF_INIT; + + while (strbuf_getline(&line, in, '\n') != EOF) { + char *abs_path, abs_buf[PATH_MAX]; + if (!line.len || line.buf[0] == '#') + continue; + if (is_absolute_path(line.buf)) { + add_to_alternates_file(line.buf); + continue; + } + abs_path = mkpath("%s/objects/%s", src_repo, line.buf); + normalize_path_copy(abs_buf, abs_path); + add_to_alternates_file(abs_buf); + } + strbuf_release(&line); + fclose(in); +} + +static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest, + const char *src_repo, int src_baselen) +{ + struct dirent *de; + struct stat buf; + int src_len, dest_len; + DIR *dir; + + dir = opendir(src->buf); + if (!dir) + die_errno(_("failed to open '%s'"), src->buf); + + if (mkdir(dest->buf, 0777)) { + if (errno != EEXIST) + die_errno(_("failed to create directory '%s'"), dest->buf); + else if (stat(dest->buf, &buf)) + die_errno(_("failed to stat '%s'"), dest->buf); + else if (!S_ISDIR(buf.st_mode)) + die(_("%s exists and is not a directory"), dest->buf); + } + + strbuf_addch(src, '/'); + src_len = src->len; + strbuf_addch(dest, '/'); + dest_len = dest->len; + + while ((de = readdir(dir)) != NULL) { + strbuf_setlen(src, src_len); + strbuf_addstr(src, de->d_name); + strbuf_setlen(dest, dest_len); + strbuf_addstr(dest, de->d_name); + if (stat(src->buf, &buf)) { + warning (_("failed to stat %s\n"), src->buf); + continue; + } + if (S_ISDIR(buf.st_mode)) { + if (de->d_name[0] != '.') + copy_or_link_directory(src, dest, + src_repo, src_baselen); + continue; + } + + /* Files that cannot be copied bit-for-bit... */ + if (!strcmp(src->buf + src_baselen, "/info/alternates")) { + copy_alternates(src, dest, src_repo); + continue; + } + + if (unlink(dest->buf) && errno != ENOENT) + die_errno(_("failed to unlink '%s'"), dest->buf); + if (!option_no_hardlinks) { + if (!link(src->buf, dest->buf)) + continue; + if (option_local > 0) + die_errno(_("failed to create link '%s'"), dest->buf); + option_no_hardlinks = 1; + } + if (copy_file_with_time(dest->buf, src->buf, 0666)) + die_errno(_("failed to copy file to '%s'"), dest->buf); + } + closedir(dir); +} + +static void clone_local(const char *src_repo, const char *dest_repo) +{ + if (option_shared) { + struct strbuf alt = STRBUF_INIT; + strbuf_addf(&alt, "%s/objects", src_repo); + add_to_alternates_file(alt.buf); + strbuf_release(&alt); + } else { + struct strbuf src = STRBUF_INIT; + struct strbuf dest = STRBUF_INIT; + strbuf_addf(&src, "%s/objects", src_repo); + strbuf_addf(&dest, "%s/objects", dest_repo); + copy_or_link_directory(&src, &dest, src_repo, src.len); + strbuf_release(&src); + strbuf_release(&dest); + } + + if (0 <= option_verbosity) + printf(_("done.\n")); +} + +static const char *junk_work_tree; +static const char *junk_git_dir; +static pid_t junk_pid; + +static void remove_junk(void) +{ + struct strbuf sb = STRBUF_INIT; + if (getpid() != junk_pid) + return; + 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(); + sigchain_pop(signo); + raise(signo); +} + +static struct ref *find_remote_branch(const struct ref *refs, const char *branch) +{ + struct ref *ref; + struct strbuf head = STRBUF_INIT; + strbuf_addstr(&head, "refs/heads/"); + strbuf_addstr(&head, branch); + ref = find_ref_by_name(refs, head.buf); + strbuf_release(&head); + + if (ref) + return ref; + + strbuf_addstr(&head, "refs/tags/"); + strbuf_addstr(&head, branch); + ref = find_ref_by_name(refs, head.buf); + strbuf_release(&head); + + return ref; +} + +static struct ref *wanted_peer_refs(const struct ref *refs, + struct refspec *refspec) +{ + struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD")); + struct ref *local_refs = head; + struct ref **tail = head ? &head->next : &local_refs; + + if (option_single_branch) { + struct ref *remote_head = NULL; + + if (!option_branch) + remote_head = guess_remote_head(head, refs, 0); + else { + local_refs = NULL; + tail = &local_refs; + remote_head = copy_ref(find_remote_branch(refs, option_branch)); + } + + if (!remote_head && option_branch) + warning(_("Could not find remote branch %s to clone."), + option_branch); + else { + get_fetch_map(remote_head, refspec, &tail, 0); + + /* if --branch=tag, pull the requested tag explicitly */ + get_fetch_map(remote_head, tag_refspec, &tail, 0); + } + } else + get_fetch_map(refs, refspec, &tail, 0); + + if (!option_mirror && !option_single_branch) + get_fetch_map(refs, tag_refspec, &tail, 0); + + return local_refs; +} + +static void write_remote_refs(const struct ref *local_refs) +{ + const struct ref *r; + + for (r = local_refs; r; r = r->next) { + if (!r->peer_ref) + continue; + add_packed_ref(r->peer_ref->name, r->old_sha1); + } + + pack_refs(PACK_REFS_ALL); +} + +static void write_followtags(const struct ref *refs, const char *msg) +{ + const struct ref *ref; + for (ref = refs; ref; ref = ref->next) { + if (prefixcmp(ref->name, "refs/tags/")) + continue; + if (!suffixcmp(ref->name, "^{}")) + continue; + if (!has_sha1_file(ref->old_sha1)) + continue; + update_ref(msg, ref->name, ref->old_sha1, + NULL, 0, DIE_ON_ERR); + } +} + +static void update_remote_refs(const struct ref *refs, + const struct ref *mapped_refs, + const struct ref *remote_head_points_at, + const char *branch_top, + const char *msg) +{ + if (refs) { + write_remote_refs(mapped_refs); + if (option_single_branch) + write_followtags(refs, msg); + } + + if (remote_head_points_at && !option_bare) { + struct strbuf head_ref = STRBUF_INIT; + strbuf_addstr(&head_ref, branch_top); + strbuf_addstr(&head_ref, "HEAD"); + create_symref(head_ref.buf, + remote_head_points_at->peer_ref->name, + msg); + } +} + +static void update_head(const struct ref *our, const struct ref *remote, + const char *msg) +{ + if (our && !prefixcmp(our->name, "refs/heads/")) { + /* Local default branch link */ + create_symref("HEAD", our->name, NULL); + if (!option_bare) { + const char *head = skip_prefix(our->name, "refs/heads/"); + update_ref(msg, "HEAD", our->old_sha1, NULL, 0, DIE_ON_ERR); + install_branch_config(0, head, option_origin, our->name); + } + } else if (our) { + struct commit *c = lookup_commit_reference(our->old_sha1); + /* --branch specifies a non-branch (i.e. tags), detach HEAD */ + update_ref(msg, "HEAD", c->object.sha1, + NULL, REF_NODEREF, DIE_ON_ERR); + } else if (remote) { + /* + * We know remote HEAD points to a non-branch, or + * HEAD points to a branch but we don't know which one. + * Detach HEAD in all these cases. + */ + update_ref(msg, "HEAD", remote->old_sha1, + NULL, REF_NODEREF, DIE_ON_ERR); + } +} + +static int checkout(void) +{ + unsigned char sha1[20]; + char *head; + struct lock_file *lock_file; + struct unpack_trees_options opts; + struct tree *tree; + struct tree_desc t; + int err = 0, fd; + + if (option_no_checkout) + return 0; + + head = resolve_refdup("HEAD", sha1, 1, NULL); + if (!head) { + warning(_("remote HEAD refers to nonexistent ref, " + "unable to checkout.\n")); + return 0; + } + if (!strcmp(head, "HEAD")) { + if (advice_detached_head) + detach_advice(sha1_to_hex(sha1)); + } else { + if (prefixcmp(head, "refs/heads/")) + die(_("HEAD not found below refs/heads!")); + } + free(head); + + /* We need to be in the new work tree for the checkout */ + setup_work_tree(); + + lock_file = xcalloc(1, sizeof(struct lock_file)); + 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_verbosity >= 0); + opts.src_index = &the_index; + opts.dst_index = &the_index; + + tree = parse_tree_indirect(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")); + + err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1), + sha1_to_hex(sha1), "1", NULL); + + if (!err && option_recursive) + err = run_command_v_opt(argv_submodule, RUN_GIT_CMD); + + return err; +} + +static int write_one_config(const char *key, const char *value, void *data) +{ + return git_config_set_multivar(key, value ? value : "true", "^$", 0); +} + +static void write_config(struct string_list *config) +{ + int i; + + for (i = 0; i < config->nr; i++) { + if (git_config_parse_parameter(config->items[i].string, + write_one_config, NULL) < 0) + die("unable to write parameters to config file"); + } +} + +static void write_refspec_config(const char* src_ref_prefix, + const struct ref* our_head_points_at, + const struct ref* remote_head_points_at, struct strbuf* branch_top) +{ + struct strbuf key = STRBUF_INIT; + struct strbuf value = STRBUF_INIT; + + if (option_mirror || !option_bare) { + if (option_single_branch && !option_mirror) { + if (option_branch) { + if (strstr(our_head_points_at->name, "refs/tags/")) + strbuf_addf(&value, "+%s:%s", our_head_points_at->name, + our_head_points_at->name); + else + strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name, + branch_top->buf, option_branch); + } else if (remote_head_points_at) { + strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name, + branch_top->buf, + skip_prefix(remote_head_points_at->name, "refs/heads/")); + } + /* + * otherwise, the next "git fetch" will + * simply fetch from HEAD without updating + * any remote tracking branch, which is what + * we want. + */ + } else { + strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf); + } + /* Configure the remote */ + if (value.len) { + strbuf_addf(&key, "remote.%s.fetch", option_origin); + git_config_set_multivar(key.buf, value.buf, "^$", 0); + strbuf_reset(&key); + + if (option_mirror) { + strbuf_addf(&key, "remote.%s.mirror", option_origin); + git_config_set(key.buf, "true"); + strbuf_reset(&key); + } + } + } + + strbuf_release(&key); + strbuf_release(&value); +} + +int cmd_clone(int argc, const char **argv, const char *prefix) +{ + int is_bundle = 0, is_local; + struct stat buf; + const char *repo_name, *repo, *work_tree, *git_dir; + char *path, *dir; + int dest_exists; + const struct ref *refs, *remote_head; + const struct ref *remote_head_points_at; + const struct ref *our_head_points_at; + struct ref *mapped_refs; + const struct ref *ref; + struct strbuf key = STRBUF_INIT, value = STRBUF_INIT; + struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT; + struct transport *transport = NULL; + const char *src_ref_prefix = "refs/heads/"; + struct remote *remote; + int err = 0, complete_refs_before_fetch = 1; + + struct refspec *refspec; + const char *fetch_pattern; + + junk_pid = getpid(); + + packet_trace_identity("clone"); + argc = parse_options(argc, argv, prefix, builtin_clone_options, + builtin_clone_usage, 0); + + if (argc > 2) + usage_msg_opt(_("Too many arguments."), + builtin_clone_usage, builtin_clone_options); + + if (argc == 0) + usage_msg_opt(_("You must specify a repository to clone."), + builtin_clone_usage, builtin_clone_options); + + if (option_single_branch == -1) + option_single_branch = option_depth ? 1 : 0; + + if (option_mirror) + option_bare = 1; + + if (option_bare) { + if (option_origin) + die(_("--bare and --origin %s options are incompatible."), + option_origin); + if (real_git_dir) + die(_("--bare and --separate-git-dir are incompatible.")); + option_no_checkout = 1; + } + + if (!option_origin) + option_origin = "origin"; + + repo_name = argv[0]; + + path = get_repo_path(repo_name, &is_bundle); + if (path) + repo = xstrdup(absolute_path(repo_name)); + else if (!strchr(repo_name, ':')) + die(_("repository '%s' does not exist"), repo_name); + else + repo = repo_name; + is_local = option_local != 0 && path && !is_bundle; + if (is_local && option_depth) + warning(_("--depth is ignored in local clones; use file:// instead.")); + + if (argc == 2) + dir = xstrdup(argv[1]); + else + dir = guess_dir_name(repo_name, is_bundle, option_bare); + strip_trailing_slashes(dir); + + dest_exists = !stat(dir, &buf); + if (dest_exists && !is_empty_dir(dir)) + die(_("destination path '%s' already exists and is not " + "an empty directory."), dir); + + 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 = mkpathdup("%s/.git", dir); + } + + if (!option_bare) { + junk_work_tree = work_tree; + if (safe_create_leading_directories_const(work_tree) < 0) + die_errno(_("could not create leading directories of '%s'"), + work_tree); + if (!dest_exists && mkdir(work_tree, 0777)) + die_errno(_("could not create work tree dir '%s'."), + work_tree); + set_git_work_tree(work_tree); + } + junk_git_dir = git_dir; + atexit(remove_junk); + sigchain_push_common(remove_junk_on_signal); + + setenv(CONFIG_ENVIRONMENT, mkpath("%s/config", git_dir), 1); + + if (safe_create_leading_directories_const(git_dir) < 0) + die(_("could not create leading directories of '%s'"), git_dir); + + set_git_dir_init(git_dir, real_git_dir, 0); + if (real_git_dir) { + git_dir = real_git_dir; + junk_git_dir = real_git_dir; + } + + if (0 <= option_verbosity) { + if (option_bare) + printf(_("Cloning into bare repository '%s'...\n"), dir); + else + printf(_("Cloning into '%s'...\n"), dir); + } + init_db(option_template, INIT_DB_QUIET); + write_config(&option_config); + + /* + * At this point, the config exists, so we do not need the + * environment variable. We actually need to unset it, too, to + * re-enable parsing of the global configs. + */ + unsetenv(CONFIG_ENVIRONMENT); + + git_config(git_default_config, NULL); + + if (option_bare) { + if (option_mirror) + src_ref_prefix = "refs/"; + strbuf_addstr(&branch_top, src_ref_prefix); + + git_config_set("core.bare", "true"); + } else { + strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin); + } + + strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf); + strbuf_addf(&key, "remote.%s.url", option_origin); + git_config_set(key.buf, repo); + strbuf_reset(&key); + + if (option_reference.nr) + setup_reference(); + + fetch_pattern = value.buf; + refspec = parse_fetch_refspec(1, &fetch_pattern); + + strbuf_reset(&value); + + remote = remote_get(option_origin); + transport = transport_get(remote, remote->url[0]); + + if (!is_local) { + if (!transport->get_refs_list || !transport->fetch) + die(_("Don't know how to clone %s"), transport->url); + + transport_set_option(transport, TRANS_OPT_KEEP, "yes"); + + if (option_depth) + transport_set_option(transport, TRANS_OPT_DEPTH, + option_depth); + if (option_single_branch) + transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); + + transport_set_verbosity(transport, option_verbosity, option_progress); + + if (option_upload_pack) + transport_set_option(transport, TRANS_OPT_UPLOADPACK, + option_upload_pack); + } + + refs = transport_get_remote_refs(transport); + + if (refs) { + mapped_refs = wanted_peer_refs(refs, refspec); + /* + * transport_get_remote_refs() may return refs with null sha-1 + * in mapped_refs (see struct transport->get_refs_list + * comment). In that case we need fetch it early because + * remote_head code below relies on it. + * + * for normal clones, transport_get_remote_refs() should + * return reliable ref set, we can delay cloning until after + * remote HEAD check. + */ + for (ref = refs; ref; ref = ref->next) + if (is_null_sha1(ref->old_sha1)) { + complete_refs_before_fetch = 0; + break; + } + + if (!is_local && !complete_refs_before_fetch) + transport_fetch_refs(transport, mapped_refs); + + remote_head = find_ref_by_name(refs, "HEAD"); + remote_head_points_at = + guess_remote_head(remote_head, mapped_refs, 0); + + if (option_branch) { + our_head_points_at = + find_remote_branch(mapped_refs, option_branch); + + if (!our_head_points_at) + die(_("Remote branch %s not found in upstream %s"), + option_branch, option_origin); + } + else + our_head_points_at = remote_head_points_at; + } + else { + warning(_("You appear to have cloned an empty repository.")); + mapped_refs = NULL; + our_head_points_at = NULL; + remote_head_points_at = NULL; + remote_head = NULL; + option_no_checkout = 1; + if (!option_bare) + install_branch_config(0, "master", option_origin, + "refs/heads/master"); + } + + write_refspec_config(src_ref_prefix, our_head_points_at, + remote_head_points_at, &branch_top); + + if (is_local) + clone_local(path, git_dir); + else if (refs && complete_refs_before_fetch) + transport_fetch_refs(transport, mapped_refs); + + update_remote_refs(refs, mapped_refs, remote_head_points_at, + branch_top.buf, reflog_msg.buf); + + update_head(our_head_points_at, remote_head, reflog_msg.buf); + + transport_unlock_pack(transport); + transport_disconnect(transport); + + err = checkout(); + + strbuf_release(&reflog_msg); + strbuf_release(&branch_top); + strbuf_release(&key); + strbuf_release(&value); + junk_pid = 0; + return err; +} diff --git a/builtin/column.c b/builtin/column.c new file mode 100644 index 0000000000..e125a55fc9 --- /dev/null +++ b/builtin/column.c @@ -0,0 +1,59 @@ +#include "builtin.h" +#include "cache.h" +#include "strbuf.h" +#include "parse-options.h" +#include "string-list.h" +#include "column.h" + +static const char * const builtin_column_usage[] = { + N_("git column [options]"), + NULL +}; +static unsigned int colopts; + +static int column_config(const char *var, const char *value, void *cb) +{ + return git_column_config(var, value, cb, &colopts); +} + +int cmd_column(int argc, const char **argv, const char *prefix) +{ + struct string_list list = STRING_LIST_INIT_DUP; + struct strbuf sb = STRBUF_INIT; + struct column_options copts; + const char *command = NULL, *real_command = NULL; + struct option options[] = { + OPT_STRING(0, "command", &real_command, N_("name"), N_("lookup config vars")), + OPT_COLUMN(0, "mode", &colopts, N_("layout to use")), + OPT_INTEGER(0, "raw-mode", &colopts, N_("layout to use")), + OPT_INTEGER(0, "width", &copts.width, N_("Maximum width")), + OPT_STRING(0, "indent", &copts.indent, N_("string"), N_("Padding space on left border")), + OPT_INTEGER(0, "nl", &copts.nl, N_("Padding space on right border")), + OPT_INTEGER(0, "padding", &copts.padding, N_("Padding space between columns")), + OPT_END() + }; + + /* This one is special and must be the first one */ + if (argc > 1 && !prefixcmp(argv[1], "--command=")) { + command = argv[1] + 10; + git_config(column_config, (void *)command); + } else + git_config(column_config, NULL); + + memset(&copts, 0, sizeof(copts)); + copts.width = term_columns(); + copts.padding = 1; + argc = parse_options(argc, argv, "", options, builtin_column_usage, 0); + if (argc) + usage_with_options(builtin_column_usage, options); + if (real_command || command) { + if (!real_command || !command || strcmp(real_command, command)) + die(_("--command must be the first argument")); + } + finalize_colopts(&colopts, -1); + while (!strbuf_getline(&sb, stdin, '\n')) + string_list_append(&list, sb.buf); + + print_columns(&list, colopts, &copts); + return 0; +} diff --git a/builtin/commit-tree.c b/builtin/commit-tree.c new file mode 100644 index 0000000000..eac901a0ee --- /dev/null +++ b/builtin/commit-tree.c @@ -0,0 +1,125 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "commit.h" +#include "tree.h" +#include "builtin.h" +#include "utf8.h" +#include "gpg-interface.h" + +static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S<signer>] [-m <message>] [-F <file>] <sha1> <changelog"; + +static void new_parent(struct commit *parent, struct commit_list **parents_p) +{ + unsigned char *sha1 = parent->object.sha1; + struct commit_list *parents; + for (parents = *parents_p; parents; parents = parents->next) { + if (parents->item == parent) { + error("duplicate parent %s ignored", sha1_to_hex(sha1)); + return; + } + parents_p = &parents->next; + } + commit_list_insert(parent, parents_p); +} + +static int commit_tree_config(const char *var, const char *value, void *cb) +{ + int status = git_gpg_config(var, value, NULL); + if (status) + return status; + return git_default_config(var, value, cb); +} + +int cmd_commit_tree(int argc, const char **argv, const char *prefix) +{ + int i, got_tree = 0; + struct commit_list *parents = NULL; + unsigned char tree_sha1[20]; + unsigned char commit_sha1[20]; + struct strbuf buffer = STRBUF_INIT; + const char *sign_commit = NULL; + + git_config(commit_tree_config, NULL); + + if (argc < 2 || !strcmp(argv[1], "-h")) + usage(commit_tree_usage); + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (!strcmp(arg, "-p")) { + unsigned char sha1[20]; + if (argc <= ++i) + usage(commit_tree_usage); + if (get_sha1_commit(argv[i], sha1)) + die("Not a valid object name %s", argv[i]); + assert_sha1_type(sha1, OBJ_COMMIT); + new_parent(lookup_commit(sha1), &parents); + continue; + } + + if (!memcmp(arg, "-S", 2)) { + sign_commit = arg + 2; + continue; + } + + if (!strcmp(arg, "-m")) { + if (argc <= ++i) + usage(commit_tree_usage); + if (buffer.len) + strbuf_addch(&buffer, '\n'); + strbuf_addstr(&buffer, argv[i]); + strbuf_complete_line(&buffer); + continue; + } + + if (!strcmp(arg, "-F")) { + int fd; + + if (argc <= ++i) + usage(commit_tree_usage); + if (buffer.len) + strbuf_addch(&buffer, '\n'); + if (!strcmp(argv[i], "-")) + fd = 0; + else { + fd = open(argv[i], O_RDONLY); + if (fd < 0) + die_errno("git commit-tree: failed to open '%s'", + argv[i]); + } + if (strbuf_read(&buffer, fd, 0) < 0) + die_errno("git commit-tree: failed to read '%s'", + argv[i]); + if (fd && close(fd)) + die_errno("git commit-tree: failed to close '%s'", + argv[i]); + strbuf_complete_line(&buffer); + continue; + } + + if (get_sha1_tree(arg, tree_sha1)) + die("Not a valid object name %s", arg); + if (got_tree) + die("Cannot give more than one trees"); + got_tree = 1; + } + + if (!buffer.len) { + if (strbuf_read(&buffer, 0, 0) < 0) + die_errno("git commit-tree: failed to read"); + } + + if (commit_tree(&buffer, tree_sha1, parents, commit_sha1, + NULL, sign_commit)) { + strbuf_release(&buffer); + return 1; + } + + printf("%s\n", sha1_to_hex(commit_sha1)); + strbuf_release(&buffer); + return 0; +} diff --git a/builtin/commit.c b/builtin/commit.c new file mode 100644 index 0000000000..7c2a3d48b4 --- /dev/null +++ b/builtin/commit.c @@ -0,0 +1,1612 @@ +/* + * Builtin "git commit" + * + * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com> + * Based on git-commit.sh by Junio C Hamano and Linus Torvalds + */ + +#include "cache.h" +#include "cache-tree.h" +#include "color.h" +#include "dir.h" +#include "builtin.h" +#include "diff.h" +#include "diffcore.h" +#include "commit.h" +#include "revision.h" +#include "wt-status.h" +#include "run-command.h" +#include "refs.h" +#include "log-tree.h" +#include "strbuf.h" +#include "utf8.h" +#include "parse-options.h" +#include "string-list.h" +#include "rerere.h" +#include "unpack-trees.h" +#include "quote.h" +#include "submodule.h" +#include "gpg-interface.h" +#include "column.h" +#include "sequencer.h" + +static const char * const builtin_commit_usage[] = { + N_("git commit [options] [--] <filepattern>..."), + NULL +}; + +static const char * const builtin_status_usage[] = { + N_("git status [options] [--] <filepattern>..."), + NULL +}; + +static const char implicit_ident_advice[] = +N_("Your name and email address were configured automatically based\n" +"on your username and hostname. Please check that they are accurate.\n" +"You can suppress this message by setting them explicitly:\n" +"\n" +" git config --global user.name \"Your Name\"\n" +" git config --global user.email you@example.com\n" +"\n" +"After doing this, you may fix the identity used for this commit with:\n" +"\n" +" git commit --amend --reset-author\n"); + +static const char empty_amend_advice[] = +N_("You asked to amend the most recent commit, but doing so would make\n" +"it empty. You can repeat your command with --allow-empty, or you can\n" +"remove the commit entirely with \"git reset HEAD^\".\n"); + +static const char empty_cherry_pick_advice[] = +N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n" +"If you wish to commit it anyway, use:\n" +"\n" +" git commit --allow-empty\n" +"\n" +"Otherwise, please use 'git reset'\n"); + +static const char *use_message_buffer; +static const char commit_editmsg[] = "COMMIT_EDITMSG"; +static struct lock_file index_lock; /* real index */ +static struct lock_file false_lock; /* used only for partial commits */ +static enum { + COMMIT_AS_IS = 1, + COMMIT_NORMAL, + COMMIT_PARTIAL +} commit_style; + +static const char *logfile, *force_author; +static const char *template_file; +/* + * The _message variables are commit names from which to take + * the commit message and/or authorship. + */ +static const char *author_message, *author_message_buffer; +static char *edit_message, *use_message; +static char *fixup_message, *squash_message; +static int all, also, interactive, patch_interactive, only, amend, signoff; +static int edit_flag = -1; /* unspecified */ +static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; +static int no_post_rewrite, allow_empty_message; +static char *untracked_files_arg, *force_date, *ignore_submodule_arg; +static char *sign_commit; + +/* + * The default commit message cleanup mode will remove the lines + * beginning with # (shell comments) and leading and trailing + * whitespaces (empty lines or containing only whitespaces) + * if editor is used, and only the whitespaces if the message + * is specified explicitly. + */ +static enum { + CLEANUP_SPACE, + CLEANUP_NONE, + CLEANUP_ALL +} cleanup_mode; +static const char *cleanup_arg; + +static enum commit_whence whence; +static int use_editor = 1, include_status = 1; +static int show_ignored_in_status; +static const char *only_include_assumed; +static struct strbuf message = STRBUF_INIT; + +static enum { + STATUS_FORMAT_NONE = 0, + STATUS_FORMAT_LONG, + STATUS_FORMAT_SHORT, + STATUS_FORMAT_PORCELAIN +} status_format; + +static int opt_parse_m(const struct option *opt, const char *arg, int unset) +{ + struct strbuf *buf = opt->value; + if (unset) + strbuf_setlen(buf, 0); + else { + strbuf_addstr(buf, arg); + strbuf_addstr(buf, "\n\n"); + } + return 0; +} + +static void determine_whence(struct wt_status *s) +{ + if (file_exists(git_path("MERGE_HEAD"))) + whence = FROM_MERGE; + else if (file_exists(git_path("CHERRY_PICK_HEAD"))) + whence = FROM_CHERRY_PICK; + else + whence = FROM_COMMIT; + if (s) + s->whence = whence; +} + +static void rollback_index_files(void) +{ + switch (commit_style) { + case COMMIT_AS_IS: + break; /* nothing to do */ + case COMMIT_NORMAL: + rollback_lock_file(&index_lock); + break; + case COMMIT_PARTIAL: + rollback_lock_file(&index_lock); + rollback_lock_file(&false_lock); + break; + } +} + +static int commit_index_files(void) +{ + int err = 0; + + switch (commit_style) { + case COMMIT_AS_IS: + break; /* nothing to do */ + case COMMIT_NORMAL: + err = commit_lock_file(&index_lock); + break; + case COMMIT_PARTIAL: + err = commit_lock_file(&index_lock); + rollback_lock_file(&false_lock); + break; + } + + return err; +} + +/* + * Take a union of paths in the index and the named tree (typically, "HEAD"), + * and return the paths that match the given pattern in list. + */ +static int list_paths(struct string_list *list, const char *with_tree, + const char *prefix, const char **pattern) +{ + int i; + char *m; + + if (!pattern) + return 0; + + for (i = 0; pattern[i]; i++) + ; + m = xcalloc(1, i); + + if (with_tree) { + char *max_prefix = common_prefix(pattern); + overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix); + free(max_prefix); + } + + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + struct string_list_item *item; + + if (ce->ce_flags & CE_UPDATE) + continue; + if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m)) + continue; + item = string_list_insert(list, ce->name); + if (ce_skip_worktree(ce)) + item->util = item; /* better a valid pointer than a fake one */ + } + + return report_path_error(m, pattern, prefix); +} + +static void add_remove_files(struct string_list *list) +{ + int i; + for (i = 0; i < list->nr; i++) { + struct stat st; + struct string_list_item *p = &(list->items[i]); + + /* p->util is skip-worktree */ + if (p->util) + continue; + + if (!lstat(p->string, &st)) { + if (add_to_cache(p->string, &st, 0)) + die(_("updating files failed")); + } else + remove_file_from_cache(p->string); + } +} + +static void create_base_index(const struct commit *current_head) +{ + struct tree *tree; + struct unpack_trees_options opts; + struct tree_desc t; + + if (!current_head) { + discard_cache(); + return; + } + + memset(&opts, 0, sizeof(opts)); + opts.head_idx = 1; + opts.index_only = 1; + opts.merge = 1; + opts.src_index = &the_index; + opts.dst_index = &the_index; + + opts.fn = oneway_merge; + tree = parse_tree_indirect(current_head->object.sha1); + if (!tree) + die(_("failed to unpack HEAD tree object")); + parse_tree(tree); + init_tree_desc(&t, tree->buffer, tree->size); + if (unpack_trees(1, &t, &opts)) + exit(128); /* We've already reported the error, finish dying */ +} + +static void refresh_cache_or_die(int refresh_flags) +{ + /* + * refresh_flags contains REFRESH_QUIET, so the only errors + * are for unmerged entries. + */ + if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN)) + die_resolve_conflict("commit"); +} + +static char *prepare_index(int argc, const char **argv, const char *prefix, + const struct commit *current_head, int is_status) +{ + int fd; + struct string_list partial; + const char **pathspec = NULL; + char *old_index_env = NULL; + int refresh_flags = REFRESH_QUIET; + + if (is_status) + refresh_flags |= REFRESH_UNMERGED; + + if (*argv) + pathspec = get_pathspec(prefix, argv); + + if (read_cache_preload(pathspec) < 0) + die(_("index file corrupt")); + + if (interactive) { + fd = hold_locked_index(&index_lock, 1); + + refresh_cache_or_die(refresh_flags); + + if (write_cache(fd, active_cache, active_nr) || + close_lock_file(&index_lock)) + die(_("unable to create temporary index")); + + old_index_env = getenv(INDEX_ENVIRONMENT); + setenv(INDEX_ENVIRONMENT, index_lock.filename, 1); + + if (interactive_add(argc, argv, prefix, patch_interactive) != 0) + die(_("interactive add failed")); + + if (old_index_env && *old_index_env) + setenv(INDEX_ENVIRONMENT, old_index_env, 1); + else + unsetenv(INDEX_ENVIRONMENT); + + discard_cache(); + read_cache_from(index_lock.filename); + + commit_style = COMMIT_NORMAL; + return index_lock.filename; + } + + /* + * Non partial, non as-is commit. + * + * (1) get the real index; + * (2) update the_index as necessary; + * (3) write the_index out to the real index (still locked); + * (4) return the name of the locked index file. + * + * The caller should run hooks on the locked real index, and + * (A) if all goes well, commit the real index; + * (B) on failure, rollback the real index. + */ + if (all || (also && pathspec && *pathspec)) { + fd = hold_locked_index(&index_lock, 1); + add_files_to_cache(also ? prefix : NULL, pathspec, 0); + refresh_cache_or_die(refresh_flags); + update_main_cache_tree(WRITE_TREE_SILENT); + if (write_cache(fd, active_cache, active_nr) || + close_lock_file(&index_lock)) + die(_("unable to write new_index file")); + commit_style = COMMIT_NORMAL; + return index_lock.filename; + } + + /* + * As-is commit. + * + * (1) return the name of the real index file. + * + * The caller should run hooks on the real index, + * and create commit from the_index. + * We still need to refresh the index here. + */ + if (!only && (!pathspec || !*pathspec)) { + fd = hold_locked_index(&index_lock, 1); + refresh_cache_or_die(refresh_flags); + if (active_cache_changed) { + update_main_cache_tree(WRITE_TREE_SILENT); + if (write_cache(fd, active_cache, active_nr) || + commit_locked_index(&index_lock)) + die(_("unable to write new_index file")); + } else { + rollback_lock_file(&index_lock); + } + commit_style = COMMIT_AS_IS; + return get_index_file(); + } + + /* + * A partial commit. + * + * (0) find the set of affected paths; + * (1) get lock on the real index file; + * (2) update the_index with the given paths; + * (3) write the_index out to the real index (still locked); + * (4) get lock on the false index file; + * (5) reset the_index from HEAD; + * (6) update the_index the same way as (2); + * (7) write the_index out to the false index file; + * (8) return the name of the false index file (still locked); + * + * The caller should run hooks on the locked false index, and + * create commit from it. Then + * (A) if all goes well, commit the real index; + * (B) on failure, rollback the real index; + * In either case, rollback the false index. + */ + commit_style = COMMIT_PARTIAL; + + if (whence != FROM_COMMIT) { + if (whence == FROM_MERGE) + die(_("cannot do a partial commit during a merge.")); + else if (whence == FROM_CHERRY_PICK) + die(_("cannot do a partial commit during a cherry-pick.")); + } + + memset(&partial, 0, sizeof(partial)); + partial.strdup_strings = 1; + if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec)) + exit(1); + + discard_cache(); + if (read_cache() < 0) + die(_("cannot read the index")); + + fd = hold_locked_index(&index_lock, 1); + add_remove_files(&partial); + refresh_cache(REFRESH_QUIET); + if (write_cache(fd, active_cache, active_nr) || + close_lock_file(&index_lock)) + die(_("unable to write new_index file")); + + fd = hold_lock_file_for_update(&false_lock, + git_path("next-index-%"PRIuMAX, + (uintmax_t) getpid()), + LOCK_DIE_ON_ERROR); + + create_base_index(current_head); + add_remove_files(&partial); + refresh_cache(REFRESH_QUIET); + + if (write_cache(fd, active_cache, active_nr) || + close_lock_file(&false_lock)) + die(_("unable to write temporary index file")); + + discard_cache(); + read_cache_from(false_lock.filename); + + return false_lock.filename; +} + +static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, + struct wt_status *s) +{ + unsigned char sha1[20]; + + if (s->relative_paths) + s->prefix = prefix; + + if (amend) { + s->amend = 1; + s->reference = "HEAD^1"; + } + s->verbose = verbose; + s->index_file = index_file; + s->fp = fp; + s->nowarn = nowarn; + s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0; + + wt_status_collect(s); + + switch (status_format) { + case STATUS_FORMAT_SHORT: + wt_shortstatus_print(s); + break; + case STATUS_FORMAT_PORCELAIN: + wt_porcelain_print(s); + break; + case STATUS_FORMAT_NONE: + case STATUS_FORMAT_LONG: + wt_status_print(s); + break; + } + + return s->commitable; +} + +static int is_a_merge(const struct commit *current_head) +{ + return !!(current_head->parents && current_head->parents->next); +} + +static void export_one(const char *var, const char *s, const char *e, int hack) +{ + struct strbuf buf = STRBUF_INIT; + if (hack) + strbuf_addch(&buf, hack); + strbuf_addf(&buf, "%.*s", (int)(e - s), s); + setenv(var, buf.buf, 1); + strbuf_release(&buf); +} + +static int sane_ident_split(struct ident_split *person) +{ + if (!person->name_begin || !person->name_end || + person->name_begin == person->name_end) + return 0; /* no human readable name */ + if (!person->mail_begin || !person->mail_end || + person->mail_begin == person->mail_end) + return 0; /* no usable mail */ + if (!person->date_begin || !person->date_end || + !person->tz_begin || !person->tz_end) + return 0; + return 1; +} + +static void determine_author_info(struct strbuf *author_ident) +{ + char *name, *email, *date; + struct ident_split author; + + name = getenv("GIT_AUTHOR_NAME"); + email = getenv("GIT_AUTHOR_EMAIL"); + date = getenv("GIT_AUTHOR_DATE"); + + if (author_message) { + const char *a, *lb, *rb, *eol; + size_t len; + + a = strstr(author_message_buffer, "\nauthor "); + if (!a) + die(_("invalid commit: %s"), author_message); + + lb = strchrnul(a + strlen("\nauthor "), '<'); + rb = strchrnul(lb, '>'); + eol = strchrnul(rb, '\n'); + if (!*lb || !*rb || !*eol) + die(_("invalid commit: %s"), author_message); + + if (lb == a + strlen("\nauthor ")) + /* \nauthor <foo@example.com> */ + name = xcalloc(1, 1); + else + name = xmemdupz(a + strlen("\nauthor "), + (lb - strlen(" ") - + (a + strlen("\nauthor ")))); + email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<"))); + date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> "))); + len = eol - (rb + strlen("> ")); + date = xmalloc(len + 2); + *date = '@'; + memcpy(date + 1, rb + strlen("> "), len); + date[len + 1] = '\0'; + } + + if (force_author) { + const char *lb = strstr(force_author, " <"); + const char *rb = strchr(force_author, '>'); + + if (!lb || !rb) + die(_("malformed --author parameter")); + name = xstrndup(force_author, lb - force_author); + email = xstrndup(lb + 2, rb - (lb + 2)); + } + + if (force_date) + date = force_date; + strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT)); + if (!split_ident_line(&author, author_ident->buf, author_ident->len) && + sane_ident_split(&author)) { + export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0); + export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0); + export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@'); + } +} + +static char *cut_ident_timestamp_part(char *string) +{ + char *ket = strrchr(string, '>'); + if (!ket || ket[1] != ' ') + die(_("Malformed ident string: '%s'"), string); + *++ket = '\0'; + return ket; +} + +static int prepare_to_commit(const char *index_file, const char *prefix, + struct commit *current_head, + struct wt_status *s, + struct strbuf *author_ident) +{ + struct stat statbuf; + struct strbuf committer_ident = STRBUF_INIT; + int commitable, saved_color_setting; + struct strbuf sb = STRBUF_INIT; + char *buffer; + const char *hook_arg1 = NULL; + const char *hook_arg2 = NULL; + int ident_shown = 0; + int clean_message_contents = (cleanup_mode != CLEANUP_NONE); + + /* This checks and barfs if author is badly specified */ + determine_author_info(author_ident); + + if (!no_verify && run_hook(index_file, "pre-commit", NULL)) + return 0; + + if (squash_message) { + /* + * Insert the proper subject line before other commit + * message options add their content. + */ + if (use_message && !strcmp(use_message, squash_message)) + strbuf_addstr(&sb, "squash! "); + else { + struct pretty_print_context ctx = {0}; + struct commit *c; + c = lookup_commit_reference_by_name(squash_message); + if (!c) + die(_("could not lookup commit %s"), squash_message); + ctx.output_encoding = get_commit_output_encoding(); + format_commit_message(c, "squash! %s\n\n", &sb, + &ctx); + } + } + + if (message.len) { + strbuf_addbuf(&sb, &message); + hook_arg1 = "message"; + } else if (logfile && !strcmp(logfile, "-")) { + if (isatty(0)) + fprintf(stderr, _("(reading log message from standard input)\n")); + if (strbuf_read(&sb, 0, 0) < 0) + die_errno(_("could not read log from standard input")); + hook_arg1 = "message"; + } else if (logfile) { + if (strbuf_read_file(&sb, logfile, 0) < 0) + die_errno(_("could not read log file '%s'"), + logfile); + hook_arg1 = "message"; + } else if (use_message) { + buffer = strstr(use_message_buffer, "\n\n"); + if (!use_editor && (!buffer || buffer[2] == '\0')) + die(_("commit has empty message")); + strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); + hook_arg1 = "commit"; + hook_arg2 = use_message; + } else if (fixup_message) { + struct pretty_print_context ctx = {0}; + struct commit *commit; + commit = lookup_commit_reference_by_name(fixup_message); + if (!commit) + die(_("could not lookup commit %s"), fixup_message); + ctx.output_encoding = get_commit_output_encoding(); + format_commit_message(commit, "fixup! %s\n\n", + &sb, &ctx); + hook_arg1 = "message"; + } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { + if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) + die_errno(_("could not read MERGE_MSG")); + hook_arg1 = "merge"; + } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { + if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) + die_errno(_("could not read SQUASH_MSG")); + hook_arg1 = "squash"; + } else if (template_file) { + if (strbuf_read_file(&sb, template_file, 0) < 0) + die_errno(_("could not read '%s'"), template_file); + hook_arg1 = "template"; + clean_message_contents = 0; + } + + /* + * The remaining cases don't modify the template message, but + * just set the argument(s) to the prepare-commit-msg hook. + */ + else if (whence == FROM_MERGE) + hook_arg1 = "merge"; + else if (whence == FROM_CHERRY_PICK) { + hook_arg1 = "commit"; + hook_arg2 = "CHERRY_PICK_HEAD"; + } + + if (squash_message) { + /* + * If squash_commit was used for the commit subject, + * then we're possibly hijacking other commit log options. + * Reset the hook args to tell the real story. + */ + hook_arg1 = "message"; + hook_arg2 = ""; + } + + s->fp = fopen(git_path(commit_editmsg), "w"); + if (s->fp == NULL) + die_errno(_("could not open '%s'"), git_path(commit_editmsg)); + + if (clean_message_contents) + stripspace(&sb, 0); + + if (signoff) { + /* + * See if we have a Conflicts: block at the end. If yes, count + * its size, so we can ignore it. + */ + int ignore_footer = 0; + int i, eol, previous = 0; + const char *nl; + + for (i = 0; i < sb.len; i++) { + nl = memchr(sb.buf + i, '\n', sb.len - i); + if (nl) + eol = nl - sb.buf; + else + eol = sb.len; + if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) { + ignore_footer = sb.len - previous; + break; + } + while (i < eol) + i++; + previous = eol; + } + + append_signoff(&sb, ignore_footer); + } + + if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) + die_errno(_("could not write commit template")); + + strbuf_release(&sb); + + /* This checks if committer ident is explicitly given */ + strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT)); + if (use_editor && include_status) { + char *ai_tmp, *ci_tmp; + if (whence != FROM_COMMIT) + status_printf_ln(s, GIT_COLOR_NORMAL, + whence == FROM_MERGE + ? _("\n" + "It looks like you may be committing a merge.\n" + "If this is not correct, please remove the file\n" + " %s\n" + "and try again.\n") + : _("\n" + "It looks like you may be committing a cherry-pick.\n" + "If this is not correct, please remove the file\n" + " %s\n" + "and try again.\n"), + git_path(whence == FROM_MERGE + ? "MERGE_HEAD" + : "CHERRY_PICK_HEAD")); + + fprintf(s->fp, "\n"); + if (cleanup_mode == CLEANUP_ALL) + status_printf(s, GIT_COLOR_NORMAL, + _("Please enter the commit message for your changes." + " Lines starting\nwith '#' will be ignored, and an empty" + " message aborts the commit.\n")); + else /* CLEANUP_SPACE, that is. */ + status_printf(s, GIT_COLOR_NORMAL, + _("Please enter the commit message for your changes." + " Lines starting\n" + "with '#' will be kept; you may remove them" + " yourself if you want to.\n" + "An empty message aborts the commit.\n")); + if (only_include_assumed) + status_printf_ln(s, GIT_COLOR_NORMAL, + "%s", only_include_assumed); + + ai_tmp = cut_ident_timestamp_part(author_ident->buf); + ci_tmp = cut_ident_timestamp_part(committer_ident.buf); + if (strcmp(author_ident->buf, committer_ident.buf)) + status_printf_ln(s, GIT_COLOR_NORMAL, + _("%s" + "Author: %s"), + ident_shown++ ? "" : "\n", + author_ident->buf); + + if (!committer_ident_sufficiently_given()) + status_printf_ln(s, GIT_COLOR_NORMAL, + _("%s" + "Committer: %s"), + ident_shown++ ? "" : "\n", + committer_ident.buf); + + if (ident_shown) + status_printf_ln(s, GIT_COLOR_NORMAL, ""); + + saved_color_setting = s->use_color; + s->use_color = 0; + commitable = run_status(s->fp, index_file, prefix, 1, s); + s->use_color = saved_color_setting; + + *ai_tmp = ' '; + *ci_tmp = ' '; + } else { + unsigned char sha1[20]; + const char *parent = "HEAD"; + + if (!active_nr && read_cache() < 0) + die(_("Cannot read index")); + + if (amend) + parent = "HEAD^1"; + + if (get_sha1(parent, sha1)) + commitable = !!active_nr; + else + commitable = index_differs_from(parent, 0); + } + strbuf_release(&committer_ident); + + fclose(s->fp); + + /* + * Reject an attempt to record a non-merge empty commit without + * explicit --allow-empty. In the cherry-pick case, it may be + * empty due to conflict resolution, which the user should okay. + */ + if (!commitable && whence != FROM_MERGE && !allow_empty && + !(amend && is_a_merge(current_head))) { + run_status(stdout, index_file, prefix, 0, s); + if (amend) + fputs(_(empty_amend_advice), stderr); + else if (whence == FROM_CHERRY_PICK) + fputs(_(empty_cherry_pick_advice), stderr); + return 0; + } + + /* + * Re-read the index as pre-commit hook could have updated it, + * and write it out as a tree. We must do this before we invoke + * the editor and after we invoke run_status above. + */ + discard_cache(); + read_cache_from(index_file); + if (update_main_cache_tree(0)) { + error(_("Error building trees")); + return 0; + } + + if (run_hook(index_file, "prepare-commit-msg", + git_path(commit_editmsg), hook_arg1, hook_arg2, NULL)) + return 0; + + if (use_editor) { + char index[PATH_MAX]; + const char *env[2] = { NULL }; + env[0] = index; + snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); + if (launch_editor(git_path(commit_editmsg), NULL, env)) { + fprintf(stderr, + _("Please supply the message using either -m or -F option.\n")); + exit(1); + } + } + + if (!no_verify && + run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) { + return 0; + } + + return 1; +} + +static int rest_is_empty(struct strbuf *sb, int start) +{ + int i, eol; + const char *nl; + + /* Check if the rest is just whitespace and Signed-of-by's. */ + for (i = start; i < sb->len; i++) { + nl = memchr(sb->buf + i, '\n', sb->len - i); + if (nl) + eol = nl - sb->buf; + else + eol = sb->len; + + if (strlen(sign_off_header) <= eol - i && + !prefixcmp(sb->buf + i, sign_off_header)) { + i = eol; + continue; + } + while (i < eol) + if (!isspace(sb->buf[i++])) + return 0; + } + + return 1; +} + +/* + * Find out if the message in the strbuf contains only whitespace and + * Signed-off-by lines. + */ +static int message_is_empty(struct strbuf *sb) +{ + if (cleanup_mode == CLEANUP_NONE && sb->len) + return 0; + return rest_is_empty(sb, 0); +} + +/* + * See if the user edited the message in the editor or left what + * was in the template intact + */ +static int template_untouched(struct strbuf *sb) +{ + struct strbuf tmpl = STRBUF_INIT; + char *start; + + if (cleanup_mode == CLEANUP_NONE && sb->len) + return 0; + + if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) + return 0; + + stripspace(&tmpl, cleanup_mode == CLEANUP_ALL); + start = (char *)skip_prefix(sb->buf, tmpl.buf); + if (!start) + start = sb->buf; + strbuf_release(&tmpl); + return rest_is_empty(sb, start - sb->buf); +} + +static const char *find_author_by_nickname(const char *name) +{ + struct rev_info revs; + struct commit *commit; + struct strbuf buf = STRBUF_INIT; + const char *av[20]; + int ac = 0; + + init_revisions(&revs, NULL); + strbuf_addf(&buf, "--author=%s", name); + av[++ac] = "--all"; + av[++ac] = "-i"; + av[++ac] = buf.buf; + av[++ac] = NULL; + setup_revisions(ac, av, &revs, NULL); + prepare_revision_walk(&revs); + commit = get_revision(&revs); + if (commit) { + struct pretty_print_context ctx = {0}; + ctx.date_mode = DATE_NORMAL; + strbuf_release(&buf); + format_commit_message(commit, "%an <%ae>", &buf, &ctx); + return strbuf_detach(&buf, NULL); + } + die(_("No existing author found with '%s'"), name); +} + + +static void handle_untracked_files_arg(struct wt_status *s) +{ + if (!untracked_files_arg) + ; /* default already initialized */ + else if (!strcmp(untracked_files_arg, "no")) + s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; + else if (!strcmp(untracked_files_arg, "normal")) + s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; + else if (!strcmp(untracked_files_arg, "all")) + s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; + else + die(_("Invalid untracked files mode '%s'"), untracked_files_arg); +} + +static const char *read_commit_message(const char *name) +{ + const char *out_enc, *out; + struct commit *commit; + + commit = lookup_commit_reference_by_name(name); + if (!commit) + die(_("could not lookup commit %s"), name); + out_enc = get_commit_output_encoding(); + out = logmsg_reencode(commit, out_enc); + + /* + * If we failed to reencode the buffer, just copy it + * byte for byte so the user can try to fix it up. + * This also handles the case where input and output + * encodings are identical. + */ + if (out == NULL) + out = xstrdup(commit->buffer); + return out; +} + +static int parse_and_validate_options(int argc, const char *argv[], + const struct option *options, + const char * const usage[], + const char *prefix, + struct commit *current_head, + struct wt_status *s) +{ + int f = 0; + + argc = parse_options(argc, argv, prefix, options, usage, 0); + + if (force_author && !strchr(force_author, '>')) + force_author = find_author_by_nickname(force_author); + + if (force_author && renew_authorship) + die(_("Using both --reset-author and --author does not make sense")); + + if (logfile || message.len || use_message || fixup_message) + use_editor = 0; + if (0 <= edit_flag) + use_editor = edit_flag; + if (!use_editor) + setenv("GIT_EDITOR", ":", 1); + + /* Sanity check options */ + if (amend && !current_head) + die(_("You have nothing to amend.")); + if (amend && whence != FROM_COMMIT) { + if (whence == FROM_MERGE) + die(_("You are in the middle of a merge -- cannot amend.")); + else if (whence == FROM_CHERRY_PICK) + die(_("You are in the middle of a cherry-pick -- cannot amend.")); + } + if (fixup_message && squash_message) + die(_("Options --squash and --fixup cannot be used together")); + if (use_message) + f++; + if (edit_message) + f++; + if (fixup_message) + f++; + if (logfile) + f++; + if (f > 1) + die(_("Only one of -c/-C/-F/--fixup can be used.")); + if (message.len && f > 0) + die((_("Option -m cannot be combined with -c/-C/-F/--fixup."))); + if (f || message.len) + template_file = NULL; + if (edit_message) + use_message = edit_message; + if (amend && !use_message && !fixup_message) + use_message = "HEAD"; + if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship) + die(_("--reset-author can be used only with -C, -c or --amend.")); + if (use_message) { + use_message_buffer = read_commit_message(use_message); + if (!renew_authorship) { + author_message = use_message; + author_message_buffer = use_message_buffer; + } + } + if (whence == FROM_CHERRY_PICK && !renew_authorship) { + author_message = "CHERRY_PICK_HEAD"; + author_message_buffer = read_commit_message(author_message); + } + + if (patch_interactive) + interactive = 1; + + if (!!also + !!only + !!all + !!interactive > 1) + die(_("Only one of --include/--only/--all/--interactive/--patch can be used.")); + if (argc == 0 && (also || (only && !amend))) + die(_("No paths with --include/--only does not make sense.")); + if (argc == 0 && only && amend) + only_include_assumed = _("Clever... amending the last one with dirty index."); + if (argc > 0 && !also && !only) + only_include_assumed = _("Explicit paths specified without -i nor -o; assuming --only paths..."); + if (!cleanup_arg || !strcmp(cleanup_arg, "default")) + cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE; + else if (!strcmp(cleanup_arg, "verbatim")) + cleanup_mode = CLEANUP_NONE; + else if (!strcmp(cleanup_arg, "whitespace")) + cleanup_mode = CLEANUP_SPACE; + else if (!strcmp(cleanup_arg, "strip")) + cleanup_mode = CLEANUP_ALL; + else + die(_("Invalid cleanup mode %s"), cleanup_arg); + + handle_untracked_files_arg(s); + + if (all && argc > 0) + die(_("Paths with -a does not make sense.")); + + if (s->null_termination) { + if (status_format == STATUS_FORMAT_NONE) + status_format = STATUS_FORMAT_PORCELAIN; + else if (status_format == STATUS_FORMAT_LONG) + die(_("--long and -z are incompatible")); + } + if (status_format != STATUS_FORMAT_NONE) + dry_run = 1; + + return argc; +} + +static int dry_run_commit(int argc, const char **argv, const char *prefix, + const struct commit *current_head, struct wt_status *s) +{ + int commitable; + const char *index_file; + + index_file = prepare_index(argc, argv, prefix, current_head, 1); + commitable = run_status(stdout, index_file, prefix, 0, s); + rollback_index_files(); + + return commitable ? 0 : 1; +} + +static int parse_status_slot(const char *var, int offset) +{ + if (!strcasecmp(var+offset, "header")) + return WT_STATUS_HEADER; + if (!strcasecmp(var+offset, "branch")) + return WT_STATUS_ONBRANCH; + if (!strcasecmp(var+offset, "updated") + || !strcasecmp(var+offset, "added")) + return WT_STATUS_UPDATED; + if (!strcasecmp(var+offset, "changed")) + return WT_STATUS_CHANGED; + if (!strcasecmp(var+offset, "untracked")) + return WT_STATUS_UNTRACKED; + if (!strcasecmp(var+offset, "nobranch")) + return WT_STATUS_NOBRANCH; + if (!strcasecmp(var+offset, "unmerged")) + return WT_STATUS_UNMERGED; + return -1; +} + +static int git_status_config(const char *k, const char *v, void *cb) +{ + struct wt_status *s = cb; + + if (!prefixcmp(k, "column.")) + return git_column_config(k, v, "status", &s->colopts); + if (!strcmp(k, "status.submodulesummary")) { + int is_bool; + s->submodule_summary = git_config_bool_or_int(k, v, &is_bool); + if (is_bool && s->submodule_summary) + s->submodule_summary = -1; + return 0; + } + if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { + s->use_color = git_config_colorbool(k, v); + return 0; + } + if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { + int slot = parse_status_slot(k, 13); + if (slot < 0) + return 0; + if (!v) + return config_error_nonbool(k); + color_parse(v, k, s->color_palette[slot]); + return 0; + } + if (!strcmp(k, "status.relativepaths")) { + s->relative_paths = git_config_bool(k, v); + return 0; + } + if (!strcmp(k, "status.showuntrackedfiles")) { + if (!v) + return config_error_nonbool(k); + else if (!strcmp(v, "no")) + s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; + else if (!strcmp(v, "normal")) + s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; + else if (!strcmp(v, "all")) + s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; + else + return error(_("Invalid untracked files mode '%s'"), v); + return 0; + } + return git_diff_ui_config(k, v, NULL); +} + +int cmd_status(int argc, const char **argv, const char *prefix) +{ + static struct wt_status s; + int fd; + unsigned char sha1[20]; + static struct option builtin_status_options[] = { + OPT__VERBOSE(&verbose, N_("be verbose")), + OPT_SET_INT('s', "short", &status_format, + N_("show status concisely"), STATUS_FORMAT_SHORT), + OPT_BOOLEAN('b', "branch", &s.show_branch, + N_("show branch information")), + OPT_SET_INT(0, "porcelain", &status_format, + N_("machine-readable output"), + STATUS_FORMAT_PORCELAIN), + OPT_SET_INT(0, "long", &status_format, + N_("show status in long format (default)"), + STATUS_FORMAT_LONG), + OPT_BOOLEAN('z', "null", &s.null_termination, + N_("terminate entries with NUL")), + { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, + N_("mode"), + N_("show untracked files, optional modes: all, normal, no. (Default: all)"), + PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, + OPT_BOOLEAN(0, "ignored", &show_ignored_in_status, + N_("show ignored files")), + { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"), + N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"), + PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, + OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")), + OPT_END(), + }; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(builtin_status_usage, builtin_status_options); + + wt_status_prepare(&s); + gitmodules_config(); + git_config(git_status_config, &s); + determine_whence(&s); + argc = parse_options(argc, argv, prefix, + builtin_status_options, + builtin_status_usage, 0); + finalize_colopts(&s.colopts, -1); + + if (s.null_termination) { + if (status_format == STATUS_FORMAT_NONE) + status_format = STATUS_FORMAT_PORCELAIN; + else if (status_format == STATUS_FORMAT_LONG) + die(_("--long and -z are incompatible")); + } + + handle_untracked_files_arg(&s); + if (show_ignored_in_status) + s.show_ignored_files = 1; + if (*argv) + s.pathspec = get_pathspec(prefix, argv); + + read_cache_preload(s.pathspec); + refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL); + + fd = hold_locked_index(&index_lock, 0); + if (0 <= fd) + update_index_if_able(&the_index, &index_lock); + + s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0; + s.ignore_submodule_arg = ignore_submodule_arg; + wt_status_collect(&s); + + if (s.relative_paths) + s.prefix = prefix; + + switch (status_format) { + case STATUS_FORMAT_SHORT: + wt_shortstatus_print(&s); + break; + case STATUS_FORMAT_PORCELAIN: + wt_porcelain_print(&s); + break; + case STATUS_FORMAT_NONE: + case STATUS_FORMAT_LONG: + s.verbose = verbose; + s.ignore_submodule_arg = ignore_submodule_arg; + wt_status_print(&s); + break; + } + return 0; +} + +static void print_summary(const char *prefix, const unsigned char *sha1, + int initial_commit) +{ + struct rev_info rev; + struct commit *commit; + struct strbuf format = STRBUF_INIT; + unsigned char junk_sha1[20]; + const char *head; + struct pretty_print_context pctx = {0}; + struct strbuf author_ident = STRBUF_INIT; + struct strbuf committer_ident = STRBUF_INIT; + + commit = lookup_commit(sha1); + if (!commit) + die(_("couldn't look up newly created commit")); + if (!commit || parse_commit(commit)) + die(_("could not parse newly created commit")); + + strbuf_addstr(&format, "format:%h] %s"); + + format_commit_message(commit, "%an <%ae>", &author_ident, &pctx); + format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx); + if (strbuf_cmp(&author_ident, &committer_ident)) { + strbuf_addstr(&format, "\n Author: "); + strbuf_addbuf_percentquote(&format, &author_ident); + } + if (!committer_ident_sufficiently_given()) { + strbuf_addstr(&format, "\n Committer: "); + strbuf_addbuf_percentquote(&format, &committer_ident); + if (advice_implicit_identity) { + strbuf_addch(&format, '\n'); + strbuf_addstr(&format, _(implicit_ident_advice)); + } + } + strbuf_release(&author_ident); + strbuf_release(&committer_ident); + + init_revisions(&rev, prefix); + setup_revisions(0, NULL, &rev, NULL); + + rev.diff = 1; + rev.diffopt.output_format = + DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; + + rev.verbose_header = 1; + rev.show_root_diff = 1; + get_commit_format(format.buf, &rev); + rev.always_show_header = 0; + rev.diffopt.detect_rename = 1; + rev.diffopt.break_opt = 0; + diff_setup_done(&rev.diffopt); + + head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL); + printf("[%s%s ", + !prefixcmp(head, "refs/heads/") ? + head + 11 : + !strcmp(head, "HEAD") ? + _("detached HEAD") : + head, + initial_commit ? _(" (root-commit)") : ""); + + if (!log_tree_commit(&rev, commit)) { + rev.always_show_header = 1; + rev.use_terminator = 1; + log_tree_commit(&rev, commit); + } + + strbuf_release(&format); +} + +static int git_commit_config(const char *k, const char *v, void *cb) +{ + struct wt_status *s = cb; + int status; + + if (!strcmp(k, "commit.template")) + return git_config_pathname(&template_file, k, v); + if (!strcmp(k, "commit.status")) { + include_status = git_config_bool(k, v); + return 0; + } + if (!strcmp(k, "commit.cleanup")) + return git_config_string(&cleanup_arg, k, v); + + status = git_gpg_config(k, v, NULL); + if (status) + return status; + return git_status_config(k, v, s); +} + +static const char post_rewrite_hook[] = "hooks/post-rewrite"; + +static int run_rewrite_hook(const unsigned char *oldsha1, + const unsigned char *newsha1) +{ + /* oldsha1 SP newsha1 LF NUL */ + static char buf[2*40 + 3]; + struct child_process proc; + const char *argv[3]; + int code; + size_t n; + + if (access(git_path(post_rewrite_hook), X_OK) < 0) + return 0; + + argv[0] = git_path(post_rewrite_hook); + argv[1] = "amend"; + argv[2] = NULL; + + memset(&proc, 0, sizeof(proc)); + proc.argv = argv; + proc.in = -1; + proc.stdout_to_stderr = 1; + + code = start_command(&proc); + if (code) + return code; + n = snprintf(buf, sizeof(buf), "%s %s\n", + sha1_to_hex(oldsha1), sha1_to_hex(newsha1)); + write_in_full(proc.in, buf, n); + close(proc.in); + return finish_command(&proc); +} + +int cmd_commit(int argc, const char **argv, const char *prefix) +{ + static struct wt_status s; + static struct option builtin_commit_options[] = { + OPT__QUIET(&quiet, N_("suppress summary after successful commit")), + OPT__VERBOSE(&verbose, N_("show diff in commit message template")), + + OPT_GROUP(N_("Commit message options")), + OPT_FILENAME('F', "file", &logfile, N_("read message from file")), + OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")), + OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")), + OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m), + OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")), + OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")), + OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")), + OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")), + OPT_BOOLEAN(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")), + OPT_BOOLEAN('s', "signoff", &signoff, N_("add Signed-off-by:")), + OPT_FILENAME('t', "template", &template_file, N_("use specified template file")), + OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")), + OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")), + OPT_BOOLEAN(0, "status", &include_status, N_("include status in commit message template")), + { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key id"), + N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, + /* end commit message options */ + + OPT_GROUP(N_("Commit contents options")), + OPT_BOOLEAN('a', "all", &all, N_("commit all changed files")), + OPT_BOOLEAN('i', "include", &also, N_("add specified files to index for commit")), + OPT_BOOLEAN(0, "interactive", &interactive, N_("interactively add files")), + OPT_BOOLEAN('p', "patch", &patch_interactive, N_("interactively add changes")), + OPT_BOOLEAN('o', "only", &only, N_("commit only specified files")), + OPT_BOOLEAN('n', "no-verify", &no_verify, N_("bypass pre-commit hook")), + OPT_BOOLEAN(0, "dry-run", &dry_run, N_("show what would be committed")), + OPT_SET_INT(0, "short", &status_format, N_("show status concisely"), + STATUS_FORMAT_SHORT), + OPT_BOOLEAN(0, "branch", &s.show_branch, N_("show branch information")), + OPT_SET_INT(0, "porcelain", &status_format, + N_("machine-readable output"), STATUS_FORMAT_PORCELAIN), + OPT_SET_INT(0, "long", &status_format, + N_("show status in long format (default)"), + STATUS_FORMAT_LONG), + OPT_BOOLEAN('z', "null", &s.null_termination, + N_("terminate entries with NUL")), + OPT_BOOLEAN(0, "amend", &amend, N_("amend previous commit")), + OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")), + { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, N_("mode"), N_("show untracked files, optional modes: all, normal, no. (Default: all)"), PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, + /* end commit contents options */ + + { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL, + N_("ok to record an empty change"), + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, + { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL, + N_("ok to record a change with an empty message"), + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, + + OPT_END() + }; + + struct strbuf sb = STRBUF_INIT; + struct strbuf author_ident = STRBUF_INIT; + const char *index_file, *reflog_msg; + char *nl, *p; + unsigned char sha1[20]; + struct ref_lock *ref_lock; + struct commit_list *parents = NULL, **pptr = &parents; + struct stat statbuf; + int allow_fast_forward = 1; + struct commit *current_head = NULL; + struct commit_extra_header *extra = NULL; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(builtin_commit_usage, builtin_commit_options); + + wt_status_prepare(&s); + gitmodules_config(); + git_config(git_commit_config, &s); + determine_whence(&s); + s.colopts = 0; + + if (get_sha1("HEAD", sha1)) + current_head = NULL; + else { + current_head = lookup_commit_or_die(sha1, "HEAD"); + if (!current_head || parse_commit(current_head)) + die(_("could not parse HEAD commit")); + } + argc = parse_and_validate_options(argc, argv, builtin_commit_options, + builtin_commit_usage, + prefix, current_head, &s); + if (dry_run) + return dry_run_commit(argc, argv, prefix, current_head, &s); + index_file = prepare_index(argc, argv, prefix, current_head, 0); + + /* Set up everything for writing the commit object. This includes + running hooks, writing the trees, and interacting with the user. */ + if (!prepare_to_commit(index_file, prefix, + current_head, &s, &author_ident)) { + rollback_index_files(); + return 1; + } + + /* Determine parents */ + reflog_msg = getenv("GIT_REFLOG_ACTION"); + if (!current_head) { + if (!reflog_msg) + reflog_msg = "commit (initial)"; + } else if (amend) { + struct commit_list *c; + + if (!reflog_msg) + reflog_msg = "commit (amend)"; + for (c = current_head->parents; c; c = c->next) + pptr = &commit_list_insert(c->item, pptr)->next; + } else if (whence == FROM_MERGE) { + struct strbuf m = STRBUF_INIT; + FILE *fp; + + if (!reflog_msg) + reflog_msg = "commit (merge)"; + pptr = &commit_list_insert(current_head, pptr)->next; + fp = fopen(git_path("MERGE_HEAD"), "r"); + if (fp == NULL) + die_errno(_("could not open '%s' for reading"), + git_path("MERGE_HEAD")); + while (strbuf_getline(&m, fp, '\n') != EOF) { + struct commit *parent; + + parent = get_merge_parent(m.buf); + if (!parent) + die(_("Corrupt MERGE_HEAD file (%s)"), m.buf); + pptr = &commit_list_insert(parent, pptr)->next; + } + fclose(fp); + strbuf_release(&m); + if (!stat(git_path("MERGE_MODE"), &statbuf)) { + if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0) + die_errno(_("could not read MERGE_MODE")); + if (!strcmp(sb.buf, "no-ff")) + allow_fast_forward = 0; + } + if (allow_fast_forward) + parents = reduce_heads(parents); + } else { + if (!reflog_msg) + reflog_msg = (whence == FROM_CHERRY_PICK) + ? "commit (cherry-pick)" + : "commit"; + pptr = &commit_list_insert(current_head, pptr)->next; + } + + /* Finally, get the commit message */ + strbuf_reset(&sb); + if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) { + int saved_errno = errno; + rollback_index_files(); + die(_("could not read commit message: %s"), strerror(saved_errno)); + } + + /* Truncate the message just before the diff, if any. */ + if (verbose) { + p = strstr(sb.buf, "\ndiff --git "); + if (p != NULL) + strbuf_setlen(&sb, p - sb.buf + 1); + } + + if (cleanup_mode != CLEANUP_NONE) + stripspace(&sb, cleanup_mode == CLEANUP_ALL); + if (template_untouched(&sb) && !allow_empty_message) { + rollback_index_files(); + fprintf(stderr, _("Aborting commit; you did not edit the message.\n")); + exit(1); + } + if (message_is_empty(&sb) && !allow_empty_message) { + rollback_index_files(); + fprintf(stderr, _("Aborting commit due to empty commit message.\n")); + exit(1); + } + + if (amend) { + const char *exclude_gpgsig[2] = { "gpgsig", NULL }; + extra = read_commit_extra_headers(current_head, exclude_gpgsig); + } else { + struct commit_extra_header **tail = &extra; + append_merge_tag_headers(parents, &tail); + } + + if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1, + author_ident.buf, sign_commit, extra)) { + rollback_index_files(); + die(_("failed to write commit object")); + } + strbuf_release(&author_ident); + free_commit_extra_headers(extra); + + ref_lock = lock_any_ref_for_update("HEAD", + !current_head + ? NULL + : current_head->object.sha1, + 0); + + nl = strchr(sb.buf, '\n'); + if (nl) + strbuf_setlen(&sb, nl + 1 - sb.buf); + else + strbuf_addch(&sb, '\n'); + strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg)); + strbuf_insert(&sb, strlen(reflog_msg), ": ", 2); + + if (!ref_lock) { + rollback_index_files(); + die(_("cannot lock HEAD ref")); + } + if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) { + rollback_index_files(); + die(_("cannot update HEAD ref")); + } + + unlink(git_path("CHERRY_PICK_HEAD")); + unlink(git_path("REVERT_HEAD")); + unlink(git_path("MERGE_HEAD")); + unlink(git_path("MERGE_MSG")); + unlink(git_path("MERGE_MODE")); + unlink(git_path("SQUASH_MSG")); + + if (commit_index_files()) + die (_("Repository has been updated, but unable to write\n" + "new_index file. Check that disk is not full or quota is\n" + "not exceeded, and then \"git reset HEAD\" to recover.")); + + rerere(0); + run_hook(get_index_file(), "post-commit", NULL); + if (amend && !no_post_rewrite) { + struct notes_rewrite_cfg *cfg; + cfg = init_copy_notes_for_rewrite("amend"); + if (cfg) { + /* we are amending, so current_head is not NULL */ + copy_note_for_rewrite(cfg, current_head->object.sha1, sha1); + finish_copy_notes_for_rewrite(cfg); + } + run_rewrite_hook(current_head->object.sha1, sha1); + } + if (!quiet) + print_summary(prefix, sha1, !current_head); + + return 0; +} diff --git a/builtin/config.c b/builtin/config.c new file mode 100644 index 0000000000..33c9bf9d84 --- /dev/null +++ b/builtin/config.c @@ -0,0 +1,552 @@ +#include "builtin.h" +#include "cache.h" +#include "color.h" +#include "parse-options.h" + +static const char *const builtin_config_usage[] = { + N_("git config [options]"), + NULL +}; + +static char *key; +static regex_t *key_regexp; +static regex_t *regexp; +static int show_keys; +static int use_key_regexp; +static int do_all; +static int do_not_match; +static char delim = '='; +static char key_delim = ' '; +static char term = '\n'; + +static int use_global_config, use_system_config, use_local_config; +static const char *given_config_file; +static int actions, types; +static const char *get_color_slot, *get_colorbool_slot; +static int end_null; +static int respect_includes = -1; + +#define ACTION_GET (1<<0) +#define ACTION_GET_ALL (1<<1) +#define ACTION_GET_REGEXP (1<<2) +#define ACTION_REPLACE_ALL (1<<3) +#define ACTION_ADD (1<<4) +#define ACTION_UNSET (1<<5) +#define ACTION_UNSET_ALL (1<<6) +#define ACTION_RENAME_SECTION (1<<7) +#define ACTION_REMOVE_SECTION (1<<8) +#define ACTION_LIST (1<<9) +#define ACTION_EDIT (1<<10) +#define ACTION_SET (1<<11) +#define ACTION_SET_ALL (1<<12) +#define ACTION_GET_COLOR (1<<13) +#define ACTION_GET_COLORBOOL (1<<14) + +#define TYPE_BOOL (1<<0) +#define TYPE_INT (1<<1) +#define TYPE_BOOL_OR_INT (1<<2) +#define TYPE_PATH (1<<3) + +static struct option builtin_config_options[] = { + OPT_GROUP(N_("Config file location")), + OPT_BOOLEAN(0, "global", &use_global_config, N_("use global config file")), + OPT_BOOLEAN(0, "system", &use_system_config, N_("use system config file")), + OPT_BOOLEAN(0, "local", &use_local_config, N_("use repository config file")), + OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")), + OPT_GROUP(N_("Action")), + OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET), + OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL), + OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP), + OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL), + OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), + OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET), + OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL), + OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), + OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), + OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), + OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), + OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")), + OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")), + OPT_GROUP(N_("Type")), + OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL), + OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT), + OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), + OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH), + OPT_GROUP(N_("Other")), + OPT_BOOLEAN('z', "null", &end_null, N_("terminate values with NUL byte")), + OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")), + OPT_END(), +}; + +static void check_argc(int argc, int min, int max) { + if (argc >= min && argc <= max) + return; + error("wrong number of arguments"); + usage_with_options(builtin_config_usage, builtin_config_options); +} + +static int show_all_config(const char *key_, const char *value_, void *cb) +{ + if (value_) + printf("%s%c%s%c", key_, delim, value_, term); + else + printf("%s%c", key_, term); + return 0; +} + +struct strbuf_list { + struct strbuf *items; + int nr; + int alloc; +}; + +static int collect_config(const char *key_, const char *value_, void *cb) +{ + struct strbuf_list *values = cb; + struct strbuf *buf; + char value[256]; + const char *vptr = value; + int must_free_vptr = 0; + int must_print_delim = 0; + + if (!use_key_regexp && strcmp(key_, key)) + return 0; + if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0)) + return 0; + if (regexp != NULL && + (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0))) + return 0; + + ALLOC_GROW(values->items, values->nr + 1, values->alloc); + buf = &values->items[values->nr++]; + strbuf_init(buf, 0); + + if (show_keys) { + strbuf_addstr(buf, key_); + must_print_delim = 1; + } + if (types == TYPE_INT) + sprintf(value, "%d", git_config_int(key_, value_?value_:"")); + else if (types == TYPE_BOOL) + vptr = git_config_bool(key_, value_) ? "true" : "false"; + else if (types == TYPE_BOOL_OR_INT) { + int is_bool, v; + v = git_config_bool_or_int(key_, value_, &is_bool); + if (is_bool) + vptr = v ? "true" : "false"; + else + sprintf(value, "%d", v); + } else if (types == TYPE_PATH) { + if (git_config_pathname(&vptr, key_, value_) < 0) + return -1; + must_free_vptr = 1; + } else if (value_) { + vptr = value_; + } else { + /* Just show the key name */ + vptr = ""; + must_print_delim = 0; + } + + if (must_print_delim) + strbuf_addch(buf, key_delim); + strbuf_addstr(buf, vptr); + strbuf_addch(buf, term); + + if (must_free_vptr) + /* If vptr must be freed, it's a pointer to a + * dynamically allocated buffer, it's safe to cast to + * const. + */ + free((char *)vptr); + + return 0; +} + +static int get_value(const char *key_, const char *regex_) +{ + int ret = CONFIG_GENERIC_ERROR; + struct strbuf_list values = {NULL}; + int i; + + if (use_key_regexp) { + char *tl; + + /* + * NEEDSWORK: this naive pattern lowercasing obviously does not + * work for more complex patterns like "^[^.]*Foo.*bar". + * Perhaps we should deprecate this altogether someday. + */ + + key = xstrdup(key_); + for (tl = key + strlen(key) - 1; + tl >= key && *tl != '.'; + tl--) + *tl = tolower(*tl); + for (tl = key; *tl && *tl != '.'; tl++) + *tl = tolower(*tl); + + key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); + if (regcomp(key_regexp, key, REG_EXTENDED)) { + fprintf(stderr, "Invalid key pattern: %s\n", key_); + free(key_regexp); + key_regexp = NULL; + ret = CONFIG_INVALID_PATTERN; + goto free_strings; + } + } else { + if (git_config_parse_key(key_, &key, NULL)) { + ret = CONFIG_INVALID_KEY; + goto free_strings; + } + } + + if (regex_) { + if (regex_[0] == '!') { + do_not_match = 1; + regex_++; + } + + regexp = (regex_t*)xmalloc(sizeof(regex_t)); + if (regcomp(regexp, regex_, REG_EXTENDED)) { + fprintf(stderr, "Invalid pattern: %s\n", regex_); + free(regexp); + regexp = NULL; + ret = CONFIG_INVALID_PATTERN; + goto free_strings; + } + } + + git_config_with_options(collect_config, &values, + given_config_file, respect_includes); + + ret = !values.nr; + + for (i = 0; i < values.nr; i++) { + struct strbuf *buf = values.items + i; + if (do_all || i == values.nr - 1) + fwrite(buf->buf, 1, buf->len, stdout); + strbuf_release(buf); + } + free(values.items); + +free_strings: + free(key); + if (key_regexp) { + regfree(key_regexp); + free(key_regexp); + } + if (regexp) { + regfree(regexp); + free(regexp); + } + + return ret; +} + +static char *normalize_value(const char *key, const char *value) +{ + char *normalized; + + if (!value) + return NULL; + + if (types == 0 || types == TYPE_PATH) + /* + * We don't do normalization for TYPE_PATH here: If + * the path is like ~/foobar/, we prefer to store + * "~/foobar/" in the config file, and to expand the ~ + * when retrieving the value. + */ + normalized = xstrdup(value); + else { + normalized = xmalloc(64); + if (types == TYPE_INT) { + int v = git_config_int(key, value); + sprintf(normalized, "%d", v); + } + else if (types == TYPE_BOOL) + sprintf(normalized, "%s", + git_config_bool(key, value) ? "true" : "false"); + else if (types == TYPE_BOOL_OR_INT) { + int is_bool, v; + v = git_config_bool_or_int(key, value, &is_bool); + if (!is_bool) + sprintf(normalized, "%d", v); + else + sprintf(normalized, "%s", v ? "true" : "false"); + } + } + + return normalized; +} + +static int get_color_found; +static const char *get_color_slot; +static const char *get_colorbool_slot; +static char parsed_color[COLOR_MAXLEN]; + +static int git_get_color_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, get_color_slot)) { + if (!value) + config_error_nonbool(var); + color_parse(value, var, parsed_color); + get_color_found = 1; + } + return 0; +} + +static void get_color(const char *def_color) +{ + get_color_found = 0; + parsed_color[0] = '\0'; + git_config_with_options(git_get_color_config, NULL, + given_config_file, respect_includes); + + if (!get_color_found && def_color) + color_parse(def_color, "command line", parsed_color); + + fputs(parsed_color, stdout); +} + +static int get_colorbool_found; +static int get_diff_color_found; +static int get_color_ui_found; +static int git_get_colorbool_config(const char *var, const char *value, + void *cb) +{ + if (!strcmp(var, get_colorbool_slot)) + get_colorbool_found = git_config_colorbool(var, value); + else if (!strcmp(var, "diff.color")) + get_diff_color_found = git_config_colorbool(var, value); + else if (!strcmp(var, "color.ui")) + get_color_ui_found = git_config_colorbool(var, value); + return 0; +} + +static int get_colorbool(int print) +{ + get_colorbool_found = -1; + get_diff_color_found = -1; + git_config_with_options(git_get_colorbool_config, NULL, + given_config_file, respect_includes); + + if (get_colorbool_found < 0) { + if (!strcmp(get_colorbool_slot, "color.diff")) + get_colorbool_found = get_diff_color_found; + if (get_colorbool_found < 0) + get_colorbool_found = get_color_ui_found; + } + + get_colorbool_found = want_color(get_colorbool_found); + + if (print) { + printf("%s\n", get_colorbool_found ? "true" : "false"); + return 0; + } else + return get_colorbool_found ? 0 : 1; +} + +int cmd_config(int argc, const char **argv, const char *prefix) +{ + int nongit = !startup_info->have_repository; + char *value; + + given_config_file = getenv(CONFIG_ENVIRONMENT); + + argc = parse_options(argc, argv, prefix, builtin_config_options, + builtin_config_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + + if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) { + error("only one config file at a time."); + usage_with_options(builtin_config_usage, builtin_config_options); + } + + if (use_global_config) { + char *user_config = NULL; + char *xdg_config = NULL; + + home_config_paths(&user_config, &xdg_config, "config"); + + if (!user_config) + /* + * It is unknown if HOME/.gitconfig exists, so + * we do not know if we should write to XDG + * location; error out even if XDG_CONFIG_HOME + * is set and points at a sane location. + */ + die("$HOME not set"); + + if (access_or_warn(user_config, R_OK) && + xdg_config && !access_or_warn(xdg_config, R_OK)) + given_config_file = xdg_config; + else + given_config_file = user_config; + } + else if (use_system_config) + given_config_file = git_etc_gitconfig(); + else if (use_local_config) + given_config_file = git_pathdup("config"); + else if (given_config_file) { + if (!is_absolute_path(given_config_file) && prefix) + given_config_file = + xstrdup(prefix_filename(prefix, + strlen(prefix), + given_config_file)); + } + + if (respect_includes == -1) + respect_includes = !given_config_file; + + if (end_null) { + term = '\0'; + delim = '\n'; + key_delim = '\n'; + } + + if (HAS_MULTI_BITS(types)) { + error("only one type at a time."); + usage_with_options(builtin_config_usage, builtin_config_options); + } + + if (get_color_slot) + actions |= ACTION_GET_COLOR; + if (get_colorbool_slot) + actions |= ACTION_GET_COLORBOOL; + + if ((get_color_slot || get_colorbool_slot) && types) { + error("--get-color and variable type are incoherent"); + usage_with_options(builtin_config_usage, builtin_config_options); + } + + if (HAS_MULTI_BITS(actions)) { + error("only one action at a time."); + usage_with_options(builtin_config_usage, builtin_config_options); + } + if (actions == 0) + switch (argc) { + case 1: actions = ACTION_GET; break; + case 2: actions = ACTION_SET; break; + case 3: actions = ACTION_SET_ALL; break; + default: + usage_with_options(builtin_config_usage, builtin_config_options); + } + + if (actions == ACTION_LIST) { + check_argc(argc, 0, 0); + if (git_config_with_options(show_all_config, NULL, + given_config_file, + respect_includes) < 0) { + if (given_config_file) + die_errno("unable to read config file '%s'", + given_config_file); + else + die("error processing config file(s)"); + } + } + else if (actions == ACTION_EDIT) { + check_argc(argc, 0, 0); + if (!given_config_file && nongit) + die("not in a git directory"); + git_config(git_default_config, NULL); + launch_editor(given_config_file ? + given_config_file : git_path("config"), + NULL, NULL); + } + else if (actions == ACTION_SET) { + int ret; + check_argc(argc, 2, 2); + value = normalize_value(argv[0], argv[1]); + ret = git_config_set_in_file(given_config_file, argv[0], value); + if (ret == CONFIG_NOTHING_SET) + error("cannot overwrite multiple values with a single value\n" + " Use a regexp, --add or --replace-all to change %s.", argv[0]); + return ret; + } + else if (actions == ACTION_SET_ALL) { + check_argc(argc, 2, 3); + value = normalize_value(argv[0], argv[1]); + return git_config_set_multivar_in_file(given_config_file, + argv[0], value, argv[2], 0); + } + else if (actions == ACTION_ADD) { + check_argc(argc, 2, 2); + value = normalize_value(argv[0], argv[1]); + return git_config_set_multivar_in_file(given_config_file, + argv[0], value, "^$", 0); + } + else if (actions == ACTION_REPLACE_ALL) { + check_argc(argc, 2, 3); + value = normalize_value(argv[0], argv[1]); + return git_config_set_multivar_in_file(given_config_file, + argv[0], value, argv[2], 1); + } + else if (actions == ACTION_GET) { + check_argc(argc, 1, 2); + return get_value(argv[0], argv[1]); + } + else if (actions == ACTION_GET_ALL) { + do_all = 1; + check_argc(argc, 1, 2); + return get_value(argv[0], argv[1]); + } + else if (actions == ACTION_GET_REGEXP) { + show_keys = 1; + use_key_regexp = 1; + do_all = 1; + check_argc(argc, 1, 2); + return get_value(argv[0], argv[1]); + } + else if (actions == ACTION_UNSET) { + check_argc(argc, 1, 2); + if (argc == 2) + return git_config_set_multivar_in_file(given_config_file, + argv[0], NULL, argv[1], 0); + else + return git_config_set_in_file(given_config_file, + argv[0], NULL); + } + else if (actions == ACTION_UNSET_ALL) { + check_argc(argc, 1, 2); + return git_config_set_multivar_in_file(given_config_file, + argv[0], NULL, argv[1], 1); + } + else if (actions == ACTION_RENAME_SECTION) { + int ret; + check_argc(argc, 2, 2); + ret = git_config_rename_section_in_file(given_config_file, + argv[0], argv[1]); + if (ret < 0) + return ret; + if (ret == 0) + die("No such section!"); + } + else if (actions == ACTION_REMOVE_SECTION) { + int ret; + check_argc(argc, 1, 1); + ret = git_config_rename_section_in_file(given_config_file, + argv[0], NULL); + if (ret < 0) + return ret; + if (ret == 0) + die("No such section!"); + } + else if (actions == ACTION_GET_COLOR) { + get_color(argv[0]); + } + else if (actions == ACTION_GET_COLORBOOL) { + if (argc == 1) + color_stdout_is_tty = git_config_bool("command line", argv[0]); + return get_colorbool(argc != 0); + } + + return 0; +} + +int cmd_repo_config(int argc, const char **argv, const char *prefix) +{ + fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n"); + return cmd_config(argc, argv, prefix); +} diff --git a/builtin/count-objects.c b/builtin/count-objects.c new file mode 100644 index 0000000000..9afaa88f77 --- /dev/null +++ b/builtin/count-objects.c @@ -0,0 +1,130 @@ +/* + * Builtin "git count-objects". + * + * Copyright (c) 2006 Junio C Hamano + */ + +#include "cache.h" +#include "dir.h" +#include "builtin.h" +#include "parse-options.h" + +static void count_objects(DIR *d, char *path, int len, int verbose, + unsigned long *loose, + off_t *loose_size, + unsigned long *packed_loose, + unsigned long *garbage) +{ + struct dirent *ent; + while ((ent = readdir(d)) != NULL) { + char hex[41]; + unsigned char sha1[20]; + const char *cp; + int bad = 0; + + if (is_dot_or_dotdot(ent->d_name)) + continue; + for (cp = ent->d_name; *cp; cp++) { + int ch = *cp; + if (('0' <= ch && ch <= '9') || + ('a' <= ch && ch <= 'f')) + continue; + bad = 1; + break; + } + if (cp - ent->d_name != 38) + bad = 1; + else { + struct stat st; + memcpy(path + len + 3, ent->d_name, 38); + path[len + 2] = '/'; + path[len + 41] = 0; + if (lstat(path, &st) || !S_ISREG(st.st_mode)) + bad = 1; + else + (*loose_size) += xsize_t(on_disk_bytes(st)); + } + if (bad) { + if (verbose) { + error("garbage found: %.*s/%s", + len + 2, path, ent->d_name); + (*garbage)++; + } + continue; + } + (*loose)++; + if (!verbose) + continue; + memcpy(hex, path+len, 2); + memcpy(hex+2, ent->d_name, 38); + hex[40] = 0; + if (get_sha1_hex(hex, sha1)) + die("internal error"); + if (has_sha1_pack(sha1)) + (*packed_loose)++; + } +} + +static char const * const count_objects_usage[] = { + N_("git count-objects [-v]"), + NULL +}; + +int cmd_count_objects(int argc, const char **argv, const char *prefix) +{ + int i, verbose = 0; + const char *objdir = get_object_directory(); + int len = strlen(objdir); + char *path = xmalloc(len + 50); + unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0; + off_t loose_size = 0; + struct option opts[] = { + OPT__VERBOSE(&verbose, N_("be verbose")), + OPT_END(), + }; + + argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0); + /* we do not take arguments other than flags for now */ + if (argc) + usage_with_options(count_objects_usage, opts); + memcpy(path, objdir, len); + if (len && objdir[len-1] != '/') + path[len++] = '/'; + for (i = 0; i < 256; i++) { + DIR *d; + sprintf(path + len, "%02x", i); + d = opendir(path); + if (!d) + continue; + count_objects(d, path, len, verbose, + &loose, &loose_size, &packed_loose, &garbage); + closedir(d); + } + if (verbose) { + struct packed_git *p; + unsigned long num_pack = 0; + off_t size_pack = 0; + if (!packed_git) + prepare_packed_git(); + for (p = packed_git; p; p = p->next) { + if (!p->pack_local) + continue; + if (open_pack_index(p)) + continue; + packed += p->num_objects; + size_pack += p->pack_size + p->index_size; + num_pack++; + } + printf("count: %lu\n", loose); + printf("size: %lu\n", (unsigned long) (loose_size / 1024)); + printf("in-pack: %lu\n", packed); + printf("packs: %lu\n", num_pack); + printf("size-pack: %lu\n", (unsigned long) (size_pack / 1024)); + printf("prune-packable: %lu\n", packed_loose); + printf("garbage: %lu\n", garbage); + } + else + printf("%lu objects, %lu kilobytes\n", + loose, (unsigned long) (loose_size / 1024)); + return 0; +} diff --git a/builtin/credential.c b/builtin/credential.c new file mode 100644 index 0000000000..0412fa00f0 --- /dev/null +++ b/builtin/credential.c @@ -0,0 +1,31 @@ +#include "git-compat-util.h" +#include "credential.h" +#include "builtin.h" + +static const char usage_msg[] = + "git credential [fill|approve|reject]"; + +int cmd_credential(int argc, const char **argv, const char *prefix) +{ + const char *op; + struct credential c = CREDENTIAL_INIT; + + op = argv[1]; + if (!op) + usage(usage_msg); + + if (credential_read(&c, stdin) < 0) + die("unable to read credential from stdin"); + + if (!strcmp(op, "fill")) { + credential_fill(&c); + credential_write(&c, stdout); + } else if (!strcmp(op, "approve")) { + credential_approve(&c); + } else if (!strcmp(op, "reject")) { + credential_reject(&c); + } else { + usage(usage_msg); + } + return 0; +} diff --git a/builtin/describe.c b/builtin/describe.c new file mode 100644 index 0000000000..04c185b1fb --- /dev/null +++ b/builtin/describe.c @@ -0,0 +1,489 @@ +#include "cache.h" +#include "commit.h" +#include "tag.h" +#include "refs.h" +#include "builtin.h" +#include "exec_cmd.h" +#include "parse-options.h" +#include "diff.h" +#include "hash.h" + +#define SEEN (1u<<0) +#define MAX_TAGS (FLAG_BITS - 1) + +static const char * const describe_usage[] = { + N_("git describe [options] <committish>*"), + N_("git describe [options] --dirty"), + NULL +}; + +static int debug; /* Display lots of verbose info */ +static int all; /* Any valid ref can be used */ +static int tags; /* Allow lightweight tags */ +static int longformat; +static int abbrev = -1; /* unspecified */ +static int max_candidates = 10; +static struct hash_table names; +static int have_util; +static const char *pattern; +static int always; +static const char *dirty; + +/* diff-index command arguments to check if working tree is dirty. */ +static const char *diff_index_args[] = { + "diff-index", "--quiet", "HEAD", "--", NULL +}; + + +struct commit_name { + struct commit_name *next; + unsigned char peeled[20]; + struct tag *tag; + unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */ + unsigned name_checked:1; + unsigned char sha1[20]; + const char *path; +}; +static const char *prio_names[] = { + "head", "lightweight", "annotated", +}; + +static inline unsigned int hash_sha1(const unsigned char *sha1) +{ + unsigned int hash; + memcpy(&hash, sha1, sizeof(hash)); + return hash; +} + +static inline struct commit_name *find_commit_name(const unsigned char *peeled) +{ + struct commit_name *n = lookup_hash(hash_sha1(peeled), &names); + while (n && !!hashcmp(peeled, n->peeled)) + n = n->next; + return n; +} + +static int set_util(void *chain, void *data) +{ + struct commit_name *n; + for (n = chain; n; n = n->next) { + struct commit *c = lookup_commit_reference_gently(n->peeled, 1); + if (c) + c->util = n; + } + return 0; +} + +static int replace_name(struct commit_name *e, + int prio, + const unsigned char *sha1, + struct tag **tag) +{ + if (!e || e->prio < prio) + return 1; + + if (e->prio == 2 && prio == 2) { + /* Multiple annotated tags point to the same commit. + * Select one to keep based upon their tagger date. + */ + struct tag *t; + + if (!e->tag) { + t = lookup_tag(e->sha1); + if (!t || parse_tag(t)) + return 1; + e->tag = t; + } + + t = lookup_tag(sha1); + if (!t || parse_tag(t)) + return 0; + *tag = t; + + if (e->tag->date < t->date) + return 1; + } + + return 0; +} + +static void add_to_known_names(const char *path, + const unsigned char *peeled, + int prio, + const unsigned char *sha1) +{ + struct commit_name *e = find_commit_name(peeled); + struct tag *tag = NULL; + if (replace_name(e, prio, sha1, &tag)) { + if (!e) { + void **pos; + e = xmalloc(sizeof(struct commit_name)); + hashcpy(e->peeled, peeled); + pos = insert_hash(hash_sha1(peeled), e, &names); + if (pos) { + e->next = *pos; + *pos = e; + } else { + e->next = NULL; + } + } + e->tag = tag; + e->prio = prio; + e->name_checked = 0; + hashcpy(e->sha1, sha1); + e->path = path; + } +} + +static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data) +{ + int might_be_tag = !prefixcmp(path, "refs/tags/"); + unsigned char peeled[20]; + int is_tag, prio; + + if (!all && !might_be_tag) + return 0; + + if (!peel_ref(path, peeled)) { + is_tag = !!hashcmp(sha1, peeled); + } else { + hashcpy(peeled, sha1); + is_tag = 0; + } + + /* If --all, then any refs are used. + * If --tags, then any tags are used. + * Otherwise only annotated tags are used. + */ + if (might_be_tag) { + if (is_tag) + prio = 2; + else + prio = 1; + + if (pattern && fnmatch(pattern, path + 10, 0)) + prio = 0; + } + else + prio = 0; + + if (!all) { + if (!prio) + return 0; + } + add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1); + return 0; +} + +struct possible_tag { + struct commit_name *name; + int depth; + int found_order; + unsigned flag_within; +}; + +static int compare_pt(const void *a_, const void *b_) +{ + struct possible_tag *a = (struct possible_tag *)a_; + struct possible_tag *b = (struct possible_tag *)b_; + if (a->depth != b->depth) + return a->depth - b->depth; + if (a->found_order != b->found_order) + return a->found_order - b->found_order; + return 0; +} + +static unsigned long finish_depth_computation( + struct commit_list **list, + struct possible_tag *best) +{ + unsigned long seen_commits = 0; + while (*list) { + struct commit *c = pop_commit(list); + struct commit_list *parents = c->parents; + seen_commits++; + if (c->object.flags & best->flag_within) { + struct commit_list *a = *list; + while (a) { + struct commit *i = a->item; + if (!(i->object.flags & best->flag_within)) + break; + a = a->next; + } + if (!a) + break; + } else + best->depth++; + while (parents) { + struct commit *p = parents->item; + parse_commit(p); + if (!(p->object.flags & SEEN)) + commit_list_insert_by_date(p, list); + p->object.flags |= c->object.flags; + parents = parents->next; + } + } + return seen_commits; +} + +static void display_name(struct commit_name *n) +{ + if (n->prio == 2 && !n->tag) { + n->tag = lookup_tag(n->sha1); + if (!n->tag || parse_tag(n->tag)) + die(_("annotated tag %s not available"), n->path); + } + if (n->tag && !n->name_checked) { + if (!n->tag->tag) + die(_("annotated tag %s has no embedded name"), n->path); + if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) + warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path); + n->name_checked = 1; + } + + if (n->tag) + printf("%s", n->tag->tag); + else + printf("%s", n->path); +} + +static void show_suffix(int depth, const unsigned char *sha1) +{ + printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev)); +} + +static void describe(const char *arg, int last_one) +{ + unsigned char sha1[20]; + struct commit *cmit, *gave_up_on = NULL; + struct commit_list *list; + struct commit_name *n; + struct possible_tag all_matches[MAX_TAGS]; + unsigned int match_cnt = 0, annotated_cnt = 0, cur_match; + unsigned long seen_commits = 0; + unsigned int unannotated_cnt = 0; + + if (get_sha1(arg, sha1)) + die(_("Not a valid object name %s"), arg); + cmit = lookup_commit_reference(sha1); + if (!cmit) + die(_("%s is not a valid '%s' object"), arg, commit_type); + + n = find_commit_name(cmit->object.sha1); + if (n && (tags || all || n->prio == 2)) { + /* + * Exact match to an existing ref. + */ + display_name(n); + if (longformat) + show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1); + if (dirty) + printf("%s", dirty); + printf("\n"); + return; + } + + if (!max_candidates) + die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1)); + if (debug) + fprintf(stderr, _("searching to describe %s\n"), arg); + + if (!have_util) { + for_each_hash(&names, set_util, NULL); + have_util = 1; + } + + list = NULL; + cmit->object.flags = SEEN; + commit_list_insert(cmit, &list); + while (list) { + struct commit *c = pop_commit(&list); + struct commit_list *parents = c->parents; + seen_commits++; + n = c->util; + if (n) { + if (!tags && !all && n->prio < 2) { + unannotated_cnt++; + } else if (match_cnt < max_candidates) { + struct possible_tag *t = &all_matches[match_cnt++]; + t->name = n; + t->depth = seen_commits - 1; + t->flag_within = 1u << match_cnt; + t->found_order = match_cnt; + c->object.flags |= t->flag_within; + if (n->prio == 2) + annotated_cnt++; + } + else { + gave_up_on = c; + break; + } + } + for (cur_match = 0; cur_match < match_cnt; cur_match++) { + struct possible_tag *t = &all_matches[cur_match]; + if (!(c->object.flags & t->flag_within)) + t->depth++; + } + if (annotated_cnt && !list) { + if (debug) + fprintf(stderr, _("finished search at %s\n"), + sha1_to_hex(c->object.sha1)); + break; + } + while (parents) { + struct commit *p = parents->item; + parse_commit(p); + if (!(p->object.flags & SEEN)) + commit_list_insert_by_date(p, &list); + p->object.flags |= c->object.flags; + parents = parents->next; + } + } + + if (!match_cnt) { + const unsigned char *sha1 = cmit->object.sha1; + if (always) { + printf("%s", find_unique_abbrev(sha1, abbrev)); + if (dirty) + printf("%s", dirty); + printf("\n"); + return; + } + if (unannotated_cnt) + die(_("No annotated tags can describe '%s'.\n" + "However, there were unannotated tags: try --tags."), + sha1_to_hex(sha1)); + else + die(_("No tags can describe '%s'.\n" + "Try --always, or create some tags."), + sha1_to_hex(sha1)); + } + + qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt); + + if (gave_up_on) { + commit_list_insert_by_date(gave_up_on, &list); + seen_commits--; + } + seen_commits += finish_depth_computation(&list, &all_matches[0]); + free_commit_list(list); + + if (debug) { + for (cur_match = 0; cur_match < match_cnt; cur_match++) { + struct possible_tag *t = &all_matches[cur_match]; + fprintf(stderr, " %-11s %8d %s\n", + prio_names[t->name->prio], + t->depth, t->name->path); + } + fprintf(stderr, _("traversed %lu commits\n"), seen_commits); + if (gave_up_on) { + fprintf(stderr, + _("more than %i tags found; listed %i most recent\n" + "gave up search at %s\n"), + max_candidates, max_candidates, + sha1_to_hex(gave_up_on->object.sha1)); + } + } + + display_name(all_matches[0].name); + if (abbrev) + show_suffix(all_matches[0].depth, cmit->object.sha1); + if (dirty) + printf("%s", dirty); + printf("\n"); + + if (!last_one) + clear_commit_marks(cmit, -1); +} + +int cmd_describe(int argc, const char **argv, const char *prefix) +{ + int contains = 0; + struct option options[] = { + OPT_BOOLEAN(0, "contains", &contains, N_("find the tag that comes after the commit")), + OPT_BOOLEAN(0, "debug", &debug, N_("debug search strategy on stderr")), + OPT_BOOLEAN(0, "all", &all, N_("use any ref in .git/refs")), + OPT_BOOLEAN(0, "tags", &tags, N_("use any tag in .git/refs/tags")), + OPT_BOOLEAN(0, "long", &longformat, N_("always use long format")), + OPT__ABBREV(&abbrev), + OPT_SET_INT(0, "exact-match", &max_candidates, + N_("only output exact matches"), 0), + OPT_INTEGER(0, "candidates", &max_candidates, + N_("consider <n> most recent tags (default: 10)")), + OPT_STRING(0, "match", &pattern, N_("pattern"), + N_("only consider tags matching <pattern>")), + OPT_BOOLEAN(0, "always", &always, + N_("show abbreviated commit object as fallback")), + {OPTION_STRING, 0, "dirty", &dirty, N_("mark"), + N_("append <mark> on dirty working tree (default: \"-dirty\")"), + PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"}, + OPT_END(), + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, describe_usage, 0); + if (abbrev < 0) + abbrev = DEFAULT_ABBREV; + + if (max_candidates < 0) + max_candidates = 0; + else if (max_candidates > MAX_TAGS) + max_candidates = MAX_TAGS; + + save_commit_buffer = 0; + + if (longformat && abbrev == 0) + die(_("--long is incompatible with --abbrev=0")); + + if (contains) { + const char **args = xmalloc((7 + argc) * sizeof(char *)); + int i = 0; + args[i++] = "name-rev"; + args[i++] = "--name-only"; + args[i++] = "--no-undefined"; + if (always) + args[i++] = "--always"; + if (!all) { + args[i++] = "--tags"; + if (pattern) { + char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1); + sprintf(s, "--refs=refs/tags/%s", pattern); + args[i++] = s; + } + } + memcpy(args + i, argv, argc * sizeof(char *)); + args[i + argc] = NULL; + return cmd_name_rev(i + argc, args, prefix); + } + + init_hash(&names); + for_each_rawref(get_name, NULL); + if (!names.nr && !always) + die(_("No names found, cannot describe anything.")); + + if (argc == 0) { + if (dirty) { + static struct lock_file index_lock; + int fd; + + read_cache_preload(NULL); + refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, + NULL, NULL, NULL); + fd = hold_locked_index(&index_lock, 0); + if (0 <= fd) + update_index_if_able(&the_index, &index_lock); + + if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, + diff_index_args, prefix)) + dirty = NULL; + } + describe("HEAD", 1); + } else if (dirty) { + die(_("--dirty is incompatible with committishes")); + } else { + while (argc-- > 0) { + describe(*argv++, argc == 0); + } + } + return 0; +} diff --git a/builtin/diff-files.c b/builtin/diff-files.c new file mode 100644 index 0000000000..46085f862f --- /dev/null +++ b/builtin/diff-files.c @@ -0,0 +1,70 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "diff.h" +#include "commit.h" +#include "revision.h" +#include "builtin.h" +#include "submodule.h" + +static const char diff_files_usage[] = +"git diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]" +COMMON_DIFF_OPTIONS_HELP; + +int cmd_diff_files(int argc, const char **argv, const char *prefix) +{ + struct rev_info rev; + int result; + unsigned options = 0; + + init_revisions(&rev, prefix); + gitmodules_config(); + git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ + rev.abbrev = 0; + + argc = setup_revisions(argc, argv, &rev, NULL); + while (1 < argc && argv[1][0] == '-') { + if (!strcmp(argv[1], "--base")) + rev.max_count = 1; + else if (!strcmp(argv[1], "--ours")) + rev.max_count = 2; + else if (!strcmp(argv[1], "--theirs")) + rev.max_count = 3; + else if (!strcmp(argv[1], "-q")) + options |= DIFF_SILENT_ON_REMOVED; + else + usage(diff_files_usage); + argv++; argc--; + } + if (!rev.diffopt.output_format) + rev.diffopt.output_format = DIFF_FORMAT_RAW; + + /* + * Make sure there are NO revision (i.e. pending object) parameter, + * rev.max_count is reasonable (0 <= n <= 3), and + * there is no other revision filtering parameters. + */ + if (rev.pending.nr || + rev.min_age != -1 || rev.max_age != -1 || + 3 < rev.max_count) + usage(diff_files_usage); + + /* + * "diff-files --base -p" should not combine merges because it + * was not asked to. "diff-files -c -p" should not densify + * (the user should ask with "diff-files --cc" explicitly). + */ + if (rev.max_count == -1 && !rev.combine_merges && + (rev.diffopt.output_format & DIFF_FORMAT_PATCH)) + rev.combine_merges = rev.dense_combined_merges = 1; + + if (read_cache_preload(rev.diffopt.pathspec.raw) < 0) { + perror("read_cache_preload"); + return -1; + } + result = run_diff_files(&rev, options); + return diff_result_code(&rev.diffopt, result); +} diff --git a/builtin/diff-index.c b/builtin/diff-index.c new file mode 100644 index 0000000000..1c737f7921 --- /dev/null +++ b/builtin/diff-index.c @@ -0,0 +1,56 @@ +#include "cache.h" +#include "diff.h" +#include "commit.h" +#include "revision.h" +#include "builtin.h" +#include "submodule.h" + +static const char diff_cache_usage[] = +"git diff-index [-m] [--cached] " +"[<common diff options>] <tree-ish> [<path>...]" +COMMON_DIFF_OPTIONS_HELP; + +int cmd_diff_index(int argc, const char **argv, const char *prefix) +{ + struct rev_info rev; + int cached = 0; + int i; + int result; + + init_revisions(&rev, prefix); + gitmodules_config(); + git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ + rev.abbrev = 0; + + argc = setup_revisions(argc, argv, &rev, NULL); + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + + if (!strcmp(arg, "--cached")) + cached = 1; + else + usage(diff_cache_usage); + } + if (!rev.diffopt.output_format) + rev.diffopt.output_format = DIFF_FORMAT_RAW; + + /* + * Make sure there is one revision (i.e. pending object), + * and there is no revision filtering parameters. + */ + if (rev.pending.nr != 1 || + rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1) + usage(diff_cache_usage); + if (!cached) { + setup_work_tree(); + if (read_cache_preload(rev.diffopt.pathspec.raw) < 0) { + perror("read_cache_preload"); + return -1; + } + } else if (read_cache() < 0) { + perror("read_cache"); + return -1; + } + result = run_diff_index(&rev, cached); + return diff_result_code(&rev.diffopt, result); +} diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c new file mode 100644 index 0000000000..be6417d166 --- /dev/null +++ b/builtin/diff-tree.c @@ -0,0 +1,192 @@ +#include "cache.h" +#include "diff.h" +#include "commit.h" +#include "log-tree.h" +#include "builtin.h" +#include "submodule.h" + +static struct rev_info log_tree_opt; + +static int diff_tree_commit_sha1(const unsigned char *sha1) +{ + struct commit *commit = lookup_commit_reference(sha1); + if (!commit) + return -1; + return log_tree_commit(&log_tree_opt, commit); +} + +/* Diff one or more commits. */ +static int stdin_diff_commit(struct commit *commit, char *line, int len) +{ + unsigned char sha1[20]; + if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) { + /* Graft the fake parents locally to the commit */ + int pos = 41; + struct commit_list **pptr, *parents; + + /* Free the real parent list */ + for (parents = commit->parents; parents; ) { + struct commit_list *tmp = parents->next; + free(parents); + parents = tmp; + } + commit->parents = NULL; + pptr = &(commit->parents); + while (line[pos] && !get_sha1_hex(line + pos, sha1)) { + struct commit *parent = lookup_commit(sha1); + if (parent) { + pptr = &commit_list_insert(parent, pptr)->next; + } + pos += 41; + } + } + return log_tree_commit(&log_tree_opt, commit); +} + +/* Diff two trees. */ +static int stdin_diff_trees(struct tree *tree1, char *line, int len) +{ + unsigned char sha1[20]; + struct tree *tree2; + if (len != 82 || !isspace(line[40]) || get_sha1_hex(line + 41, sha1)) + return error("Need exactly two trees, separated by a space"); + tree2 = lookup_tree(sha1); + if (!tree2 || parse_tree(tree2)) + return -1; + printf("%s %s\n", sha1_to_hex(tree1->object.sha1), + sha1_to_hex(tree2->object.sha1)); + diff_tree_sha1(tree1->object.sha1, tree2->object.sha1, + "", &log_tree_opt.diffopt); + log_tree_diff_flush(&log_tree_opt); + return 0; +} + +static int diff_tree_stdin(char *line) +{ + int len = strlen(line); + unsigned char sha1[20]; + struct object *obj; + + if (!len || line[len-1] != '\n') + return -1; + line[len-1] = 0; + if (get_sha1_hex(line, sha1)) + return -1; + obj = lookup_unknown_object(sha1); + if (!obj || !obj->parsed) + obj = parse_object(sha1); + if (!obj) + return -1; + if (obj->type == OBJ_COMMIT) + return stdin_diff_commit((struct commit *)obj, line, len); + if (obj->type == OBJ_TREE) + return stdin_diff_trees((struct tree *)obj, line, len); + error("Object %s is a %s, not a commit or tree", + sha1_to_hex(sha1), typename(obj->type)); + return -1; +} + +static const char diff_tree_usage[] = +"git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] " +"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n" +" -r diff recursively\n" +" --root include the initial commit as diff against /dev/null\n" +COMMON_DIFF_OPTIONS_HELP; + +static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt) +{ + if (!rev->diffopt.output_format) { + if (rev->dense_combined_merges) + rev->diffopt.output_format = DIFF_FORMAT_PATCH; + else + rev->diffopt.output_format = DIFF_FORMAT_RAW; + } +} + +int cmd_diff_tree(int argc, const char **argv, const char *prefix) +{ + int nr_sha1; + char line[1000]; + struct object *tree1, *tree2; + static struct rev_info *opt = &log_tree_opt; + struct setup_revision_opt s_r_opt; + int read_stdin = 0; + + init_revisions(opt, prefix); + gitmodules_config(); + git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ + opt->abbrev = 0; + opt->diff = 1; + opt->disable_stdin = 1; + memset(&s_r_opt, 0, sizeof(s_r_opt)); + s_r_opt.tweak = diff_tree_tweak_rev; + argc = setup_revisions(argc, argv, opt, &s_r_opt); + + while (--argc > 0) { + const char *arg = *++argv; + + if (!strcmp(arg, "--stdin")) { + read_stdin = 1; + continue; + } + usage(diff_tree_usage); + } + + /* + * NOTE! We expect "a ^b" to be equal to "a..b", so we + * reverse the order of the objects if the second one + * is marked UNINTERESTING. + */ + nr_sha1 = opt->pending.nr; + switch (nr_sha1) { + case 0: + if (!read_stdin) + usage(diff_tree_usage); + break; + case 1: + tree1 = opt->pending.objects[0].item; + diff_tree_commit_sha1(tree1->sha1); + break; + case 2: + tree1 = opt->pending.objects[0].item; + tree2 = opt->pending.objects[1].item; + if (tree2->flags & UNINTERESTING) { + struct object *tmp = tree2; + tree2 = tree1; + tree1 = tmp; + } + diff_tree_sha1(tree1->sha1, + tree2->sha1, + "", &opt->diffopt); + log_tree_diff_flush(opt); + break; + } + + if (read_stdin) { + int saved_nrl = 0; + int saved_dcctc = 0; + + if (opt->diffopt.detect_rename) + opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE | + DIFF_SETUP_USE_CACHE); + while (fgets(line, sizeof(line), stdin)) { + unsigned char sha1[20]; + + if (get_sha1_hex(line, sha1)) { + fputs(line, stdout); + fflush(stdout); + } + else { + diff_tree_stdin(line); + if (saved_nrl < opt->diffopt.needed_rename_limit) + saved_nrl = opt->diffopt.needed_rename_limit; + if (opt->diffopt.degraded_cc_to_c) + saved_dcctc = 1; + } + } + opt->diffopt.degraded_cc_to_c = saved_dcctc; + opt->diffopt.needed_rename_limit = saved_nrl; + } + + return diff_result_code(&opt->diffopt, 0); +} diff --git a/builtin/diff.c b/builtin/diff.c new file mode 100644 index 0000000000..8c2af6cb43 --- /dev/null +++ b/builtin/diff.c @@ -0,0 +1,424 @@ +/* + * Builtin "git diff" + * + * Copyright (c) 2006 Junio C Hamano + */ +#include "cache.h" +#include "color.h" +#include "commit.h" +#include "blob.h" +#include "tag.h" +#include "diff.h" +#include "diffcore.h" +#include "revision.h" +#include "log-tree.h" +#include "builtin.h" +#include "submodule.h" +#include "sha1-array.h" + +struct blobinfo { + unsigned char sha1[20]; + const char *name; + unsigned mode; +}; + +static const char builtin_diff_usage[] = +"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]"; + +static void stuff_change(struct diff_options *opt, + unsigned old_mode, unsigned new_mode, + const unsigned char *old_sha1, + const unsigned char *new_sha1, + int old_sha1_valid, + int new_sha1_valid, + const char *old_name, + const char *new_name) +{ + struct diff_filespec *one, *two; + + if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) && + !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode)) + return; + + if (DIFF_OPT_TST(opt, REVERSE_DIFF)) { + unsigned tmp; + const unsigned char *tmp_u; + const char *tmp_c; + tmp = old_mode; old_mode = new_mode; new_mode = tmp; + tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u; + tmp_c = old_name; old_name = new_name; new_name = tmp_c; + } + + if (opt->prefix && + (strncmp(old_name, opt->prefix, opt->prefix_length) || + strncmp(new_name, opt->prefix, opt->prefix_length))) + return; + + one = alloc_filespec(old_name); + two = alloc_filespec(new_name); + fill_filespec(one, old_sha1, old_sha1_valid, old_mode); + fill_filespec(two, new_sha1, new_sha1_valid, new_mode); + + diff_queue(&diff_queued_diff, one, two); +} + +static int builtin_diff_b_f(struct rev_info *revs, + int argc, const char **argv, + struct blobinfo *blob, + const char *path) +{ + /* Blob vs file in the working tree*/ + struct stat st; + + if (argc > 1) + usage(builtin_diff_usage); + + if (lstat(path, &st)) + die_errno(_("failed to stat '%s'"), path); + if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) + die(_("'%s': not a regular file or symlink"), path); + + diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/"); + + if (blob[0].mode == S_IFINVALID) + blob[0].mode = canon_mode(st.st_mode); + + stuff_change(&revs->diffopt, + blob[0].mode, canon_mode(st.st_mode), + blob[0].sha1, null_sha1, + 1, 0, + path, path); + diffcore_std(&revs->diffopt); + diff_flush(&revs->diffopt); + return 0; +} + +static int builtin_diff_blobs(struct rev_info *revs, + int argc, const char **argv, + struct blobinfo *blob) +{ + unsigned mode = canon_mode(S_IFREG | 0644); + + if (argc > 1) + usage(builtin_diff_usage); + + if (blob[0].mode == S_IFINVALID) + blob[0].mode = mode; + + if (blob[1].mode == S_IFINVALID) + blob[1].mode = mode; + + stuff_change(&revs->diffopt, + blob[0].mode, blob[1].mode, + blob[0].sha1, blob[1].sha1, + 1, 1, + blob[0].name, blob[1].name); + diffcore_std(&revs->diffopt); + diff_flush(&revs->diffopt); + return 0; +} + +static int builtin_diff_index(struct rev_info *revs, + int argc, const char **argv) +{ + int cached = 0; + while (1 < argc) { + const char *arg = argv[1]; + if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged")) + cached = 1; + else + usage(builtin_diff_usage); + argv++; argc--; + } + /* + * Make sure there is one revision (i.e. pending object), + * and there is no revision filtering parameters. + */ + if (revs->pending.nr != 1 || + revs->max_count != -1 || revs->min_age != -1 || + revs->max_age != -1) + usage(builtin_diff_usage); + if (!cached) { + setup_work_tree(); + if (read_cache_preload(revs->diffopt.pathspec.raw) < 0) { + perror("read_cache_preload"); + return -1; + } + } else if (read_cache() < 0) { + perror("read_cache"); + return -1; + } + return run_diff_index(revs, cached); +} + +static int builtin_diff_tree(struct rev_info *revs, + int argc, const char **argv, + struct object_array_entry *ent) +{ + const unsigned char *(sha1[2]); + int swap = 0; + + if (argc > 1) + usage(builtin_diff_usage); + + /* We saw two trees, ent[0] and ent[1]. + * if ent[1] is uninteresting, they are swapped + */ + if (ent[1].item->flags & UNINTERESTING) + swap = 1; + sha1[swap] = ent[0].item->sha1; + sha1[1-swap] = ent[1].item->sha1; + diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt); + log_tree_diff_flush(revs); + return 0; +} + +static int builtin_diff_combined(struct rev_info *revs, + int argc, const char **argv, + struct object_array_entry *ent, + int ents) +{ + struct sha1_array parents = SHA1_ARRAY_INIT; + int i; + + if (argc > 1) + usage(builtin_diff_usage); + + if (!revs->dense_combined_merges && !revs->combine_merges) + revs->dense_combined_merges = revs->combine_merges = 1; + for (i = 1; i < ents; i++) + sha1_array_append(&parents, ent[i].item->sha1); + diff_tree_combined(ent[0].item->sha1, &parents, + revs->dense_combined_merges, revs); + sha1_array_clear(&parents); + return 0; +} + +static void refresh_index_quietly(void) +{ + struct lock_file *lock_file; + int fd; + + lock_file = xcalloc(1, sizeof(struct lock_file)); + fd = hold_locked_index(lock_file, 0); + if (fd < 0) + return; + discard_cache(); + read_cache(); + refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED); + update_index_if_able(&the_index, lock_file); +} + +static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv) +{ + unsigned int options = 0; + + while (1 < argc && argv[1][0] == '-') { + if (!strcmp(argv[1], "--base")) + revs->max_count = 1; + else if (!strcmp(argv[1], "--ours")) + revs->max_count = 2; + else if (!strcmp(argv[1], "--theirs")) + revs->max_count = 3; + else if (!strcmp(argv[1], "-q")) + options |= DIFF_SILENT_ON_REMOVED; + else if (!strcmp(argv[1], "-h")) + usage(builtin_diff_usage); + else + return error(_("invalid option: %s"), argv[1]); + argv++; argc--; + } + + /* + * "diff --base" should not combine merges because it was not + * asked to. "diff -c" should not densify (if the user wants + * dense one, --cc can be explicitly asked for, or just rely + * on the default). + */ + if (revs->max_count == -1 && !revs->combine_merges && + (revs->diffopt.output_format & DIFF_FORMAT_PATCH)) + revs->combine_merges = revs->dense_combined_merges = 1; + + setup_work_tree(); + if (read_cache_preload(revs->diffopt.pathspec.raw) < 0) { + perror("read_cache_preload"); + return -1; + } + return run_diff_files(revs, options); +} + +int cmd_diff(int argc, const char **argv, const char *prefix) +{ + int i; + struct rev_info rev; + struct object_array_entry ent[100]; + int ents = 0, blobs = 0, paths = 0; + const char *path = NULL; + struct blobinfo blob[2]; + int nongit; + int result = 0; + + /* + * We could get N tree-ish in the rev.pending_objects list. + * Also there could be M blobs there, and P pathspecs. + * + * N=0, M=0: + * cache vs files (diff-files) + * N=0, M=2: + * compare two random blobs. P must be zero. + * N=0, M=1, P=1: + * compare a blob with a working tree file. + * + * N=1, M=0: + * tree vs cache (diff-index --cached) + * + * N=2, M=0: + * tree vs tree (diff-tree) + * + * N=0, M=0, P=2: + * compare two filesystem entities (aka --no-index). + * + * Other cases are errors. + */ + + prefix = setup_git_directory_gently(&nongit); + gitmodules_config(); + git_config(git_diff_ui_config, NULL); + + init_revisions(&rev, prefix); + + /* If this is a no-index diff, just run it and exit there. */ + diff_no_index(&rev, argc, argv, nongit, prefix); + + /* Otherwise, we are doing the usual "git" diff */ + rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index; + + /* Scale to real terminal size and respect statGraphWidth config */ + rev.diffopt.stat_width = -1; + rev.diffopt.stat_graph_width = -1; + + /* Default to let external and textconv be used */ + DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL); + DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV); + + if (nongit) + die(_("Not a git repository")); + argc = setup_revisions(argc, argv, &rev, NULL); + if (!rev.diffopt.output_format) { + rev.diffopt.output_format = DIFF_FORMAT_PATCH; + diff_setup_done(&rev.diffopt); + } + + DIFF_OPT_SET(&rev.diffopt, RECURSIVE); + + setup_diff_pager(&rev.diffopt); + + /* + * Do we have --cached and not have a pending object, then + * default to HEAD by hand. Eek. + */ + if (!rev.pending.nr) { + int i; + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (!strcmp(arg, "--")) + break; + else if (!strcmp(arg, "--cached") || + !strcmp(arg, "--staged")) { + add_head_to_pending(&rev); + if (!rev.pending.nr) { + struct tree *tree; + tree = lookup_tree(EMPTY_TREE_SHA1_BIN); + add_pending_object(&rev, &tree->object, "HEAD"); + } + break; + } + } + } + + for (i = 0; i < rev.pending.nr; i++) { + struct object_array_entry *list = rev.pending.objects+i; + struct object *obj = list->item; + const char *name = list->name; + int flags = (obj->flags & UNINTERESTING); + if (!obj->parsed) + obj = parse_object(obj->sha1); + obj = deref_tag(obj, NULL, 0); + if (!obj) + die(_("invalid object '%s' given."), name); + if (obj->type == OBJ_COMMIT) + obj = &((struct commit *)obj)->tree->object; + if (obj->type == OBJ_TREE) { + if (ARRAY_SIZE(ent) <= ents) + die(_("more than %d trees given: '%s'"), + (int) ARRAY_SIZE(ent), name); + obj->flags |= flags; + ent[ents].item = obj; + ent[ents].name = name; + ents++; + continue; + } + if (obj->type == OBJ_BLOB) { + if (2 <= blobs) + die(_("more than two blobs given: '%s'"), name); + hashcpy(blob[blobs].sha1, obj->sha1); + blob[blobs].name = name; + blob[blobs].mode = list->mode; + blobs++; + continue; + + } + die(_("unhandled object '%s' given."), name); + } + if (rev.prune_data.nr) { + if (!path) + path = rev.prune_data.items[0].match; + paths += rev.prune_data.nr; + } + + /* + * Now, do the arguments look reasonable? + */ + if (!ents) { + switch (blobs) { + case 0: + result = builtin_diff_files(&rev, argc, argv); + break; + case 1: + if (paths != 1) + usage(builtin_diff_usage); + result = builtin_diff_b_f(&rev, argc, argv, blob, path); + break; + case 2: + if (paths) + usage(builtin_diff_usage); + result = builtin_diff_blobs(&rev, argc, argv, blob); + break; + default: + usage(builtin_diff_usage); + } + } + else if (blobs) + usage(builtin_diff_usage); + else if (ents == 1) + result = builtin_diff_index(&rev, argc, argv); + else if (ents == 2) + result = builtin_diff_tree(&rev, argc, argv, ent); + else if (ent[0].item->flags & UNINTERESTING) { + /* + * diff A...B where there is at least one merge base + * between A and B. We have ent[0] == merge-base, + * ent[ents-2] == A, and ent[ents-1] == B. Show diff + * between the base and B. Note that we pick one + * merge base at random if there are more than one. + */ + ent[1] = ent[ents-1]; + result = builtin_diff_tree(&rev, argc, argv, ent); + } else + result = builtin_diff_combined(&rev, argc, argv, + ent, ents); + result = diff_result_code(&rev.diffopt, result); + if (1 < rev.diffopt.skip_stat_unmatch) + refresh_index_quietly(); + return result; +} diff --git a/builtin/fast-export.c b/builtin/fast-export.c new file mode 100644 index 0000000000..77dffd1ce3 --- /dev/null +++ b/builtin/fast-export.c @@ -0,0 +1,716 @@ +/* + * "git fast-export" builtin command + * + * Copyright (C) 2007 Johannes E. Schindelin + */ +#include "builtin.h" +#include "cache.h" +#include "commit.h" +#include "object.h" +#include "tag.h" +#include "diff.h" +#include "diffcore.h" +#include "log-tree.h" +#include "revision.h" +#include "decorate.h" +#include "string-list.h" +#include "utf8.h" +#include "parse-options.h" +#include "quote.h" + +static const char *fast_export_usage[] = { + N_("git fast-export [rev-list-opts]"), + NULL +}; + +static int progress; +static enum { ABORT, VERBATIM, WARN, STRIP } signed_tag_mode = ABORT; +static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR; +static int fake_missing_tagger; +static int use_done_feature; +static int no_data; +static int full_tree; + +static int parse_opt_signed_tag_mode(const struct option *opt, + const char *arg, int unset) +{ + if (unset || !strcmp(arg, "abort")) + signed_tag_mode = ABORT; + else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore")) + signed_tag_mode = VERBATIM; + else if (!strcmp(arg, "warn")) + signed_tag_mode = WARN; + else if (!strcmp(arg, "strip")) + signed_tag_mode = STRIP; + else + return error("Unknown signed-tag mode: %s", arg); + return 0; +} + +static int parse_opt_tag_of_filtered_mode(const struct option *opt, + const char *arg, int unset) +{ + if (unset || !strcmp(arg, "abort")) + tag_of_filtered_mode = ERROR; + else if (!strcmp(arg, "drop")) + tag_of_filtered_mode = DROP; + else if (!strcmp(arg, "rewrite")) + tag_of_filtered_mode = REWRITE; + else + return error("Unknown tag-of-filtered mode: %s", arg); + return 0; +} + +static struct decoration idnums; +static uint32_t last_idnum; + +static int has_unshown_parent(struct commit *commit) +{ + struct commit_list *parent; + + for (parent = commit->parents; parent; parent = parent->next) + if (!(parent->item->object.flags & SHOWN) && + !(parent->item->object.flags & UNINTERESTING)) + return 1; + return 0; +} + +/* Since intptr_t is C99, we do not use it here */ +static inline uint32_t *mark_to_ptr(uint32_t mark) +{ + return ((uint32_t *)NULL) + mark; +} + +static inline uint32_t ptr_to_mark(void * mark) +{ + return (uint32_t *)mark - (uint32_t *)NULL; +} + +static inline void mark_object(struct object *object, uint32_t mark) +{ + add_decoration(&idnums, object, mark_to_ptr(mark)); +} + +static inline void mark_next_object(struct object *object) +{ + mark_object(object, ++last_idnum); +} + +static int get_object_mark(struct object *object) +{ + void *decoration = lookup_decoration(&idnums, object); + if (!decoration) + return 0; + return ptr_to_mark(decoration); +} + +static void show_progress(void) +{ + static int counter = 0; + if (!progress) + return; + if ((++counter % progress) == 0) + printf("progress %d objects\n", counter); +} + +static void handle_object(const unsigned char *sha1) +{ + unsigned long size; + enum object_type type; + char *buf; + struct object *object; + + if (no_data) + return; + + if (is_null_sha1(sha1)) + return; + + object = parse_object(sha1); + if (!object) + die ("Could not read blob %s", sha1_to_hex(sha1)); + + if (object->flags & SHOWN) + return; + + buf = read_sha1_file(sha1, &type, &size); + if (!buf) + die ("Could not read blob %s", sha1_to_hex(sha1)); + + mark_next_object(object); + + printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size); + if (size && fwrite(buf, size, 1, stdout) != 1) + die_errno ("Could not write blob '%s'", sha1_to_hex(sha1)); + printf("\n"); + + show_progress(); + + object->flags |= SHOWN; + free(buf); +} + +static int depth_first(const void *a_, const void *b_) +{ + const struct diff_filepair *a = *((const struct diff_filepair **)a_); + const struct diff_filepair *b = *((const struct diff_filepair **)b_); + const char *name_a, *name_b; + int len_a, len_b, len; + int cmp; + + name_a = a->one ? a->one->path : a->two->path; + name_b = b->one ? b->one->path : b->two->path; + + len_a = strlen(name_a); + len_b = strlen(name_b); + len = (len_a < len_b) ? len_a : len_b; + + /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */ + cmp = memcmp(name_a, name_b, len); + if (cmp) + return cmp; + cmp = len_b - len_a; + if (cmp) + return cmp; + /* + * Move 'R'ename entries last so that all references of the file + * appear in the output before it is renamed (e.g., when a file + * was copied and renamed in the same commit). + */ + return (a->status == 'R') - (b->status == 'R'); +} + +static void print_path(const char *path) +{ + int need_quote = quote_c_style(path, NULL, NULL, 0); + if (need_quote) + quote_c_style(path, NULL, stdout, 0); + else if (strchr(path, ' ')) + printf("\"%s\"", path); + else + printf("%s", path); +} + +static void show_filemodify(struct diff_queue_struct *q, + struct diff_options *options, void *data) +{ + int i; + + /* + * Handle files below a directory first, in case they are all deleted + * and the directory changes to a file or symlink. + */ + qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first); + + for (i = 0; i < q->nr; i++) { + struct diff_filespec *ospec = q->queue[i]->one; + struct diff_filespec *spec = q->queue[i]->two; + + switch (q->queue[i]->status) { + case DIFF_STATUS_DELETED: + printf("D "); + print_path(spec->path); + putchar('\n'); + break; + + case DIFF_STATUS_COPIED: + case DIFF_STATUS_RENAMED: + printf("%c ", q->queue[i]->status); + print_path(ospec->path); + putchar(' '); + print_path(spec->path); + putchar('\n'); + + if (!hashcmp(ospec->sha1, spec->sha1) && + ospec->mode == spec->mode) + break; + /* fallthrough */ + + case DIFF_STATUS_TYPE_CHANGED: + case DIFF_STATUS_MODIFIED: + case DIFF_STATUS_ADDED: + /* + * Links refer to objects in another repositories; + * output the SHA-1 verbatim. + */ + if (no_data || S_ISGITLINK(spec->mode)) + printf("M %06o %s ", spec->mode, + sha1_to_hex(spec->sha1)); + else { + struct object *object = lookup_object(spec->sha1); + printf("M %06o :%d ", spec->mode, + get_object_mark(object)); + } + print_path(spec->path); + putchar('\n'); + break; + + default: + die("Unexpected comparison status '%c' for %s, %s", + q->queue[i]->status, + ospec->path ? ospec->path : "none", + spec->path ? spec->path : "none"); + } + } +} + +static const char *find_encoding(const char *begin, const char *end) +{ + const char *needle = "\nencoding "; + char *bol, *eol; + + bol = memmem(begin, end ? end - begin : strlen(begin), + needle, strlen(needle)); + if (!bol) + return git_commit_encoding; + bol += strlen(needle); + eol = strchrnul(bol, '\n'); + *eol = '\0'; + return bol; +} + +static void handle_commit(struct commit *commit, struct rev_info *rev) +{ + int saved_output_format = rev->diffopt.output_format; + const char *author, *author_end, *committer, *committer_end; + const char *encoding, *message; + char *reencoded = NULL; + struct commit_list *p; + int i; + + rev->diffopt.output_format = DIFF_FORMAT_CALLBACK; + + parse_commit(commit); + author = strstr(commit->buffer, "\nauthor "); + if (!author) + die ("Could not find author in commit %s", + sha1_to_hex(commit->object.sha1)); + author++; + author_end = strchrnul(author, '\n'); + committer = strstr(author_end, "\ncommitter "); + if (!committer) + die ("Could not find committer in commit %s", + sha1_to_hex(commit->object.sha1)); + committer++; + committer_end = strchrnul(committer, '\n'); + message = strstr(committer_end, "\n\n"); + encoding = find_encoding(committer_end, message); + if (message) + message += 2; + + if (commit->parents && + get_object_mark(&commit->parents->item->object) != 0 && + !full_tree) { + parse_commit(commit->parents->item); + diff_tree_sha1(commit->parents->item->tree->object.sha1, + commit->tree->object.sha1, "", &rev->diffopt); + } + else + diff_root_tree_sha1(commit->tree->object.sha1, + "", &rev->diffopt); + + /* Export the referenced blobs, and remember the marks. */ + for (i = 0; i < diff_queued_diff.nr; i++) + if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode)) + handle_object(diff_queued_diff.queue[i]->two->sha1); + + mark_next_object(&commit->object); + if (!is_encoding_utf8(encoding)) + reencoded = reencode_string(message, "UTF-8", encoding); + if (!commit->parents) + printf("reset %s\n", (const char*)commit->util); + printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s", + (const char *)commit->util, last_idnum, + (int)(author_end - author), author, + (int)(committer_end - committer), committer, + (unsigned)(reencoded + ? strlen(reencoded) : message + ? strlen(message) : 0), + reencoded ? reencoded : message ? message : ""); + free(reencoded); + + for (i = 0, p = commit->parents; p; p = p->next) { + int mark = get_object_mark(&p->item->object); + if (!mark) + continue; + if (i == 0) + printf("from :%d\n", mark); + else + printf("merge :%d\n", mark); + i++; + } + + if (full_tree) + printf("deleteall\n"); + log_tree_diff_flush(rev); + rev->diffopt.output_format = saved_output_format; + + printf("\n"); + + show_progress(); +} + +static void handle_tail(struct object_array *commits, struct rev_info *revs) +{ + struct commit *commit; + while (commits->nr) { + commit = (struct commit *)commits->objects[commits->nr - 1].item; + if (has_unshown_parent(commit)) + return; + handle_commit(commit, revs); + commits->nr--; + } +} + +static void handle_tag(const char *name, struct tag *tag) +{ + unsigned long size; + enum object_type type; + char *buf; + const char *tagger, *tagger_end, *message; + size_t message_size = 0; + struct object *tagged; + int tagged_mark; + struct commit *p; + + /* Trees have no identifer in fast-export output, thus we have no way + * to output tags of trees, tags of tags of trees, etc. Simply omit + * such tags. + */ + tagged = tag->tagged; + while (tagged->type == OBJ_TAG) { + tagged = ((struct tag *)tagged)->tagged; + } + if (tagged->type == OBJ_TREE) { + warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.", + sha1_to_hex(tag->object.sha1)); + return; + } + + buf = read_sha1_file(tag->object.sha1, &type, &size); + if (!buf) + die ("Could not read tag %s", sha1_to_hex(tag->object.sha1)); + message = memmem(buf, size, "\n\n", 2); + if (message) { + message += 2; + message_size = strlen(message); + } + tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8); + if (!tagger) { + if (fake_missing_tagger) + tagger = "tagger Unspecified Tagger " + "<unspecified-tagger> 0 +0000"; + else + tagger = ""; + tagger_end = tagger + strlen(tagger); + } else { + tagger++; + tagger_end = strchrnul(tagger, '\n'); + } + + /* handle signed tags */ + if (message) { + const char *signature = strstr(message, + "\n-----BEGIN PGP SIGNATURE-----\n"); + if (signature) + switch(signed_tag_mode) { + case ABORT: + die ("Encountered signed tag %s; use " + "--signed-tag=<mode> to handle it.", + sha1_to_hex(tag->object.sha1)); + case WARN: + warning ("Exporting signed tag %s", + sha1_to_hex(tag->object.sha1)); + /* fallthru */ + case VERBATIM: + break; + case STRIP: + message_size = signature + 1 - message; + break; + } + } + + /* handle tag->tagged having been filtered out due to paths specified */ + tagged = tag->tagged; + tagged_mark = get_object_mark(tagged); + if (!tagged_mark) { + switch(tag_of_filtered_mode) { + case ABORT: + die ("Tag %s tags unexported object; use " + "--tag-of-filtered-object=<mode> to handle it.", + sha1_to_hex(tag->object.sha1)); + case DROP: + /* Ignore this tag altogether */ + return; + case REWRITE: + if (tagged->type != OBJ_COMMIT) { + die ("Tag %s tags unexported %s!", + sha1_to_hex(tag->object.sha1), + typename(tagged->type)); + } + p = (struct commit *)tagged; + for (;;) { + if (p->parents && p->parents->next) + break; + if (p->object.flags & UNINTERESTING) + break; + if (!(p->object.flags & TREESAME)) + break; + if (!p->parents) + die ("Can't find replacement commit for tag %s\n", + sha1_to_hex(tag->object.sha1)); + p = p->parents->item; + } + tagged_mark = get_object_mark(&p->object); + } + } + + if (!prefixcmp(name, "refs/tags/")) + name += 10; + printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n", + name, tagged_mark, + (int)(tagger_end - tagger), tagger, + tagger == tagger_end ? "" : "\n", + (int)message_size, (int)message_size, message ? message : ""); +} + +static void get_tags_and_duplicates(struct rev_cmdline_info *info, + struct string_list *extra_refs) +{ + struct tag *tag; + int i; + + for (i = 0; i < info->nr; i++) { + struct rev_cmdline_entry *e = info->rev + i; + unsigned char sha1[20]; + struct commit *commit; + char *full_name; + + if (e->flags & UNINTERESTING) + continue; + + if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1) + continue; + + switch (e->item->type) { + case OBJ_COMMIT: + commit = (struct commit *)e->item; + break; + case OBJ_TAG: + tag = (struct tag *)e->item; + + /* handle nested tags */ + while (tag && tag->object.type == OBJ_TAG) { + parse_object(tag->object.sha1); + string_list_append(extra_refs, full_name)->util = tag; + tag = (struct tag *)tag->tagged; + } + if (!tag) + die ("Tag %s points nowhere?", e->name); + switch(tag->object.type) { + case OBJ_COMMIT: + commit = (struct commit *)tag; + break; + case OBJ_BLOB: + handle_object(tag->object.sha1); + continue; + default: /* OBJ_TAG (nested tags) is already handled */ + warning("Tag points to object of unexpected type %s, skipping.", + typename(tag->object.type)); + continue; + } + break; + default: + warning("%s: Unexpected object of type %s, skipping.", + e->name, + typename(e->item->type)); + continue; + } + + /* + * This ref will not be updated through a commit, lets make + * sure it gets properly updated eventually. + */ + if (commit->util || commit->object.flags & SHOWN) + string_list_append(extra_refs, full_name)->util = commit; + if (!commit->util) + commit->util = full_name; + } +} + +static void handle_tags_and_duplicates(struct string_list *extra_refs) +{ + struct commit *commit; + int i; + + for (i = extra_refs->nr - 1; i >= 0; i--) { + const char *name = extra_refs->items[i].string; + struct object *object = extra_refs->items[i].util; + switch (object->type) { + case OBJ_TAG: + handle_tag(name, (struct tag *)object); + break; + case OBJ_COMMIT: + /* create refs pointing to already seen commits */ + commit = (struct commit *)object; + printf("reset %s\nfrom :%d\n\n", name, + get_object_mark(&commit->object)); + show_progress(); + break; + } + } +} + +static void export_marks(char *file) +{ + unsigned int i; + uint32_t mark; + struct object_decoration *deco = idnums.hash; + FILE *f; + int e = 0; + + f = fopen(file, "w"); + if (!f) + die_errno("Unable to open marks file %s for writing.", file); + + for (i = 0; i < idnums.size; i++) { + if (deco->base && deco->base->type == 1) { + mark = ptr_to_mark(deco->decoration); + if (fprintf(f, ":%"PRIu32" %s\n", mark, + sha1_to_hex(deco->base->sha1)) < 0) { + e = 1; + break; + } + } + deco++; + } + + e |= ferror(f); + e |= fclose(f); + if (e) + error("Unable to write marks file %s.", file); +} + +static void import_marks(char *input_file) +{ + char line[512]; + FILE *f = fopen(input_file, "r"); + if (!f) + die_errno("cannot read '%s'", input_file); + + while (fgets(line, sizeof(line), f)) { + uint32_t mark; + char *line_end, *mark_end; + unsigned char sha1[20]; + struct object *object; + + line_end = strchr(line, '\n'); + if (line[0] != ':' || !line_end) + die("corrupt mark line: %s", line); + *line_end = '\0'; + + mark = strtoumax(line + 1, &mark_end, 10); + if (!mark || mark_end == line + 1 + || *mark_end != ' ' || get_sha1(mark_end + 1, sha1)) + die("corrupt mark line: %s", line); + + object = parse_object(sha1); + if (!object) + die ("Could not read blob %s", sha1_to_hex(sha1)); + + if (object->flags & SHOWN) + error("Object %s already has a mark", sha1_to_hex(sha1)); + + if (object->type != OBJ_COMMIT) + /* only commits */ + continue; + + mark_object(object, mark); + if (last_idnum < mark) + last_idnum = mark; + + object->flags |= SHOWN; + } + fclose(f); +} + +int cmd_fast_export(int argc, const char **argv, const char *prefix) +{ + struct rev_info revs; + struct object_array commits = OBJECT_ARRAY_INIT; + struct string_list extra_refs = STRING_LIST_INIT_NODUP; + struct commit *commit; + char *export_filename = NULL, *import_filename = NULL; + struct option options[] = { + OPT_INTEGER(0, "progress", &progress, + N_("show progress after <n> objects")), + OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"), + N_("select handling of signed tags"), + parse_opt_signed_tag_mode), + OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"), + N_("select handling of tags that tag filtered objects"), + parse_opt_tag_of_filtered_mode), + OPT_STRING(0, "export-marks", &export_filename, N_("file"), + N_("Dump marks to this file")), + OPT_STRING(0, "import-marks", &import_filename, N_("file"), + N_("Import marks from this file")), + OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger, + N_("Fake a tagger when tags lack one")), + OPT_BOOLEAN(0, "full-tree", &full_tree, + N_("Output full tree for each commit")), + OPT_BOOLEAN(0, "use-done-feature", &use_done_feature, + N_("Use the done feature to terminate the stream")), + OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")), + OPT_END() + }; + + if (argc == 1) + usage_with_options (fast_export_usage, options); + + /* we handle encodings */ + git_config(git_default_config, NULL); + + init_revisions(&revs, prefix); + revs.topo_order = 1; + revs.show_source = 1; + revs.rewrite_parents = 1; + argc = setup_revisions(argc, argv, &revs, NULL); + argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0); + if (argc > 1) + usage_with_options (fast_export_usage, options); + + if (use_done_feature) + printf("feature done\n"); + + if (import_filename) + import_marks(import_filename); + + if (import_filename && revs.prune_data.nr) + full_tree = 1; + + get_tags_and_duplicates(&revs.cmdline, &extra_refs); + + if (prepare_revision_walk(&revs)) + die("revision walk setup failed"); + revs.diffopt.format_callback = show_filemodify; + DIFF_OPT_SET(&revs.diffopt, RECURSIVE); + while ((commit = get_revision(&revs))) { + if (has_unshown_parent(commit)) { + add_object_array(&commit->object, NULL, &commits); + } + else { + handle_commit(commit, &revs); + handle_tail(&commits, &revs); + } + } + + handle_tags_and_duplicates(&extra_refs); + + if (export_filename) + export_marks(export_filename); + + if (use_done_feature) + printf("done\n"); + + return 0; +} diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c new file mode 100644 index 0000000000..940ae35dc2 --- /dev/null +++ b/builtin/fetch-pack.c @@ -0,0 +1,161 @@ +#include "builtin.h" +#include "pkt-line.h" +#include "fetch-pack.h" + +static const char fetch_pack_usage[] = +"git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] " +"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] " +"[--no-progress] [-v] [<host>:]<directory> [<refs>...]"; + +int cmd_fetch_pack(int argc, const char **argv, const char *prefix) +{ + int i, ret; + struct ref *ref = NULL; + const char *dest = NULL; + struct string_list sought = STRING_LIST_INIT_DUP; + int fd[2]; + char *pack_lockfile = NULL; + char **pack_lockfile_ptr = NULL; + struct child_process *conn; + struct fetch_pack_args args; + + packet_trace_identity("fetch-pack"); + + memset(&args, 0, sizeof(args)); + args.uploadpack = "git-upload-pack"; + + for (i = 1; i < argc && *argv[i] == '-'; i++) { + const char *arg = argv[i]; + + if (!prefixcmp(arg, "--upload-pack=")) { + args.uploadpack = arg + 14; + continue; + } + if (!prefixcmp(arg, "--exec=")) { + args.uploadpack = arg + 7; + continue; + } + if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { + args.quiet = 1; + continue; + } + if (!strcmp("--keep", arg) || !strcmp("-k", arg)) { + args.lock_pack = args.keep_pack; + args.keep_pack = 1; + continue; + } + if (!strcmp("--thin", arg)) { + args.use_thin_pack = 1; + continue; + } + if (!strcmp("--include-tag", arg)) { + args.include_tag = 1; + continue; + } + if (!strcmp("--all", arg)) { + args.fetch_all = 1; + continue; + } + if (!strcmp("--stdin", arg)) { + args.stdin_refs = 1; + continue; + } + if (!strcmp("-v", arg)) { + args.verbose = 1; + continue; + } + if (!prefixcmp(arg, "--depth=")) { + args.depth = strtol(arg + 8, NULL, 0); + continue; + } + if (!strcmp("--no-progress", arg)) { + args.no_progress = 1; + continue; + } + if (!strcmp("--stateless-rpc", arg)) { + args.stateless_rpc = 1; + continue; + } + if (!strcmp("--lock-pack", arg)) { + args.lock_pack = 1; + pack_lockfile_ptr = &pack_lockfile; + continue; + } + usage(fetch_pack_usage); + } + + if (i < argc) + dest = argv[i++]; + else + usage(fetch_pack_usage); + + /* + * Copy refs from cmdline to growable list, then append any + * refs from the standard input: + */ + for (; i < argc; i++) + string_list_append(&sought, xstrdup(argv[i])); + if (args.stdin_refs) { + if (args.stateless_rpc) { + /* in stateless RPC mode we use pkt-line to read + * from stdin, until we get a flush packet + */ + static char line[1000]; + for (;;) { + int n = packet_read_line(0, line, sizeof(line)); + if (!n) + break; + if (line[n-1] == '\n') + n--; + string_list_append(&sought, xmemdupz(line, n)); + } + } + else { + /* read from stdin one ref per line, until EOF */ + struct strbuf line = STRBUF_INIT; + while (strbuf_getline(&line, stdin, '\n') != EOF) + string_list_append(&sought, strbuf_detach(&line, NULL)); + strbuf_release(&line); + } + } + + if (args.stateless_rpc) { + conn = NULL; + fd[0] = 0; + fd[1] = 1; + } else { + conn = git_connect(fd, dest, args.uploadpack, + args.verbose ? CONNECT_VERBOSE : 0); + } + + get_remote_heads(fd[0], &ref, 0, NULL); + + ref = fetch_pack(&args, fd, conn, ref, dest, + &sought, pack_lockfile_ptr); + if (pack_lockfile) { + printf("lock %s\n", pack_lockfile); + fflush(stdout); + } + close(fd[0]); + close(fd[1]); + if (finish_connect(conn)) + return 1; + + ret = !ref || sought.nr; + + /* + * If the heads to pull were given, we should have consumed + * all of them by matching the remote. Otherwise, 'git fetch + * remote no-such-ref' would silently succeed without issuing + * an error. + */ + for (i = 0; i < sought.nr; i++) + error("no such remote ref %s", sought.items[i].string); + while (ref) { + printf("%s %s\n", + sha1_to_hex(ref->old_sha1), ref->name); + ref = ref->next; + } + + return ret; +} diff --git a/builtin/fetch.c b/builtin/fetch.c new file mode 100644 index 0000000000..4b5a89839b --- /dev/null +++ b/builtin/fetch.c @@ -0,0 +1,1030 @@ +/* + * "git fetch" + */ +#include "cache.h" +#include "refs.h" +#include "commit.h" +#include "builtin.h" +#include "string-list.h" +#include "remote.h" +#include "transport.h" +#include "run-command.h" +#include "parse-options.h" +#include "sigchain.h" +#include "transport.h" +#include "submodule.h" +#include "connected.h" +#include "argv-array.h" + +static const char * const builtin_fetch_usage[] = { + N_("git fetch [<options>] [<repository> [<refspec>...]]"), + N_("git fetch [<options>] <group>"), + N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"), + N_("git fetch --all [<options>]"), + NULL +}; + +enum { + TAGS_UNSET = 0, + TAGS_DEFAULT = 1, + TAGS_SET = 2 +}; + +static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity; +static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT; +static int tags = TAGS_DEFAULT; +static const char *depth; +static const char *upload_pack; +static struct strbuf default_rla = STRBUF_INIT; +static struct transport *transport; +static const char *submodule_prefix = ""; +static const char *recurse_submodules_default; + +static int option_parse_recurse_submodules(const struct option *opt, + const char *arg, int unset) +{ + if (unset) { + recurse_submodules = RECURSE_SUBMODULES_OFF; + } else { + if (arg) + recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg); + else + recurse_submodules = RECURSE_SUBMODULES_ON; + } + return 0; +} + +static struct option builtin_fetch_options[] = { + OPT__VERBOSITY(&verbosity), + OPT_BOOLEAN(0, "all", &all, + N_("fetch from all remotes")), + OPT_BOOLEAN('a', "append", &append, + N_("append to .git/FETCH_HEAD instead of overwriting")), + OPT_STRING(0, "upload-pack", &upload_pack, N_("path"), + N_("path to upload pack on remote end")), + OPT__FORCE(&force, N_("force overwrite of local branch")), + OPT_BOOLEAN('m', "multiple", &multiple, + N_("fetch from multiple remotes")), + OPT_SET_INT('t', "tags", &tags, + N_("fetch all tags and associated objects"), TAGS_SET), + OPT_SET_INT('n', NULL, &tags, + N_("do not fetch all tags (--no-tags)"), TAGS_UNSET), + OPT_BOOLEAN('p', "prune", &prune, + N_("prune remote-tracking branches no longer on remote")), + { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"), + N_("control recursive fetching of submodules"), + PARSE_OPT_OPTARG, option_parse_recurse_submodules }, + OPT_BOOLEAN(0, "dry-run", &dry_run, + N_("dry run")), + OPT_BOOLEAN('k', "keep", &keep, N_("keep downloaded pack")), + OPT_BOOLEAN('u', "update-head-ok", &update_head_ok, + N_("allow updating of HEAD ref")), + OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), + OPT_STRING(0, "depth", &depth, N_("depth"), + N_("deepen history of shallow clone")), + { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"), + N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN }, + { OPTION_STRING, 0, "recurse-submodules-default", + &recurse_submodules_default, NULL, + N_("default mode for recursion"), PARSE_OPT_HIDDEN }, + OPT_END() +}; + +static void unlock_pack(void) +{ + if (transport) + transport_unlock_pack(transport); +} + +static void unlock_pack_on_signal(int signo) +{ + unlock_pack(); + sigchain_pop(signo); + raise(signo); +} + +static void add_merge_config(struct ref **head, + const struct ref *remote_refs, + struct branch *branch, + struct ref ***tail) +{ + int i; + + for (i = 0; i < branch->merge_nr; i++) { + struct ref *rm, **old_tail = *tail; + struct refspec refspec; + + for (rm = *head; rm; rm = rm->next) { + if (branch_merge_matches(branch, i, rm->name)) { + rm->merge = 1; + break; + } + } + if (rm) + continue; + + /* + * Not fetched to a remote-tracking branch? We need to fetch + * it anyway to allow this branch's "branch.$name.merge" + * to be honored by 'git pull', but we do not have to + * fail if branch.$name.merge is misconfigured to point + * at a nonexisting branch. If we were indeed called by + * 'git pull', it will notice the misconfiguration because + * there is no entry in the resulting FETCH_HEAD marked + * for merging. + */ + memset(&refspec, 0, sizeof(refspec)); + refspec.src = branch->merge[i]->src; + get_fetch_map(remote_refs, &refspec, tail, 1); + for (rm = *old_tail; rm; rm = rm->next) + rm->merge = 1; + } +} + +static void find_non_local_tags(struct transport *transport, + struct ref **head, + struct ref ***tail); + +static struct ref *get_ref_map(struct transport *transport, + struct refspec *refs, int ref_count, int tags, + int *autotags) +{ + int i; + struct ref *rm; + struct ref *ref_map = NULL; + struct ref **tail = &ref_map; + + const struct ref *remote_refs = transport_get_remote_refs(transport); + + if (ref_count || tags == TAGS_SET) { + for (i = 0; i < ref_count; i++) { + get_fetch_map(remote_refs, &refs[i], &tail, 0); + if (refs[i].dst && refs[i].dst[0]) + *autotags = 1; + } + /* Merge everything on the command line, but not --tags */ + for (rm = ref_map; rm; rm = rm->next) + rm->merge = 1; + if (tags == TAGS_SET) + get_fetch_map(remote_refs, tag_refspec, &tail, 0); + } else { + /* Use the defaults */ + struct remote *remote = transport->remote; + struct branch *branch = branch_get(NULL); + int has_merge = branch_has_merge_config(branch); + if (remote && + (remote->fetch_refspec_nr || + /* Note: has_merge implies non-NULL branch->remote_name */ + (has_merge && !strcmp(branch->remote_name, remote->name)))) { + for (i = 0; i < remote->fetch_refspec_nr; i++) { + get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0); + if (remote->fetch[i].dst && + remote->fetch[i].dst[0]) + *autotags = 1; + if (!i && !has_merge && ref_map && + !remote->fetch[0].pattern) + ref_map->merge = 1; + } + /* + * if the remote we're fetching from is the same + * as given in branch.<name>.remote, we add the + * ref given in branch.<name>.merge, too. + * + * Note: has_merge implies non-NULL branch->remote_name + */ + if (has_merge && + !strcmp(branch->remote_name, remote->name)) + add_merge_config(&ref_map, remote_refs, branch, &tail); + } else { + ref_map = get_remote_ref(remote_refs, "HEAD"); + if (!ref_map) + die(_("Couldn't find remote ref HEAD")); + ref_map->merge = 1; + tail = &ref_map->next; + } + } + if (tags == TAGS_DEFAULT && *autotags) + find_non_local_tags(transport, &ref_map, &tail); + ref_remove_duplicates(ref_map); + + return ref_map; +} + +#define STORE_REF_ERROR_OTHER 1 +#define STORE_REF_ERROR_DF_CONFLICT 2 + +static int s_update_ref(const char *action, + struct ref *ref, + int check_old) +{ + char msg[1024]; + char *rla = getenv("GIT_REFLOG_ACTION"); + static struct ref_lock *lock; + + if (dry_run) + return 0; + if (!rla) + rla = default_rla.buf; + snprintf(msg, sizeof(msg), "%s: %s", rla, action); + lock = lock_any_ref_for_update(ref->name, + check_old ? ref->old_sha1 : NULL, 0); + if (!lock) + return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT : + STORE_REF_ERROR_OTHER; + if (write_ref_sha1(lock, ref->new_sha1, msg) < 0) + return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT : + STORE_REF_ERROR_OTHER; + return 0; +} + +#define REFCOL_WIDTH 10 + +static int update_local_ref(struct ref *ref, + const char *remote, + const struct ref *remote_ref, + struct strbuf *display) +{ + struct commit *current = NULL, *updated; + enum object_type type; + struct branch *current_branch = branch_get(NULL); + const char *pretty_ref = prettify_refname(ref->name); + + type = sha1_object_info(ref->new_sha1, NULL); + if (type < 0) + die(_("object %s not found"), sha1_to_hex(ref->new_sha1)); + + if (!hashcmp(ref->old_sha1, ref->new_sha1)) { + if (verbosity > 0) + strbuf_addf(display, "= %-*s %-*s -> %s", + TRANSPORT_SUMMARY(_("[up to date]")), + REFCOL_WIDTH, remote, pretty_ref); + return 0; + } + + if (current_branch && + !strcmp(ref->name, current_branch->name) && + !(update_head_ok || is_bare_repository()) && + !is_null_sha1(ref->old_sha1)) { + /* + * If this is the head, and it's not okay to update + * the head, and the old value of the head isn't empty... + */ + strbuf_addf(display, + _("! %-*s %-*s -> %s (can't fetch in current branch)"), + TRANSPORT_SUMMARY(_("[rejected]")), + REFCOL_WIDTH, remote, pretty_ref); + return 1; + } + + if (!is_null_sha1(ref->old_sha1) && + !prefixcmp(ref->name, "refs/tags/")) { + int r; + r = s_update_ref("updating tag", ref, 0); + strbuf_addf(display, "%c %-*s %-*s -> %s%s", + r ? '!' : '-', + TRANSPORT_SUMMARY(_("[tag update]")), + REFCOL_WIDTH, remote, pretty_ref, + r ? _(" (unable to update local ref)") : ""); + return r; + } + + current = lookup_commit_reference_gently(ref->old_sha1, 1); + updated = lookup_commit_reference_gently(ref->new_sha1, 1); + if (!current || !updated) { + const char *msg; + const char *what; + int r; + /* + * Nicely describe the new ref we're fetching. + * Base this on the remote's ref name, as it's + * more likely to follow a standard layout. + */ + const char *name = remote_ref ? remote_ref->name : ""; + if (!prefixcmp(name, "refs/tags/")) { + msg = "storing tag"; + what = _("[new tag]"); + } else if (!prefixcmp(name, "refs/heads/")) { + msg = "storing head"; + what = _("[new branch]"); + } else { + msg = "storing ref"; + what = _("[new ref]"); + } + + if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && + (recurse_submodules != RECURSE_SUBMODULES_ON)) + check_for_new_submodule_commits(ref->new_sha1); + r = s_update_ref(msg, ref, 0); + strbuf_addf(display, "%c %-*s %-*s -> %s%s", + r ? '!' : '*', + TRANSPORT_SUMMARY(what), + REFCOL_WIDTH, remote, pretty_ref, + r ? _(" (unable to update local ref)") : ""); + return r; + } + + if (in_merge_bases(current, updated)) { + char quickref[83]; + int r; + strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV)); + strcat(quickref, ".."); + strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); + if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && + (recurse_submodules != RECURSE_SUBMODULES_ON)) + check_for_new_submodule_commits(ref->new_sha1); + r = s_update_ref("fast-forward", ref, 1); + strbuf_addf(display, "%c %-*s %-*s -> %s%s", + r ? '!' : ' ', + TRANSPORT_SUMMARY_WIDTH, quickref, + REFCOL_WIDTH, remote, pretty_ref, + r ? _(" (unable to update local ref)") : ""); + return r; + } else if (force || ref->force) { + char quickref[84]; + int r; + strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV)); + strcat(quickref, "..."); + strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); + if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && + (recurse_submodules != RECURSE_SUBMODULES_ON)) + check_for_new_submodule_commits(ref->new_sha1); + r = s_update_ref("forced-update", ref, 1); + strbuf_addf(display, "%c %-*s %-*s -> %s (%s)", + r ? '!' : '+', + TRANSPORT_SUMMARY_WIDTH, quickref, + REFCOL_WIDTH, remote, pretty_ref, + r ? _("unable to update local ref") : _("forced update")); + return r; + } else { + strbuf_addf(display, "! %-*s %-*s -> %s %s", + TRANSPORT_SUMMARY(_("[rejected]")), + REFCOL_WIDTH, remote, pretty_ref, + _("(non-fast-forward)")); + return 1; + } +} + +static int iterate_ref_map(void *cb_data, unsigned char sha1[20]) +{ + struct ref **rm = cb_data; + struct ref *ref = *rm; + + if (!ref) + return -1; /* end of the list */ + *rm = ref->next; + hashcpy(sha1, ref->old_sha1); + return 0; +} + +static int store_updated_refs(const char *raw_url, const char *remote_name, + struct ref *ref_map) +{ + FILE *fp; + struct commit *commit; + int url_len, i, shown_url = 0, rc = 0; + struct strbuf note = STRBUF_INIT; + const char *what, *kind; + struct ref *rm; + char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD"); + int want_merge; + + fp = fopen(filename, "a"); + if (!fp) + return error(_("cannot open %s: %s\n"), filename, strerror(errno)); + + if (raw_url) + url = transport_anonymize_url(raw_url); + else + url = xstrdup("foreign"); + + rm = ref_map; + if (check_everything_connected(iterate_ref_map, 0, &rm)) { + rc = error(_("%s did not send all necessary objects\n"), url); + goto abort; + } + + /* + * The first pass writes objects to be merged and then the + * second pass writes the rest, in order to allow using + * FETCH_HEAD as a refname to refer to the ref to be merged. + */ + for (want_merge = 1; 0 <= want_merge; want_merge--) { + for (rm = ref_map; rm; rm = rm->next) { + struct ref *ref = NULL; + + commit = lookup_commit_reference_gently(rm->old_sha1, 1); + if (!commit) + rm->merge = 0; + + if (rm->merge != want_merge) + continue; + + if (rm->peer_ref) { + ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1); + strcpy(ref->name, rm->peer_ref->name); + hashcpy(ref->old_sha1, rm->peer_ref->old_sha1); + hashcpy(ref->new_sha1, rm->old_sha1); + ref->force = rm->peer_ref->force; + } + + + if (!strcmp(rm->name, "HEAD")) { + kind = ""; + what = ""; + } + else if (!prefixcmp(rm->name, "refs/heads/")) { + kind = "branch"; + what = rm->name + 11; + } + else if (!prefixcmp(rm->name, "refs/tags/")) { + kind = "tag"; + what = rm->name + 10; + } + else if (!prefixcmp(rm->name, "refs/remotes/")) { + kind = "remote-tracking branch"; + what = rm->name + 13; + } + else { + kind = ""; + what = rm->name; + } + + url_len = strlen(url); + for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) + ; + url_len = i + 1; + if (4 < i && !strncmp(".git", url + i - 3, 4)) + url_len = i - 3; + + strbuf_reset(¬e); + if (*what) { + if (*kind) + strbuf_addf(¬e, "%s ", kind); + strbuf_addf(¬e, "'%s' of ", what); + } + fprintf(fp, "%s\t%s\t%s", + sha1_to_hex(rm->old_sha1), + rm->merge ? "" : "not-for-merge", + note.buf); + for (i = 0; i < url_len; ++i) + if ('\n' == url[i]) + fputs("\\n", fp); + else + fputc(url[i], fp); + fputc('\n', fp); + + strbuf_reset(¬e); + if (ref) { + rc |= update_local_ref(ref, what, rm, ¬e); + free(ref); + } else + strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD", + TRANSPORT_SUMMARY_WIDTH, + *kind ? kind : "branch", + REFCOL_WIDTH, + *what ? what : "HEAD"); + if (note.len) { + if (verbosity >= 0 && !shown_url) { + fprintf(stderr, _("From %.*s\n"), + url_len, url); + shown_url = 1; + } + if (verbosity >= 0) + fprintf(stderr, " %s\n", note.buf); + } + } + } + + if (rc & STORE_REF_ERROR_DF_CONFLICT) + error(_("some local refs could not be updated; try running\n" + " 'git remote prune %s' to remove any old, conflicting " + "branches"), remote_name); + + abort: + strbuf_release(¬e); + free(url); + fclose(fp); + return rc; +} + +/* + * We would want to bypass the object transfer altogether if + * everything we are going to fetch already exists and is connected + * locally. + */ +static int quickfetch(struct ref *ref_map) +{ + struct ref *rm = ref_map; + + /* + * If we are deepening a shallow clone we already have these + * objects reachable. Running rev-list here will return with + * a good (0) exit status and we'll bypass the fetch that we + * really need to perform. Claiming failure now will ensure + * we perform the network exchange to deepen our history. + */ + if (depth) + return -1; + return check_everything_connected(iterate_ref_map, 1, &rm); +} + +static int fetch_refs(struct transport *transport, struct ref *ref_map) +{ + int ret = quickfetch(ref_map); + if (ret) + ret = transport_fetch_refs(transport, ref_map); + if (!ret) + ret |= store_updated_refs(transport->url, + transport->remote->name, + ref_map); + transport_unlock_pack(transport); + return ret; +} + +static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map) +{ + int result = 0; + struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map); + const char *dangling_msg = dry_run + ? _(" (%s will become dangling)") + : _(" (%s has become dangling)"); + + for (ref = stale_refs; ref; ref = ref->next) { + if (!dry_run) + result |= delete_ref(ref->name, NULL, 0); + if (verbosity >= 0) { + fprintf(stderr, " x %-*s %-*s -> %s\n", + TRANSPORT_SUMMARY(_("[deleted]")), + REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name)); + warn_dangling_symref(stderr, dangling_msg, ref->name); + } + } + free_refs(stale_refs); + return result; +} + +static int add_existing(const char *refname, const unsigned char *sha1, + int flag, void *cbdata) +{ + struct string_list *list = (struct string_list *)cbdata; + struct string_list_item *item = string_list_insert(list, refname); + item->util = (void *)sha1; + return 0; +} + +static int will_fetch(struct ref **head, const unsigned char *sha1) +{ + struct ref *rm = *head; + while (rm) { + if (!hashcmp(rm->old_sha1, sha1)) + return 1; + rm = rm->next; + } + return 0; +} + +static void find_non_local_tags(struct transport *transport, + struct ref **head, + struct ref ***tail) +{ + struct string_list existing_refs = STRING_LIST_INIT_NODUP; + struct string_list remote_refs = STRING_LIST_INIT_NODUP; + const struct ref *ref; + struct string_list_item *item = NULL; + + for_each_ref(add_existing, &existing_refs); + for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) { + if (prefixcmp(ref->name, "refs/tags/")) + continue; + + /* + * The peeled ref always follows the matching base + * ref, so if we see a peeled ref that we don't want + * to fetch then we can mark the ref entry in the list + * as one to ignore by setting util to NULL. + */ + if (!suffixcmp(ref->name, "^{}")) { + if (item && !has_sha1_file(ref->old_sha1) && + !will_fetch(head, ref->old_sha1) && + !has_sha1_file(item->util) && + !will_fetch(head, item->util)) + item->util = NULL; + item = NULL; + continue; + } + + /* + * If item is non-NULL here, then we previously saw a + * ref not followed by a peeled reference, so we need + * to check if it is a lightweight tag that we want to + * fetch. + */ + if (item && !has_sha1_file(item->util) && + !will_fetch(head, item->util)) + item->util = NULL; + + item = NULL; + + /* skip duplicates and refs that we already have */ + if (string_list_has_string(&remote_refs, ref->name) || + string_list_has_string(&existing_refs, ref->name)) + continue; + + item = string_list_insert(&remote_refs, ref->name); + item->util = (void *)ref->old_sha1; + } + string_list_clear(&existing_refs, 0); + + /* + * We may have a final lightweight tag that needs to be + * checked to see if it needs fetching. + */ + if (item && !has_sha1_file(item->util) && + !will_fetch(head, item->util)) + item->util = NULL; + + /* + * For all the tags in the remote_refs string list, + * add them to the list of refs to be fetched + */ + for_each_string_list_item(item, &remote_refs) { + /* Unless we have already decided to ignore this item... */ + if (item->util) + { + struct ref *rm = alloc_ref(item->string); + rm->peer_ref = alloc_ref(item->string); + hashcpy(rm->old_sha1, item->util); + **tail = rm; + *tail = &rm->next; + } + } + + string_list_clear(&remote_refs, 0); +} + +static void check_not_current_branch(struct ref *ref_map) +{ + struct branch *current_branch = branch_get(NULL); + + if (is_bare_repository() || !current_branch) + return; + + for (; ref_map; ref_map = ref_map->next) + if (ref_map->peer_ref && !strcmp(current_branch->refname, + ref_map->peer_ref->name)) + die(_("Refusing to fetch into current branch %s " + "of non-bare repository"), current_branch->refname); +} + +static int truncate_fetch_head(void) +{ + char *filename = git_path("FETCH_HEAD"); + FILE *fp = fopen(filename, "w"); + + if (!fp) + return error(_("cannot open %s: %s\n"), filename, strerror(errno)); + fclose(fp); + return 0; +} + +static int do_fetch(struct transport *transport, + struct refspec *refs, int ref_count) +{ + struct string_list existing_refs = STRING_LIST_INIT_NODUP; + struct string_list_item *peer_item = NULL; + struct ref *ref_map; + struct ref *rm; + int autotags = (transport->remote->fetch_tags == 1); + + for_each_ref(add_existing, &existing_refs); + + if (tags == TAGS_DEFAULT) { + if (transport->remote->fetch_tags == 2) + tags = TAGS_SET; + if (transport->remote->fetch_tags == -1) + tags = TAGS_UNSET; + } + + if (!transport->get_refs_list || !transport->fetch) + die(_("Don't know how to fetch from %s"), transport->url); + + /* if not appending, truncate FETCH_HEAD */ + if (!append && !dry_run) { + int errcode = truncate_fetch_head(); + if (errcode) + return errcode; + } + + ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags); + if (!update_head_ok) + check_not_current_branch(ref_map); + + for (rm = ref_map; rm; rm = rm->next) { + if (rm->peer_ref) { + peer_item = string_list_lookup(&existing_refs, + rm->peer_ref->name); + if (peer_item) + hashcpy(rm->peer_ref->old_sha1, + peer_item->util); + } + } + + if (tags == TAGS_DEFAULT && autotags) + transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); + if (fetch_refs(transport, ref_map)) { + free_refs(ref_map); + return 1; + } + if (prune) { + /* If --tags was specified, pretend the user gave us the canonical tags refspec */ + if (tags == TAGS_SET) { + const char *tags_str = "refs/tags/*:refs/tags/*"; + struct refspec *tags_refspec, *refspec; + + /* Copy the refspec and add the tags to it */ + refspec = xcalloc(ref_count + 1, sizeof(struct refspec)); + tags_refspec = parse_fetch_refspec(1, &tags_str); + memcpy(refspec, refs, ref_count * sizeof(struct refspec)); + memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec)); + ref_count++; + + prune_refs(refspec, ref_count, ref_map); + + ref_count--; + /* The rest of the strings belong to fetch_one */ + free_refspec(1, tags_refspec); + free(refspec); + } else if (ref_count) { + prune_refs(refs, ref_count, ref_map); + } else { + prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map); + } + } + free_refs(ref_map); + + /* if neither --no-tags nor --tags was specified, do automated tag + * following ... */ + if (tags == TAGS_DEFAULT && autotags) { + struct ref **tail = &ref_map; + ref_map = NULL; + find_non_local_tags(transport, &ref_map, &tail); + if (ref_map) { + transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); + transport_set_option(transport, TRANS_OPT_DEPTH, "0"); + fetch_refs(transport, ref_map); + } + free_refs(ref_map); + } + + return 0; +} + +static void set_option(const char *name, const char *value) +{ + int r = transport_set_option(transport, name, value); + if (r < 0) + die(_("Option \"%s\" value \"%s\" is not valid for %s"), + name, value, transport->url); + if (r > 0) + warning(_("Option \"%s\" is ignored for %s\n"), + name, transport->url); +} + +static int get_one_remote_for_fetch(struct remote *remote, void *priv) +{ + struct string_list *list = priv; + if (!remote->skip_default_update) + string_list_append(list, remote->name); + return 0; +} + +struct remote_group_data { + const char *name; + struct string_list *list; +}; + +static int get_remote_group(const char *key, const char *value, void *priv) +{ + struct remote_group_data *g = priv; + + if (!prefixcmp(key, "remotes.") && + !strcmp(key + 8, g->name)) { + /* split list by white space */ + int space = strcspn(value, " \t\n"); + while (*value) { + if (space > 1) { + string_list_append(g->list, + xstrndup(value, space)); + } + value += space + (value[space] != '\0'); + space = strcspn(value, " \t\n"); + } + } + + return 0; +} + +static int add_remote_or_group(const char *name, struct string_list *list) +{ + int prev_nr = list->nr; + struct remote_group_data g; + g.name = name; g.list = list; + + git_config(get_remote_group, &g); + if (list->nr == prev_nr) { + struct remote *remote; + if (!remote_is_configured(name)) + return 0; + remote = remote_get(name); + string_list_append(list, remote->name); + } + return 1; +} + +static void add_options_to_argv(struct argv_array *argv) +{ + if (dry_run) + argv_array_push(argv, "--dry-run"); + if (prune) + argv_array_push(argv, "--prune"); + if (update_head_ok) + argv_array_push(argv, "--update-head-ok"); + if (force) + argv_array_push(argv, "--force"); + if (keep) + argv_array_push(argv, "--keep"); + if (recurse_submodules == RECURSE_SUBMODULES_ON) + argv_array_push(argv, "--recurse-submodules"); + else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) + argv_array_push(argv, "--recurse-submodules=on-demand"); + if (tags == TAGS_SET) + argv_array_push(argv, "--tags"); + else if (tags == TAGS_UNSET) + argv_array_push(argv, "--no-tags"); + if (verbosity >= 2) + argv_array_push(argv, "-v"); + if (verbosity >= 1) + argv_array_push(argv, "-v"); + else if (verbosity < 0) + argv_array_push(argv, "-q"); + +} + +static int fetch_multiple(struct string_list *list) +{ + int i, result = 0; + struct argv_array argv = ARGV_ARRAY_INIT; + + if (!append && !dry_run) { + int errcode = truncate_fetch_head(); + if (errcode) + return errcode; + } + + argv_array_pushl(&argv, "fetch", "--append", NULL); + add_options_to_argv(&argv); + + for (i = 0; i < list->nr; i++) { + const char *name = list->items[i].string; + argv_array_push(&argv, name); + if (verbosity >= 0) + printf(_("Fetching %s\n"), name); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not fetch %s"), name); + result = 1; + } + argv_array_pop(&argv); + } + + argv_array_clear(&argv); + return result; +} + +static int fetch_one(struct remote *remote, int argc, const char **argv) +{ + int i; + static const char **refs = NULL; + struct refspec *refspec; + int ref_nr = 0; + int exit_code; + + if (!remote) + die(_("No remote repository specified. Please, specify either a URL or a\n" + "remote name from which new revisions should be fetched.")); + + transport = transport_get(remote, NULL); + transport_set_verbosity(transport, verbosity, progress); + if (upload_pack) + set_option(TRANS_OPT_UPLOADPACK, upload_pack); + if (keep) + set_option(TRANS_OPT_KEEP, "yes"); + if (depth) + set_option(TRANS_OPT_DEPTH, depth); + + if (argc > 0) { + int j = 0; + refs = xcalloc(argc + 1, sizeof(const char *)); + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "tag")) { + char *ref; + i++; + if (i >= argc) + die(_("You need to specify a tag name.")); + ref = xmalloc(strlen(argv[i]) * 2 + 22); + strcpy(ref, "refs/tags/"); + strcat(ref, argv[i]); + strcat(ref, ":refs/tags/"); + strcat(ref, argv[i]); + refs[j++] = ref; + } else + refs[j++] = argv[i]; + } + refs[j] = NULL; + ref_nr = j; + } + + sigchain_push_common(unlock_pack_on_signal); + atexit(unlock_pack); + refspec = parse_fetch_refspec(ref_nr, refs); + exit_code = do_fetch(transport, refspec, ref_nr); + free_refspec(ref_nr, refspec); + transport_disconnect(transport); + transport = NULL; + return exit_code; +} + +int cmd_fetch(int argc, const char **argv, const char *prefix) +{ + int i; + struct string_list list = STRING_LIST_INIT_NODUP; + struct remote *remote; + int result = 0; + + packet_trace_identity("fetch"); + + /* Record the command line for the reflog */ + strbuf_addstr(&default_rla, "fetch"); + for (i = 1; i < argc; i++) + strbuf_addf(&default_rla, " %s", argv[i]); + + argc = parse_options(argc, argv, prefix, + builtin_fetch_options, builtin_fetch_usage, 0); + + if (recurse_submodules != RECURSE_SUBMODULES_OFF) { + if (recurse_submodules_default) { + int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default); + set_config_fetch_recurse_submodules(arg); + } + gitmodules_config(); + git_config(submodule_config, NULL); + } + + if (all) { + if (argc == 1) + die(_("fetch --all does not take a repository argument")); + else if (argc > 1) + die(_("fetch --all does not make sense with refspecs")); + (void) for_each_remote(get_one_remote_for_fetch, &list); + result = fetch_multiple(&list); + } else if (argc == 0) { + /* No arguments -- use default remote */ + remote = remote_get(NULL); + result = fetch_one(remote, argc, argv); + } else if (multiple) { + /* All arguments are assumed to be remotes or groups */ + for (i = 0; i < argc; i++) + if (!add_remote_or_group(argv[i], &list)) + die(_("No such remote or remote group: %s"), argv[i]); + result = fetch_multiple(&list); + } else { + /* Single remote or group */ + (void) add_remote_or_group(argv[0], &list); + if (list.nr > 1) { + /* More than one remote */ + if (argc > 1) + die(_("Fetching a group and specifying refspecs does not make sense")); + result = fetch_multiple(&list); + } else { + /* Zero or one remotes */ + remote = remote_get(argv[0]); + result = fetch_one(remote, argc-1, argv+1); + } + } + + if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) { + struct argv_array options = ARGV_ARRAY_INIT; + + add_options_to_argv(&options); + result = fetch_populated_submodules(&options, + submodule_prefix, + recurse_submodules, + verbosity < 0); + argv_array_clear(&options); + } + + /* All names were strdup()ed or strndup()ed */ + list.strdup_strings = 1; + string_list_clear(&list, 0); + + return result; +} diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c new file mode 100644 index 0000000000..d9af43c257 --- /dev/null +++ b/builtin/fmt-merge-msg.c @@ -0,0 +1,707 @@ +#include "builtin.h" +#include "cache.h" +#include "commit.h" +#include "diff.h" +#include "revision.h" +#include "tag.h" +#include "string-list.h" +#include "branch.h" +#include "fmt-merge-msg.h" +#include "gpg-interface.h" + +static const char * const fmt_merge_msg_usage[] = { + N_("git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]"), + NULL +}; + +static int use_branch_desc; + +int fmt_merge_msg_config(const char *key, const char *value, void *cb) +{ + if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) { + int is_bool; + merge_log_config = git_config_bool_or_int(key, value, &is_bool); + if (!is_bool && merge_log_config < 0) + return error("%s: negative length %s", key, value); + if (is_bool && merge_log_config) + merge_log_config = DEFAULT_MERGE_LOG_LEN; + } else if (!strcmp(key, "merge.branchdesc")) { + use_branch_desc = git_config_bool(key, value); + } else { + return git_default_config(key, value, cb); + } + return 0; +} + +/* merge data per repository where the merged tips came from */ +struct src_data { + struct string_list branch, tag, r_branch, generic; + int head_status; +}; + +struct origin_data { + unsigned char sha1[20]; + unsigned is_local_branch:1; +}; + +static void init_src_data(struct src_data *data) +{ + data->branch.strdup_strings = 1; + data->tag.strdup_strings = 1; + data->r_branch.strdup_strings = 1; + data->generic.strdup_strings = 1; +} + +static struct string_list srcs = STRING_LIST_INIT_DUP; +static struct string_list origins = STRING_LIST_INIT_DUP; + +struct merge_parents { + int alloc, nr; + struct merge_parent { + unsigned char given[20]; + unsigned char commit[20]; + unsigned char used; + } *item; +}; + +/* + * I know, I know, this is inefficient, but you won't be pulling and merging + * hundreds of heads at a time anyway. + */ +static struct merge_parent *find_merge_parent(struct merge_parents *table, + unsigned char *given, + unsigned char *commit) +{ + int i; + for (i = 0; i < table->nr; i++) { + if (given && hashcmp(table->item[i].given, given)) + continue; + if (commit && hashcmp(table->item[i].commit, commit)) + continue; + return &table->item[i]; + } + return NULL; +} + +static void add_merge_parent(struct merge_parents *table, + unsigned char *given, + unsigned char *commit) +{ + if (table->nr && find_merge_parent(table, given, commit)) + return; + ALLOC_GROW(table->item, table->nr + 1, table->alloc); + hashcpy(table->item[table->nr].given, given); + hashcpy(table->item[table->nr].commit, commit); + table->item[table->nr].used = 0; + table->nr++; +} + +static int handle_line(char *line, struct merge_parents *merge_parents) +{ + int i, len = strlen(line); + struct origin_data *origin_data; + char *src, *origin; + struct src_data *src_data; + struct string_list_item *item; + int pulling_head = 0; + unsigned char sha1[20]; + + if (len < 43 || line[40] != '\t') + return 1; + + if (!prefixcmp(line + 41, "not-for-merge")) + return 0; + + if (line[41] != '\t') + return 2; + + i = get_sha1_hex(line, sha1); + if (i) + return 3; + + if (!find_merge_parent(merge_parents, sha1, NULL)) + return 0; /* subsumed by other parents */ + + origin_data = xcalloc(1, sizeof(struct origin_data)); + hashcpy(origin_data->sha1, sha1); + + if (line[len - 1] == '\n') + line[len - 1] = 0; + line += 42; + + /* + * At this point, line points at the beginning of comment e.g. + * "branch 'frotz' of git://that/repository.git". + * Find the repository name and point it with src. + */ + src = strstr(line, " of "); + if (src) { + *src = 0; + src += 4; + pulling_head = 0; + } else { + src = line; + pulling_head = 1; + } + + item = unsorted_string_list_lookup(&srcs, src); + if (!item) { + item = string_list_append(&srcs, src); + item->util = xcalloc(1, sizeof(struct src_data)); + init_src_data(item->util); + } + src_data = item->util; + + if (pulling_head) { + origin = src; + src_data->head_status |= 1; + } else if (!prefixcmp(line, "branch ")) { + origin_data->is_local_branch = 1; + origin = line + 7; + string_list_append(&src_data->branch, origin); + src_data->head_status |= 2; + } else if (!prefixcmp(line, "tag ")) { + origin = line; + string_list_append(&src_data->tag, origin + 4); + src_data->head_status |= 2; + } else if (!prefixcmp(line, "remote-tracking branch ")) { + origin = line + strlen("remote-tracking branch "); + string_list_append(&src_data->r_branch, origin); + src_data->head_status |= 2; + } else { + origin = src; + string_list_append(&src_data->generic, line); + src_data->head_status |= 2; + } + + if (!strcmp(".", src) || !strcmp(src, origin)) { + int len = strlen(origin); + if (origin[0] == '\'' && origin[len - 1] == '\'') + origin = xmemdupz(origin + 1, len - 2); + } else { + char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5); + sprintf(new_origin, "%s of %s", origin, src); + origin = new_origin; + } + if (strcmp(".", src)) + origin_data->is_local_branch = 0; + string_list_append(&origins, origin)->util = origin_data; + return 0; +} + +static void print_joined(const char *singular, const char *plural, + struct string_list *list, struct strbuf *out) +{ + if (list->nr == 0) + return; + if (list->nr == 1) { + strbuf_addf(out, "%s%s", singular, list->items[0].string); + } else { + int i; + strbuf_addstr(out, plural); + for (i = 0; i < list->nr - 1; i++) + strbuf_addf(out, "%s%s", i > 0 ? ", " : "", + list->items[i].string); + strbuf_addf(out, " and %s", list->items[list->nr - 1].string); + } +} + +static void add_branch_desc(struct strbuf *out, const char *name) +{ + struct strbuf desc = STRBUF_INIT; + + if (!read_branch_desc(&desc, name)) { + const char *bp = desc.buf; + while (*bp) { + const char *ep = strchrnul(bp, '\n'); + if (*ep) + ep++; + strbuf_addf(out, " : %.*s", (int)(ep - bp), bp); + bp = ep; + } + if (out->buf[out->len - 1] != '\n') + strbuf_addch(out, '\n'); + } + strbuf_release(&desc); +} + +#define util_as_integral(elem) ((intptr_t)((elem)->util)) + +static void record_person(int which, struct string_list *people, + struct commit *commit) +{ + char *name_buf, *name, *name_end; + struct string_list_item *elem; + const char *field; + + field = (which == 'a') ? "\nauthor " : "\ncommitter "; + name = strstr(commit->buffer, field); + if (!name) + return; + name += strlen(field); + name_end = strchrnul(name, '<'); + if (*name_end) + name_end--; + while (isspace(*name_end) && name <= name_end) + name_end--; + if (name_end < name) + return; + name_buf = xmemdupz(name, name_end - name + 1); + + elem = string_list_lookup(people, name_buf); + if (!elem) { + elem = string_list_insert(people, name_buf); + elem->util = (void *)0; + } + elem->util = (void*)(util_as_integral(elem) + 1); + free(name_buf); +} + +static int cmp_string_list_util_as_integral(const void *a_, const void *b_) +{ + const struct string_list_item *a = a_, *b = b_; + return util_as_integral(b) - util_as_integral(a); +} + +static void add_people_count(struct strbuf *out, struct string_list *people) +{ + if (people->nr == 1) + strbuf_addf(out, "%s", people->items[0].string); + else if (people->nr == 2) + strbuf_addf(out, "%s (%d) and %s (%d)", + people->items[0].string, + (int)util_as_integral(&people->items[0]), + people->items[1].string, + (int)util_as_integral(&people->items[1])); + else if (people->nr) + strbuf_addf(out, "%s (%d) and others", + people->items[0].string, + (int)util_as_integral(&people->items[0])); +} + +static void credit_people(struct strbuf *out, + struct string_list *them, + int kind) +{ + const char *label; + const char *me; + + if (kind == 'a') { + label = "\n# By "; + me = git_author_info(IDENT_NO_DATE); + } else { + label = "\n# Via "; + me = git_committer_info(IDENT_NO_DATE); + } + + if (!them->nr || + (them->nr == 1 && + me && + (me = skip_prefix(me, them->items->string)) != NULL && + skip_prefix(me, " <"))) + return; + strbuf_addstr(out, label); + add_people_count(out, them); +} + +static void add_people_info(struct strbuf *out, + struct string_list *authors, + struct string_list *committers) +{ + if (authors->nr) + qsort(authors->items, + authors->nr, sizeof(authors->items[0]), + cmp_string_list_util_as_integral); + if (committers->nr) + qsort(committers->items, + committers->nr, sizeof(committers->items[0]), + cmp_string_list_util_as_integral); + + credit_people(out, authors, 'a'); + credit_people(out, committers, 'c'); +} + +static void shortlog(const char *name, + struct origin_data *origin_data, + struct commit *head, + struct rev_info *rev, + struct fmt_merge_msg_opts *opts, + struct strbuf *out) +{ + int i, count = 0; + struct commit *commit; + struct object *branch; + struct string_list subjects = STRING_LIST_INIT_DUP; + struct string_list authors = STRING_LIST_INIT_DUP; + struct string_list committers = STRING_LIST_INIT_DUP; + int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED; + struct strbuf sb = STRBUF_INIT; + const unsigned char *sha1 = origin_data->sha1; + int limit = opts->shortlog_len; + + branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40); + if (!branch || branch->type != OBJ_COMMIT) + return; + + setup_revisions(0, NULL, rev, NULL); + add_pending_object(rev, branch, name); + add_pending_object(rev, &head->object, "^HEAD"); + head->object.flags |= UNINTERESTING; + if (prepare_revision_walk(rev)) + die("revision walk setup failed"); + while ((commit = get_revision(rev)) != NULL) { + struct pretty_print_context ctx = {0}; + + if (commit->parents && commit->parents->next) { + /* do not list a merge but count committer */ + if (opts->credit_people) + record_person('c', &committers, commit); + continue; + } + if (!count && opts->credit_people) + /* the 'tip' committer */ + record_person('c', &committers, commit); + if (opts->credit_people) + record_person('a', &authors, commit); + count++; + if (subjects.nr > limit) + continue; + + format_commit_message(commit, "%s", &sb, &ctx); + strbuf_ltrim(&sb); + + if (!sb.len) + string_list_append(&subjects, + sha1_to_hex(commit->object.sha1)); + else + string_list_append(&subjects, strbuf_detach(&sb, NULL)); + } + + if (opts->credit_people) + add_people_info(out, &authors, &committers); + if (count > limit) + strbuf_addf(out, "\n* %s: (%d commits)\n", name, count); + else + strbuf_addf(out, "\n* %s:\n", name); + + if (origin_data->is_local_branch && use_branch_desc) + add_branch_desc(out, name); + + for (i = 0; i < subjects.nr; i++) + if (i >= limit) + strbuf_addf(out, " ...\n"); + else + strbuf_addf(out, " %s\n", subjects.items[i].string); + + clear_commit_marks((struct commit *)branch, flags); + clear_commit_marks(head, flags); + free_commit_list(rev->commits); + rev->commits = NULL; + rev->pending.nr = 0; + + string_list_clear(&authors, 0); + string_list_clear(&committers, 0); + string_list_clear(&subjects, 0); +} + +static void fmt_merge_msg_title(struct strbuf *out, + const char *current_branch) { + int i = 0; + char *sep = ""; + + strbuf_addstr(out, "Merge "); + for (i = 0; i < srcs.nr; i++) { + struct src_data *src_data = srcs.items[i].util; + const char *subsep = ""; + + strbuf_addstr(out, sep); + sep = "; "; + + if (src_data->head_status == 1) { + strbuf_addstr(out, srcs.items[i].string); + continue; + } + if (src_data->head_status == 3) { + subsep = ", "; + strbuf_addstr(out, "HEAD"); + } + if (src_data->branch.nr) { + strbuf_addstr(out, subsep); + subsep = ", "; + print_joined("branch ", "branches ", &src_data->branch, + out); + } + if (src_data->r_branch.nr) { + strbuf_addstr(out, subsep); + subsep = ", "; + print_joined("remote-tracking branch ", "remote-tracking branches ", + &src_data->r_branch, out); + } + if (src_data->tag.nr) { + strbuf_addstr(out, subsep); + subsep = ", "; + print_joined("tag ", "tags ", &src_data->tag, out); + } + if (src_data->generic.nr) { + strbuf_addstr(out, subsep); + print_joined("commit ", "commits ", &src_data->generic, + out); + } + if (strcmp(".", srcs.items[i].string)) + strbuf_addf(out, " of %s", srcs.items[i].string); + } + + if (!strcmp("master", current_branch)) + strbuf_addch(out, '\n'); + else + strbuf_addf(out, " into %s\n", current_branch); +} + +static void fmt_tag_signature(struct strbuf *tagbuf, + struct strbuf *sig, + const char *buf, + unsigned long len) +{ + const char *tag_body = strstr(buf, "\n\n"); + if (tag_body) { + tag_body += 2; + strbuf_add(tagbuf, tag_body, buf + len - tag_body); + } + strbuf_complete_line(tagbuf); + if (sig->len) { + strbuf_addch(tagbuf, '\n'); + strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len); + } +} + +static void fmt_merge_msg_sigs(struct strbuf *out) +{ + int i, tag_number = 0, first_tag = 0; + struct strbuf tagbuf = STRBUF_INIT; + + for (i = 0; i < origins.nr; i++) { + unsigned char *sha1 = origins.items[i].util; + enum object_type type; + unsigned long size, len; + char *buf = read_sha1_file(sha1, &type, &size); + struct strbuf sig = STRBUF_INIT; + + if (!buf || type != OBJ_TAG) + goto next; + len = parse_signature(buf, size); + + if (size == len) + ; /* merely annotated */ + else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) { + if (!sig.len) + strbuf_addstr(&sig, "gpg verification failed.\n"); + } + + if (!tag_number++) { + fmt_tag_signature(&tagbuf, &sig, buf, len); + first_tag = i; + } else { + if (tag_number == 2) { + struct strbuf tagline = STRBUF_INIT; + strbuf_addf(&tagline, "\n# %s\n", + origins.items[first_tag].string); + strbuf_insert(&tagbuf, 0, tagline.buf, + tagline.len); + strbuf_release(&tagline); + } + strbuf_addf(&tagbuf, "\n# %s\n", + origins.items[i].string); + fmt_tag_signature(&tagbuf, &sig, buf, len); + } + strbuf_release(&sig); + next: + free(buf); + } + if (tagbuf.len) { + strbuf_addch(out, '\n'); + strbuf_addbuf(out, &tagbuf); + } + strbuf_release(&tagbuf); +} + +static void find_merge_parents(struct merge_parents *result, + struct strbuf *in, unsigned char *head) +{ + struct commit_list *parents, *next; + struct commit *head_commit; + int pos = 0, i, j; + + parents = NULL; + while (pos < in->len) { + int len; + char *p = in->buf + pos; + char *newline = strchr(p, '\n'); + unsigned char sha1[20]; + struct commit *parent; + struct object *obj; + + len = newline ? newline - p : strlen(p); + pos += len + !!newline; + + if (len < 43 || + get_sha1_hex(p, sha1) || + p[40] != '\t' || + p[41] != '\t') + continue; /* skip not-for-merge */ + /* + * Do not use get_merge_parent() here; we do not have + * "name" here and we do not want to contaminate its + * util field yet. + */ + obj = parse_object(sha1); + parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT); + if (!parent) + continue; + commit_list_insert(parent, &parents); + add_merge_parent(result, obj->sha1, parent->object.sha1); + } + head_commit = lookup_commit(head); + if (head_commit) + commit_list_insert(head_commit, &parents); + parents = reduce_heads(parents); + + while (parents) { + for (i = 0; i < result->nr; i++) + if (!hashcmp(result->item[i].commit, + parents->item->object.sha1)) + result->item[i].used = 1; + next = parents->next; + free(parents); + parents = next; + } + + for (i = j = 0; i < result->nr; i++) { + if (result->item[i].used) { + if (i != j) + result->item[j] = result->item[i]; + j++; + } + } + result->nr = j; +} + +int fmt_merge_msg(struct strbuf *in, struct strbuf *out, + struct fmt_merge_msg_opts *opts) +{ + int i = 0, pos = 0; + unsigned char head_sha1[20]; + const char *current_branch; + void *current_branch_to_free; + struct merge_parents merge_parents; + + memset(&merge_parents, 0, sizeof(merge_parents)); + + /* get current branch */ + current_branch = current_branch_to_free = + resolve_refdup("HEAD", head_sha1, 1, NULL); + if (!current_branch) + die("No current branch"); + if (!prefixcmp(current_branch, "refs/heads/")) + current_branch += 11; + + find_merge_parents(&merge_parents, in, head_sha1); + + /* get a line */ + while (pos < in->len) { + int len; + char *newline, *p = in->buf + pos; + + newline = strchr(p, '\n'); + len = newline ? newline - p : strlen(p); + pos += len + !!newline; + i++; + p[len] = 0; + if (handle_line(p, &merge_parents)) + die ("Error in line %d: %.*s", i, len, p); + } + + if (opts->add_title && srcs.nr) + fmt_merge_msg_title(out, current_branch); + + if (origins.nr) + fmt_merge_msg_sigs(out); + + if (opts->shortlog_len) { + struct commit *head; + struct rev_info rev; + + head = lookup_commit_or_die(head_sha1, "HEAD"); + init_revisions(&rev, NULL); + rev.commit_format = CMIT_FMT_ONELINE; + rev.ignore_merges = 1; + rev.limited = 1; + + strbuf_complete_line(out); + + for (i = 0; i < origins.nr; i++) + shortlog(origins.items[i].string, + origins.items[i].util, + head, &rev, opts, out); + } + + strbuf_complete_line(out); + free(current_branch_to_free); + free(merge_parents.item); + return 0; +} + +int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) +{ + const char *inpath = NULL; + const char *message = NULL; + int shortlog_len = -1; + struct option options[] = { + { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"), + N_("populate log with at most <n> entries from shortlog"), + PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN }, + { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"), + N_("alias for --log (deprecated)"), + PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL, + DEFAULT_MERGE_LOG_LEN }, + OPT_STRING('m', "message", &message, N_("text"), + N_("use <text> as start of message")), + OPT_FILENAME('F', "file", &inpath, N_("file to read from")), + OPT_END() + }; + + FILE *in = stdin; + struct strbuf input = STRBUF_INIT, output = STRBUF_INIT; + int ret; + struct fmt_merge_msg_opts opts; + + git_config(fmt_merge_msg_config, NULL); + argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage, + 0); + if (argc > 0) + usage_with_options(fmt_merge_msg_usage, options); + if (shortlog_len < 0) + shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; + + if (inpath && strcmp(inpath, "-")) { + in = fopen(inpath, "r"); + if (!in) + die_errno("cannot open '%s'", inpath); + } + + if (strbuf_read(&input, fileno(in), 0) < 0) + die_errno("could not read input file"); + + if (message) + strbuf_addstr(&output, message); + + memset(&opts, 0, sizeof(opts)); + opts.add_title = !message; + opts.credit_people = 1; + opts.shortlog_len = shortlog_len; + + ret = fmt_merge_msg(&input, &output, &opts); + if (ret) + return ret; + write_in_full(STDOUT_FILENO, output.buf, output.len); + return 0; +} diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c new file mode 100644 index 0000000000..7f059c31df --- /dev/null +++ b/builtin/for-each-ref.c @@ -0,0 +1,1042 @@ +#include "builtin.h" +#include "cache.h" +#include "refs.h" +#include "object.h" +#include "tag.h" +#include "commit.h" +#include "tree.h" +#include "blob.h" +#include "quote.h" +#include "parse-options.h" +#include "remote.h" + +/* Quoting styles */ +#define QUOTE_NONE 0 +#define QUOTE_SHELL 1 +#define QUOTE_PERL 2 +#define QUOTE_PYTHON 4 +#define QUOTE_TCL 8 + +typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; + +struct atom_value { + const char *s; + unsigned long ul; /* used for sorting when not FIELD_STR */ +}; + +struct ref_sort { + struct ref_sort *next; + int atom; /* index into used_atom array */ + unsigned reverse : 1; +}; + +struct refinfo { + char *refname; + unsigned char objectname[20]; + int flag; + const char *symref; + struct atom_value *value; +}; + +static struct { + const char *name; + cmp_type cmp_type; +} valid_atom[] = { + { "refname" }, + { "objecttype" }, + { "objectsize", FIELD_ULONG }, + { "objectname" }, + { "tree" }, + { "parent" }, + { "numparent", FIELD_ULONG }, + { "object" }, + { "type" }, + { "tag" }, + { "author" }, + { "authorname" }, + { "authoremail" }, + { "authordate", FIELD_TIME }, + { "committer" }, + { "committername" }, + { "committeremail" }, + { "committerdate", FIELD_TIME }, + { "tagger" }, + { "taggername" }, + { "taggeremail" }, + { "taggerdate", FIELD_TIME }, + { "creator" }, + { "creatordate", FIELD_TIME }, + { "subject" }, + { "body" }, + { "contents" }, + { "contents:subject" }, + { "contents:body" }, + { "contents:signature" }, + { "upstream" }, + { "symref" }, + { "flag" }, +}; + +/* + * An atom is a valid field atom listed above, possibly prefixed with + * a "*" to denote deref_tag(). + * + * We parse given format string and sort specifiers, and make a list + * of properties that we need to extract out of objects. refinfo + * structure will hold an array of values extracted that can be + * indexed with the "atom number", which is an index into this + * array. + */ +static const char **used_atom; +static cmp_type *used_atom_type; +static int used_atom_cnt, sort_atom_limit, need_tagged, need_symref; + +/* + * Used to parse format string and sort specifiers + */ +static int parse_atom(const char *atom, const char *ep) +{ + const char *sp; + int i, at; + + sp = atom; + if (*sp == '*' && sp < ep) + sp++; /* deref */ + if (ep <= sp) + die("malformed field name: %.*s", (int)(ep-atom), atom); + + /* Do we have the atom already used elsewhere? */ + for (i = 0; i < used_atom_cnt; i++) { + int len = strlen(used_atom[i]); + if (len == ep - atom && !memcmp(used_atom[i], atom, len)) + return i; + } + + /* Is the atom a valid one? */ + for (i = 0; i < ARRAY_SIZE(valid_atom); i++) { + int len = strlen(valid_atom[i].name); + /* + * If the atom name has a colon, strip it and everything after + * it off - it specifies the format for this entry, and + * shouldn't be used for checking against the valid_atom + * table. + */ + const char *formatp = strchr(sp, ':'); + if (!formatp || ep < formatp) + formatp = ep; + if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len)) + break; + } + + if (ARRAY_SIZE(valid_atom) <= i) + die("unknown field name: %.*s", (int)(ep-atom), atom); + + /* Add it in, including the deref prefix */ + at = used_atom_cnt; + used_atom_cnt++; + used_atom = xrealloc(used_atom, + (sizeof *used_atom) * used_atom_cnt); + used_atom_type = xrealloc(used_atom_type, + (sizeof(*used_atom_type) * used_atom_cnt)); + used_atom[at] = xmemdupz(atom, ep - atom); + used_atom_type[at] = valid_atom[i].cmp_type; + if (*atom == '*') + need_tagged = 1; + if (!strcmp(used_atom[at], "symref")) + need_symref = 1; + return at; +} + +/* + * In a format string, find the next occurrence of %(atom). + */ +static const char *find_next(const char *cp) +{ + while (*cp) { + if (*cp == '%') { + /* + * %( is the start of an atom; + * %% is a quoted per-cent. + */ + if (cp[1] == '(') + return cp; + else if (cp[1] == '%') + cp++; /* skip over two % */ + /* otherwise this is a singleton, literal % */ + } + cp++; + } + return NULL; +} + +/* + * Make sure the format string is well formed, and parse out + * the used atoms. + */ +static int verify_format(const char *format) +{ + const char *cp, *sp; + for (cp = format; *cp && (sp = find_next(cp)); ) { + const char *ep = strchr(sp, ')'); + if (!ep) + return error("malformed format string %s", sp); + /* sp points at "%(" and ep points at the closing ")" */ + parse_atom(sp + 2, ep); + cp = ep + 1; + } + return 0; +} + +/* + * Given an object name, read the object data and size, and return a + * "struct object". If the object data we are returning is also borrowed + * by the "struct object" representation, set *eaten as well---it is a + * signal from parse_object_buffer to us not to free the buffer. + */ +static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten) +{ + enum object_type type; + void *buf = read_sha1_file(sha1, &type, sz); + + if (buf) + *obj = parse_object_buffer(sha1, type, *sz, buf, eaten); + else + *obj = NULL; + return buf; +} + +/* See grab_values */ +static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (!strcmp(name, "objecttype")) + v->s = typename(obj->type); + else if (!strcmp(name, "objectsize")) { + char *s = xmalloc(40); + sprintf(s, "%lu", sz); + v->ul = sz; + v->s = s; + } + else if (!strcmp(name, "objectname")) { + char *s = xmalloc(41); + strcpy(s, sha1_to_hex(obj->sha1)); + v->s = s; + } + else if (!strcmp(name, "objectname:short")) { + v->s = xstrdup(find_unique_abbrev(obj->sha1, + DEFAULT_ABBREV)); + } + } +} + +/* See grab_values */ +static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + struct tag *tag = (struct tag *) obj; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (!strcmp(name, "tag")) + v->s = tag->tag; + else if (!strcmp(name, "type") && tag->tagged) + v->s = typename(tag->tagged->type); + else if (!strcmp(name, "object") && tag->tagged) { + char *s = xmalloc(41); + strcpy(s, sha1_to_hex(tag->tagged->sha1)); + v->s = s; + } + } +} + +static int num_parents(struct commit *commit) +{ + struct commit_list *parents; + int i; + + for (i = 0, parents = commit->parents; + parents; + parents = parents->next) + i++; + return i; +} + +/* See grab_values */ +static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + struct commit *commit = (struct commit *) obj; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (!strcmp(name, "tree")) { + char *s = xmalloc(41); + strcpy(s, sha1_to_hex(commit->tree->object.sha1)); + v->s = s; + } + if (!strcmp(name, "numparent")) { + char *s = xmalloc(40); + v->ul = num_parents(commit); + sprintf(s, "%lu", v->ul); + v->s = s; + } + else if (!strcmp(name, "parent")) { + int num = num_parents(commit); + int i; + struct commit_list *parents; + char *s = xmalloc(41 * num + 1); + v->s = s; + for (i = 0, parents = commit->parents; + parents; + parents = parents->next, i = i + 41) { + struct commit *parent = parents->item; + strcpy(s+i, sha1_to_hex(parent->object.sha1)); + if (parents->next) + s[i+40] = ' '; + } + if (!i) + *s = '\0'; + } + } +} + +static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz) +{ + const char *eol; + while (*buf) { + if (!strncmp(buf, who, wholen) && + buf[wholen] == ' ') + return buf + wholen + 1; + eol = strchr(buf, '\n'); + if (!eol) + return ""; + eol++; + if (*eol == '\n') + return ""; /* end of header */ + buf = eol; + } + return ""; +} + +static const char *copy_line(const char *buf) +{ + const char *eol = strchrnul(buf, '\n'); + return xmemdupz(buf, eol - buf); +} + +static const char *copy_name(const char *buf) +{ + const char *cp; + for (cp = buf; *cp && *cp != '\n'; cp++) { + if (!strncmp(cp, " <", 2)) + return xmemdupz(buf, cp - buf); + } + return ""; +} + +static const char *copy_email(const char *buf) +{ + const char *email = strchr(buf, '<'); + const char *eoemail; + if (!email) + return ""; + eoemail = strchr(email, '>'); + if (!eoemail) + return ""; + return xmemdupz(email, eoemail + 1 - email); +} + +static char *copy_subject(const char *buf, unsigned long len) +{ + char *r = xmemdupz(buf, len); + int i; + + for (i = 0; i < len; i++) + if (r[i] == '\n') + r[i] = ' '; + + return r; +} + +static void grab_date(const char *buf, struct atom_value *v, const char *atomname) +{ + const char *eoemail = strstr(buf, "> "); + char *zone; + unsigned long timestamp; + long tz; + enum date_mode date_mode = DATE_NORMAL; + const char *formatp; + + /* + * We got here because atomname ends in "date" or "date<something>"; + * it's not possible that <something> is not ":<format>" because + * parse_atom() wouldn't have allowed it, so we can assume that no + * ":" means no format is specified, and use the default. + */ + formatp = strchr(atomname, ':'); + if (formatp != NULL) { + formatp++; + date_mode = parse_date_format(formatp); + } + + if (!eoemail) + goto bad; + timestamp = strtoul(eoemail + 2, &zone, 10); + if (timestamp == ULONG_MAX) + goto bad; + tz = strtol(zone, NULL, 10); + if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) + goto bad; + v->s = xstrdup(show_date(timestamp, tz, date_mode)); + v->ul = timestamp; + return; + bad: + v->s = ""; + v->ul = 0; +} + +/* See grab_values */ +static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + int wholen = strlen(who); + const char *wholine = NULL; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (strncmp(who, name, wholen)) + continue; + if (name[wholen] != 0 && + strcmp(name + wholen, "name") && + strcmp(name + wholen, "email") && + prefixcmp(name + wholen, "date")) + continue; + if (!wholine) + wholine = find_wholine(who, wholen, buf, sz); + if (!wholine) + return; /* no point looking for it */ + if (name[wholen] == 0) + v->s = copy_line(wholine); + else if (!strcmp(name + wholen, "name")) + v->s = copy_name(wholine); + else if (!strcmp(name + wholen, "email")) + v->s = copy_email(wholine); + else if (!prefixcmp(name + wholen, "date")) + grab_date(wholine, v, name); + } + + /* + * For a tag or a commit object, if "creator" or "creatordate" is + * requested, do something special. + */ + if (strcmp(who, "tagger") && strcmp(who, "committer")) + return; /* "author" for commit object is not wanted */ + if (!wholine) + wholine = find_wholine(who, wholen, buf, sz); + if (!wholine) + return; + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + + if (!prefixcmp(name, "creatordate")) + grab_date(wholine, v, name); + else if (!strcmp(name, "creator")) + v->s = copy_line(wholine); + } +} + +static void find_subpos(const char *buf, unsigned long sz, + const char **sub, unsigned long *sublen, + const char **body, unsigned long *bodylen, + unsigned long *nonsiglen, + const char **sig, unsigned long *siglen) +{ + const char *eol; + /* skip past header until we hit empty line */ + while (*buf && *buf != '\n') { + eol = strchrnul(buf, '\n'); + if (*eol) + eol++; + buf = eol; + } + /* skip any empty lines */ + while (*buf == '\n') + buf++; + + /* parse signature first; we might not even have a subject line */ + *sig = buf + parse_signature(buf, strlen(buf)); + *siglen = strlen(*sig); + + /* subject is first non-empty line */ + *sub = buf; + /* subject goes to first empty line */ + while (buf < *sig && *buf && *buf != '\n') { + eol = strchrnul(buf, '\n'); + if (*eol) + eol++; + buf = eol; + } + *sublen = buf - *sub; + /* drop trailing newline, if present */ + if (*sublen && (*sub)[*sublen - 1] == '\n') + *sublen -= 1; + + /* skip any empty lines */ + while (*buf == '\n') + buf++; + *body = buf; + *bodylen = strlen(buf); + *nonsiglen = *sig - buf; +} + +/* See grab_values */ +static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; + unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (strcmp(name, "subject") && + strcmp(name, "body") && + strcmp(name, "contents") && + strcmp(name, "contents:subject") && + strcmp(name, "contents:body") && + strcmp(name, "contents:signature")) + continue; + if (!subpos) + find_subpos(buf, sz, + &subpos, &sublen, + &bodypos, &bodylen, &nonsiglen, + &sigpos, &siglen); + + if (!strcmp(name, "subject")) + v->s = copy_subject(subpos, sublen); + else if (!strcmp(name, "contents:subject")) + v->s = copy_subject(subpos, sublen); + else if (!strcmp(name, "body")) + v->s = xmemdupz(bodypos, bodylen); + else if (!strcmp(name, "contents:body")) + v->s = xmemdupz(bodypos, nonsiglen); + else if (!strcmp(name, "contents:signature")) + v->s = xmemdupz(sigpos, siglen); + else if (!strcmp(name, "contents")) + v->s = xstrdup(subpos); + } +} + +/* + * We want to have empty print-string for field requests + * that do not apply (e.g. "authordate" for a tag object) + */ +static void fill_missing_values(struct atom_value *val) +{ + int i; + for (i = 0; i < used_atom_cnt; i++) { + struct atom_value *v = &val[i]; + if (v->s == NULL) + v->s = ""; + } +} + +/* + * val is a list of atom_value to hold returned values. Extract + * the values for atoms in used_atom array out of (obj, buf, sz). + * when deref is false, (obj, buf, sz) is the object that is + * pointed at by the ref itself; otherwise it is the object the + * ref (which is a tag) refers to. + */ +static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + grab_common_values(val, deref, obj, buf, sz); + switch (obj->type) { + case OBJ_TAG: + grab_tag_values(val, deref, obj, buf, sz); + grab_sub_body_contents(val, deref, obj, buf, sz); + grab_person("tagger", val, deref, obj, buf, sz); + break; + case OBJ_COMMIT: + grab_commit_values(val, deref, obj, buf, sz); + grab_sub_body_contents(val, deref, obj, buf, sz); + grab_person("author", val, deref, obj, buf, sz); + grab_person("committer", val, deref, obj, buf, sz); + break; + case OBJ_TREE: + /* grab_tree_values(val, deref, obj, buf, sz); */ + break; + case OBJ_BLOB: + /* grab_blob_values(val, deref, obj, buf, sz); */ + break; + default: + die("Eh? Object of type %d?", obj->type); + } +} + +static inline char *copy_advance(char *dst, const char *src) +{ + while (*src) + *dst++ = *src++; + return dst; +} + +/* + * Parse the object referred by ref, and grab needed value. + */ +static void populate_value(struct refinfo *ref) +{ + void *buf; + struct object *obj; + int eaten, i; + unsigned long size; + const unsigned char *tagged; + + ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt); + + if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { + unsigned char unused1[20]; + ref->symref = resolve_refdup(ref->refname, unused1, 1, NULL); + if (!ref->symref) + ref->symref = ""; + } + + /* Fill in specials first */ + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &ref->value[i]; + int deref = 0; + const char *refname; + const char *formatp; + + if (*name == '*') { + deref = 1; + name++; + } + + if (!prefixcmp(name, "refname")) + refname = ref->refname; + else if (!prefixcmp(name, "symref")) + refname = ref->symref ? ref->symref : ""; + else if (!prefixcmp(name, "upstream")) { + struct branch *branch; + /* only local branches may have an upstream */ + if (prefixcmp(ref->refname, "refs/heads/")) + continue; + branch = branch_get(ref->refname + 11); + + if (!branch || !branch->merge || !branch->merge[0] || + !branch->merge[0]->dst) + continue; + refname = branch->merge[0]->dst; + } + else if (!strcmp(name, "flag")) { + char buf[256], *cp = buf; + if (ref->flag & REF_ISSYMREF) + cp = copy_advance(cp, ",symref"); + if (ref->flag & REF_ISPACKED) + cp = copy_advance(cp, ",packed"); + if (cp == buf) + v->s = ""; + else { + *cp = '\0'; + v->s = xstrdup(buf + 1); + } + continue; + } + else + continue; + + formatp = strchr(name, ':'); + /* look for "short" refname format */ + if (formatp) { + formatp++; + if (!strcmp(formatp, "short")) + refname = shorten_unambiguous_ref(refname, + warn_ambiguous_refs); + else + die("unknown %.*s format %s", + (int)(formatp - name), name, formatp); + } + + if (!deref) + v->s = refname; + else { + int len = strlen(refname); + char *s = xmalloc(len + 4); + sprintf(s, "%s^{}", refname); + v->s = s; + } + } + + for (i = 0; i < used_atom_cnt; i++) { + struct atom_value *v = &ref->value[i]; + if (v->s == NULL) + goto need_obj; + } + return; + + need_obj: + buf = get_obj(ref->objectname, &obj, &size, &eaten); + if (!buf) + die("missing object %s for %s", + sha1_to_hex(ref->objectname), ref->refname); + if (!obj) + die("parse_object_buffer failed on %s for %s", + sha1_to_hex(ref->objectname), ref->refname); + + grab_values(ref->value, 0, obj, buf, size); + if (!eaten) + free(buf); + + /* + * If there is no atom that wants to know about tagged + * object, we are done. + */ + if (!need_tagged || (obj->type != OBJ_TAG)) + return; + + /* + * If it is a tag object, see if we use a value that derefs + * the object, and if we do grab the object it refers to. + */ + tagged = ((struct tag *)obj)->tagged->sha1; + + /* + * NEEDSWORK: This derefs tag only once, which + * is good to deal with chains of trust, but + * is not consistent with what deref_tag() does + * which peels the onion to the core. + */ + buf = get_obj(tagged, &obj, &size, &eaten); + if (!buf) + die("missing object %s for %s", + sha1_to_hex(tagged), ref->refname); + if (!obj) + die("parse_object_buffer failed on %s for %s", + sha1_to_hex(tagged), ref->refname); + grab_values(ref->value, 1, obj, buf, size); + if (!eaten) + free(buf); +} + +/* + * Given a ref, return the value for the atom. This lazily gets value + * out of the object by calling populate value. + */ +static void get_value(struct refinfo *ref, int atom, struct atom_value **v) +{ + if (!ref->value) { + populate_value(ref); + fill_missing_values(ref->value); + } + *v = &ref->value[atom]; +} + +struct grab_ref_cbdata { + struct refinfo **grab_array; + const char **grab_pattern; + int grab_cnt; +}; + +/* + * A call-back given to for_each_ref(). Filter refs and keep them for + * later object processing. + */ +static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + struct grab_ref_cbdata *cb = cb_data; + struct refinfo *ref; + int cnt; + + if (*cb->grab_pattern) { + const char **pattern; + int namelen = strlen(refname); + for (pattern = cb->grab_pattern; *pattern; pattern++) { + const char *p = *pattern; + int plen = strlen(p); + + if ((plen <= namelen) && + !strncmp(refname, p, plen) && + (refname[plen] == '\0' || + refname[plen] == '/' || + p[plen-1] == '/')) + break; + if (!fnmatch(p, refname, FNM_PATHNAME)) + break; + } + if (!*pattern) + return 0; + } + + /* + * We do not open the object yet; sort may only need refname + * to do its job and the resulting list may yet to be pruned + * by maxcount logic. + */ + ref = xcalloc(1, sizeof(*ref)); + ref->refname = xstrdup(refname); + hashcpy(ref->objectname, sha1); + ref->flag = flag; + + cnt = cb->grab_cnt; + cb->grab_array = xrealloc(cb->grab_array, + sizeof(*cb->grab_array) * (cnt + 1)); + cb->grab_array[cnt++] = ref; + cb->grab_cnt = cnt; + return 0; +} + +static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b) +{ + struct atom_value *va, *vb; + int cmp; + cmp_type cmp_type = used_atom_type[s->atom]; + + get_value(a, s->atom, &va); + get_value(b, s->atom, &vb); + switch (cmp_type) { + case FIELD_STR: + cmp = strcmp(va->s, vb->s); + break; + default: + if (va->ul < vb->ul) + cmp = -1; + else if (va->ul == vb->ul) + cmp = 0; + else + cmp = 1; + break; + } + return (s->reverse) ? -cmp : cmp; +} + +static struct ref_sort *ref_sort; +static int compare_refs(const void *a_, const void *b_) +{ + struct refinfo *a = *((struct refinfo **)a_); + struct refinfo *b = *((struct refinfo **)b_); + struct ref_sort *s; + + for (s = ref_sort; s; s = s->next) { + int cmp = cmp_ref_sort(s, a, b); + if (cmp) + return cmp; + } + return 0; +} + +static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs) +{ + ref_sort = sort; + qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs); +} + +static void print_value(struct refinfo *ref, int atom, int quote_style) +{ + struct atom_value *v; + get_value(ref, atom, &v); + switch (quote_style) { + case QUOTE_NONE: + fputs(v->s, stdout); + break; + case QUOTE_SHELL: + sq_quote_print(stdout, v->s); + break; + case QUOTE_PERL: + perl_quote_print(stdout, v->s); + break; + case QUOTE_PYTHON: + python_quote_print(stdout, v->s); + break; + case QUOTE_TCL: + tcl_quote_print(stdout, v->s); + break; + } +} + +static int hex1(char ch) +{ + if ('0' <= ch && ch <= '9') + return ch - '0'; + else if ('a' <= ch && ch <= 'f') + return ch - 'a' + 10; + else if ('A' <= ch && ch <= 'F') + return ch - 'A' + 10; + return -1; +} +static int hex2(const char *cp) +{ + if (cp[0] && cp[1]) + return (hex1(cp[0]) << 4) | hex1(cp[1]); + else + return -1; +} + +static void emit(const char *cp, const char *ep) +{ + while (*cp && (!ep || cp < ep)) { + if (*cp == '%') { + if (cp[1] == '%') + cp++; + else { + int ch = hex2(cp + 1); + if (0 <= ch) { + putchar(ch); + cp += 3; + continue; + } + } + } + putchar(*cp); + cp++; + } +} + +static void show_ref(struct refinfo *info, const char *format, int quote_style) +{ + const char *cp, *sp, *ep; + + for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) { + ep = strchr(sp, ')'); + if (cp < sp) + emit(cp, sp); + print_value(info, parse_atom(sp + 2, ep), quote_style); + } + if (*cp) { + sp = cp + strlen(cp); + emit(cp, sp); + } + putchar('\n'); +} + +static struct ref_sort *default_sort(void) +{ + static const char cstr_name[] = "refname"; + + struct ref_sort *sort = xcalloc(1, sizeof(*sort)); + + sort->next = NULL; + sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name)); + return sort; +} + +static int opt_parse_sort(const struct option *opt, const char *arg, int unset) +{ + struct ref_sort **sort_tail = opt->value; + struct ref_sort *s; + int len; + + if (!arg) /* should --no-sort void the list ? */ + return -1; + + s = xcalloc(1, sizeof(*s)); + s->next = *sort_tail; + *sort_tail = s; + + if (*arg == '-') { + s->reverse = 1; + arg++; + } + len = strlen(arg); + s->atom = parse_atom(arg, arg+len); + return 0; +} + +static char const * const for_each_ref_usage[] = { + N_("git for-each-ref [options] [<pattern>]"), + NULL +}; + +int cmd_for_each_ref(int argc, const char **argv, const char *prefix) +{ + int i, num_refs; + const char *format = "%(objectname) %(objecttype)\t%(refname)"; + struct ref_sort *sort = NULL, **sort_tail = &sort; + int maxcount = 0, quote_style = 0; + struct refinfo **refs; + struct grab_ref_cbdata cbdata; + + struct option opts[] = { + OPT_BIT('s', "shell", "e_style, + N_("quote placeholders suitably for shells"), QUOTE_SHELL), + OPT_BIT('p', "perl", "e_style, + N_("quote placeholders suitably for perl"), QUOTE_PERL), + OPT_BIT(0 , "python", "e_style, + N_("quote placeholders suitably for python"), QUOTE_PYTHON), + OPT_BIT(0 , "tcl", "e_style, + N_("quote placeholders suitably for tcl"), QUOTE_TCL), + + OPT_GROUP(""), + OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")), + OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")), + OPT_CALLBACK(0 , "sort", sort_tail, N_("key"), + N_("field name to sort on"), &opt_parse_sort), + OPT_END(), + }; + + parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0); + if (maxcount < 0) { + error("invalid --count argument: `%d'", maxcount); + usage_with_options(for_each_ref_usage, opts); + } + if (HAS_MULTI_BITS(quote_style)) { + error("more than one quoting style?"); + usage_with_options(for_each_ref_usage, opts); + } + if (verify_format(format)) + usage_with_options(for_each_ref_usage, opts); + + if (!sort) + sort = default_sort(); + sort_atom_limit = used_atom_cnt; + + /* for warn_ambiguous_refs */ + git_config(git_default_config, NULL); + + memset(&cbdata, 0, sizeof(cbdata)); + cbdata.grab_pattern = argv; + for_each_rawref(grab_single_ref, &cbdata); + refs = cbdata.grab_array; + num_refs = cbdata.grab_cnt; + + sort_refs(sort, refs, num_refs); + + if (!maxcount || num_refs < maxcount) + maxcount = num_refs; + for (i = 0; i < maxcount; i++) + show_ref(refs[i], format, quote_style); + return 0; +} diff --git a/builtin/fsck.c b/builtin/fsck.c new file mode 100644 index 0000000000..bb9a2cd447 --- /dev/null +++ b/builtin/fsck.c @@ -0,0 +1,738 @@ +#include "builtin.h" +#include "cache.h" +#include "commit.h" +#include "tree.h" +#include "blob.h" +#include "tag.h" +#include "refs.h" +#include "pack.h" +#include "cache-tree.h" +#include "tree-walk.h" +#include "fsck.h" +#include "parse-options.h" +#include "dir.h" +#include "progress.h" +#include "streaming.h" + +#define REACHABLE 0x0001 +#define SEEN 0x0002 + +static int show_root; +static int show_tags; +static int show_unreachable; +static int include_reflogs = 1; +static int check_full = 1; +static int check_strict; +static int keep_cache_objects; +static unsigned char head_sha1[20]; +static const char *head_points_at; +static int errors_found; +static int write_lost_and_found; +static int verbose; +static int show_progress = -1; +static int show_dangling = 1; +#define ERROR_OBJECT 01 +#define ERROR_REACHABLE 02 +#define ERROR_PACK 04 + +#ifdef NO_D_INO_IN_DIRENT +#define SORT_DIRENT 0 +#define DIRENT_SORT_HINT(de) 0 +#else +#define SORT_DIRENT 1 +#define DIRENT_SORT_HINT(de) ((de)->d_ino) +#endif + +static void objreport(struct object *obj, const char *severity, + const char *err, va_list params) +{ + fprintf(stderr, "%s in %s %s: ", + severity, typename(obj->type), sha1_to_hex(obj->sha1)); + vfprintf(stderr, err, params); + fputs("\n", stderr); +} + +__attribute__((format (printf, 2, 3))) +static int objerror(struct object *obj, const char *err, ...) +{ + va_list params; + va_start(params, err); + errors_found |= ERROR_OBJECT; + objreport(obj, "error", err, params); + va_end(params); + return -1; +} + +__attribute__((format (printf, 3, 4))) +static int fsck_error_func(struct object *obj, int type, const char *err, ...) +{ + va_list params; + va_start(params, err); + objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params); + va_end(params); + return (type == FSCK_WARN) ? 0 : 1; +} + +static struct object_array pending; + +static int mark_object(struct object *obj, int type, void *data) +{ + struct object *parent = data; + + /* + * The only case data is NULL or type is OBJ_ANY is when + * mark_object_reachable() calls us. All the callers of + * that function has non-NULL obj hence ... + */ + if (!obj) { + /* ... these references to parent->fld are safe here */ + printf("broken link from %7s %s\n", + typename(parent->type), sha1_to_hex(parent->sha1)); + printf("broken link from %7s %s\n", + (type == OBJ_ANY ? "unknown" : typename(type)), "unknown"); + errors_found |= ERROR_REACHABLE; + return 1; + } + + if (type != OBJ_ANY && obj->type != type) + /* ... and the reference to parent is safe here */ + objerror(parent, "wrong object type in link"); + + if (obj->flags & REACHABLE) + return 0; + obj->flags |= REACHABLE; + if (!obj->parsed) { + if (parent && !has_sha1_file(obj->sha1)) { + printf("broken link from %7s %s\n", + typename(parent->type), sha1_to_hex(parent->sha1)); + printf(" to %7s %s\n", + typename(obj->type), sha1_to_hex(obj->sha1)); + errors_found |= ERROR_REACHABLE; + } + return 1; + } + + add_object_array(obj, (void *) parent, &pending); + return 0; +} + +static void mark_object_reachable(struct object *obj) +{ + mark_object(obj, OBJ_ANY, NULL); +} + +static int traverse_one_object(struct object *obj) +{ + int result; + struct tree *tree = NULL; + + if (obj->type == OBJ_TREE) { + obj->parsed = 0; + tree = (struct tree *)obj; + if (parse_tree(tree) < 0) + return 1; /* error already displayed */ + } + result = fsck_walk(obj, mark_object, obj); + if (tree) { + free(tree->buffer); + tree->buffer = NULL; + } + return result; +} + +static int traverse_reachable(void) +{ + struct progress *progress = NULL; + unsigned int nr = 0; + int result = 0; + if (show_progress) + progress = start_progress_delay("Checking connectivity", 0, 0, 2); + while (pending.nr) { + struct object_array_entry *entry; + struct object *obj; + + entry = pending.objects + --pending.nr; + obj = entry->item; + result |= traverse_one_object(obj); + display_progress(progress, ++nr); + } + stop_progress(&progress); + return !!result; +} + +static int mark_used(struct object *obj, int type, void *data) +{ + if (!obj) + return 1; + obj->used = 1; + return 0; +} + +/* + * Check a single reachable object + */ +static void check_reachable_object(struct object *obj) +{ + /* + * We obviously want the object to be parsed, + * except if it was in a pack-file and we didn't + * do a full fsck + */ + if (!obj->parsed) { + if (has_sha1_pack(obj->sha1)) + return; /* it is in pack - forget about it */ + printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); + errors_found |= ERROR_REACHABLE; + return; + } +} + +/* + * Check a single unreachable object + */ +static void check_unreachable_object(struct object *obj) +{ + /* + * Missing unreachable object? Ignore it. It's not like + * we miss it (since it can't be reached), nor do we want + * to complain about it being unreachable (since it does + * not exist). + */ + if (!obj->parsed) + return; + + /* + * Unreachable object that exists? Show it if asked to, + * since this is something that is prunable. + */ + if (show_unreachable) { + printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); + return; + } + + /* + * "!used" means that nothing at all points to it, including + * other unreachable objects. In other words, it's the "tip" + * of some set of unreachable objects, usually a commit that + * got dropped. + * + * Such starting points are more interesting than some random + * set of unreachable objects, so we show them even if the user + * hasn't asked for _all_ unreachable objects. If you have + * deleted a branch by mistake, this is a prime candidate to + * start looking at, for example. + */ + if (!obj->used) { + if (show_dangling) + printf("dangling %s %s\n", typename(obj->type), + sha1_to_hex(obj->sha1)); + if (write_lost_and_found) { + char *filename = git_path("lost-found/%s/%s", + obj->type == OBJ_COMMIT ? "commit" : "other", + sha1_to_hex(obj->sha1)); + FILE *f; + + if (safe_create_leading_directories(filename)) { + error("Could not create lost-found"); + return; + } + if (!(f = fopen(filename, "w"))) + die_errno("Could not open '%s'", filename); + if (obj->type == OBJ_BLOB) { + if (stream_blob_to_fd(fileno(f), obj->sha1, NULL, 1)) + die_errno("Could not write '%s'", filename); + } else + fprintf(f, "%s\n", sha1_to_hex(obj->sha1)); + if (fclose(f)) + die_errno("Could not finish '%s'", + filename); + } + return; + } + + /* + * Otherwise? It's there, it's unreachable, and some other unreachable + * object points to it. Ignore it - it's not interesting, and we showed + * all the interesting cases above. + */ +} + +static void check_object(struct object *obj) +{ + if (verbose) + fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1)); + + if (obj->flags & REACHABLE) + check_reachable_object(obj); + else + check_unreachable_object(obj); +} + +static void check_connectivity(void) +{ + int i, max; + + /* Traverse the pending reachable objects */ + traverse_reachable(); + + /* Look up all the requirements, warn about missing objects.. */ + max = get_max_object_index(); + if (verbose) + fprintf(stderr, "Checking connectivity (%d objects)\n", max); + + for (i = 0; i < max; i++) { + struct object *obj = get_indexed_object(i); + + if (obj) + check_object(obj); + } +} + +static int fsck_obj(struct object *obj) +{ + if (obj->flags & SEEN) + return 0; + obj->flags |= SEEN; + + if (verbose) + fprintf(stderr, "Checking %s %s\n", + typename(obj->type), sha1_to_hex(obj->sha1)); + + if (fsck_walk(obj, mark_used, NULL)) + objerror(obj, "broken links"); + if (fsck_object(obj, check_strict, fsck_error_func)) + return -1; + + if (obj->type == OBJ_TREE) { + struct tree *item = (struct tree *) obj; + + free(item->buffer); + item->buffer = NULL; + } + + if (obj->type == OBJ_COMMIT) { + struct commit *commit = (struct commit *) obj; + + free(commit->buffer); + commit->buffer = NULL; + + if (!commit->parents && show_root) + printf("root %s\n", sha1_to_hex(commit->object.sha1)); + } + + if (obj->type == OBJ_TAG) { + struct tag *tag = (struct tag *) obj; + + if (show_tags && tag->tagged) { + printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1)); + printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1)); + } + } + + return 0; +} + +static int fsck_sha1(const unsigned char *sha1) +{ + struct object *obj = parse_object(sha1); + if (!obj) { + errors_found |= ERROR_OBJECT; + return error("%s: object corrupt or missing", + sha1_to_hex(sha1)); + } + return fsck_obj(obj); +} + +static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type, + unsigned long size, void *buffer, int *eaten) +{ + struct object *obj; + obj = parse_object_buffer(sha1, type, size, buffer, eaten); + if (!obj) { + errors_found |= ERROR_OBJECT; + return error("%s: object corrupt or missing", sha1_to_hex(sha1)); + } + return fsck_obj(obj); +} + +/* + * This is the sorting chunk size: make it reasonably + * big so that we can sort well.. + */ +#define MAX_SHA1_ENTRIES (1024) + +struct sha1_entry { + unsigned long ino; + unsigned char sha1[20]; +}; + +static struct { + unsigned long nr; + struct sha1_entry *entry[MAX_SHA1_ENTRIES]; +} sha1_list; + +static int ino_compare(const void *_a, const void *_b) +{ + const struct sha1_entry *a = _a, *b = _b; + unsigned long ino1 = a->ino, ino2 = b->ino; + return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0; +} + +static void fsck_sha1_list(void) +{ + int i, nr = sha1_list.nr; + + if (SORT_DIRENT) + qsort(sha1_list.entry, nr, + sizeof(struct sha1_entry *), ino_compare); + for (i = 0; i < nr; i++) { + struct sha1_entry *entry = sha1_list.entry[i]; + unsigned char *sha1 = entry->sha1; + + sha1_list.entry[i] = NULL; + fsck_sha1(sha1); + free(entry); + } + sha1_list.nr = 0; +} + +static void add_sha1_list(unsigned char *sha1, unsigned long ino) +{ + struct sha1_entry *entry = xmalloc(sizeof(*entry)); + int nr; + + entry->ino = ino; + hashcpy(entry->sha1, sha1); + nr = sha1_list.nr; + if (nr == MAX_SHA1_ENTRIES) { + fsck_sha1_list(); + nr = 0; + } + sha1_list.entry[nr] = entry; + sha1_list.nr = ++nr; +} + +static inline int is_loose_object_file(struct dirent *de, + char *name, unsigned char *sha1) +{ + if (strlen(de->d_name) != 38) + return 0; + memcpy(name + 2, de->d_name, 39); + return !get_sha1_hex(name, sha1); +} + +static void fsck_dir(int i, char *path) +{ + DIR *dir = opendir(path); + struct dirent *de; + char name[100]; + + if (!dir) + return; + + if (verbose) + fprintf(stderr, "Checking directory %s\n", path); + + sprintf(name, "%02x", i); + while ((de = readdir(dir)) != NULL) { + unsigned char sha1[20]; + + if (is_dot_or_dotdot(de->d_name)) + continue; + if (is_loose_object_file(de, name, sha1)) { + add_sha1_list(sha1, DIRENT_SORT_HINT(de)); + continue; + } + if (!prefixcmp(de->d_name, "tmp_obj_")) + continue; + fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); + } + closedir(dir); +} + +static int default_refs; + +static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, + const char *email, unsigned long timestamp, int tz, + const char *message, void *cb_data) +{ + struct object *obj; + + if (verbose) + fprintf(stderr, "Checking reflog %s->%s\n", + sha1_to_hex(osha1), sha1_to_hex(nsha1)); + + if (!is_null_sha1(osha1)) { + obj = lookup_object(osha1); + if (obj) { + obj->used = 1; + mark_object_reachable(obj); + } + } + obj = lookup_object(nsha1); + if (obj) { + obj->used = 1; + mark_object_reachable(obj); + } + return 0; +} + +static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data) +{ + for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL); + return 0; +} + +static int is_branch(const char *refname) +{ + return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/"); +} + +static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + struct object *obj; + + obj = parse_object(sha1); + if (!obj) { + error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1)); + /* We'll continue with the rest despite the error.. */ + return 0; + } + if (obj->type != OBJ_COMMIT && is_branch(refname)) + error("%s: not a commit", refname); + default_refs++; + obj->used = 1; + mark_object_reachable(obj); + + return 0; +} + +static void get_default_heads(void) +{ + if (head_points_at && !is_null_sha1(head_sha1)) + fsck_handle_ref("HEAD", head_sha1, 0, NULL); + for_each_ref(fsck_handle_ref, NULL); + if (include_reflogs) + for_each_reflog(fsck_handle_reflog, NULL); + + /* + * Not having any default heads isn't really fatal, but + * it does mean that "--unreachable" no longer makes any + * sense (since in this case everything will obviously + * be unreachable by definition. + * + * Showing dangling objects is valid, though (as those + * dangling objects are likely lost heads). + * + * So we just print a warning about it, and clear the + * "show_unreachable" flag. + */ + if (!default_refs) { + fprintf(stderr, "notice: No default references\n"); + show_unreachable = 0; + } +} + +static void fsck_object_dir(const char *path) +{ + int i; + struct progress *progress = NULL; + + if (verbose) + fprintf(stderr, "Checking object directory\n"); + + if (show_progress) + progress = start_progress("Checking object directories", 256); + for (i = 0; i < 256; i++) { + static char dir[4096]; + sprintf(dir, "%s/%02x", path, i); + fsck_dir(i, dir); + display_progress(progress, i+1); + } + stop_progress(&progress); + fsck_sha1_list(); +} + +static int fsck_head_link(void) +{ + int flag; + int null_is_error = 0; + + if (verbose) + fprintf(stderr, "Checking HEAD link\n"); + + head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag); + if (!head_points_at) + return error("Invalid HEAD"); + if (!strcmp(head_points_at, "HEAD")) + /* detached HEAD */ + null_is_error = 1; + else if (prefixcmp(head_points_at, "refs/heads/")) + return error("HEAD points to something strange (%s)", + head_points_at); + if (is_null_sha1(head_sha1)) { + if (null_is_error) + return error("HEAD: detached HEAD points at nothing"); + fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n", + head_points_at + 11); + } + return 0; +} + +static int fsck_cache_tree(struct cache_tree *it) +{ + int i; + int err = 0; + + if (verbose) + fprintf(stderr, "Checking cache tree\n"); + + if (0 <= it->entry_count) { + struct object *obj = parse_object(it->sha1); + if (!obj) { + error("%s: invalid sha1 pointer in cache-tree", + sha1_to_hex(it->sha1)); + return 1; + } + obj->used = 1; + mark_object_reachable(obj); + if (obj->type != OBJ_TREE) + err |= objerror(obj, "non-tree in cache-tree"); + } + for (i = 0; i < it->subtree_nr; i++) + err |= fsck_cache_tree(it->down[i]->cache_tree); + return err; +} + +static char const * const fsck_usage[] = { + N_("git fsck [options] [<object>...]"), + NULL +}; + +static struct option fsck_opts[] = { + OPT__VERBOSE(&verbose, N_("be verbose")), + OPT_BOOLEAN(0, "unreachable", &show_unreachable, N_("show unreachable objects")), + OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")), + OPT_BOOLEAN(0, "tags", &show_tags, N_("report tags")), + OPT_BOOLEAN(0, "root", &show_root, N_("report root nodes")), + OPT_BOOLEAN(0, "cache", &keep_cache_objects, N_("make index objects head nodes")), + OPT_BOOLEAN(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")), + OPT_BOOLEAN(0, "full", &check_full, N_("also consider packs and alternate objects")), + OPT_BOOLEAN(0, "strict", &check_strict, N_("enable more strict checking")), + OPT_BOOLEAN(0, "lost-found", &write_lost_and_found, + N_("write dangling objects in .git/lost-found")), + OPT_BOOL(0, "progress", &show_progress, N_("show progress")), + OPT_END(), +}; + +int cmd_fsck(int argc, const char **argv, const char *prefix) +{ + int i, heads; + struct alternate_object_database *alt; + + errors_found = 0; + read_replace_refs = 0; + + argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0); + + if (show_progress == -1) + show_progress = isatty(2); + if (verbose) + show_progress = 0; + + if (write_lost_and_found) { + check_full = 1; + include_reflogs = 0; + } + + fsck_head_link(); + fsck_object_dir(get_object_directory()); + + prepare_alt_odb(); + for (alt = alt_odb_list; alt; alt = alt->next) { + char namebuf[PATH_MAX]; + int namelen = alt->name - alt->base; + memcpy(namebuf, alt->base, namelen); + namebuf[namelen - 1] = 0; + fsck_object_dir(namebuf); + } + + if (check_full) { + struct packed_git *p; + uint32_t total = 0, count = 0; + struct progress *progress = NULL; + + prepare_packed_git(); + + if (show_progress) { + for (p = packed_git; p; p = p->next) { + if (open_pack_index(p)) + continue; + total += p->num_objects; + } + + progress = start_progress("Checking objects", total); + } + for (p = packed_git; p; p = p->next) { + /* verify gives error messages itself */ + if (verify_pack(p, fsck_obj_buffer, + progress, count)) + errors_found |= ERROR_PACK; + count += p->num_objects; + } + stop_progress(&progress); + } + + heads = 0; + for (i = 0; i < argc; i++) { + const char *arg = argv[i]; + unsigned char sha1[20]; + if (!get_sha1(arg, sha1)) { + struct object *obj = lookup_object(sha1); + + /* Error is printed by lookup_object(). */ + if (!obj) + continue; + + obj->used = 1; + mark_object_reachable(obj); + heads++; + continue; + } + error("invalid parameter: expected sha1, got '%s'", arg); + } + + /* + * If we've not been given any explicit head information, do the + * default ones from .git/refs. We also consider the index file + * in this case (ie this implies --cache). + */ + if (!heads) { + get_default_heads(); + keep_cache_objects = 1; + } + + if (keep_cache_objects) { + read_cache(); + for (i = 0; i < active_nr; i++) { + unsigned int mode; + struct blob *blob; + struct object *obj; + + mode = active_cache[i]->ce_mode; + if (S_ISGITLINK(mode)) + continue; + blob = lookup_blob(active_cache[i]->sha1); + if (!blob) + continue; + obj = &blob->object; + obj->used = 1; + mark_object_reachable(obj); + } + if (active_cache_tree) + fsck_cache_tree(active_cache_tree); + } + + check_connectivity(); + return errors_found; +} diff --git a/builtin/gc.c b/builtin/gc.c new file mode 100644 index 0000000000..6be6c8d65b --- /dev/null +++ b/builtin/gc.c @@ -0,0 +1,253 @@ +/* + * git gc builtin command + * + * Cleanup unreachable files and optimize the repository. + * + * Copyright (c) 2007 James Bowes + * + * Based on git-gc.sh, which is + * + * Copyright (c) 2006 Shawn O. Pearce + */ + +#include "builtin.h" +#include "cache.h" +#include "parse-options.h" +#include "run-command.h" +#include "argv-array.h" + +#define FAILED_RUN "failed to run %s" + +static const char * const builtin_gc_usage[] = { + N_("git gc [options]"), + NULL +}; + +static int pack_refs = 1; +static int aggressive_window = 250; +static int gc_auto_threshold = 6700; +static int gc_auto_pack_limit = 50; +static const char *prune_expire = "2.weeks.ago"; + +static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT; +static struct argv_array reflog = ARGV_ARRAY_INIT; +static struct argv_array repack = ARGV_ARRAY_INIT; +static struct argv_array prune = ARGV_ARRAY_INIT; +static struct argv_array rerere = ARGV_ARRAY_INIT; + +static int gc_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "gc.packrefs")) { + if (value && !strcmp(value, "notbare")) + pack_refs = -1; + else + pack_refs = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "gc.aggressivewindow")) { + aggressive_window = git_config_int(var, value); + return 0; + } + if (!strcmp(var, "gc.auto")) { + gc_auto_threshold = git_config_int(var, value); + return 0; + } + if (!strcmp(var, "gc.autopacklimit")) { + gc_auto_pack_limit = git_config_int(var, value); + return 0; + } + if (!strcmp(var, "gc.pruneexpire")) { + if (value && strcmp(value, "now")) { + unsigned long now = approxidate("now"); + if (approxidate(value) >= now) + return error(_("Invalid %s: '%s'"), var, value); + } + return git_config_string(&prune_expire, var, value); + } + return git_default_config(var, value, cb); +} + +static int too_many_loose_objects(void) +{ + /* + * Quickly check if a "gc" is needed, by estimating how + * many loose objects there are. Because SHA-1 is evenly + * distributed, we can check only one and get a reasonable + * estimate. + */ + char path[PATH_MAX]; + const char *objdir = get_object_directory(); + DIR *dir; + struct dirent *ent; + int auto_threshold; + int num_loose = 0; + int needed = 0; + + if (gc_auto_threshold <= 0) + return 0; + + if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) { + warning(_("insanely long object directory %.*s"), 50, objdir); + return 0; + } + dir = opendir(path); + if (!dir) + return 0; + + auto_threshold = (gc_auto_threshold + 255) / 256; + while ((ent = readdir(dir)) != NULL) { + if (strspn(ent->d_name, "0123456789abcdef") != 38 || + ent->d_name[38] != '\0') + continue; + if (++num_loose > auto_threshold) { + needed = 1; + break; + } + } + closedir(dir); + return needed; +} + +static int too_many_packs(void) +{ + struct packed_git *p; + int cnt; + + if (gc_auto_pack_limit <= 0) + return 0; + + prepare_packed_git(); + for (cnt = 0, p = packed_git; p; p = p->next) { + if (!p->pack_local) + continue; + if (p->pack_keep) + continue; + /* + * Perhaps check the size of the pack and count only + * very small ones here? + */ + cnt++; + } + return gc_auto_pack_limit <= cnt; +} + +static void add_repack_all_option(void) +{ + if (prune_expire && !strcmp(prune_expire, "now")) + argv_array_push(&repack, "-a"); + else { + argv_array_push(&repack, "-A"); + if (prune_expire) + argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire); + } +} + +static int need_to_gc(void) +{ + /* + * Setting gc.auto to 0 or negative can disable the + * automatic gc. + */ + if (gc_auto_threshold <= 0) + return 0; + + /* + * If there are too many loose objects, but not too many + * packs, we run "repack -d -l". If there are too many packs, + * we run "repack -A -d -l". Otherwise we tell the caller + * there is no need. + */ + if (too_many_packs()) + add_repack_all_option(); + else if (!too_many_loose_objects()) + return 0; + + if (run_hook(NULL, "pre-auto-gc", NULL)) + return 0; + return 1; +} + +int cmd_gc(int argc, const char **argv, const char *prefix) +{ + int aggressive = 0; + int auto_gc = 0; + int quiet = 0; + + struct option builtin_gc_options[] = { + OPT__QUIET(&quiet, N_("suppress progress reporting")), + { OPTION_STRING, 0, "prune", &prune_expire, N_("date"), + N_("prune unreferenced objects"), + PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire }, + OPT_BOOLEAN(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")), + OPT_BOOLEAN(0, "auto", &auto_gc, N_("enable auto-gc mode")), + OPT_END() + }; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(builtin_gc_usage, builtin_gc_options); + + argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL); + argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL); + argv_array_pushl(&repack, "repack", "-d", "-l", NULL); + argv_array_pushl(&prune, "prune", "--expire", NULL ); + argv_array_pushl(&rerere, "rerere", "gc", NULL); + + git_config(gc_config, NULL); + + if (pack_refs < 0) + pack_refs = !is_bare_repository(); + + argc = parse_options(argc, argv, prefix, builtin_gc_options, + builtin_gc_usage, 0); + if (argc > 0) + usage_with_options(builtin_gc_usage, builtin_gc_options); + + if (aggressive) { + argv_array_push(&repack, "-f"); + argv_array_push(&repack, "--depth=250"); + if (aggressive_window > 0) + argv_array_pushf(&repack, "--window=%d", aggressive_window); + } + if (quiet) + argv_array_push(&repack, "-q"); + + if (auto_gc) { + /* + * Auto-gc should be least intrusive as possible. + */ + if (!need_to_gc()) + return 0; + if (!quiet) + fprintf(stderr, + _("Auto packing the repository for optimum performance. You may also\n" + "run \"git gc\" manually. See " + "\"git help gc\" for more information.\n")); + } else + add_repack_all_option(); + + if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD)) + return error(FAILED_RUN, pack_refs_cmd.argv[0]); + + if (run_command_v_opt(reflog.argv, RUN_GIT_CMD)) + return error(FAILED_RUN, reflog.argv[0]); + + if (run_command_v_opt(repack.argv, RUN_GIT_CMD)) + return error(FAILED_RUN, repack.argv[0]); + + if (prune_expire) { + argv_array_push(&prune, prune_expire); + if (quiet) + argv_array_push(&prune, "--no-progress"); + if (run_command_v_opt(prune.argv, RUN_GIT_CMD)) + return error(FAILED_RUN, prune.argv[0]); + } + + if (run_command_v_opt(rerere.argv, RUN_GIT_CMD)) + return error(FAILED_RUN, rerere.argv[0]); + + if (auto_gc && too_many_loose_objects()) + warning(_("There are too many unreachable loose objects; " + "run 'git prune' to remove them.")); + + return 0; +} diff --git a/builtin/grep.c b/builtin/grep.c new file mode 100644 index 0000000000..0e1b6c860e --- /dev/null +++ b/builtin/grep.c @@ -0,0 +1,915 @@ +/* + * Builtin "git grep" + * + * Copyright (c) 2006 Junio C Hamano + */ +#include "cache.h" +#include "blob.h" +#include "tree.h" +#include "commit.h" +#include "tag.h" +#include "tree-walk.h" +#include "builtin.h" +#include "parse-options.h" +#include "string-list.h" +#include "run-command.h" +#include "userdiff.h" +#include "grep.h" +#include "quote.h" +#include "dir.h" + +static char const * const grep_usage[] = { + N_("git grep [options] [-e] <pattern> [<rev>...] [[--] <path>...]"), + NULL +}; + +static int use_threads = 1; + +#ifndef NO_PTHREADS +#define THREADS 8 +static pthread_t threads[THREADS]; + +/* We use one producer thread and THREADS consumer + * threads. The producer adds struct work_items to 'todo' and the + * consumers pick work items from the same array. + */ +struct work_item { + struct grep_source source; + char done; + struct strbuf out; +}; + +/* In the range [todo_done, todo_start) in 'todo' we have work_items + * that have been or are processed by a consumer thread. We haven't + * written the result for these to stdout yet. + * + * The work_items in [todo_start, todo_end) are waiting to be picked + * up by a consumer thread. + * + * The ranges are modulo TODO_SIZE. + */ +#define TODO_SIZE 128 +static struct work_item todo[TODO_SIZE]; +static int todo_start; +static int todo_end; +static int todo_done; + +/* Has all work items been added? */ +static int all_work_added; + +/* This lock protects all the variables above. */ +static pthread_mutex_t grep_mutex; + +static inline void grep_lock(void) +{ + if (use_threads) + pthread_mutex_lock(&grep_mutex); +} + +static inline void grep_unlock(void) +{ + if (use_threads) + pthread_mutex_unlock(&grep_mutex); +} + +/* Signalled when a new work_item is added to todo. */ +static pthread_cond_t cond_add; + +/* Signalled when the result from one work_item is written to + * stdout. + */ +static pthread_cond_t cond_write; + +/* Signalled when we are finished with everything. */ +static pthread_cond_t cond_result; + +static int skip_first_line; + +static void add_work(struct grep_opt *opt, enum grep_source_type type, + const char *name, const char *path, const void *id) +{ + grep_lock(); + + while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { + pthread_cond_wait(&cond_write, &grep_mutex); + } + + grep_source_init(&todo[todo_end].source, type, name, path, id); + if (opt->binary != GREP_BINARY_TEXT) + grep_source_load_driver(&todo[todo_end].source); + todo[todo_end].done = 0; + strbuf_reset(&todo[todo_end].out); + todo_end = (todo_end + 1) % ARRAY_SIZE(todo); + + pthread_cond_signal(&cond_add); + grep_unlock(); +} + +static struct work_item *get_work(void) +{ + struct work_item *ret; + + grep_lock(); + while (todo_start == todo_end && !all_work_added) { + pthread_cond_wait(&cond_add, &grep_mutex); + } + + if (todo_start == todo_end && all_work_added) { + ret = NULL; + } else { + ret = &todo[todo_start]; + todo_start = (todo_start + 1) % ARRAY_SIZE(todo); + } + grep_unlock(); + return ret; +} + +static void work_done(struct work_item *w) +{ + int old_done; + + grep_lock(); + w->done = 1; + old_done = todo_done; + for(; todo[todo_done].done && todo_done != todo_start; + todo_done = (todo_done+1) % ARRAY_SIZE(todo)) { + w = &todo[todo_done]; + if (w->out.len) { + const char *p = w->out.buf; + size_t len = w->out.len; + + /* Skip the leading hunk mark of the first file. */ + if (skip_first_line) { + while (len) { + len--; + if (*p++ == '\n') + break; + } + skip_first_line = 0; + } + + write_or_die(1, p, len); + } + grep_source_clear(&w->source); + } + + if (old_done != todo_done) + pthread_cond_signal(&cond_write); + + if (all_work_added && todo_done == todo_end) + pthread_cond_signal(&cond_result); + + grep_unlock(); +} + +static void *run(void *arg) +{ + int hit = 0; + struct grep_opt *opt = arg; + + while (1) { + struct work_item *w = get_work(); + if (!w) + break; + + opt->output_priv = w; + hit |= grep_source(opt, &w->source); + grep_source_clear_data(&w->source); + work_done(w); + } + free_grep_patterns(arg); + free(arg); + + return (void*) (intptr_t) hit; +} + +static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size) +{ + struct work_item *w = opt->output_priv; + strbuf_add(&w->out, buf, size); +} + +static void start_threads(struct grep_opt *opt) +{ + int i; + + pthread_mutex_init(&grep_mutex, NULL); + pthread_mutex_init(&grep_read_mutex, NULL); + pthread_mutex_init(&grep_attr_mutex, NULL); + pthread_cond_init(&cond_add, NULL); + pthread_cond_init(&cond_write, NULL); + pthread_cond_init(&cond_result, NULL); + grep_use_locks = 1; + + for (i = 0; i < ARRAY_SIZE(todo); i++) { + strbuf_init(&todo[i].out, 0); + } + + for (i = 0; i < ARRAY_SIZE(threads); i++) { + int err; + struct grep_opt *o = grep_opt_dup(opt); + o->output = strbuf_out; + o->debug = 0; + compile_grep_patterns(o); + err = pthread_create(&threads[i], NULL, run, o); + + if (err) + die(_("grep: failed to create thread: %s"), + strerror(err)); + } +} + +static int wait_all(void) +{ + int hit = 0; + int i; + + grep_lock(); + all_work_added = 1; + + /* Wait until all work is done. */ + while (todo_done != todo_end) + pthread_cond_wait(&cond_result, &grep_mutex); + + /* Wake up all the consumer threads so they can see that there + * is no more work to do. + */ + pthread_cond_broadcast(&cond_add); + grep_unlock(); + + for (i = 0; i < ARRAY_SIZE(threads); i++) { + void *h; + pthread_join(threads[i], &h); + hit |= (int) (intptr_t) h; + } + + pthread_mutex_destroy(&grep_mutex); + pthread_mutex_destroy(&grep_read_mutex); + pthread_mutex_destroy(&grep_attr_mutex); + pthread_cond_destroy(&cond_add); + pthread_cond_destroy(&cond_write); + pthread_cond_destroy(&cond_result); + grep_use_locks = 0; + + return hit; +} +#else /* !NO_PTHREADS */ + +static int wait_all(void) +{ + return 0; +} +#endif + +static int grep_cmd_config(const char *var, const char *value, void *cb) +{ + int st = grep_config(var, value, cb); + if (git_color_default_config(var, value, cb) < 0) + st = -1; + return st; +} + +static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size) +{ + void *data; + + grep_read_lock(); + data = read_sha1_file(sha1, type, size); + grep_read_unlock(); + return data; +} + +static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, + const char *filename, int tree_name_len, + const char *path) +{ + struct strbuf pathbuf = STRBUF_INIT; + + if (opt->relative && opt->prefix_length) { + quote_path_relative(filename + tree_name_len, -1, &pathbuf, + opt->prefix); + strbuf_insert(&pathbuf, 0, filename, tree_name_len); + } else { + strbuf_addstr(&pathbuf, filename); + } + +#ifndef NO_PTHREADS + if (use_threads) { + add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1); + strbuf_release(&pathbuf); + return 0; + } else +#endif + { + struct grep_source gs; + int hit; + + grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1); + strbuf_release(&pathbuf); + hit = grep_source(opt, &gs); + + grep_source_clear(&gs); + return hit; + } +} + +static int grep_file(struct grep_opt *opt, const char *filename) +{ + struct strbuf buf = STRBUF_INIT; + + if (opt->relative && opt->prefix_length) + quote_path_relative(filename, -1, &buf, opt->prefix); + else + strbuf_addstr(&buf, filename); + +#ifndef NO_PTHREADS + if (use_threads) { + add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename); + strbuf_release(&buf); + return 0; + } else +#endif + { + struct grep_source gs; + int hit; + + grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename); + strbuf_release(&buf); + hit = grep_source(opt, &gs); + + grep_source_clear(&gs); + return hit; + } +} + +static void append_path(struct grep_opt *opt, const void *data, size_t len) +{ + struct string_list *path_list = opt->output_priv; + + if (len == 1 && *(const char *)data == '\0') + return; + string_list_append(path_list, xstrndup(data, len)); +} + +static void run_pager(struct grep_opt *opt, const char *prefix) +{ + struct string_list *path_list = opt->output_priv; + const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1)); + int i, status; + + for (i = 0; i < path_list->nr; i++) + argv[i] = path_list->items[i].string; + argv[path_list->nr] = NULL; + + if (prefix && chdir(prefix)) + die(_("Failed to chdir: %s"), prefix); + status = run_command_v_opt(argv, RUN_USING_SHELL); + if (status) + exit(status); + free(argv); +} + +static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int cached) +{ + int hit = 0; + int nr; + read_cache(); + + for (nr = 0; nr < active_nr; nr++) { + struct cache_entry *ce = active_cache[nr]; + if (!S_ISREG(ce->ce_mode)) + continue; + if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL)) + continue; + /* + * If CE_VALID is on, we assume worktree file and its cache entry + * are identical, even if worktree file has been modified, so use + * cache version instead + */ + if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) { + if (ce_stage(ce)) + continue; + hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name); + } + else + hit |= grep_file(opt, ce->name); + if (ce_stage(ce)) { + do { + nr++; + } while (nr < active_nr && + !strcmp(ce->name, active_cache[nr]->name)); + nr--; /* compensate for loop control */ + } + if (hit && opt->status_only) + break; + } + return hit; +} + +static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, + struct tree_desc *tree, struct strbuf *base, int tn_len, + int check_attr) +{ + int hit = 0; + enum interesting match = entry_not_interesting; + struct name_entry entry; + int old_baselen = base->len; + + while (tree_entry(tree, &entry)) { + int te_len = tree_entry_len(&entry); + + if (match != all_entries_interesting) { + match = tree_entry_interesting(&entry, base, tn_len, pathspec); + if (match == all_entries_not_interesting) + break; + if (match == entry_not_interesting) + continue; + } + + strbuf_add(base, entry.path, te_len); + + if (S_ISREG(entry.mode)) { + hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len, + check_attr ? base->buf + tn_len : NULL); + } + else if (S_ISDIR(entry.mode)) { + enum object_type type; + struct tree_desc sub; + void *data; + unsigned long size; + + data = lock_and_read_sha1_file(entry.sha1, &type, &size); + if (!data) + die(_("unable to read tree (%s)"), + sha1_to_hex(entry.sha1)); + + strbuf_addch(base, '/'); + init_tree_desc(&sub, data, size); + hit |= grep_tree(opt, pathspec, &sub, base, tn_len, + check_attr); + free(data); + } + strbuf_setlen(base, old_baselen); + + if (hit && opt->status_only) + break; + } + return hit; +} + +static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec, + struct object *obj, const char *name) +{ + if (obj->type == OBJ_BLOB) + return grep_sha1(opt, obj->sha1, name, 0, NULL); + if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { + struct tree_desc tree; + void *data; + unsigned long size; + struct strbuf base; + int hit, len; + + grep_read_lock(); + data = read_object_with_reference(obj->sha1, tree_type, + &size, NULL); + grep_read_unlock(); + + if (!data) + die(_("unable to read tree (%s)"), sha1_to_hex(obj->sha1)); + + len = name ? strlen(name) : 0; + strbuf_init(&base, PATH_MAX + len + 1); + if (len) { + strbuf_add(&base, name, len); + strbuf_addch(&base, ':'); + } + init_tree_desc(&tree, data, size); + hit = grep_tree(opt, pathspec, &tree, &base, base.len, + obj->type == OBJ_COMMIT); + strbuf_release(&base); + free(data); + return hit; + } + die(_("unable to grep from object of type %s"), typename(obj->type)); +} + +static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec, + const struct object_array *list) +{ + unsigned int i; + int hit = 0; + const unsigned int nr = list->nr; + + for (i = 0; i < nr; i++) { + struct object *real_obj; + real_obj = deref_tag(list->objects[i].item, NULL, 0); + if (grep_object(opt, pathspec, real_obj, list->objects[i].name)) { + hit = 1; + if (opt->status_only) + break; + } + } + return hit; +} + +static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec, + int exc_std) +{ + struct dir_struct dir; + int i, hit = 0; + + memset(&dir, 0, sizeof(dir)); + if (exc_std) + setup_standard_excludes(&dir); + + fill_directory(&dir, pathspec->raw); + for (i = 0; i < dir.nr; i++) { + const char *name = dir.entries[i]->name; + int namelen = strlen(name); + if (!match_pathspec_depth(pathspec, name, namelen, 0, NULL)) + continue; + hit |= grep_file(opt, dir.entries[i]->name); + if (hit && opt->status_only) + break; + } + return hit; +} + +static int context_callback(const struct option *opt, const char *arg, + int unset) +{ + struct grep_opt *grep_opt = opt->value; + int value; + const char *endp; + + if (unset) { + grep_opt->pre_context = grep_opt->post_context = 0; + return 0; + } + value = strtol(arg, (char **)&endp, 10); + if (*endp) { + return error(_("switch `%c' expects a numerical value"), + opt->short_name); + } + grep_opt->pre_context = grep_opt->post_context = value; + return 0; +} + +static int file_callback(const struct option *opt, const char *arg, int unset) +{ + struct grep_opt *grep_opt = opt->value; + int from_stdin = !strcmp(arg, "-"); + FILE *patterns; + int lno = 0; + struct strbuf sb = STRBUF_INIT; + + patterns = from_stdin ? stdin : fopen(arg, "r"); + if (!patterns) + die_errno(_("cannot open '%s'"), arg); + while (strbuf_getline(&sb, patterns, '\n') == 0) { + /* ignore empty line like grep does */ + if (sb.len == 0) + continue; + + append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno, + GREP_PATTERN); + } + if (!from_stdin) + fclose(patterns); + strbuf_release(&sb); + return 0; +} + +static int not_callback(const struct option *opt, const char *arg, int unset) +{ + struct grep_opt *grep_opt = opt->value; + append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT); + return 0; +} + +static int and_callback(const struct option *opt, const char *arg, int unset) +{ + struct grep_opt *grep_opt = opt->value; + append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND); + return 0; +} + +static int open_callback(const struct option *opt, const char *arg, int unset) +{ + struct grep_opt *grep_opt = opt->value; + append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN); + return 0; +} + +static int close_callback(const struct option *opt, const char *arg, int unset) +{ + struct grep_opt *grep_opt = opt->value; + append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN); + return 0; +} + +static int pattern_callback(const struct option *opt, const char *arg, + int unset) +{ + struct grep_opt *grep_opt = opt->value; + append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN); + return 0; +} + +static int help_callback(const struct option *opt, const char *arg, int unset) +{ + return -1; +} + +int cmd_grep(int argc, const char **argv, const char *prefix) +{ + int hit = 0; + int cached = 0, untracked = 0, opt_exclude = -1; + int seen_dashdash = 0; + int external_grep_allowed__ignored; + const char *show_in_pager = NULL, *default_pager = "dummy"; + struct grep_opt opt; + struct object_array list = OBJECT_ARRAY_INIT; + const char **paths = NULL; + struct pathspec pathspec; + struct string_list path_list = STRING_LIST_INIT_NODUP; + int i; + int dummy; + int use_index = 1; + int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED; + + struct option options[] = { + OPT_BOOLEAN(0, "cached", &cached, + N_("search in index instead of in the work tree")), + OPT_NEGBIT(0, "no-index", &use_index, + N_("find in contents not managed by git"), 1), + OPT_BOOLEAN(0, "untracked", &untracked, + N_("search in both tracked and untracked files")), + OPT_SET_INT(0, "exclude-standard", &opt_exclude, + N_("search also in ignored files"), 1), + OPT_GROUP(""), + OPT_BOOLEAN('v', "invert-match", &opt.invert, + N_("show non-matching lines")), + OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case, + N_("case insensitive matching")), + OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp, + N_("match patterns only at word boundaries")), + OPT_SET_INT('a', "text", &opt.binary, + N_("process binary files as text"), GREP_BINARY_TEXT), + OPT_SET_INT('I', NULL, &opt.binary, + N_("don't match patterns in binary files"), + GREP_BINARY_NOMATCH), + { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"), + N_("descend at most <depth> levels"), PARSE_OPT_NONEG, + NULL, 1 }, + OPT_GROUP(""), + OPT_SET_INT('E', "extended-regexp", &pattern_type_arg, + N_("use extended POSIX regular expressions"), + GREP_PATTERN_TYPE_ERE), + OPT_SET_INT('G', "basic-regexp", &pattern_type_arg, + N_("use basic POSIX regular expressions (default)"), + GREP_PATTERN_TYPE_BRE), + OPT_SET_INT('F', "fixed-strings", &pattern_type_arg, + N_("interpret patterns as fixed strings"), + GREP_PATTERN_TYPE_FIXED), + OPT_SET_INT('P', "perl-regexp", &pattern_type_arg, + N_("use Perl-compatible regular expressions"), + GREP_PATTERN_TYPE_PCRE), + OPT_GROUP(""), + OPT_BOOLEAN('n', "line-number", &opt.linenum, N_("show line numbers")), + OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1), + OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1), + OPT_NEGBIT(0, "full-name", &opt.relative, + N_("show filenames relative to top directory"), 1), + OPT_BOOLEAN('l', "files-with-matches", &opt.name_only, + N_("show only filenames instead of matching lines")), + OPT_BOOLEAN(0, "name-only", &opt.name_only, + N_("synonym for --files-with-matches")), + OPT_BOOLEAN('L', "files-without-match", + &opt.unmatch_name_only, + N_("show only the names of files without match")), + OPT_BOOLEAN('z', "null", &opt.null_following_name, + N_("print NUL after filenames")), + OPT_BOOLEAN('c', "count", &opt.count, + N_("show the number of matches instead of matching lines")), + OPT__COLOR(&opt.color, N_("highlight matches")), + OPT_BOOLEAN(0, "break", &opt.file_break, + N_("print empty line between matches from different files")), + OPT_BOOLEAN(0, "heading", &opt.heading, + N_("show filename only once above matches from same file")), + OPT_GROUP(""), + OPT_CALLBACK('C', "context", &opt, N_("n"), + N_("show <n> context lines before and after matches"), + context_callback), + OPT_INTEGER('B', "before-context", &opt.pre_context, + N_("show <n> context lines before matches")), + OPT_INTEGER('A', "after-context", &opt.post_context, + N_("show <n> context lines after matches")), + OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"), + context_callback), + OPT_BOOLEAN('p', "show-function", &opt.funcname, + N_("show a line with the function name before matches")), + OPT_BOOLEAN('W', "function-context", &opt.funcbody, + N_("show the surrounding function")), + OPT_GROUP(""), + OPT_CALLBACK('f', NULL, &opt, N_("file"), + N_("read patterns from file"), file_callback), + { OPTION_CALLBACK, 'e', NULL, &opt, N_("pattern"), + N_("match <pattern>"), PARSE_OPT_NONEG, pattern_callback }, + { OPTION_CALLBACK, 0, "and", &opt, NULL, + N_("combine patterns specified with -e"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback }, + OPT_BOOLEAN(0, "or", &dummy, ""), + { OPTION_CALLBACK, 0, "not", &opt, NULL, "", + PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback }, + { OPTION_CALLBACK, '(', NULL, &opt, NULL, "", + PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, + open_callback }, + { OPTION_CALLBACK, ')', NULL, &opt, NULL, "", + PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, + close_callback }, + OPT__QUIET(&opt.status_only, + N_("indicate hit with exit status without output")), + OPT_BOOLEAN(0, "all-match", &opt.all_match, + N_("show only matches from files that match all patterns")), + { OPTION_SET_INT, 0, "debug", &opt.debug, NULL, + N_("show parse tree for grep expression"), + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1 }, + OPT_GROUP(""), + { OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager, + N_("pager"), N_("show matching files in the pager"), + PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager }, + OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored, + N_("allow calling of grep(1) (ignored by this build)")), + { OPTION_CALLBACK, 0, "help-all", &options, NULL, N_("show usage"), + PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback }, + OPT_END() + }; + + /* + * 'git grep -h', unlike 'git grep -h <pattern>', is a request + * to show usage information and exit. + */ + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(grep_usage, options); + + init_grep_defaults(); + git_config(grep_cmd_config, NULL); + grep_init(&opt, prefix); + + /* + * If there is no -- then the paths must exist in the working + * tree. If there is no explicit pattern specified with -e or + * -f, we take the first unrecognized non option to be the + * pattern, but then what follows it must be zero or more + * valid refs up to the -- (if exists), and then existing + * paths. If there is an explicit pattern, then the first + * unrecognized non option is the beginning of the refs list + * that continues up to the -- (if exists), and then paths. + */ + argc = parse_options(argc, argv, prefix, options, grep_usage, + PARSE_OPT_KEEP_DASHDASH | + PARSE_OPT_STOP_AT_NON_OPTION | + PARSE_OPT_NO_INTERNAL_HELP); + grep_commit_pattern_type(pattern_type_arg, &opt); + + if (use_index && !startup_info->have_repository) + /* die the same way as if we did it at the beginning */ + setup_git_directory(); + + /* + * skip a -- separator; we know it cannot be + * separating revisions from pathnames if + * we haven't even had any patterns yet + */ + if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) { + argv++; + argc--; + } + + /* First unrecognized non-option token */ + if (argc > 0 && !opt.pattern_list) { + append_grep_pattern(&opt, argv[0], "command line", 0, + GREP_PATTERN); + argv++; + argc--; + } + + if (show_in_pager == default_pager) + show_in_pager = git_pager(1); + if (show_in_pager) { + opt.color = 0; + opt.name_only = 1; + opt.null_following_name = 1; + opt.output_priv = &path_list; + opt.output = append_path; + string_list_append(&path_list, show_in_pager); + use_threads = 0; + } + + if (!opt.pattern_list) + die(_("no pattern given.")); + if (!opt.fixed && opt.ignore_case) + opt.regflags |= REG_ICASE; + + compile_grep_patterns(&opt); + + /* Check revs and then paths */ + for (i = 0; i < argc; i++) { + const char *arg = argv[i]; + unsigned char sha1[20]; + /* Is it a rev? */ + if (!get_sha1(arg, sha1)) { + struct object *object = parse_object(sha1); + if (!object) + die(_("bad object %s"), arg); + add_object_array(object, arg, &list); + continue; + } + if (!strcmp(arg, "--")) { + i++; + seen_dashdash = 1; + } + break; + } + +#ifndef NO_PTHREADS + if (list.nr || cached || online_cpus() == 1) + use_threads = 0; +#else + use_threads = 0; +#endif + +#ifndef NO_PTHREADS + if (use_threads) { + if (!(opt.name_only || opt.unmatch_name_only || opt.count) + && (opt.pre_context || opt.post_context || + opt.file_break || opt.funcbody)) + skip_first_line = 1; + start_threads(&opt); + } +#endif + + /* The rest are paths */ + if (!seen_dashdash) { + int j; + for (j = i; j < argc; j++) + verify_filename(prefix, argv[j], j == i); + } + + paths = get_pathspec(prefix, argv + i); + init_pathspec(&pathspec, paths); + pathspec.max_depth = opt.max_depth; + pathspec.recursive = 1; + + if (show_in_pager && (cached || list.nr)) + die(_("--open-files-in-pager only works on the worktree")); + + if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) { + const char *pager = path_list.items[0].string; + int len = strlen(pager); + + if (len > 4 && is_dir_sep(pager[len - 5])) + pager += len - 4; + + if (!strcmp("less", pager) || !strcmp("vi", pager)) { + struct strbuf buf = STRBUF_INIT; + strbuf_addf(&buf, "+/%s%s", + strcmp("less", pager) ? "" : "*", + opt.pattern_list->pattern); + string_list_append(&path_list, buf.buf); + strbuf_detach(&buf, NULL); + } + } + + if (!show_in_pager) + setup_pager(); + + if (!use_index && (untracked || cached)) + die(_("--cached or --untracked cannot be used with --no-index.")); + + if (!use_index || untracked) { + int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude; + if (list.nr) + die(_("--no-index or --untracked cannot be used with revs.")); + hit = grep_directory(&opt, &pathspec, use_exclude); + } else if (0 <= opt_exclude) { + die(_("--[no-]exclude-standard cannot be used for tracked contents.")); + } else if (!list.nr) { + if (!cached) + setup_work_tree(); + + hit = grep_cache(&opt, &pathspec, cached); + } else { + if (cached) + die(_("both --cached and trees are given.")); + hit = grep_objects(&opt, &pathspec, &list); + } + + if (use_threads) + hit |= wait_all(); + if (hit && show_in_pager) + run_pager(&opt, prefix); + free_grep_patterns(&opt); + return !hit; +} diff --git a/builtin/hash-object.c b/builtin/hash-object.c new file mode 100644 index 0000000000..8d184f1a99 --- /dev/null +++ b/builtin/hash-object.c @@ -0,0 +1,137 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + * Copyright (C) Junio C Hamano, 2005 + */ +#include "builtin.h" +#include "blob.h" +#include "quote.h" +#include "parse-options.h" +#include "exec_cmd.h" + +static void hash_fd(int fd, const char *type, int write_object, const char *path) +{ + struct stat st; + unsigned char sha1[20]; + unsigned flags = (HASH_FORMAT_CHECK | + (write_object ? HASH_WRITE_OBJECT : 0)); + + if (fstat(fd, &st) < 0 || + index_fd(sha1, fd, &st, type_from_string(type), path, flags)) + die(write_object + ? "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_object(const char *path, const char *type, int write_object, + const char *vpath) +{ + int fd; + fd = open(path, O_RDONLY); + if (fd < 0) + die_errno("Cannot open '%s'", path); + hash_fd(fd, type, write_object, vpath); +} + +static int no_filters; + +static void hash_stdin_paths(const char *type, int write_objects) +{ + struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; + + 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, write_objects, + no_filters ? NULL : buf.buf); + } + strbuf_release(&buf); + strbuf_release(&nbuf); +} + +static const char * const hash_object_usage[] = { + N_("git hash-object [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>..."), + N_("git hash-object --stdin-paths < <list-of-paths>"), + NULL +}; + +static const char *type; +static int write_object; +static int hashstdin; +static int stdin_paths; +static const char *vpath; + +static const struct option hash_object_options[] = { + OPT_STRING('t', NULL, &type, N_("type"), N_("object type")), + OPT_BOOLEAN('w', NULL, &write_object, N_("write the object into the object database")), + OPT_BOOLEAN( 0 , "stdin", &hashstdin, N_("read the object from stdin")), + OPT_BOOLEAN( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")), + OPT_BOOLEAN( 0 , "no-filters", &no_filters, N_("store file as is without filters")), + OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")), + OPT_END() +}; + +int cmd_hash_object(int argc, const char **argv, const char *prefix) +{ + int i; + int prefix_length = -1; + const char *errstr = NULL; + + type = blob_type; + + argc = parse_options(argc, argv, NULL, hash_object_options, + hash_object_usage, 0); + + if (write_object) { + prefix = setup_git_directory(); + prefix_length = prefix ? strlen(prefix) : 0; + if (vpath && prefix) + vpath = prefix_filename(prefix, prefix_length, vpath); + } + + git_config(git_default_config, NULL); + + if (stdin_paths) { + if (hashstdin) + errstr = "Can't use --stdin-paths with --stdin"; + else if (argc) + errstr = "Can't specify files with --stdin-paths"; + else if (vpath) + errstr = "Can't use --stdin-paths with --path"; + } + else { + if (hashstdin > 1) + errstr = "Multiple --stdin arguments are not supported"; + if (vpath && no_filters) + errstr = "Can't use --path with --no-filters"; + } + + if (errstr) { + error("%s", errstr); + usage_with_options(hash_object_usage, hash_object_options); + } + + if (hashstdin) + hash_fd(0, type, write_object, vpath); + + for (i = 0 ; i < argc; i++) { + const char *arg = argv[i]; + + if (0 <= prefix_length) + arg = prefix_filename(prefix, prefix_length, arg); + hash_object(arg, type, write_object, + no_filters ? NULL : vpath ? vpath : arg); + } + + if (stdin_paths) + hash_stdin_paths(type, write_object); + + return 0; +} diff --git a/builtin/help.c b/builtin/help.c new file mode 100644 index 0000000000..bd86253d83 --- /dev/null +++ b/builtin/help.c @@ -0,0 +1,488 @@ +/* + * builtin-help.c + * + * Builtin help command + */ +#include "cache.h" +#include "builtin.h" +#include "exec_cmd.h" +#include "common-cmds.h" +#include "parse-options.h" +#include "run-command.h" +#include "column.h" +#include "help.h" + +#ifndef DEFAULT_HELP_FORMAT +#define DEFAULT_HELP_FORMAT "man" +#endif + +static struct man_viewer_list { + struct man_viewer_list *next; + char name[FLEX_ARRAY]; +} *man_viewer_list; + +static struct man_viewer_info_list { + struct man_viewer_info_list *next; + const char *info; + char name[FLEX_ARRAY]; +} *man_viewer_info_list; + +enum help_format { + HELP_FORMAT_NONE, + HELP_FORMAT_MAN, + HELP_FORMAT_INFO, + HELP_FORMAT_WEB +}; + +static const char *html_path; + +static int show_all = 0; +static unsigned int colopts; +static enum help_format help_format = HELP_FORMAT_NONE; +static struct option builtin_help_options[] = { + OPT_BOOLEAN('a', "all", &show_all, N_("print all available commands")), + OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN), + OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"), + HELP_FORMAT_WEB), + OPT_SET_INT('i', "info", &help_format, N_("show info page"), + HELP_FORMAT_INFO), + OPT_END(), +}; + +static const char * const builtin_help_usage[] = { + N_("git help [--all] [--man|--web|--info] [command]"), + NULL +}; + +static enum help_format parse_help_format(const char *format) +{ + if (!strcmp(format, "man")) + return HELP_FORMAT_MAN; + if (!strcmp(format, "info")) + return HELP_FORMAT_INFO; + if (!strcmp(format, "web") || !strcmp(format, "html")) + return HELP_FORMAT_WEB; + die(_("unrecognized help format '%s'"), format); +} + +static const char *get_man_viewer_info(const char *name) +{ + struct man_viewer_info_list *viewer; + + for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) + { + if (!strcasecmp(name, viewer->name)) + return viewer->info; + } + return NULL; +} + +static int check_emacsclient_version(void) +{ + struct strbuf buffer = STRBUF_INIT; + struct child_process ec_process; + const char *argv_ec[] = { "emacsclient", "--version", NULL }; + int version; + + /* emacsclient prints its version number on stderr */ + memset(&ec_process, 0, sizeof(ec_process)); + ec_process.argv = argv_ec; + ec_process.err = -1; + ec_process.stdout_to_stderr = 1; + if (start_command(&ec_process)) + return error(_("Failed to start emacsclient.")); + + strbuf_read(&buffer, ec_process.err, 20); + close(ec_process.err); + + /* + * Don't bother checking return value, because "emacsclient --version" + * seems to always exits with code 1. + */ + finish_command(&ec_process); + + if (prefixcmp(buffer.buf, "emacsclient")) { + strbuf_release(&buffer); + return error(_("Failed to parse emacsclient version.")); + } + + strbuf_remove(&buffer, 0, strlen("emacsclient")); + version = atoi(buffer.buf); + + if (version < 22) { + strbuf_release(&buffer); + return error(_("emacsclient version '%d' too old (< 22)."), + version); + } + + strbuf_release(&buffer); + return 0; +} + +static void exec_woman_emacs(const char *path, const char *page) +{ + if (!check_emacsclient_version()) { + /* This works only with emacsclient version >= 22. */ + struct strbuf man_page = STRBUF_INIT; + + if (!path) + path = "emacsclient"; + strbuf_addf(&man_page, "(woman \"%s\")", page); + execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL); + warning(_("failed to exec '%s': %s"), path, strerror(errno)); + } +} + +static void exec_man_konqueror(const char *path, const char *page) +{ + const char *display = getenv("DISPLAY"); + if (display && *display) { + struct strbuf man_page = STRBUF_INIT; + const char *filename = "kfmclient"; + + /* It's simpler to launch konqueror using kfmclient. */ + if (path) { + const char *file = strrchr(path, '/'); + if (file && !strcmp(file + 1, "konqueror")) { + char *new = xstrdup(path); + char *dest = strrchr(new, '/'); + + /* strlen("konqueror") == strlen("kfmclient") */ + strcpy(dest + 1, "kfmclient"); + path = new; + } + if (file) + filename = file; + } else + path = "kfmclient"; + strbuf_addf(&man_page, "man:%s(1)", page); + execlp(path, filename, "newTab", man_page.buf, (char *)NULL); + warning(_("failed to exec '%s': %s"), path, strerror(errno)); + } +} + +static void exec_man_man(const char *path, const char *page) +{ + if (!path) + path = "man"; + execlp(path, "man", page, (char *)NULL); + warning(_("failed to exec '%s': %s"), path, strerror(errno)); +} + +static void exec_man_cmd(const char *cmd, const char *page) +{ + struct strbuf shell_cmd = STRBUF_INIT; + strbuf_addf(&shell_cmd, "%s %s", cmd, page); + execl("/bin/sh", "sh", "-c", shell_cmd.buf, (char *)NULL); + warning(_("failed to exec '%s': %s"), cmd, strerror(errno)); +} + +static void add_man_viewer(const char *name) +{ + struct man_viewer_list **p = &man_viewer_list; + size_t len = strlen(name); + + while (*p) + p = &((*p)->next); + *p = xcalloc(1, (sizeof(**p) + len + 1)); + strncpy((*p)->name, name, len); +} + +static int supported_man_viewer(const char *name, size_t len) +{ + return (!strncasecmp("man", name, len) || + !strncasecmp("woman", name, len) || + !strncasecmp("konqueror", name, len)); +} + +static void do_add_man_viewer_info(const char *name, + size_t len, + const char *value) +{ + struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1); + + strncpy(new->name, name, len); + new->info = xstrdup(value); + new->next = man_viewer_info_list; + man_viewer_info_list = new; +} + +static int add_man_viewer_path(const char *name, + size_t len, + const char *value) +{ + if (supported_man_viewer(name, len)) + do_add_man_viewer_info(name, len, value); + else + warning(_("'%s': path for unsupported man viewer.\n" + "Please consider using 'man.<tool>.cmd' instead."), + name); + + return 0; +} + +static int add_man_viewer_cmd(const char *name, + size_t len, + const char *value) +{ + if (supported_man_viewer(name, len)) + warning(_("'%s': cmd for supported man viewer.\n" + "Please consider using 'man.<tool>.path' instead."), + name); + else + do_add_man_viewer_info(name, len, value); + + return 0; +} + +static int add_man_viewer_info(const char *var, const char *value) +{ + const char *name = var + 4; + const char *subkey = strrchr(name, '.'); + + if (!subkey) + return 0; + + if (!strcmp(subkey, ".path")) { + if (!value) + return config_error_nonbool(var); + return add_man_viewer_path(name, subkey - name, value); + } + if (!strcmp(subkey, ".cmd")) { + if (!value) + return config_error_nonbool(var); + return add_man_viewer_cmd(name, subkey - name, value); + } + + return 0; +} + +static int git_help_config(const char *var, const char *value, void *cb) +{ + if (!prefixcmp(var, "column.")) + return git_column_config(var, value, "help", &colopts); + if (!strcmp(var, "help.format")) { + if (!value) + return config_error_nonbool(var); + help_format = parse_help_format(value); + return 0; + } + if (!strcmp(var, "help.htmlpath")) { + if (!value) + return config_error_nonbool(var); + html_path = xstrdup(value); + return 0; + } + if (!strcmp(var, "man.viewer")) { + if (!value) + return config_error_nonbool(var); + add_man_viewer(value); + return 0; + } + if (!prefixcmp(var, "man.")) + return add_man_viewer_info(var, value); + + return git_default_config(var, value, cb); +} + +static struct cmdnames main_cmds, other_cmds; + +void list_common_cmds_help(void) +{ + int i, longest = 0; + + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { + if (longest < strlen(common_cmds[i].name)) + longest = strlen(common_cmds[i].name); + } + + puts(_("The most commonly used git commands are:")); + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { + printf(" %s ", common_cmds[i].name); + mput_char(' ', longest - strlen(common_cmds[i].name)); + puts(_(common_cmds[i].help)); + } +} + +static int is_git_command(const char *s) +{ + return is_in_cmdlist(&main_cmds, s) || + is_in_cmdlist(&other_cmds, s); +} + +static const char *prepend(const char *prefix, const char *cmd) +{ + size_t pre_len = strlen(prefix); + size_t cmd_len = strlen(cmd); + char *p = xmalloc(pre_len + cmd_len + 1); + memcpy(p, prefix, pre_len); + strcpy(p + pre_len, cmd); + return p; +} + +static const char *cmd_to_page(const char *git_cmd) +{ + if (!git_cmd) + return "git"; + else if (!prefixcmp(git_cmd, "git")) + return git_cmd; + else if (is_git_command(git_cmd)) + return prepend("git-", git_cmd); + else + return prepend("git", git_cmd); +} + +static void setup_man_path(void) +{ + struct strbuf new_path = STRBUF_INIT; + const char *old_path = getenv("MANPATH"); + + /* We should always put ':' after our path. If there is no + * old_path, the ':' at the end will let 'man' to try + * system-wide paths after ours to find the manual page. If + * there is old_path, we need ':' as delimiter. */ + strbuf_addstr(&new_path, system_path(GIT_MAN_PATH)); + strbuf_addch(&new_path, ':'); + if (old_path) + strbuf_addstr(&new_path, old_path); + + setenv("MANPATH", new_path.buf, 1); + + strbuf_release(&new_path); +} + +static void exec_viewer(const char *name, const char *page) +{ + const char *info = get_man_viewer_info(name); + + if (!strcasecmp(name, "man")) + exec_man_man(info, page); + else if (!strcasecmp(name, "woman")) + exec_woman_emacs(info, page); + else if (!strcasecmp(name, "konqueror")) + exec_man_konqueror(info, page); + else if (info) + exec_man_cmd(info, page); + else + warning(_("'%s': unknown man viewer."), name); +} + +static void show_man_page(const char *git_cmd) +{ + struct man_viewer_list *viewer; + const char *page = cmd_to_page(git_cmd); + const char *fallback = getenv("GIT_MAN_VIEWER"); + + setup_man_path(); + for (viewer = man_viewer_list; viewer; viewer = viewer->next) + { + exec_viewer(viewer->name, page); /* will return when unable */ + } + if (fallback) + exec_viewer(fallback, page); + exec_viewer("man", page); + die(_("no man viewer handled the request")); +} + +static void show_info_page(const char *git_cmd) +{ + const char *page = cmd_to_page(git_cmd); + setenv("INFOPATH", system_path(GIT_INFO_PATH), 1); + execlp("info", "info", "gitman", page, (char *)NULL); + die(_("no info viewer handled the request")); +} + +static void get_html_page_path(struct strbuf *page_path, const char *page) +{ + struct stat st; + if (!html_path) + html_path = system_path(GIT_HTML_PATH); + + /* Check that we have a git documentation directory. */ + if (!strstr(html_path, "://")) { + if (stat(mkpath("%s/git.html", html_path), &st) + || !S_ISREG(st.st_mode)) + die("'%s': not a documentation directory.", html_path); + } + + strbuf_init(page_path, 0); + strbuf_addf(page_path, "%s/%s.html", html_path, page); +} + +/* + * If open_html is not defined in a platform-specific way (see for + * example compat/mingw.h), we use the script web--browse to display + * HTML. + */ +#ifndef open_html +static void open_html(const char *path) +{ + execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL); +} +#endif + +static void show_html_page(const char *git_cmd) +{ + const char *page = cmd_to_page(git_cmd); + struct strbuf page_path; /* it leaks but we exec bellow */ + + get_html_page_path(&page_path, page); + + open_html(page_path.buf); +} + +int cmd_help(int argc, const char **argv, const char *prefix) +{ + int nongit; + const char *alias; + enum help_format parsed_help_format; + load_command_list("git-", &main_cmds, &other_cmds); + + argc = parse_options(argc, argv, prefix, builtin_help_options, + builtin_help_usage, 0); + parsed_help_format = help_format; + + if (show_all) { + git_config(git_help_config, NULL); + printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); + list_commands(colopts, &main_cmds, &other_cmds); + printf("%s\n", _(git_more_info_string)); + return 0; + } + + if (!argv[0]) { + printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); + list_common_cmds_help(); + printf("\n%s\n", _(git_more_info_string)); + return 0; + } + + setup_git_directory_gently(&nongit); + git_config(git_help_config, NULL); + + if (parsed_help_format != HELP_FORMAT_NONE) + help_format = parsed_help_format; + if (help_format == HELP_FORMAT_NONE) + help_format = parse_help_format(DEFAULT_HELP_FORMAT); + + alias = alias_lookup(argv[0]); + if (alias && !is_git_command(argv[0])) { + printf_ln(_("`git %s' is aliased to `%s'"), argv[0], alias); + return 0; + } + + switch (help_format) { + case HELP_FORMAT_NONE: + case HELP_FORMAT_MAN: + show_man_page(argv[0]); + break; + case HELP_FORMAT_INFO: + show_info_page(argv[0]); + break; + case HELP_FORMAT_WEB: + show_html_page(argv[0]); + break; + } + + return 0; +} diff --git a/builtin/index-pack.c b/builtin/index-pack.c new file mode 100644 index 0000000000..43d364b8d5 --- /dev/null +++ b/builtin/index-pack.c @@ -0,0 +1,1634 @@ +#include "builtin.h" +#include "delta.h" +#include "pack.h" +#include "csum-file.h" +#include "blob.h" +#include "commit.h" +#include "tag.h" +#include "tree.h" +#include "progress.h" +#include "fsck.h" +#include "exec_cmd.h" +#include "streaming.h" +#include "thread-utils.h" + +static const char index_pack_usage[] = +"git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])"; + +struct object_entry { + struct pack_idx_entry idx; + unsigned long size; + unsigned int hdr_size; + enum object_type type; + enum object_type real_type; + unsigned delta_depth; + int base_object_no; +}; + +union delta_base { + unsigned char sha1[20]; + off_t offset; +}; + +struct base_data { + struct base_data *base; + struct base_data *child; + struct object_entry *obj; + void *data; + unsigned long size; + int ref_first, ref_last; + int ofs_first, ofs_last; +}; + +#if !defined(NO_PTHREADS) && defined(NO_THREAD_SAFE_PREAD) +/* pread() emulation is not thread-safe. Disable threading. */ +#define NO_PTHREADS +#endif + +struct thread_local { +#ifndef NO_PTHREADS + pthread_t thread; +#endif + struct base_data *base_cache; + size_t base_cache_used; +}; + +/* + * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want + * to memcmp() only the first 20 bytes. + */ +#define UNION_BASE_SZ 20 + +#define FLAG_LINK (1u<<20) +#define FLAG_CHECKED (1u<<21) + +struct delta_entry { + union delta_base base; + int obj_no; +}; + +static struct object_entry *objects; +static struct delta_entry *deltas; +static struct thread_local nothread_data; +static int nr_objects; +static int nr_deltas; +static int nr_resolved_deltas; +static int nr_threads; + +static int from_stdin; +static int strict; +static int verbose; + +static struct progress *progress; + +/* We always read in 4kB chunks. */ +static unsigned char input_buffer[4096]; +static unsigned int input_offset, input_len; +static off_t consumed_bytes; +static unsigned deepest_delta; +static git_SHA_CTX input_ctx; +static uint32_t input_crc32; +static int input_fd, output_fd, pack_fd; + +#ifndef NO_PTHREADS + +static struct thread_local *thread_data; +static int nr_dispatched; +static int threads_active; + +static pthread_mutex_t read_mutex; +#define read_lock() lock_mutex(&read_mutex) +#define read_unlock() unlock_mutex(&read_mutex) + +static pthread_mutex_t counter_mutex; +#define counter_lock() lock_mutex(&counter_mutex) +#define counter_unlock() unlock_mutex(&counter_mutex) + +static pthread_mutex_t work_mutex; +#define work_lock() lock_mutex(&work_mutex) +#define work_unlock() unlock_mutex(&work_mutex) + +static pthread_key_t key; + +static inline void lock_mutex(pthread_mutex_t *mutex) +{ + if (threads_active) + pthread_mutex_lock(mutex); +} + +static inline void unlock_mutex(pthread_mutex_t *mutex) +{ + if (threads_active) + pthread_mutex_unlock(mutex); +} + +/* + * Mutex and conditional variable can't be statically-initialized on Windows. + */ +static void init_thread(void) +{ + init_recursive_mutex(&read_mutex); + pthread_mutex_init(&counter_mutex, NULL); + pthread_mutex_init(&work_mutex, NULL); + pthread_key_create(&key, NULL); + thread_data = xcalloc(nr_threads, sizeof(*thread_data)); + threads_active = 1; +} + +static void cleanup_thread(void) +{ + if (!threads_active) + return; + threads_active = 0; + pthread_mutex_destroy(&read_mutex); + pthread_mutex_destroy(&counter_mutex); + pthread_mutex_destroy(&work_mutex); + pthread_key_delete(key); + free(thread_data); +} + +#else + +#define read_lock() +#define read_unlock() + +#define counter_lock() +#define counter_unlock() + +#define work_lock() +#define work_unlock() + +#endif + + +static int mark_link(struct object *obj, int type, void *data) +{ + if (!obj) + return -1; + + if (type != OBJ_ANY && obj->type != type) + die(_("object type mismatch at %s"), sha1_to_hex(obj->sha1)); + + obj->flags |= FLAG_LINK; + return 0; +} + +/* The content of each linked object must have been checked + or it must be already present in the object database */ +static void check_object(struct object *obj) +{ + if (!obj) + return; + + if (!(obj->flags & FLAG_LINK)) + return; + + if (!(obj->flags & FLAG_CHECKED)) { + unsigned long size; + int type = sha1_object_info(obj->sha1, &size); + if (type != obj->type || type <= 0) + die(_("object of unexpected type")); + obj->flags |= FLAG_CHECKED; + return; + } +} + +static void check_objects(void) +{ + unsigned i, max; + + max = get_max_object_index(); + for (i = 0; i < max; i++) + check_object(get_indexed_object(i)); +} + + +/* Discard current buffer used content. */ +static void flush(void) +{ + if (input_offset) { + if (output_fd >= 0) + write_or_die(output_fd, input_buffer, input_offset); + git_SHA1_Update(&input_ctx, input_buffer, input_offset); + memmove(input_buffer, input_buffer + input_offset, input_len); + input_offset = 0; + } +} + +/* + * Make sure at least "min" bytes are available in the buffer, and + * return the pointer to the buffer. + */ +static void *fill(int min) +{ + if (min <= input_len) + return input_buffer + input_offset; + if (min > sizeof(input_buffer)) + die(Q_("cannot fill %d byte", + "cannot fill %d bytes", + min), + min); + flush(); + do { + ssize_t ret = xread(input_fd, input_buffer + input_len, + sizeof(input_buffer) - input_len); + if (ret <= 0) { + if (!ret) + die(_("early EOF")); + die_errno(_("read error on input")); + } + input_len += ret; + if (from_stdin) + display_throughput(progress, consumed_bytes + input_len); + } while (input_len < min); + return input_buffer; +} + +static void use(int bytes) +{ + if (bytes > input_len) + die(_("used more bytes than were available")); + input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes); + input_len -= bytes; + input_offset += bytes; + + /* make sure off_t is sufficiently large not to wrap */ + if (signed_add_overflows(consumed_bytes, bytes)) + die(_("pack too large for current definition of off_t")); + consumed_bytes += bytes; +} + +static const char *open_pack_file(const char *pack_name) +{ + if (from_stdin) { + input_fd = 0; + if (!pack_name) { + static char tmp_file[PATH_MAX]; + output_fd = odb_mkstemp(tmp_file, sizeof(tmp_file), + "pack/tmp_pack_XXXXXX"); + pack_name = xstrdup(tmp_file); + } else + output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); + if (output_fd < 0) + die_errno(_("unable to create '%s'"), pack_name); + pack_fd = output_fd; + } else { + input_fd = open(pack_name, O_RDONLY); + if (input_fd < 0) + die_errno(_("cannot open packfile '%s'"), pack_name); + output_fd = -1; + pack_fd = input_fd; + } + git_SHA1_Init(&input_ctx); + return pack_name; +} + +static void parse_pack_header(void) +{ + struct pack_header *hdr = fill(sizeof(struct pack_header)); + + /* Header consistency check */ + if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) + die(_("pack signature mismatch")); + if (!pack_version_ok(hdr->hdr_version)) + die(_("pack version %"PRIu32" unsupported"), + ntohl(hdr->hdr_version)); + + nr_objects = ntohl(hdr->hdr_entries); + use(sizeof(struct pack_header)); +} + +static NORETURN void bad_object(unsigned long offset, const char *format, + ...) __attribute__((format (printf, 2, 3))); + +static NORETURN void bad_object(unsigned long offset, const char *format, ...) +{ + va_list params; + char buf[1024]; + + va_start(params, format); + vsnprintf(buf, sizeof(buf), format, params); + va_end(params); + die(_("pack has bad object at offset %lu: %s"), offset, buf); +} + +static inline struct thread_local *get_thread_data(void) +{ +#ifndef NO_PTHREADS + if (threads_active) + return pthread_getspecific(key); + assert(!threads_active && + "This should only be reached when all threads are gone"); +#endif + return ¬hread_data; +} + +#ifndef NO_PTHREADS +static void set_thread_data(struct thread_local *data) +{ + if (threads_active) + pthread_setspecific(key, data); +} +#endif + +static struct base_data *alloc_base_data(void) +{ + struct base_data *base = xmalloc(sizeof(struct base_data)); + memset(base, 0, sizeof(*base)); + base->ref_last = -1; + base->ofs_last = -1; + return base; +} + +static void free_base_data(struct base_data *c) +{ + if (c->data) { + free(c->data); + c->data = NULL; + get_thread_data()->base_cache_used -= c->size; + } +} + +static void prune_base_data(struct base_data *retain) +{ + struct base_data *b; + struct thread_local *data = get_thread_data(); + for (b = data->base_cache; + data->base_cache_used > delta_base_cache_limit && b; + b = b->child) { + if (b->data && b != retain) + free_base_data(b); + } +} + +static void link_base_data(struct base_data *base, struct base_data *c) +{ + if (base) + base->child = c; + else + get_thread_data()->base_cache = c; + + c->base = base; + c->child = NULL; + if (c->data) + get_thread_data()->base_cache_used += c->size; + prune_base_data(c); +} + +static void unlink_base_data(struct base_data *c) +{ + struct base_data *base = c->base; + if (base) + base->child = NULL; + else + get_thread_data()->base_cache = NULL; + free_base_data(c); +} + +static int is_delta_type(enum object_type type) +{ + return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA); +} + +static void *unpack_entry_data(unsigned long offset, unsigned long size, + enum object_type type, unsigned char *sha1) +{ + static char fixed_buf[8192]; + int status; + git_zstream stream; + void *buf; + git_SHA_CTX c; + char hdr[32]; + int hdrlen; + + if (!is_delta_type(type)) { + hdrlen = sprintf(hdr, "%s %lu", typename(type), size) + 1; + git_SHA1_Init(&c); + git_SHA1_Update(&c, hdr, hdrlen); + } else + sha1 = NULL; + if (type == OBJ_BLOB && size > big_file_threshold) + buf = fixed_buf; + else + buf = xmalloc(size); + + memset(&stream, 0, sizeof(stream)); + git_inflate_init(&stream); + stream.next_out = buf; + stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size; + + do { + unsigned char *last_out = stream.next_out; + stream.next_in = fill(1); + stream.avail_in = input_len; + status = git_inflate(&stream, 0); + use(input_len - stream.avail_in); + if (sha1) + git_SHA1_Update(&c, last_out, stream.next_out - last_out); + if (buf == fixed_buf) { + stream.next_out = buf; + stream.avail_out = sizeof(fixed_buf); + } + } while (status == Z_OK); + if (stream.total_out != size || status != Z_STREAM_END) + bad_object(offset, _("inflate returned %d"), status); + git_inflate_end(&stream); + if (sha1) + git_SHA1_Final(sha1, &c); + return buf == fixed_buf ? NULL : buf; +} + +static void *unpack_raw_entry(struct object_entry *obj, + union delta_base *delta_base, + unsigned char *sha1) +{ + unsigned char *p; + unsigned long size, c; + off_t base_offset; + unsigned shift; + void *data; + + obj->idx.offset = consumed_bytes; + input_crc32 = crc32(0, NULL, 0); + + p = fill(1); + c = *p; + use(1); + obj->type = (c >> 4) & 7; + size = (c & 15); + shift = 4; + while (c & 0x80) { + p = fill(1); + c = *p; + use(1); + size += (c & 0x7f) << shift; + shift += 7; + } + obj->size = size; + + switch (obj->type) { + case OBJ_REF_DELTA: + hashcpy(delta_base->sha1, fill(20)); + use(20); + break; + case OBJ_OFS_DELTA: + memset(delta_base, 0, sizeof(*delta_base)); + p = fill(1); + c = *p; + use(1); + base_offset = c & 127; + while (c & 128) { + base_offset += 1; + if (!base_offset || MSB(base_offset, 7)) + bad_object(obj->idx.offset, _("offset value overflow for delta base object")); + p = fill(1); + c = *p; + use(1); + base_offset = (base_offset << 7) + (c & 127); + } + delta_base->offset = obj->idx.offset - base_offset; + if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset) + bad_object(obj->idx.offset, _("delta base offset is out of bound")); + break; + case OBJ_COMMIT: + case OBJ_TREE: + case OBJ_BLOB: + case OBJ_TAG: + break; + default: + bad_object(obj->idx.offset, _("unknown object type %d"), obj->type); + } + obj->hdr_size = consumed_bytes - obj->idx.offset; + + data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, sha1); + obj->idx.crc32 = input_crc32; + return data; +} + +static void *unpack_data(struct object_entry *obj, + int (*consume)(const unsigned char *, unsigned long, void *), + void *cb_data) +{ + off_t from = obj[0].idx.offset + obj[0].hdr_size; + unsigned long len = obj[1].idx.offset - from; + unsigned char *data, *inbuf; + git_zstream stream; + int status; + + data = xmalloc(consume ? 64*1024 : obj->size); + inbuf = xmalloc((len < 64*1024) ? len : 64*1024); + + memset(&stream, 0, sizeof(stream)); + git_inflate_init(&stream); + stream.next_out = data; + stream.avail_out = consume ? 64*1024 : obj->size; + + do { + ssize_t n = (len < 64*1024) ? len : 64*1024; + n = pread(pack_fd, inbuf, n, from); + if (n < 0) + die_errno(_("cannot pread pack file")); + if (!n) + die(Q_("premature end of pack file, %lu byte missing", + "premature end of pack file, %lu bytes missing", + len), + len); + from += n; + len -= n; + stream.next_in = inbuf; + stream.avail_in = n; + if (!consume) + status = git_inflate(&stream, 0); + else { + do { + status = git_inflate(&stream, 0); + if (consume(data, stream.next_out - data, cb_data)) { + free(inbuf); + free(data); + return NULL; + } + stream.next_out = data; + stream.avail_out = 64*1024; + } while (status == Z_OK && stream.avail_in); + } + } while (len && status == Z_OK && !stream.avail_in); + + /* This has been inflated OK when first encountered, so... */ + if (status != Z_STREAM_END || stream.total_out != obj->size) + die(_("serious inflate inconsistency")); + + git_inflate_end(&stream); + free(inbuf); + if (consume) { + free(data); + data = NULL; + } + return data; +} + +static void *get_data_from_pack(struct object_entry *obj) +{ + return unpack_data(obj, NULL, NULL); +} + +static int compare_delta_bases(const union delta_base *base1, + const union delta_base *base2, + enum object_type type1, + enum object_type type2) +{ + int cmp = type1 - type2; + if (cmp) + return cmp; + return memcmp(base1, base2, UNION_BASE_SZ); +} + +static int find_delta(const union delta_base *base, enum object_type type) +{ + int first = 0, last = nr_deltas; + + while (first < last) { + int next = (first + last) / 2; + struct delta_entry *delta = &deltas[next]; + int cmp; + + cmp = compare_delta_bases(base, &delta->base, + type, objects[delta->obj_no].type); + if (!cmp) + return next; + if (cmp < 0) { + last = next; + continue; + } + first = next+1; + } + return -first-1; +} + +static void find_delta_children(const union delta_base *base, + int *first_index, int *last_index, + enum object_type type) +{ + int first = find_delta(base, type); + int last = first; + int end = nr_deltas - 1; + + if (first < 0) { + *first_index = 0; + *last_index = -1; + return; + } + while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ)) + --first; + while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ)) + ++last; + *first_index = first; + *last_index = last; +} + +struct compare_data { + struct object_entry *entry; + struct git_istream *st; + unsigned char *buf; + unsigned long buf_size; +}; + +static int compare_objects(const unsigned char *buf, unsigned long size, + void *cb_data) +{ + struct compare_data *data = cb_data; + + if (data->buf_size < size) { + free(data->buf); + data->buf = xmalloc(size); + data->buf_size = size; + } + + while (size) { + ssize_t len = read_istream(data->st, data->buf, size); + if (len == 0) + die(_("SHA1 COLLISION FOUND WITH %s !"), + sha1_to_hex(data->entry->idx.sha1)); + if (len < 0) + die(_("unable to read %s"), + sha1_to_hex(data->entry->idx.sha1)); + if (memcmp(buf, data->buf, len)) + die(_("SHA1 COLLISION FOUND WITH %s !"), + sha1_to_hex(data->entry->idx.sha1)); + size -= len; + buf += len; + } + return 0; +} + +static int check_collison(struct object_entry *entry) +{ + struct compare_data data; + enum object_type type; + unsigned long size; + + if (entry->size <= big_file_threshold || entry->type != OBJ_BLOB) + return -1; + + memset(&data, 0, sizeof(data)); + data.entry = entry; + data.st = open_istream(entry->idx.sha1, &type, &size, NULL); + if (!data.st) + return -1; + if (size != entry->size || type != entry->type) + die(_("SHA1 COLLISION FOUND WITH %s !"), + sha1_to_hex(entry->idx.sha1)); + unpack_data(entry, compare_objects, &data); + close_istream(data.st); + free(data.buf); + return 0; +} + +static void sha1_object(const void *data, struct object_entry *obj_entry, + unsigned long size, enum object_type type, + const unsigned char *sha1) +{ + void *new_data = NULL; + int collision_test_needed; + + assert(data || obj_entry); + + read_lock(); + collision_test_needed = has_sha1_file(sha1); + read_unlock(); + + if (collision_test_needed && !data) { + read_lock(); + if (!check_collison(obj_entry)) + collision_test_needed = 0; + read_unlock(); + } + if (collision_test_needed) { + void *has_data; + enum object_type has_type; + unsigned long has_size; + read_lock(); + has_type = sha1_object_info(sha1, &has_size); + if (has_type != type || has_size != size) + die(_("SHA1 COLLISION FOUND WITH %s !"), sha1_to_hex(sha1)); + has_data = read_sha1_file(sha1, &has_type, &has_size); + read_unlock(); + if (!data) + data = new_data = get_data_from_pack(obj_entry); + if (!has_data) + die(_("cannot read existing object %s"), sha1_to_hex(sha1)); + if (size != has_size || type != has_type || + memcmp(data, has_data, size) != 0) + die(_("SHA1 COLLISION FOUND WITH %s !"), sha1_to_hex(sha1)); + free(has_data); + } + + if (strict) { + read_lock(); + if (type == OBJ_BLOB) { + struct blob *blob = lookup_blob(sha1); + if (blob) + blob->object.flags |= FLAG_CHECKED; + else + die(_("invalid blob object %s"), sha1_to_hex(sha1)); + } else { + struct object *obj; + int eaten; + void *buf = (void *) data; + + if (!buf) + buf = new_data = get_data_from_pack(obj_entry); + + /* + * we do not need to free the memory here, as the + * buf is deleted by the caller. + */ + obj = parse_object_buffer(sha1, type, size, buf, &eaten); + if (!obj) + die(_("invalid %s"), typename(type)); + if (fsck_object(obj, 1, fsck_error_function)) + die(_("Error in object")); + if (fsck_walk(obj, mark_link, NULL)) + die(_("Not all child objects of %s are reachable"), sha1_to_hex(obj->sha1)); + + if (obj->type == OBJ_TREE) { + struct tree *item = (struct tree *) obj; + item->buffer = NULL; + } + if (obj->type == OBJ_COMMIT) { + struct commit *commit = (struct commit *) obj; + commit->buffer = NULL; + } + obj->flags |= FLAG_CHECKED; + } + read_unlock(); + } + + free(new_data); +} + +/* + * This function is part of find_unresolved_deltas(). There are two + * walkers going in the opposite ways. + * + * The first one in find_unresolved_deltas() traverses down from + * parent node to children, deflating nodes along the way. However, + * memory for deflated nodes is limited by delta_base_cache_limit, so + * at some point parent node's deflated content may be freed. + * + * The second walker is this function, which goes from current node up + * to top parent if necessary to deflate the node. In normal + * situation, its parent node would be already deflated, so it just + * needs to apply delta. + * + * In the worst case scenario, parent node is no longer deflated because + * we're running out of delta_base_cache_limit; we need to re-deflate + * parents, possibly up to the top base. + * + * All deflated objects here are subject to be freed if we exceed + * delta_base_cache_limit, just like in find_unresolved_deltas(), we + * just need to make sure the last node is not freed. + */ +static void *get_base_data(struct base_data *c) +{ + if (!c->data) { + struct object_entry *obj = c->obj; + struct base_data **delta = NULL; + int delta_nr = 0, delta_alloc = 0; + + while (is_delta_type(c->obj->type) && !c->data) { + ALLOC_GROW(delta, delta_nr + 1, delta_alloc); + delta[delta_nr++] = c; + c = c->base; + } + if (!delta_nr) { + c->data = get_data_from_pack(obj); + c->size = obj->size; + get_thread_data()->base_cache_used += c->size; + prune_base_data(c); + } + for (; delta_nr > 0; delta_nr--) { + void *base, *raw; + c = delta[delta_nr - 1]; + obj = c->obj; + base = get_base_data(c->base); + raw = get_data_from_pack(obj); + c->data = patch_delta( + base, c->base->size, + raw, obj->size, + &c->size); + free(raw); + if (!c->data) + bad_object(obj->idx.offset, _("failed to apply delta")); + get_thread_data()->base_cache_used += c->size; + prune_base_data(c); + } + free(delta); + } + return c->data; +} + +static void resolve_delta(struct object_entry *delta_obj, + struct base_data *base, struct base_data *result) +{ + void *base_data, *delta_data; + + delta_obj->real_type = base->obj->real_type; + delta_obj->delta_depth = base->obj->delta_depth + 1; + if (deepest_delta < delta_obj->delta_depth) + deepest_delta = delta_obj->delta_depth; + delta_obj->base_object_no = base->obj - objects; + delta_data = get_data_from_pack(delta_obj); + base_data = get_base_data(base); + result->obj = delta_obj; + result->data = patch_delta(base_data, base->size, + delta_data, delta_obj->size, &result->size); + free(delta_data); + if (!result->data) + bad_object(delta_obj->idx.offset, _("failed to apply delta")); + hash_sha1_file(result->data, result->size, + typename(delta_obj->real_type), delta_obj->idx.sha1); + sha1_object(result->data, NULL, result->size, delta_obj->real_type, + delta_obj->idx.sha1); + counter_lock(); + nr_resolved_deltas++; + counter_unlock(); +} + +static struct base_data *find_unresolved_deltas_1(struct base_data *base, + struct base_data *prev_base) +{ + if (base->ref_last == -1 && base->ofs_last == -1) { + union delta_base base_spec; + + hashcpy(base_spec.sha1, base->obj->idx.sha1); + find_delta_children(&base_spec, + &base->ref_first, &base->ref_last, OBJ_REF_DELTA); + + memset(&base_spec, 0, sizeof(base_spec)); + base_spec.offset = base->obj->idx.offset; + find_delta_children(&base_spec, + &base->ofs_first, &base->ofs_last, OBJ_OFS_DELTA); + + if (base->ref_last == -1 && base->ofs_last == -1) { + free(base->data); + return NULL; + } + + link_base_data(prev_base, base); + } + + if (base->ref_first <= base->ref_last) { + struct object_entry *child = objects + deltas[base->ref_first].obj_no; + struct base_data *result = alloc_base_data(); + + assert(child->real_type == OBJ_REF_DELTA); + resolve_delta(child, base, result); + if (base->ref_first == base->ref_last && base->ofs_last == -1) + free_base_data(base); + + base->ref_first++; + return result; + } + + if (base->ofs_first <= base->ofs_last) { + struct object_entry *child = objects + deltas[base->ofs_first].obj_no; + struct base_data *result = alloc_base_data(); + + assert(child->real_type == OBJ_OFS_DELTA); + resolve_delta(child, base, result); + if (base->ofs_first == base->ofs_last) + free_base_data(base); + + base->ofs_first++; + return result; + } + + unlink_base_data(base); + return NULL; +} + +static void find_unresolved_deltas(struct base_data *base) +{ + struct base_data *new_base, *prev_base = NULL; + for (;;) { + new_base = find_unresolved_deltas_1(base, prev_base); + + if (new_base) { + prev_base = base; + base = new_base; + } else { + free(base); + base = prev_base; + if (!base) + return; + prev_base = base->base; + } + } +} + +static int compare_delta_entry(const void *a, const void *b) +{ + const struct delta_entry *delta_a = a; + const struct delta_entry *delta_b = b; + + /* group by type (ref vs ofs) and then by value (sha-1 or offset) */ + return compare_delta_bases(&delta_a->base, &delta_b->base, + objects[delta_a->obj_no].type, + objects[delta_b->obj_no].type); +} + +static void resolve_base(struct object_entry *obj) +{ + struct base_data *base_obj = alloc_base_data(); + base_obj->obj = obj; + base_obj->data = NULL; + find_unresolved_deltas(base_obj); +} + +#ifndef NO_PTHREADS +static void *threaded_second_pass(void *data) +{ + set_thread_data(data); + for (;;) { + int i; + work_lock(); + display_progress(progress, nr_resolved_deltas); + while (nr_dispatched < nr_objects && + is_delta_type(objects[nr_dispatched].type)) + nr_dispatched++; + if (nr_dispatched >= nr_objects) { + work_unlock(); + break; + } + i = nr_dispatched++; + work_unlock(); + + resolve_base(&objects[i]); + } + return NULL; +} +#endif + +/* + * First pass: + * - find locations of all objects; + * - calculate SHA1 of all non-delta objects; + * - remember base (SHA1 or offset) for all deltas. + */ +static void parse_pack_objects(unsigned char *sha1) +{ + int i, nr_delays = 0; + struct delta_entry *delta = deltas; + struct stat st; + + if (verbose) + progress = start_progress( + from_stdin ? _("Receiving objects") : _("Indexing objects"), + nr_objects); + for (i = 0; i < nr_objects; i++) { + struct object_entry *obj = &objects[i]; + void *data = unpack_raw_entry(obj, &delta->base, obj->idx.sha1); + obj->real_type = obj->type; + if (is_delta_type(obj->type)) { + nr_deltas++; + delta->obj_no = i; + delta++; + } else if (!data) { + /* large blobs, check later */ + obj->real_type = OBJ_BAD; + nr_delays++; + } else + sha1_object(data, NULL, obj->size, obj->type, obj->idx.sha1); + free(data); + display_progress(progress, i+1); + } + objects[i].idx.offset = consumed_bytes; + stop_progress(&progress); + + /* Check pack integrity */ + flush(); + git_SHA1_Final(sha1, &input_ctx); + if (hashcmp(fill(20), sha1)) + die(_("pack is corrupted (SHA1 mismatch)")); + use(20); + + /* If input_fd is a file, we should have reached its end now. */ + if (fstat(input_fd, &st)) + die_errno(_("cannot fstat packfile")); + if (S_ISREG(st.st_mode) && + lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size) + die(_("pack has junk at the end")); + + for (i = 0; i < nr_objects; i++) { + struct object_entry *obj = &objects[i]; + if (obj->real_type != OBJ_BAD) + continue; + obj->real_type = obj->type; + sha1_object(NULL, obj, obj->size, obj->type, obj->idx.sha1); + nr_delays--; + } + if (nr_delays) + die(_("confusion beyond insanity in parse_pack_objects()")); +} + +/* + * Second pass: + * - for all non-delta objects, look if it is used as a base for + * deltas; + * - if used as a base, uncompress the object and apply all deltas, + * recursively checking if the resulting object is used as a base + * for some more deltas. + */ +static void resolve_deltas(void) +{ + int i; + + if (!nr_deltas) + return; + + /* Sort deltas by base SHA1/offset for fast searching */ + qsort(deltas, nr_deltas, sizeof(struct delta_entry), + compare_delta_entry); + + if (verbose) + progress = start_progress(_("Resolving deltas"), nr_deltas); + +#ifndef NO_PTHREADS + nr_dispatched = 0; + if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) { + init_thread(); + for (i = 0; i < nr_threads; i++) { + int ret = pthread_create(&thread_data[i].thread, NULL, + threaded_second_pass, thread_data + i); + if (ret) + die(_("unable to create thread: %s"), + strerror(ret)); + } + for (i = 0; i < nr_threads; i++) + pthread_join(thread_data[i].thread, NULL); + cleanup_thread(); + return; + } +#endif + + for (i = 0; i < nr_objects; i++) { + struct object_entry *obj = &objects[i]; + + if (is_delta_type(obj->type)) + continue; + resolve_base(obj); + display_progress(progress, nr_resolved_deltas); + } +} + +/* + * Third pass: + * - append objects to convert thin pack to full pack if required + * - write the final 20-byte SHA-1 + */ +static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved); +static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_sha1) +{ + if (nr_deltas == nr_resolved_deltas) { + stop_progress(&progress); + /* Flush remaining pack final 20-byte SHA1. */ + flush(); + return; + } + + if (fix_thin_pack) { + struct sha1file *f; + unsigned char read_sha1[20], tail_sha1[20]; + char msg[48]; + int nr_unresolved = nr_deltas - nr_resolved_deltas; + int nr_objects_initial = nr_objects; + if (nr_unresolved <= 0) + die(_("confusion beyond insanity")); + objects = xrealloc(objects, + (nr_objects + nr_unresolved + 1) + * sizeof(*objects)); + f = sha1fd(output_fd, curr_pack); + fix_unresolved_deltas(f, nr_unresolved); + sprintf(msg, _("completed with %d local objects"), + nr_objects - nr_objects_initial); + stop_progress_msg(&progress, msg); + sha1close(f, tail_sha1, 0); + hashcpy(read_sha1, pack_sha1); + fixup_pack_header_footer(output_fd, pack_sha1, + curr_pack, nr_objects, + read_sha1, consumed_bytes-20); + if (hashcmp(read_sha1, tail_sha1) != 0) + die(_("Unexpected tail checksum for %s " + "(disk corruption?)"), curr_pack); + } + if (nr_deltas != nr_resolved_deltas) + die(Q_("pack has %d unresolved delta", + "pack has %d unresolved deltas", + nr_deltas - nr_resolved_deltas), + nr_deltas - nr_resolved_deltas); +} + +static int write_compressed(struct sha1file *f, void *in, unsigned int size) +{ + git_zstream stream; + int status; + unsigned char outbuf[4096]; + + memset(&stream, 0, sizeof(stream)); + git_deflate_init(&stream, zlib_compression_level); + stream.next_in = in; + stream.avail_in = size; + + do { + stream.next_out = outbuf; + stream.avail_out = sizeof(outbuf); + status = git_deflate(&stream, Z_FINISH); + sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out); + } while (status == Z_OK); + + if (status != Z_STREAM_END) + die(_("unable to deflate appended object (%d)"), status); + size = stream.total_out; + git_deflate_end(&stream); + return size; +} + +static struct object_entry *append_obj_to_pack(struct sha1file *f, + const unsigned char *sha1, void *buf, + unsigned long size, enum object_type type) +{ + struct object_entry *obj = &objects[nr_objects++]; + unsigned char header[10]; + unsigned long s = size; + int n = 0; + unsigned char c = (type << 4) | (s & 15); + s >>= 4; + while (s) { + header[n++] = c | 0x80; + c = s & 0x7f; + s >>= 7; + } + header[n++] = c; + crc32_begin(f); + sha1write(f, header, n); + obj[0].size = size; + obj[0].hdr_size = n; + obj[0].type = type; + obj[0].real_type = type; + obj[1].idx.offset = obj[0].idx.offset + n; + obj[1].idx.offset += write_compressed(f, buf, size); + obj[0].idx.crc32 = crc32_end(f); + sha1flush(f); + hashcpy(obj->idx.sha1, sha1); + return obj; +} + +static int delta_pos_compare(const void *_a, const void *_b) +{ + struct delta_entry *a = *(struct delta_entry **)_a; + struct delta_entry *b = *(struct delta_entry **)_b; + return a->obj_no - b->obj_no; +} + +static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved) +{ + struct delta_entry **sorted_by_pos; + int i, n = 0; + + /* + * Since many unresolved deltas may well be themselves base objects + * for more unresolved deltas, we really want to include the + * smallest number of base objects that would cover as much delta + * as possible by picking the + * trunc deltas first, allowing for other deltas to resolve without + * additional base objects. Since most base objects are to be found + * before deltas depending on them, a good heuristic is to start + * resolving deltas in the same order as their position in the pack. + */ + sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos)); + for (i = 0; i < nr_deltas; i++) { + if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA) + continue; + sorted_by_pos[n++] = &deltas[i]; + } + qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare); + + for (i = 0; i < n; i++) { + struct delta_entry *d = sorted_by_pos[i]; + enum object_type type; + struct base_data *base_obj = alloc_base_data(); + + if (objects[d->obj_no].real_type != OBJ_REF_DELTA) + continue; + base_obj->data = read_sha1_file(d->base.sha1, &type, &base_obj->size); + if (!base_obj->data) + continue; + + if (check_sha1_signature(d->base.sha1, base_obj->data, + base_obj->size, typename(type))) + die(_("local object %s is corrupt"), sha1_to_hex(d->base.sha1)); + base_obj->obj = append_obj_to_pack(f, d->base.sha1, + base_obj->data, base_obj->size, type); + find_unresolved_deltas(base_obj); + display_progress(progress, nr_resolved_deltas); + } + free(sorted_by_pos); +} + +static void final(const char *final_pack_name, const char *curr_pack_name, + const char *final_index_name, const char *curr_index_name, + const char *keep_name, const char *keep_msg, + unsigned char *sha1) +{ + const char *report = "pack"; + char name[PATH_MAX]; + int err; + + if (!from_stdin) { + close(input_fd); + } else { + fsync_or_die(output_fd, curr_pack_name); + err = close(output_fd); + if (err) + die_errno(_("error while closing pack file")); + } + + if (keep_msg) { + int keep_fd, keep_msg_len = strlen(keep_msg); + + if (!keep_name) + keep_fd = odb_pack_keep(name, sizeof(name), sha1); + else + keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600); + + if (keep_fd < 0) { + if (errno != EEXIST) + die_errno(_("cannot write keep file '%s'"), + keep_name); + } else { + if (keep_msg_len > 0) { + write_or_die(keep_fd, keep_msg, keep_msg_len); + write_or_die(keep_fd, "\n", 1); + } + if (close(keep_fd) != 0) + die_errno(_("cannot close written keep file '%s'"), + keep_name); + report = "keep"; + } + } + + if (final_pack_name != curr_pack_name) { + if (!final_pack_name) { + snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", + get_object_directory(), sha1_to_hex(sha1)); + final_pack_name = name; + } + if (move_temp_to_file(curr_pack_name, final_pack_name)) + die(_("cannot store pack file")); + } else if (from_stdin) + chmod(final_pack_name, 0444); + + if (final_index_name != curr_index_name) { + if (!final_index_name) { + snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", + get_object_directory(), sha1_to_hex(sha1)); + final_index_name = name; + } + if (move_temp_to_file(curr_index_name, final_index_name)) + die(_("cannot store index file")); + } else + chmod(final_index_name, 0444); + + if (!from_stdin) { + printf("%s\n", sha1_to_hex(sha1)); + } else { + char buf[48]; + int len = snprintf(buf, sizeof(buf), "%s\t%s\n", + report, sha1_to_hex(sha1)); + write_or_die(1, buf, len); + + /* + * Let's just mimic git-unpack-objects here and write + * the last part of the input buffer to stdout. + */ + while (input_len) { + err = xwrite(1, input_buffer + input_offset, input_len); + if (err <= 0) + break; + input_len -= err; + input_offset += err; + } + } +} + +static int git_index_pack_config(const char *k, const char *v, void *cb) +{ + struct pack_idx_option *opts = cb; + + if (!strcmp(k, "pack.indexversion")) { + opts->version = git_config_int(k, v); + if (opts->version > 2) + die(_("bad pack.indexversion=%"PRIu32), opts->version); + return 0; + } + if (!strcmp(k, "pack.threads")) { + nr_threads = git_config_int(k, v); + if (nr_threads < 0) + die(_("invalid number of threads specified (%d)"), + nr_threads); +#ifdef NO_PTHREADS + if (nr_threads != 1) + warning(_("no threads support, ignoring %s"), k); + nr_threads = 1; +#endif + return 0; + } + return git_default_config(k, v, cb); +} + +static int cmp_uint32(const void *a_, const void *b_) +{ + uint32_t a = *((uint32_t *)a_); + uint32_t b = *((uint32_t *)b_); + + return (a < b) ? -1 : (a != b); +} + +static void read_v2_anomalous_offsets(struct packed_git *p, + struct pack_idx_option *opts) +{ + const uint32_t *idx1, *idx2; + uint32_t i; + + /* The address of the 4-byte offset table */ + idx1 = (((const uint32_t *)p->index_data) + + 2 /* 8-byte header */ + + 256 /* fan out */ + + 5 * p->num_objects /* 20-byte SHA-1 table */ + + p->num_objects /* CRC32 table */ + ); + + /* The address of the 8-byte offset table */ + idx2 = idx1 + p->num_objects; + + for (i = 0; i < p->num_objects; i++) { + uint32_t off = ntohl(idx1[i]); + if (!(off & 0x80000000)) + continue; + off = off & 0x7fffffff; + if (idx2[off * 2]) + continue; + /* + * The real offset is ntohl(idx2[off * 2]) in high 4 + * octets, and ntohl(idx2[off * 2 + 1]) in low 4 + * octets. But idx2[off * 2] is Zero!!! + */ + ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc); + opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]); + } + + if (1 < opts->anomaly_nr) + qsort(opts->anomaly, opts->anomaly_nr, sizeof(uint32_t), cmp_uint32); +} + +static void read_idx_option(struct pack_idx_option *opts, const char *pack_name) +{ + struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1); + + if (!p) + die(_("Cannot open existing pack file '%s'"), pack_name); + if (open_pack_index(p)) + die(_("Cannot open existing pack idx file for '%s'"), pack_name); + + /* Read the attributes from the existing idx file */ + opts->version = p->index_version; + + if (opts->version == 2) + read_v2_anomalous_offsets(p, opts); + + /* + * Get rid of the idx file as we do not need it anymore. + * NEEDSWORK: extract this bit from free_pack_by_name() in + * sha1_file.c, perhaps? It shouldn't matter very much as we + * know we haven't installed this pack (hence we never have + * read anything from it). + */ + close_pack_index(p); + free(p); +} + +static void show_pack_info(int stat_only) +{ + int i, baseobjects = nr_objects - nr_deltas; + unsigned long *chain_histogram = NULL; + + if (deepest_delta) + chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long)); + + for (i = 0; i < nr_objects; i++) { + struct object_entry *obj = &objects[i]; + + if (is_delta_type(obj->type)) + chain_histogram[obj->delta_depth - 1]++; + if (stat_only) + continue; + printf("%s %-6s %lu %lu %"PRIuMAX, + sha1_to_hex(obj->idx.sha1), + typename(obj->real_type), obj->size, + (unsigned long)(obj[1].idx.offset - obj->idx.offset), + (uintmax_t)obj->idx.offset); + if (is_delta_type(obj->type)) { + struct object_entry *bobj = &objects[obj->base_object_no]; + printf(" %u %s", obj->delta_depth, sha1_to_hex(bobj->idx.sha1)); + } + putchar('\n'); + } + + if (baseobjects) + printf_ln(Q_("non delta: %d object", + "non delta: %d objects", + baseobjects), + baseobjects); + for (i = 0; i < deepest_delta; i++) { + if (!chain_histogram[i]) + continue; + printf_ln(Q_("chain length = %d: %lu object", + "chain length = %d: %lu objects", + chain_histogram[i]), + i + 1, + chain_histogram[i]); + } +} + +int cmd_index_pack(int argc, const char **argv, const char *prefix) +{ + int i, fix_thin_pack = 0, verify = 0, stat_only = 0, stat = 0; + const char *curr_pack, *curr_index; + const char *index_name = NULL, *pack_name = NULL; + const char *keep_name = NULL, *keep_msg = NULL; + char *index_name_buf = NULL, *keep_name_buf = NULL; + struct pack_idx_entry **idx_objects; + struct pack_idx_option opts; + unsigned char pack_sha1[20]; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage(index_pack_usage); + + read_replace_refs = 0; + + reset_pack_idx_option(&opts); + git_config(git_index_pack_config, &opts); + if (prefix && chdir(prefix)) + die(_("Cannot come back to cwd")); + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + + if (*arg == '-') { + if (!strcmp(arg, "--stdin")) { + from_stdin = 1; + } else if (!strcmp(arg, "--fix-thin")) { + fix_thin_pack = 1; + } else if (!strcmp(arg, "--strict")) { + strict = 1; + } else if (!strcmp(arg, "--verify")) { + verify = 1; + } else if (!strcmp(arg, "--verify-stat")) { + verify = 1; + stat = 1; + } else if (!strcmp(arg, "--verify-stat-only")) { + verify = 1; + stat = 1; + stat_only = 1; + } else if (!strcmp(arg, "--keep")) { + keep_msg = ""; + } else if (!prefixcmp(arg, "--keep=")) { + keep_msg = arg + 7; + } else if (!prefixcmp(arg, "--threads=")) { + char *end; + nr_threads = strtoul(arg+10, &end, 0); + if (!arg[10] || *end || nr_threads < 0) + usage(index_pack_usage); +#ifdef NO_PTHREADS + if (nr_threads != 1) + warning(_("no threads support, " + "ignoring %s"), arg); + nr_threads = 1; +#endif + } else if (!prefixcmp(arg, "--pack_header=")) { + struct pack_header *hdr; + char *c; + + hdr = (struct pack_header *)input_buffer; + hdr->hdr_signature = htonl(PACK_SIGNATURE); + hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); + if (*c != ',') + die(_("bad %s"), arg); + hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); + if (*c) + die(_("bad %s"), arg); + input_len = sizeof(*hdr); + } else if (!strcmp(arg, "-v")) { + verbose = 1; + } else if (!strcmp(arg, "-o")) { + if (index_name || (i+1) >= argc) + usage(index_pack_usage); + index_name = argv[++i]; + } else if (!prefixcmp(arg, "--index-version=")) { + char *c; + opts.version = strtoul(arg + 16, &c, 10); + if (opts.version > 2) + die(_("bad %s"), arg); + if (*c == ',') + opts.off32_limit = strtoul(c+1, &c, 0); + if (*c || opts.off32_limit & 0x80000000) + die(_("bad %s"), arg); + } else + usage(index_pack_usage); + continue; + } + + if (pack_name) + usage(index_pack_usage); + pack_name = arg; + } + + if (!pack_name && !from_stdin) + usage(index_pack_usage); + if (fix_thin_pack && !from_stdin) + die(_("--fix-thin cannot be used without --stdin")); + if (!index_name && pack_name) { + int len = strlen(pack_name); + if (!has_extension(pack_name, ".pack")) + die(_("packfile name '%s' does not end with '.pack'"), + pack_name); + index_name_buf = xmalloc(len); + memcpy(index_name_buf, pack_name, len - 5); + strcpy(index_name_buf + len - 5, ".idx"); + index_name = index_name_buf; + } + if (keep_msg && !keep_name && pack_name) { + int len = strlen(pack_name); + if (!has_extension(pack_name, ".pack")) + die(_("packfile name '%s' does not end with '.pack'"), + pack_name); + keep_name_buf = xmalloc(len); + memcpy(keep_name_buf, pack_name, len - 5); + strcpy(keep_name_buf + len - 5, ".keep"); + keep_name = keep_name_buf; + } + if (verify) { + if (!index_name) + die(_("--verify with no packfile name given")); + read_idx_option(&opts, index_name); + opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT; + } + if (strict) + opts.flags |= WRITE_IDX_STRICT; + +#ifndef NO_PTHREADS + if (!nr_threads) { + nr_threads = online_cpus(); + /* An experiment showed that more threads does not mean faster */ + if (nr_threads > 3) + nr_threads = 3; + } +#endif + + curr_pack = open_pack_file(pack_name); + parse_pack_header(); + objects = xcalloc(nr_objects + 1, sizeof(struct object_entry)); + deltas = xcalloc(nr_objects, sizeof(struct delta_entry)); + parse_pack_objects(pack_sha1); + resolve_deltas(); + conclude_pack(fix_thin_pack, curr_pack, pack_sha1); + free(deltas); + if (strict) + check_objects(); + + if (stat) + show_pack_info(stat_only); + + idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *)); + for (i = 0; i < nr_objects; i++) + idx_objects[i] = &objects[i].idx; + curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1); + free(idx_objects); + + if (!verify) + final(pack_name, curr_pack, + index_name, curr_index, + keep_name, keep_msg, + pack_sha1); + else + close(input_fd); + free(objects); + free(index_name_buf); + free(keep_name_buf); + if (pack_name == NULL) + free((void *) curr_pack); + if (index_name == NULL) + free((void *) curr_index); + + return 0; +} diff --git a/builtin/init-db.c b/builtin/init-db.c new file mode 100644 index 0000000000..78aa3872dd --- /dev/null +++ b/builtin/init-db.c @@ -0,0 +1,597 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "builtin.h" +#include "exec_cmd.h" +#include "parse-options.h" + +#ifndef DEFAULT_GIT_TEMPLATE_DIR +#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" +#endif + +#ifdef NO_TRUSTABLE_FILEMODE +#define TEST_FILEMODE 0 +#else +#define TEST_FILEMODE 1 +#endif + +static int init_is_bare_repository = 0; +static int init_shared_repository = -1; +static const char *init_db_template_dir; +static const char *git_link; + +static void safe_create_dir(const char *dir, int share) +{ + if (mkdir(dir, 0777) < 0) { + if (errno != EEXIST) { + perror(dir); + exit(1); + } + } + else if (share && adjust_shared_perm(dir)) + die(_("Could not make %s writable by group"), dir); +} + +static void copy_templates_1(char *path, int baselen, + char *template, int template_baselen, + DIR *dir) +{ + struct dirent *de; + + /* Note: if ".git/hooks" file exists in the repository being + * re-initialized, /etc/core-git/templates/hooks/update would + * cause "git init" to fail here. I think this is sane but + * it means that the set of templates we ship by default, along + * with the way the namespace under .git/ is organized, should + * be really carefully chosen. + */ + safe_create_dir(path, 1); + while ((de = readdir(dir)) != NULL) { + struct stat st_git, st_template; + int namelen; + int exists = 0; + + if (de->d_name[0] == '.') + continue; + namelen = strlen(de->d_name); + if ((PATH_MAX <= baselen + namelen) || + (PATH_MAX <= template_baselen + namelen)) + die(_("insanely long template name %s"), de->d_name); + memcpy(path + baselen, de->d_name, namelen+1); + memcpy(template + template_baselen, de->d_name, namelen+1); + if (lstat(path, &st_git)) { + if (errno != ENOENT) + die_errno(_("cannot stat '%s'"), path); + } + else + exists = 1; + + if (lstat(template, &st_template)) + die_errno(_("cannot stat template '%s'"), template); + + if (S_ISDIR(st_template.st_mode)) { + DIR *subdir = opendir(template); + int baselen_sub = baselen + namelen; + int template_baselen_sub = template_baselen + namelen; + if (!subdir) + die_errno(_("cannot opendir '%s'"), template); + path[baselen_sub++] = + template[template_baselen_sub++] = '/'; + path[baselen_sub] = + template[template_baselen_sub] = 0; + copy_templates_1(path, baselen_sub, + template, template_baselen_sub, + subdir); + closedir(subdir); + } + else if (exists) + continue; + else if (S_ISLNK(st_template.st_mode)) { + char lnk[256]; + int len; + len = readlink(template, lnk, sizeof(lnk)); + if (len < 0) + die_errno(_("cannot readlink '%s'"), template); + if (sizeof(lnk) <= len) + die(_("insanely long symlink %s"), template); + lnk[len] = 0; + if (symlink(lnk, path)) + die_errno(_("cannot symlink '%s' '%s'"), lnk, path); + } + else if (S_ISREG(st_template.st_mode)) { + if (copy_file(path, template, st_template.st_mode)) + die_errno(_("cannot copy '%s' to '%s'"), template, + path); + } + else + error(_("ignoring template %s"), template); + } +} + +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); + if (!template_dir) + template_dir = init_db_template_dir; + if (!template_dir) + template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); + if (!template_dir[0]) + return; + template_len = strlen(template_dir); + if (PATH_MAX <= (template_len+strlen("/config"))) + die(_("insanely long template path %s"), template_dir); + strcpy(template_path, template_dir); + if (template_path[template_len-1] != '/') { + template_path[template_len++] = '/'; + template_path[template_len] = 0; + } + dir = opendir(template_path); + if (!dir) { + warning(_("templates not found %s"), template_dir); + return; + } + + /* Make sure that template is from the correct vintage */ + strcpy(template_path + template_len, "config"); + repository_format_version = 0; + git_config_from_file(check_repository_format_version, + template_path, NULL); + template_path[template_len] = 0; + + if (repository_format_version && + repository_format_version != GIT_REPO_VERSION) { + warning(_("not copying templates of " + "a wrong format version %d from '%s'"), + repository_format_version, + template_dir); + closedir(dir); + return; + } + + memcpy(path, git_dir, len); + if (len && path[len - 1] != '/') + path[len++] = '/'; + path[len] = 0; + copy_templates_1(path, len, + template_path, template_len, + dir); + closedir(dir); +} + +static int git_init_db_config(const char *k, const char *v, void *cb) +{ + if (!strcmp(k, "init.templatedir")) + return git_config_pathname(&init_db_template_dir, k, v); + + return 0; +} + +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; + char repo_version_string[10]; + char junk[2]; + int reinit; + int filemode; + + if (len > sizeof(path)-50) + die(_("insane git directory %s"), git_dir); + memcpy(path, git_dir, len); + + if (len && path[len-1] != '/') + path[len++] = '/'; + + /* + * Create .git/refs/{heads,tags} + */ + safe_create_dir(git_path("refs"), 1); + safe_create_dir(git_path("refs/heads"), 1); + safe_create_dir(git_path("refs/tags"), 1); + + /* Just look for `init.templatedir` */ + git_config(git_init_db_config, NULL); + + /* First copy the templates -- we might have the default + * config file there, in which case we would want to read + * from it after installing. + */ + copy_templates(template_path); + + git_config(git_default_config, NULL); + is_bare_repository_cfg = init_is_bare_repository; + + /* reading existing config may have overwrote it */ + if (init_shared_repository != -1) + shared_repository = init_shared_repository; + + /* + * We would have created the above under user's umask -- under + * shared-repository settings, we would need to fix them up. + */ + if (shared_repository) { + 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")); + } + + /* + * Create the default symlink from ".git/HEAD" to the "master" + * branch, if it does not exist yet. + */ + strcpy(path + len, "HEAD"); + reinit = (!access(path, R_OK) + || readlink(path, junk, sizeof(junk)-1) != -1); + if (!reinit) { + if (create_symref("HEAD", "refs/heads/master", NULL) < 0) + exit(1); + } + + /* This forces creation of new config file */ + sprintf(repo_version_string, "%d", GIT_REPO_VERSION); + git_config_set("core.repositoryformatversion", repo_version_string); + + path[len] = 0; + strcpy(path + len, "config"); + + /* Check filemode trustability */ + filemode = TEST_FILEMODE; + if (TEST_FILEMODE && !lstat(path, &st1)) { + struct stat st2; + filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && + !lstat(path, &st2) && + st1.st_mode != st2.st_mode); + } + git_config_set("core.filemode", filemode ? "true" : "false"); + + if (is_bare_repository()) + git_config_set("core.bare", "true"); + else { + const char *work_tree = get_git_work_tree(); + git_config_set("core.bare", "false"); + /* allow template config file to override the default */ + if (log_all_ref_updates == -1) + git_config_set("core.logallrefupdates", "true"); + if (prefixcmp(git_dir, work_tree) || + strcmp(git_dir + strlen(work_tree), "/.git")) { + git_config_set("core.worktree", work_tree); + } + } + + if (!reinit) { + /* Check if symlink is supported in the work tree */ + path[len] = 0; + strcpy(path + len, "tXXXXXX"); + if (!close(xmkstemp(path)) && + !unlink(path) && + !symlink("testing", path) && + !lstat(path, &st1) && + S_ISLNK(st1.st_mode)) + unlink(path); /* good */ + else + git_config_set("core.symlinks", "false"); + + /* Check if the filesystem is case-insensitive */ + path[len] = 0; + strcpy(path + len, "CoNfIg"); + if (!access(path, F_OK)) + git_config_set("core.ignorecase", "true"); + probe_utf8_pathname_composition(path, len); + } + + return reinit; +} + +static void create_object_directory(void) +{ + const char *object_directory = get_object_directory(); + int len = strlen(object_directory); + char *path = xmalloc(len + 40); + + memcpy(path, object_directory, len); + + safe_create_dir(object_directory, 1); + strcpy(path+len, "/pack"); + safe_create_dir(path, 1); + strcpy(path+len, "/info"); + safe_create_dir(path, 1); + + free(path); +} + +int set_git_dir_init(const char *git_dir, const char *real_git_dir, + int exist_ok) +{ + if (real_git_dir) { + struct stat st; + + if (!exist_ok && !stat(git_dir, &st)) + die(_("%s already exists"), git_dir); + + if (!exist_ok && !stat(real_git_dir, &st)) + die(_("%s already exists"), real_git_dir); + + /* + * make sure symlinks are resolved because we'll be + * moving the target repo later on in separate_git_dir() + */ + git_link = xstrdup(real_path(git_dir)); + } + else { + real_git_dir = real_path(git_dir); + git_link = NULL; + } + set_git_dir(real_path(real_git_dir)); + return 0; +} + +static void separate_git_dir(const char *git_dir) +{ + struct stat st; + FILE *fp; + + if (!stat(git_link, &st)) { + const char *src; + + if (S_ISREG(st.st_mode)) + src = read_gitfile(git_link); + else if (S_ISDIR(st.st_mode)) + src = git_link; + else + die(_("unable to handle file type %d"), (int)st.st_mode); + + if (rename(src, git_dir)) + die_errno(_("unable to move %s to %s"), src, git_dir); + } + + fp = fopen(git_link, "w"); + if (!fp) + die(_("Could not create git link %s"), git_link); + fprintf(fp, "gitdir: %s\n", git_dir); + fclose(fp); +} + +int init_db(const char *template_dir, unsigned int flags) +{ + int reinit; + const char *git_dir = get_git_dir(); + + if (git_link) + separate_git_dir(git_dir); + + safe_create_dir(git_dir, 0); + + init_is_bare_repository = is_bare_repository(); + + /* 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); + + create_object_directory(); + + 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 < 0) + /* force to the mode value */ + sprintf(buf, "0%o", -shared_repository); + else if (shared_repository == PERM_GROUP) + sprintf(buf, "%d", OLD_PERM_GROUP); + else if (shared_repository == PERM_EVERYBODY) + sprintf(buf, "%d", OLD_PERM_EVERYBODY); + else + die("oops"); + git_config_set("core.sharedrepository", buf); + git_config_set("receive.denyNonFastforwards", "true"); + } + + if (!(flags & INIT_DB_QUIET)) { + int len = strlen(git_dir); + + /* + * TRANSLATORS: The first '%s' is either "Reinitialized + * existing" or "Initialized empty", the second " shared" or + * "", and the last '%s%s' is the verbatim directory name. + */ + printf(_("%s%s Git repository in %s%s\n"), + reinit ? _("Reinitialized existing") : _("Initialized empty"), + shared_repository ? _(" shared") : "", + git_dir, len && git_dir[len-1] != '/' ? "/" : ""); + } + + return 0; +} + +static int guess_repository_type(const char *git_dir) +{ + char cwd[PATH_MAX]; + const char *slash; + + /* + * "GIT_DIR=. git init" is always bare. + * "GIT_DIR=`pwd` git init" too. + */ + if (!strcmp(".", git_dir)) + return 1; + if (!getcwd(cwd, sizeof(cwd))) + die_errno(_("cannot tell cwd")); + if (!strcmp(git_dir, cwd)) + return 1; + /* + * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. + */ + if (!strcmp(git_dir, ".git")) + return 0; + slash = strrchr(git_dir, '/'); + if (slash && !strcmp(slash, "/.git")) + return 0; + + /* + * Otherwise it is often bare. At this point + * we are just guessing. + */ + return 1; +} + +static int shared_callback(const struct option *opt, const char *arg, int unset) +{ + *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; + return 0; +} + +static const char *const init_db_usage[] = { + N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]"), + NULL +}; + +/* + * If you want to, you can share the DB area with any number of branches. + * That has advantages: you can save space by sharing all the SHA1 objects. + * On the other hand, it might just make lookup slower and messier. You + * be the judge. The default case is to have one DB per managed directory. + */ +int cmd_init_db(int argc, const char **argv, const char *prefix) +{ + const char *git_dir; + const char *real_git_dir = NULL; + const char *work_tree; + const char *template_dir = NULL; + unsigned int flags = 0; + const struct option init_db_options[] = { + OPT_STRING(0, "template", &template_dir, N_("template-directory"), + N_("directory from which templates will be used")), + OPT_SET_INT(0, "bare", &is_bare_repository_cfg, + N_("create a bare repository"), 1), + { OPTION_CALLBACK, 0, "shared", &init_shared_repository, + N_("permissions"), + N_("specify that the git repository is to be shared amongst several users"), + PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, + OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET), + OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), + N_("separate git dir from working tree")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); + + if (real_git_dir && !is_absolute_path(real_git_dir)) + real_git_dir = xstrdup(real_path(real_git_dir)); + + if (argc == 1) { + int mkdir_tried = 0; + retry: + if (chdir(argv[0]) < 0) { + if (!mkdir_tried) { + int saved; + /* + * At this point we haven't read any configuration, + * and we know shared_repository should always be 0; + * but just in case we play safe. + */ + saved = shared_repository; + shared_repository = 0; + switch (safe_create_leading_directories_const(argv[0])) { + case -3: + errno = EEXIST; + /* fallthru */ + case -1: + die_errno(_("cannot mkdir %s"), argv[0]); + break; + default: + break; + } + shared_repository = saved; + if (mkdir(argv[0], 0777) < 0) + die_errno(_("cannot mkdir %s"), argv[0]); + mkdir_tried = 1; + goto retry; + } + die_errno(_("cannot chdir to %s"), argv[0]); + } + } else if (0 < argc) { + usage(init_db_usage[0]); + } + if (is_bare_repository_cfg == 1) { + static char git_dir[PATH_MAX+1]; + + setenv(GIT_DIR_ENVIRONMENT, + getcwd(git_dir, sizeof(git_dir)), argc > 0); + } + + if (init_shared_repository != -1) + shared_repository = init_shared_repository; + + /* + * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR + * without --bare. Catch the error early. + */ + git_dir = getenv(GIT_DIR_ENVIRONMENT); + work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); + if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) + die(_("%s (or --work-tree=<directory>) not allowed without " + "specifying %s (or --git-dir=<directory>)"), + GIT_WORK_TREE_ENVIRONMENT, + GIT_DIR_ENVIRONMENT); + + /* + * Set up the default .git directory contents + */ + if (!git_dir) + git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; + + if (is_bare_repository_cfg < 0) + is_bare_repository_cfg = guess_repository_type(git_dir); + + if (!is_bare_repository_cfg) { + 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(real_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_errno (_("Cannot access current working directory")); + } + if (work_tree) + set_git_work_tree(real_path(work_tree)); + else + set_git_work_tree(git_work_tree_cfg); + if (access(get_git_work_tree(), X_OK)) + die_errno (_("Cannot access work tree '%s'"), + get_git_work_tree()); + } + else { + if (work_tree) + set_git_work_tree(real_path(work_tree)); + } + + set_git_dir_init(git_dir, real_git_dir, 1); + + return init_db(template_dir, flags); +} diff --git a/builtin/log.c b/builtin/log.c new file mode 100644 index 0000000000..8f0b2e84fe --- /dev/null +++ b/builtin/log.c @@ -0,0 +1,1591 @@ +/* + * Builtin "git log" and related commands (show, whatchanged) + * + * (C) Copyright 2006 Linus Torvalds + * 2006 Junio Hamano + */ +#include "cache.h" +#include "color.h" +#include "commit.h" +#include "diff.h" +#include "revision.h" +#include "log-tree.h" +#include "builtin.h" +#include "tag.h" +#include "reflog-walk.h" +#include "patch-ids.h" +#include "run-command.h" +#include "shortlog.h" +#include "remote.h" +#include "string-list.h" +#include "parse-options.h" +#include "branch.h" +#include "streaming.h" +#include "version.h" +#include "mailmap.h" + +/* Set a default date-time format for git log ("log.date" config variable) */ +static const char *default_date_mode = NULL; + +static int default_abbrev_commit; +static int default_show_root = 1; +static int decoration_style; +static int decoration_given; +static int use_mailmap_config; +static const char *fmt_patch_subject_prefix = "PATCH"; +static const char *fmt_pretty; + +static const char * const builtin_log_usage[] = { + N_("git log [<options>] [<since>..<until>] [[--] <path>...]\n") + N_(" or: git show [options] <object>..."), + NULL +}; + +static int parse_decoration_style(const char *var, const char *value) +{ + switch (git_config_maybe_bool(var, value)) { + case 1: + return DECORATE_SHORT_REFS; + case 0: + return 0; + default: + break; + } + if (!strcmp(value, "full")) + return DECORATE_FULL_REFS; + else if (!strcmp(value, "short")) + return DECORATE_SHORT_REFS; + return -1; +} + +static int decorate_callback(const struct option *opt, const char *arg, int unset) +{ + if (unset) + decoration_style = 0; + else if (arg) + decoration_style = parse_decoration_style("command line", arg); + else + decoration_style = DECORATE_SHORT_REFS; + + if (decoration_style < 0) + die("invalid --decorate option: %s", arg); + + decoration_given = 1; + + return 0; +} + +static void cmd_log_init_defaults(struct rev_info *rev) +{ + if (fmt_pretty) + get_commit_format(fmt_pretty, rev); + rev->verbose_header = 1; + DIFF_OPT_SET(&rev->diffopt, RECURSIVE); + rev->diffopt.stat_width = -1; /* use full terminal width */ + rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */ + rev->abbrev_commit = default_abbrev_commit; + rev->show_root_diff = default_show_root; + rev->subject_prefix = fmt_patch_subject_prefix; + DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV); + + if (default_date_mode) + rev->date_mode = parse_date_format(default_date_mode); +} + +static void cmd_log_init_finish(int argc, const char **argv, const char *prefix, + struct rev_info *rev, struct setup_revision_opt *opt) +{ + struct userformat_want w; + int quiet = 0, source = 0, mailmap = 0; + + const struct option builtin_log_options[] = { + OPT_BOOLEAN(0, "quiet", &quiet, N_("suppress diff output")), + OPT_BOOLEAN(0, "source", &source, N_("show source")), + OPT_BOOLEAN(0, "use-mailmap", &mailmap, N_("Use mail map file")), + { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"), + PARSE_OPT_OPTARG, decorate_callback}, + OPT_END() + }; + + mailmap = use_mailmap_config; + argc = parse_options(argc, argv, prefix, + builtin_log_options, builtin_log_usage, + PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | + PARSE_OPT_KEEP_DASHDASH); + + if (quiet) + rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT; + argc = setup_revisions(argc, argv, rev, opt); + + /* Any arguments at this point are not recognized */ + if (argc > 1) + die("unrecognized argument: %s", argv[1]); + + memset(&w, 0, sizeof(w)); + userformat_find_requirements(NULL, &w); + + if (!rev->show_notes_given && (!rev->pretty_given || w.notes)) + rev->show_notes = 1; + if (rev->show_notes) + init_display_notes(&rev->notes_opt); + + if (rev->diffopt.pickaxe || rev->diffopt.filter) + rev->always_show_header = 0; + if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) { + rev->always_show_header = 0; + if (rev->diffopt.pathspec.nr != 1) + usage("git logs can only follow renames on one pathname at a time"); + } + + if (source) + rev->show_source = 1; + + if (mailmap) { + rev->mailmap = xcalloc(1, sizeof(struct string_list)); + read_mailmap(rev->mailmap, NULL); + } + + if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) { + /* + * "log --pretty=raw" is special; ignore UI oriented + * configuration variables such as decoration. + */ + if (!decoration_given) + decoration_style = 0; + if (!rev->abbrev_commit_given) + rev->abbrev_commit = 0; + } + + if (decoration_style) { + rev->show_decorations = 1; + load_ref_decorations(decoration_style); + } + setup_pager(); +} + +static void cmd_log_init(int argc, const char **argv, const char *prefix, + struct rev_info *rev, struct setup_revision_opt *opt) +{ + cmd_log_init_defaults(rev); + cmd_log_init_finish(argc, argv, prefix, rev, opt); +} + +/* + * This gives a rough estimate for how many commits we + * will print out in the list. + */ +static int estimate_commit_count(struct rev_info *rev, struct commit_list *list) +{ + int n = 0; + + while (list) { + struct commit *commit = list->item; + unsigned int flags = commit->object.flags; + list = list->next; + if (!(flags & (TREESAME | UNINTERESTING))) + n++; + } + return n; +} + +static void show_early_header(struct rev_info *rev, const char *stage, int nr) +{ + if (rev->shown_one) { + rev->shown_one = 0; + if (rev->commit_format != CMIT_FMT_ONELINE) + putchar(rev->diffopt.line_termination); + } + printf(_("Final output: %d %s\n"), nr, stage); +} + +static struct itimerval early_output_timer; + +static void log_show_early(struct rev_info *revs, struct commit_list *list) +{ + int i = revs->early_output; + int show_header = 1; + + sort_in_topological_order(&list, revs->lifo); + while (list && i) { + struct commit *commit = list->item; + switch (simplify_commit(revs, commit)) { + case commit_show: + if (show_header) { + int n = estimate_commit_count(revs, list); + show_early_header(revs, "incomplete", n); + show_header = 0; + } + log_tree_commit(revs, commit); + i--; + break; + case commit_ignore: + break; + case commit_error: + return; + } + list = list->next; + } + + /* Did we already get enough commits for the early output? */ + if (!i) + return; + + /* + * ..if no, then repeat it twice a second until we + * do. + * + * NOTE! We don't use "it_interval", because if the + * reader isn't listening, we want our output to be + * throttled by the writing, and not have the timer + * trigger every second even if we're blocked on a + * reader! + */ + early_output_timer.it_value.tv_sec = 0; + early_output_timer.it_value.tv_usec = 500000; + setitimer(ITIMER_REAL, &early_output_timer, NULL); +} + +static void early_output(int signal) +{ + show_early_output = log_show_early; +} + +static void setup_early_output(struct rev_info *rev) +{ + struct sigaction sa; + + /* + * Set up the signal handler, minimally intrusively: + * we only set a single volatile integer word (not + * using sigatomic_t - trying to avoid unnecessary + * system dependencies and headers), and using + * SA_RESTART. + */ + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = early_output; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART; + sigaction(SIGALRM, &sa, NULL); + + /* + * If we can get the whole output in less than a + * tenth of a second, don't even bother doing the + * early-output thing.. + * + * This is a one-time-only trigger. + */ + early_output_timer.it_value.tv_sec = 0; + early_output_timer.it_value.tv_usec = 100000; + setitimer(ITIMER_REAL, &early_output_timer, NULL); +} + +static void finish_early_output(struct rev_info *rev) +{ + int n = estimate_commit_count(rev, rev->commits); + signal(SIGALRM, SIG_IGN); + show_early_header(rev, "done", n); +} + +static int cmd_log_walk(struct rev_info *rev) +{ + struct commit *commit; + int saved_nrl = 0; + int saved_dcctc = 0; + + if (rev->early_output) + setup_early_output(rev); + + if (prepare_revision_walk(rev)) + die(_("revision walk setup failed")); + + if (rev->early_output) + finish_early_output(rev); + + /* + * For --check and --exit-code, the exit code is based on CHECK_FAILED + * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to + * retain that state information if replacing rev->diffopt in this loop + */ + while ((commit = get_revision(rev)) != NULL) { + if (!log_tree_commit(rev, commit) && + rev->max_count >= 0) + /* + * We decremented max_count in get_revision, + * but we didn't actually show the commit. + */ + rev->max_count++; + if (!rev->reflog_info) { + /* we allow cycles in reflog ancestry */ + free(commit->buffer); + commit->buffer = NULL; + } + free_commit_list(commit->parents); + commit->parents = NULL; + if (saved_nrl < rev->diffopt.needed_rename_limit) + saved_nrl = rev->diffopt.needed_rename_limit; + if (rev->diffopt.degraded_cc_to_c) + saved_dcctc = 1; + } + rev->diffopt.degraded_cc_to_c = saved_dcctc; + rev->diffopt.needed_rename_limit = saved_nrl; + + if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF && + DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) { + return 02; + } + return diff_result_code(&rev->diffopt, 0); +} + +static int git_log_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "format.pretty")) + return git_config_string(&fmt_pretty, var, value); + if (!strcmp(var, "format.subjectprefix")) + return git_config_string(&fmt_patch_subject_prefix, var, value); + if (!strcmp(var, "log.abbrevcommit")) { + default_abbrev_commit = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "log.date")) + return git_config_string(&default_date_mode, var, value); + if (!strcmp(var, "log.decorate")) { + decoration_style = parse_decoration_style(var, value); + if (decoration_style < 0) + decoration_style = 0; /* maybe warn? */ + return 0; + } + if (!strcmp(var, "log.showroot")) { + default_show_root = git_config_bool(var, value); + return 0; + } + if (!prefixcmp(var, "color.decorate.")) + return parse_decorate_color_config(var, 15, value); + if (!strcmp(var, "log.mailmap")) { + use_mailmap_config = git_config_bool(var, value); + return 0; + } + + if (grep_config(var, value, cb) < 0) + return -1; + return git_diff_ui_config(var, value, cb); +} + +int cmd_whatchanged(int argc, const char **argv, const char *prefix) +{ + struct rev_info rev; + struct setup_revision_opt opt; + + init_grep_defaults(); + git_config(git_log_config, NULL); + + init_revisions(&rev, prefix); + rev.diff = 1; + rev.simplify_history = 0; + memset(&opt, 0, sizeof(opt)); + opt.def = "HEAD"; + opt.revarg_opt = REVARG_COMMITTISH; + cmd_log_init(argc, argv, prefix, &rev, &opt); + if (!rev.diffopt.output_format) + rev.diffopt.output_format = DIFF_FORMAT_RAW; + return cmd_log_walk(&rev); +} + +static void show_tagger(char *buf, int len, struct rev_info *rev) +{ + struct strbuf out = STRBUF_INIT; + struct pretty_print_context pp = {0}; + + pp.fmt = rev->commit_format; + pp.date_mode = rev->date_mode; + pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding()); + printf("%s", out.buf); + strbuf_release(&out); +} + +static int show_blob_object(const unsigned char *sha1, struct rev_info *rev) +{ + fflush(stdout); + return stream_blob_to_fd(1, sha1, NULL, 0); +} + +static int show_tag_object(const unsigned char *sha1, struct rev_info *rev) +{ + unsigned long size; + enum object_type type; + char *buf = read_sha1_file(sha1, &type, &size); + int offset = 0; + + if (!buf) + return error(_("Could not read object %s"), sha1_to_hex(sha1)); + + assert(type == OBJ_TAG); + while (offset < size && buf[offset] != '\n') { + int new_offset = offset + 1; + while (new_offset < size && buf[new_offset++] != '\n') + ; /* do nothing */ + if (!prefixcmp(buf + offset, "tagger ")) + show_tagger(buf + offset + 7, + new_offset - offset - 7, rev); + offset = new_offset; + } + + if (offset < size) + fwrite(buf + offset, size - offset, 1, stdout); + free(buf); + return 0; +} + +static int show_tree_object(const unsigned char *sha1, + const char *base, int baselen, + const char *pathname, unsigned mode, int stage, void *context) +{ + printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); + return 0; +} + +static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt) +{ + if (rev->ignore_merges) { + /* There was no "-m" on the command line */ + rev->ignore_merges = 0; + if (!rev->first_parent_only && !rev->combine_merges) { + /* No "--first-parent", "-c", nor "--cc" */ + rev->combine_merges = 1; + rev->dense_combined_merges = 1; + } + } + if (!rev->diffopt.output_format) + rev->diffopt.output_format = DIFF_FORMAT_PATCH; +} + +int cmd_show(int argc, const char **argv, const char *prefix) +{ + struct rev_info rev; + struct object_array_entry *objects; + struct setup_revision_opt opt; + struct pathspec match_all; + int i, count, ret = 0; + + init_grep_defaults(); + git_config(git_log_config, NULL); + + init_pathspec(&match_all, NULL); + init_revisions(&rev, prefix); + rev.diff = 1; + rev.always_show_header = 1; + rev.no_walk = REVISION_WALK_NO_WALK_SORTED; + rev.diffopt.stat_width = -1; /* Scale to real terminal size */ + + memset(&opt, 0, sizeof(opt)); + opt.def = "HEAD"; + opt.tweak = show_rev_tweak_rev; + cmd_log_init(argc, argv, prefix, &rev, &opt); + + if (!rev.no_walk) + return cmd_log_walk(&rev); + + count = rev.pending.nr; + objects = rev.pending.objects; + for (i = 0; i < count && !ret; i++) { + struct object *o = objects[i].item; + const char *name = objects[i].name; + switch (o->type) { + case OBJ_BLOB: + ret = show_blob_object(o->sha1, NULL); + break; + case OBJ_TAG: { + struct tag *t = (struct tag *)o; + + if (rev.shown_one) + putchar('\n'); + printf("%stag %s%s\n", + diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), + t->tag, + diff_get_color_opt(&rev.diffopt, DIFF_RESET)); + ret = show_tag_object(o->sha1, &rev); + rev.shown_one = 1; + if (ret) + break; + o = parse_object(t->tagged->sha1); + if (!o) + ret = error(_("Could not read object %s"), + sha1_to_hex(t->tagged->sha1)); + objects[i].item = o; + i--; + break; + } + case OBJ_TREE: + if (rev.shown_one) + putchar('\n'); + printf("%stree %s%s\n\n", + diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), + name, + diff_get_color_opt(&rev.diffopt, DIFF_RESET)); + read_tree_recursive((struct tree *)o, "", 0, 0, &match_all, + show_tree_object, NULL); + rev.shown_one = 1; + break; + case OBJ_COMMIT: + rev.pending.nr = rev.pending.alloc = 0; + rev.pending.objects = NULL; + add_object_array(o, name, &rev.pending); + ret = cmd_log_walk(&rev); + break; + default: + ret = error(_("Unknown type: %d"), o->type); + } + } + free(objects); + return ret; +} + +/* + * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" + */ +int cmd_log_reflog(int argc, const char **argv, const char *prefix) +{ + struct rev_info rev; + struct setup_revision_opt opt; + + init_grep_defaults(); + git_config(git_log_config, NULL); + + init_revisions(&rev, prefix); + init_reflog_walk(&rev.reflog_info); + rev.verbose_header = 1; + memset(&opt, 0, sizeof(opt)); + opt.def = "HEAD"; + cmd_log_init_defaults(&rev); + rev.abbrev_commit = 1; + rev.commit_format = CMIT_FMT_ONELINE; + rev.use_terminator = 1; + rev.always_show_header = 1; + cmd_log_init_finish(argc, argv, prefix, &rev, &opt); + + return cmd_log_walk(&rev); +} + +int cmd_log(int argc, const char **argv, const char *prefix) +{ + struct rev_info rev; + struct setup_revision_opt opt; + + init_grep_defaults(); + git_config(git_log_config, NULL); + + init_revisions(&rev, prefix); + rev.always_show_header = 1; + memset(&opt, 0, sizeof(opt)); + opt.def = "HEAD"; + opt.revarg_opt = REVARG_COMMITTISH; + cmd_log_init(argc, argv, prefix, &rev, &opt); + return cmd_log_walk(&rev); +} + +/* format-patch */ + +static const char *fmt_patch_suffix = ".patch"; +static int numbered = 0; +static int auto_number = 1; + +static char *default_attach = NULL; + +static struct string_list extra_hdr; +static struct string_list extra_to; +static struct string_list extra_cc; + +static void add_header(const char *value) +{ + struct string_list_item *item; + int len = strlen(value); + while (len && value[len - 1] == '\n') + len--; + + if (!strncasecmp(value, "to: ", 4)) { + item = string_list_append(&extra_to, value + 4); + len -= 4; + } else if (!strncasecmp(value, "cc: ", 4)) { + item = string_list_append(&extra_cc, value + 4); + len -= 4; + } else { + item = string_list_append(&extra_hdr, value); + } + + item->string[len] = '\0'; +} + +#define THREAD_SHALLOW 1 +#define THREAD_DEEP 2 +static int thread; +static int do_signoff; +static const char *signature = git_version_string; + +static int git_format_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "format.headers")) { + if (!value) + die(_("format.headers without value")); + add_header(value); + return 0; + } + if (!strcmp(var, "format.suffix")) + return git_config_string(&fmt_patch_suffix, var, value); + if (!strcmp(var, "format.to")) { + if (!value) + return config_error_nonbool(var); + string_list_append(&extra_to, value); + return 0; + } + if (!strcmp(var, "format.cc")) { + if (!value) + return config_error_nonbool(var); + string_list_append(&extra_cc, value); + return 0; + } + if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") || + !strcmp(var, "color.ui")) { + return 0; + } + if (!strcmp(var, "format.numbered")) { + if (value && !strcasecmp(value, "auto")) { + auto_number = 1; + return 0; + } + numbered = git_config_bool(var, value); + auto_number = auto_number && numbered; + return 0; + } + if (!strcmp(var, "format.attach")) { + if (value && *value) + default_attach = xstrdup(value); + else + default_attach = xstrdup(git_version_string); + return 0; + } + if (!strcmp(var, "format.thread")) { + if (value && !strcasecmp(value, "deep")) { + thread = THREAD_DEEP; + return 0; + } + if (value && !strcasecmp(value, "shallow")) { + thread = THREAD_SHALLOW; + return 0; + } + thread = git_config_bool(var, value) && THREAD_SHALLOW; + return 0; + } + if (!strcmp(var, "format.signoff")) { + do_signoff = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "format.signature")) + return git_config_string(&signature, var, value); + + return git_log_config(var, value, cb); +} + +static FILE *realstdout = NULL; +static const char *output_directory = NULL; +static int outdir_offset; + +static int reopen_stdout(struct commit *commit, const char *subject, + struct rev_info *rev, int quiet) +{ + struct strbuf filename = STRBUF_INIT; + int suffix_len = strlen(rev->patch_suffix) + 1; + + if (output_directory) { + strbuf_addstr(&filename, output_directory); + if (filename.len >= + PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) + return error(_("name of output directory is too long")); + if (filename.buf[filename.len - 1] != '/') + strbuf_addch(&filename, '/'); + } + + if (rev->numbered_files) + strbuf_addf(&filename, "%d", rev->nr); + else if (commit) + fmt_output_commit(&filename, commit, rev); + else + fmt_output_subject(&filename, subject, rev); + + if (!quiet) + fprintf(realstdout, "%s\n", filename.buf + outdir_offset); + + if (freopen(filename.buf, "w", stdout) == NULL) + return error(_("Cannot open patch file %s"), filename.buf); + + strbuf_release(&filename); + return 0; +} + +static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids) +{ + struct rev_info check_rev; + struct commit *commit; + struct object *o1, *o2; + unsigned flags1, flags2; + + if (rev->pending.nr != 2) + die(_("Need exactly one range.")); + + o1 = rev->pending.objects[0].item; + flags1 = o1->flags; + o2 = rev->pending.objects[1].item; + flags2 = o2->flags; + + if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) + die(_("Not a range.")); + + init_patch_ids(ids); + + /* given a range a..b get all patch ids for b..a */ + init_revisions(&check_rev, rev->prefix); + check_rev.max_parents = 1; + o1->flags ^= UNINTERESTING; + o2->flags ^= UNINTERESTING; + add_pending_object(&check_rev, o1, "o1"); + add_pending_object(&check_rev, o2, "o2"); + if (prepare_revision_walk(&check_rev)) + die(_("revision walk setup failed")); + + while ((commit = get_revision(&check_rev)) != NULL) { + add_commit_patch_id(commit, ids); + } + + /* reset for next revision walk */ + clear_commit_marks((struct commit *)o1, + SEEN | UNINTERESTING | SHOWN | ADDED); + clear_commit_marks((struct commit *)o2, + SEEN | UNINTERESTING | SHOWN | ADDED); + o1->flags = flags1; + o2->flags = flags2; +} + +static void gen_message_id(struct rev_info *info, char *base) +{ + struct strbuf buf = STRBUF_INIT; + strbuf_addf(&buf, "%s.%lu.git.%s", base, + (unsigned long) time(NULL), + git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT)); + info->message_id = strbuf_detach(&buf, NULL); +} + +static void print_signature(void) +{ + if (signature && *signature) + printf("-- \n%s\n\n", signature); +} + +static void add_branch_description(struct strbuf *buf, const char *branch_name) +{ + struct strbuf desc = STRBUF_INIT; + if (!branch_name || !*branch_name) + return; + read_branch_desc(&desc, branch_name); + if (desc.len) { + strbuf_addch(buf, '\n'); + strbuf_add(buf, desc.buf, desc.len); + strbuf_addch(buf, '\n'); + } +} + +static void make_cover_letter(struct rev_info *rev, int use_stdout, + struct commit *origin, + int nr, struct commit **list, struct commit *head, + const char *branch_name, + int quiet) +{ + const char *committer; + const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; + const char *msg; + struct shortlog log; + struct strbuf sb = STRBUF_INIT; + int i; + const char *encoding = "UTF-8"; + struct diff_options opts; + int need_8bit_cte = 0; + struct pretty_print_context pp = {0}; + + if (rev->commit_format != CMIT_FMT_EMAIL) + die(_("Cover letter needs email format")); + + committer = git_committer_info(0); + + if (!use_stdout && + reopen_stdout(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet)) + return; + + log_write_email_headers(rev, head, &pp.subject, &pp.after_subject, + &need_8bit_cte); + + for (i = 0; !need_8bit_cte && i < nr; i++) + if (has_non_ascii(list[i]->buffer)) + need_8bit_cte = 1; + + msg = body; + pp.fmt = CMIT_FMT_EMAIL; + pp.date_mode = DATE_RFC2822; + pp_user_info(&pp, NULL, &sb, committer, encoding); + pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte); + pp_remainder(&pp, &msg, &sb, 0); + add_branch_description(&sb, branch_name); + printf("%s\n", sb.buf); + + strbuf_release(&sb); + + shortlog_init(&log); + log.wrap_lines = 1; + log.wrap = 72; + log.in1 = 2; + log.in2 = 4; + for (i = 0; i < nr; i++) + shortlog_add_commit(&log, list[i]); + + shortlog_output(&log); + + /* + * We can only do diffstat with a unique reference point + */ + if (!origin) + return; + + memcpy(&opts, &rev->diffopt, sizeof(opts)); + opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; + + diff_setup_done(&opts); + + diff_tree_sha1(origin->tree->object.sha1, + head->tree->object.sha1, + "", &opts); + diffcore_std(&opts); + diff_flush(&opts); + + printf("\n"); + print_signature(); +} + +static const char *clean_message_id(const char *msg_id) +{ + char ch; + const char *a, *z, *m; + + m = msg_id; + while ((ch = *m) && (isspace(ch) || (ch == '<'))) + m++; + a = m; + z = NULL; + while ((ch = *m)) { + if (!isspace(ch) && (ch != '>')) + z = m; + m++; + } + if (!z) + die(_("insane in-reply-to: %s"), msg_id); + if (++z == m) + return a; + return xmemdupz(a, z - a); +} + +static const char *set_outdir(const char *prefix, const char *output_directory) +{ + if (output_directory && is_absolute_path(output_directory)) + return output_directory; + + if (!prefix || !*prefix) { + if (output_directory) + return output_directory; + /* The user did not explicitly ask for "./" */ + outdir_offset = 2; + return "./"; + } + + outdir_offset = strlen(prefix); + if (!output_directory) + return prefix; + + return xstrdup(prefix_filename(prefix, outdir_offset, + output_directory)); +} + +static const char * const builtin_format_patch_usage[] = { + N_("git format-patch [options] [<since> | <revision range>]"), + NULL +}; + +static int keep_subject = 0; + +static int keep_callback(const struct option *opt, const char *arg, int unset) +{ + ((struct rev_info *)opt->value)->total = -1; + keep_subject = 1; + return 0; +} + +static int subject_prefix = 0; + +static int subject_prefix_callback(const struct option *opt, const char *arg, + int unset) +{ + subject_prefix = 1; + ((struct rev_info *)opt->value)->subject_prefix = arg; + return 0; +} + +static int numbered_cmdline_opt = 0; + +static int numbered_callback(const struct option *opt, const char *arg, + int unset) +{ + *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1; + if (unset) + auto_number = 0; + return 0; +} + +static int no_numbered_callback(const struct option *opt, const char *arg, + int unset) +{ + return numbered_callback(opt, arg, 1); +} + +static int output_directory_callback(const struct option *opt, const char *arg, + int unset) +{ + const char **dir = (const char **)opt->value; + if (*dir) + die(_("Two output directories?")); + *dir = arg; + return 0; +} + +static int thread_callback(const struct option *opt, const char *arg, int unset) +{ + int *thread = (int *)opt->value; + if (unset) + *thread = 0; + else if (!arg || !strcmp(arg, "shallow")) + *thread = THREAD_SHALLOW; + else if (!strcmp(arg, "deep")) + *thread = THREAD_DEEP; + else + return 1; + return 0; +} + +static int attach_callback(const struct option *opt, const char *arg, int unset) +{ + struct rev_info *rev = (struct rev_info *)opt->value; + if (unset) + rev->mime_boundary = NULL; + else if (arg) + rev->mime_boundary = arg; + else + rev->mime_boundary = git_version_string; + rev->no_inline = unset ? 0 : 1; + return 0; +} + +static int inline_callback(const struct option *opt, const char *arg, int unset) +{ + struct rev_info *rev = (struct rev_info *)opt->value; + if (unset) + rev->mime_boundary = NULL; + else if (arg) + rev->mime_boundary = arg; + else + rev->mime_boundary = git_version_string; + rev->no_inline = 0; + return 0; +} + +static int header_callback(const struct option *opt, const char *arg, int unset) +{ + if (unset) { + string_list_clear(&extra_hdr, 0); + string_list_clear(&extra_to, 0); + string_list_clear(&extra_cc, 0); + } else { + add_header(arg); + } + return 0; +} + +static int to_callback(const struct option *opt, const char *arg, int unset) +{ + if (unset) + string_list_clear(&extra_to, 0); + else + string_list_append(&extra_to, arg); + return 0; +} + +static int cc_callback(const struct option *opt, const char *arg, int unset) +{ + if (unset) + string_list_clear(&extra_cc, 0); + else + string_list_append(&extra_cc, arg); + return 0; +} + +static char *find_branch_name(struct rev_info *rev) +{ + int i, positive = -1; + unsigned char branch_sha1[20]; + const unsigned char *tip_sha1; + const char *ref; + char *full_ref, *branch = NULL; + + for (i = 0; i < rev->cmdline.nr; i++) { + if (rev->cmdline.rev[i].flags & UNINTERESTING) + continue; + if (positive < 0) + positive = i; + else + return NULL; + } + if (0 <= positive) { + ref = rev->cmdline.rev[positive].name; + tip_sha1 = rev->cmdline.rev[positive].item->sha1; + } else if (!rev->cmdline.nr && rev->pending.nr == 1 && + !strcmp(rev->pending.objects[0].name, "HEAD")) { + /* + * No actual ref from command line, but "HEAD" from + * rev->def was added in setup_revisions() + * e.g. format-patch --cover-letter -12 + */ + ref = "HEAD"; + tip_sha1 = rev->pending.objects[0].item->sha1; + } else { + return NULL; + } + if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) && + !prefixcmp(full_ref, "refs/heads/") && + !hashcmp(tip_sha1, branch_sha1)) + branch = xstrdup(full_ref + strlen("refs/heads/")); + free(full_ref); + return branch; +} + +int cmd_format_patch(int argc, const char **argv, const char *prefix) +{ + struct commit *commit; + struct commit **list = NULL; + struct rev_info rev; + struct setup_revision_opt s_r_opt; + int nr = 0, total, i; + int use_stdout = 0; + int start_number = -1; + int just_numbers = 0; + int ignore_if_in_upstream = 0; + int cover_letter = 0; + int boundary_count = 0; + int no_binary_diff = 0; + struct commit *origin = NULL, *head = NULL; + const char *in_reply_to = NULL; + struct patch_ids ids; + char *add_signoff = NULL; + struct strbuf buf = STRBUF_INIT; + int use_patch_format = 0; + int quiet = 0; + int reroll_count = -1; + char *branch_name = NULL; + const struct option builtin_format_patch_options[] = { + { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL, + N_("use [PATCH n/m] even with a single patch"), + PARSE_OPT_NOARG, numbered_callback }, + { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL, + N_("use [PATCH] even with multiple patches"), + PARSE_OPT_NOARG, no_numbered_callback }, + OPT_BOOLEAN('s', "signoff", &do_signoff, N_("add Signed-off-by:")), + OPT_BOOLEAN(0, "stdout", &use_stdout, + N_("print patches to standard out")), + OPT_BOOLEAN(0, "cover-letter", &cover_letter, + N_("generate a cover letter")), + OPT_BOOLEAN(0, "numbered-files", &just_numbers, + N_("use simple number sequence for output file names")), + OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"), + N_("use <sfx> instead of '.patch'")), + OPT_INTEGER(0, "start-number", &start_number, + N_("start numbering patches at <n> instead of 1")), + OPT_INTEGER('v', "reroll-count", &reroll_count, + N_("mark the series as Nth re-roll")), + { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"), + N_("Use [<prefix>] instead of [PATCH]"), + PARSE_OPT_NONEG, subject_prefix_callback }, + { OPTION_CALLBACK, 'o', "output-directory", &output_directory, + N_("dir"), N_("store resulting files in <dir>"), + PARSE_OPT_NONEG, output_directory_callback }, + { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL, + N_("don't strip/add [PATCH]"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback }, + OPT_BOOLEAN(0, "no-binary", &no_binary_diff, + N_("don't output binary diffs")), + OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream, + N_("don't include a patch matching a commit upstream")), + { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL, + N_("show patch format instead of default (patch + stat)"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG }, + OPT_GROUP(N_("Messaging")), + { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"), + N_("add email header"), 0, header_callback }, + { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"), + 0, to_callback }, + { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"), + 0, cc_callback }, + OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"), + N_("make first mail a reply to <message-id>")), + { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"), + N_("attach the patch"), PARSE_OPT_OPTARG, + attach_callback }, + { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"), + N_("inline the patch"), + PARSE_OPT_OPTARG | PARSE_OPT_NONEG, + inline_callback }, + { OPTION_CALLBACK, 0, "thread", &thread, N_("style"), + N_("enable message threading, styles: shallow, deep"), + PARSE_OPT_OPTARG, thread_callback }, + OPT_STRING(0, "signature", &signature, N_("signature"), + N_("add a signature")), + OPT_BOOLEAN(0, "quiet", &quiet, + N_("don't print the patch filenames")), + OPT_END() + }; + + extra_hdr.strdup_strings = 1; + extra_to.strdup_strings = 1; + extra_cc.strdup_strings = 1; + init_grep_defaults(); + git_config(git_format_config, NULL); + init_revisions(&rev, prefix); + rev.commit_format = CMIT_FMT_EMAIL; + rev.verbose_header = 1; + rev.diff = 1; + rev.max_parents = 1; + DIFF_OPT_SET(&rev.diffopt, RECURSIVE); + rev.subject_prefix = fmt_patch_subject_prefix; + memset(&s_r_opt, 0, sizeof(s_r_opt)); + s_r_opt.def = "HEAD"; + s_r_opt.revarg_opt = REVARG_COMMITTISH; + + if (default_attach) { + rev.mime_boundary = default_attach; + rev.no_inline = 1; + } + + /* + * Parse the arguments before setup_revisions(), or something + * like "git format-patch -o a123 HEAD^.." may fail; a123 is + * possibly a valid SHA1. + */ + argc = parse_options(argc, argv, prefix, builtin_format_patch_options, + builtin_format_patch_usage, + PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | + PARSE_OPT_KEEP_DASHDASH); + + if (0 < reroll_count) { + struct strbuf sprefix = STRBUF_INIT; + strbuf_addf(&sprefix, "%s v%d", + rev.subject_prefix, reroll_count); + rev.reroll_count = reroll_count; + rev.subject_prefix = strbuf_detach(&sprefix, NULL); + } + + if (do_signoff) { + const char *committer; + const char *endpos; + committer = git_committer_info(IDENT_STRICT); + endpos = strchr(committer, '>'); + if (!endpos) + die(_("bogus committer info %s"), committer); + add_signoff = xmemdupz(committer, endpos - committer + 1); + } + + for (i = 0; i < extra_hdr.nr; i++) { + strbuf_addstr(&buf, extra_hdr.items[i].string); + strbuf_addch(&buf, '\n'); + } + + if (extra_to.nr) + strbuf_addstr(&buf, "To: "); + for (i = 0; i < extra_to.nr; i++) { + if (i) + strbuf_addstr(&buf, " "); + strbuf_addstr(&buf, extra_to.items[i].string); + if (i + 1 < extra_to.nr) + strbuf_addch(&buf, ','); + strbuf_addch(&buf, '\n'); + } + + if (extra_cc.nr) + strbuf_addstr(&buf, "Cc: "); + for (i = 0; i < extra_cc.nr; i++) { + if (i) + strbuf_addstr(&buf, " "); + strbuf_addstr(&buf, extra_cc.items[i].string); + if (i + 1 < extra_cc.nr) + strbuf_addch(&buf, ','); + strbuf_addch(&buf, '\n'); + } + + rev.extra_headers = strbuf_detach(&buf, NULL); + + if (start_number < 0) + start_number = 1; + + /* + * If numbered is set solely due to format.numbered in config, + * and it would conflict with --keep-subject (-k) from the + * command line, reset "numbered". + */ + if (numbered && keep_subject && !numbered_cmdline_opt) + numbered = 0; + + if (numbered && keep_subject) + die (_("-n and -k are mutually exclusive.")); + if (keep_subject && subject_prefix) + die (_("--subject-prefix and -k are mutually exclusive.")); + rev.preserve_subject = keep_subject; + + argc = setup_revisions(argc, argv, &rev, &s_r_opt); + if (argc > 1) + die (_("unrecognized argument: %s"), argv[1]); + + if (rev.diffopt.output_format & DIFF_FORMAT_NAME) + die(_("--name-only does not make sense")); + if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS) + die(_("--name-status does not make sense")); + if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF) + die(_("--check does not make sense")); + + if (!use_patch_format && + (!rev.diffopt.output_format || + rev.diffopt.output_format == DIFF_FORMAT_PATCH)) + rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY; + + /* Always generate a patch */ + rev.diffopt.output_format |= DIFF_FORMAT_PATCH; + + if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff) + DIFF_OPT_SET(&rev.diffopt, BINARY); + + if (rev.show_notes) + init_display_notes(&rev.notes_opt); + + if (!use_stdout) + output_directory = set_outdir(prefix, output_directory); + else + setup_pager(); + + if (output_directory) { + if (use_stdout) + die(_("standard output, or directory, which one?")); + if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) + die_errno(_("Could not create directory '%s'"), + output_directory); + } + + if (rev.pending.nr == 1) { + if (rev.max_count < 0 && !rev.show_root_diff) { + /* + * This is traditional behaviour of "git format-patch + * origin" that prepares what the origin side still + * does not have. + */ + unsigned char sha1[20]; + const char *ref; + + rev.pending.objects[0].item->flags |= UNINTERESTING; + add_head_to_pending(&rev); + ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL); + if (ref && !prefixcmp(ref, "refs/heads/")) + branch_name = xstrdup(ref + strlen("refs/heads/")); + else + branch_name = xstrdup(""); /* no branch */ + } + /* + * Otherwise, it is "format-patch -22 HEAD", and/or + * "format-patch --root HEAD". The user wants + * get_revision() to do the usual traversal. + */ + } + + /* + * We cannot move this anywhere earlier because we do want to + * know if --root was given explicitly from the command line. + */ + rev.show_root_diff = 1; + + if (cover_letter) { + /* + * NEEDSWORK:randomly pick one positive commit to show + * diffstat; this is often the tip and the command + * happens to do the right thing in most cases, but a + * complex command like "--cover-letter a b c ^bottom" + * picks "c" and shows diffstat between bottom..c + * which may not match what the series represents at + * all and totally broken. + */ + int i; + for (i = 0; i < rev.pending.nr; i++) { + struct object *o = rev.pending.objects[i].item; + if (!(o->flags & UNINTERESTING)) + head = (struct commit *)o; + } + /* There is nothing to show; it is not an error, though. */ + if (!head) + return 0; + if (!branch_name) + branch_name = find_branch_name(&rev); + } + + if (ignore_if_in_upstream) { + /* Don't say anything if head and upstream are the same. */ + if (rev.pending.nr == 2) { + struct object_array_entry *o = rev.pending.objects; + if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) + return 0; + } + get_patch_ids(&rev, &ids); + } + + if (!use_stdout) + realstdout = xfdopen(xdup(1), "w"); + + if (prepare_revision_walk(&rev)) + die(_("revision walk setup failed")); + rev.boundary = 1; + while ((commit = get_revision(&rev)) != NULL) { + if (commit->object.flags & BOUNDARY) { + boundary_count++; + origin = (boundary_count == 1) ? commit : NULL; + continue; + } + + if (ignore_if_in_upstream && + has_commit_patch_id(commit, &ids)) + continue; + + nr++; + list = xrealloc(list, nr * sizeof(list[0])); + list[nr - 1] = commit; + } + total = nr; + if (!keep_subject && auto_number && total > 1) + numbered = 1; + if (numbered) + rev.total = total + start_number - 1; + if (in_reply_to || thread || cover_letter) + rev.ref_message_ids = xcalloc(1, sizeof(struct string_list)); + if (in_reply_to) { + const char *msgid = clean_message_id(in_reply_to); + string_list_append(rev.ref_message_ids, msgid); + } + rev.numbered_files = just_numbers; + rev.patch_suffix = fmt_patch_suffix; + if (cover_letter) { + if (thread) + gen_message_id(&rev, "cover"); + make_cover_letter(&rev, use_stdout, + origin, nr, list, head, branch_name, quiet); + total++; + start_number--; + } + rev.add_signoff = add_signoff; + while (0 <= --nr) { + int shown; + commit = list[nr]; + rev.nr = total - nr + (start_number - 1); + /* Make the second and subsequent mails replies to the first */ + if (thread) { + /* Have we already had a message ID? */ + if (rev.message_id) { + /* + * For deep threading: make every mail + * a reply to the previous one, no + * matter what other options are set. + * + * For shallow threading: + * + * Without --cover-letter and + * --in-reply-to, make every mail a + * reply to the one before. + * + * With --in-reply-to but no + * --cover-letter, make every mail a + * reply to the <reply-to>. + * + * With --cover-letter, make every + * mail but the cover letter a reply + * to the cover letter. The cover + * letter is a reply to the + * --in-reply-to, if specified. + */ + if (thread == THREAD_SHALLOW + && rev.ref_message_ids->nr > 0 + && (!cover_letter || rev.nr > 1)) + free(rev.message_id); + else + string_list_append(rev.ref_message_ids, + rev.message_id); + } + gen_message_id(&rev, sha1_to_hex(commit->object.sha1)); + } + + if (!use_stdout && + reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet)) + die(_("Failed to create output files")); + shown = log_tree_commit(&rev, commit); + free(commit->buffer); + commit->buffer = NULL; + + /* We put one extra blank line between formatted + * patches and this flag is used by log-tree code + * to see if it needs to emit a LF before showing + * the log; when using one file per patch, we do + * not want the extra blank line. + */ + if (!use_stdout) + rev.shown_one = 0; + if (shown) { + if (rev.mime_boundary) + printf("\n--%s%s--\n\n\n", + mime_boundary_leader, + rev.mime_boundary); + else + print_signature(); + } + if (!use_stdout) + fclose(stdout); + } + free(list); + free(branch_name); + string_list_clear(&extra_to, 0); + string_list_clear(&extra_cc, 0); + string_list_clear(&extra_hdr, 0); + if (ignore_if_in_upstream) + free_patch_ids(&ids); + return 0; +} + +static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) +{ + unsigned char sha1[20]; + if (get_sha1(arg, sha1) == 0) { + struct commit *commit = lookup_commit_reference(sha1); + if (commit) { + commit->object.flags |= flags; + add_pending_object(revs, &commit->object, arg); + return 0; + } + } + return -1; +} + +static const char * const cherry_usage[] = { + N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"), + NULL +}; + +static void print_commit(char sign, struct commit *commit, int verbose, + int abbrev) +{ + if (!verbose) { + printf("%c %s\n", sign, + find_unique_abbrev(commit->object.sha1, abbrev)); + } else { + struct strbuf buf = STRBUF_INIT; + pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf); + printf("%c %s %s\n", sign, + find_unique_abbrev(commit->object.sha1, abbrev), + buf.buf); + strbuf_release(&buf); + } +} + +int cmd_cherry(int argc, const char **argv, const char *prefix) +{ + struct rev_info revs; + struct patch_ids ids; + struct commit *commit; + struct commit_list *list = NULL; + struct branch *current_branch; + const char *upstream; + const char *head = "HEAD"; + const char *limit = NULL; + int verbose = 0, abbrev = 0; + + struct option options[] = { + OPT__ABBREV(&abbrev), + OPT__VERBOSE(&verbose, N_("be verbose")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, cherry_usage, 0); + + switch (argc) { + case 3: + limit = argv[2]; + /* FALLTHROUGH */ + case 2: + head = argv[1]; + /* FALLTHROUGH */ + case 1: + upstream = argv[0]; + break; + default: + current_branch = branch_get(NULL); + if (!current_branch || !current_branch->merge + || !current_branch->merge[0] + || !current_branch->merge[0]->dst) { + fprintf(stderr, _("Could not find a tracked" + " remote branch, please" + " specify <upstream> manually.\n")); + usage_with_options(cherry_usage, options); + } + + upstream = current_branch->merge[0]->dst; + } + + init_revisions(&revs, prefix); + revs.max_parents = 1; + + if (add_pending_commit(head, &revs, 0)) + die(_("Unknown commit %s"), head); + if (add_pending_commit(upstream, &revs, UNINTERESTING)) + die(_("Unknown commit %s"), upstream); + + /* Don't say anything if head and upstream are the same. */ + if (revs.pending.nr == 2) { + struct object_array_entry *o = revs.pending.objects; + if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) + return 0; + } + + get_patch_ids(&revs, &ids); + + if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) + die(_("Unknown commit %s"), limit); + + /* reverse the list of commits */ + if (prepare_revision_walk(&revs)) + die(_("revision walk setup failed")); + while ((commit = get_revision(&revs)) != NULL) { + commit_list_insert(commit, &list); + } + + while (list) { + char sign = '+'; + + commit = list->item; + if (has_commit_patch_id(commit, &ids)) + sign = '-'; + print_commit(sign, commit, verbose, abbrev); + list = list->next; + } + + free_patch_ids(&ids); + return 0; +} diff --git a/builtin/ls-files.c b/builtin/ls-files.c new file mode 100644 index 0000000000..373c573449 --- /dev/null +++ b/builtin/ls-files.c @@ -0,0 +1,603 @@ +/* + * This merges the file listing in the directory cache index + * with the actual working directory list, and shows different + * combinations of the two. + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "quote.h" +#include "dir.h" +#include "builtin.h" +#include "tree.h" +#include "parse-options.h" +#include "resolve-undo.h" +#include "string-list.h" + +static int abbrev; +static int show_deleted; +static int show_cached; +static int show_others; +static int show_stage; +static int show_unmerged; +static int show_resolve_undo; +static int show_modified; +static int show_killed; +static int show_valid_bit; +static int line_terminator = '\n'; +static int debug_mode; + +static const char *prefix; +static int max_prefix_len; +static int prefix_len; +static const char **pathspec; +static int error_unmatch; +static char *ps_matched; +static const char *with_tree; +static int exc_given; + +static const char *tag_cached = ""; +static const char *tag_unmerged = ""; +static const char *tag_removed = ""; +static const char *tag_other = ""; +static const char *tag_killed = ""; +static const char *tag_modified = ""; +static const char *tag_skip_worktree = ""; +static const char *tag_resolve_undo = ""; + +static void write_name(const char* name, size_t len) +{ + write_name_quoted_relative(name, len, prefix, prefix_len, stdout, + line_terminator); +} + +static void show_dir_entry(const char *tag, struct dir_entry *ent) +{ + int len = max_prefix_len; + + if (len >= ent->len) + die("git ls-files: internal error - directory entry not superset of prefix"); + + if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched)) + return; + + fputs(tag, stdout); + write_name(ent->name, ent->len); +} + +static void show_other_files(struct dir_struct *dir) +{ + int i; + + for (i = 0; i < dir->nr; i++) { + struct dir_entry *ent = dir->entries[i]; + if (!cache_name_is_other(ent->name, ent->len)) + continue; + show_dir_entry(tag_other, ent); + } +} + +static void show_killed_files(struct dir_struct *dir) +{ + int i; + for (i = 0; i < dir->nr; i++) { + struct dir_entry *ent = dir->entries[i]; + char *cp, *sp; + int pos, len, killed = 0; + + for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) { + sp = strchr(cp, '/'); + if (!sp) { + /* If ent->name is prefix of an entry in the + * cache, it will be killed. + */ + pos = cache_name_pos(ent->name, ent->len); + if (0 <= pos) + die("bug in show-killed-files"); + pos = -pos - 1; + while (pos < active_nr && + ce_stage(active_cache[pos])) + pos++; /* skip unmerged */ + if (active_nr <= pos) + break; + /* pos points at a name immediately after + * ent->name in the cache. Does it expect + * ent->name to be a directory? + */ + len = ce_namelen(active_cache[pos]); + if ((ent->len < len) && + !strncmp(active_cache[pos]->name, + ent->name, ent->len) && + active_cache[pos]->name[ent->len] == '/') + killed = 1; + break; + } + if (0 <= cache_name_pos(ent->name, sp - ent->name)) { + /* If any of the leading directories in + * ent->name is registered in the cache, + * ent->name will be killed. + */ + killed = 1; + break; + } + } + if (killed) + show_dir_entry(tag_killed, dir->entries[i]); + } +} + +static void show_ce_entry(const char *tag, struct cache_entry *ce) +{ + int len = max_prefix_len; + + if (len >= ce_namelen(ce)) + die("git ls-files: internal error - cache entry not superset of prefix"); + + if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched)) + return; + + if (tag && *tag && show_valid_bit && + (ce->ce_flags & CE_VALID)) { + static char alttag[4]; + memcpy(alttag, tag, 3); + if (isalpha(tag[0])) + alttag[0] = tolower(tag[0]); + else if (tag[0] == '?') + alttag[0] = '!'; + else { + alttag[0] = 'v'; + alttag[1] = tag[0]; + alttag[2] = ' '; + alttag[3] = 0; + } + tag = alttag; + } + + if (!show_stage) { + fputs(tag, stdout); + } else { + printf("%s%06o %s %d\t", + tag, + ce->ce_mode, + find_unique_abbrev(ce->sha1,abbrev), + ce_stage(ce)); + } + write_name(ce->name, ce_namelen(ce)); + if (debug_mode) { + printf(" ctime: %d:%d\n", ce->ce_ctime.sec, ce->ce_ctime.nsec); + printf(" mtime: %d:%d\n", ce->ce_mtime.sec, ce->ce_mtime.nsec); + printf(" dev: %d\tino: %d\n", ce->ce_dev, ce->ce_ino); + printf(" uid: %d\tgid: %d\n", ce->ce_uid, ce->ce_gid); + printf(" size: %d\tflags: %x\n", ce->ce_size, ce->ce_flags); + } +} + +static void show_ru_info(void) +{ + struct string_list_item *item; + + if (!the_index.resolve_undo) + return; + + for_each_string_list_item(item, the_index.resolve_undo) { + const char *path = item->string; + struct resolve_undo_info *ui = item->util; + int i, len; + + len = strlen(path); + if (len < max_prefix_len) + continue; /* outside of the prefix */ + if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched)) + continue; /* uninterested */ + for (i = 0; i < 3; i++) { + if (!ui->mode[i]) + continue; + printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i], + find_unique_abbrev(ui->sha1[i], abbrev), + i + 1); + write_name(path, len); + } + } +} + +static int ce_excluded(struct path_exclude_check *check, struct cache_entry *ce) +{ + int dtype = ce_to_dtype(ce); + return is_path_excluded(check, ce->name, ce_namelen(ce), &dtype); +} + +static void show_files(struct dir_struct *dir) +{ + int i; + struct path_exclude_check check; + + if ((dir->flags & DIR_SHOW_IGNORED)) + path_exclude_check_init(&check, dir); + + /* For cached/deleted files we don't need to even do the readdir */ + if (show_others || show_killed) { + fill_directory(dir, pathspec); + if (show_others) + show_other_files(dir); + if (show_killed) + show_killed_files(dir); + } + if (show_cached | show_stage) { + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + if ((dir->flags & DIR_SHOW_IGNORED) && + !ce_excluded(&check, ce)) + continue; + if (show_unmerged && !ce_stage(ce)) + continue; + if (ce->ce_flags & CE_UPDATE) + continue; + show_ce_entry(ce_stage(ce) ? tag_unmerged : + (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce); + } + } + if (show_deleted | show_modified) { + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + struct stat st; + int err; + if ((dir->flags & DIR_SHOW_IGNORED) && + !ce_excluded(&check, ce)) + continue; + if (ce->ce_flags & CE_UPDATE) + continue; + if (ce_skip_worktree(ce)) + continue; + err = lstat(ce->name, &st); + if (show_deleted && err) + show_ce_entry(tag_removed, ce); + if (show_modified && ce_modified(ce, &st, 0)) + show_ce_entry(tag_modified, ce); + } + } + + if ((dir->flags & DIR_SHOW_IGNORED)) + path_exclude_check_clear(&check); +} + +/* + * Prune the index to only contain stuff starting with "prefix" + */ +static void prune_cache(const char *prefix) +{ + int pos = cache_name_pos(prefix, max_prefix_len); + unsigned int first, last; + + if (pos < 0) + pos = -pos-1; + memmove(active_cache, active_cache + pos, + (active_nr - pos) * sizeof(struct cache_entry *)); + active_nr -= pos; + first = 0; + last = active_nr; + while (last > first) { + int next = (last + first) >> 1; + struct cache_entry *ce = active_cache[next]; + if (!strncmp(ce->name, prefix, max_prefix_len)) { + first = next+1; + continue; + } + last = next; + } + active_nr = last; +} + +static void strip_trailing_slash_from_submodules(void) +{ + const char **p; + + for (p = pathspec; *p != NULL; p++) { + int len = strlen(*p), pos; + + if (len < 1 || (*p)[len - 1] != '/') + continue; + pos = cache_name_pos(*p, len - 1); + if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode)) + *p = xstrndup(*p, len - 1); + } +} + +/* + * Read the tree specified with --with-tree option + * (typically, HEAD) into stage #1 and then + * squash them down to stage #0. This is used for + * --error-unmatch to list and check the path patterns + * that were given from the command line. We are not + * going to write this index out. + */ +void overlay_tree_on_cache(const char *tree_name, const char *prefix) +{ + struct tree *tree; + unsigned char sha1[20]; + struct pathspec pathspec; + struct cache_entry *last_stage0 = NULL; + int i; + + if (get_sha1(tree_name, sha1)) + die("tree-ish %s not found.", tree_name); + tree = parse_tree_indirect(sha1); + if (!tree) + die("bad tree-ish %s", tree_name); + + /* Hoist the unmerged entries up to stage #3 to make room */ + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + if (!ce_stage(ce)) + continue; + ce->ce_flags |= CE_STAGEMASK; + } + + if (prefix) { + static const char *(matchbuf[2]); + matchbuf[0] = prefix; + matchbuf[1] = NULL; + init_pathspec(&pathspec, matchbuf); + pathspec.items[0].nowildcard_len = pathspec.items[0].len; + } else + init_pathspec(&pathspec, NULL); + if (read_tree(tree, 1, &pathspec)) + die("unable to read tree entries %s", tree_name); + + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + switch (ce_stage(ce)) { + case 0: + last_stage0 = ce; + /* fallthru */ + default: + continue; + case 1: + /* + * If there is stage #0 entry for this, we do not + * need to show it. We use CE_UPDATE bit to mark + * such an entry. + */ + if (last_stage0 && + !strcmp(last_stage0->name, ce->name)) + ce->ce_flags |= CE_UPDATE; + } + } +} + +int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix) +{ + /* + * Make sure all pathspec matched; otherwise it is an error. + */ + struct strbuf sb = STRBUF_INIT; + const char *name; + int num, errors = 0; + for (num = 0; pathspec[num]; num++) { + int other, found_dup; + + if (ps_matched[num]) + continue; + /* + * The caller might have fed identical pathspec + * twice. Do not barf on such a mistake. + */ + for (found_dup = other = 0; + !found_dup && pathspec[other]; + other++) { + if (other == num || !ps_matched[other]) + continue; + if (!strcmp(pathspec[other], pathspec[num])) + /* + * Ok, we have a match already. + */ + found_dup = 1; + } + if (found_dup) + continue; + + name = quote_path_relative(pathspec[num], -1, &sb, prefix); + error("pathspec '%s' did not match any file(s) known to git.", + name); + errors++; + } + strbuf_release(&sb); + return errors; +} + +static const char * const ls_files_usage[] = { + N_("git ls-files [options] [<file>...]"), + NULL +}; + +static int option_parse_z(const struct option *opt, + const char *arg, int unset) +{ + line_terminator = unset ? '\n' : '\0'; + + return 0; +} + +static int option_parse_exclude(const struct option *opt, + const char *arg, int unset) +{ + struct exclude_list *list = opt->value; + + exc_given = 1; + add_exclude(arg, "", 0, list); + + return 0; +} + +static int option_parse_exclude_from(const struct option *opt, + const char *arg, int unset) +{ + struct dir_struct *dir = opt->value; + + exc_given = 1; + add_excludes_from_file(dir, arg); + + return 0; +} + +static int option_parse_exclude_standard(const struct option *opt, + const char *arg, int unset) +{ + struct dir_struct *dir = opt->value; + + exc_given = 1; + setup_standard_excludes(dir); + + return 0; +} + +int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) +{ + int require_work_tree = 0, show_tag = 0; + const char *max_prefix; + struct dir_struct dir; + struct option builtin_ls_files_options[] = { + { OPTION_CALLBACK, 'z', NULL, NULL, NULL, + N_("paths are separated with NUL character"), + PARSE_OPT_NOARG, option_parse_z }, + OPT_BOOLEAN('t', NULL, &show_tag, + N_("identify the file status with tags")), + OPT_BOOLEAN('v', NULL, &show_valid_bit, + N_("use lowercase letters for 'assume unchanged' files")), + OPT_BOOLEAN('c', "cached", &show_cached, + N_("show cached files in the output (default)")), + OPT_BOOLEAN('d', "deleted", &show_deleted, + N_("show deleted files in the output")), + OPT_BOOLEAN('m', "modified", &show_modified, + N_("show modified files in the output")), + OPT_BOOLEAN('o', "others", &show_others, + N_("show other files in the output")), + OPT_BIT('i', "ignored", &dir.flags, + N_("show ignored files in the output"), + DIR_SHOW_IGNORED), + OPT_BOOLEAN('s', "stage", &show_stage, + N_("show staged contents' object name in the output")), + OPT_BOOLEAN('k', "killed", &show_killed, + N_("show files on the filesystem that need to be removed")), + OPT_BIT(0, "directory", &dir.flags, + N_("show 'other' directories' name only"), + DIR_SHOW_OTHER_DIRECTORIES), + OPT_NEGBIT(0, "empty-directory", &dir.flags, + N_("don't show empty directories"), + DIR_HIDE_EMPTY_DIRECTORIES), + OPT_BOOLEAN('u', "unmerged", &show_unmerged, + N_("show unmerged files in the output")), + OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo, + N_("show resolve-undo information")), + { OPTION_CALLBACK, 'x', "exclude", &dir.exclude_list[EXC_CMDL], N_("pattern"), + N_("skip files matching pattern"), + 0, option_parse_exclude }, + { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"), + N_("exclude patterns are read from <file>"), + 0, option_parse_exclude_from }, + OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"), + N_("read additional per-directory exclude patterns in <file>")), + { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL, + N_("add the standard git exclusions"), + PARSE_OPT_NOARG, option_parse_exclude_standard }, + { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL, + N_("make the output relative to the project top directory"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL }, + OPT_BOOLEAN(0, "error-unmatch", &error_unmatch, + N_("if any <file> is not in the index, treat this as an error")), + OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"), + N_("pretend that paths removed since <tree-ish> are still present")), + OPT__ABBREV(&abbrev), + OPT_BOOLEAN(0, "debug", &debug_mode, N_("show debugging data")), + OPT_END() + }; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(ls_files_usage, builtin_ls_files_options); + + memset(&dir, 0, sizeof(dir)); + prefix = cmd_prefix; + if (prefix) + prefix_len = strlen(prefix); + git_config(git_default_config, NULL); + + if (read_cache() < 0) + die("index file corrupt"); + + argc = parse_options(argc, argv, prefix, builtin_ls_files_options, + ls_files_usage, 0); + if (show_tag || show_valid_bit) { + tag_cached = "H "; + tag_unmerged = "M "; + tag_removed = "R "; + tag_modified = "C "; + tag_other = "? "; + tag_killed = "K "; + tag_skip_worktree = "S "; + tag_resolve_undo = "U "; + } + if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed) + require_work_tree = 1; + if (show_unmerged) + /* + * There's no point in showing unmerged unless + * you also show the stage information. + */ + show_stage = 1; + if (dir.exclude_per_dir) + exc_given = 1; + + if (require_work_tree && !is_inside_work_tree()) + setup_work_tree(); + + pathspec = get_pathspec(prefix, argv); + + /* be nice with submodule paths ending in a slash */ + if (pathspec) + strip_trailing_slash_from_submodules(); + + /* Find common prefix for all pathspec's */ + max_prefix = common_prefix(pathspec); + max_prefix_len = max_prefix ? strlen(max_prefix) : 0; + + /* Treat unmatching pathspec elements as errors */ + if (pathspec && error_unmatch) { + int num; + for (num = 0; pathspec[num]; num++) + ; + ps_matched = xcalloc(1, num); + } + + if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given) + die("ls-files --ignored needs some exclude pattern"); + + /* With no flags, we default to showing the cached files */ + if (!(show_stage | show_deleted | show_others | show_unmerged | + show_killed | show_modified | show_resolve_undo)) + show_cached = 1; + + if (max_prefix) + prune_cache(max_prefix); + if (with_tree) { + /* + * Basic sanity check; show-stages and show-unmerged + * would not make any sense with this option. + */ + if (show_stage || show_unmerged) + die("ls-files --with-tree is incompatible with -s or -u"); + overlay_tree_on_cache(with_tree, max_prefix); + } + show_files(&dir); + if (show_resolve_undo) + show_ru_info(); + + if (ps_matched) { + int bad; + bad = report_path_error(ps_matched, pathspec, prefix); + if (bad) + fprintf(stderr, "Did you forget to 'git add'?\n"); + + return bad ? 1 : 0; + } + + return 0; +} diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c new file mode 100644 index 0000000000..25e83cfe9d --- /dev/null +++ b/builtin/ls-remote.c @@ -0,0 +1,136 @@ +#include "builtin.h" +#include "cache.h" +#include "transport.h" +#include "remote.h" + +static const char ls_remote_usage[] = +"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n" +" [-q|--quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]"; + +/* + * Is there one among the list of patterns that match the tail part + * of the path? + */ +static int tail_match(const char **pattern, const char *path) +{ + const char *p; + char pathbuf[PATH_MAX]; + + if (!pattern) + return 1; /* no restriction */ + + if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf)) + return error("insanely long ref %.*s...", 20, path); + while ((p = *(pattern++)) != NULL) { + if (!fnmatch(p, pathbuf, 0)) + return 1; + } + return 0; +} + +int cmd_ls_remote(int argc, const char **argv, const char *prefix) +{ + int i; + const char *dest = NULL; + unsigned flags = 0; + int get_url = 0; + int quiet = 0; + int status = 0; + const char *uploadpack = NULL; + const char **pattern = NULL; + + struct remote *remote; + struct transport *transport; + const struct ref *ref; + + if (argc == 2 && !strcmp("-h", argv[1])) + usage(ls_remote_usage); + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + + if (*arg == '-') { + if (!prefixcmp(arg, "--upload-pack=")) { + uploadpack = arg + 14; + continue; + } + if (!prefixcmp(arg, "--exec=")) { + uploadpack = arg + 7; + continue; + } + if (!strcmp("--tags", arg) || !strcmp("-t", arg)) { + flags |= REF_TAGS; + continue; + } + if (!strcmp("--heads", arg) || !strcmp("-h", arg)) { + flags |= REF_HEADS; + continue; + } + if (!strcmp("--refs", arg)) { + flags |= REF_NORMAL; + continue; + } + if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { + quiet = 1; + continue; + } + if (!strcmp("--get-url", arg)) { + get_url = 1; + continue; + } + if (!strcmp("--exit-code", arg)) { + /* return this code if no refs are reported */ + status = 2; + continue; + } + usage(ls_remote_usage); + } + dest = arg; + i++; + break; + } + + if (argv[i]) { + int j; + pattern = xcalloc(sizeof(const char *), argc - i + 1); + for (j = i; j < argc; j++) { + int len = strlen(argv[j]); + char *p = xmalloc(len + 3); + sprintf(p, "*/%s", argv[j]); + pattern[j - i] = p; + } + } + remote = remote_get(dest); + if (!remote) { + if (dest) + die("bad repository '%s'", dest); + die("No remote configured to list refs from."); + } + if (!remote->url_nr) + die("remote %s has no configured URL", dest); + + if (get_url) { + printf("%s\n", *remote->url); + return 0; + } + + transport = transport_get(remote, NULL); + if (uploadpack != NULL) + transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack); + + ref = transport_get_remote_refs(transport); + if (transport_disconnect(transport)) + return 1; + + if (!dest && !quiet) + fprintf(stderr, "From %s\n", *remote->url); + for ( ; ref; ref = ref->next) { + if (!check_ref_type(ref, flags)) + continue; + if (!tail_match(pattern, ref->name)) + continue; + printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name); + status = 0; /* we found something */ + } + return status; +} diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c new file mode 100644 index 0000000000..fb76e38d84 --- /dev/null +++ b/builtin/ls-tree.c @@ -0,0 +1,177 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "blob.h" +#include "tree.h" +#include "commit.h" +#include "quote.h" +#include "builtin.h" +#include "parse-options.h" + +static int line_termination = '\n'; +#define LS_RECURSIVE 1 +#define LS_TREE_ONLY 2 +#define LS_SHOW_TREES 4 +#define LS_NAME_ONLY 8 +#define LS_SHOW_SIZE 16 +static int abbrev; +static int ls_options; +static struct pathspec pathspec; +static int chomp_prefix; +static const char *ls_tree_prefix; + +static const char * const ls_tree_usage[] = { + N_("git ls-tree [<options>] <tree-ish> [<path>...]"), + NULL +}; + +static int show_recursive(const char *base, int baselen, const char *pathname) +{ + const char **s; + + if (ls_options & LS_RECURSIVE) + return 1; + + s = pathspec.raw; + if (!s) + return 0; + + for (;;) { + const char *spec = *s++; + int len, speclen; + + if (!spec) + return 0; + if (strncmp(base, spec, baselen)) + continue; + len = strlen(pathname); + spec += baselen; + speclen = strlen(spec); + if (speclen <= len) + continue; + if (spec[len] && spec[len] != '/') + continue; + if (memcmp(pathname, spec, len)) + continue; + return 1; + } +} + +static int show_tree(const unsigned char *sha1, const char *base, int baselen, + const char *pathname, unsigned mode, int stage, void *context) +{ + int retval = 0; + const char *type = blob_type; + + if (S_ISGITLINK(mode)) { + /* + * Maybe we want to have some recursive version here? + * + * Something similar to this incomplete example: + * + if (show_subprojects(base, baselen, pathname)) + retval = READ_TREE_RECURSIVE; + * + */ + type = commit_type; + } else if (S_ISDIR(mode)) { + if (show_recursive(base, baselen, pathname)) { + retval = READ_TREE_RECURSIVE; + if (!(ls_options & LS_SHOW_TREES)) + return retval; + } + type = tree_type; + } + else if (ls_options & LS_TREE_ONLY) + return 0; + + if (chomp_prefix && + (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix))) + return 0; + + if (!(ls_options & LS_NAME_ONLY)) { + if (ls_options & LS_SHOW_SIZE) { + char size_text[24]; + if (!strcmp(type, blob_type)) { + unsigned long size; + if (sha1_object_info(sha1, &size) == OBJ_BAD) + strcpy(size_text, "BAD"); + else + snprintf(size_text, sizeof(size_text), + "%lu", size); + } else + strcpy(size_text, "-"); + printf("%06o %s %s %7s\t", mode, type, + find_unique_abbrev(sha1, abbrev), + size_text); + } else + printf("%06o %s %s\t", mode, type, + find_unique_abbrev(sha1, abbrev)); + } + write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix, + pathname, stdout, line_termination); + return retval; +} + +int cmd_ls_tree(int argc, const char **argv, const char *prefix) +{ + unsigned char sha1[20]; + struct tree *tree; + int i, full_tree = 0; + const struct option ls_tree_options[] = { + OPT_BIT('d', NULL, &ls_options, N_("only show trees"), + LS_TREE_ONLY), + OPT_BIT('r', NULL, &ls_options, N_("recurse into subtrees"), + LS_RECURSIVE), + OPT_BIT('t', NULL, &ls_options, N_("show trees when recursing"), + LS_SHOW_TREES), + OPT_SET_INT('z', NULL, &line_termination, + N_("terminate entries with NUL byte"), 0), + OPT_BIT('l', "long", &ls_options, N_("include object size"), + LS_SHOW_SIZE), + OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"), + LS_NAME_ONLY), + OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"), + LS_NAME_ONLY), + OPT_SET_INT(0, "full-name", &chomp_prefix, + N_("use full path names"), 0), + OPT_BOOLEAN(0, "full-tree", &full_tree, + N_("list entire tree; not just current directory " + "(implies --full-name)")), + OPT__ABBREV(&abbrev), + OPT_END() + }; + + git_config(git_default_config, NULL); + ls_tree_prefix = prefix; + if (prefix && *prefix) + chomp_prefix = strlen(prefix); + + argc = parse_options(argc, argv, prefix, ls_tree_options, + ls_tree_usage, 0); + if (full_tree) { + ls_tree_prefix = prefix = NULL; + chomp_prefix = 0; + } + /* -d -r should imply -t, but -d by itself should not have to. */ + if ( (LS_TREE_ONLY|LS_RECURSIVE) == + ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options)) + ls_options |= LS_SHOW_TREES; + + if (argc < 1) + usage_with_options(ls_tree_usage, ls_tree_options); + if (get_sha1(argv[0], sha1)) + die("Not a valid object name %s", argv[0]); + + init_pathspec(&pathspec, get_pathspec(prefix, argv + 1)); + for (i = 0; i < pathspec.nr; i++) + pathspec.items[i].nowildcard_len = pathspec.items[i].len; + pathspec.has_wildcard = 0; + tree = parse_tree_indirect(sha1); + if (!tree) + die("not a tree object"); + return !!read_tree_recursive(tree, "", 0, 0, &pathspec, show_tree, NULL); +} diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c new file mode 100644 index 0000000000..24a772d8e1 --- /dev/null +++ b/builtin/mailinfo.c @@ -0,0 +1,1040 @@ +/* + * Another stupid program, this one parsing the headers of an + * email to figure out authorship and subject + */ +#include "cache.h" +#include "builtin.h" +#include "utf8.h" +#include "strbuf.h" + +static FILE *cmitmsg, *patchfile, *fin, *fout; + +static int keep_subject; +static int keep_non_patch_brackets_in_subject; +static const char *metainfo_charset; +static struct strbuf line = STRBUF_INIT; +static struct strbuf name = STRBUF_INIT; +static struct strbuf email = STRBUF_INIT; + +static enum { + TE_DONTCARE, TE_QP, TE_BASE64 +} transfer_encoding; + +static struct strbuf charset = STRBUF_INIT; +static int patch_lines; +static struct strbuf **p_hdr_data, **s_hdr_data; +static int use_scissors; +static int use_inbody_headers = 1; + +#define MAX_HDR_PARSED 10 +#define MAX_BOUNDARIES 5 + +static void cleanup_space(struct strbuf *sb); + + +static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email) +{ + struct strbuf *src = name; + if (name->len < 3 || 60 < name->len || strchr(name->buf, '@') || + strchr(name->buf, '<') || strchr(name->buf, '>')) + src = email; + else if (name == out) + return; + strbuf_reset(out); + strbuf_addbuf(out, src); +} + +static void parse_bogus_from(const struct strbuf *line) +{ + /* John Doe <johndoe> */ + + char *bra, *ket; + /* This is fallback, so do not bother if we already have an + * e-mail address. + */ + if (email.len) + return; + + bra = strchr(line->buf, '<'); + if (!bra) + return; + ket = strchr(bra, '>'); + if (!ket) + return; + + strbuf_reset(&email); + strbuf_add(&email, bra + 1, ket - bra - 1); + + strbuf_reset(&name); + strbuf_add(&name, line->buf, bra - line->buf); + strbuf_trim(&name); + get_sane_name(&name, &name, &email); +} + +static void handle_from(const struct strbuf *from) +{ + char *at; + size_t el; + struct strbuf f; + + strbuf_init(&f, from->len); + strbuf_addbuf(&f, from); + + at = strchr(f.buf, '@'); + if (!at) { + parse_bogus_from(from); + return; + } + + /* + * If we already have one email, don't take any confusing lines + */ + if (email.len && strchr(at + 1, '@')) { + strbuf_release(&f); + return; + } + + /* Pick up the string around '@', possibly delimited with <> + * pair; that is the email part. + */ + while (at > f.buf) { + char c = at[-1]; + if (isspace(c)) + break; + if (c == '<') { + at[-1] = ' '; + break; + } + at--; + } + el = strcspn(at, " \n\t\r\v\f>"); + strbuf_reset(&email); + strbuf_add(&email, at, el); + strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0)); + + /* The remainder is name. It could be + * + * - "John Doe <john.doe@xz>" (a), or + * - "john.doe@xz (John Doe)" (b), or + * - "John (zzz) Doe <john.doe@xz> (Comment)" (c) + * + * but we have removed the email part, so + * + * - remove extra spaces which could stay after email (case 'c'), and + * - trim from both ends, possibly removing the () pair at the end + * (cases 'a' and 'b'). + */ + cleanup_space(&f); + strbuf_trim(&f); + if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') { + strbuf_remove(&f, 0, 1); + strbuf_setlen(&f, f.len - 1); + } + + get_sane_name(&name, &f, &email); + strbuf_release(&f); +} + +static void handle_header(struct strbuf **out, const struct strbuf *line) +{ + if (!*out) { + *out = xmalloc(sizeof(struct strbuf)); + strbuf_init(*out, line->len); + } else + strbuf_reset(*out); + + strbuf_addbuf(*out, line); +} + +/* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt + * to have enough heuristics to grok MIME encoded patches often found + * on our mailing lists. For example, we do not even treat header lines + * case insensitively. + */ + +static int slurp_attr(const char *line, const char *name, struct strbuf *attr) +{ + const char *ends, *ap = strcasestr(line, name); + size_t sz; + + strbuf_setlen(attr, 0); + if (!ap) + return 0; + ap += strlen(name); + if (*ap == '"') { + ap++; + ends = "\""; + } + else + ends = "; \t"; + sz = strcspn(ap, ends); + strbuf_add(attr, ap, sz); + return 1; +} + +static struct strbuf *content[MAX_BOUNDARIES]; + +static struct strbuf **content_top = content; + +static void handle_content_type(struct strbuf *line) +{ + struct strbuf *boundary = xmalloc(sizeof(struct strbuf)); + strbuf_init(boundary, line->len); + + if (slurp_attr(line->buf, "boundary=", boundary)) { + strbuf_insert(boundary, 0, "--", 2); + if (++content_top > &content[MAX_BOUNDARIES]) { + fprintf(stderr, "Too many boundaries to handle\n"); + exit(1); + } + *content_top = boundary; + boundary = NULL; + } + slurp_attr(line->buf, "charset=", &charset); + + if (boundary) { + strbuf_release(boundary); + free(boundary); + } +} + +static void handle_content_transfer_encoding(const struct strbuf *line) +{ + if (strcasestr(line->buf, "base64")) + transfer_encoding = TE_BASE64; + else if (strcasestr(line->buf, "quoted-printable")) + transfer_encoding = TE_QP; + else + transfer_encoding = TE_DONTCARE; +} + +static int is_multipart_boundary(const struct strbuf *line) +{ + return (((*content_top)->len <= line->len) && + !memcmp(line->buf, (*content_top)->buf, (*content_top)->len)); +} + +static void cleanup_subject(struct strbuf *subject) +{ + size_t at = 0; + + while (at < subject->len) { + char *pos; + size_t remove; + + switch (subject->buf[at]) { + case 'r': case 'R': + if (subject->len <= at + 3) + break; + if ((subject->buf[at + 1] == 'e' || + subject->buf[at + 1] == 'E') && + subject->buf[at + 2] == ':') { + strbuf_remove(subject, at, 3); + continue; + } + at++; + break; + case ' ': case '\t': case ':': + strbuf_remove(subject, at, 1); + continue; + case '[': + pos = strchr(subject->buf + at, ']'); + if (!pos) + break; + remove = pos - subject->buf + at + 1; + if (!keep_non_patch_brackets_in_subject || + (7 <= remove && + memmem(subject->buf + at, remove, "PATCH", 5))) + strbuf_remove(subject, at, remove); + else { + at += remove; + /* + * If the input had a space after the ], keep + * it. We don't bother with finding the end of + * the space, since we later normalize it + * anyway. + */ + if (isspace(subject->buf[at])) + at += 1; + } + continue; + } + break; + } + strbuf_trim(subject); +} + +static void cleanup_space(struct strbuf *sb) +{ + size_t pos, cnt; + for (pos = 0; pos < sb->len; pos++) { + if (isspace(sb->buf[pos])) { + sb->buf[pos] = ' '; + for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++); + strbuf_remove(sb, pos + 1, cnt); + } + } +} + +static void decode_header(struct strbuf *line); +static const char *header[MAX_HDR_PARSED] = { + "From","Subject","Date", +}; + +static inline int cmp_header(const struct strbuf *line, const char *hdr) +{ + int len = strlen(hdr); + return !strncasecmp(line->buf, hdr, len) && line->len > len && + line->buf[len] == ':' && isspace(line->buf[len + 1]); +} + +static int check_header(const struct strbuf *line, + struct strbuf *hdr_data[], int overwrite) +{ + int i, ret = 0, len; + struct strbuf sb = STRBUF_INIT; + /* search for the interesting parts */ + for (i = 0; header[i]; i++) { + int len = strlen(header[i]); + if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) { + /* Unwrap inline B and Q encoding, and optionally + * normalize the meta information to utf8. + */ + strbuf_add(&sb, line->buf + len + 2, line->len - len - 2); + decode_header(&sb); + handle_header(&hdr_data[i], &sb); + ret = 1; + goto check_header_out; + } + } + + /* Content stuff */ + if (cmp_header(line, "Content-Type")) { + len = strlen("Content-Type: "); + strbuf_add(&sb, line->buf + len, line->len - len); + decode_header(&sb); + strbuf_insert(&sb, 0, "Content-Type: ", len); + handle_content_type(&sb); + ret = 1; + goto check_header_out; + } + if (cmp_header(line, "Content-Transfer-Encoding")) { + len = strlen("Content-Transfer-Encoding: "); + strbuf_add(&sb, line->buf + len, line->len - len); + decode_header(&sb); + handle_content_transfer_encoding(&sb); + ret = 1; + goto check_header_out; + } + + /* for inbody stuff */ + if (!prefixcmp(line->buf, ">From") && isspace(line->buf[5])) { + ret = 1; /* Should this return 0? */ + goto check_header_out; + } + if (!prefixcmp(line->buf, "[PATCH]") && isspace(line->buf[7])) { + for (i = 0; header[i]; i++) { + if (!memcmp("Subject", header[i], 7)) { + handle_header(&hdr_data[i], line); + ret = 1; + goto check_header_out; + } + } + } + +check_header_out: + strbuf_release(&sb); + return ret; +} + +static int is_rfc2822_header(const struct strbuf *line) +{ + /* + * The section that defines the loosest possible + * field name is "3.6.8 Optional fields". + * + * optional-field = field-name ":" unstructured CRLF + * field-name = 1*ftext + * ftext = %d33-57 / %59-126 + */ + int ch; + char *cp = line->buf; + + /* Count mbox From headers as headers */ + if (!prefixcmp(cp, "From ") || !prefixcmp(cp, ">From ")) + return 1; + + while ((ch = *cp++)) { + if (ch == ':') + return 1; + if ((33 <= ch && ch <= 57) || + (59 <= ch && ch <= 126)) + continue; + break; + } + return 0; +} + +static int read_one_header_line(struct strbuf *line, FILE *in) +{ + /* Get the first part of the line. */ + if (strbuf_getline(line, in, '\n')) + return 0; + + /* + * Is it an empty line or not a valid rfc2822 header? + * If so, stop here, and return false ("not a header") + */ + strbuf_rtrim(line); + if (!line->len || !is_rfc2822_header(line)) { + /* Re-add the newline */ + strbuf_addch(line, '\n'); + return 0; + } + + /* + * Now we need to eat all the continuation lines.. + * Yuck, 2822 header "folding" + */ + for (;;) { + int peek; + struct strbuf continuation = STRBUF_INIT; + + peek = fgetc(in); ungetc(peek, in); + if (peek != ' ' && peek != '\t') + break; + if (strbuf_getline(&continuation, in, '\n')) + break; + continuation.buf[0] = ' '; + strbuf_rtrim(&continuation); + strbuf_addbuf(line, &continuation); + } + + return 1; +} + +static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047) +{ + const char *in = q_seg->buf; + int c; + struct strbuf *out = xmalloc(sizeof(struct strbuf)); + strbuf_init(out, q_seg->len); + + while ((c = *in++) != 0) { + if (c == '=') { + int d = *in++; + if (d == '\n' || !d) + break; /* drop trailing newline */ + strbuf_addch(out, (hexval(d) << 4) | hexval(*in++)); + continue; + } + if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */ + c = 0x20; + strbuf_addch(out, c); + } + return out; +} + +static struct strbuf *decode_b_segment(const struct strbuf *b_seg) +{ + /* Decode in..ep, possibly in-place to ot */ + int c, pos = 0, acc = 0; + const char *in = b_seg->buf; + struct strbuf *out = xmalloc(sizeof(struct strbuf)); + strbuf_init(out, b_seg->len); + + while ((c = *in++) != 0) { + if (c == '+') + c = 62; + else if (c == '/') + c = 63; + else if ('A' <= c && c <= 'Z') + c -= 'A'; + else if ('a' <= c && c <= 'z') + c -= 'a' - 26; + else if ('0' <= c && c <= '9') + c -= '0' - 52; + else + continue; /* garbage */ + switch (pos++) { + case 0: + acc = (c << 2); + break; + case 1: + strbuf_addch(out, (acc | (c >> 4))); + acc = (c & 15) << 4; + break; + case 2: + strbuf_addch(out, (acc | (c >> 2))); + acc = (c & 3) << 6; + break; + case 3: + strbuf_addch(out, (acc | c)); + acc = pos = 0; + break; + } + } + return out; +} + +static void convert_to_utf8(struct strbuf *line, const char *charset) +{ + char *out; + + if (!charset || !*charset) + return; + + if (same_encoding(metainfo_charset, charset)) + return; + out = reencode_string(line->buf, metainfo_charset, charset); + if (!out) + die("cannot convert from %s to %s", + charset, metainfo_charset); + strbuf_attach(line, out, strlen(out), strlen(out)); +} + +static int decode_header_bq(struct strbuf *it) +{ + char *in, *ep, *cp; + struct strbuf outbuf = STRBUF_INIT, *dec; + struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT; + int rfc2047 = 0; + + in = it->buf; + while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) { + int encoding; + strbuf_reset(&charset_q); + strbuf_reset(&piecebuf); + rfc2047 = 1; + + if (in != ep) { + /* + * We are about to process an encoded-word + * that begins at ep, but there is something + * before the encoded word. + */ + char *scan; + for (scan = in; scan < ep; scan++) + if (!isspace(*scan)) + break; + + if (scan != ep || in == it->buf) { + /* + * We should not lose that "something", + * unless we have just processed an + * encoded-word, and there is only LWS + * before the one we are about to process. + */ + strbuf_add(&outbuf, in, ep - in); + } + } + /* E.g. + * ep : "=?iso-2022-jp?B?GyR...?= foo" + * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz" + */ + ep += 2; + + if (ep - it->buf >= it->len || !(cp = strchr(ep, '?'))) + goto decode_header_bq_out; + + if (cp + 3 - it->buf > it->len) + goto decode_header_bq_out; + strbuf_add(&charset_q, ep, cp - ep); + + encoding = cp[1]; + if (!encoding || cp[2] != '?') + goto decode_header_bq_out; + ep = strstr(cp + 3, "?="); + if (!ep) + goto decode_header_bq_out; + strbuf_add(&piecebuf, cp + 3, ep - cp - 3); + switch (tolower(encoding)) { + default: + goto decode_header_bq_out; + case 'b': + dec = decode_b_segment(&piecebuf); + break; + case 'q': + dec = decode_q_segment(&piecebuf, 1); + break; + } + if (metainfo_charset) + convert_to_utf8(dec, charset_q.buf); + + strbuf_addbuf(&outbuf, dec); + strbuf_release(dec); + free(dec); + in = ep + 2; + } + strbuf_addstr(&outbuf, in); + strbuf_reset(it); + strbuf_addbuf(it, &outbuf); +decode_header_bq_out: + strbuf_release(&outbuf); + strbuf_release(&charset_q); + strbuf_release(&piecebuf); + return rfc2047; +} + +static void decode_header(struct strbuf *it) +{ + if (decode_header_bq(it)) + return; + /* otherwise "it" is a straight copy of the input. + * This can be binary guck but there is no charset specified. + */ + if (metainfo_charset) + convert_to_utf8(it, ""); +} + +static void decode_transfer_encoding(struct strbuf *line) +{ + struct strbuf *ret; + + switch (transfer_encoding) { + case TE_QP: + ret = decode_q_segment(line, 0); + break; + case TE_BASE64: + ret = decode_b_segment(line); + break; + case TE_DONTCARE: + default: + return; + } + strbuf_reset(line); + strbuf_addbuf(line, ret); + strbuf_release(ret); + free(ret); +} + +static void handle_filter(struct strbuf *line); + +static int find_boundary(void) +{ + while (!strbuf_getline(&line, fin, '\n')) { + if (*content_top && is_multipart_boundary(&line)) + return 1; + } + return 0; +} + +static int handle_boundary(void) +{ + struct strbuf newline = STRBUF_INIT; + + strbuf_addch(&newline, '\n'); +again: + if (line.len >= (*content_top)->len + 2 && + !memcmp(line.buf + (*content_top)->len, "--", 2)) { + /* we hit an end boundary */ + /* pop the current boundary off the stack */ + strbuf_release(*content_top); + free(*content_top); + *content_top = NULL; + + /* technically won't happen as is_multipart_boundary() + will fail first. But just in case.. + */ + if (--content_top < content) { + fprintf(stderr, "Detected mismatched boundaries, " + "can't recover\n"); + exit(1); + } + handle_filter(&newline); + strbuf_release(&newline); + + /* skip to the next boundary */ + if (!find_boundary()) + return 0; + goto again; + } + + /* set some defaults */ + transfer_encoding = TE_DONTCARE; + strbuf_reset(&charset); + + /* slurp in this section's info */ + while (read_one_header_line(&line, fin)) + check_header(&line, p_hdr_data, 0); + + strbuf_release(&newline); + /* replenish line */ + if (strbuf_getline(&line, fin, '\n')) + return 0; + strbuf_addch(&line, '\n'); + return 1; +} + +static inline int patchbreak(const struct strbuf *line) +{ + size_t i; + + /* Beginning of a "diff -" header? */ + if (!prefixcmp(line->buf, "diff -")) + return 1; + + /* CVS "Index: " line? */ + if (!prefixcmp(line->buf, "Index: ")) + return 1; + + /* + * "--- <filename>" starts patches without headers + * "---<sp>*" is a manual separator + */ + if (line->len < 4) + return 0; + + if (!prefixcmp(line->buf, "---")) { + /* space followed by a filename? */ + if (line->buf[3] == ' ' && !isspace(line->buf[4])) + return 1; + /* Just whitespace? */ + for (i = 3; i < line->len; i++) { + unsigned char c = line->buf[i]; + if (c == '\n') + return 1; + if (!isspace(c)) + break; + } + return 0; + } + return 0; +} + +static int is_scissors_line(const struct strbuf *line) +{ + size_t i, len = line->len; + int scissors = 0, gap = 0; + int first_nonblank = -1; + int last_nonblank = 0, visible, perforation = 0, in_perforation = 0; + const char *buf = line->buf; + + for (i = 0; i < len; i++) { + if (isspace(buf[i])) { + if (in_perforation) { + perforation++; + gap++; + } + continue; + } + last_nonblank = i; + if (first_nonblank < 0) + first_nonblank = i; + if (buf[i] == '-') { + in_perforation = 1; + perforation++; + continue; + } + if (i + 1 < len && + (!memcmp(buf + i, ">8", 2) || !memcmp(buf + i, "8<", 2) || + !memcmp(buf + i, ">%", 2) || !memcmp(buf + i, "%<", 2))) { + in_perforation = 1; + perforation += 2; + scissors += 2; + i++; + continue; + } + in_perforation = 0; + } + + /* + * The mark must be at least 8 bytes long (e.g. "-- >8 --"). + * Even though there can be arbitrary cruft on the same line + * (e.g. "cut here"), in order to avoid misidentification, the + * perforation must occupy more than a third of the visible + * width of the line, and dashes and scissors must occupy more + * than half of the perforation. + */ + + visible = last_nonblank - first_nonblank + 1; + return (scissors && 8 <= visible && + visible < perforation * 3 && + gap * 2 < perforation); +} + +static int handle_commit_msg(struct strbuf *line) +{ + static int still_looking = 1; + + if (!cmitmsg) + return 0; + + if (still_looking) { + if (!line->len || (line->len == 1 && line->buf[0] == '\n')) + return 0; + } + + if (use_inbody_headers && still_looking) { + still_looking = check_header(line, s_hdr_data, 0); + if (still_looking) + return 0; + } else + /* Only trim the first (blank) line of the commit message + * when ignoring in-body headers. + */ + still_looking = 0; + + /* normalize the log message to UTF-8. */ + if (metainfo_charset) + convert_to_utf8(line, charset.buf); + + if (use_scissors && is_scissors_line(line)) { + int i; + if (fseek(cmitmsg, 0L, SEEK_SET)) + die_errno("Could not rewind output message file"); + if (ftruncate(fileno(cmitmsg), 0)) + die_errno("Could not truncate output message file at scissors"); + still_looking = 1; + + /* + * We may have already read "secondary headers"; purge + * them to give ourselves a clean restart. + */ + for (i = 0; header[i]; i++) { + if (s_hdr_data[i]) + strbuf_release(s_hdr_data[i]); + s_hdr_data[i] = NULL; + } + return 0; + } + + if (patchbreak(line)) { + fclose(cmitmsg); + cmitmsg = NULL; + return 1; + } + + fputs(line->buf, cmitmsg); + return 0; +} + +static void handle_patch(const struct strbuf *line) +{ + fwrite(line->buf, 1, line->len, patchfile); + patch_lines++; +} + +static void handle_filter(struct strbuf *line) +{ + static int filter = 0; + + /* filter tells us which part we left off on */ + switch (filter) { + case 0: + if (!handle_commit_msg(line)) + break; + filter++; + case 1: + handle_patch(line); + break; + } +} + +static void handle_body(void) +{ + struct strbuf prev = STRBUF_INIT; + + /* Skip up to the first boundary */ + if (*content_top) { + if (!find_boundary()) + goto handle_body_out; + } + + do { + /* process any boundary lines */ + if (*content_top && is_multipart_boundary(&line)) { + /* flush any leftover */ + if (prev.len) { + handle_filter(&prev); + strbuf_reset(&prev); + } + if (!handle_boundary()) + goto handle_body_out; + } + + /* Unwrap transfer encoding */ + decode_transfer_encoding(&line); + + switch (transfer_encoding) { + case TE_BASE64: + case TE_QP: + { + struct strbuf **lines, **it, *sb; + + /* Prepend any previous partial lines */ + strbuf_insert(&line, 0, prev.buf, prev.len); + strbuf_reset(&prev); + + /* + * This is a decoded line that may contain + * multiple new lines. Pass only one chunk + * at a time to handle_filter() + */ + lines = strbuf_split(&line, '\n'); + for (it = lines; (sb = *it); it++) { + if (*(it + 1) == NULL) /* The last line */ + if (sb->buf[sb->len - 1] != '\n') { + /* Partial line, save it for later. */ + strbuf_addbuf(&prev, sb); + break; + } + handle_filter(sb); + } + /* + * The partial chunk is saved in "prev" and will be + * appended by the next iteration of read_line_with_nul(). + */ + strbuf_list_free(lines); + break; + } + default: + handle_filter(&line); + } + + } while (!strbuf_getwholeline(&line, fin, '\n')); + +handle_body_out: + strbuf_release(&prev); +} + +static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data) +{ + const char *sp = data->buf; + while (1) { + char *ep = strchr(sp, '\n'); + int len; + if (!ep) + len = strlen(sp); + else + len = ep - sp; + fprintf(fout, "%s: %.*s\n", hdr, len, sp); + if (!ep) + break; + sp = ep + 1; + } +} + +static void handle_info(void) +{ + struct strbuf *hdr; + int i; + + for (i = 0; header[i]; i++) { + /* only print inbody headers if we output a patch file */ + if (patch_lines && s_hdr_data[i]) + hdr = s_hdr_data[i]; + else if (p_hdr_data[i]) + hdr = p_hdr_data[i]; + else + continue; + + if (!memcmp(header[i], "Subject", 7)) { + if (!keep_subject) { + cleanup_subject(hdr); + cleanup_space(hdr); + } + output_header_lines(fout, "Subject", hdr); + } else if (!memcmp(header[i], "From", 4)) { + cleanup_space(hdr); + handle_from(hdr); + fprintf(fout, "Author: %s\n", name.buf); + fprintf(fout, "Email: %s\n", email.buf); + } else { + cleanup_space(hdr); + fprintf(fout, "%s: %s\n", header[i], hdr->buf); + } + } + fprintf(fout, "\n"); +} + +static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch) +{ + int peek; + fin = in; + fout = out; + + cmitmsg = fopen(msg, "w"); + if (!cmitmsg) { + perror(msg); + return -1; + } + patchfile = fopen(patch, "w"); + if (!patchfile) { + perror(patch); + fclose(cmitmsg); + return -1; + } + + p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*p_hdr_data)); + s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data)); + + do { + peek = fgetc(in); + } while (isspace(peek)); + ungetc(peek, in); + + /* process the email header */ + while (read_one_header_line(&line, fin)) + check_header(&line, p_hdr_data, 1); + + handle_body(); + handle_info(); + + return 0; +} + +static int git_mailinfo_config(const char *var, const char *value, void *unused) +{ + if (prefixcmp(var, "mailinfo.")) + return git_default_config(var, value, unused); + if (!strcmp(var, "mailinfo.scissors")) { + use_scissors = git_config_bool(var, value); + return 0; + } + /* perhaps others here */ + return 0; +} + +static const char mailinfo_usage[] = + "git mailinfo [-k|-b] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] msg patch < mail >info"; + +int cmd_mailinfo(int argc, const char **argv, const char *prefix) +{ + const char *def_charset; + + /* NEEDSWORK: might want to do the optional .git/ directory + * discovery + */ + git_config(git_mailinfo_config, NULL); + + def_charset = get_commit_output_encoding(); + metainfo_charset = def_charset; + + while (1 < argc && argv[1][0] == '-') { + if (!strcmp(argv[1], "-k")) + keep_subject = 1; + else if (!strcmp(argv[1], "-b")) + keep_non_patch_brackets_in_subject = 1; + else if (!strcmp(argv[1], "-u")) + metainfo_charset = def_charset; + else if (!strcmp(argv[1], "-n")) + metainfo_charset = NULL; + else if (!prefixcmp(argv[1], "--encoding=")) + metainfo_charset = argv[1] + 11; + else if (!strcmp(argv[1], "--scissors")) + use_scissors = 1; + else if (!strcmp(argv[1], "--no-scissors")) + use_scissors = 0; + else if (!strcmp(argv[1], "--no-inbody-headers")) + use_inbody_headers = 0; + else + usage(mailinfo_usage); + argc--; argv++; + } + + if (argc != 3) + usage(mailinfo_usage); + + return !!mailinfo(stdin, stdout, argv[1], argv[2]); +} diff --git a/builtin/mailsplit.c b/builtin/mailsplit.c new file mode 100644 index 0000000000..2d4327801e --- /dev/null +++ b/builtin/mailsplit.c @@ -0,0 +1,309 @@ +/* + * Totally braindamaged mbox splitter program. + * + * It just splits a mbox into a list of files: "0001" "0002" .. + * so you can process them further from there. + */ +#include "cache.h" +#include "builtin.h" +#include "string-list.h" +#include "strbuf.h" + +static const char git_mailsplit_usage[] = +"git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]"; + +static int is_from_line(const char *line, int len) +{ + const char *colon; + + if (len < 20 || memcmp("From ", line, 5)) + return 0; + + colon = line + len - 2; + line += 5; + for (;;) { + if (colon < line) + return 0; + if (*--colon == ':') + break; + } + + if (!isdigit(colon[-4]) || + !isdigit(colon[-2]) || + !isdigit(colon[-1]) || + !isdigit(colon[ 1]) || + !isdigit(colon[ 2])) + return 0; + + /* year */ + if (strtol(colon+3, NULL, 10) <= 90) + return 0; + + /* Ok, close enough */ + return 1; +} + +static struct strbuf buf = STRBUF_INIT; +static int keep_cr; + +/* Called with the first line (potentially partial) + * already in buf[] -- normally that should begin with + * the Unix "From " line. Write it into the specified + * file. + */ +static int split_one(FILE *mbox, const char *name, int allow_bare) +{ + FILE *output = NULL; + int fd; + int status = 0; + int is_bare = !is_from_line(buf.buf, buf.len); + + if (is_bare && !allow_bare) + goto corrupt; + + fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666); + if (fd < 0) + die_errno("cannot open output file '%s'", name); + output = xfdopen(fd, "w"); + + /* Copy it out, while searching for a line that begins with + * "From " and having something that looks like a date format. + */ + for (;;) { + if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' && + buf.buf[buf.len-2] == '\r') { + strbuf_setlen(&buf, buf.len-2); + strbuf_addch(&buf, '\n'); + } + + if (fwrite(buf.buf, 1, buf.len, output) != buf.len) + die_errno("cannot write output"); + + if (strbuf_getwholeline(&buf, mbox, '\n')) { + if (feof(mbox)) { + status = 1; + break; + } + die_errno("cannot read mbox"); + } + if (!is_bare && is_from_line(buf.buf, buf.len)) + break; /* done with one message */ + } + fclose(output); + return status; + + corrupt: + if (output) + fclose(output); + unlink(name); + fprintf(stderr, "corrupt mailbox\n"); + exit(1); +} + +static int populate_maildir_list(struct string_list *list, const char *path) +{ + DIR *dir; + struct dirent *dent; + char name[PATH_MAX]; + char *subs[] = { "cur", "new", NULL }; + char **sub; + + for (sub = subs; *sub; ++sub) { + snprintf(name, sizeof(name), "%s/%s", path, *sub); + if ((dir = opendir(name)) == NULL) { + if (errno == ENOENT) + continue; + error("cannot opendir %s (%s)", name, strerror(errno)); + return -1; + } + + while ((dent = readdir(dir)) != NULL) { + if (dent->d_name[0] == '.') + continue; + snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name); + string_list_insert(list, name); + } + + closedir(dir); + } + + return 0; +} + +static int split_maildir(const char *maildir, const char *dir, + int nr_prec, int skip) +{ + char file[PATH_MAX]; + char name[PATH_MAX]; + int ret = -1; + int i; + struct string_list list = STRING_LIST_INIT_DUP; + + if (populate_maildir_list(&list, maildir) < 0) + goto out; + + for (i = 0; i < list.nr; i++) { + FILE *f; + snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string); + f = fopen(file, "r"); + if (!f) { + error("cannot open mail %s (%s)", file, strerror(errno)); + goto out; + } + + if (strbuf_getwholeline(&buf, f, '\n')) { + error("cannot read mail %s (%s)", file, strerror(errno)); + goto out; + } + + sprintf(name, "%s/%0*d", dir, nr_prec, ++skip); + split_one(f, name, 1); + + fclose(f); + } + + ret = skip; +out: + string_list_clear(&list, 1); + return ret; +} + +static int split_mbox(const char *file, const char *dir, int allow_bare, + int nr_prec, int skip) +{ + char name[PATH_MAX]; + int ret = -1; + int peek; + + FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r"); + int file_done = 0; + + if (!f) { + error("cannot open mbox %s", file); + goto out; + } + + do { + peek = fgetc(f); + } while (isspace(peek)); + ungetc(peek, f); + + if (strbuf_getwholeline(&buf, f, '\n')) { + /* empty stdin is OK */ + if (f != stdin) { + error("cannot read mbox %s", file); + goto out; + } + file_done = 1; + } + + while (!file_done) { + sprintf(name, "%s/%0*d", dir, nr_prec, ++skip); + file_done = split_one(f, name, allow_bare); + } + + if (f != stdin) + fclose(f); + + ret = skip; +out: + return ret; +} + +int cmd_mailsplit(int argc, const char **argv, const char *prefix) +{ + int nr = 0, nr_prec = 4, num = 0; + int allow_bare = 0; + const char *dir = NULL; + const char **argp; + static const char *stdin_only[] = { "-", NULL }; + + for (argp = argv+1; *argp; argp++) { + const char *arg = *argp; + + if (arg[0] != '-') + break; + /* do flags here */ + if ( arg[1] == 'd' ) { + nr_prec = strtol(arg+2, NULL, 10); + if (nr_prec < 3 || 10 <= nr_prec) + usage(git_mailsplit_usage); + continue; + } else if ( arg[1] == 'f' ) { + nr = strtol(arg+2, NULL, 10); + } else if ( arg[1] == 'h' ) { + usage(git_mailsplit_usage); + } else if ( arg[1] == 'b' && !arg[2] ) { + allow_bare = 1; + } else if (!strcmp(arg, "--keep-cr")) { + keep_cr = 1; + } else if ( arg[1] == 'o' && arg[2] ) { + dir = arg+2; + } else if ( arg[1] == '-' && !arg[2] ) { + argp++; /* -- marks end of options */ + break; + } else { + die("unknown option: %s", arg); + } + } + + if ( !dir ) { + /* Backwards compatibility: if no -o specified, accept + <mbox> <dir> or just <dir> */ + switch (argc - (argp-argv)) { + case 1: + dir = argp[0]; + argp = stdin_only; + break; + case 2: + stdin_only[0] = argp[0]; + dir = argp[1]; + argp = stdin_only; + break; + default: + usage(git_mailsplit_usage); + } + } else { + /* New usage: if no more argument, parse stdin */ + if ( !*argp ) + argp = stdin_only; + } + + while (*argp) { + const char *arg = *argp++; + struct stat argstat; + int ret = 0; + + if (arg[0] == '-' && arg[1] == 0) { + ret = split_mbox(arg, dir, allow_bare, nr_prec, nr); + if (ret < 0) { + error("cannot split patches from stdin"); + return 1; + } + num += (ret - nr); + nr = ret; + continue; + } + + if (stat(arg, &argstat) == -1) { + error("cannot stat %s (%s)", arg, strerror(errno)); + return 1; + } + + if (S_ISDIR(argstat.st_mode)) + ret = split_maildir(arg, dir, nr_prec, nr); + else + ret = split_mbox(arg, dir, allow_bare, nr_prec, nr); + + if (ret < 0) { + error("cannot split patches from %s", arg); + return 1; + } + num += (ret - nr); + nr = ret; + } + + printf("%d\n", num); + + return 0; +} diff --git a/builtin/merge-base.c b/builtin/merge-base.c new file mode 100644 index 0000000000..1bc7991048 --- /dev/null +++ b/builtin/merge-base.c @@ -0,0 +1,124 @@ +#include "builtin.h" +#include "cache.h" +#include "commit.h" +#include "parse-options.h" + +static int show_merge_base(struct commit **rev, int rev_nr, int show_all) +{ + struct commit_list *result; + + result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0); + + if (!result) + return 1; + + while (result) { + printf("%s\n", sha1_to_hex(result->item->object.sha1)); + if (!show_all) + return 0; + result = result->next; + } + + return 0; +} + +static const char * const merge_base_usage[] = { + N_("git merge-base [-a|--all] <commit> <commit>..."), + N_("git merge-base [-a|--all] --octopus <commit>..."), + N_("git merge-base --independent <commit>..."), + N_("git merge-base --is-ancestor <commit> <commit>"), + NULL +}; + +static struct commit *get_commit_reference(const char *arg) +{ + unsigned char revkey[20]; + struct commit *r; + + if (get_sha1(arg, revkey)) + die("Not a valid object name %s", arg); + r = lookup_commit_reference(revkey); + if (!r) + die("Not a valid commit name %s", arg); + + return r; +} + +static int handle_octopus(int count, const char **args, int reduce, int show_all) +{ + struct commit_list *revs = NULL; + struct commit_list *result; + int i; + + if (reduce) + show_all = 1; + + for (i = count - 1; i >= 0; i--) + commit_list_insert(get_commit_reference(args[i]), &revs); + + result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs); + + if (!result) + return 1; + + while (result) { + printf("%s\n", sha1_to_hex(result->item->object.sha1)); + if (!show_all) + return 0; + result = result->next; + } + + return 0; +} + +static int handle_is_ancestor(int argc, const char **argv) +{ + struct commit *one, *two; + + if (argc != 2) + die("--is-ancestor takes exactly two commits"); + one = get_commit_reference(argv[0]); + two = get_commit_reference(argv[1]); + if (in_merge_bases(one, two)) + return 0; + else + return 1; +} + +int cmd_merge_base(int argc, const char **argv, const char *prefix) +{ + struct commit **rev; + int rev_nr = 0; + int show_all = 0; + int octopus = 0; + int reduce = 0; + int is_ancestor = 0; + + struct option options[] = { + OPT_BOOLEAN('a', "all", &show_all, N_("output all common ancestors")), + OPT_BOOLEAN(0, "octopus", &octopus, N_("find ancestors for a single n-way merge")), + OPT_BOOLEAN(0, "independent", &reduce, N_("list revs not reachable from others")), + OPT_BOOLEAN(0, "is-ancestor", &is_ancestor, + N_("is the first one ancestor of the other?")), + OPT_END() + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0); + if (!octopus && !reduce && argc < 2) + usage_with_options(merge_base_usage, options); + if (is_ancestor && (show_all | octopus | reduce)) + die("--is-ancestor cannot be used with other options"); + if (is_ancestor) + return handle_is_ancestor(argc, argv); + if (reduce && (show_all || octopus)) + die("--independent cannot be used with other options"); + + if (octopus || reduce) + return handle_octopus(argc, argv, reduce, show_all); + + rev = xmalloc(argc * sizeof(*rev)); + while (argc-- > 0) + rev[rev_nr++] = get_commit_reference(*argv++); + return show_merge_base(rev, rev_nr, show_all); +} diff --git a/builtin/merge-file.c b/builtin/merge-file.c new file mode 100644 index 0000000000..c0570f2407 --- /dev/null +++ b/builtin/merge-file.c @@ -0,0 +1,106 @@ +#include "builtin.h" +#include "cache.h" +#include "xdiff/xdiff.h" +#include "xdiff-interface.h" +#include "parse-options.h" + +static const char *const merge_file_usage[] = { + N_("git merge-file [options] [-L name1 [-L orig [-L name2]]] file1 orig_file file2"), + NULL +}; + +static int label_cb(const struct option *opt, const char *arg, int unset) +{ + static int label_count = 0; + const char **names = (const char **)opt->value; + + if (label_count >= 3) + return error("too many labels on the command line"); + names[label_count++] = arg; + return 0; +} + +int cmd_merge_file(int argc, const char **argv, const char *prefix) +{ + const char *names[3] = { NULL, NULL, NULL }; + mmfile_t mmfs[3]; + mmbuffer_t result = {NULL, 0}; + xmparam_t xmp = {{0}}; + int ret = 0, i = 0, to_stdout = 0; + int quiet = 0; + int prefixlen = 0; + struct option options[] = { + OPT_BOOLEAN('p', "stdout", &to_stdout, N_("send results to standard output")), + OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3), + OPT_SET_INT(0, "ours", &xmp.favor, N_("for conflicts, use our version"), + XDL_MERGE_FAVOR_OURS), + OPT_SET_INT(0, "theirs", &xmp.favor, N_("for conflicts, use their version"), + XDL_MERGE_FAVOR_THEIRS), + OPT_SET_INT(0, "union", &xmp.favor, N_("for conflicts, use a union version"), + XDL_MERGE_FAVOR_UNION), + OPT_INTEGER(0, "marker-size", &xmp.marker_size, + N_("for conflicts, use this marker size")), + OPT__QUIET(&quiet, N_("do not warn about conflicts")), + OPT_CALLBACK('L', NULL, names, N_("name"), + N_("set labels for file1/orig_file/file2"), &label_cb), + OPT_END(), + }; + + xmp.level = XDL_MERGE_ZEALOUS_ALNUM; + xmp.style = 0; + xmp.favor = 0; + + if (startup_info->have_repository) { + /* Read the configuration file */ + git_config(git_xmerge_config, NULL); + if (0 <= git_xmerge_style) + xmp.style = git_xmerge_style; + } + + argc = parse_options(argc, argv, prefix, options, merge_file_usage, 0); + if (argc != 3) + usage_with_options(merge_file_usage, options); + if (quiet) { + if (!freopen("/dev/null", "w", stderr)) + return error("failed to redirect stderr to /dev/null: " + "%s", strerror(errno)); + } + + if (prefix) + prefixlen = strlen(prefix); + + for (i = 0; i < 3; i++) { + const char *fname = prefix_filename(prefix, prefixlen, argv[i]); + if (!names[i]) + names[i] = argv[i]; + if (read_mmfile(mmfs + i, fname)) + return -1; + if (buffer_is_binary(mmfs[i].ptr, mmfs[i].size)) + return error("Cannot merge binary files: %s", + argv[i]); + } + + xmp.ancestor = names[1]; + xmp.file1 = names[0]; + xmp.file2 = names[2]; + ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result); + + for (i = 0; i < 3; i++) + free(mmfs[i].ptr); + + if (ret >= 0) { + const char *filename = argv[0]; + FILE *f = to_stdout ? stdout : fopen(filename, "wb"); + + if (!f) + ret = error("Could not open %s for writing", filename); + else if (result.size && + fwrite(result.ptr, result.size, 1, f) != 1) + ret = error("Could not write to %s", filename); + else if (fclose(f)) + ret = error("Could not close %s", filename); + free(result.ptr); + } + + return ret; +} diff --git a/builtin/merge-index.c b/builtin/merge-index.c new file mode 100644 index 0000000000..be5e514324 --- /dev/null +++ b/builtin/merge-index.c @@ -0,0 +1,110 @@ +#include "builtin.h" +#include "run-command.h" + +static const char *pgm; +static int one_shot, quiet; +static int err; + +static int merge_entry(int pos, const char *path) +{ + int found; + const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL }; + char hexbuf[4][60]; + char ownbuf[4][60]; + + if (pos >= active_nr) + die("git merge-index: %s not in the cache", path); + found = 0; + do { + struct cache_entry *ce = active_cache[pos]; + int stage = ce_stage(ce); + + if (strcmp(ce->name, path)) + break; + found++; + strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); + sprintf(ownbuf[stage], "%o", ce->ce_mode); + arguments[stage] = hexbuf[stage]; + arguments[stage + 4] = ownbuf[stage]; + } while (++pos < active_nr); + if (!found) + die("git merge-index: %s not in the cache", path); + + if (run_command_v_opt(arguments, 0)) { + if (one_shot) + err++; + else { + if (!quiet) + die("merge program failed"); + exit(1); + } + } + return found; +} + +static void merge_one_path(const char *path) +{ + int pos = cache_name_pos(path, strlen(path)); + + /* + * If it already exists in the cache as stage0, it's + * already merged and there is nothing to do. + */ + if (pos < 0) + merge_entry(-pos-1, path); +} + +static void merge_all(void) +{ + int i; + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + if (!ce_stage(ce)) + continue; + i += merge_entry(i, ce->name)-1; + } +} + +int cmd_merge_index(int argc, const char **argv, const char *prefix) +{ + int i, force_file = 0; + + /* Without this we cannot rely on waitpid() to tell + * what happened to our children. + */ + signal(SIGCHLD, SIG_DFL); + + if (argc < 3) + usage("git merge-index [-o] [-q] <merge-program> (-a | [--] <filename>*)"); + + read_cache(); + + i = 1; + if (!strcmp(argv[i], "-o")) { + one_shot = 1; + i++; + } + if (!strcmp(argv[i], "-q")) { + quiet = 1; + i++; + } + pgm = argv[i++]; + for (; i < argc; i++) { + const char *arg = argv[i]; + if (!force_file && *arg == '-') { + if (!strcmp(arg, "--")) { + force_file = 1; + continue; + } + if (!strcmp(arg, "-a")) { + merge_all(); + continue; + } + die("git merge-index: unknown option %s", arg); + } + merge_one_path(arg); + } + if (err && !quiet) + die("merge program failed"); + return err; +} diff --git a/builtin/merge-ours.c b/builtin/merge-ours.c new file mode 100644 index 0000000000..684411694f --- /dev/null +++ b/builtin/merge-ours.c @@ -0,0 +1,34 @@ +/* + * Implementation of git-merge-ours.sh as builtin + * + * Copyright (c) 2007 Thomas Harning Jr + * Original: + * Original Copyright (c) 2005 Junio C Hamano + * + * Pretend we resolved the heads, but declare our tree trumps everybody else. + */ +#include "git-compat-util.h" +#include "builtin.h" + +static const char builtin_merge_ours_usage[] = + "git merge-ours <base>... -- HEAD <remote>..."; + +static const char *diff_index_args[] = { + "diff-index", "--quiet", "--cached", "HEAD", "--", NULL +}; +#define NARGS (ARRAY_SIZE(diff_index_args) - 1) + +int cmd_merge_ours(int argc, const char **argv, const char *prefix) +{ + if (argc == 2 && !strcmp(argv[1], "-h")) + usage(builtin_merge_ours_usage); + + /* + * We need to exit with 2 if the index does not match our HEAD tree, + * because the current index is what we will be committing as the + * merge result. + */ + if (cmd_diff_index(NARGS, diff_index_args, prefix)) + exit(2); + exit(0); +} diff --git a/builtin/merge-recursive.c b/builtin/merge-recursive.c new file mode 100644 index 0000000000..3a64f5d0bd --- /dev/null +++ b/builtin/merge-recursive.c @@ -0,0 +1,80 @@ +#include "builtin.h" +#include "commit.h" +#include "tag.h" +#include "merge-recursive.h" +#include "xdiff-interface.h" + +static const char builtin_merge_recursive_usage[] = + "git %s <base>... -- <head> <remote> ..."; + +static const char *better_branch_name(const char *branch) +{ + static char githead_env[8 + 40 + 1]; + char *name; + + if (strlen(branch) != 40) + return branch; + sprintf(githead_env, "GITHEAD_%s", branch); + name = getenv(githead_env); + return name ? name : branch; +} + +int cmd_merge_recursive(int argc, const char **argv, const char *prefix) +{ + const unsigned char *bases[21]; + unsigned bases_count = 0; + int i, failed; + unsigned char h1[20], h2[20]; + struct merge_options o; + struct commit *result; + + init_merge_options(&o); + if (argv[0] && !suffixcmp(argv[0], "-subtree")) + o.subtree_shift = ""; + + if (argc < 4) + usagef(builtin_merge_recursive_usage, argv[0]); + + for (i = 1; i < argc; ++i) { + const char *arg = argv[i]; + + if (!prefixcmp(arg, "--")) { + if (!arg[2]) + break; + if (parse_merge_opt(&o, arg + 2)) + die("Unknown option %s", arg); + continue; + } + if (bases_count < ARRAY_SIZE(bases)-1) { + unsigned char *sha = xmalloc(20); + if (get_sha1(argv[i], sha)) + die("Could not parse object '%s'", argv[i]); + bases[bases_count++] = sha; + } + else + warning("Cannot handle more than %d bases. " + "Ignoring %s.", + (int)ARRAY_SIZE(bases)-1, argv[i]); + } + if (argc - i != 3) /* "--" "<head>" "<remote>" */ + die("Not handling anything other than two heads merge."); + + o.branch1 = argv[++i]; + o.branch2 = argv[++i]; + + if (get_sha1(o.branch1, h1)) + die("Could not resolve ref '%s'", o.branch1); + if (get_sha1(o.branch2, h2)) + die("Could not resolve ref '%s'", o.branch2); + + o.branch1 = better_branch_name(o.branch1); + o.branch2 = better_branch_name(o.branch2); + + if (o.verbosity >= 3) + printf("Merging %s with %s\n", o.branch1, o.branch2); + + failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result); + if (failed < 0) + return 128; /* die() error code */ + return failed; +} diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c new file mode 100644 index 0000000000..e0d0b7d28b --- /dev/null +++ b/builtin/merge-tree.c @@ -0,0 +1,380 @@ +#include "builtin.h" +#include "tree-walk.h" +#include "xdiff-interface.h" +#include "blob.h" +#include "exec_cmd.h" +#include "merge-blobs.h" + +static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>"; + +struct merge_list { + struct merge_list *next; + struct merge_list *link; /* other stages for this object */ + + unsigned int stage : 2; + unsigned int mode; + const char *path; + struct blob *blob; +}; + +static struct merge_list *merge_result, **merge_result_end = &merge_result; + +static void add_merge_entry(struct merge_list *entry) +{ + *merge_result_end = entry; + merge_result_end = &entry->next; +} + +static void merge_trees_recursive(struct tree_desc t[3], const char *base, int df_conflict); + +static const char *explanation(struct merge_list *entry) +{ + switch (entry->stage) { + case 0: + return "merged"; + case 3: + return "added in remote"; + case 2: + if (entry->link) + return "added in both"; + return "added in local"; + } + + /* Existed in base */ + entry = entry->link; + if (!entry) + return "removed in both"; + + if (entry->link) + return "changed in both"; + + if (entry->stage == 3) + return "removed in local"; + return "removed in remote"; +} + +static void *result(struct merge_list *entry, unsigned long *size) +{ + enum object_type type; + struct blob *base, *our, *their; + const char *path = entry->path; + + if (!entry->stage) + return read_sha1_file(entry->blob->object.sha1, &type, size); + base = NULL; + if (entry->stage == 1) { + base = entry->blob; + entry = entry->link; + } + our = NULL; + if (entry && entry->stage == 2) { + our = entry->blob; + entry = entry->link; + } + their = NULL; + if (entry) + their = entry->blob; + return merge_blobs(path, base, our, their, size); +} + +static void *origin(struct merge_list *entry, unsigned long *size) +{ + enum object_type type; + while (entry) { + if (entry->stage == 2) + return read_sha1_file(entry->blob->object.sha1, &type, size); + entry = entry->link; + } + return NULL; +} + +static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf) +{ + int i; + for (i = 0; i < nbuf; i++) + printf("%.*s", (int) mb[i].size, mb[i].ptr); + return 0; +} + +static void show_diff(struct merge_list *entry) +{ + unsigned long size; + mmfile_t src, dst; + xpparam_t xpp; + xdemitconf_t xecfg; + xdemitcb_t ecb; + + xpp.flags = 0; + memset(&xecfg, 0, sizeof(xecfg)); + xecfg.ctxlen = 3; + ecb.outf = show_outf; + ecb.priv = NULL; + + src.ptr = origin(entry, &size); + if (!src.ptr) + size = 0; + src.size = size; + dst.ptr = result(entry, &size); + if (!dst.ptr) + size = 0; + dst.size = size; + xdi_diff(&src, &dst, &xpp, &xecfg, &ecb); + free(src.ptr); + free(dst.ptr); +} + +static void show_result_list(struct merge_list *entry) +{ + printf("%s\n", explanation(entry)); + do { + struct merge_list *link = entry->link; + static const char *desc[4] = { "result", "base", "our", "their" }; + printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path); + entry = link; + } while (entry); +} + +static void show_result(void) +{ + struct merge_list *walk; + + walk = merge_result; + while (walk) { + show_result_list(walk); + show_diff(walk); + walk = walk->next; + } +} + +/* An empty entry never compares same, not even to another empty entry */ +static int same_entry(struct name_entry *a, struct name_entry *b) +{ + return a->sha1 && + b->sha1 && + !hashcmp(a->sha1, b->sha1) && + a->mode == b->mode; +} + +static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path) +{ + struct merge_list *res = xcalloc(1, sizeof(*res)); + + res->stage = stage; + res->path = path; + res->mode = mode; + res->blob = lookup_blob(sha1); + return res; +} + +static char *traverse_path(const struct traverse_info *info, const struct name_entry *n) +{ + char *path = xmalloc(traverse_path_len(info, n) + 1); + return make_traverse_path(path, info, n); +} + +static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result) +{ + struct merge_list *orig, *final; + const char *path; + + /* If it's already ours, don't bother showing it */ + if (!ours) + return; + + path = traverse_path(info, result); + orig = create_entry(2, ours->mode, ours->sha1, path); + final = create_entry(0, result->mode, result->sha1, path); + + final->link = orig; + + add_merge_entry(final); +} + +static void unresolved_directory(const struct traverse_info *info, struct name_entry n[3], + int df_conflict) +{ + char *newbase; + struct name_entry *p; + struct tree_desc t[3]; + void *buf0, *buf1, *buf2; + + for (p = n; p < n + 3; p++) { + if (p->mode && S_ISDIR(p->mode)) + break; + } + if (n + 3 <= p) + return; /* there is no tree here */ + + newbase = traverse_path(info, p); + +#define ENTRY_SHA1(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->sha1 : NULL) + buf0 = fill_tree_descriptor(t+0, ENTRY_SHA1(n + 0)); + buf1 = fill_tree_descriptor(t+1, ENTRY_SHA1(n + 1)); + buf2 = fill_tree_descriptor(t+2, ENTRY_SHA1(n + 2)); +#undef ENTRY_SHA1 + + merge_trees_recursive(t, newbase, df_conflict); + + free(buf0); + free(buf1); + free(buf2); + free(newbase); +} + + +static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry) +{ + const char *path; + struct merge_list *link; + + if (!n->mode) + return entry; + if (entry) + path = entry->path; + else + path = traverse_path(info, n); + link = create_entry(stage, n->mode, n->sha1, path); + link->link = entry; + return link; +} + +static void unresolved(const struct traverse_info *info, struct name_entry n[3]) +{ + struct merge_list *entry = NULL; + int i; + unsigned dirmask = 0, mask = 0; + + for (i = 0; i < 3; i++) { + mask |= (1 << 1); + if (n[i].mode && S_ISDIR(n[i].mode)) + dirmask |= (1 << i); + } + + unresolved_directory(info, n, dirmask && (dirmask != mask)); + + if (dirmask == mask) + return; + + if (n[2].mode && !S_ISDIR(n[2].mode)) + entry = link_entry(3, info, n + 2, entry); + if (n[1].mode && !S_ISDIR(n[1].mode)) + entry = link_entry(2, info, n + 1, entry); + if (n[0].mode && !S_ISDIR(n[0].mode)) + entry = link_entry(1, info, n + 0, entry); + + add_merge_entry(entry); +} + +/* + * Merge two trees together (t[1] and t[2]), using a common base (t[0]) + * as the origin. + * + * This walks the (sorted) trees in lock-step, checking every possible + * name. Note that directories automatically sort differently from other + * files (see "base_name_compare"), so you'll never see file/directory + * conflicts, because they won't ever compare the same. + * + * IOW, if a directory changes to a filename, it will automatically be + * seen as the directory going away, and the filename being created. + * + * Think of this as a three-way diff. + * + * The output will be either: + * - successful merge + * "0 mode sha1 filename" + * NOTE NOTE NOTE! FIXME! We really really need to walk the index + * in parallel with this too! + * + * - conflict: + * "1 mode sha1 filename" + * "2 mode sha1 filename" + * "3 mode sha1 filename" + * where not all of the 1/2/3 lines may exist, of course. + * + * The successful merge rules are the same as for the three-way merge + * in git-read-tree. + */ +static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info) +{ + /* Same in both? */ + if (same_entry(entry+1, entry+2)) { + if (entry[0].sha1) { + /* Modified identically */ + resolve(info, NULL, entry+1); + return mask; + } + /* "Both added the same" is left unresolved */ + } + + if (same_entry(entry+0, entry+1)) { + if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) { + /* We did not touch, they modified -- take theirs */ + resolve(info, entry+1, entry+2); + return mask; + } + /* + * If we did not touch a directory but they made it + * into a file, we fall through and unresolved() + * recurses down. Likewise for the opposite case. + */ + } + + if (same_entry(entry+0, entry+2)) { + if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) { + /* We modified, they did not touch -- take ours */ + resolve(info, NULL, entry+1); + return mask; + } + } + + unresolved(info, entry); + return mask; +} + +static void merge_trees_recursive(struct tree_desc t[3], const char *base, int df_conflict) +{ + struct traverse_info info; + + setup_traverse_info(&info, base); + info.data = &df_conflict; + info.fn = threeway_callback; + traverse_trees(3, t, &info); +} + +static void merge_trees(struct tree_desc t[3], const char *base) +{ + merge_trees_recursive(t, base, 0); +} + +static void *get_tree_descriptor(struct tree_desc *desc, const char *rev) +{ + unsigned char sha1[20]; + void *buf; + + if (get_sha1(rev, sha1)) + die("unknown rev %s", rev); + buf = fill_tree_descriptor(desc, sha1); + if (!buf) + die("%s is not a tree", rev); + return buf; +} + +int cmd_merge_tree(int argc, const char **argv, const char *prefix) +{ + struct tree_desc t[3]; + void *buf1, *buf2, *buf3; + + if (argc != 4) + usage(merge_tree_usage); + + buf1 = get_tree_descriptor(t+0, argv[1]); + buf2 = get_tree_descriptor(t+1, argv[2]); + buf3 = get_tree_descriptor(t+2, argv[3]); + merge_trees(t, ""); + free(buf1); + free(buf2); + free(buf3); + + show_result(); + return 0; +} diff --git a/builtin/merge.c b/builtin/merge.c new file mode 100644 index 0000000000..9307e9c726 --- /dev/null +++ b/builtin/merge.c @@ -0,0 +1,1513 @@ +/* + * Builtin "git merge" + * + * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org> + * + * Based on git-merge.sh by Junio C Hamano. + */ + +#include "cache.h" +#include "parse-options.h" +#include "builtin.h" +#include "run-command.h" +#include "diff.h" +#include "refs.h" +#include "commit.h" +#include "diffcore.h" +#include "revision.h" +#include "unpack-trees.h" +#include "cache-tree.h" +#include "dir.h" +#include "utf8.h" +#include "log-tree.h" +#include "color.h" +#include "rerere.h" +#include "help.h" +#include "merge-recursive.h" +#include "resolve-undo.h" +#include "remote.h" +#include "fmt-merge-msg.h" +#include "gpg-interface.h" + +#define DEFAULT_TWOHEAD (1<<0) +#define DEFAULT_OCTOPUS (1<<1) +#define NO_FAST_FORWARD (1<<2) +#define NO_TRIVIAL (1<<3) + +struct strategy { + const char *name; + unsigned attr; +}; + +static const char * const builtin_merge_usage[] = { + N_("git merge [options] [<commit>...]"), + N_("git merge [options] <msg> HEAD <commit>"), + N_("git merge --abort"), + NULL +}; + +static int show_diffstat = 1, shortlog_len = -1, squash; +static int option_commit = 1, allow_fast_forward = 1; +static int fast_forward_only, option_edit = -1; +static int allow_trivial = 1, have_message; +static int overwrite_ignore = 1; +static struct strbuf merge_msg = STRBUF_INIT; +static struct strategy **use_strategies; +static size_t use_strategies_nr, use_strategies_alloc; +static const char **xopts; +static size_t xopts_nr, xopts_alloc; +static const char *branch; +static char *branch_mergeoptions; +static int option_renormalize; +static int verbosity; +static int allow_rerere_auto; +static int abort_current_merge; +static int show_progress = -1; +static int default_to_upstream; +static const char *sign_commit; + +static struct strategy all_strategy[] = { + { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL }, + { "octopus", DEFAULT_OCTOPUS }, + { "resolve", 0 }, + { "ours", NO_FAST_FORWARD | NO_TRIVIAL }, + { "subtree", NO_FAST_FORWARD | NO_TRIVIAL }, +}; + +static const char *pull_twohead, *pull_octopus; + +static int option_parse_message(const struct option *opt, + const char *arg, int unset) +{ + struct strbuf *buf = opt->value; + + if (unset) + strbuf_setlen(buf, 0); + else if (arg) { + strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg); + have_message = 1; + } else + return error(_("switch `m' requires a value")); + return 0; +} + +static struct strategy *get_strategy(const char *name) +{ + int i; + struct strategy *ret; + static struct cmdnames main_cmds, other_cmds; + static int loaded; + + if (!name) + return NULL; + + for (i = 0; i < ARRAY_SIZE(all_strategy); i++) + if (!strcmp(name, all_strategy[i].name)) + return &all_strategy[i]; + + if (!loaded) { + struct cmdnames not_strategies; + loaded = 1; + + memset(¬_strategies, 0, sizeof(struct cmdnames)); + load_command_list("git-merge-", &main_cmds, &other_cmds); + for (i = 0; i < main_cmds.cnt; i++) { + int j, found = 0; + struct cmdname *ent = main_cmds.names[i]; + for (j = 0; j < ARRAY_SIZE(all_strategy); j++) + if (!strncmp(ent->name, all_strategy[j].name, ent->len) + && !all_strategy[j].name[ent->len]) + found = 1; + if (!found) + add_cmdname(¬_strategies, ent->name, ent->len); + } + exclude_cmds(&main_cmds, ¬_strategies); + } + if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) { + fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name); + fprintf(stderr, _("Available strategies are:")); + for (i = 0; i < main_cmds.cnt; i++) + fprintf(stderr, " %s", main_cmds.names[i]->name); + fprintf(stderr, ".\n"); + if (other_cmds.cnt) { + fprintf(stderr, _("Available custom strategies are:")); + for (i = 0; i < other_cmds.cnt; i++) + fprintf(stderr, " %s", other_cmds.names[i]->name); + fprintf(stderr, ".\n"); + } + exit(1); + } + + ret = xcalloc(1, sizeof(struct strategy)); + ret->name = xstrdup(name); + ret->attr = NO_TRIVIAL; + return ret; +} + +static void append_strategy(struct strategy *s) +{ + ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc); + use_strategies[use_strategies_nr++] = s; +} + +static int option_parse_strategy(const struct option *opt, + const char *name, int unset) +{ + if (unset) + return 0; + + append_strategy(get_strategy(name)); + return 0; +} + +static int option_parse_x(const struct option *opt, + const char *arg, int unset) +{ + if (unset) + return 0; + + ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc); + xopts[xopts_nr++] = xstrdup(arg); + return 0; +} + +static int option_parse_n(const struct option *opt, + const char *arg, int unset) +{ + show_diffstat = unset; + return 0; +} + +static struct option builtin_merge_options[] = { + { OPTION_CALLBACK, 'n', NULL, NULL, NULL, + N_("do not show a diffstat at the end of the merge"), + PARSE_OPT_NOARG, option_parse_n }, + OPT_BOOLEAN(0, "stat", &show_diffstat, + N_("show a diffstat at the end of the merge")), + OPT_BOOLEAN(0, "summary", &show_diffstat, N_("(synonym to --stat)")), + { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"), + N_("add (at most <n>) entries from shortlog to merge commit message"), + PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN }, + OPT_BOOLEAN(0, "squash", &squash, + N_("create a single commit instead of doing a merge")), + OPT_BOOLEAN(0, "commit", &option_commit, + N_("perform a commit if the merge succeeds (default)")), + OPT_BOOL('e', "edit", &option_edit, + N_("edit message before committing")), + OPT_BOOLEAN(0, "ff", &allow_fast_forward, + N_("allow fast-forward (default)")), + OPT_BOOLEAN(0, "ff-only", &fast_forward_only, + N_("abort if fast-forward is not possible")), + OPT_RERERE_AUTOUPDATE(&allow_rerere_auto), + OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"), + N_("merge strategy to use"), option_parse_strategy), + OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"), + N_("option for selected merge strategy"), option_parse_x), + OPT_CALLBACK('m', "message", &merge_msg, N_("message"), + N_("merge commit message (for a non-fast-forward merge)"), + option_parse_message), + OPT__VERBOSITY(&verbosity), + OPT_BOOLEAN(0, "abort", &abort_current_merge, + N_("abort the current in-progress merge")), + OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1), + { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key id"), + N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, + OPT_BOOLEAN(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")), + OPT_END() +}; + +/* Cleans up metadata that is uninteresting after a succeeded merge. */ +static void drop_save(void) +{ + unlink(git_path("MERGE_HEAD")); + unlink(git_path("MERGE_MSG")); + unlink(git_path("MERGE_MODE")); +} + +static int save_state(unsigned char *stash) +{ + int len; + struct child_process cp; + struct strbuf buffer = STRBUF_INIT; + const char *argv[] = {"stash", "create", NULL}; + + memset(&cp, 0, sizeof(cp)); + cp.argv = argv; + cp.out = -1; + cp.git_cmd = 1; + + if (start_command(&cp)) + die(_("could not run stash.")); + len = strbuf_read(&buffer, cp.out, 1024); + close(cp.out); + + if (finish_command(&cp) || len < 0) + die(_("stash failed")); + else if (!len) /* no changes */ + return -1; + strbuf_setlen(&buffer, buffer.len-1); + if (get_sha1(buffer.buf, stash)) + die(_("not a valid object: %s"), buffer.buf); + return 0; +} + +static void read_empty(unsigned const char *sha1, int verbose) +{ + int i = 0; + const char *args[7]; + + args[i++] = "read-tree"; + if (verbose) + args[i++] = "-v"; + args[i++] = "-m"; + args[i++] = "-u"; + args[i++] = EMPTY_TREE_SHA1_HEX; + args[i++] = sha1_to_hex(sha1); + args[i] = NULL; + + if (run_command_v_opt(args, RUN_GIT_CMD)) + die(_("read-tree failed")); +} + +static void reset_hard(unsigned const char *sha1, int verbose) +{ + int i = 0; + const char *args[6]; + + args[i++] = "read-tree"; + if (verbose) + args[i++] = "-v"; + args[i++] = "--reset"; + args[i++] = "-u"; + args[i++] = sha1_to_hex(sha1); + args[i] = NULL; + + if (run_command_v_opt(args, RUN_GIT_CMD)) + die(_("read-tree failed")); +} + +static void restore_state(const unsigned char *head, + const unsigned char *stash) +{ + struct strbuf sb = STRBUF_INIT; + const char *args[] = { "stash", "apply", NULL, NULL }; + + if (is_null_sha1(stash)) + return; + + reset_hard(head, 1); + + args[2] = sha1_to_hex(stash); + + /* + * It is OK to ignore error here, for example when there was + * nothing to restore. + */ + run_command_v_opt(args, RUN_GIT_CMD); + + strbuf_release(&sb); + refresh_cache(REFRESH_QUIET); +} + +/* This is called when no merge was necessary. */ +static void finish_up_to_date(const char *msg) +{ + if (verbosity >= 0) + printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg); + drop_save(); +} + +static void squash_message(struct commit *commit, struct commit_list *remoteheads) +{ + struct rev_info rev; + struct strbuf out = STRBUF_INIT; + struct commit_list *j; + const char *filename; + int fd; + struct pretty_print_context ctx = {0}; + + printf(_("Squash commit -- not updating HEAD\n")); + filename = git_path("SQUASH_MSG"); + fd = open(filename, O_WRONLY | O_CREAT, 0666); + if (fd < 0) + die_errno(_("Could not write to '%s'"), filename); + + init_revisions(&rev, NULL); + rev.ignore_merges = 1; + rev.commit_format = CMIT_FMT_MEDIUM; + + commit->object.flags |= UNINTERESTING; + add_pending_object(&rev, &commit->object, NULL); + + for (j = remoteheads; j; j = j->next) + add_pending_object(&rev, &j->item->object, NULL); + + setup_revisions(0, NULL, &rev, NULL); + if (prepare_revision_walk(&rev)) + die(_("revision walk setup failed")); + + ctx.abbrev = rev.abbrev; + ctx.date_mode = rev.date_mode; + ctx.fmt = rev.commit_format; + + strbuf_addstr(&out, "Squashed commit of the following:\n"); + while ((commit = get_revision(&rev)) != NULL) { + strbuf_addch(&out, '\n'); + strbuf_addf(&out, "commit %s\n", + sha1_to_hex(commit->object.sha1)); + pretty_print_commit(&ctx, commit, &out); + } + if (write(fd, out.buf, out.len) < 0) + die_errno(_("Writing SQUASH_MSG")); + if (close(fd)) + die_errno(_("Finishing SQUASH_MSG")); + strbuf_release(&out); +} + +static void finish(struct commit *head_commit, + struct commit_list *remoteheads, + const unsigned char *new_head, const char *msg) +{ + struct strbuf reflog_message = STRBUF_INIT; + const unsigned char *head = head_commit->object.sha1; + + if (!msg) + strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION")); + else { + if (verbosity >= 0) + printf("%s\n", msg); + strbuf_addf(&reflog_message, "%s: %s", + getenv("GIT_REFLOG_ACTION"), msg); + } + if (squash) { + squash_message(head_commit, remoteheads); + } else { + if (verbosity >= 0 && !merge_msg.len) + printf(_("No merge message -- not updating HEAD\n")); + else { + const char *argv_gc_auto[] = { "gc", "--auto", NULL }; + update_ref(reflog_message.buf, "HEAD", + new_head, head, 0, + DIE_ON_ERR); + /* + * We ignore errors in 'gc --auto', since the + * user should see them. + */ + run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); + } + } + if (new_head && show_diffstat) { + struct diff_options opts; + diff_setup(&opts); + opts.stat_width = -1; /* use full terminal width */ + opts.stat_graph_width = -1; /* respect statGraphWidth config */ + opts.output_format |= + DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; + opts.detect_rename = DIFF_DETECT_RENAME; + diff_setup_done(&opts); + diff_tree_sha1(head, new_head, "", &opts); + diffcore_std(&opts); + diff_flush(&opts); + } + + /* Run a post-merge hook */ + run_hook(NULL, "post-merge", squash ? "1" : "0", NULL); + + strbuf_release(&reflog_message); +} + +/* Get the name for the merge commit's message. */ +static void merge_name(const char *remote, struct strbuf *msg) +{ + struct commit *remote_head; + unsigned char branch_head[20]; + struct strbuf buf = STRBUF_INIT; + struct strbuf bname = STRBUF_INIT; + const char *ptr; + char *found_ref; + int len, early; + + strbuf_branchname(&bname, remote); + remote = bname.buf; + + memset(branch_head, 0, sizeof(branch_head)); + remote_head = get_merge_parent(remote); + if (!remote_head) + die(_("'%s' does not point to a commit"), remote); + + if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) { + if (!prefixcmp(found_ref, "refs/heads/")) { + strbuf_addf(msg, "%s\t\tbranch '%s' of .\n", + sha1_to_hex(branch_head), remote); + goto cleanup; + } + if (!prefixcmp(found_ref, "refs/tags/")) { + strbuf_addf(msg, "%s\t\ttag '%s' of .\n", + sha1_to_hex(branch_head), remote); + goto cleanup; + } + if (!prefixcmp(found_ref, "refs/remotes/")) { + strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n", + sha1_to_hex(branch_head), remote); + goto cleanup; + } + } + + /* See if remote matches <name>^^^.. or <name>~<number> */ + for (len = 0, ptr = remote + strlen(remote); + remote < ptr && ptr[-1] == '^'; + ptr--) + len++; + if (len) + early = 1; + else { + early = 0; + ptr = strrchr(remote, '~'); + if (ptr) { + int seen_nonzero = 0; + + len++; /* count ~ */ + while (*++ptr && isdigit(*ptr)) { + seen_nonzero |= (*ptr != '0'); + len++; + } + if (*ptr) + len = 0; /* not ...~<number> */ + else if (seen_nonzero) + early = 1; + else if (len == 1) + early = 1; /* "name~" is "name~1"! */ + } + } + if (len) { + struct strbuf truname = STRBUF_INIT; + strbuf_addstr(&truname, "refs/heads/"); + strbuf_addstr(&truname, remote); + strbuf_setlen(&truname, truname.len - len); + if (ref_exists(truname.buf)) { + strbuf_addf(msg, + "%s\t\tbranch '%s'%s of .\n", + sha1_to_hex(remote_head->object.sha1), + truname.buf + 11, + (early ? " (early part)" : "")); + strbuf_release(&truname); + goto cleanup; + } + } + + if (!strcmp(remote, "FETCH_HEAD") && + !access(git_path("FETCH_HEAD"), R_OK)) { + const char *filename; + FILE *fp; + struct strbuf line = STRBUF_INIT; + char *ptr; + + filename = git_path("FETCH_HEAD"); + fp = fopen(filename, "r"); + if (!fp) + die_errno(_("could not open '%s' for reading"), + filename); + strbuf_getline(&line, fp, '\n'); + fclose(fp); + ptr = strstr(line.buf, "\tnot-for-merge\t"); + if (ptr) + strbuf_remove(&line, ptr-line.buf+1, 13); + strbuf_addbuf(msg, &line); + strbuf_release(&line); + goto cleanup; + } + strbuf_addf(msg, "%s\t\tcommit '%s'\n", + sha1_to_hex(remote_head->object.sha1), remote); +cleanup: + strbuf_release(&buf); + strbuf_release(&bname); +} + +static void parse_branch_merge_options(char *bmo) +{ + const char **argv; + int argc; + + if (!bmo) + return; + argc = split_cmdline(bmo, &argv); + if (argc < 0) + die(_("Bad branch.%s.mergeoptions string: %s"), branch, + split_cmdline_strerror(argc)); + argv = xrealloc(argv, sizeof(*argv) * (argc + 2)); + memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); + argc++; + argv[0] = "branch.*.mergeoptions"; + parse_options(argc, argv, NULL, builtin_merge_options, + builtin_merge_usage, 0); + free(argv); +} + +static int git_merge_config(const char *k, const char *v, void *cb) +{ + int status; + + if (branch && !prefixcmp(k, "branch.") && + !prefixcmp(k + 7, branch) && + !strcmp(k + 7 + strlen(branch), ".mergeoptions")) { + free(branch_mergeoptions); + branch_mergeoptions = xstrdup(v); + return 0; + } + + if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat")) + show_diffstat = git_config_bool(k, v); + else if (!strcmp(k, "pull.twohead")) + return git_config_string(&pull_twohead, k, v); + else if (!strcmp(k, "pull.octopus")) + return git_config_string(&pull_octopus, k, v); + else if (!strcmp(k, "merge.renormalize")) + option_renormalize = git_config_bool(k, v); + else if (!strcmp(k, "merge.ff")) { + int boolval = git_config_maybe_bool(k, v); + if (0 <= boolval) { + allow_fast_forward = boolval; + } else if (v && !strcmp(v, "only")) { + allow_fast_forward = 1; + fast_forward_only = 1; + } /* do not barf on values from future versions of git */ + return 0; + } else if (!strcmp(k, "merge.defaulttoupstream")) { + default_to_upstream = git_config_bool(k, v); + return 0; + } + + status = fmt_merge_msg_config(k, v, cb); + if (status) + return status; + status = git_gpg_config(k, v, NULL); + if (status) + return status; + return git_diff_ui_config(k, v, cb); +} + +static int read_tree_trivial(unsigned char *common, unsigned char *head, + unsigned char *one) +{ + int i, nr_trees = 0; + struct tree *trees[MAX_UNPACK_TREES]; + struct tree_desc t[MAX_UNPACK_TREES]; + struct unpack_trees_options opts; + + memset(&opts, 0, sizeof(opts)); + opts.head_idx = 2; + opts.src_index = &the_index; + opts.dst_index = &the_index; + opts.update = 1; + opts.verbose_update = 1; + opts.trivial_merges_only = 1; + opts.merge = 1; + trees[nr_trees] = parse_tree_indirect(common); + if (!trees[nr_trees++]) + return -1; + trees[nr_trees] = parse_tree_indirect(head); + if (!trees[nr_trees++]) + return -1; + trees[nr_trees] = parse_tree_indirect(one); + if (!trees[nr_trees++]) + return -1; + opts.fn = threeway_merge; + cache_tree_free(&active_cache_tree); + for (i = 0; i < nr_trees; i++) { + parse_tree(trees[i]); + init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); + } + if (unpack_trees(nr_trees, t, &opts)) + return -1; + return 0; +} + +static void write_tree_trivial(unsigned char *sha1) +{ + if (write_cache_as_tree(sha1, 0, NULL)) + die(_("git write-tree failed to write a tree")); +} + +static int try_merge_strategy(const char *strategy, struct commit_list *common, + struct commit_list *remoteheads, + struct commit *head, const char *head_arg) +{ + int index_fd; + struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); + + index_fd = hold_locked_index(lock, 1); + refresh_cache(REFRESH_QUIET); + if (active_cache_changed && + (write_cache(index_fd, active_cache, active_nr) || + commit_locked_index(lock))) + return error(_("Unable to write index.")); + rollback_lock_file(lock); + + if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) { + int clean, x; + struct commit *result; + struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); + int index_fd; + struct commit_list *reversed = NULL; + struct merge_options o; + struct commit_list *j; + + if (remoteheads->next) { + error(_("Not handling anything other than two heads merge.")); + return 2; + } + + init_merge_options(&o); + if (!strcmp(strategy, "subtree")) + o.subtree_shift = ""; + + o.renormalize = option_renormalize; + o.show_rename_progress = + show_progress == -1 ? isatty(2) : show_progress; + + for (x = 0; x < xopts_nr; x++) + if (parse_merge_opt(&o, xopts[x])) + die(_("Unknown option for merge-recursive: -X%s"), xopts[x]); + + o.branch1 = head_arg; + o.branch2 = merge_remote_util(remoteheads->item)->name; + + for (j = common; j; j = j->next) + commit_list_insert(j->item, &reversed); + + index_fd = hold_locked_index(lock, 1); + clean = merge_recursive(&o, head, + remoteheads->item, reversed, &result); + if (active_cache_changed && + (write_cache(index_fd, active_cache, active_nr) || + commit_locked_index(lock))) + die (_("unable to write %s"), get_index_file()); + rollback_lock_file(lock); + return clean ? 0 : 1; + } else { + return try_merge_command(strategy, xopts_nr, xopts, + common, head_arg, remoteheads); + } +} + +static void count_diff_files(struct diff_queue_struct *q, + struct diff_options *opt, void *data) +{ + int *count = data; + + (*count) += q->nr; +} + +static int count_unmerged_entries(void) +{ + int i, ret = 0; + + for (i = 0; i < active_nr; i++) + if (ce_stage(active_cache[i])) + ret++; + + return ret; +} + +static void split_merge_strategies(const char *string, struct strategy **list, + int *nr, int *alloc) +{ + char *p, *q, *buf; + + if (!string) + return; + + buf = xstrdup(string); + q = buf; + for (;;) { + p = strchr(q, ' '); + if (!p) { + ALLOC_GROW(*list, *nr + 1, *alloc); + (*list)[(*nr)++].name = xstrdup(q); + free(buf); + return; + } else { + *p = '\0'; + ALLOC_GROW(*list, *nr + 1, *alloc); + (*list)[(*nr)++].name = xstrdup(q); + q = ++p; + } + } +} + +static void add_strategies(const char *string, unsigned attr) +{ + struct strategy *list = NULL; + int list_alloc = 0, list_nr = 0, i; + + memset(&list, 0, sizeof(list)); + split_merge_strategies(string, &list, &list_nr, &list_alloc); + if (list) { + for (i = 0; i < list_nr; i++) + append_strategy(get_strategy(list[i].name)); + return; + } + for (i = 0; i < ARRAY_SIZE(all_strategy); i++) + if (all_strategy[i].attr & attr) + append_strategy(&all_strategy[i]); + +} + +static void write_merge_msg(struct strbuf *msg) +{ + const char *filename = git_path("MERGE_MSG"); + int fd = open(filename, O_WRONLY | O_CREAT, 0666); + if (fd < 0) + die_errno(_("Could not open '%s' for writing"), + filename); + if (write_in_full(fd, msg->buf, msg->len) != msg->len) + die_errno(_("Could not write to '%s'"), filename); + close(fd); +} + +static void read_merge_msg(struct strbuf *msg) +{ + const char *filename = git_path("MERGE_MSG"); + strbuf_reset(msg); + if (strbuf_read_file(msg, filename, 0) < 0) + die_errno(_("Could not read from '%s'"), filename); +} + +static void write_merge_state(struct commit_list *); +static void abort_commit(struct commit_list *remoteheads, const char *err_msg) +{ + if (err_msg) + error("%s", err_msg); + fprintf(stderr, + _("Not committing merge; use 'git commit' to complete the merge.\n")); + write_merge_state(remoteheads); + exit(1); +} + +static const char merge_editor_comment[] = +N_("Please enter a commit message to explain why this merge is necessary,\n" + "especially if it merges an updated upstream into a topic branch.\n" + "\n" + "Lines starting with '#' will be ignored, and an empty message aborts\n" + "the commit.\n"); + +static void prepare_to_commit(struct commit_list *remoteheads) +{ + struct strbuf msg = STRBUF_INIT; + const char *comment = _(merge_editor_comment); + strbuf_addbuf(&msg, &merge_msg); + strbuf_addch(&msg, '\n'); + if (0 < option_edit) + strbuf_add_lines(&msg, "# ", comment, strlen(comment)); + write_merge_msg(&msg); + if (run_hook(get_index_file(), "prepare-commit-msg", + git_path("MERGE_MSG"), "merge", NULL, NULL)) + abort_commit(remoteheads, NULL); + if (0 < option_edit) { + if (launch_editor(git_path("MERGE_MSG"), NULL, NULL)) + abort_commit(remoteheads, NULL); + } + read_merge_msg(&msg); + stripspace(&msg, 0 < option_edit); + if (!msg.len) + abort_commit(remoteheads, _("Empty commit message.")); + strbuf_release(&merge_msg); + strbuf_addbuf(&merge_msg, &msg); + strbuf_release(&msg); +} + +static int merge_trivial(struct commit *head, struct commit_list *remoteheads) +{ + unsigned char result_tree[20], result_commit[20]; + struct commit_list *parent = xmalloc(sizeof(*parent)); + + write_tree_trivial(result_tree); + printf(_("Wonderful.\n")); + parent->item = head; + parent->next = xmalloc(sizeof(*parent->next)); + parent->next->item = remoteheads->item; + parent->next->next = NULL; + prepare_to_commit(remoteheads); + if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL, + sign_commit)) + die(_("failed to write commit object")); + finish(head, remoteheads, result_commit, "In-index merge"); + drop_save(); + return 0; +} + +static int finish_automerge(struct commit *head, + int head_subsumed, + struct commit_list *common, + struct commit_list *remoteheads, + unsigned char *result_tree, + const char *wt_strategy) +{ + struct commit_list *parents = NULL; + struct strbuf buf = STRBUF_INIT; + unsigned char result_commit[20]; + + free_commit_list(common); + parents = remoteheads; + if (!head_subsumed || !allow_fast_forward) + commit_list_insert(head, &parents); + strbuf_addch(&merge_msg, '\n'); + prepare_to_commit(remoteheads); + if (commit_tree(&merge_msg, result_tree, parents, result_commit, + NULL, sign_commit)) + die(_("failed to write commit object")); + strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy); + finish(head, remoteheads, result_commit, buf.buf); + strbuf_release(&buf); + drop_save(); + return 0; +} + +static int suggest_conflicts(int renormalizing) +{ + const char *filename; + FILE *fp; + int pos; + + filename = git_path("MERGE_MSG"); + fp = fopen(filename, "a"); + if (!fp) + die_errno(_("Could not open '%s' for writing"), filename); + fprintf(fp, "\nConflicts:\n"); + for (pos = 0; pos < active_nr; pos++) { + struct cache_entry *ce = active_cache[pos]; + + if (ce_stage(ce)) { + fprintf(fp, "\t%s\n", ce->name); + while (pos + 1 < active_nr && + !strcmp(ce->name, + active_cache[pos + 1]->name)) + pos++; + } + } + fclose(fp); + rerere(allow_rerere_auto); + printf(_("Automatic merge failed; " + "fix conflicts and then commit the result.\n")); + return 1; +} + +static struct commit *is_old_style_invocation(int argc, const char **argv, + const unsigned char *head) +{ + struct commit *second_token = NULL; + if (argc > 2) { + unsigned char second_sha1[20]; + + if (get_sha1(argv[1], second_sha1)) + return NULL; + second_token = lookup_commit_reference_gently(second_sha1, 0); + if (!second_token) + die(_("'%s' is not a commit"), argv[1]); + if (hashcmp(second_token->object.sha1, head)) + return NULL; + } + return second_token; +} + +static int evaluate_result(void) +{ + int cnt = 0; + struct rev_info rev; + + /* Check how many files differ. */ + init_revisions(&rev, ""); + setup_revisions(0, NULL, &rev, NULL); + rev.diffopt.output_format |= + DIFF_FORMAT_CALLBACK; + rev.diffopt.format_callback = count_diff_files; + rev.diffopt.format_callback_data = &cnt; + run_diff_files(&rev, 0); + + /* + * Check how many unmerged entries are + * there. + */ + cnt += count_unmerged_entries(); + + return cnt; +} + +/* + * Pretend as if the user told us to merge with the tracking + * branch we have for the upstream of the current branch + */ +static int setup_with_upstream(const char ***argv) +{ + struct branch *branch = branch_get(NULL); + int i; + const char **args; + + if (!branch) + die(_("No current branch.")); + if (!branch->remote) + die(_("No remote for the current branch.")); + if (!branch->merge_nr) + die(_("No default upstream defined for the current branch.")); + + args = xcalloc(branch->merge_nr + 1, sizeof(char *)); + for (i = 0; i < branch->merge_nr; i++) { + if (!branch->merge[i]->dst) + die(_("No remote tracking branch for %s from %s"), + branch->merge[i]->src, branch->remote_name); + args[i] = branch->merge[i]->dst; + } + args[i] = NULL; + *argv = args; + return i; +} + +static void write_merge_state(struct commit_list *remoteheads) +{ + const char *filename; + int fd; + struct commit_list *j; + struct strbuf buf = STRBUF_INIT; + + for (j = remoteheads; j; j = j->next) { + unsigned const char *sha1; + struct commit *c = j->item; + if (c->util && merge_remote_util(c)->obj) { + sha1 = merge_remote_util(c)->obj->sha1; + } else { + sha1 = c->object.sha1; + } + strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1)); + } + filename = git_path("MERGE_HEAD"); + fd = open(filename, O_WRONLY | O_CREAT, 0666); + if (fd < 0) + die_errno(_("Could not open '%s' for writing"), filename); + if (write_in_full(fd, buf.buf, buf.len) != buf.len) + die_errno(_("Could not write to '%s'"), filename); + close(fd); + strbuf_addch(&merge_msg, '\n'); + write_merge_msg(&merge_msg); + + filename = git_path("MERGE_MODE"); + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (fd < 0) + die_errno(_("Could not open '%s' for writing"), filename); + strbuf_reset(&buf); + if (!allow_fast_forward) + strbuf_addf(&buf, "no-ff"); + if (write_in_full(fd, buf.buf, buf.len) != buf.len) + die_errno(_("Could not write to '%s'"), filename); + close(fd); +} + +static int default_edit_option(void) +{ + static const char name[] = "GIT_MERGE_AUTOEDIT"; + const char *e = getenv(name); + struct stat st_stdin, st_stdout; + + if (have_message) + /* an explicit -m msg without --[no-]edit */ + return 0; + + if (e) { + int v = git_config_maybe_bool(name, e); + if (v < 0) + die("Bad value '%s' in environment '%s'", e, name); + return v; + } + + /* Use editor if stdin and stdout are the same and is a tty */ + return (!fstat(0, &st_stdin) && + !fstat(1, &st_stdout) && + isatty(0) && isatty(1) && + st_stdin.st_dev == st_stdout.st_dev && + st_stdin.st_ino == st_stdout.st_ino && + st_stdin.st_mode == st_stdout.st_mode); +} + +static struct commit_list *collect_parents(struct commit *head_commit, + int *head_subsumed, + int argc, const char **argv) +{ + int i; + struct commit_list *remoteheads = NULL, *parents, *next; + struct commit_list **remotes = &remoteheads; + + if (head_commit) + remotes = &commit_list_insert(head_commit, remotes)->next; + for (i = 0; i < argc; i++) { + struct commit *commit = get_merge_parent(argv[i]); + if (!commit) + die(_("%s - not something we can merge"), argv[i]); + remotes = &commit_list_insert(commit, remotes)->next; + } + *remotes = NULL; + + parents = reduce_heads(remoteheads); + + *head_subsumed = 1; /* we will flip this to 0 when we find it */ + for (remoteheads = NULL, remotes = &remoteheads; + parents; + parents = next) { + struct commit *commit = parents->item; + next = parents->next; + if (commit == head_commit) + *head_subsumed = 0; + else + remotes = &commit_list_insert(commit, remotes)->next; + } + return remoteheads; +} + +int cmd_merge(int argc, const char **argv, const char *prefix) +{ + unsigned char result_tree[20]; + unsigned char stash[20]; + unsigned char head_sha1[20]; + struct commit *head_commit; + struct strbuf buf = STRBUF_INIT; + const char *head_arg; + int flag, i, ret = 0, head_subsumed; + int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; + struct commit_list *common = NULL; + const char *best_strategy = NULL, *wt_strategy = NULL; + struct commit_list *remoteheads, *p; + void *branch_to_free; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(builtin_merge_usage, builtin_merge_options); + + /* + * Check if we are _not_ on a detached HEAD, i.e. if there is a + * current branch. + */ + branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag); + if (branch && !prefixcmp(branch, "refs/heads/")) + branch += 11; + if (!branch || is_null_sha1(head_sha1)) + head_commit = NULL; + else + head_commit = lookup_commit_or_die(head_sha1, "HEAD"); + + git_config(git_merge_config, NULL); + + if (branch_mergeoptions) + parse_branch_merge_options(branch_mergeoptions); + argc = parse_options(argc, argv, prefix, builtin_merge_options, + builtin_merge_usage, 0); + if (shortlog_len < 0) + shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; + + if (verbosity < 0 && show_progress == -1) + show_progress = 0; + + if (abort_current_merge) { + int nargc = 2; + const char *nargv[] = {"reset", "--merge", NULL}; + + if (!file_exists(git_path("MERGE_HEAD"))) + die(_("There is no merge to abort (MERGE_HEAD missing).")); + + /* Invoke 'git reset --merge' */ + ret = cmd_reset(nargc, nargv, prefix); + goto done; + } + + if (read_cache_unmerged()) + die_resolve_conflict("merge"); + + if (file_exists(git_path("MERGE_HEAD"))) { + /* + * There is no unmerged entry, don't advise 'git + * add/rm <file>', just 'git commit'. + */ + if (advice_resolve_conflict) + die(_("You have not concluded your merge (MERGE_HEAD exists).\n" + "Please, commit your changes before you can merge.")); + else + die(_("You have not concluded your merge (MERGE_HEAD exists).")); + } + if (file_exists(git_path("CHERRY_PICK_HEAD"))) { + if (advice_resolve_conflict) + die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" + "Please, commit your changes before you can merge.")); + else + die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).")); + } + resolve_undo_clear(); + + if (verbosity < 0) + show_diffstat = 0; + + if (squash) { + if (!allow_fast_forward) + die(_("You cannot combine --squash with --no-ff.")); + option_commit = 0; + } + + if (!allow_fast_forward && fast_forward_only) + die(_("You cannot combine --no-ff with --ff-only.")); + + if (!abort_current_merge) { + if (!argc) { + if (default_to_upstream) + argc = setup_with_upstream(&argv); + else + die(_("No commit specified and merge.defaultToUpstream not set.")); + } else if (argc == 1 && !strcmp(argv[0], "-")) + argv[0] = "@{-1}"; + } + if (!argc) + usage_with_options(builtin_merge_usage, + builtin_merge_options); + + /* + * This could be traditional "merge <msg> HEAD <commit>..." and + * the way we can tell it is to see if the second token is HEAD, + * but some people might have misused the interface and used a + * committish that is the same as HEAD there instead. + * Traditional format never would have "-m" so it is an + * additional safety measure to check for it. + */ + + if (!have_message && head_commit && + is_old_style_invocation(argc, argv, head_commit->object.sha1)) { + strbuf_addstr(&merge_msg, argv[0]); + head_arg = argv[1]; + argv += 2; + argc -= 2; + remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv); + } else if (!head_commit) { + struct commit *remote_head; + /* + * If the merged head is a valid one there is no reason + * to forbid "git merge" into a branch yet to be born. + * We do the same for "git pull". + */ + if (argc != 1) + die(_("Can merge only exactly one commit into " + "empty head")); + if (squash) + die(_("Squash commit into empty head not supported yet")); + if (!allow_fast_forward) + die(_("Non-fast-forward commit does not make sense into " + "an empty head")); + remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv); + remote_head = remoteheads->item; + if (!remote_head) + die(_("%s - not something we can merge"), argv[0]); + read_empty(remote_head->object.sha1, 0); + update_ref("initial pull", "HEAD", remote_head->object.sha1, + NULL, 0, DIE_ON_ERR); + goto done; + } else { + struct strbuf merge_names = STRBUF_INIT; + + /* We are invoked directly as the first-class UI. */ + head_arg = "HEAD"; + + /* + * All the rest are the commits being merged; prepare + * the standard merge summary message to be appended + * to the given message. + */ + remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv); + for (p = remoteheads; p; p = p->next) + merge_name(merge_remote_util(p->item)->name, &merge_names); + + if (!have_message || shortlog_len) { + struct fmt_merge_msg_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.add_title = !have_message; + opts.shortlog_len = shortlog_len; + opts.credit_people = (0 < option_edit); + + fmt_merge_msg(&merge_names, &merge_msg, &opts); + if (merge_msg.len) + strbuf_setlen(&merge_msg, merge_msg.len - 1); + } + } + + if (!head_commit || !argc) + usage_with_options(builtin_merge_usage, + builtin_merge_options); + + strbuf_addstr(&buf, "merge"); + for (p = remoteheads; p; p = p->next) + strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name); + setenv("GIT_REFLOG_ACTION", buf.buf, 0); + strbuf_reset(&buf); + + for (p = remoteheads; p; p = p->next) { + struct commit *commit = p->item; + strbuf_addf(&buf, "GITHEAD_%s", + sha1_to_hex(commit->object.sha1)); + setenv(buf.buf, merge_remote_util(commit)->name, 1); + strbuf_reset(&buf); + if (!fast_forward_only && + merge_remote_util(commit) && + merge_remote_util(commit)->obj && + merge_remote_util(commit)->obj->type == OBJ_TAG) + allow_fast_forward = 0; + } + + if (option_edit < 0) + option_edit = default_edit_option(); + + if (!use_strategies) { + if (!remoteheads) + ; /* already up-to-date */ + else if (!remoteheads->next) + add_strategies(pull_twohead, DEFAULT_TWOHEAD); + else + add_strategies(pull_octopus, DEFAULT_OCTOPUS); + } + + for (i = 0; i < use_strategies_nr; i++) { + if (use_strategies[i]->attr & NO_FAST_FORWARD) + allow_fast_forward = 0; + if (use_strategies[i]->attr & NO_TRIVIAL) + allow_trivial = 0; + } + + if (!remoteheads) + ; /* already up-to-date */ + else if (!remoteheads->next) + common = get_merge_bases(head_commit, remoteheads->item, 1); + else { + struct commit_list *list = remoteheads; + commit_list_insert(head_commit, &list); + common = get_octopus_merge_bases(list); + free(list); + } + + update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1, + NULL, 0, DIE_ON_ERR); + + if (remoteheads && !common) + ; /* No common ancestors found. We need a real merge. */ + else if (!remoteheads || + (!remoteheads->next && !common->next && + common->item == remoteheads->item)) { + /* + * If head can reach all the merge then we are up to date. + * but first the most common case of merging one remote. + */ + finish_up_to_date("Already up-to-date."); + goto done; + } else if (allow_fast_forward && !remoteheads->next && + !common->next && + !hashcmp(common->item->object.sha1, head_commit->object.sha1)) { + /* Again the most common case of merging one remote. */ + struct strbuf msg = STRBUF_INIT; + struct commit *commit; + char hex[41]; + + strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV)); + + if (verbosity >= 0) + printf(_("Updating %s..%s\n"), + hex, + find_unique_abbrev(remoteheads->item->object.sha1, + DEFAULT_ABBREV)); + strbuf_addstr(&msg, "Fast-forward"); + if (have_message) + strbuf_addstr(&msg, + " (no commit created; -m option ignored)"); + commit = remoteheads->item; + if (!commit) { + ret = 1; + goto done; + } + + if (checkout_fast_forward(head_commit->object.sha1, + commit->object.sha1, + overwrite_ignore)) { + ret = 1; + goto done; + } + + finish(head_commit, remoteheads, commit->object.sha1, msg.buf); + drop_save(); + goto done; + } else if (!remoteheads->next && common->next) + ; + /* + * We are not doing octopus and not fast-forward. Need + * a real merge. + */ + else if (!remoteheads->next && !common->next && option_commit) { + /* + * We are not doing octopus, not fast-forward, and have + * only one common. + */ + refresh_cache(REFRESH_QUIET); + if (allow_trivial && !fast_forward_only) { + /* See if it is really trivial. */ + git_committer_info(IDENT_STRICT); + printf(_("Trying really trivial in-index merge...\n")); + if (!read_tree_trivial(common->item->object.sha1, + head_commit->object.sha1, + remoteheads->item->object.sha1)) { + ret = merge_trivial(head_commit, remoteheads); + goto done; + } + printf(_("Nope.\n")); + } + } else { + /* + * An octopus. If we can reach all the remote we are up + * to date. + */ + int up_to_date = 1; + struct commit_list *j; + + for (j = remoteheads; j; j = j->next) { + struct commit_list *common_one; + + /* + * Here we *have* to calculate the individual + * merge_bases again, otherwise "git merge HEAD^ + * HEAD^^" would be missed. + */ + common_one = get_merge_bases(head_commit, j->item, 1); + if (hashcmp(common_one->item->object.sha1, + j->item->object.sha1)) { + up_to_date = 0; + break; + } + } + if (up_to_date) { + finish_up_to_date("Already up-to-date. Yeeah!"); + goto done; + } + } + + if (fast_forward_only) + die(_("Not possible to fast-forward, aborting.")); + + /* We are going to make a new commit. */ + git_committer_info(IDENT_STRICT); + + /* + * At this point, we need a real merge. No matter what strategy + * we use, it would operate on the index, possibly affecting the + * working tree, and when resolved cleanly, have the desired + * tree in the index -- this means that the index must be in + * sync with the head commit. The strategies are responsible + * to ensure this. + */ + if (use_strategies_nr == 1 || + /* + * Stash away the local changes so that we can try more than one. + */ + save_state(stash)) + hashcpy(stash, null_sha1); + + for (i = 0; i < use_strategies_nr; i++) { + int ret; + if (i) { + printf(_("Rewinding the tree to pristine...\n")); + restore_state(head_commit->object.sha1, stash); + } + if (use_strategies_nr != 1) + printf(_("Trying merge strategy %s...\n"), + use_strategies[i]->name); + /* + * Remember which strategy left the state in the working + * tree. + */ + wt_strategy = use_strategies[i]->name; + + ret = try_merge_strategy(use_strategies[i]->name, + common, remoteheads, + head_commit, head_arg); + if (!option_commit && !ret) { + merge_was_ok = 1; + /* + * This is necessary here just to avoid writing + * the tree, but later we will *not* exit with + * status code 1 because merge_was_ok is set. + */ + ret = 1; + } + + if (ret) { + /* + * The backend exits with 1 when conflicts are + * left to be resolved, with 2 when it does not + * handle the given merge at all. + */ + if (ret == 1) { + int cnt = evaluate_result(); + + if (best_cnt <= 0 || cnt <= best_cnt) { + best_strategy = use_strategies[i]->name; + best_cnt = cnt; + } + } + if (merge_was_ok) + break; + else + continue; + } + + /* Automerge succeeded. */ + write_tree_trivial(result_tree); + automerge_was_ok = 1; + break; + } + + /* + * If we have a resulting tree, that means the strategy module + * auto resolved the merge cleanly. + */ + if (automerge_was_ok) { + ret = finish_automerge(head_commit, head_subsumed, + common, remoteheads, + result_tree, wt_strategy); + goto done; + } + + /* + * Pick the result from the best strategy and have the user fix + * it up. + */ + if (!best_strategy) { + restore_state(head_commit->object.sha1, stash); + if (use_strategies_nr > 1) + fprintf(stderr, + _("No merge strategy handled the merge.\n")); + else + fprintf(stderr, _("Merge with strategy %s failed.\n"), + use_strategies[0]->name); + ret = 2; + goto done; + } else if (best_strategy == wt_strategy) + ; /* We already have its result in the working tree. */ + else { + printf(_("Rewinding the tree to pristine...\n")); + restore_state(head_commit->object.sha1, stash); + printf(_("Using the %s to prepare resolving by hand.\n"), + best_strategy); + try_merge_strategy(best_strategy, common, remoteheads, + head_commit, head_arg); + } + + if (squash) + finish(head_commit, remoteheads, NULL, NULL); + else + write_merge_state(remoteheads); + + if (merge_was_ok) + fprintf(stderr, _("Automatic merge went well; " + "stopped before committing as requested\n")); + else + ret = suggest_conflicts(option_renormalize); + +done: + free(branch_to_free); + return ret; +} diff --git a/builtin/mktag.c b/builtin/mktag.c new file mode 100644 index 0000000000..640ab64f41 --- /dev/null +++ b/builtin/mktag.c @@ -0,0 +1,174 @@ +#include "builtin.h" +#include "tag.h" + +/* + * A signature file has a very simple fixed format: four lines + * of "object <sha1>" + "type <typename>" + "tag <tagname>" + + * "tagger <committer>", followed by a blank line, a free-form tag + * message and a signature block that git itself doesn't care about, + * but that can be verified with gpg or similar. + * + * The first four lines are guaranteed to be at least 83 bytes: + * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the + * shortest possible type-line, "tag .\n" at 6 bytes is the shortest + * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is + * the shortest possible tagger-line. + */ + +/* + * We refuse to tag something we can't verify. Just because. + */ +static int verify_object(const unsigned char *sha1, const char *expected_type) +{ + int ret = -1; + enum object_type type; + unsigned long size; + void *buffer = read_sha1_file(sha1, &type, &size); + const unsigned char *repl = lookup_replace_object(sha1); + + if (buffer) { + if (type == type_from_string(expected_type)) + ret = check_sha1_signature(repl, buffer, size, expected_type); + free(buffer); + } + return ret; +} + +static int verify_tag(char *buffer, unsigned long size) +{ + int typelen; + char type[20]; + unsigned char sha1[20]; + const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb; + size_t len; + + if (size < 84) + return error("wanna fool me ? you obviously got the size wrong !"); + + buffer[size] = 0; + + /* Verify object line */ + object = buffer; + if (memcmp(object, "object ", 7)) + return error("char%d: does not start with \"object \"", 0); + + if (get_sha1_hex(object + 7, sha1)) + return error("char%d: could not get SHA1 hash", 7); + + /* Verify type line */ + type_line = object + 48; + if (memcmp(type_line - 1, "\ntype ", 6)) + return error("char%d: could not find \"\\ntype \"", 47); + + /* Verify tag-line */ + tag_line = strchr(type_line, '\n'); + if (!tag_line) + return error("char%"PRIuMAX": could not find next \"\\n\"", + (uintmax_t) (type_line - buffer)); + tag_line++; + if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n') + return error("char%"PRIuMAX": no \"tag \" found", + (uintmax_t) (tag_line - buffer)); + + /* Get the actual type */ + typelen = tag_line - type_line - strlen("type \n"); + if (typelen >= sizeof(type)) + return error("char%"PRIuMAX": type too long", + (uintmax_t) (type_line+5 - buffer)); + + memcpy(type, type_line+5, typelen); + type[typelen] = 0; + + /* Verify that the object matches */ + if (verify_object(sha1, type)) + return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1)); + + /* Verify the tag-name: we don't allow control characters or spaces in it */ + tag_line += 4; + for (;;) { + unsigned char c = *tag_line++; + if (c == '\n') + break; + if (c > ' ') + continue; + return error("char%"PRIuMAX": could not verify tag name", + (uintmax_t) (tag_line - buffer)); + } + + /* Verify the tagger line */ + tagger_line = tag_line; + + if (memcmp(tagger_line, "tagger ", 7)) + return error("char%"PRIuMAX": could not find \"tagger \"", + (uintmax_t) (tagger_line - buffer)); + + /* + * Check for correct form for name and email + * i.e. " <" followed by "> " on _this_ line + * No angle brackets within the name or email address fields. + * No spaces within the email address field. + */ + tagger_line += 7; + if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) || + strpbrk(tagger_line, "<>\n") != lb+1 || + strpbrk(lb+2, "><\n ") != rb) + return error("char%"PRIuMAX": malformed tagger field", + (uintmax_t) (tagger_line - buffer)); + + /* Check for author name, at least one character, space is acceptable */ + if (lb == tagger_line) + return error("char%"PRIuMAX": missing tagger name", + (uintmax_t) (tagger_line - buffer)); + + /* timestamp, 1 or more digits followed by space */ + tagger_line = rb + 2; + if (!(len = strspn(tagger_line, "0123456789"))) + return error("char%"PRIuMAX": missing tag timestamp", + (uintmax_t) (tagger_line - buffer)); + tagger_line += len; + if (*tagger_line != ' ') + return error("char%"PRIuMAX": malformed tag timestamp", + (uintmax_t) (tagger_line - buffer)); + tagger_line++; + + /* timezone, 5 digits [+-]hhmm, max. 1400 */ + if (!((tagger_line[0] == '+' || tagger_line[0] == '-') && + strspn(tagger_line+1, "0123456789") == 4 && + tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400)) + return error("char%"PRIuMAX": malformed tag timezone", + (uintmax_t) (tagger_line - buffer)); + tagger_line += 6; + + /* Verify the blank line separating the header from the body */ + if (*tagger_line != '\n') + return error("char%"PRIuMAX": trailing garbage in tag header", + (uintmax_t) (tagger_line - buffer)); + + /* The actual stuff afterwards we don't care about.. */ + return 0; +} + +int cmd_mktag(int argc, const char **argv, const char *prefix) +{ + struct strbuf buf = STRBUF_INIT; + unsigned char result_sha1[20]; + + if (argc != 1) + usage("git mktag < signaturefile"); + + if (strbuf_read(&buf, 0, 4096) < 0) { + die_errno("could not read from stdin"); + } + + /* Verify it for some basic sanity: it needs to start with + "object <sha1>\ntype\ntagger " */ + if (verify_tag(buf.buf, buf.len) < 0) + die("invalid tag signature file"); + + if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0) + die("unable to write tag file"); + + strbuf_release(&buf); + printf("%s\n", sha1_to_hex(result_sha1)); + return 0; +} diff --git a/builtin/mktree.c b/builtin/mktree.c new file mode 100644 index 0000000000..f92ba404ab --- /dev/null +++ b/builtin/mktree.c @@ -0,0 +1,191 @@ +/* + * GIT - the stupid content tracker + * + * Copyright (c) Junio C Hamano, 2006, 2009 + */ +#include "builtin.h" +#include "quote.h" +#include "tree.h" +#include "parse-options.h" + +static struct treeent { + unsigned mode; + unsigned char sha1[20]; + int len; + char name[FLEX_ARRAY]; +} **entries; +static int alloc, used; + +static void append_to_tree(unsigned mode, unsigned char *sha1, char *path) +{ + struct treeent *ent; + int len = strlen(path); + if (strchr(path, '/')) + die("path %s contains slash", path); + + if (alloc <= used) { + alloc = alloc_nr(used); + entries = xrealloc(entries, sizeof(*entries) * alloc); + } + ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1); + ent->mode = mode; + ent->len = len; + hashcpy(ent->sha1, sha1); + memcpy(ent->name, path, len+1); +} + +static int ent_compare(const void *a_, const void *b_) +{ + struct treeent *a = *(struct treeent **)a_; + struct treeent *b = *(struct treeent **)b_; + return base_name_compare(a->name, a->len, a->mode, + b->name, b->len, b->mode); +} + +static void write_tree(unsigned char *sha1) +{ + struct strbuf buf; + size_t size; + int i; + + qsort(entries, used, sizeof(*entries), ent_compare); + for (size = i = 0; i < used; i++) + size += 32 + entries[i]->len; + + strbuf_init(&buf, size); + for (i = 0; i < used; i++) { + struct treeent *ent = entries[i]; + strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); + strbuf_add(&buf, ent->sha1, 20); + } + + write_sha1_file(buf.buf, buf.len, tree_type, sha1); + strbuf_release(&buf); +} + +static const char *mktree_usage[] = { + N_("git mktree [-z] [--missing] [--batch]"), + NULL +}; + +static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing) +{ + char *ptr, *ntr; + unsigned mode; + enum object_type mode_type; /* object type derived from mode */ + enum object_type obj_type; /* object type derived from sha */ + char *path; + unsigned char sha1[20]; + + ptr = buf; + /* + * Read non-recursive ls-tree output format: + * mode SP type SP sha1 TAB name + */ + mode = strtoul(ptr, &ntr, 8); + if (ptr == ntr || !ntr || *ntr != ' ') + die("input format error: %s", buf); + ptr = ntr + 1; /* type */ + ntr = strchr(ptr, ' '); + if (!ntr || buf + len <= ntr + 40 || + ntr[41] != '\t' || + get_sha1_hex(ntr + 1, sha1)) + die("input format error: %s", buf); + + /* It is perfectly normal if we do not have a commit from a submodule */ + if (S_ISGITLINK(mode)) + allow_missing = 1; + + + *ntr++ = 0; /* now at the beginning of SHA1 */ + + path = ntr + 41; /* at the beginning of name */ + if (line_termination && path[0] == '"') { + struct strbuf p_uq = STRBUF_INIT; + if (unquote_c_style(&p_uq, path, NULL)) + die("invalid quoting"); + path = strbuf_detach(&p_uq, NULL); + } + + /* + * Object type is redundantly derivable three ways. + * These should all agree. + */ + mode_type = object_type(mode); + if (mode_type != type_from_string(ptr)) { + die("entry '%s' object type (%s) doesn't match mode type (%s)", + path, ptr, typename(mode_type)); + } + + /* Check the type of object identified by sha1 */ + obj_type = sha1_object_info(sha1, NULL); + if (obj_type < 0) { + if (allow_missing) { + ; /* no problem - missing objects are presumed to be of the right type */ + } else { + die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1)); + } + } else { + if (obj_type != mode_type) { + /* + * The object exists but is of the wrong type. + * This is a problem regardless of allow_missing + * because the new tree entry will never be correct. + */ + die("entry '%s' object %s is a %s but specified type was (%s)", + path, sha1_to_hex(sha1), typename(obj_type), typename(mode_type)); + } + } + + append_to_tree(mode, sha1, path); +} + +int cmd_mktree(int ac, const char **av, const char *prefix) +{ + struct strbuf sb = STRBUF_INIT; + unsigned char sha1[20]; + int line_termination = '\n'; + int allow_missing = 0; + int is_batch_mode = 0; + int got_eof = 0; + + const struct option option[] = { + OPT_SET_INT('z', NULL, &line_termination, N_("input is NUL terminated"), '\0'), + OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1), + OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1), + OPT_END() + }; + + ac = parse_options(ac, av, prefix, option, mktree_usage, 0); + + while (!got_eof) { + while (1) { + if (strbuf_getline(&sb, stdin, line_termination) == EOF) { + got_eof = 1; + break; + } + if (sb.buf[0] == '\0') { + /* empty lines denote tree boundaries in batch mode */ + if (is_batch_mode) + break; + die("input format error: (blank line only valid in batch mode)"); + } + mktree_line(sb.buf, sb.len, line_termination, allow_missing); + } + if (is_batch_mode && got_eof && used < 1) { + /* + * Execution gets here if the last tree entry is terminated with a + * new-line. The final new-line has been made optional to be + * consistent with the original non-batch behaviour of mktree. + */ + ; /* skip creating an empty tree */ + } else { + write_tree(sha1); + puts(sha1_to_hex(sha1)); + fflush(stdout); + } + used=0; /* reset tree entry buffer for re-use in batch mode */ + } + strbuf_release(&sb); + exit(0); +} diff --git a/builtin/mv.c b/builtin/mv.c new file mode 100644 index 0000000000..034fec92a1 --- /dev/null +++ b/builtin/mv.c @@ -0,0 +1,233 @@ +/* + * "git mv" builtin command + * + * Copyright (C) 2006 Johannes Schindelin + */ +#include "cache.h" +#include "builtin.h" +#include "dir.h" +#include "cache-tree.h" +#include "string-list.h" +#include "parse-options.h" + +static const char * const builtin_mv_usage[] = { + N_("git mv [options] <source>... <destination>"), + NULL +}; + +static const char **copy_pathspec(const char *prefix, const char **pathspec, + int count, int base_name) +{ + int i; + const char **result = xmalloc((count + 1) * sizeof(const char *)); + memcpy(result, pathspec, count * sizeof(const char *)); + result[count] = NULL; + for (i = 0; i < count; i++) { + int length = strlen(result[i]); + int to_copy = length; + while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1])) + to_copy--; + if (to_copy != length || base_name) { + char *it = xmemdupz(result[i], to_copy); + if (base_name) { + result[i] = xstrdup(basename(it)); + free(it); + } else + result[i] = it; + } + } + return get_pathspec(prefix, result); +} + +static const char *add_slash(const char *path) +{ + int len = strlen(path); + if (path[len - 1] != '/') { + char *with_slash = xmalloc(len + 2); + memcpy(with_slash, path, len); + with_slash[len++] = '/'; + with_slash[len] = 0; + return with_slash; + } + return path; +} + +static struct lock_file lock_file; + +int cmd_mv(int argc, const char **argv, const char *prefix) +{ + int i, newfd; + int verbose = 0, show_only = 0, force = 0, ignore_errors = 0; + struct option builtin_mv_options[] = { + OPT__VERBOSE(&verbose, N_("be verbose")), + OPT__DRY_RUN(&show_only, N_("dry run")), + OPT__FORCE(&force, N_("force move/rename even if target exists")), + OPT_BOOLEAN('k', NULL, &ignore_errors, N_("skip move/rename errors")), + OPT_END(), + }; + const char **source, **destination, **dest_path; + enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes; + struct stat st; + struct string_list src_for_dst = STRING_LIST_INIT_NODUP; + + git_config(git_default_config, NULL); + + argc = parse_options(argc, argv, prefix, builtin_mv_options, + builtin_mv_usage, 0); + if (--argc < 1) + usage_with_options(builtin_mv_usage, builtin_mv_options); + + newfd = hold_locked_index(&lock_file, 1); + if (read_cache() < 0) + die(_("index file corrupt")); + + source = copy_pathspec(prefix, argv, argc, 0); + modes = xcalloc(argc, sizeof(enum update_mode)); + dest_path = copy_pathspec(prefix, argv + argc, 1, 0); + + if (dest_path[0][0] == '\0') + /* special case: "." was normalized to "" */ + destination = copy_pathspec(dest_path[0], argv, argc, 1); + else if (!lstat(dest_path[0], &st) && + S_ISDIR(st.st_mode)) { + dest_path[0] = add_slash(dest_path[0]); + destination = copy_pathspec(dest_path[0], argv, argc, 1); + } else { + if (argc != 1) + die("destination '%s' is not a directory", dest_path[0]); + destination = dest_path; + } + + /* Checking */ + for (i = 0; i < argc; i++) { + const char *src = source[i], *dst = destination[i]; + int length, src_is_dir; + const char *bad = NULL; + + if (show_only) + printf(_("Checking rename of '%s' to '%s'\n"), src, dst); + + length = strlen(src); + if (lstat(src, &st) < 0) + bad = _("bad source"); + else if (!strncmp(src, dst, length) && + (dst[length] == 0 || dst[length] == '/')) { + bad = _("can not move directory into itself"); + } else if ((src_is_dir = S_ISDIR(st.st_mode)) + && lstat(dst, &st) == 0) + bad = _("cannot move directory over file"); + else if (src_is_dir) { + const char *src_w_slash = add_slash(src); + int len_w_slash = length + 1; + int first, last; + + modes[i] = WORKING_DIRECTORY; + + first = cache_name_pos(src_w_slash, len_w_slash); + if (first >= 0) + die (_("Huh? %.*s is in index?"), + len_w_slash, src_w_slash); + + first = -1 - first; + for (last = first; last < active_nr; last++) { + const char *path = active_cache[last]->name; + if (strncmp(path, src_w_slash, len_w_slash)) + break; + } + free((char *)src_w_slash); + + if (last - first < 1) + bad = _("source directory is empty"); + else { + int j, dst_len; + + if (last - first > 0) { + source = xrealloc(source, + (argc + last - first) + * sizeof(char *)); + destination = xrealloc(destination, + (argc + last - first) + * sizeof(char *)); + modes = xrealloc(modes, + (argc + last - first) + * sizeof(enum update_mode)); + } + + dst = add_slash(dst); + dst_len = strlen(dst); + + for (j = 0; j < last - first; j++) { + const char *path = + active_cache[first + j]->name; + source[argc + j] = path; + destination[argc + j] = + prefix_path(dst, dst_len, + path + length + 1); + modes[argc + j] = INDEX; + } + argc += last - first; + } + } else if (cache_name_pos(src, length) < 0) + bad = _("not under version control"); + else if (lstat(dst, &st) == 0) { + bad = _("destination exists"); + if (force) { + /* + * only files can overwrite each other: + * check both source and destination + */ + if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { + if (verbose) + warning(_("overwriting '%s'"), dst); + bad = NULL; + } else + bad = _("Cannot overwrite"); + } + } else if (string_list_has_string(&src_for_dst, dst)) + bad = _("multiple sources for the same target"); + else + string_list_insert(&src_for_dst, dst); + + if (bad) { + if (ignore_errors) { + if (--argc > 0) { + memmove(source + i, source + i + 1, + (argc - i) * sizeof(char *)); + memmove(destination + i, + destination + i + 1, + (argc - i) * sizeof(char *)); + i--; + } + } else + die (_("%s, source=%s, destination=%s"), + bad, src, dst); + } + } + + for (i = 0; i < argc; i++) { + const char *src = source[i], *dst = destination[i]; + enum update_mode mode = modes[i]; + int pos; + if (show_only || verbose) + printf(_("Renaming %s to %s\n"), src, dst); + if (!show_only && mode != INDEX && + rename(src, dst) < 0 && !ignore_errors) + die_errno (_("renaming '%s' failed"), src); + + if (mode == WORKING_DIRECTORY) + continue; + + pos = cache_name_pos(src, strlen(src)); + assert(pos >= 0); + if (!show_only) + rename_cache_entry_at(pos, dst); + } + + if (active_cache_changed) { + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(&lock_file)) + die(_("Unable to write new index file")); + } + + return 0; +} diff --git a/builtin/name-rev.c b/builtin/name-rev.c new file mode 100644 index 0000000000..6238247974 --- /dev/null +++ b/builtin/name-rev.c @@ -0,0 +1,307 @@ +#include "builtin.h" +#include "cache.h" +#include "commit.h" +#include "tag.h" +#include "refs.h" +#include "parse-options.h" + +#define CUTOFF_DATE_SLOP 86400 /* one day */ + +typedef struct rev_name { + const char *tip_name; + int generation; + int distance; +} rev_name; + +static long cutoff = LONG_MAX; + +/* How many generations are maximally preferred over _one_ merge traversal? */ +#define MERGE_TRAVERSAL_WEIGHT 65535 + +static void name_rev(struct commit *commit, + const char *tip_name, int generation, int distance, + int deref) +{ + struct rev_name *name = (struct rev_name *)commit->util; + struct commit_list *parents; + int parent_number = 1; + + if (!commit->object.parsed) + parse_commit(commit); + + if (commit->date < cutoff) + return; + + if (deref) { + char *new_name = xmalloc(strlen(tip_name)+3); + strcpy(new_name, tip_name); + strcat(new_name, "^0"); + tip_name = new_name; + + if (generation) + die("generation: %d, but deref?", generation); + } + + if (name == NULL) { + name = xmalloc(sizeof(rev_name)); + commit->util = name; + goto copy_data; + } else if (name->distance > distance) { +copy_data: + name->tip_name = tip_name; + name->generation = generation; + name->distance = distance; + } else + return; + + for (parents = commit->parents; + parents; + parents = parents->next, parent_number++) { + if (parent_number > 1) { + int len = strlen(tip_name); + char *new_name = xmalloc(len + + 1 + decimal_length(generation) + /* ~<n> */ + 1 + 2 + /* ^NN */ + 1); + + if (len > 2 && !strcmp(tip_name + len - 2, "^0")) + len -= 2; + if (generation > 0) + sprintf(new_name, "%.*s~%d^%d", len, tip_name, + generation, parent_number); + else + sprintf(new_name, "%.*s^%d", len, tip_name, + parent_number); + + name_rev(parents->item, new_name, 0, + distance + MERGE_TRAVERSAL_WEIGHT, 0); + } else { + name_rev(parents->item, tip_name, generation + 1, + distance + 1, 0); + } + } +} + +struct name_ref_data { + int tags_only; + int name_only; + const char *ref_filter; +}; + +static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data) +{ + struct object *o = parse_object(sha1); + struct name_ref_data *data = cb_data; + int deref = 0; + + if (data->tags_only && prefixcmp(path, "refs/tags/")) + return 0; + + if (data->ref_filter && fnmatch(data->ref_filter, path, 0)) + return 0; + + while (o && o->type == OBJ_TAG) { + struct tag *t = (struct tag *) o; + if (!t->tagged) + break; /* broken repository */ + o = parse_object(t->tagged->sha1); + deref = 1; + } + if (o && o->type == OBJ_COMMIT) { + struct commit *commit = (struct commit *)o; + + if (!prefixcmp(path, "refs/heads/")) + path = path + 11; + else if (data->tags_only + && data->name_only + && !prefixcmp(path, "refs/tags/")) + path = path + 10; + else if (!prefixcmp(path, "refs/")) + path = path + 5; + + name_rev(commit, xstrdup(path), 0, 0, deref); + } + return 0; +} + +/* returns a static buffer */ +static const char *get_rev_name(const struct object *o) +{ + static char buffer[1024]; + struct rev_name *n; + struct commit *c; + + if (o->type != OBJ_COMMIT) + return NULL; + c = (struct commit *) o; + n = c->util; + if (!n) + return NULL; + + if (!n->generation) + return n->tip_name; + else { + int len = strlen(n->tip_name); + if (len > 2 && !strcmp(n->tip_name + len - 2, "^0")) + len -= 2; + snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name, + n->generation); + + return buffer; + } +} + +static void show_name(const struct object *obj, + const char *caller_name, + int always, int allow_undefined, int name_only) +{ + const char *name; + const unsigned char *sha1 = obj->sha1; + + if (!name_only) + printf("%s ", caller_name ? caller_name : sha1_to_hex(sha1)); + name = get_rev_name(obj); + if (name) + printf("%s\n", name); + else if (allow_undefined) + printf("undefined\n"); + else if (always) + printf("%s\n", find_unique_abbrev(sha1, DEFAULT_ABBREV)); + else + die("cannot describe '%s'", sha1_to_hex(sha1)); +} + +static char const * const name_rev_usage[] = { + N_("git name-rev [options] <commit>..."), + N_("git name-rev [options] --all"), + N_("git name-rev [options] --stdin"), + NULL +}; + +static void name_rev_line(char *p, struct name_ref_data *data) +{ + int forty = 0; + char *p_start; + for (p_start = p; *p; p++) { +#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) + if (!ishex(*p)) + forty = 0; + else if (++forty == 40 && + !ishex(*(p+1))) { + unsigned char sha1[40]; + const char *name = NULL; + char c = *(p+1); + int p_len = p - p_start + 1; + + forty = 0; + + *(p+1) = 0; + if (!get_sha1(p - 39, sha1)) { + struct object *o = + lookup_object(sha1); + if (o) + name = get_rev_name(o); + } + *(p+1) = c; + + if (!name) + continue; + + if (data->name_only) + printf("%.*s%s", p_len - 40, p_start, name); + else + printf("%.*s (%s)", p_len, p_start, name); + p_start = p + 1; + } + } + + /* flush */ + if (p_start != p) + fwrite(p_start, p - p_start, 1, stdout); +} + +int cmd_name_rev(int argc, const char **argv, const char *prefix) +{ + struct object_array revs = OBJECT_ARRAY_INIT; + int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0; + struct name_ref_data data = { 0, 0, NULL }; + struct option opts[] = { + OPT_BOOLEAN(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")), + OPT_BOOLEAN(0, "tags", &data.tags_only, N_("only use tags to name the commits")), + OPT_STRING(0, "refs", &data.ref_filter, N_("pattern"), + N_("only use refs matching <pattern>")), + OPT_GROUP(""), + OPT_BOOLEAN(0, "all", &all, N_("list all commits reachable from all refs")), + OPT_BOOLEAN(0, "stdin", &transform_stdin, N_("read from stdin")), + OPT_BOOLEAN(0, "undefined", &allow_undefined, N_("allow to print `undefined` names")), + OPT_BOOLEAN(0, "always", &always, + N_("show abbreviated commit object as fallback")), + OPT_END(), + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0); + if (!!all + !!transform_stdin + !!argc > 1) { + error("Specify either a list, or --all, not both!"); + usage_with_options(name_rev_usage, opts); + } + if (all || transform_stdin) + cutoff = 0; + + for (; argc; argc--, argv++) { + unsigned char sha1[20]; + struct object *o; + struct commit *commit; + + if (get_sha1(*argv, sha1)) { + fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", + *argv); + continue; + } + + o = deref_tag(parse_object(sha1), *argv, 0); + if (!o || o->type != OBJ_COMMIT) { + fprintf(stderr, "Could not get commit for %s. Skipping.\n", + *argv); + continue; + } + + commit = (struct commit *)o; + if (cutoff > commit->date) + cutoff = commit->date; + add_object_array((struct object *)commit, *argv, &revs); + } + + if (cutoff) + cutoff = cutoff - CUTOFF_DATE_SLOP; + for_each_ref(name_ref, &data); + + if (transform_stdin) { + char buffer[2048]; + + while (!feof(stdin)) { + char *p = fgets(buffer, sizeof(buffer), stdin); + if (!p) + break; + name_rev_line(p, &data); + } + } else if (all) { + int i, max; + + max = get_max_object_index(); + for (i = 0; i < max; i++) { + struct object *obj = get_indexed_object(i); + if (!obj || obj->type != OBJ_COMMIT) + continue; + show_name(obj, NULL, + always, allow_undefined, data.name_only); + } + } else { + int i; + for (i = 0; i < revs.nr; i++) + show_name(revs.objects[i].item, revs.objects[i].name, + always, allow_undefined, data.name_only); + } + + return 0; +} diff --git a/builtin/notes.c b/builtin/notes.c new file mode 100644 index 0000000000..453457adb9 --- /dev/null +++ b/builtin/notes.c @@ -0,0 +1,1111 @@ +/* + * Builtin "git notes" + * + * Copyright (c) 2010 Johan Herland <johan@herland.net> + * + * Based on git-notes.sh by Johannes Schindelin, + * and builtin-tag.c by Kristian Høgsberg and Carlos Rica. + */ + +#include "cache.h" +#include "builtin.h" +#include "notes.h" +#include "blob.h" +#include "commit.h" +#include "refs.h" +#include "exec_cmd.h" +#include "run-command.h" +#include "parse-options.h" +#include "string-list.h" +#include "notes-merge.h" + +static void commit_notes(struct notes_tree *t, const char *msg); +static combine_notes_fn parse_combine_notes_fn(const char *v); + +static const char * const git_notes_usage[] = { + N_("git notes [--ref <notes_ref>] [list [<object>]]"), + N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"), + N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"), + N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"), + N_("git notes [--ref <notes_ref>] edit [<object>]"), + N_("git notes [--ref <notes_ref>] show [<object>]"), + N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"), + N_("git notes merge --commit [-v | -q]"), + N_("git notes merge --abort [-v | -q]"), + N_("git notes [--ref <notes_ref>] remove [<object>...]"), + N_("git notes [--ref <notes_ref>] prune [-n | -v]"), + N_("git notes [--ref <notes_ref>] get-ref"), + NULL +}; + +static const char * const git_notes_list_usage[] = { + N_("git notes [list [<object>]]"), + NULL +}; + +static const char * const git_notes_add_usage[] = { + N_("git notes add [<options>] [<object>]"), + NULL +}; + +static const char * const git_notes_copy_usage[] = { + N_("git notes copy [<options>] <from-object> <to-object>"), + N_("git notes copy --stdin [<from-object> <to-object>]..."), + NULL +}; + +static const char * const git_notes_append_usage[] = { + N_("git notes append [<options>] [<object>]"), + NULL +}; + +static const char * const git_notes_edit_usage[] = { + N_("git notes edit [<object>]"), + NULL +}; + +static const char * const git_notes_show_usage[] = { + N_("git notes show [<object>]"), + NULL +}; + +static const char * const git_notes_merge_usage[] = { + N_("git notes merge [<options>] <notes_ref>"), + N_("git notes merge --commit [<options>]"), + N_("git notes merge --abort [<options>]"), + NULL +}; + +static const char * const git_notes_remove_usage[] = { + N_("git notes remove [<object>]"), + NULL +}; + +static const char * const git_notes_prune_usage[] = { + N_("git notes prune [<options>]"), + NULL +}; + +static const char * const git_notes_get_ref_usage[] = { + N_("git notes get-ref"), + NULL +}; + +static const char note_template[] = + "\n" + "#\n" + "# Write/edit the notes for the following object:\n" + "#\n"; + +struct msg_arg { + int given; + int use_editor; + struct strbuf buf; +}; + +static int list_each_note(const unsigned char *object_sha1, + const unsigned char *note_sha1, char *note_path, + void *cb_data) +{ + printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1)); + return 0; +} + +static void write_note_data(int fd, const unsigned char *sha1) +{ + unsigned long size; + enum object_type type; + char *buf = read_sha1_file(sha1, &type, &size); + if (buf) { + if (size) + write_or_die(fd, buf, size); + free(buf); + } +} + +static void write_commented_object(int fd, const unsigned char *object) +{ + const char *show_args[5] = + {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL}; + struct child_process show; + struct strbuf buf = STRBUF_INIT; + FILE *show_out; + + /* Invoke "git show --stat --no-notes $object" */ + memset(&show, 0, sizeof(show)); + show.argv = show_args; + show.no_stdin = 1; + show.out = -1; + show.err = 0; + show.git_cmd = 1; + if (start_command(&show)) + die(_("unable to start 'show' for object '%s'"), + sha1_to_hex(object)); + + /* Open the output as FILE* so strbuf_getline() can be used. */ + show_out = xfdopen(show.out, "r"); + if (show_out == NULL) + die_errno(_("can't fdopen 'show' output fd")); + + /* Prepend "# " to each output line and write result to 'fd' */ + while (strbuf_getline(&buf, show_out, '\n') != EOF) { + write_or_die(fd, "# ", 2); + write_or_die(fd, buf.buf, buf.len); + write_or_die(fd, "\n", 1); + } + strbuf_release(&buf); + if (fclose(show_out)) + die_errno(_("failed to close pipe to 'show' for object '%s'"), + sha1_to_hex(object)); + if (finish_command(&show)) + die(_("failed to finish 'show' for object '%s'"), + sha1_to_hex(object)); +} + +static void create_note(const unsigned char *object, struct msg_arg *msg, + int append_only, const unsigned char *prev, + unsigned char *result) +{ + char *path = NULL; + + if (msg->use_editor || !msg->given) { + int fd; + + /* write the template message before editing: */ + path = git_pathdup("NOTES_EDITMSG"); + fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); + if (fd < 0) + die_errno(_("could not create file '%s'"), path); + + if (msg->given) + write_or_die(fd, msg->buf.buf, msg->buf.len); + else if (prev && !append_only) + write_note_data(fd, prev); + write_or_die(fd, note_template, strlen(note_template)); + + write_commented_object(fd, object); + + close(fd); + strbuf_reset(&(msg->buf)); + + if (launch_editor(path, &(msg->buf), NULL)) { + die(_("Please supply the note contents using either -m" \ + " or -F option")); + } + stripspace(&(msg->buf), 1); + } + + if (prev && append_only) { + /* Append buf to previous note contents */ + unsigned long size; + enum object_type type; + char *prev_buf = read_sha1_file(prev, &type, &size); + + strbuf_grow(&(msg->buf), size + 1); + if (msg->buf.len && prev_buf && size) + strbuf_insert(&(msg->buf), 0, "\n", 1); + if (prev_buf && size) + strbuf_insert(&(msg->buf), 0, prev_buf, size); + free(prev_buf); + } + + if (!msg->buf.len) { + fprintf(stderr, _("Removing note for object %s\n"), + sha1_to_hex(object)); + hashclr(result); + } else { + if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) { + error(_("unable to write note object")); + if (path) + error(_("The note contents has been left in %s"), + path); + exit(128); + } + } + + if (path) { + unlink_or_warn(path); + free(path); + } +} + +static int parse_msg_arg(const struct option *opt, const char *arg, int unset) +{ + struct msg_arg *msg = opt->value; + + strbuf_grow(&(msg->buf), strlen(arg) + 2); + if (msg->buf.len) + strbuf_addch(&(msg->buf), '\n'); + strbuf_addstr(&(msg->buf), arg); + stripspace(&(msg->buf), 0); + + msg->given = 1; + return 0; +} + +static int parse_file_arg(const struct option *opt, const char *arg, int unset) +{ + struct msg_arg *msg = opt->value; + + if (msg->buf.len) + strbuf_addch(&(msg->buf), '\n'); + if (!strcmp(arg, "-")) { + if (strbuf_read(&(msg->buf), 0, 1024) < 0) + die_errno(_("cannot read '%s'"), arg); + } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0) + die_errno(_("could not open or read '%s'"), arg); + stripspace(&(msg->buf), 0); + + msg->given = 1; + return 0; +} + +static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) +{ + struct msg_arg *msg = opt->value; + char *buf; + unsigned char object[20]; + enum object_type type; + unsigned long len; + + if (msg->buf.len) + strbuf_addch(&(msg->buf), '\n'); + + if (get_sha1(arg, object)) + die(_("Failed to resolve '%s' as a valid ref."), arg); + if (!(buf = read_sha1_file(object, &type, &len)) || !len) { + free(buf); + die(_("Failed to read object '%s'."), arg);; + } + strbuf_add(&(msg->buf), buf, len); + free(buf); + + msg->given = 1; + return 0; +} + +static int parse_reedit_arg(const struct option *opt, const char *arg, int unset) +{ + struct msg_arg *msg = opt->value; + msg->use_editor = 1; + return parse_reuse_arg(opt, arg, unset); +} + +static void commit_notes(struct notes_tree *t, const char *msg) +{ + struct strbuf buf = STRBUF_INIT; + unsigned char commit_sha1[20]; + + if (!t) + t = &default_notes_tree; + if (!t->initialized || !t->ref || !*t->ref) + die(_("Cannot commit uninitialized/unreferenced notes tree")); + if (!t->dirty) + return; /* don't have to commit an unchanged tree */ + + /* Prepare commit message and reflog message */ + strbuf_addstr(&buf, msg); + if (buf.buf[buf.len - 1] != '\n') + strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */ + + create_notes_commit(t, NULL, &buf, commit_sha1); + strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */ + update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR); + + strbuf_release(&buf); +} + +static combine_notes_fn parse_combine_notes_fn(const char *v) +{ + if (!strcasecmp(v, "overwrite")) + return combine_notes_overwrite; + else if (!strcasecmp(v, "ignore")) + return combine_notes_ignore; + else if (!strcasecmp(v, "concatenate")) + return combine_notes_concatenate; + else if (!strcasecmp(v, "cat_sort_uniq")) + return combine_notes_cat_sort_uniq; + else + return NULL; +} + +static int notes_rewrite_config(const char *k, const char *v, void *cb) +{ + struct notes_rewrite_cfg *c = cb; + if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) { + c->enabled = git_config_bool(k, v); + return 0; + } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) { + if (!v) + config_error_nonbool(k); + c->combine = parse_combine_notes_fn(v); + if (!c->combine) { + error(_("Bad notes.rewriteMode value: '%s'"), v); + return 1; + } + return 0; + } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) { + /* note that a refs/ prefix is implied in the + * underlying for_each_glob_ref */ + if (!prefixcmp(v, "refs/notes/")) + string_list_add_refs_by_glob(c->refs, v); + else + warning(_("Refusing to rewrite notes in %s" + " (outside of refs/notes/)"), v); + return 0; + } + + return 0; +} + + +struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd) +{ + struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg)); + const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT); + const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT); + c->cmd = cmd; + c->enabled = 1; + c->combine = combine_notes_concatenate; + c->refs = xcalloc(1, sizeof(struct string_list)); + c->refs->strdup_strings = 1; + c->refs_from_env = 0; + c->mode_from_env = 0; + if (rewrite_mode_env) { + c->mode_from_env = 1; + c->combine = parse_combine_notes_fn(rewrite_mode_env); + if (!c->combine) + /* TRANSLATORS: The first %s is the name of the + environment variable, the second %s is its value */ + error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT, + rewrite_mode_env); + } + if (rewrite_refs_env) { + c->refs_from_env = 1; + string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env); + } + git_config(notes_rewrite_config, c); + if (!c->enabled || !c->refs->nr) { + string_list_clear(c->refs, 0); + free(c->refs); + free(c); + return NULL; + } + c->trees = load_notes_trees(c->refs); + string_list_clear(c->refs, 0); + free(c->refs); + return c; +} + +int copy_note_for_rewrite(struct notes_rewrite_cfg *c, + const unsigned char *from_obj, const unsigned char *to_obj) +{ + int ret = 0; + int i; + for (i = 0; c->trees[i]; i++) + ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret; + return ret; +} + +void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c) +{ + int i; + for (i = 0; c->trees[i]; i++) { + commit_notes(c->trees[i], "Notes added by 'git notes copy'"); + free_notes(c->trees[i]); + } + free(c->trees); + free(c); +} + +static int notes_copy_from_stdin(int force, const char *rewrite_cmd) +{ + struct strbuf buf = STRBUF_INIT; + struct notes_rewrite_cfg *c = NULL; + struct notes_tree *t = NULL; + int ret = 0; + + if (rewrite_cmd) { + c = init_copy_notes_for_rewrite(rewrite_cmd); + if (!c) + return 0; + } else { + init_notes(NULL, NULL, NULL, 0); + t = &default_notes_tree; + } + + while (strbuf_getline(&buf, stdin, '\n') != EOF) { + unsigned char from_obj[20], to_obj[20]; + struct strbuf **split; + int err; + + split = strbuf_split(&buf, ' '); + if (!split[0] || !split[1]) + die(_("Malformed input line: '%s'."), buf.buf); + strbuf_rtrim(split[0]); + strbuf_rtrim(split[1]); + if (get_sha1(split[0]->buf, from_obj)) + die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf); + if (get_sha1(split[1]->buf, to_obj)) + die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf); + + if (rewrite_cmd) + err = copy_note_for_rewrite(c, from_obj, to_obj); + else + err = copy_note(t, from_obj, to_obj, force, + combine_notes_overwrite); + + if (err) { + error(_("Failed to copy notes from '%s' to '%s'"), + split[0]->buf, split[1]->buf); + ret = 1; + } + + strbuf_list_free(split); + } + + if (!rewrite_cmd) { + commit_notes(t, "Notes added by 'git notes copy'"); + free_notes(t); + } else { + finish_copy_notes_for_rewrite(c); + } + return ret; +} + +static struct notes_tree *init_notes_check(const char *subcommand) +{ + struct notes_tree *t; + init_notes(NULL, NULL, NULL, 0); + t = &default_notes_tree; + + if (prefixcmp(t->ref, "refs/notes/")) + die("Refusing to %s notes in %s (outside of refs/notes/)", + subcommand, t->ref); + return t; +} + +static int list(int argc, const char **argv, const char *prefix) +{ + struct notes_tree *t; + unsigned char object[20]; + const unsigned char *note; + int retval = -1; + struct option options[] = { + OPT_END() + }; + + if (argc) + argc = parse_options(argc, argv, prefix, options, + git_notes_list_usage, 0); + + if (1 < argc) { + error(_("too many parameters")); + usage_with_options(git_notes_list_usage, options); + } + + t = init_notes_check("list"); + if (argc) { + if (get_sha1(argv[0], object)) + die(_("Failed to resolve '%s' as a valid ref."), argv[0]); + note = get_note(t, object); + if (note) { + puts(sha1_to_hex(note)); + retval = 0; + } else + retval = error(_("No note found for object %s."), + sha1_to_hex(object)); + } else + retval = for_each_note(t, 0, list_each_note, NULL); + + free_notes(t); + return retval; +} + +static int append_edit(int argc, const char **argv, const char *prefix); + +static int add(int argc, const char **argv, const char *prefix) +{ + int retval = 0, force = 0; + const char *object_ref; + struct notes_tree *t; + unsigned char object[20], new_note[20]; + char logmsg[100]; + const unsigned char *note; + struct msg_arg msg = { 0, 0, STRBUF_INIT }; + struct option options[] = { + { OPTION_CALLBACK, 'm', "message", &msg, N_("message"), + N_("note contents as a string"), PARSE_OPT_NONEG, + parse_msg_arg}, + { OPTION_CALLBACK, 'F', "file", &msg, N_("file"), + N_("note contents in a file"), PARSE_OPT_NONEG, + parse_file_arg}, + { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"), + N_("reuse and edit specified note object"), PARSE_OPT_NONEG, + parse_reedit_arg}, + { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"), + N_("reuse specified note object"), PARSE_OPT_NONEG, + parse_reuse_arg}, + OPT__FORCE(&force, N_("replace existing notes")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, + PARSE_OPT_KEEP_ARGV0); + + if (2 < argc) { + error(_("too many parameters")); + usage_with_options(git_notes_add_usage, options); + } + + object_ref = argc > 1 ? argv[1] : "HEAD"; + + if (get_sha1(object_ref, object)) + die(_("Failed to resolve '%s' as a valid ref."), object_ref); + + t = init_notes_check("add"); + note = get_note(t, object); + + if (note) { + if (!force) { + if (!msg.given) { + /* + * Redirect to "edit" subcommand. + * + * We only end up here if none of -m/-F/-c/-C + * or -f are given. The original args are + * therefore still in argv[0-1]. + */ + argv[0] = "edit"; + free_notes(t); + return append_edit(argc, argv, prefix); + } + retval = error(_("Cannot add notes. Found existing notes " + "for object %s. Use '-f' to overwrite " + "existing notes"), sha1_to_hex(object)); + goto out; + } + fprintf(stderr, _("Overwriting existing notes for object %s\n"), + sha1_to_hex(object)); + } + + create_note(object, &msg, 0, note, new_note); + + if (is_null_sha1(new_note)) + remove_note(t, object); + else if (add_note(t, object, new_note, combine_notes_overwrite)) + die("BUG: combine_notes_overwrite failed"); + + snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'", + is_null_sha1(new_note) ? "removed" : "added", "add"); + commit_notes(t, logmsg); +out: + free_notes(t); + strbuf_release(&(msg.buf)); + return retval; +} + +static int copy(int argc, const char **argv, const char *prefix) +{ + int retval = 0, force = 0, from_stdin = 0; + const unsigned char *from_note, *note; + const char *object_ref; + unsigned char object[20], from_obj[20]; + struct notes_tree *t; + const char *rewrite_cmd = NULL; + struct option options[] = { + OPT__FORCE(&force, N_("replace existing notes")), + OPT_BOOLEAN(0, "stdin", &from_stdin, N_("read objects from stdin")), + OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"), + N_("load rewriting config for <command> (implies " + "--stdin)")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage, + 0); + + if (from_stdin || rewrite_cmd) { + if (argc) { + error(_("too many parameters")); + usage_with_options(git_notes_copy_usage, options); + } else { + return notes_copy_from_stdin(force, rewrite_cmd); + } + } + + if (argc < 2) { + error(_("too few parameters")); + usage_with_options(git_notes_copy_usage, options); + } + if (2 < argc) { + error(_("too many parameters")); + usage_with_options(git_notes_copy_usage, options); + } + + if (get_sha1(argv[0], from_obj)) + die(_("Failed to resolve '%s' as a valid ref."), argv[0]); + + object_ref = 1 < argc ? argv[1] : "HEAD"; + + if (get_sha1(object_ref, object)) + die(_("Failed to resolve '%s' as a valid ref."), object_ref); + + t = init_notes_check("copy"); + note = get_note(t, object); + + if (note) { + if (!force) { + retval = error(_("Cannot copy notes. Found existing " + "notes for object %s. Use '-f' to " + "overwrite existing notes"), + sha1_to_hex(object)); + goto out; + } + fprintf(stderr, _("Overwriting existing notes for object %s\n"), + sha1_to_hex(object)); + } + + from_note = get_note(t, from_obj); + if (!from_note) { + retval = error(_("Missing notes on source object %s. Cannot " + "copy."), sha1_to_hex(from_obj)); + goto out; + } + + if (add_note(t, object, from_note, combine_notes_overwrite)) + die("BUG: combine_notes_overwrite failed"); + commit_notes(t, "Notes added by 'git notes copy'"); +out: + free_notes(t); + return retval; +} + +static int append_edit(int argc, const char **argv, const char *prefix) +{ + const char *object_ref; + struct notes_tree *t; + unsigned char object[20], new_note[20]; + const unsigned char *note; + char logmsg[100]; + const char * const *usage; + struct msg_arg msg = { 0, 0, STRBUF_INIT }; + struct option options[] = { + { OPTION_CALLBACK, 'm', "message", &msg, N_("message"), + N_("note contents as a string"), PARSE_OPT_NONEG, + parse_msg_arg}, + { OPTION_CALLBACK, 'F', "file", &msg, N_("file"), + N_("note contents in a file"), PARSE_OPT_NONEG, + parse_file_arg}, + { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"), + N_("reuse and edit specified note object"), PARSE_OPT_NONEG, + parse_reedit_arg}, + { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"), + N_("reuse specified note object"), PARSE_OPT_NONEG, + parse_reuse_arg}, + OPT_END() + }; + int edit = !strcmp(argv[0], "edit"); + + usage = edit ? git_notes_edit_usage : git_notes_append_usage; + argc = parse_options(argc, argv, prefix, options, usage, + PARSE_OPT_KEEP_ARGV0); + + if (2 < argc) { + error(_("too many parameters")); + usage_with_options(usage, options); + } + + if (msg.given && edit) + fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated " + "for the 'edit' subcommand.\n" + "Please use 'git notes add -f -m/-F/-c/-C' instead.\n")); + + object_ref = 1 < argc ? argv[1] : "HEAD"; + + if (get_sha1(object_ref, object)) + die(_("Failed to resolve '%s' as a valid ref."), object_ref); + + t = init_notes_check(argv[0]); + note = get_note(t, object); + + create_note(object, &msg, !edit, note, new_note); + + if (is_null_sha1(new_note)) + remove_note(t, object); + else if (add_note(t, object, new_note, combine_notes_overwrite)) + die("BUG: combine_notes_overwrite failed"); + + snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'", + is_null_sha1(new_note) ? "removed" : "added", argv[0]); + commit_notes(t, logmsg); + free_notes(t); + strbuf_release(&(msg.buf)); + return 0; +} + +static int show(int argc, const char **argv, const char *prefix) +{ + const char *object_ref; + struct notes_tree *t; + unsigned char object[20]; + const unsigned char *note; + int retval; + struct option options[] = { + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, git_notes_show_usage, + 0); + + if (1 < argc) { + error(_("too many parameters")); + usage_with_options(git_notes_show_usage, options); + } + + object_ref = argc ? argv[0] : "HEAD"; + + if (get_sha1(object_ref, object)) + die(_("Failed to resolve '%s' as a valid ref."), object_ref); + + t = init_notes_check("show"); + note = get_note(t, object); + + if (!note) + retval = error(_("No note found for object %s."), + sha1_to_hex(object)); + else { + const char *show_args[3] = {"show", sha1_to_hex(note), NULL}; + retval = execv_git_cmd(show_args); + } + free_notes(t); + return retval; +} + +static int merge_abort(struct notes_merge_options *o) +{ + int ret = 0; + + /* + * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call + * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. + */ + + if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0)) + ret += error("Failed to delete ref NOTES_MERGE_PARTIAL"); + if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF)) + ret += error("Failed to delete ref NOTES_MERGE_REF"); + if (notes_merge_abort(o)) + ret += error("Failed to remove 'git notes merge' worktree"); + return ret; +} + +static int merge_commit(struct notes_merge_options *o) +{ + struct strbuf msg = STRBUF_INIT; + unsigned char sha1[20], parent_sha1[20]; + struct notes_tree *t; + struct commit *partial; + struct pretty_print_context pretty_ctx; + void *local_ref_to_free; + int ret; + + /* + * Read partial merge result from .git/NOTES_MERGE_PARTIAL, + * and target notes ref from .git/NOTES_MERGE_REF. + */ + + if (get_sha1("NOTES_MERGE_PARTIAL", sha1)) + die("Failed to read ref NOTES_MERGE_PARTIAL"); + else if (!(partial = lookup_commit_reference(sha1))) + die("Could not find commit from NOTES_MERGE_PARTIAL."); + else if (parse_commit(partial)) + die("Could not parse commit from NOTES_MERGE_PARTIAL."); + + if (partial->parents) + hashcpy(parent_sha1, partial->parents->item->object.sha1); + else + hashclr(parent_sha1); + + t = xcalloc(1, sizeof(struct notes_tree)); + init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); + + o->local_ref = local_ref_to_free = + resolve_refdup("NOTES_MERGE_REF", sha1, 0, NULL); + if (!o->local_ref) + die("Failed to resolve NOTES_MERGE_REF"); + + if (notes_merge_commit(o, t, partial, sha1)) + die("Failed to finalize notes merge"); + + /* Reuse existing commit message in reflog message */ + memset(&pretty_ctx, 0, sizeof(pretty_ctx)); + format_commit_message(partial, "%s", &msg, &pretty_ctx); + strbuf_trim(&msg); + strbuf_insert(&msg, 0, "notes: ", 7); + update_ref(msg.buf, o->local_ref, sha1, + is_null_sha1(parent_sha1) ? NULL : parent_sha1, + 0, DIE_ON_ERR); + + free_notes(t); + strbuf_release(&msg); + ret = merge_abort(o); + free(local_ref_to_free); + return ret; +} + +static int merge(int argc, const char **argv, const char *prefix) +{ + struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; + unsigned char result_sha1[20]; + struct notes_tree *t; + struct notes_merge_options o; + int do_merge = 0, do_commit = 0, do_abort = 0; + int verbosity = 0, result; + const char *strategy = NULL; + struct option options[] = { + OPT_GROUP(N_("General options")), + OPT__VERBOSITY(&verbosity), + OPT_GROUP(N_("Merge options")), + OPT_STRING('s', "strategy", &strategy, N_("strategy"), + N_("resolve notes conflicts using the given strategy " + "(manual/ours/theirs/union/cat_sort_uniq)")), + OPT_GROUP(N_("Committing unmerged notes")), + { OPTION_BOOLEAN, 0, "commit", &do_commit, NULL, + N_("finalize notes merge by committing unmerged notes"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG }, + OPT_GROUP(N_("Aborting notes merge resolution")), + { OPTION_BOOLEAN, 0, "abort", &do_abort, NULL, + N_("abort notes merge"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG }, + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_notes_merge_usage, 0); + + if (strategy || do_commit + do_abort == 0) + do_merge = 1; + if (do_merge + do_commit + do_abort != 1) { + error("cannot mix --commit, --abort or -s/--strategy"); + usage_with_options(git_notes_merge_usage, options); + } + + if (do_merge && argc != 1) { + error("Must specify a notes ref to merge"); + usage_with_options(git_notes_merge_usage, options); + } else if (!do_merge && argc) { + error("too many parameters"); + usage_with_options(git_notes_merge_usage, options); + } + + init_notes_merge_options(&o); + o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT; + + if (do_abort) + return merge_abort(&o); + if (do_commit) + return merge_commit(&o); + + o.local_ref = default_notes_ref(); + strbuf_addstr(&remote_ref, argv[0]); + expand_notes_ref(&remote_ref); + o.remote_ref = remote_ref.buf; + + if (strategy) { + if (!strcmp(strategy, "manual")) + o.strategy = NOTES_MERGE_RESOLVE_MANUAL; + else if (!strcmp(strategy, "ours")) + o.strategy = NOTES_MERGE_RESOLVE_OURS; + else if (!strcmp(strategy, "theirs")) + o.strategy = NOTES_MERGE_RESOLVE_THEIRS; + else if (!strcmp(strategy, "union")) + o.strategy = NOTES_MERGE_RESOLVE_UNION; + else if (!strcmp(strategy, "cat_sort_uniq")) + o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ; + else { + error("Unknown -s/--strategy: %s", strategy); + usage_with_options(git_notes_merge_usage, options); + } + } + + t = init_notes_check("merge"); + + strbuf_addf(&msg, "notes: Merged notes from %s into %s", + remote_ref.buf, default_notes_ref()); + strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ + + result = notes_merge(&o, t, result_sha1); + + if (result >= 0) /* Merge resulted (trivially) in result_sha1 */ + /* Update default notes ref with new commit */ + update_ref(msg.buf, default_notes_ref(), result_sha1, NULL, + 0, DIE_ON_ERR); + else { /* Merge has unresolved conflicts */ + /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ + update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL, + 0, DIE_ON_ERR); + /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ + if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL)) + die("Failed to store link to current notes ref (%s)", + default_notes_ref()); + printf("Automatic notes merge failed. Fix conflicts in %s and " + "commit the result with 'git notes merge --commit', or " + "abort the merge with 'git notes merge --abort'.\n", + git_path(NOTES_MERGE_WORKTREE)); + } + + free_notes(t); + strbuf_release(&remote_ref); + strbuf_release(&msg); + return result < 0; /* return non-zero on conflicts */ +} + +#define IGNORE_MISSING 1 + +static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag) +{ + int status; + unsigned char sha1[20]; + if (get_sha1(name, sha1)) + return error(_("Failed to resolve '%s' as a valid ref."), name); + status = remove_note(t, sha1); + if (status) + fprintf(stderr, _("Object %s has no note\n"), name); + else + fprintf(stderr, _("Removing note for object %s\n"), name); + return (flag & IGNORE_MISSING) ? 0 : status; +} + +static int remove_cmd(int argc, const char **argv, const char *prefix) +{ + unsigned flag = 0; + int from_stdin = 0; + struct option options[] = { + OPT_BIT(0, "ignore-missing", &flag, + N_("attempt to remove non-existent note is not an error"), + IGNORE_MISSING), + OPT_BOOLEAN(0, "stdin", &from_stdin, + N_("read object names from the standard input")), + OPT_END() + }; + struct notes_tree *t; + int retval = 0; + + argc = parse_options(argc, argv, prefix, options, + git_notes_remove_usage, 0); + + t = init_notes_check("remove"); + + if (!argc && !from_stdin) { + retval = remove_one_note(t, "HEAD", flag); + } else { + while (*argv) { + retval |= remove_one_note(t, *argv, flag); + argv++; + } + } + if (from_stdin) { + struct strbuf sb = STRBUF_INIT; + while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { + strbuf_rtrim(&sb); + retval |= remove_one_note(t, sb.buf, flag); + } + strbuf_release(&sb); + } + if (!retval) + commit_notes(t, "Notes removed by 'git notes remove'"); + free_notes(t); + return retval; +} + +static int prune(int argc, const char **argv, const char *prefix) +{ + struct notes_tree *t; + int show_only = 0, verbose = 0; + struct option options[] = { + OPT__DRY_RUN(&show_only, "do not remove, show only"), + OPT__VERBOSE(&verbose, "report pruned notes"), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage, + 0); + + if (argc) { + error(_("too many parameters")); + usage_with_options(git_notes_prune_usage, options); + } + + t = init_notes_check("prune"); + + prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | + (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); + if (!show_only) + commit_notes(t, "Notes removed by 'git notes prune'"); + free_notes(t); + return 0; +} + +static int get_ref(int argc, const char **argv, const char *prefix) +{ + struct option options[] = { OPT_END() }; + argc = parse_options(argc, argv, prefix, options, + git_notes_get_ref_usage, 0); + + if (argc) { + error("too many parameters"); + usage_with_options(git_notes_get_ref_usage, options); + } + + puts(default_notes_ref()); + return 0; +} + +int cmd_notes(int argc, const char **argv, const char *prefix) +{ + int result; + const char *override_notes_ref = NULL; + struct option options[] = { + OPT_STRING(0, "ref", &override_notes_ref, N_("notes_ref"), + N_("use notes from <notes_ref>")), + OPT_END() + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, git_notes_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + + if (override_notes_ref) { + struct strbuf sb = STRBUF_INIT; + strbuf_addstr(&sb, override_notes_ref); + expand_notes_ref(&sb); + setenv("GIT_NOTES_REF", sb.buf, 1); + strbuf_release(&sb); + } + + if (argc < 1 || !strcmp(argv[0], "list")) + result = list(argc, argv, prefix); + else if (!strcmp(argv[0], "add")) + result = add(argc, argv, prefix); + else if (!strcmp(argv[0], "copy")) + result = copy(argc, argv, prefix); + else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit")) + result = append_edit(argc, argv, prefix); + else if (!strcmp(argv[0], "show")) + result = show(argc, argv, prefix); + else if (!strcmp(argv[0], "merge")) + result = merge(argc, argv, prefix); + else if (!strcmp(argv[0], "remove")) + result = remove_cmd(argc, argv, prefix); + else if (!strcmp(argv[0], "prune")) + result = prune(argc, argv, prefix); + else if (!strcmp(argv[0], "get-ref")) + result = get_ref(argc, argv, prefix); + else { + result = error(_("Unknown subcommand: %s"), argv[0]); + usage_with_options(git_notes_usage, options); + } + + return result ? 1 : 0; +} diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c new file mode 100644 index 0000000000..f069462cb0 --- /dev/null +++ b/builtin/pack-objects.c @@ -0,0 +1,2602 @@ +#include "builtin.h" +#include "cache.h" +#include "attr.h" +#include "object.h" +#include "blob.h" +#include "commit.h" +#include "tag.h" +#include "tree.h" +#include "delta.h" +#include "pack.h" +#include "pack-revindex.h" +#include "csum-file.h" +#include "tree-walk.h" +#include "diff.h" +#include "revision.h" +#include "list-objects.h" +#include "progress.h" +#include "refs.h" +#include "streaming.h" +#include "thread-utils.h" + +static const char *pack_usage[] = { + N_("git pack-objects --stdout [options...] [< ref-list | < object-list]"), + N_("git pack-objects [options...] base-name [< ref-list | < object-list]"), + NULL +}; + +struct object_entry { + struct pack_idx_entry idx; + unsigned long size; /* uncompressed size */ + struct packed_git *in_pack; /* already in pack */ + off_t in_pack_offset; + struct object_entry *delta; /* delta base object */ + struct object_entry *delta_child; /* deltified objects who bases me */ + struct object_entry *delta_sibling; /* other deltified objects who + * uses the same base as me + */ + void *delta_data; /* cached delta (uncompressed) */ + unsigned long delta_size; /* delta data size (uncompressed) */ + unsigned long z_delta_size; /* delta data size (compressed) */ + unsigned int hash; /* name hint hash */ + enum object_type type; + enum object_type in_pack_type; /* could be delta */ + unsigned char in_pack_header_size; + unsigned char preferred_base; /* we do not pack this, but is available + * to be used as the base object to delta + * objects against. + */ + unsigned char no_try_delta; + unsigned char tagged; /* near the very tip of refs */ + unsigned char filled; /* assigned write-order */ +}; + +/* + * Objects we are going to pack are collected in objects array (dynamically + * expanded). nr_objects & nr_alloc controls this array. They are stored + * in the order we see -- typically rev-list --objects order that gives us + * nice "minimum seek" order. + */ +static struct object_entry *objects; +static struct pack_idx_entry **written_list; +static uint32_t nr_objects, nr_alloc, nr_result, nr_written; + +static int non_empty; +static int reuse_delta = 1, reuse_object = 1; +static int keep_unreachable, unpack_unreachable, include_tag; +static unsigned long unpack_unreachable_expiration; +static int local; +static int incremental; +static int ignore_packed_keep; +static int allow_ofs_delta; +static struct pack_idx_option pack_idx_opts; +static const char *base_name; +static int progress = 1; +static int window = 10; +static unsigned long pack_size_limit; +static int depth = 50; +static int delta_search_threads; +static int pack_to_stdout; +static int num_preferred_base; +static struct progress *progress_state; +static int pack_compression_level = Z_DEFAULT_COMPRESSION; +static int pack_compression_seen; + +static unsigned long delta_cache_size = 0; +static unsigned long max_delta_cache_size = 256 * 1024 * 1024; +static unsigned long cache_max_small_delta_size = 1000; + +static unsigned long window_memory_limit = 0; + +/* + * The object names in objects array are hashed with this hashtable, + * to help looking up the entry by object name. + * This hashtable is built after all the objects are seen. + */ +static int *object_ix; +static int object_ix_hashsz; +static struct object_entry *locate_object_entry(const unsigned char *sha1); + +/* + * stats + */ +static uint32_t written, written_delta; +static uint32_t reused, reused_delta; + + +static void *get_delta(struct object_entry *entry) +{ + unsigned long size, base_size, delta_size; + void *buf, *base_buf, *delta_buf; + enum object_type type; + + buf = read_sha1_file(entry->idx.sha1, &type, &size); + if (!buf) + die("unable to read %s", sha1_to_hex(entry->idx.sha1)); + base_buf = read_sha1_file(entry->delta->idx.sha1, &type, &base_size); + if (!base_buf) + die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1)); + delta_buf = diff_delta(base_buf, base_size, + buf, size, &delta_size, 0); + if (!delta_buf || delta_size != entry->delta_size) + die("delta size changed"); + free(buf); + free(base_buf); + return delta_buf; +} + +static unsigned long do_compress(void **pptr, unsigned long size) +{ + git_zstream stream; + void *in, *out; + unsigned long maxsize; + + memset(&stream, 0, sizeof(stream)); + git_deflate_init(&stream, pack_compression_level); + maxsize = git_deflate_bound(&stream, size); + + in = *pptr; + out = xmalloc(maxsize); + *pptr = out; + + stream.next_in = in; + stream.avail_in = size; + stream.next_out = out; + stream.avail_out = maxsize; + while (git_deflate(&stream, Z_FINISH) == Z_OK) + ; /* nothing */ + git_deflate_end(&stream); + + free(in); + return stream.total_out; +} + +static unsigned long write_large_blob_data(struct git_istream *st, struct sha1file *f, + const unsigned char *sha1) +{ + git_zstream stream; + unsigned char ibuf[1024 * 16]; + unsigned char obuf[1024 * 16]; + unsigned long olen = 0; + + memset(&stream, 0, sizeof(stream)); + git_deflate_init(&stream, pack_compression_level); + + for (;;) { + ssize_t readlen; + int zret = Z_OK; + readlen = read_istream(st, ibuf, sizeof(ibuf)); + if (readlen == -1) + die(_("unable to read %s"), sha1_to_hex(sha1)); + + stream.next_in = ibuf; + stream.avail_in = readlen; + while ((stream.avail_in || readlen == 0) && + (zret == Z_OK || zret == Z_BUF_ERROR)) { + stream.next_out = obuf; + stream.avail_out = sizeof(obuf); + zret = git_deflate(&stream, readlen ? 0 : Z_FINISH); + sha1write(f, obuf, stream.next_out - obuf); + olen += stream.next_out - obuf; + } + if (stream.avail_in) + die(_("deflate error (%d)"), zret); + if (readlen == 0) { + if (zret != Z_STREAM_END) + die(_("deflate error (%d)"), zret); + break; + } + } + git_deflate_end(&stream); + return olen; +} + +/* + * we are going to reuse the existing object data as is. make + * sure it is not corrupt. + */ +static int check_pack_inflate(struct packed_git *p, + struct pack_window **w_curs, + off_t offset, + off_t len, + unsigned long expect) +{ + git_zstream stream; + unsigned char fakebuf[4096], *in; + int st; + + memset(&stream, 0, sizeof(stream)); + git_inflate_init(&stream); + do { + in = use_pack(p, w_curs, offset, &stream.avail_in); + stream.next_in = in; + stream.next_out = fakebuf; + stream.avail_out = sizeof(fakebuf); + st = git_inflate(&stream, Z_FINISH); + offset += stream.next_in - in; + } while (st == Z_OK || st == Z_BUF_ERROR); + git_inflate_end(&stream); + return (st == Z_STREAM_END && + stream.total_out == expect && + stream.total_in == len) ? 0 : -1; +} + +static void copy_pack_data(struct sha1file *f, + struct packed_git *p, + struct pack_window **w_curs, + off_t offset, + off_t len) +{ + unsigned char *in; + unsigned long avail; + + while (len) { + in = use_pack(p, w_curs, offset, &avail); + if (avail > len) + avail = (unsigned long)len; + sha1write(f, in, avail); + offset += avail; + len -= avail; + } +} + +/* Return 0 if we will bust the pack-size limit */ +static unsigned long write_no_reuse_object(struct sha1file *f, struct object_entry *entry, + unsigned long limit, int usable_delta) +{ + unsigned long size, datalen; + unsigned char header[10], dheader[10]; + unsigned hdrlen; + enum object_type type; + void *buf; + struct git_istream *st = NULL; + + if (!usable_delta) { + if (entry->type == OBJ_BLOB && + entry->size > big_file_threshold && + (st = open_istream(entry->idx.sha1, &type, &size, NULL)) != NULL) + buf = NULL; + else { + buf = read_sha1_file(entry->idx.sha1, &type, &size); + if (!buf) + die(_("unable to read %s"), sha1_to_hex(entry->idx.sha1)); + } + /* + * make sure no cached delta data remains from a + * previous attempt before a pack split occurred. + */ + free(entry->delta_data); + entry->delta_data = NULL; + entry->z_delta_size = 0; + } else if (entry->delta_data) { + size = entry->delta_size; + buf = entry->delta_data; + entry->delta_data = NULL; + type = (allow_ofs_delta && entry->delta->idx.offset) ? + OBJ_OFS_DELTA : OBJ_REF_DELTA; + } else { + buf = get_delta(entry); + size = entry->delta_size; + type = (allow_ofs_delta && entry->delta->idx.offset) ? + OBJ_OFS_DELTA : OBJ_REF_DELTA; + } + + if (st) /* large blob case, just assume we don't compress well */ + datalen = size; + else if (entry->z_delta_size) + datalen = entry->z_delta_size; + else + datalen = do_compress(&buf, size); + + /* + * The object header is a byte of 'type' followed by zero or + * more bytes of length. + */ + hdrlen = encode_in_pack_object_header(type, size, header); + + if (type == OBJ_OFS_DELTA) { + /* + * Deltas with relative base contain an additional + * encoding of the relative offset for the delta + * base from this object's position in the pack. + */ + off_t ofs = entry->idx.offset - entry->delta->idx.offset; + unsigned pos = sizeof(dheader) - 1; + dheader[pos] = ofs & 127; + while (ofs >>= 7) + dheader[--pos] = 128 | (--ofs & 127); + if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { + if (st) + close_istream(st); + free(buf); + return 0; + } + sha1write(f, header, hdrlen); + sha1write(f, dheader + pos, sizeof(dheader) - pos); + hdrlen += sizeof(dheader) - pos; + } else if (type == OBJ_REF_DELTA) { + /* + * Deltas with a base reference contain + * an additional 20 bytes for the base sha1. + */ + if (limit && hdrlen + 20 + datalen + 20 >= limit) { + if (st) + close_istream(st); + free(buf); + return 0; + } + sha1write(f, header, hdrlen); + sha1write(f, entry->delta->idx.sha1, 20); + hdrlen += 20; + } else { + if (limit && hdrlen + datalen + 20 >= limit) { + if (st) + close_istream(st); + free(buf); + return 0; + } + sha1write(f, header, hdrlen); + } + if (st) { + datalen = write_large_blob_data(st, f, entry->idx.sha1); + close_istream(st); + } else { + sha1write(f, buf, datalen); + free(buf); + } + + return hdrlen + datalen; +} + +/* Return 0 if we will bust the pack-size limit */ +static unsigned long write_reuse_object(struct sha1file *f, struct object_entry *entry, + unsigned long limit, int usable_delta) +{ + struct packed_git *p = entry->in_pack; + struct pack_window *w_curs = NULL; + struct revindex_entry *revidx; + off_t offset; + enum object_type type = entry->type; + unsigned long datalen; + unsigned char header[10], dheader[10]; + unsigned hdrlen; + + if (entry->delta) + type = (allow_ofs_delta && entry->delta->idx.offset) ? + OBJ_OFS_DELTA : OBJ_REF_DELTA; + hdrlen = encode_in_pack_object_header(type, entry->size, header); + + offset = entry->in_pack_offset; + revidx = find_pack_revindex(p, offset); + datalen = revidx[1].offset - offset; + if (!pack_to_stdout && p->index_version > 1 && + check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { + error("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1)); + unuse_pack(&w_curs); + return write_no_reuse_object(f, entry, limit, usable_delta); + } + + offset += entry->in_pack_header_size; + datalen -= entry->in_pack_header_size; + + if (!pack_to_stdout && p->index_version == 1 && + check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) { + error("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1)); + unuse_pack(&w_curs); + return write_no_reuse_object(f, entry, limit, usable_delta); + } + + if (type == OBJ_OFS_DELTA) { + off_t ofs = entry->idx.offset - entry->delta->idx.offset; + unsigned pos = sizeof(dheader) - 1; + dheader[pos] = ofs & 127; + while (ofs >>= 7) + dheader[--pos] = 128 | (--ofs & 127); + if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { + unuse_pack(&w_curs); + return 0; + } + sha1write(f, header, hdrlen); + sha1write(f, dheader + pos, sizeof(dheader) - pos); + hdrlen += sizeof(dheader) - pos; + reused_delta++; + } else if (type == OBJ_REF_DELTA) { + if (limit && hdrlen + 20 + datalen + 20 >= limit) { + unuse_pack(&w_curs); + return 0; + } + sha1write(f, header, hdrlen); + sha1write(f, entry->delta->idx.sha1, 20); + hdrlen += 20; + reused_delta++; + } else { + if (limit && hdrlen + datalen + 20 >= limit) { + unuse_pack(&w_curs); + return 0; + } + sha1write(f, header, hdrlen); + } + copy_pack_data(f, p, &w_curs, offset, datalen); + unuse_pack(&w_curs); + reused++; + return hdrlen + datalen; +} + +/* Return 0 if we will bust the pack-size limit */ +static unsigned long write_object(struct sha1file *f, + struct object_entry *entry, + off_t write_offset) +{ + unsigned long limit, len; + int usable_delta, to_reuse; + + if (!pack_to_stdout) + crc32_begin(f); + + /* apply size limit if limited packsize and not first object */ + if (!pack_size_limit || !nr_written) + limit = 0; + else if (pack_size_limit <= write_offset) + /* + * the earlier object did not fit the limit; avoid + * mistaking this with unlimited (i.e. limit = 0). + */ + limit = 1; + else + limit = pack_size_limit - write_offset; + + if (!entry->delta) + usable_delta = 0; /* no delta */ + else if (!pack_size_limit) + usable_delta = 1; /* unlimited packfile */ + else if (entry->delta->idx.offset == (off_t)-1) + usable_delta = 0; /* base was written to another pack */ + else if (entry->delta->idx.offset) + usable_delta = 1; /* base already exists in this pack */ + else + usable_delta = 0; /* base could end up in another pack */ + + if (!reuse_object) + to_reuse = 0; /* explicit */ + else if (!entry->in_pack) + to_reuse = 0; /* can't reuse what we don't have */ + else if (entry->type == OBJ_REF_DELTA || entry->type == OBJ_OFS_DELTA) + /* check_object() decided it for us ... */ + to_reuse = usable_delta; + /* ... but pack split may override that */ + else if (entry->type != entry->in_pack_type) + to_reuse = 0; /* pack has delta which is unusable */ + else if (entry->delta) + to_reuse = 0; /* we want to pack afresh */ + else + to_reuse = 1; /* we have it in-pack undeltified, + * and we do not need to deltify it. + */ + + if (!to_reuse) + len = write_no_reuse_object(f, entry, limit, usable_delta); + else + len = write_reuse_object(f, entry, limit, usable_delta); + if (!len) + return 0; + + if (usable_delta) + written_delta++; + written++; + if (!pack_to_stdout) + entry->idx.crc32 = crc32_end(f); + return len; +} + +enum write_one_status { + WRITE_ONE_SKIP = -1, /* already written */ + WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ + WRITE_ONE_WRITTEN = 1, /* normal */ + WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ +}; + +static enum write_one_status write_one(struct sha1file *f, + struct object_entry *e, + off_t *offset) +{ + unsigned long size; + int recursing; + + /* + * we set offset to 1 (which is an impossible value) to mark + * the fact that this object is involved in "write its base + * first before writing a deltified object" recursion. + */ + recursing = (e->idx.offset == 1); + if (recursing) { + warning("recursive delta detected for object %s", + sha1_to_hex(e->idx.sha1)); + return WRITE_ONE_RECURSIVE; + } else if (e->idx.offset || e->preferred_base) { + /* offset is non zero if object is written already. */ + return WRITE_ONE_SKIP; + } + + /* if we are deltified, write out base object first. */ + if (e->delta) { + e->idx.offset = 1; /* now recurse */ + switch (write_one(f, e->delta, offset)) { + case WRITE_ONE_RECURSIVE: + /* we cannot depend on this one */ + e->delta = NULL; + break; + default: + break; + case WRITE_ONE_BREAK: + e->idx.offset = recursing; + return WRITE_ONE_BREAK; + } + } + + e->idx.offset = *offset; + size = write_object(f, e, *offset); + if (!size) { + e->idx.offset = recursing; + return WRITE_ONE_BREAK; + } + written_list[nr_written++] = &e->idx; + + /* make sure off_t is sufficiently large not to wrap */ + if (signed_add_overflows(*offset, size)) + die("pack too large for current definition of off_t"); + *offset += size; + return WRITE_ONE_WRITTEN; +} + +static int mark_tagged(const char *path, const unsigned char *sha1, int flag, + void *cb_data) +{ + unsigned char peeled[20]; + struct object_entry *entry = locate_object_entry(sha1); + + if (entry) + entry->tagged = 1; + if (!peel_ref(path, peeled)) { + entry = locate_object_entry(peeled); + if (entry) + entry->tagged = 1; + } + return 0; +} + +static inline void add_to_write_order(struct object_entry **wo, + unsigned int *endp, + struct object_entry *e) +{ + if (e->filled) + return; + wo[(*endp)++] = e; + e->filled = 1; +} + +static void add_descendants_to_write_order(struct object_entry **wo, + unsigned int *endp, + struct object_entry *e) +{ + int add_to_order = 1; + while (e) { + if (add_to_order) { + struct object_entry *s; + /* add this node... */ + add_to_write_order(wo, endp, e); + /* all its siblings... */ + for (s = e->delta_sibling; s; s = s->delta_sibling) { + add_to_write_order(wo, endp, s); + } + } + /* drop down a level to add left subtree nodes if possible */ + if (e->delta_child) { + add_to_order = 1; + e = e->delta_child; + } else { + add_to_order = 0; + /* our sibling might have some children, it is next */ + if (e->delta_sibling) { + e = e->delta_sibling; + continue; + } + /* go back to our parent node */ + e = e->delta; + while (e && !e->delta_sibling) { + /* we're on the right side of a subtree, keep + * going up until we can go right again */ + e = e->delta; + } + if (!e) { + /* done- we hit our original root node */ + return; + } + /* pass it off to sibling at this level */ + e = e->delta_sibling; + } + }; +} + +static void add_family_to_write_order(struct object_entry **wo, + unsigned int *endp, + struct object_entry *e) +{ + struct object_entry *root; + + for (root = e; root->delta; root = root->delta) + ; /* nothing */ + add_descendants_to_write_order(wo, endp, root); +} + +static struct object_entry **compute_write_order(void) +{ + unsigned int i, wo_end, last_untagged; + + struct object_entry **wo = xmalloc(nr_objects * sizeof(*wo)); + + for (i = 0; i < nr_objects; i++) { + objects[i].tagged = 0; + objects[i].filled = 0; + objects[i].delta_child = NULL; + objects[i].delta_sibling = NULL; + } + + /* + * Fully connect delta_child/delta_sibling network. + * Make sure delta_sibling is sorted in the original + * recency order. + */ + for (i = nr_objects; i > 0;) { + struct object_entry *e = &objects[--i]; + if (!e->delta) + continue; + /* Mark me as the first child */ + e->delta_sibling = e->delta->delta_child; + e->delta->delta_child = e; + } + + /* + * Mark objects that are at the tip of tags. + */ + for_each_tag_ref(mark_tagged, NULL); + + /* + * Give the objects in the original recency order until + * we see a tagged tip. + */ + for (i = wo_end = 0; i < nr_objects; i++) { + if (objects[i].tagged) + break; + add_to_write_order(wo, &wo_end, &objects[i]); + } + last_untagged = i; + + /* + * Then fill all the tagged tips. + */ + for (; i < nr_objects; i++) { + if (objects[i].tagged) + add_to_write_order(wo, &wo_end, &objects[i]); + } + + /* + * And then all remaining commits and tags. + */ + for (i = last_untagged; i < nr_objects; i++) { + if (objects[i].type != OBJ_COMMIT && + objects[i].type != OBJ_TAG) + continue; + add_to_write_order(wo, &wo_end, &objects[i]); + } + + /* + * And then all the trees. + */ + for (i = last_untagged; i < nr_objects; i++) { + if (objects[i].type != OBJ_TREE) + continue; + add_to_write_order(wo, &wo_end, &objects[i]); + } + + /* + * Finally all the rest in really tight order + */ + for (i = last_untagged; i < nr_objects; i++) { + if (!objects[i].filled) + add_family_to_write_order(wo, &wo_end, &objects[i]); + } + + if (wo_end != nr_objects) + die("ordered %u objects, expected %"PRIu32, wo_end, nr_objects); + + return wo; +} + +static void write_pack_file(void) +{ + uint32_t i = 0, j; + struct sha1file *f; + off_t offset; + uint32_t nr_remaining = nr_result; + time_t last_mtime = 0; + struct object_entry **write_order; + + if (progress > pack_to_stdout) + progress_state = start_progress("Writing objects", nr_result); + written_list = xmalloc(nr_objects * sizeof(*written_list)); + write_order = compute_write_order(); + + do { + unsigned char sha1[20]; + char *pack_tmp_name = NULL; + + if (pack_to_stdout) + f = sha1fd_throughput(1, "<stdout>", progress_state); + else + f = create_tmp_packfile(&pack_tmp_name); + + offset = write_pack_header(f, nr_remaining); + if (!offset) + die_errno("unable to write pack header"); + nr_written = 0; + for (; i < nr_objects; i++) { + struct object_entry *e = write_order[i]; + if (write_one(f, e, &offset) == WRITE_ONE_BREAK) + break; + display_progress(progress_state, written); + } + + /* + * Did we write the wrong # entries in the header? + * If so, rewrite it like in fast-import + */ + if (pack_to_stdout) { + sha1close(f, sha1, CSUM_CLOSE); + } else if (nr_written == nr_remaining) { + sha1close(f, sha1, CSUM_FSYNC); + } else { + int fd = sha1close(f, sha1, 0); + fixup_pack_header_footer(fd, sha1, pack_tmp_name, + nr_written, sha1, offset); + close(fd); + } + + if (!pack_to_stdout) { + struct stat st; + char tmpname[PATH_MAX]; + + /* + * Packs are runtime accessed in their mtime + * order since newer packs are more likely to contain + * younger objects. So if we are creating multiple + * packs then we should modify the mtime of later ones + * to preserve this property. + */ + if (stat(pack_tmp_name, &st) < 0) { + warning("failed to stat %s: %s", + pack_tmp_name, strerror(errno)); + } else if (!last_mtime) { + last_mtime = st.st_mtime; + } else { + struct utimbuf utb; + utb.actime = st.st_atime; + utb.modtime = --last_mtime; + if (utime(pack_tmp_name, &utb) < 0) + warning("failed utime() on %s: %s", + tmpname, strerror(errno)); + } + + /* Enough space for "-<sha-1>.pack"? */ + if (sizeof(tmpname) <= strlen(base_name) + 50) + die("pack base name '%s' too long", base_name); + snprintf(tmpname, sizeof(tmpname), "%s-", base_name); + finish_tmp_packfile(tmpname, pack_tmp_name, + written_list, nr_written, + &pack_idx_opts, sha1); + free(pack_tmp_name); + puts(sha1_to_hex(sha1)); + } + + /* mark written objects as written to previous pack */ + for (j = 0; j < nr_written; j++) { + written_list[j]->offset = (off_t)-1; + } + nr_remaining -= nr_written; + } while (nr_remaining && i < nr_objects); + + free(written_list); + free(write_order); + stop_progress(&progress_state); + if (written != nr_result) + die("wrote %"PRIu32" objects while expecting %"PRIu32, + written, nr_result); +} + +static int locate_object_entry_hash(const unsigned char *sha1) +{ + int i; + unsigned int ui; + memcpy(&ui, sha1, sizeof(unsigned int)); + i = ui % object_ix_hashsz; + while (0 < object_ix[i]) { + if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1)) + return i; + if (++i == object_ix_hashsz) + i = 0; + } + return -1 - i; +} + +static struct object_entry *locate_object_entry(const unsigned char *sha1) +{ + int i; + + if (!object_ix_hashsz) + return NULL; + + i = locate_object_entry_hash(sha1); + if (0 <= i) + return &objects[object_ix[i]-1]; + return NULL; +} + +static void rehash_objects(void) +{ + uint32_t i; + struct object_entry *oe; + + object_ix_hashsz = nr_objects * 3; + if (object_ix_hashsz < 1024) + object_ix_hashsz = 1024; + object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz); + memset(object_ix, 0, sizeof(int) * object_ix_hashsz); + for (i = 0, oe = objects; i < nr_objects; i++, oe++) { + int ix = locate_object_entry_hash(oe->idx.sha1); + if (0 <= ix) + continue; + ix = -1 - ix; + object_ix[ix] = i + 1; + } +} + +static unsigned name_hash(const char *name) +{ + unsigned c, hash = 0; + + if (!name) + return 0; + + /* + * This effectively just creates a sortable number from the + * last sixteen non-whitespace characters. Last characters + * count "most", so things that end in ".c" sort together. + */ + while ((c = *name++) != 0) { + if (isspace(c)) + continue; + hash = (hash >> 2) + (c << 24); + } + return hash; +} + +static void setup_delta_attr_check(struct git_attr_check *check) +{ + static struct git_attr *attr_delta; + + if (!attr_delta) + attr_delta = git_attr("delta"); + + check[0].attr = attr_delta; +} + +static int no_try_delta(const char *path) +{ + struct git_attr_check check[1]; + + setup_delta_attr_check(check); + if (git_check_attr(path, ARRAY_SIZE(check), check)) + return 0; + if (ATTR_FALSE(check->value)) + return 1; + return 0; +} + +static int add_object_entry(const unsigned char *sha1, enum object_type type, + const char *name, int exclude) +{ + struct object_entry *entry; + struct packed_git *p, *found_pack = NULL; + off_t found_offset = 0; + int ix; + unsigned hash = name_hash(name); + + ix = nr_objects ? locate_object_entry_hash(sha1) : -1; + if (ix >= 0) { + if (exclude) { + entry = objects + object_ix[ix] - 1; + if (!entry->preferred_base) + nr_result--; + entry->preferred_base = 1; + } + return 0; + } + + if (!exclude && local && has_loose_object_nonlocal(sha1)) + return 0; + + for (p = packed_git; p; p = p->next) { + off_t offset = find_pack_entry_one(sha1, p); + if (offset) { + if (!found_pack) { + if (!is_pack_valid(p)) { + warning("packfile %s cannot be accessed", p->pack_name); + continue; + } + found_offset = offset; + found_pack = p; + } + if (exclude) + break; + if (incremental) + return 0; + if (local && !p->pack_local) + return 0; + if (ignore_packed_keep && p->pack_local && p->pack_keep) + return 0; + } + } + + if (nr_objects >= nr_alloc) { + nr_alloc = (nr_alloc + 1024) * 3 / 2; + objects = xrealloc(objects, nr_alloc * sizeof(*entry)); + } + + entry = objects + nr_objects++; + memset(entry, 0, sizeof(*entry)); + hashcpy(entry->idx.sha1, sha1); + entry->hash = hash; + if (type) + entry->type = type; + if (exclude) + entry->preferred_base = 1; + else + nr_result++; + if (found_pack) { + entry->in_pack = found_pack; + entry->in_pack_offset = found_offset; + } + + if (object_ix_hashsz * 3 <= nr_objects * 4) + rehash_objects(); + else + object_ix[-1 - ix] = nr_objects; + + display_progress(progress_state, nr_objects); + + if (name && no_try_delta(name)) + entry->no_try_delta = 1; + + return 1; +} + +struct pbase_tree_cache { + unsigned char sha1[20]; + int ref; + int temporary; + void *tree_data; + unsigned long tree_size; +}; + +static struct pbase_tree_cache *(pbase_tree_cache[256]); +static int pbase_tree_cache_ix(const unsigned char *sha1) +{ + return sha1[0] % ARRAY_SIZE(pbase_tree_cache); +} +static int pbase_tree_cache_ix_incr(int ix) +{ + return (ix+1) % ARRAY_SIZE(pbase_tree_cache); +} + +static struct pbase_tree { + struct pbase_tree *next; + /* This is a phony "cache" entry; we are not + * going to evict it nor find it through _get() + * mechanism -- this is for the toplevel node that + * would almost always change with any commit. + */ + struct pbase_tree_cache pcache; +} *pbase_tree; + +static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1) +{ + struct pbase_tree_cache *ent, *nent; + void *data; + unsigned long size; + enum object_type type; + int neigh; + int my_ix = pbase_tree_cache_ix(sha1); + int available_ix = -1; + + /* pbase-tree-cache acts as a limited hashtable. + * your object will be found at your index or within a few + * slots after that slot if it is cached. + */ + for (neigh = 0; neigh < 8; neigh++) { + ent = pbase_tree_cache[my_ix]; + if (ent && !hashcmp(ent->sha1, sha1)) { + ent->ref++; + return ent; + } + else if (((available_ix < 0) && (!ent || !ent->ref)) || + ((0 <= available_ix) && + (!ent && pbase_tree_cache[available_ix]))) + available_ix = my_ix; + if (!ent) + break; + my_ix = pbase_tree_cache_ix_incr(my_ix); + } + + /* Did not find one. Either we got a bogus request or + * we need to read and perhaps cache. + */ + data = read_sha1_file(sha1, &type, &size); + if (!data) + return NULL; + if (type != OBJ_TREE) { + free(data); + return NULL; + } + + /* We need to either cache or return a throwaway copy */ + + if (available_ix < 0) + ent = NULL; + else { + ent = pbase_tree_cache[available_ix]; + my_ix = available_ix; + } + + if (!ent) { + nent = xmalloc(sizeof(*nent)); + nent->temporary = (available_ix < 0); + } + else { + /* evict and reuse */ + free(ent->tree_data); + nent = ent; + } + hashcpy(nent->sha1, sha1); + nent->tree_data = data; + nent->tree_size = size; + nent->ref = 1; + if (!nent->temporary) + pbase_tree_cache[my_ix] = nent; + return nent; +} + +static void pbase_tree_put(struct pbase_tree_cache *cache) +{ + if (!cache->temporary) { + cache->ref--; + return; + } + free(cache->tree_data); + free(cache); +} + +static int name_cmp_len(const char *name) +{ + int i; + for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++) + ; + return i; +} + +static void add_pbase_object(struct tree_desc *tree, + const char *name, + int cmplen, + const char *fullname) +{ + struct name_entry entry; + int cmp; + + while (tree_entry(tree,&entry)) { + if (S_ISGITLINK(entry.mode)) + continue; + cmp = tree_entry_len(&entry) != cmplen ? 1 : + memcmp(name, entry.path, cmplen); + if (cmp > 0) + continue; + if (cmp < 0) + return; + if (name[cmplen] != '/') { + add_object_entry(entry.sha1, + object_type(entry.mode), + fullname, 1); + return; + } + if (S_ISDIR(entry.mode)) { + struct tree_desc sub; + struct pbase_tree_cache *tree; + const char *down = name+cmplen+1; + int downlen = name_cmp_len(down); + + tree = pbase_tree_get(entry.sha1); + if (!tree) + return; + init_tree_desc(&sub, tree->tree_data, tree->tree_size); + + add_pbase_object(&sub, down, downlen, fullname); + pbase_tree_put(tree); + } + } +} + +static unsigned *done_pbase_paths; +static int done_pbase_paths_num; +static int done_pbase_paths_alloc; +static int done_pbase_path_pos(unsigned hash) +{ + int lo = 0; + int hi = done_pbase_paths_num; + while (lo < hi) { + int mi = (hi + lo) / 2; + if (done_pbase_paths[mi] == hash) + return mi; + if (done_pbase_paths[mi] < hash) + hi = mi; + else + lo = mi + 1; + } + return -lo-1; +} + +static int check_pbase_path(unsigned hash) +{ + int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash); + if (0 <= pos) + return 1; + pos = -pos - 1; + if (done_pbase_paths_alloc <= done_pbase_paths_num) { + done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc); + done_pbase_paths = xrealloc(done_pbase_paths, + done_pbase_paths_alloc * + sizeof(unsigned)); + } + done_pbase_paths_num++; + if (pos < done_pbase_paths_num) + memmove(done_pbase_paths + pos + 1, + done_pbase_paths + pos, + (done_pbase_paths_num - pos - 1) * sizeof(unsigned)); + done_pbase_paths[pos] = hash; + return 0; +} + +static void add_preferred_base_object(const char *name) +{ + struct pbase_tree *it; + int cmplen; + unsigned hash = name_hash(name); + + if (!num_preferred_base || check_pbase_path(hash)) + return; + + cmplen = name_cmp_len(name); + for (it = pbase_tree; it; it = it->next) { + if (cmplen == 0) { + add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1); + } + else { + struct tree_desc tree; + init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size); + add_pbase_object(&tree, name, cmplen, name); + } + } +} + +static void add_preferred_base(unsigned char *sha1) +{ + struct pbase_tree *it; + void *data; + unsigned long size; + unsigned char tree_sha1[20]; + + if (window <= num_preferred_base++) + return; + + data = read_object_with_reference(sha1, tree_type, &size, tree_sha1); + if (!data) + return; + + for (it = pbase_tree; it; it = it->next) { + if (!hashcmp(it->pcache.sha1, tree_sha1)) { + free(data); + return; + } + } + + it = xcalloc(1, sizeof(*it)); + it->next = pbase_tree; + pbase_tree = it; + + hashcpy(it->pcache.sha1, tree_sha1); + it->pcache.tree_data = data; + it->pcache.tree_size = size; +} + +static void cleanup_preferred_base(void) +{ + struct pbase_tree *it; + unsigned i; + + it = pbase_tree; + pbase_tree = NULL; + while (it) { + struct pbase_tree *this = it; + it = this->next; + free(this->pcache.tree_data); + free(this); + } + + for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) { + if (!pbase_tree_cache[i]) + continue; + free(pbase_tree_cache[i]->tree_data); + free(pbase_tree_cache[i]); + pbase_tree_cache[i] = NULL; + } + + free(done_pbase_paths); + done_pbase_paths = NULL; + done_pbase_paths_num = done_pbase_paths_alloc = 0; +} + +static void check_object(struct object_entry *entry) +{ + if (entry->in_pack) { + struct packed_git *p = entry->in_pack; + struct pack_window *w_curs = NULL; + const unsigned char *base_ref = NULL; + struct object_entry *base_entry; + unsigned long used, used_0; + unsigned long avail; + off_t ofs; + unsigned char *buf, c; + + buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail); + + /* + * We want in_pack_type even if we do not reuse delta + * since non-delta representations could still be reused. + */ + used = unpack_object_header_buffer(buf, avail, + &entry->in_pack_type, + &entry->size); + if (used == 0) + goto give_up; + + /* + * Determine if this is a delta and if so whether we can + * reuse it or not. Otherwise let's find out as cheaply as + * possible what the actual type and size for this object is. + */ + switch (entry->in_pack_type) { + default: + /* Not a delta hence we've already got all we need. */ + entry->type = entry->in_pack_type; + entry->in_pack_header_size = used; + if (entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB) + goto give_up; + unuse_pack(&w_curs); + return; + case OBJ_REF_DELTA: + if (reuse_delta && !entry->preferred_base) + base_ref = use_pack(p, &w_curs, + entry->in_pack_offset + used, NULL); + entry->in_pack_header_size = used + 20; + break; + case OBJ_OFS_DELTA: + buf = use_pack(p, &w_curs, + entry->in_pack_offset + used, NULL); + used_0 = 0; + c = buf[used_0++]; + ofs = c & 127; + while (c & 128) { + ofs += 1; + if (!ofs || MSB(ofs, 7)) { + error("delta base offset overflow in pack for %s", + sha1_to_hex(entry->idx.sha1)); + goto give_up; + } + c = buf[used_0++]; + ofs = (ofs << 7) + (c & 127); + } + ofs = entry->in_pack_offset - ofs; + if (ofs <= 0 || ofs >= entry->in_pack_offset) { + error("delta base offset out of bound for %s", + sha1_to_hex(entry->idx.sha1)); + goto give_up; + } + if (reuse_delta && !entry->preferred_base) { + struct revindex_entry *revidx; + revidx = find_pack_revindex(p, ofs); + if (!revidx) + goto give_up; + base_ref = nth_packed_object_sha1(p, revidx->nr); + } + entry->in_pack_header_size = used + used_0; + break; + } + + if (base_ref && (base_entry = locate_object_entry(base_ref))) { + /* + * If base_ref was set above that means we wish to + * reuse delta data, and we even found that base + * in the list of objects we want to pack. Goodie! + * + * Depth value does not matter - find_deltas() will + * never consider reused delta as the base object to + * deltify other objects against, in order to avoid + * circular deltas. + */ + entry->type = entry->in_pack_type; + entry->delta = base_entry; + entry->delta_size = entry->size; + entry->delta_sibling = base_entry->delta_child; + base_entry->delta_child = entry; + unuse_pack(&w_curs); + return; + } + + if (entry->type) { + /* + * This must be a delta and we already know what the + * final object type is. Let's extract the actual + * object size from the delta header. + */ + entry->size = get_size_from_delta(p, &w_curs, + entry->in_pack_offset + entry->in_pack_header_size); + if (entry->size == 0) + goto give_up; + unuse_pack(&w_curs); + return; + } + + /* + * No choice but to fall back to the recursive delta walk + * with sha1_object_info() to find about the object type + * at this point... + */ + give_up: + unuse_pack(&w_curs); + } + + entry->type = sha1_object_info(entry->idx.sha1, &entry->size); + /* + * The error condition is checked in prepare_pack(). This is + * to permit a missing preferred base object to be ignored + * as a preferred base. Doing so can result in a larger + * pack file, but the transfer will still take place. + */ +} + +static int pack_offset_sort(const void *_a, const void *_b) +{ + const struct object_entry *a = *(struct object_entry **)_a; + const struct object_entry *b = *(struct object_entry **)_b; + + /* avoid filesystem trashing with loose objects */ + if (!a->in_pack && !b->in_pack) + return hashcmp(a->idx.sha1, b->idx.sha1); + + if (a->in_pack < b->in_pack) + return -1; + if (a->in_pack > b->in_pack) + return 1; + return a->in_pack_offset < b->in_pack_offset ? -1 : + (a->in_pack_offset > b->in_pack_offset); +} + +static void get_object_details(void) +{ + uint32_t i; + struct object_entry **sorted_by_offset; + + sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *)); + for (i = 0; i < nr_objects; i++) + sorted_by_offset[i] = objects + i; + qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort); + + for (i = 0; i < nr_objects; i++) { + struct object_entry *entry = sorted_by_offset[i]; + check_object(entry); + if (big_file_threshold < entry->size) + entry->no_try_delta = 1; + } + + free(sorted_by_offset); +} + +/* + * We search for deltas in a list sorted by type, by filename hash, and then + * by size, so that we see progressively smaller and smaller files. + * That's because we prefer deltas to be from the bigger file + * to the smaller -- deletes are potentially cheaper, but perhaps + * more importantly, the bigger file is likely the more recent + * one. The deepest deltas are therefore the oldest objects which are + * less susceptible to be accessed often. + */ +static int type_size_sort(const void *_a, const void *_b) +{ + const struct object_entry *a = *(struct object_entry **)_a; + const struct object_entry *b = *(struct object_entry **)_b; + + if (a->type > b->type) + return -1; + if (a->type < b->type) + return 1; + if (a->hash > b->hash) + return -1; + if (a->hash < b->hash) + return 1; + if (a->preferred_base > b->preferred_base) + return -1; + if (a->preferred_base < b->preferred_base) + return 1; + if (a->size > b->size) + return -1; + if (a->size < b->size) + return 1; + return a < b ? -1 : (a > b); /* newest first */ +} + +struct unpacked { + struct object_entry *entry; + void *data; + struct delta_index *index; + unsigned depth; +}; + +static int delta_cacheable(unsigned long src_size, unsigned long trg_size, + unsigned long delta_size) +{ + if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size) + return 0; + + if (delta_size < cache_max_small_delta_size) + return 1; + + /* cache delta, if objects are large enough compared to delta size */ + if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) + return 1; + + return 0; +} + +#ifndef NO_PTHREADS + +static pthread_mutex_t read_mutex; +#define read_lock() pthread_mutex_lock(&read_mutex) +#define read_unlock() pthread_mutex_unlock(&read_mutex) + +static pthread_mutex_t cache_mutex; +#define cache_lock() pthread_mutex_lock(&cache_mutex) +#define cache_unlock() pthread_mutex_unlock(&cache_mutex) + +static pthread_mutex_t progress_mutex; +#define progress_lock() pthread_mutex_lock(&progress_mutex) +#define progress_unlock() pthread_mutex_unlock(&progress_mutex) + +#else + +#define read_lock() (void)0 +#define read_unlock() (void)0 +#define cache_lock() (void)0 +#define cache_unlock() (void)0 +#define progress_lock() (void)0 +#define progress_unlock() (void)0 + +#endif + +static int try_delta(struct unpacked *trg, struct unpacked *src, + unsigned max_depth, unsigned long *mem_usage) +{ + struct object_entry *trg_entry = trg->entry; + struct object_entry *src_entry = src->entry; + unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; + unsigned ref_depth; + enum object_type type; + void *delta_buf; + + /* Don't bother doing diffs between different types */ + if (trg_entry->type != src_entry->type) + return -1; + + /* + * We do not bother to try a delta that we discarded on an + * earlier try, but only when reusing delta data. Note that + * src_entry that is marked as the preferred_base should always + * be considered, as even if we produce a suboptimal delta against + * it, we will still save the transfer cost, as we already know + * the other side has it and we won't send src_entry at all. + */ + if (reuse_delta && trg_entry->in_pack && + trg_entry->in_pack == src_entry->in_pack && + !src_entry->preferred_base && + trg_entry->in_pack_type != OBJ_REF_DELTA && + trg_entry->in_pack_type != OBJ_OFS_DELTA) + return 0; + + /* Let's not bust the allowed depth. */ + if (src->depth >= max_depth) + return 0; + + /* Now some size filtering heuristics. */ + trg_size = trg_entry->size; + if (!trg_entry->delta) { + max_size = trg_size/2 - 20; + ref_depth = 1; + } else { + max_size = trg_entry->delta_size; + ref_depth = trg->depth; + } + max_size = (uint64_t)max_size * (max_depth - src->depth) / + (max_depth - ref_depth + 1); + if (max_size == 0) + return 0; + src_size = src_entry->size; + sizediff = src_size < trg_size ? trg_size - src_size : 0; + if (sizediff >= max_size) + return 0; + if (trg_size < src_size / 32) + return 0; + + /* Load data if not already done */ + if (!trg->data) { + read_lock(); + trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz); + read_unlock(); + if (!trg->data) + die("object %s cannot be read", + sha1_to_hex(trg_entry->idx.sha1)); + if (sz != trg_size) + die("object %s inconsistent object length (%lu vs %lu)", + sha1_to_hex(trg_entry->idx.sha1), sz, trg_size); + *mem_usage += sz; + } + if (!src->data) { + read_lock(); + src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz); + read_unlock(); + if (!src->data) { + if (src_entry->preferred_base) { + static int warned = 0; + if (!warned++) + warning("object %s cannot be read", + sha1_to_hex(src_entry->idx.sha1)); + /* + * Those objects are not included in the + * resulting pack. Be resilient and ignore + * them if they can't be read, in case the + * pack could be created nevertheless. + */ + return 0; + } + die("object %s cannot be read", + sha1_to_hex(src_entry->idx.sha1)); + } + if (sz != src_size) + die("object %s inconsistent object length (%lu vs %lu)", + sha1_to_hex(src_entry->idx.sha1), sz, src_size); + *mem_usage += sz; + } + if (!src->index) { + src->index = create_delta_index(src->data, src_size); + if (!src->index) { + static int warned = 0; + if (!warned++) + warning("suboptimal pack - out of memory"); + return 0; + } + *mem_usage += sizeof_delta_index(src->index); + } + + delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); + if (!delta_buf) + return 0; + + if (trg_entry->delta) { + /* Prefer only shallower same-sized deltas. */ + if (delta_size == trg_entry->delta_size && + src->depth + 1 >= trg->depth) { + free(delta_buf); + return 0; + } + } + + /* + * Handle memory allocation outside of the cache + * accounting lock. Compiler will optimize the strangeness + * away when NO_PTHREADS is defined. + */ + free(trg_entry->delta_data); + cache_lock(); + if (trg_entry->delta_data) { + delta_cache_size -= trg_entry->delta_size; + trg_entry->delta_data = NULL; + } + if (delta_cacheable(src_size, trg_size, delta_size)) { + delta_cache_size += delta_size; + cache_unlock(); + trg_entry->delta_data = xrealloc(delta_buf, delta_size); + } else { + cache_unlock(); + free(delta_buf); + } + + trg_entry->delta = src_entry; + trg_entry->delta_size = delta_size; + trg->depth = src->depth + 1; + + return 1; +} + +static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) +{ + struct object_entry *child = me->delta_child; + unsigned int m = n; + while (child) { + unsigned int c = check_delta_limit(child, n + 1); + if (m < c) + m = c; + child = child->delta_sibling; + } + return m; +} + +static unsigned long free_unpacked(struct unpacked *n) +{ + unsigned long freed_mem = sizeof_delta_index(n->index); + free_delta_index(n->index); + n->index = NULL; + if (n->data) { + freed_mem += n->entry->size; + free(n->data); + n->data = NULL; + } + n->entry = NULL; + n->depth = 0; + return freed_mem; +} + +static void find_deltas(struct object_entry **list, unsigned *list_size, + int window, int depth, unsigned *processed) +{ + uint32_t i, idx = 0, count = 0; + struct unpacked *array; + unsigned long mem_usage = 0; + + array = xcalloc(window, sizeof(struct unpacked)); + + for (;;) { + struct object_entry *entry; + struct unpacked *n = array + idx; + int j, max_depth, best_base = -1; + + progress_lock(); + if (!*list_size) { + progress_unlock(); + break; + } + entry = *list++; + (*list_size)--; + if (!entry->preferred_base) { + (*processed)++; + display_progress(progress_state, *processed); + } + progress_unlock(); + + mem_usage -= free_unpacked(n); + n->entry = entry; + + while (window_memory_limit && + mem_usage > window_memory_limit && + count > 1) { + uint32_t tail = (idx + window - count) % window; + mem_usage -= free_unpacked(array + tail); + count--; + } + + /* We do not compute delta to *create* objects we are not + * going to pack. + */ + if (entry->preferred_base) + goto next; + + /* + * If the current object is at pack edge, take the depth the + * objects that depend on the current object into account + * otherwise they would become too deep. + */ + max_depth = depth; + if (entry->delta_child) { + max_depth -= check_delta_limit(entry, 0); + if (max_depth <= 0) + goto next; + } + + j = window; + while (--j > 0) { + int ret; + uint32_t other_idx = idx + j; + struct unpacked *m; + if (other_idx >= window) + other_idx -= window; + m = array + other_idx; + if (!m->entry) + break; + ret = try_delta(n, m, max_depth, &mem_usage); + if (ret < 0) + break; + else if (ret > 0) + best_base = other_idx; + } + + /* + * If we decided to cache the delta data, then it is best + * to compress it right away. First because we have to do + * it anyway, and doing it here while we're threaded will + * save a lot of time in the non threaded write phase, + * as well as allow for caching more deltas within + * the same cache size limit. + * ... + * But only if not writing to stdout, since in that case + * the network is most likely throttling writes anyway, + * and therefore it is best to go to the write phase ASAP + * instead, as we can afford spending more time compressing + * between writes at that moment. + */ + if (entry->delta_data && !pack_to_stdout) { + entry->z_delta_size = do_compress(&entry->delta_data, + entry->delta_size); + cache_lock(); + delta_cache_size -= entry->delta_size; + delta_cache_size += entry->z_delta_size; + cache_unlock(); + } + + /* if we made n a delta, and if n is already at max + * depth, leaving it in the window is pointless. we + * should evict it first. + */ + if (entry->delta && max_depth <= n->depth) + continue; + + /* + * Move the best delta base up in the window, after the + * currently deltified object, to keep it longer. It will + * be the first base object to be attempted next. + */ + if (entry->delta) { + struct unpacked swap = array[best_base]; + int dist = (window + idx - best_base) % window; + int dst = best_base; + while (dist--) { + int src = (dst + 1) % window; + array[dst] = array[src]; + dst = src; + } + array[dst] = swap; + } + + next: + idx++; + if (count + 1 < window) + count++; + if (idx >= window) + idx = 0; + } + + for (i = 0; i < window; ++i) { + free_delta_index(array[i].index); + free(array[i].data); + } + free(array); +} + +#ifndef NO_PTHREADS + +static void try_to_free_from_threads(size_t size) +{ + read_lock(); + release_pack_memory(size, -1); + read_unlock(); +} + +static try_to_free_t old_try_to_free_routine; + +/* + * The main thread waits on the condition that (at least) one of the workers + * has stopped working (which is indicated in the .working member of + * struct thread_params). + * When a work thread has completed its work, it sets .working to 0 and + * signals the main thread and waits on the condition that .data_ready + * becomes 1. + */ + +struct thread_params { + pthread_t thread; + struct object_entry **list; + unsigned list_size; + unsigned remaining; + int window; + int depth; + int working; + int data_ready; + pthread_mutex_t mutex; + pthread_cond_t cond; + unsigned *processed; +}; + +static pthread_cond_t progress_cond; + +/* + * Mutex and conditional variable can't be statically-initialized on Windows. + */ +static void init_threaded_search(void) +{ + init_recursive_mutex(&read_mutex); + pthread_mutex_init(&cache_mutex, NULL); + pthread_mutex_init(&progress_mutex, NULL); + pthread_cond_init(&progress_cond, NULL); + old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads); +} + +static void cleanup_threaded_search(void) +{ + set_try_to_free_routine(old_try_to_free_routine); + pthread_cond_destroy(&progress_cond); + pthread_mutex_destroy(&read_mutex); + pthread_mutex_destroy(&cache_mutex); + pthread_mutex_destroy(&progress_mutex); +} + +static void *threaded_find_deltas(void *arg) +{ + struct thread_params *me = arg; + + while (me->remaining) { + find_deltas(me->list, &me->remaining, + me->window, me->depth, me->processed); + + progress_lock(); + me->working = 0; + pthread_cond_signal(&progress_cond); + progress_unlock(); + + /* + * We must not set ->data_ready before we wait on the + * condition because the main thread may have set it to 1 + * before we get here. In order to be sure that new + * work is available if we see 1 in ->data_ready, it + * was initialized to 0 before this thread was spawned + * and we reset it to 0 right away. + */ + pthread_mutex_lock(&me->mutex); + while (!me->data_ready) + pthread_cond_wait(&me->cond, &me->mutex); + me->data_ready = 0; + pthread_mutex_unlock(&me->mutex); + } + /* leave ->working 1 so that this doesn't get more work assigned */ + return NULL; +} + +static void ll_find_deltas(struct object_entry **list, unsigned list_size, + int window, int depth, unsigned *processed) +{ + struct thread_params *p; + int i, ret, active_threads = 0; + + init_threaded_search(); + + if (!delta_search_threads) /* --threads=0 means autodetect */ + delta_search_threads = online_cpus(); + if (delta_search_threads <= 1) { + find_deltas(list, &list_size, window, depth, processed); + cleanup_threaded_search(); + return; + } + if (progress > pack_to_stdout) + fprintf(stderr, "Delta compression using up to %d threads.\n", + delta_search_threads); + p = xcalloc(delta_search_threads, sizeof(*p)); + + /* Partition the work amongst work threads. */ + for (i = 0; i < delta_search_threads; i++) { + unsigned sub_size = list_size / (delta_search_threads - i); + + /* don't use too small segments or no deltas will be found */ + if (sub_size < 2*window && i+1 < delta_search_threads) + sub_size = 0; + + p[i].window = window; + p[i].depth = depth; + p[i].processed = processed; + p[i].working = 1; + p[i].data_ready = 0; + + /* try to split chunks on "path" boundaries */ + while (sub_size && sub_size < list_size && + list[sub_size]->hash && + list[sub_size]->hash == list[sub_size-1]->hash) + sub_size++; + + p[i].list = list; + p[i].list_size = sub_size; + p[i].remaining = sub_size; + + list += sub_size; + list_size -= sub_size; + } + + /* Start work threads. */ + for (i = 0; i < delta_search_threads; i++) { + if (!p[i].list_size) + continue; + pthread_mutex_init(&p[i].mutex, NULL); + pthread_cond_init(&p[i].cond, NULL); + ret = pthread_create(&p[i].thread, NULL, + threaded_find_deltas, &p[i]); + if (ret) + die("unable to create thread: %s", strerror(ret)); + active_threads++; + } + + /* + * Now let's wait for work completion. Each time a thread is done + * with its work, we steal half of the remaining work from the + * thread with the largest number of unprocessed objects and give + * it to that newly idle thread. This ensure good load balancing + * until the remaining object list segments are simply too short + * to be worth splitting anymore. + */ + while (active_threads) { + struct thread_params *target = NULL; + struct thread_params *victim = NULL; + unsigned sub_size = 0; + + progress_lock(); + for (;;) { + for (i = 0; !target && i < delta_search_threads; i++) + if (!p[i].working) + target = &p[i]; + if (target) + break; + pthread_cond_wait(&progress_cond, &progress_mutex); + } + + for (i = 0; i < delta_search_threads; i++) + if (p[i].remaining > 2*window && + (!victim || victim->remaining < p[i].remaining)) + victim = &p[i]; + if (victim) { + sub_size = victim->remaining / 2; + list = victim->list + victim->list_size - sub_size; + while (sub_size && list[0]->hash && + list[0]->hash == list[-1]->hash) { + list++; + sub_size--; + } + if (!sub_size) { + /* + * It is possible for some "paths" to have + * so many objects that no hash boundary + * might be found. Let's just steal the + * exact half in that case. + */ + sub_size = victim->remaining / 2; + list -= sub_size; + } + target->list = list; + victim->list_size -= sub_size; + victim->remaining -= sub_size; + } + target->list_size = sub_size; + target->remaining = sub_size; + target->working = 1; + progress_unlock(); + + pthread_mutex_lock(&target->mutex); + target->data_ready = 1; + pthread_cond_signal(&target->cond); + pthread_mutex_unlock(&target->mutex); + + if (!sub_size) { + pthread_join(target->thread, NULL); + pthread_cond_destroy(&target->cond); + pthread_mutex_destroy(&target->mutex); + active_threads--; + } + } + cleanup_threaded_search(); + free(p); +} + +#else +#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p) +#endif + +static int add_ref_tag(const char *path, const unsigned char *sha1, int flag, void *cb_data) +{ + unsigned char peeled[20]; + + if (!prefixcmp(path, "refs/tags/") && /* is a tag? */ + !peel_ref(path, peeled) && /* peelable? */ + locate_object_entry(peeled)) /* object packed? */ + add_object_entry(sha1, OBJ_TAG, NULL, 0); + return 0; +} + +static void prepare_pack(int window, int depth) +{ + struct object_entry **delta_list; + uint32_t i, nr_deltas; + unsigned n; + + get_object_details(); + + /* + * If we're locally repacking then we need to be doubly careful + * from now on in order to make sure no stealth corruption gets + * propagated to the new pack. Clients receiving streamed packs + * should validate everything they get anyway so no need to incur + * the additional cost here in that case. + */ + if (!pack_to_stdout) + do_check_packed_object_crc = 1; + + if (!nr_objects || !window || !depth) + return; + + delta_list = xmalloc(nr_objects * sizeof(*delta_list)); + nr_deltas = n = 0; + + for (i = 0; i < nr_objects; i++) { + struct object_entry *entry = objects + i; + + if (entry->delta) + /* This happens if we decided to reuse existing + * delta from a pack. "reuse_delta &&" is implied. + */ + continue; + + if (entry->size < 50) + continue; + + if (entry->no_try_delta) + continue; + + if (!entry->preferred_base) { + nr_deltas++; + if (entry->type < 0) + die("unable to get type of object %s", + sha1_to_hex(entry->idx.sha1)); + } else { + if (entry->type < 0) { + /* + * This object is not found, but we + * don't have to include it anyway. + */ + continue; + } + } + + delta_list[n++] = entry; + } + + if (nr_deltas && n > 1) { + unsigned nr_done = 0; + if (progress) + progress_state = start_progress("Compressing objects", + nr_deltas); + qsort(delta_list, n, sizeof(*delta_list), type_size_sort); + ll_find_deltas(delta_list, n, window+1, depth, &nr_done); + stop_progress(&progress_state); + if (nr_done != nr_deltas) + die("inconsistency with delta count"); + } + free(delta_list); +} + +static int git_pack_config(const char *k, const char *v, void *cb) +{ + if (!strcmp(k, "pack.window")) { + window = git_config_int(k, v); + return 0; + } + if (!strcmp(k, "pack.windowmemory")) { + window_memory_limit = git_config_ulong(k, v); + return 0; + } + if (!strcmp(k, "pack.depth")) { + depth = git_config_int(k, v); + return 0; + } + if (!strcmp(k, "pack.compression")) { + int level = git_config_int(k, v); + if (level == -1) + level = Z_DEFAULT_COMPRESSION; + else if (level < 0 || level > Z_BEST_COMPRESSION) + die("bad pack compression level %d", level); + pack_compression_level = level; + pack_compression_seen = 1; + return 0; + } + if (!strcmp(k, "pack.deltacachesize")) { + max_delta_cache_size = git_config_int(k, v); + return 0; + } + if (!strcmp(k, "pack.deltacachelimit")) { + cache_max_small_delta_size = git_config_int(k, v); + return 0; + } + if (!strcmp(k, "pack.threads")) { + delta_search_threads = git_config_int(k, v); + if (delta_search_threads < 0) + die("invalid number of threads specified (%d)", + delta_search_threads); +#ifdef NO_PTHREADS + if (delta_search_threads != 1) + warning("no threads support, ignoring %s", k); +#endif + return 0; + } + if (!strcmp(k, "pack.indexversion")) { + pack_idx_opts.version = git_config_int(k, v); + if (pack_idx_opts.version > 2) + die("bad pack.indexversion=%"PRIu32, + pack_idx_opts.version); + return 0; + } + return git_default_config(k, v, cb); +} + +static void read_object_list_from_stdin(void) +{ + char line[40 + 1 + PATH_MAX + 2]; + unsigned char sha1[20]; + + for (;;) { + if (!fgets(line, sizeof(line), stdin)) { + if (feof(stdin)) + break; + if (!ferror(stdin)) + die("fgets returned NULL, not EOF, not error!"); + if (errno != EINTR) + die_errno("fgets"); + clearerr(stdin); + continue; + } + if (line[0] == '-') { + if (get_sha1_hex(line+1, sha1)) + die("expected edge sha1, got garbage:\n %s", + line); + add_preferred_base(sha1); + continue; + } + if (get_sha1_hex(line, sha1)) + die("expected sha1, got garbage:\n %s", line); + + add_preferred_base_object(line+41); + add_object_entry(sha1, 0, line+41, 0); + } +} + +#define OBJECT_ADDED (1u<<20) + +static void show_commit(struct commit *commit, void *data) +{ + add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0); + commit->object.flags |= OBJECT_ADDED; +} + +static void show_object(struct object *obj, + const struct name_path *path, const char *last, + void *data) +{ + char *name = path_name(path, last); + + add_preferred_base_object(name); + add_object_entry(obj->sha1, obj->type, name, 0); + obj->flags |= OBJECT_ADDED; + + /* + * We will have generated the hash from the name, + * but not saved a pointer to it - we can free it + */ + free((char *)name); +} + +static void show_edge(struct commit *commit) +{ + add_preferred_base(commit->object.sha1); +} + +struct in_pack_object { + off_t offset; + struct object *object; +}; + +struct in_pack { + int alloc; + int nr; + struct in_pack_object *array; +}; + +static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack) +{ + in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->sha1, p); + in_pack->array[in_pack->nr].object = object; + in_pack->nr++; +} + +/* + * Compare the objects in the offset order, in order to emulate the + * "git rev-list --objects" output that produced the pack originally. + */ +static int ofscmp(const void *a_, const void *b_) +{ + struct in_pack_object *a = (struct in_pack_object *)a_; + struct in_pack_object *b = (struct in_pack_object *)b_; + + if (a->offset < b->offset) + return -1; + else if (a->offset > b->offset) + return 1; + else + return hashcmp(a->object->sha1, b->object->sha1); +} + +static void add_objects_in_unpacked_packs(struct rev_info *revs) +{ + struct packed_git *p; + struct in_pack in_pack; + uint32_t i; + + memset(&in_pack, 0, sizeof(in_pack)); + + for (p = packed_git; p; p = p->next) { + const unsigned char *sha1; + struct object *o; + + if (!p->pack_local || p->pack_keep) + continue; + if (open_pack_index(p)) + die("cannot open pack index"); + + ALLOC_GROW(in_pack.array, + in_pack.nr + p->num_objects, + in_pack.alloc); + + for (i = 0; i < p->num_objects; i++) { + sha1 = nth_packed_object_sha1(p, i); + o = lookup_unknown_object(sha1); + if (!(o->flags & OBJECT_ADDED)) + mark_in_pack_object(o, p, &in_pack); + o->flags |= OBJECT_ADDED; + } + } + + if (in_pack.nr) { + qsort(in_pack.array, in_pack.nr, sizeof(in_pack.array[0]), + ofscmp); + for (i = 0; i < in_pack.nr; i++) { + struct object *o = in_pack.array[i].object; + add_object_entry(o->sha1, o->type, "", 0); + } + } + free(in_pack.array); +} + +static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1) +{ + static struct packed_git *last_found = (void *)1; + struct packed_git *p; + + p = (last_found != (void *)1) ? last_found : packed_git; + + while (p) { + if ((!p->pack_local || p->pack_keep) && + find_pack_entry_one(sha1, p)) { + last_found = p; + return 1; + } + if (p == last_found) + p = packed_git; + else + p = p->next; + if (p == last_found) + p = p->next; + } + return 0; +} + +static void loosen_unused_packed_objects(struct rev_info *revs) +{ + struct packed_git *p; + uint32_t i; + const unsigned char *sha1; + + for (p = packed_git; p; p = p->next) { + if (!p->pack_local || p->pack_keep) + continue; + + if (unpack_unreachable_expiration && + p->mtime < unpack_unreachable_expiration) + continue; + + if (open_pack_index(p)) + die("cannot open pack index"); + + for (i = 0; i < p->num_objects; i++) { + sha1 = nth_packed_object_sha1(p, i); + if (!locate_object_entry(sha1) && + !has_sha1_pack_kept_or_nonlocal(sha1)) + if (force_object_loose(sha1, p->mtime)) + die("unable to force loose object"); + } + } +} + +static void get_object_list(int ac, const char **av) +{ + struct rev_info revs; + char line[1000]; + int flags = 0; + + init_revisions(&revs, NULL); + save_commit_buffer = 0; + setup_revisions(ac, av, &revs, NULL); + + while (fgets(line, sizeof(line), stdin) != NULL) { + int len = strlen(line); + if (len && line[len - 1] == '\n') + line[--len] = 0; + if (!len) + break; + if (*line == '-') { + if (!strcmp(line, "--not")) { + flags ^= UNINTERESTING; + continue; + } + die("not a rev '%s'", line); + } + if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME)) + die("bad revision '%s'", line); + } + + if (prepare_revision_walk(&revs)) + die("revision walk setup failed"); + mark_edges_uninteresting(revs.commits, &revs, show_edge); + traverse_commit_list(&revs, show_commit, show_object, NULL); + + if (keep_unreachable) + add_objects_in_unpacked_packs(&revs); + if (unpack_unreachable) + loosen_unused_packed_objects(&revs); +} + +static int option_parse_index_version(const struct option *opt, + const char *arg, int unset) +{ + char *c; + const char *val = arg; + pack_idx_opts.version = strtoul(val, &c, 10); + if (pack_idx_opts.version > 2) + die(_("unsupported index version %s"), val); + if (*c == ',' && c[1]) + pack_idx_opts.off32_limit = strtoul(c+1, &c, 0); + if (*c || pack_idx_opts.off32_limit & 0x80000000) + die(_("bad index version '%s'"), val); + return 0; +} + +static int option_parse_unpack_unreachable(const struct option *opt, + const char *arg, int unset) +{ + if (unset) { + unpack_unreachable = 0; + unpack_unreachable_expiration = 0; + } + else { + unpack_unreachable = 1; + if (arg) + unpack_unreachable_expiration = approxidate(arg); + } + return 0; +} + +static int option_parse_ulong(const struct option *opt, + const char *arg, int unset) +{ + if (unset) + die(_("option %s does not accept negative form"), + opt->long_name); + + if (!git_parse_ulong(arg, opt->value)) + die(_("unable to parse value '%s' for option %s"), + arg, opt->long_name); + return 0; +} + +#define OPT_ULONG(s, l, v, h) \ + { OPTION_CALLBACK, (s), (l), (v), "n", (h), \ + PARSE_OPT_NONEG, option_parse_ulong } + +int cmd_pack_objects(int argc, const char **argv, const char *prefix) +{ + int use_internal_rev_list = 0; + int thin = 0; + int all_progress_implied = 0; + const char *rp_av[6]; + int rp_ac = 0; + int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0; + struct option pack_objects_options[] = { + OPT_SET_INT('q', "quiet", &progress, + N_("do not show progress meter"), 0), + OPT_SET_INT(0, "progress", &progress, + N_("show progress meter"), 1), + OPT_SET_INT(0, "all-progress", &progress, + N_("show progress meter during object writing phase"), 2), + OPT_BOOL(0, "all-progress-implied", + &all_progress_implied, + N_("similar to --all-progress when progress meter is shown")), + { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"), + N_("write the pack index file in the specified idx format version"), + 0, option_parse_index_version }, + OPT_ULONG(0, "max-pack-size", &pack_size_limit, + N_("maximum size of each output pack file")), + OPT_BOOL(0, "local", &local, + N_("ignore borrowed objects from alternate object store")), + OPT_BOOL(0, "incremental", &incremental, + N_("ignore packed objects")), + OPT_INTEGER(0, "window", &window, + N_("limit pack window by objects")), + OPT_ULONG(0, "window-memory", &window_memory_limit, + N_("limit pack window by memory in addition to object limit")), + OPT_INTEGER(0, "depth", &depth, + N_("maximum length of delta chain allowed in the resulting pack")), + OPT_BOOL(0, "reuse-delta", &reuse_delta, + N_("reuse existing deltas")), + OPT_BOOL(0, "reuse-object", &reuse_object, + N_("reuse existing objects")), + OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta, + N_("use OFS_DELTA objects")), + OPT_INTEGER(0, "threads", &delta_search_threads, + N_("use threads when searching for best delta matches")), + OPT_BOOL(0, "non-empty", &non_empty, + N_("do not create an empty pack output")), + OPT_BOOL(0, "revs", &use_internal_rev_list, + N_("read revision arguments from standard input")), + { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL, + N_("limit the objects to those that are not yet packed"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, + { OPTION_SET_INT, 0, "all", &rev_list_all, NULL, + N_("include objects reachable from any reference"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, + { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL, + N_("include objects referred by reflog entries"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, + OPT_BOOL(0, "stdout", &pack_to_stdout, + N_("output pack to stdout")), + OPT_BOOL(0, "include-tag", &include_tag, + N_("include tag objects that refer to objects to be packed")), + OPT_BOOL(0, "keep-unreachable", &keep_unreachable, + N_("keep unreachable objects")), + { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"), + N_("unpack unreachable objects newer than <time>"), + PARSE_OPT_OPTARG, option_parse_unpack_unreachable }, + OPT_BOOL(0, "thin", &thin, + N_("create thin packs")), + OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep, + N_("ignore packs that have companion .keep file")), + OPT_INTEGER(0, "compression", &pack_compression_level, + N_("pack compression level")), + OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents, + N_("do not hide commits by grafts"), 0), + OPT_END(), + }; + + read_replace_refs = 0; + + reset_pack_idx_option(&pack_idx_opts); + git_config(git_pack_config, NULL); + if (!pack_compression_seen && core_compression_seen) + pack_compression_level = core_compression_level; + + progress = isatty(2); + argc = parse_options(argc, argv, prefix, pack_objects_options, + pack_usage, 0); + + if (argc) { + base_name = argv[0]; + argc--; + } + if (pack_to_stdout != !base_name || argc) + usage_with_options(pack_usage, pack_objects_options); + + rp_av[rp_ac++] = "pack-objects"; + if (thin) { + use_internal_rev_list = 1; + rp_av[rp_ac++] = "--objects-edge"; + } else + rp_av[rp_ac++] = "--objects"; + + if (rev_list_all) { + use_internal_rev_list = 1; + rp_av[rp_ac++] = "--all"; + } + if (rev_list_reflog) { + use_internal_rev_list = 1; + rp_av[rp_ac++] = "--reflog"; + } + if (rev_list_unpacked) { + use_internal_rev_list = 1; + rp_av[rp_ac++] = "--unpacked"; + } + + if (!reuse_object) + reuse_delta = 0; + if (pack_compression_level == -1) + pack_compression_level = Z_DEFAULT_COMPRESSION; + else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION) + die("bad pack compression level %d", pack_compression_level); +#ifdef NO_PTHREADS + if (delta_search_threads != 1) + warning("no threads support, ignoring --threads"); +#endif + if (!pack_to_stdout && !pack_size_limit) + pack_size_limit = pack_size_limit_cfg; + if (pack_to_stdout && pack_size_limit) + die("--max-pack-size cannot be used to build a pack for transfer."); + if (pack_size_limit && pack_size_limit < 1024*1024) { + warning("minimum pack size limit is 1 MiB"); + pack_size_limit = 1024*1024; + } + + if (!pack_to_stdout && thin) + die("--thin cannot be used to build an indexable pack."); + + if (keep_unreachable && unpack_unreachable) + die("--keep-unreachable and --unpack-unreachable are incompatible."); + + if (progress && all_progress_implied) + progress = 2; + + prepare_packed_git(); + + if (progress) + progress_state = start_progress("Counting objects", 0); + if (!use_internal_rev_list) + read_object_list_from_stdin(); + else { + rp_av[rp_ac] = NULL; + get_object_list(rp_ac, rp_av); + } + cleanup_preferred_base(); + if (include_tag && nr_result) + for_each_ref(add_ref_tag, NULL); + stop_progress(&progress_state); + + if (non_empty && !nr_result) + return 0; + if (nr_result) + prepare_pack(window, depth); + write_pack_file(); + if (progress) + fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32")," + " reused %"PRIu32" (delta %"PRIu32")\n", + written, written_delta, reused, reused_delta); + return 0; +} diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c new file mode 100644 index 0000000000..649c3aaa93 --- /dev/null +++ b/builtin/pack-redundant.c @@ -0,0 +1,695 @@ +/* +* +* Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se> +* +* This file is licensed under the GPL v2. +* +*/ + +#include "builtin.h" + +#define BLKSIZE 512 + +static const char pack_redundant_usage[] = +"git pack-redundant [ --verbose ] [ --alt-odb ] < --all | <.pack filename> ...>"; + +static int load_all_packs, verbose, alt_odb; + +struct llist_item { + struct llist_item *next; + const unsigned char *sha1; +}; +static struct llist { + struct llist_item *front; + struct llist_item *back; + size_t size; +} *all_objects; /* all objects which must be present in local packfiles */ + +static struct pack_list { + struct pack_list *next; + struct packed_git *pack; + struct llist *unique_objects; + struct llist *all_objects; +} *local_packs = NULL, *altodb_packs = NULL; + +struct pll { + struct pll *next; + struct pack_list *pl; +}; + +static struct llist_item *free_nodes; + +static inline void llist_item_put(struct llist_item *item) +{ + item->next = free_nodes; + free_nodes = item; +} + +static inline struct llist_item *llist_item_get(void) +{ + struct llist_item *new; + if ( free_nodes ) { + new = free_nodes; + free_nodes = free_nodes->next; + } else { + int i = 1; + new = xmalloc(sizeof(struct llist_item) * BLKSIZE); + for (; i < BLKSIZE; i++) + llist_item_put(&new[i]); + } + return new; +} + +static void llist_free(struct llist *list) +{ + while ((list->back = list->front)) { + list->front = list->front->next; + llist_item_put(list->back); + } + free(list); +} + +static inline void llist_init(struct llist **list) +{ + *list = xmalloc(sizeof(struct llist)); + (*list)->front = (*list)->back = NULL; + (*list)->size = 0; +} + +static struct llist * llist_copy(struct llist *list) +{ + struct llist *ret; + struct llist_item *new, *old, *prev; + + llist_init(&ret); + + if ((ret->size = list->size) == 0) + return ret; + + new = ret->front = llist_item_get(); + new->sha1 = list->front->sha1; + + old = list->front->next; + while (old) { + prev = new; + new = llist_item_get(); + prev->next = new; + new->sha1 = old->sha1; + old = old->next; + } + new->next = NULL; + ret->back = new; + + return ret; +} + +static inline struct llist_item *llist_insert(struct llist *list, + struct llist_item *after, + const unsigned char *sha1) +{ + struct llist_item *new = llist_item_get(); + new->sha1 = sha1; + new->next = NULL; + + if (after != NULL) { + new->next = after->next; + after->next = new; + if (after == list->back) + list->back = new; + } else {/* insert in front */ + if (list->size == 0) + list->back = new; + else + new->next = list->front; + list->front = new; + } + list->size++; + return new; +} + +static inline struct llist_item *llist_insert_back(struct llist *list, + const unsigned char *sha1) +{ + return llist_insert(list, list->back, sha1); +} + +static inline struct llist_item *llist_insert_sorted_unique(struct llist *list, + const unsigned char *sha1, struct llist_item *hint) +{ + struct llist_item *prev = NULL, *l; + + l = (hint == NULL) ? list->front : hint; + while (l) { + int cmp = hashcmp(l->sha1, sha1); + if (cmp > 0) { /* we insert before this entry */ + return llist_insert(list, prev, sha1); + } + if (!cmp) { /* already exists */ + return l; + } + prev = l; + l = l->next; + } + /* insert at the end */ + return llist_insert_back(list, sha1); +} + +/* returns a pointer to an item in front of sha1 */ +static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *sha1, struct llist_item *hint) +{ + struct llist_item *prev, *l; + +redo_from_start: + l = (hint == NULL) ? list->front : hint; + prev = NULL; + while (l) { + int cmp = hashcmp(l->sha1, sha1); + if (cmp > 0) /* not in list, since sorted */ + return prev; + if (!cmp) { /* found */ + if (prev == NULL) { + if (hint != NULL && hint != list->front) { + /* we don't know the previous element */ + hint = NULL; + goto redo_from_start; + } + list->front = l->next; + } else + prev->next = l->next; + if (l == list->back) + list->back = prev; + llist_item_put(l); + list->size--; + return prev; + } + prev = l; + l = l->next; + } + return prev; +} + +/* computes A\B */ +static void llist_sorted_difference_inplace(struct llist *A, + struct llist *B) +{ + struct llist_item *hint, *b; + + hint = NULL; + b = B->front; + + while (b) { + hint = llist_sorted_remove(A, b->sha1, hint); + b = b->next; + } +} + +static inline struct pack_list * pack_list_insert(struct pack_list **pl, + struct pack_list *entry) +{ + struct pack_list *p = xmalloc(sizeof(struct pack_list)); + memcpy(p, entry, sizeof(struct pack_list)); + p->next = *pl; + *pl = p; + return p; +} + +static inline size_t pack_list_size(struct pack_list *pl) +{ + size_t ret = 0; + while (pl) { + ret++; + pl = pl->next; + } + return ret; +} + +static struct pack_list * pack_list_difference(const struct pack_list *A, + const struct pack_list *B) +{ + struct pack_list *ret; + const struct pack_list *pl; + + if (A == NULL) + return NULL; + + pl = B; + while (pl != NULL) { + if (A->pack == pl->pack) + return pack_list_difference(A->next, B); + pl = pl->next; + } + ret = xmalloc(sizeof(struct pack_list)); + memcpy(ret, A, sizeof(struct pack_list)); + ret->next = pack_list_difference(A->next, B); + return ret; +} + +static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2) +{ + unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step; + const unsigned char *p1_base, *p2_base; + struct llist_item *p1_hint = NULL, *p2_hint = NULL; + + p1_base = p1->pack->index_data; + p2_base = p2->pack->index_data; + p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8); + p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8); + p1_step = (p1->pack->index_version < 2) ? 24 : 20; + p2_step = (p2->pack->index_version < 2) ? 24 : 20; + + while (p1_off < p1->pack->num_objects * p1_step && + p2_off < p2->pack->num_objects * p2_step) + { + int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); + /* cmp ~ p1 - p2 */ + if (cmp == 0) { + p1_hint = llist_sorted_remove(p1->unique_objects, + p1_base + p1_off, p1_hint); + p2_hint = llist_sorted_remove(p2->unique_objects, + p1_base + p1_off, p2_hint); + p1_off += p1_step; + p2_off += p2_step; + continue; + } + if (cmp < 0) { /* p1 has the object, p2 doesn't */ + p1_off += p1_step; + } else { /* p2 has the object, p1 doesn't */ + p2_off += p2_step; + } + } +} + +static void pll_free(struct pll *l) +{ + struct pll *old; + struct pack_list *opl; + + while (l) { + old = l; + while (l->pl) { + opl = l->pl; + l->pl = opl->next; + free(opl); + } + l = l->next; + free(old); + } +} + +/* all the permutations have to be free()d at the same time, + * since they refer to each other + */ +static struct pll * get_permutations(struct pack_list *list, int n) +{ + struct pll *subset, *ret = NULL, *new_pll = NULL; + + if (list == NULL || pack_list_size(list) < n || n == 0) + return NULL; + + if (n == 1) { + while (list) { + new_pll = xmalloc(sizeof(*new_pll)); + new_pll->pl = NULL; + pack_list_insert(&new_pll->pl, list); + new_pll->next = ret; + ret = new_pll; + list = list->next; + } + return ret; + } + + while (list->next) { + subset = get_permutations(list->next, n - 1); + while (subset) { + new_pll = xmalloc(sizeof(*new_pll)); + new_pll->pl = subset->pl; + pack_list_insert(&new_pll->pl, list); + new_pll->next = ret; + ret = new_pll; + subset = subset->next; + } + list = list->next; + } + return ret; +} + +static int is_superset(struct pack_list *pl, struct llist *list) +{ + struct llist *diff; + + diff = llist_copy(list); + + while (pl) { + llist_sorted_difference_inplace(diff, pl->all_objects); + if (diff->size == 0) { /* we're done */ + llist_free(diff); + return 1; + } + pl = pl->next; + } + llist_free(diff); + return 0; +} + +static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2) +{ + size_t ret = 0; + unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step; + const unsigned char *p1_base, *p2_base; + + p1_base = p1->index_data; + p2_base = p2->index_data; + p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8); + p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8); + p1_step = (p1->index_version < 2) ? 24 : 20; + p2_step = (p2->index_version < 2) ? 24 : 20; + + while (p1_off < p1->num_objects * p1_step && + p2_off < p2->num_objects * p2_step) + { + int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); + /* cmp ~ p1 - p2 */ + if (cmp == 0) { + ret++; + p1_off += p1_step; + p2_off += p2_step; + continue; + } + if (cmp < 0) { /* p1 has the object, p2 doesn't */ + p1_off += p1_step; + } else { /* p2 has the object, p1 doesn't */ + p2_off += p2_step; + } + } + return ret; +} + +/* another O(n^2) function ... */ +static size_t get_pack_redundancy(struct pack_list *pl) +{ + struct pack_list *subset; + size_t ret = 0; + + if (pl == NULL) + return 0; + + while ((subset = pl->next)) { + while (subset) { + ret += sizeof_union(pl->pack, subset->pack); + subset = subset->next; + } + pl = pl->next; + } + return ret; +} + +static inline off_t pack_set_bytecount(struct pack_list *pl) +{ + off_t ret = 0; + while (pl) { + ret += pl->pack->pack_size; + ret += pl->pack->index_size; + pl = pl->next; + } + return ret; +} + +static void minimize(struct pack_list **min) +{ + struct pack_list *pl, *unique = NULL, + *non_unique = NULL, *min_perm = NULL; + struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm; + struct llist *missing; + off_t min_perm_size = 0, perm_size; + int n; + + pl = local_packs; + while (pl) { + if (pl->unique_objects->size) + pack_list_insert(&unique, pl); + else + pack_list_insert(&non_unique, pl); + pl = pl->next; + } + /* find out which objects are missing from the set of unique packs */ + missing = llist_copy(all_objects); + pl = unique; + while (pl) { + llist_sorted_difference_inplace(missing, pl->all_objects); + pl = pl->next; + } + + /* return if there are no objects missing from the unique set */ + if (missing->size == 0) { + *min = unique; + return; + } + + /* find the permutations which contain all missing objects */ + for (n = 1; n <= pack_list_size(non_unique) && !perm_ok; n++) { + perm_all = perm = get_permutations(non_unique, n); + while (perm) { + if (is_superset(perm->pl, missing)) { + new_perm = xmalloc(sizeof(struct pll)); + memcpy(new_perm, perm, sizeof(struct pll)); + new_perm->next = perm_ok; + perm_ok = new_perm; + } + perm = perm->next; + } + if (perm_ok) + break; + pll_free(perm_all); + } + if (perm_ok == NULL) + die("Internal error: No complete sets found!"); + + /* find the permutation with the smallest size */ + perm = perm_ok; + while (perm) { + perm_size = pack_set_bytecount(perm->pl); + if (!min_perm_size || min_perm_size > perm_size) { + min_perm_size = perm_size; + min_perm = perm->pl; + } + perm = perm->next; + } + *min = min_perm; + /* add the unique packs to the list */ + pl = unique; + while (pl) { + pack_list_insert(min, pl); + pl = pl->next; + } +} + +static void load_all_objects(void) +{ + struct pack_list *pl = local_packs; + struct llist_item *hint, *l; + + llist_init(&all_objects); + + while (pl) { + hint = NULL; + l = pl->all_objects->front; + while (l) { + hint = llist_insert_sorted_unique(all_objects, + l->sha1, hint); + l = l->next; + } + pl = pl->next; + } + /* remove objects present in remote packs */ + pl = altodb_packs; + while (pl) { + llist_sorted_difference_inplace(all_objects, pl->all_objects); + pl = pl->next; + } +} + +/* this scales like O(n^2) */ +static void cmp_local_packs(void) +{ + struct pack_list *subset, *pl = local_packs; + + while ((subset = pl)) { + while ((subset = subset->next)) + cmp_two_packs(pl, subset); + pl = pl->next; + } +} + +static void scan_alt_odb_packs(void) +{ + struct pack_list *local, *alt; + + alt = altodb_packs; + while (alt) { + local = local_packs; + while (local) { + llist_sorted_difference_inplace(local->unique_objects, + alt->all_objects); + local = local->next; + } + llist_sorted_difference_inplace(all_objects, alt->all_objects); + alt = alt->next; + } +} + +static struct pack_list * add_pack(struct packed_git *p) +{ + struct pack_list l; + unsigned long off = 0, step; + const unsigned char *base; + + if (!p->pack_local && !(alt_odb || verbose)) + return NULL; + + l.pack = p; + llist_init(&l.all_objects); + + if (open_pack_index(p)) + return NULL; + + base = p->index_data; + base += 256 * 4 + ((p->index_version < 2) ? 4 : 8); + step = (p->index_version < 2) ? 24 : 20; + while (off < p->num_objects * step) { + llist_insert_back(l.all_objects, base + off); + off += step; + } + /* this list will be pruned in cmp_two_packs later */ + l.unique_objects = llist_copy(l.all_objects); + if (p->pack_local) + return pack_list_insert(&local_packs, &l); + else + return pack_list_insert(&altodb_packs, &l); +} + +static struct pack_list * add_pack_file(const char *filename) +{ + struct packed_git *p = packed_git; + + if (strlen(filename) < 40) + die("Bad pack filename: %s", filename); + + while (p) { + if (strstr(p->pack_name, filename)) + return add_pack(p); + p = p->next; + } + die("Filename %s not found in packed_git", filename); +} + +static void load_all(void) +{ + struct packed_git *p = packed_git; + + while (p) { + add_pack(p); + p = p->next; + } +} + +int cmd_pack_redundant(int argc, const char **argv, const char *prefix) +{ + int i; + struct pack_list *min, *red, *pl; + struct llist *ignore; + unsigned char *sha1; + char buf[42]; /* 40 byte sha1 + \n + \0 */ + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage(pack_redundant_usage); + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (!strcmp(arg, "--")) { + i++; + break; + } + if (!strcmp(arg, "--all")) { + load_all_packs = 1; + continue; + } + if (!strcmp(arg, "--verbose")) { + verbose = 1; + continue; + } + if (!strcmp(arg, "--alt-odb")) { + alt_odb = 1; + continue; + } + if (*arg == '-') + usage(pack_redundant_usage); + else + break; + } + + prepare_packed_git(); + + if (load_all_packs) + load_all(); + else + while (*(argv + i) != NULL) + add_pack_file(*(argv + i++)); + + if (local_packs == NULL) + die("Zero packs found!"); + + load_all_objects(); + + cmp_local_packs(); + if (alt_odb) + scan_alt_odb_packs(); + + /* ignore objects given on stdin */ + llist_init(&ignore); + if (!isatty(0)) { + while (fgets(buf, sizeof(buf), stdin)) { + sha1 = xmalloc(20); + if (get_sha1_hex(buf, sha1)) + die("Bad sha1 on stdin: %s", buf); + llist_insert_sorted_unique(ignore, sha1, NULL); + } + } + llist_sorted_difference_inplace(all_objects, ignore); + pl = local_packs; + while (pl) { + llist_sorted_difference_inplace(pl->unique_objects, ignore); + pl = pl->next; + } + + minimize(&min); + + if (verbose) { + fprintf(stderr, "There are %lu packs available in alt-odbs.\n", + (unsigned long)pack_list_size(altodb_packs)); + fprintf(stderr, "The smallest (bytewise) set of packs is:\n"); + pl = min; + while (pl) { + fprintf(stderr, "\t%s\n", pl->pack->pack_name); + pl = pl->next; + } + fprintf(stderr, "containing %lu duplicate objects " + "with a total size of %lukb.\n", + (unsigned long)get_pack_redundancy(min), + (unsigned long)pack_set_bytecount(min)/1024); + fprintf(stderr, "A total of %lu unique objects were considered.\n", + (unsigned long)all_objects->size); + fprintf(stderr, "Redundant packs (with indexes):\n"); + } + pl = red = pack_list_difference(local_packs, min); + while (pl) { + printf("%s\n%s\n", + sha1_pack_index_name(pl->pack->sha1), + pl->pack->pack_name); + pl = pl->next; + } + if (verbose) + fprintf(stderr, "%luMB of redundant packs in total.\n", + (unsigned long)pack_set_bytecount(red)/(1024*1024)); + + return 0; +} diff --git a/builtin/pack-refs.c b/builtin/pack-refs.c new file mode 100644 index 0000000000..b5a0f88eb8 --- /dev/null +++ b/builtin/pack-refs.c @@ -0,0 +1,21 @@ +#include "builtin.h" +#include "parse-options.h" +#include "pack-refs.h" + +static char const * const pack_refs_usage[] = { + N_("git pack-refs [options]"), + NULL +}; + +int cmd_pack_refs(int argc, const char **argv, const char *prefix) +{ + unsigned int flags = PACK_REFS_PRUNE; + struct option opts[] = { + OPT_BIT(0, "all", &flags, N_("pack everything"), PACK_REFS_ALL), + OPT_BIT(0, "prune", &flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE), + OPT_END(), + }; + if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0)) + usage_with_options(pack_refs_usage, opts); + return pack_refs(flags); +} diff --git a/builtin/patch-id.c b/builtin/patch-id.c new file mode 100644 index 0000000000..3cfe02d5a5 --- /dev/null +++ b/builtin/patch-id.c @@ -0,0 +1,157 @@ +#include "builtin.h" + +static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c) +{ + unsigned char result[20]; + char name[50]; + + if (!patchlen) + return; + + git_SHA1_Final(result, c); + memcpy(name, sha1_to_hex(id), 41); + printf("%s %s\n", sha1_to_hex(result), name); + git_SHA1_Init(c); +} + +static int remove_space(char *line) +{ + char *src = line; + char *dst = line; + unsigned char c; + + while ((c = *src++) != '\0') { + if (!isspace(c)) + *dst++ = c; + } + return dst - line; +} + +static int scan_hunk_header(const char *p, int *p_before, int *p_after) +{ + static const char digits[] = "0123456789"; + const char *q, *r; + int n; + + q = p + 4; + n = strspn(q, digits); + if (q[n] == ',') { + q += n + 1; + n = strspn(q, digits); + } + if (n == 0 || q[n] != ' ' || q[n+1] != '+') + return 0; + + r = q + n + 2; + n = strspn(r, digits); + if (r[n] == ',') { + r += n + 1; + n = strspn(r, digits); + } + if (n == 0) + return 0; + + *p_before = atoi(q); + *p_after = atoi(r); + return 1; +} + +static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx, struct strbuf *line_buf) +{ + int patchlen = 0, found_next = 0; + int before = -1, after = -1; + + while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) { + char *line = line_buf->buf; + char *p = line; + int len; + + if (!memcmp(line, "diff-tree ", 10)) + p += 10; + else if (!memcmp(line, "commit ", 7)) + p += 7; + else if (!memcmp(line, "From ", 5)) + p += 5; + else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line)) + continue; + + if (!get_sha1_hex(p, next_sha1)) { + found_next = 1; + break; + } + + /* Ignore commit comments */ + if (!patchlen && memcmp(line, "diff ", 5)) + continue; + + /* Parsing diff header? */ + if (before == -1) { + if (!memcmp(line, "index ", 6)) + continue; + else if (!memcmp(line, "--- ", 4)) + before = after = 1; + else if (!isalpha(line[0])) + break; + } + + /* Looking for a valid hunk header? */ + if (before == 0 && after == 0) { + if (!memcmp(line, "@@ -", 4)) { + /* Parse next hunk, but ignore line numbers. */ + scan_hunk_header(line, &before, &after); + continue; + } + + /* Split at the end of the patch. */ + if (memcmp(line, "diff ", 5)) + break; + + /* Else we're parsing another header. */ + before = after = -1; + } + + /* If we get here, we're inside a hunk. */ + if (line[0] == '-' || line[0] == ' ') + before--; + if (line[0] == '+' || line[0] == ' ') + after--; + + /* Compute the sha without whitespace */ + len = remove_space(line); + patchlen += len; + git_SHA1_Update(ctx, line, len); + } + + if (!found_next) + hashclr(next_sha1); + + return patchlen; +} + +static void generate_id_list(void) +{ + unsigned char sha1[20], n[20]; + git_SHA_CTX ctx; + int patchlen; + struct strbuf line_buf = STRBUF_INIT; + + git_SHA1_Init(&ctx); + hashclr(sha1); + while (!feof(stdin)) { + patchlen = get_one_patchid(n, &ctx, &line_buf); + flush_current_id(patchlen, sha1, &ctx); + hashcpy(sha1, n); + } + strbuf_release(&line_buf); +} + +static const char patch_id_usage[] = "git patch-id < patch"; + +int cmd_patch_id(int argc, const char **argv, const char *prefix) +{ + if (argc != 1) + usage(patch_id_usage); + + generate_id_list(); + return 0; +} diff --git a/builtin/prune-packed.c b/builtin/prune-packed.c new file mode 100644 index 0000000000..83382c1fe1 --- /dev/null +++ b/builtin/prune-packed.c @@ -0,0 +1,86 @@ +#include "builtin.h" +#include "cache.h" +#include "progress.h" +#include "parse-options.h" + +static const char * const prune_packed_usage[] = { + N_("git prune-packed [-n|--dry-run] [-q|--quiet]"), + NULL +}; + +#define DRY_RUN 01 +#define VERBOSE 02 + +static struct progress *progress; + +static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts) +{ + struct dirent *de; + char hex[40]; + + sprintf(hex, "%02x", i); + while ((de = readdir(dir)) != NULL) { + unsigned char sha1[20]; + if (strlen(de->d_name) != 38) + continue; + memcpy(hex+2, de->d_name, 38); + if (get_sha1_hex(hex, sha1)) + continue; + if (!has_sha1_pack(sha1)) + continue; + memcpy(pathname + len, de->d_name, 38); + if (opts & DRY_RUN) + printf("rm -f %s\n", pathname); + else + unlink_or_warn(pathname); + display_progress(progress, i + 1); + } +} + +void prune_packed_objects(int opts) +{ + int i; + static char pathname[PATH_MAX]; + const char *dir = get_object_directory(); + int len = strlen(dir); + + if (opts == VERBOSE) + progress = start_progress_delay("Removing duplicate objects", + 256, 95, 2); + + if (len > PATH_MAX - 42) + die("impossible object directory"); + memcpy(pathname, dir, len); + if (len && pathname[len-1] != '/') + pathname[len++] = '/'; + for (i = 0; i < 256; i++) { + DIR *d; + + display_progress(progress, i + 1); + sprintf(pathname + len, "%02x/", i); + d = opendir(pathname); + if (!d) + continue; + prune_dir(i, d, pathname, len + 3, opts); + closedir(d); + pathname[len + 2] = '\0'; + rmdir(pathname); + } + stop_progress(&progress); +} + +int cmd_prune_packed(int argc, const char **argv, const char *prefix) +{ + int opts = isatty(2) ? VERBOSE : 0; + const struct option prune_packed_options[] = { + OPT_BIT('n', "dry-run", &opts, N_("dry run"), DRY_RUN), + OPT_NEGBIT('q', "quiet", &opts, N_("be quiet"), VERBOSE), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, prune_packed_options, + prune_packed_usage, 0); + + prune_packed_objects(opts); + return 0; +} diff --git a/builtin/prune.c b/builtin/prune.c new file mode 100644 index 0000000000..8cb8b9186a --- /dev/null +++ b/builtin/prune.c @@ -0,0 +1,176 @@ +#include "cache.h" +#include "commit.h" +#include "diff.h" +#include "revision.h" +#include "builtin.h" +#include "reachable.h" +#include "parse-options.h" +#include "progress.h" +#include "dir.h" + +static const char * const prune_usage[] = { + N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"), + NULL +}; +static int show_only; +static int verbose; +static unsigned long expire; +static int show_progress = -1; + +static int prune_tmp_object(const char *path, const char *filename) +{ + const char *fullpath = mkpath("%s/%s", path, filename); + struct stat st; + if (lstat(fullpath, &st)) + return error("Could not stat '%s'", fullpath); + if (st.st_mtime > expire) + return 0; + if (show_only || verbose) + printf("Removing stale temporary file %s\n", fullpath); + if (!show_only) + unlink_or_warn(fullpath); + return 0; +} + +static int prune_object(char *path, const char *filename, const unsigned char *sha1) +{ + const char *fullpath = mkpath("%s/%s", path, filename); + struct stat st; + if (lstat(fullpath, &st)) + return error("Could not stat '%s'", fullpath); + if (st.st_mtime > expire) + return 0; + if (show_only || verbose) { + enum object_type type = sha1_object_info(sha1, NULL); + printf("%s %s\n", sha1_to_hex(sha1), + (type > 0) ? typename(type) : "unknown"); + } + if (!show_only) + unlink_or_warn(fullpath); + return 0; +} + +static int prune_dir(int i, char *path) +{ + DIR *dir = opendir(path); + struct dirent *de; + + if (!dir) + return 0; + + while ((de = readdir(dir)) != NULL) { + char name[100]; + unsigned char sha1[20]; + + if (is_dot_or_dotdot(de->d_name)) + continue; + if (strlen(de->d_name) == 38) { + sprintf(name, "%02x", i); + memcpy(name+2, de->d_name, 39); + if (get_sha1_hex(name, sha1) < 0) + break; + + /* + * Do we know about this object? + * It must have been reachable + */ + if (lookup_object(sha1)) + continue; + + prune_object(path, de->d_name, sha1); + continue; + } + if (!prefixcmp(de->d_name, "tmp_obj_")) { + prune_tmp_object(path, de->d_name); + continue; + } + fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); + } + closedir(dir); + if (!show_only) + rmdir(path); + return 0; +} + +static void prune_object_dir(const char *path) +{ + int i; + for (i = 0; i < 256; i++) { + static char dir[4096]; + sprintf(dir, "%s/%02x", path, i); + prune_dir(i, dir); + } +} + +/* + * Write errors (particularly out of space) can result in + * failed temporary packs (and more rarely indexes and other + * files beginning with "tmp_") accumulating in the object + * and the pack directories. + */ +static void remove_temporary_files(const char *path) +{ + DIR *dir; + struct dirent *de; + + dir = opendir(path); + if (!dir) { + fprintf(stderr, "Unable to open directory %s\n", path); + return; + } + while ((de = readdir(dir)) != NULL) + if (!prefixcmp(de->d_name, "tmp_")) + prune_tmp_object(path, de->d_name); + closedir(dir); +} + +int cmd_prune(int argc, const char **argv, const char *prefix) +{ + struct rev_info revs; + struct progress *progress = NULL; + const struct option options[] = { + OPT__DRY_RUN(&show_only, N_("do not remove, show only")), + OPT__VERBOSE(&verbose, N_("report pruned objects")), + OPT_BOOL(0, "progress", &show_progress, N_("show progress")), + OPT_DATE(0, "expire", &expire, + N_("expire objects older than <time>")), + OPT_END() + }; + char *s; + + expire = ULONG_MAX; + save_commit_buffer = 0; + read_replace_refs = 0; + init_revisions(&revs, prefix); + + argc = parse_options(argc, argv, prefix, options, prune_usage, 0); + while (argc--) { + unsigned char sha1[20]; + const char *name = *argv++; + + if (!get_sha1(name, sha1)) { + struct object *object = parse_object(sha1); + if (!object) + die("bad object: %s", name); + add_pending_object(&revs, object, ""); + } + else + die("unrecognized argument: %s", name); + } + + if (show_progress == -1) + show_progress = isatty(2); + if (show_progress) + progress = start_progress_delay("Checking connectivity", 0, 0, 2); + + mark_reachable_objects(&revs, 1, progress); + stop_progress(&progress); + prune_object_dir(get_object_directory()); + + prune_packed_objects(show_only); + remove_temporary_files(get_object_directory()); + s = mkpathdup("%s/pack", get_object_directory()); + remove_temporary_files(s); + free(s); + return 0; +} diff --git a/builtin/push.c b/builtin/push.c new file mode 100644 index 0000000000..8491e431e4 --- /dev/null +++ b/builtin/push.c @@ -0,0 +1,435 @@ +/* + * "git push" + */ +#include "cache.h" +#include "refs.h" +#include "run-command.h" +#include "builtin.h" +#include "remote.h" +#include "transport.h" +#include "parse-options.h" +#include "submodule.h" + +static const char * const push_usage[] = { + N_("git push [<options>] [<repository> [<refspec>...]]"), + NULL, +}; + +static int thin; +static int deleterefs; +static const char *receivepack; +static int verbosity; +static int progress = -1; + +static const char **refspec; +static int refspec_nr; +static int refspec_alloc; +static int default_matching_used; + +static void add_refspec(const char *ref) +{ + refspec_nr++; + ALLOC_GROW(refspec, refspec_nr, refspec_alloc); + refspec[refspec_nr-1] = ref; +} + +static void set_refspecs(const char **refs, int nr) +{ + int i; + for (i = 0; i < nr; i++) { + const char *ref = refs[i]; + if (!strcmp("tag", ref)) { + char *tag; + int len; + if (nr <= ++i) + die(_("tag shorthand without <tag>")); + len = strlen(refs[i]) + 11; + if (deleterefs) { + tag = xmalloc(len+1); + strcpy(tag, ":refs/tags/"); + } else { + tag = xmalloc(len); + strcpy(tag, "refs/tags/"); + } + strcat(tag, refs[i]); + ref = tag; + } else if (deleterefs && !strchr(ref, ':')) { + char *delref; + int len = strlen(ref)+1; + delref = xmalloc(len+1); + strcpy(delref, ":"); + strcat(delref, ref); + ref = delref; + } else if (deleterefs) + die(_("--delete only accepts plain target ref names")); + add_refspec(ref); + } +} + +static int push_url_of_remote(struct remote *remote, const char ***url_p) +{ + if (remote->pushurl_nr) { + *url_p = remote->pushurl; + return remote->pushurl_nr; + } + *url_p = remote->url; + return remote->url_nr; +} + +static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) { + /* + * There's no point in using shorten_unambiguous_ref here, + * as the ambiguity would be on the remote side, not what + * we have locally. Plus, this is supposed to be the simple + * mode. If the user is doing something crazy like setting + * upstream to a non-branch, we should probably be showing + * them the big ugly fully qualified ref. + */ + const char *advice_maybe = ""; + const char *short_upstream = + skip_prefix(branch->merge[0]->src, "refs/heads/"); + + if (!short_upstream) + short_upstream = branch->merge[0]->src; + /* + * Don't show advice for people who explicitely set + * push.default. + */ + if (push_default == PUSH_DEFAULT_UNSPECIFIED) + advice_maybe = _("\n" + "To choose either option permanently, " + "see push.default in 'git help config'."); + die(_("The upstream branch of your current branch does not match\n" + "the name of your current branch. To push to the upstream branch\n" + "on the remote, use\n" + "\n" + " git push %s HEAD:%s\n" + "\n" + "To push to the branch of the same name on the remote, use\n" + "\n" + " git push %s %s\n" + "%s"), + remote->name, short_upstream, + remote->name, branch->name, advice_maybe); +} + +static void setup_push_upstream(struct remote *remote, int simple) +{ + struct strbuf refspec = STRBUF_INIT; + struct branch *branch = branch_get(NULL); + if (!branch) + die(_("You are not currently on a branch.\n" + "To push the history leading to the current (detached HEAD)\n" + "state now, use\n" + "\n" + " git push %s HEAD:<name-of-remote-branch>\n"), + remote->name); + if (!branch->merge_nr || !branch->merge || !branch->remote_name) + die(_("The current branch %s has no upstream branch.\n" + "To push the current branch and set the remote as upstream, use\n" + "\n" + " git push --set-upstream %s %s\n"), + branch->name, + remote->name, + branch->name); + if (branch->merge_nr != 1) + die(_("The current branch %s has multiple upstream branches, " + "refusing to push."), branch->name); + if (strcmp(branch->remote_name, remote->name)) + die(_("You are pushing to remote '%s', which is not the upstream of\n" + "your current branch '%s', without telling me what to push\n" + "to update which remote branch."), + remote->name, branch->name); + if (simple && strcmp(branch->refname, branch->merge[0]->src)) + die_push_simple(branch, remote); + + strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src); + add_refspec(refspec.buf); +} + +static char warn_unspecified_push_default_msg[] = +N_("push.default is unset; its implicit value is changing in\n" + "Git 2.0 from 'matching' to 'simple'. To squelch this message\n" + "and maintain the current behavior after the default changes, use:\n" + "\n" + " git config --global push.default matching\n" + "\n" + "To squelch this message and adopt the new behavior now, use:\n" + "\n" + " git config --global push.default simple\n" + "\n" + "See 'git help config' and search for 'push.default' for further information.\n" + "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n" + "'current' instead of 'simple' if you sometimes use older versions of Git)"); + +static void warn_unspecified_push_default_configuration(void) +{ + static int warn_once; + + if (warn_once++) + return; + warning("%s\n", _(warn_unspecified_push_default_msg)); +} + +static void setup_default_push_refspecs(struct remote *remote) +{ + switch (push_default) { + default: + case PUSH_DEFAULT_UNSPECIFIED: + default_matching_used = 1; + warn_unspecified_push_default_configuration(); + /* fallthru */ + case PUSH_DEFAULT_MATCHING: + add_refspec(":"); + break; + + case PUSH_DEFAULT_SIMPLE: + setup_push_upstream(remote, 1); + break; + + case PUSH_DEFAULT_UPSTREAM: + setup_push_upstream(remote, 0); + break; + + case PUSH_DEFAULT_CURRENT: + add_refspec("HEAD"); + break; + + case PUSH_DEFAULT_NOTHING: + die(_("You didn't specify any refspecs to push, and " + "push.default is \"nothing\".")); + break; + } +} + +static const char message_advice_pull_before_push[] = + N_("Updates were rejected because the tip of your current branch is behind\n" + "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n" + "before pushing again.\n" + "See the 'Note about fast-forwards' in 'git push --help' for details."); + +static const char message_advice_use_upstream[] = + N_("Updates were rejected because a pushed branch tip is behind its remote\n" + "counterpart. If you did not intend to push that branch, you may want to\n" + "specify branches to push or set the 'push.default' configuration variable\n" + "to 'simple', 'current' or 'upstream' to push only the current branch."); + +static const char message_advice_checkout_pull_push[] = + N_("Updates were rejected because a pushed branch tip is behind its remote\n" + "counterpart. Check out this branch and merge the remote changes\n" + "(e.g. 'git pull') before pushing again.\n" + "See the 'Note about fast-forwards' in 'git push --help' for details."); + +static const char message_advice_ref_already_exists[] = + N_("Updates were rejected because the destination reference already exists\n" + "in the remote."); + +static void advise_pull_before_push(void) +{ + if (!advice_push_non_ff_current || !advice_push_update_rejected) + return; + advise(_(message_advice_pull_before_push)); +} + +static void advise_use_upstream(void) +{ + if (!advice_push_non_ff_default || !advice_push_update_rejected) + return; + advise(_(message_advice_use_upstream)); +} + +static void advise_checkout_pull_push(void) +{ + if (!advice_push_non_ff_matching || !advice_push_update_rejected) + return; + advise(_(message_advice_checkout_pull_push)); +} + +static void advise_ref_already_exists(void) +{ + if (!advice_push_already_exists || !advice_push_update_rejected) + return; + advise(_(message_advice_ref_already_exists)); +} + +static int push_with_options(struct transport *transport, int flags) +{ + int err; + unsigned int reject_reasons; + + transport_set_verbosity(transport, verbosity, progress); + + if (receivepack) + transport_set_option(transport, + TRANS_OPT_RECEIVEPACK, receivepack); + if (thin) + transport_set_option(transport, TRANS_OPT_THIN, "yes"); + + if (verbosity > 0) + fprintf(stderr, _("Pushing to %s\n"), transport->url); + err = transport_push(transport, refspec_nr, refspec, flags, + &reject_reasons); + if (err != 0) + error(_("failed to push some refs to '%s'"), transport->url); + + err |= transport_disconnect(transport); + if (!err) + return 0; + + if (reject_reasons & REJECT_NON_FF_HEAD) { + advise_pull_before_push(); + } else if (reject_reasons & REJECT_NON_FF_OTHER) { + if (default_matching_used) + advise_use_upstream(); + else + advise_checkout_pull_push(); + } else if (reject_reasons & REJECT_ALREADY_EXISTS) { + advise_ref_already_exists(); + } + + return 1; +} + +static int do_push(const char *repo, int flags) +{ + int i, errs; + struct remote *remote = remote_get(repo); + const char **url; + int url_nr; + + if (!remote) { + if (repo) + die(_("bad repository '%s'"), repo); + die(_("No configured push destination.\n" + "Either specify the URL from the command-line or configure a remote repository using\n" + "\n" + " git remote add <name> <url>\n" + "\n" + "and then push using the remote name\n" + "\n" + " git push <name>\n")); + } + + if (remote->mirror) + flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE); + + if ((flags & TRANSPORT_PUSH_ALL) && refspec) { + if (!strcmp(*refspec, "refs/tags/*")) + return error(_("--all and --tags are incompatible")); + return error(_("--all can't be combined with refspecs")); + } + + if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) { + if (!strcmp(*refspec, "refs/tags/*")) + return error(_("--mirror and --tags are incompatible")); + return error(_("--mirror can't be combined with refspecs")); + } + + if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) == + (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) { + return error(_("--all and --mirror are incompatible")); + } + + if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) { + if (remote->push_refspec_nr) { + refspec = remote->push_refspec; + refspec_nr = remote->push_refspec_nr; + } else if (!(flags & TRANSPORT_PUSH_MIRROR)) + setup_default_push_refspecs(remote); + } + errs = 0; + url_nr = push_url_of_remote(remote, &url); + if (url_nr) { + for (i = 0; i < url_nr; i++) { + struct transport *transport = + transport_get(remote, url[i]); + if (push_with_options(transport, flags)) + errs++; + } + } else { + struct transport *transport = + transport_get(remote, NULL); + + if (push_with_options(transport, flags)) + errs++; + } + return !!errs; +} + +static int option_parse_recurse_submodules(const struct option *opt, + const char *arg, int unset) +{ + int *flags = opt->value; + + if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK | + TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND)) + die("%s can only be used once.", opt->long_name); + + if (arg) { + if (!strcmp(arg, "check")) + *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK; + else if (!strcmp(arg, "on-demand")) + *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND; + else + die("bad %s argument: %s", opt->long_name, arg); + } else + die("option %s needs an argument (check|on-demand)", + opt->long_name); + + return 0; +} + +int cmd_push(int argc, const char **argv, const char *prefix) +{ + int flags = 0; + int tags = 0; + int rc; + const char *repo = NULL; /* default repository */ + struct option options[] = { + OPT__VERBOSITY(&verbosity), + OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")), + OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL), + OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"), + (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)), + OPT_BOOLEAN( 0, "delete", &deleterefs, N_("delete refs")), + OPT_BOOLEAN( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")), + OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN), + OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN), + OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE), + { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"), + N_("control recursive pushing of submodules"), + PARSE_OPT_OPTARG, option_parse_recurse_submodules }, + OPT_BOOLEAN( 0 , "thin", &thin, N_("use thin pack")), + OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")), + OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")), + OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"), + TRANSPORT_PUSH_SET_UPSTREAM), + OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), + OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"), + TRANSPORT_PUSH_PRUNE), + OPT_END() + }; + + packet_trace_identity("push"); + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, push_usage, 0); + + if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR)))) + die(_("--delete is incompatible with --all, --mirror and --tags")); + if (deleterefs && argc < 2) + die(_("--delete doesn't make sense without any refs")); + + if (tags) + add_refspec("refs/tags/*"); + + if (argc > 0) { + repo = argv[0]; + set_refspecs(argv + 1, argc - 1); + } + + rc = do_push(repo, flags); + if (rc == -1) + usage_with_options(push_usage, options); + else + return rc; +} diff --git a/builtin/read-tree.c b/builtin/read-tree.c new file mode 100644 index 0000000000..042ac1b84f --- /dev/null +++ b/builtin/read-tree.c @@ -0,0 +1,239 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ + +#include "cache.h" +#include "object.h" +#include "tree.h" +#include "tree-walk.h" +#include "cache-tree.h" +#include "unpack-trees.h" +#include "dir.h" +#include "builtin.h" +#include "parse-options.h" +#include "resolve-undo.h" + +static int nr_trees; +static int read_empty; +static struct tree *trees[MAX_UNPACK_TREES]; + +static int list_tree(unsigned char *sha1) +{ + struct tree *tree; + + if (nr_trees >= MAX_UNPACK_TREES) + die("I cannot read more than %d trees", MAX_UNPACK_TREES); + tree = parse_tree_indirect(sha1); + if (!tree) + return -1; + trees[nr_trees++] = tree; + return 0; +} + +static const char * const read_tree_usage[] = { + N_("git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u [--exclude-per-directory=<gitignore>] | -i]] [--no-sparse-checkout] [--index-output=<file>] (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"), + NULL +}; + +static int index_output_cb(const struct option *opt, const char *arg, + int unset) +{ + set_alternate_index_output(arg); + return 0; +} + +static int exclude_per_directory_cb(const struct option *opt, const char *arg, + int unset) +{ + struct dir_struct *dir; + struct unpack_trees_options *opts; + + opts = (struct unpack_trees_options *)opt->value; + + if (opts->dir) + die("more than one --exclude-per-directory given."); + + dir = xcalloc(1, sizeof(*opts->dir)); + dir->flags |= DIR_SHOW_IGNORED; + dir->exclude_per_dir = arg; + opts->dir = dir; + /* We do not need to nor want to do read-directory + * here; we are merely interested in reusing the + * per directory ignore stack mechanism. + */ + return 0; +} + +static void debug_stage(const char *label, struct cache_entry *ce, + struct unpack_trees_options *o) +{ + printf("%s ", label); + if (!ce) + printf("(missing)\n"); + else if (ce == o->df_conflict_entry) + printf("(conflict)\n"); + else + printf("%06o #%d %s %.8s\n", + ce->ce_mode, ce_stage(ce), ce->name, + sha1_to_hex(ce->sha1)); +} + +static int debug_merge(struct cache_entry **stages, struct unpack_trees_options *o) +{ + int i; + + printf("* %d-way merge\n", o->merge_size); + debug_stage("index", stages[0], o); + for (i = 1; i <= o->merge_size; i++) { + char buf[24]; + sprintf(buf, "ent#%d", i); + debug_stage(buf, stages[i], o); + } + return 0; +} + +static struct lock_file lock_file; + +int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) +{ + int i, newfd, stage = 0; + unsigned char sha1[20]; + struct tree_desc t[MAX_UNPACK_TREES]; + struct unpack_trees_options opts; + int prefix_set = 0; + const struct option read_tree_options[] = { + { OPTION_CALLBACK, 0, "index-output", NULL, N_("file"), + N_("write resulting index to <file>"), + PARSE_OPT_NONEG, index_output_cb }, + OPT_SET_INT(0, "empty", &read_empty, + N_("only empty the index"), 1), + OPT__VERBOSE(&opts.verbose_update, N_("be verbose")), + OPT_GROUP(N_("Merging")), + OPT_SET_INT('m', NULL, &opts.merge, + N_("perform a merge in addition to a read"), 1), + OPT_SET_INT(0, "trivial", &opts.trivial_merges_only, + N_("3-way merge if no file level merging required"), 1), + OPT_SET_INT(0, "aggressive", &opts.aggressive, + N_("3-way merge in presence of adds and removes"), 1), + OPT_SET_INT(0, "reset", &opts.reset, + N_("same as -m, but discard unmerged entries"), 1), + { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"), + N_("read the tree into the index under <subdirectory>/"), + PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP }, + OPT_SET_INT('u', NULL, &opts.update, + N_("update working tree with merge result"), 1), + { OPTION_CALLBACK, 0, "exclude-per-directory", &opts, + N_("gitignore"), + N_("allow explicitly ignored files to be overwritten"), + PARSE_OPT_NONEG, exclude_per_directory_cb }, + OPT_SET_INT('i', NULL, &opts.index_only, + N_("don't check the working tree after merging"), 1), + OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")), + OPT_SET_INT(0, "no-sparse-checkout", &opts.skip_sparse_checkout, + N_("skip applying sparse checkout filter"), 1), + OPT_SET_INT(0, "debug-unpack", &opts.debug_unpack, + N_("debug unpack-trees"), 1), + OPT_END() + }; + + memset(&opts, 0, sizeof(opts)); + opts.head_idx = -1; + opts.src_index = &the_index; + opts.dst_index = &the_index; + + git_config(git_default_config, NULL); + + argc = parse_options(argc, argv, unused_prefix, read_tree_options, + read_tree_usage, 0); + + newfd = hold_locked_index(&lock_file, 1); + + prefix_set = opts.prefix ? 1 : 0; + if (1 < opts.merge + opts.reset + prefix_set) + die("Which one? -m, --reset, or --prefix?"); + + if (opts.reset || opts.merge || opts.prefix) { + if (read_cache_unmerged() && (opts.prefix || opts.merge)) + die("You need to resolve your current index first"); + stage = opts.merge = 1; + } + resolve_undo_clear(); + + for (i = 0; i < argc; i++) { + const char *arg = argv[i]; + + if (get_sha1(arg, sha1)) + die("Not a valid object name %s", arg); + if (list_tree(sha1) < 0) + die("failed to unpack tree object %s", arg); + stage++; + } + if (nr_trees == 0 && !read_empty) + warning("read-tree: emptying the index with no arguments is deprecated; use --empty"); + else if (nr_trees > 0 && read_empty) + die("passing trees as arguments contradicts --empty"); + + if (1 < opts.index_only + opts.update) + die("-u and -i at the same time makes no sense"); + if ((opts.update||opts.index_only) && !opts.merge) + die("%s is meaningless without -m, --reset, or --prefix", + opts.update ? "-u" : "-i"); + if ((opts.dir && !opts.update)) + die("--exclude-per-directory is meaningless unless -u"); + if (opts.merge && !opts.index_only) + setup_work_tree(); + + if (opts.merge) { + if (stage < 2) + die("just how do you expect me to merge %d trees?", stage-1); + switch (stage - 1) { + case 1: + opts.fn = opts.prefix ? bind_merge : oneway_merge; + break; + case 2: + opts.fn = twoway_merge; + opts.initial_checkout = is_cache_unborn(); + break; + case 3: + default: + opts.fn = threeway_merge; + break; + } + + if (stage - 1 >= 3) + opts.head_idx = stage - 2; + else + opts.head_idx = 1; + } + + if (opts.debug_unpack) + opts.fn = debug_merge; + + cache_tree_free(&active_cache_tree); + for (i = 0; i < nr_trees; i++) { + struct tree *tree = trees[i]; + parse_tree(tree); + init_tree_desc(t+i, tree->buffer, tree->size); + } + if (unpack_trees(nr_trees, t, &opts)) + return 128; + + if (opts.debug_unpack || opts.dry_run) + return 0; /* do not write the index out */ + + /* + * When reading only one tree (either the most basic form, + * "-m ent" or "--reset ent" form), we can obtain a fully + * valid cache-tree because the index must match exactly + * what came from the tree. + */ + if (nr_trees == 1 && !opts.prefix) + prime_cache_tree(&active_cache_tree, trees[0]); + + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(&lock_file)) + die("unable to write new index file"); + return 0; +} diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c new file mode 100644 index 0000000000..ff781febca --- /dev/null +++ b/builtin/receive-pack.c @@ -0,0 +1,1012 @@ +#include "builtin.h" +#include "pack.h" +#include "refs.h" +#include "pkt-line.h" +#include "sideband.h" +#include "run-command.h" +#include "exec_cmd.h" +#include "commit.h" +#include "object.h" +#include "remote.h" +#include "transport.h" +#include "string-list.h" +#include "sha1-array.h" +#include "connected.h" +#include "version.h" + +static const char receive_pack_usage[] = "git receive-pack <git-dir>"; + +enum deny_action { + DENY_UNCONFIGURED, + DENY_IGNORE, + DENY_WARN, + DENY_REFUSE +}; + +static int deny_deletes; +static int deny_non_fast_forwards; +static enum deny_action deny_current_branch = DENY_UNCONFIGURED; +static enum deny_action deny_delete_current = DENY_UNCONFIGURED; +static int receive_fsck_objects = -1; +static int transfer_fsck_objects = -1; +static int receive_unpack_limit = -1; +static int transfer_unpack_limit = -1; +static int unpack_limit = 100; +static int report_status; +static int use_sideband; +static int quiet; +static int prefer_ofs_delta = 1; +static int auto_update_server_info; +static int auto_gc = 1; +static const char *head_name; +static void *head_name_to_free; +static int sent_capabilities; + +static enum deny_action parse_deny_action(const char *var, const char *value) +{ + if (value) { + if (!strcasecmp(value, "ignore")) + return DENY_IGNORE; + if (!strcasecmp(value, "warn")) + return DENY_WARN; + if (!strcasecmp(value, "refuse")) + return DENY_REFUSE; + } + if (git_config_bool(var, value)) + return DENY_REFUSE; + return DENY_IGNORE; +} + +static int receive_pack_config(const char *var, const char *value, void *cb) +{ + if (strcmp(var, "receive.denydeletes") == 0) { + deny_deletes = git_config_bool(var, value); + return 0; + } + + if (strcmp(var, "receive.denynonfastforwards") == 0) { + deny_non_fast_forwards = git_config_bool(var, value); + return 0; + } + + if (strcmp(var, "receive.unpacklimit") == 0) { + receive_unpack_limit = git_config_int(var, value); + return 0; + } + + if (strcmp(var, "transfer.unpacklimit") == 0) { + transfer_unpack_limit = git_config_int(var, value); + return 0; + } + + if (strcmp(var, "receive.fsckobjects") == 0) { + receive_fsck_objects = git_config_bool(var, value); + return 0; + } + + if (strcmp(var, "transfer.fsckobjects") == 0) { + transfer_fsck_objects = git_config_bool(var, value); + return 0; + } + + if (!strcmp(var, "receive.denycurrentbranch")) { + deny_current_branch = parse_deny_action(var, value); + return 0; + } + + if (strcmp(var, "receive.denydeletecurrent") == 0) { + deny_delete_current = parse_deny_action(var, value); + return 0; + } + + if (strcmp(var, "repack.usedeltabaseoffset") == 0) { + prefer_ofs_delta = git_config_bool(var, value); + return 0; + } + + if (strcmp(var, "receive.updateserverinfo") == 0) { + auto_update_server_info = git_config_bool(var, value); + return 0; + } + + if (strcmp(var, "receive.autogc") == 0) { + auto_gc = git_config_bool(var, value); + return 0; + } + + return git_default_config(var, value, cb); +} + +static void show_ref(const char *path, const unsigned char *sha1) +{ + if (sent_capabilities) + packet_write(1, "%s %s\n", sha1_to_hex(sha1), path); + else + packet_write(1, "%s %s%c%s%s agent=%s\n", + sha1_to_hex(sha1), path, 0, + " report-status delete-refs side-band-64k quiet", + prefer_ofs_delta ? " ofs-delta" : "", + git_user_agent_sanitized()); + sent_capabilities = 1; +} + +static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused) +{ + path = strip_namespace(path); + /* + * Advertise refs outside our current namespace as ".have" + * refs, so that the client can use them to minimize data + * transfer but will otherwise ignore them. This happens to + * cover ".have" that are thrown in by add_one_alternate_ref() + * to mark histories that are complete in our alternates as + * well. + */ + if (!path) + path = ".have"; + show_ref(path, sha1); + return 0; +} + +static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused) +{ + show_ref(".have", sha1); +} + +static void collect_one_alternate_ref(const struct ref *ref, void *data) +{ + struct sha1_array *sa = data; + sha1_array_append(sa, ref->old_sha1); +} + +static void write_head_info(void) +{ + struct sha1_array sa = SHA1_ARRAY_INIT; + for_each_alternate_ref(collect_one_alternate_ref, &sa); + sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL); + sha1_array_clear(&sa); + for_each_ref(show_ref_cb, NULL); + if (!sent_capabilities) + show_ref("capabilities^{}", null_sha1); + + /* EOF */ + packet_flush(1); +} + +struct command { + struct command *next; + const char *error_string; + unsigned int skip_update:1, + did_not_exist:1; + unsigned char old_sha1[20]; + unsigned char new_sha1[20]; + char ref_name[FLEX_ARRAY]; /* more */ +}; + +static const char pre_receive_hook[] = "hooks/pre-receive"; +static const char post_receive_hook[] = "hooks/post-receive"; + +static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2))); +static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2))); + +static void report_message(const char *prefix, const char *err, va_list params) +{ + int sz = strlen(prefix); + char msg[4096]; + + strncpy(msg, prefix, sz); + sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params); + if (sz > (sizeof(msg) - 1)) + sz = sizeof(msg) - 1; + msg[sz++] = '\n'; + + if (use_sideband) + send_sideband(1, 2, msg, sz, use_sideband); + else + xwrite(2, msg, sz); +} + +static void rp_warning(const char *err, ...) +{ + va_list params; + va_start(params, err); + report_message("warning: ", err, params); + va_end(params); +} + +static void rp_error(const char *err, ...) +{ + va_list params; + va_start(params, err); + report_message("error: ", err, params); + va_end(params); +} + +static int copy_to_sideband(int in, int out, void *arg) +{ + char data[128]; + while (1) { + ssize_t sz = xread(in, data, sizeof(data)); + if (sz <= 0) + break; + send_sideband(1, 2, data, sz, use_sideband); + } + close(in); + return 0; +} + +typedef int (*feed_fn)(void *, const char **, size_t *); +static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state) +{ + struct child_process proc; + struct async muxer; + const char *argv[2]; + int code; + + if (access(hook_name, X_OK) < 0) + return 0; + + argv[0] = hook_name; + argv[1] = NULL; + + memset(&proc, 0, sizeof(proc)); + proc.argv = argv; + proc.in = -1; + proc.stdout_to_stderr = 1; + + if (use_sideband) { + memset(&muxer, 0, sizeof(muxer)); + muxer.proc = copy_to_sideband; + muxer.in = -1; + code = start_async(&muxer); + if (code) + return code; + proc.err = muxer.in; + } + + code = start_command(&proc); + if (code) { + if (use_sideband) + finish_async(&muxer); + return code; + } + + while (1) { + const char *buf; + size_t n; + if (feed(feed_state, &buf, &n)) + break; + if (write_in_full(proc.in, buf, n) != n) + break; + } + close(proc.in); + if (use_sideband) + finish_async(&muxer); + return finish_command(&proc); +} + +struct receive_hook_feed_state { + struct command *cmd; + int skip_broken; + struct strbuf buf; +}; + +static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) +{ + struct receive_hook_feed_state *state = state_; + struct command *cmd = state->cmd; + + while (cmd && + state->skip_broken && (cmd->error_string || cmd->did_not_exist)) + cmd = cmd->next; + if (!cmd) + return -1; /* EOF */ + strbuf_reset(&state->buf); + strbuf_addf(&state->buf, "%s %s %s\n", + sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1), + cmd->ref_name); + state->cmd = cmd->next; + if (bufp) { + *bufp = state->buf.buf; + *sizep = state->buf.len; + } + return 0; +} + +static int run_receive_hook(struct command *commands, const char *hook_name, + int skip_broken) +{ + struct receive_hook_feed_state state; + int status; + + strbuf_init(&state.buf, 0); + state.cmd = commands; + state.skip_broken = skip_broken; + if (feed_receive_hook(&state, NULL, NULL)) + return 0; + state.cmd = commands; + status = run_and_feed_hook(hook_name, feed_receive_hook, &state); + strbuf_release(&state.buf); + return status; +} + +static int run_update_hook(struct command *cmd) +{ + static const char update_hook[] = "hooks/update"; + const char *argv[5]; + struct child_process proc; + int code; + + if (access(update_hook, X_OK) < 0) + return 0; + + argv[0] = update_hook; + argv[1] = cmd->ref_name; + argv[2] = sha1_to_hex(cmd->old_sha1); + argv[3] = sha1_to_hex(cmd->new_sha1); + argv[4] = NULL; + + memset(&proc, 0, sizeof(proc)); + proc.no_stdin = 1; + proc.stdout_to_stderr = 1; + proc.err = use_sideband ? -1 : 0; + proc.argv = argv; + + code = start_command(&proc); + if (code) + return code; + if (use_sideband) + copy_to_sideband(proc.err, -1, NULL); + return finish_command(&proc); +} + +static int is_ref_checked_out(const char *ref) +{ + if (is_bare_repository()) + return 0; + + if (!head_name) + return 0; + return !strcmp(head_name, ref); +} + +static char *refuse_unconfigured_deny_msg[] = { + "By default, updating the current branch in a non-bare repository", + "is denied, because it will make the index and work tree inconsistent", + "with what you pushed, and will require 'git reset --hard' to match", + "the work tree to HEAD.", + "", + "You can set 'receive.denyCurrentBranch' configuration variable to", + "'ignore' or 'warn' in the remote repository to allow pushing into", + "its current branch; however, this is not recommended unless you", + "arranged to update its work tree to match what you pushed in some", + "other way.", + "", + "To squelch this message and still keep the default behaviour, set", + "'receive.denyCurrentBranch' configuration variable to 'refuse'." +}; + +static void refuse_unconfigured_deny(void) +{ + int i; + for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++) + rp_error("%s", refuse_unconfigured_deny_msg[i]); +} + +static char *refuse_unconfigured_deny_delete_current_msg[] = { + "By default, deleting the current branch is denied, because the next", + "'git clone' won't result in any file checked out, causing confusion.", + "", + "You can set 'receive.denyDeleteCurrent' configuration variable to", + "'warn' or 'ignore' in the remote repository to allow deleting the", + "current branch, with or without a warning message.", + "", + "To squelch this message, you can set it to 'refuse'." +}; + +static void refuse_unconfigured_deny_delete_current(void) +{ + int i; + for (i = 0; + i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg); + i++) + rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]); +} + +static const char *update(struct command *cmd) +{ + const char *name = cmd->ref_name; + struct strbuf namespaced_name_buf = STRBUF_INIT; + const char *namespaced_name; + unsigned char *old_sha1 = cmd->old_sha1; + unsigned char *new_sha1 = cmd->new_sha1; + struct ref_lock *lock; + + /* only refs/... are allowed */ + if (prefixcmp(name, "refs/") || check_refname_format(name + 5, 0)) { + rp_error("refusing to create funny ref '%s' remotely", name); + return "funny refname"; + } + + strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name); + namespaced_name = strbuf_detach(&namespaced_name_buf, NULL); + + if (is_ref_checked_out(namespaced_name)) { + switch (deny_current_branch) { + case DENY_IGNORE: + break; + case DENY_WARN: + rp_warning("updating the current branch"); + break; + case DENY_REFUSE: + case DENY_UNCONFIGURED: + rp_error("refusing to update checked out branch: %s", name); + if (deny_current_branch == DENY_UNCONFIGURED) + refuse_unconfigured_deny(); + return "branch is currently checked out"; + } + } + + if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) { + error("unpack should have generated %s, " + "but I can't find it!", sha1_to_hex(new_sha1)); + return "bad pack"; + } + + if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) { + if (deny_deletes && !prefixcmp(name, "refs/heads/")) { + rp_error("denying ref deletion for %s", name); + return "deletion prohibited"; + } + + if (!strcmp(namespaced_name, head_name)) { + switch (deny_delete_current) { + case DENY_IGNORE: + break; + case DENY_WARN: + rp_warning("deleting the current branch"); + break; + case DENY_REFUSE: + case DENY_UNCONFIGURED: + if (deny_delete_current == DENY_UNCONFIGURED) + refuse_unconfigured_deny_delete_current(); + rp_error("refusing to delete the current branch: %s", name); + return "deletion of the current branch prohibited"; + } + } + } + + if (deny_non_fast_forwards && !is_null_sha1(new_sha1) && + !is_null_sha1(old_sha1) && + !prefixcmp(name, "refs/heads/")) { + struct object *old_object, *new_object; + struct commit *old_commit, *new_commit; + + old_object = parse_object(old_sha1); + new_object = parse_object(new_sha1); + + if (!old_object || !new_object || + old_object->type != OBJ_COMMIT || + new_object->type != OBJ_COMMIT) { + error("bad sha1 objects for %s", name); + return "bad ref"; + } + old_commit = (struct commit *)old_object; + new_commit = (struct commit *)new_object; + if (!in_merge_bases(old_commit, new_commit)) { + rp_error("denying non-fast-forward %s" + " (you should pull first)", name); + return "non-fast-forward"; + } + } + if (run_update_hook(cmd)) { + rp_error("hook declined to update %s", name); + return "hook declined"; + } + + if (is_null_sha1(new_sha1)) { + if (!parse_object(old_sha1)) { + old_sha1 = NULL; + if (ref_exists(name)) { + rp_warning("Allowing deletion of corrupt ref."); + } else { + rp_warning("Deleting a non-existent ref."); + cmd->did_not_exist = 1; + } + } + if (delete_ref(namespaced_name, old_sha1, 0)) { + rp_error("failed to delete %s", name); + return "failed to delete"; + } + return NULL; /* good */ + } + else { + lock = lock_any_ref_for_update(namespaced_name, old_sha1, 0); + if (!lock) { + rp_error("failed to lock %s", name); + return "failed to lock"; + } + if (write_ref_sha1(lock, new_sha1, "push")) { + return "failed to write"; /* error() already called */ + } + return NULL; /* good */ + } +} + +static char update_post_hook[] = "hooks/post-update"; + +static void run_update_post_hook(struct command *commands) +{ + struct command *cmd; + int argc; + const char **argv; + struct child_process proc; + + for (argc = 0, cmd = commands; cmd; cmd = cmd->next) { + if (cmd->error_string || cmd->did_not_exist) + continue; + argc++; + } + if (!argc || access(update_post_hook, X_OK) < 0) + return; + argv = xmalloc(sizeof(*argv) * (2 + argc)); + argv[0] = update_post_hook; + + for (argc = 1, cmd = commands; cmd; cmd = cmd->next) { + char *p; + if (cmd->error_string || cmd->did_not_exist) + continue; + p = xmalloc(strlen(cmd->ref_name) + 1); + strcpy(p, cmd->ref_name); + argv[argc] = p; + argc++; + } + argv[argc] = NULL; + + memset(&proc, 0, sizeof(proc)); + proc.no_stdin = 1; + proc.stdout_to_stderr = 1; + proc.err = use_sideband ? -1 : 0; + proc.argv = argv; + + if (!start_command(&proc)) { + if (use_sideband) + copy_to_sideband(proc.err, -1, NULL); + finish_command(&proc); + } +} + +static void check_aliased_update(struct command *cmd, struct string_list *list) +{ + struct strbuf buf = STRBUF_INIT; + const char *dst_name; + struct string_list_item *item; + struct command *dst_cmd; + unsigned char sha1[20]; + char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41]; + int flag; + + strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name); + dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag); + strbuf_release(&buf); + + if (!(flag & REF_ISSYMREF)) + return; + + dst_name = strip_namespace(dst_name); + if (!dst_name) { + rp_error("refusing update to broken symref '%s'", cmd->ref_name); + cmd->skip_update = 1; + cmd->error_string = "broken symref"; + return; + } + + if ((item = string_list_lookup(list, dst_name)) == NULL) + return; + + cmd->skip_update = 1; + + dst_cmd = (struct command *) item->util; + + if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) && + !hashcmp(cmd->new_sha1, dst_cmd->new_sha1)) + return; + + dst_cmd->skip_update = 1; + + strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV)); + strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV)); + strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV)); + strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV)); + rp_error("refusing inconsistent update between symref '%s' (%s..%s) and" + " its target '%s' (%s..%s)", + cmd->ref_name, cmd_oldh, cmd_newh, + dst_cmd->ref_name, dst_oldh, dst_newh); + + cmd->error_string = dst_cmd->error_string = + "inconsistent aliased update"; +} + +static void check_aliased_updates(struct command *commands) +{ + struct command *cmd; + struct string_list ref_list = STRING_LIST_INIT_NODUP; + + for (cmd = commands; cmd; cmd = cmd->next) { + struct string_list_item *item = + string_list_append(&ref_list, cmd->ref_name); + item->util = (void *)cmd; + } + sort_string_list(&ref_list); + + for (cmd = commands; cmd; cmd = cmd->next) { + if (!cmd->error_string) + check_aliased_update(cmd, &ref_list); + } + + string_list_clear(&ref_list, 0); +} + +static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]) +{ + struct command **cmd_list = cb_data; + struct command *cmd = *cmd_list; + + if (!cmd || is_null_sha1(cmd->new_sha1)) + return -1; /* end of list */ + *cmd_list = NULL; /* this returns only one */ + hashcpy(sha1, cmd->new_sha1); + return 0; +} + +static void set_connectivity_errors(struct command *commands) +{ + struct command *cmd; + + for (cmd = commands; cmd; cmd = cmd->next) { + struct command *singleton = cmd; + if (!check_everything_connected(command_singleton_iterator, + 0, &singleton)) + continue; + cmd->error_string = "missing necessary objects"; + } +} + +static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20]) +{ + struct command **cmd_list = cb_data; + struct command *cmd = *cmd_list; + + while (cmd) { + if (!is_null_sha1(cmd->new_sha1)) { + hashcpy(sha1, cmd->new_sha1); + *cmd_list = cmd->next; + return 0; + } + cmd = cmd->next; + } + *cmd_list = NULL; + return -1; /* end of list */ +} + +static void execute_commands(struct command *commands, const char *unpacker_error) +{ + struct command *cmd; + unsigned char sha1[20]; + + if (unpacker_error) { + for (cmd = commands; cmd; cmd = cmd->next) + cmd->error_string = "unpacker error"; + return; + } + + cmd = commands; + if (check_everything_connected(iterate_receive_command_list, + 0, &cmd)) + set_connectivity_errors(commands); + + if (run_receive_hook(commands, pre_receive_hook, 0)) { + for (cmd = commands; cmd; cmd = cmd->next) { + if (!cmd->error_string) + cmd->error_string = "pre-receive hook declined"; + } + return; + } + + check_aliased_updates(commands); + + free(head_name_to_free); + head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL); + + for (cmd = commands; cmd; cmd = cmd->next) { + if (cmd->error_string) + continue; + + if (cmd->skip_update) + continue; + + cmd->error_string = update(cmd); + } +} + +static struct command *read_head_info(void) +{ + struct command *commands = NULL; + struct command **p = &commands; + for (;;) { + static char line[1000]; + unsigned char old_sha1[20], new_sha1[20]; + struct command *cmd; + char *refname; + int len, reflen; + + len = packet_read_line(0, line, sizeof(line)); + if (!len) + break; + if (line[len-1] == '\n') + line[--len] = 0; + if (len < 83 || + line[40] != ' ' || + line[81] != ' ' || + get_sha1_hex(line, old_sha1) || + get_sha1_hex(line + 41, new_sha1)) + die("protocol error: expected old/new/ref, got '%s'", + line); + + refname = line + 82; + reflen = strlen(refname); + if (reflen + 82 < len) { + const char *feature_list = refname + reflen + 1; + if (parse_feature_request(feature_list, "report-status")) + report_status = 1; + if (parse_feature_request(feature_list, "side-band-64k")) + use_sideband = LARGE_PACKET_MAX; + if (parse_feature_request(feature_list, "quiet")) + quiet = 1; + } + cmd = xcalloc(1, sizeof(struct command) + len - 80); + hashcpy(cmd->old_sha1, old_sha1); + hashcpy(cmd->new_sha1, new_sha1); + memcpy(cmd->ref_name, line + 82, len - 81); + *p = cmd; + p = &cmd->next; + } + return commands; +} + +static const char *parse_pack_header(struct pack_header *hdr) +{ + switch (read_pack_header(0, hdr)) { + case PH_ERROR_EOF: + return "eof before pack header was fully read"; + + case PH_ERROR_PACK_SIGNATURE: + return "protocol error (pack signature mismatch detected)"; + + case PH_ERROR_PROTOCOL: + return "protocol error (pack version unsupported)"; + + default: + return "unknown error in parse_pack_header"; + + case 0: + return NULL; + } +} + +static const char *pack_lockfile; + +static const char *unpack(int err_fd) +{ + struct pack_header hdr; + const char *hdr_err; + char hdr_arg[38]; + int fsck_objects = (receive_fsck_objects >= 0 + ? receive_fsck_objects + : transfer_fsck_objects >= 0 + ? transfer_fsck_objects + : 0); + + hdr_err = parse_pack_header(&hdr); + if (hdr_err) + return hdr_err; + snprintf(hdr_arg, sizeof(hdr_arg), + "--pack_header=%"PRIu32",%"PRIu32, + ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries)); + + if (ntohl(hdr.hdr_entries) < unpack_limit) { + int code, i = 0; + struct child_process child; + const char *unpacker[5]; + unpacker[i++] = "unpack-objects"; + if (quiet) + unpacker[i++] = "-q"; + if (fsck_objects) + unpacker[i++] = "--strict"; + unpacker[i++] = hdr_arg; + unpacker[i++] = NULL; + memset(&child, 0, sizeof(child)); + child.argv = unpacker; + child.no_stdout = 1; + child.err = err_fd; + child.git_cmd = 1; + code = run_command(&child); + if (!code) + return NULL; + return "unpack-objects abnormal exit"; + } else { + const char *keeper[7]; + int s, status, i = 0; + char keep_arg[256]; + struct child_process ip; + + s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid()); + if (gethostname(keep_arg + s, sizeof(keep_arg) - s)) + strcpy(keep_arg + s, "localhost"); + + keeper[i++] = "index-pack"; + keeper[i++] = "--stdin"; + if (fsck_objects) + keeper[i++] = "--strict"; + keeper[i++] = "--fix-thin"; + keeper[i++] = hdr_arg; + keeper[i++] = keep_arg; + keeper[i++] = NULL; + memset(&ip, 0, sizeof(ip)); + ip.argv = keeper; + ip.out = -1; + ip.err = err_fd; + ip.git_cmd = 1; + status = start_command(&ip); + if (status) { + return "index-pack fork failed"; + } + pack_lockfile = index_pack_lockfile(ip.out); + close(ip.out); + status = finish_command(&ip); + if (!status) { + reprepare_packed_git(); + return NULL; + } + return "index-pack abnormal exit"; + } +} + +static const char *unpack_with_sideband(void) +{ + struct async muxer; + const char *ret; + + if (!use_sideband) + return unpack(0); + + memset(&muxer, 0, sizeof(muxer)); + muxer.proc = copy_to_sideband; + muxer.in = -1; + if (start_async(&muxer)) + return NULL; + + ret = unpack(muxer.in); + + finish_async(&muxer); + return ret; +} + +static void report(struct command *commands, const char *unpack_status) +{ + struct command *cmd; + struct strbuf buf = STRBUF_INIT; + + packet_buf_write(&buf, "unpack %s\n", + unpack_status ? unpack_status : "ok"); + for (cmd = commands; cmd; cmd = cmd->next) { + if (!cmd->error_string) + packet_buf_write(&buf, "ok %s\n", + cmd->ref_name); + else + packet_buf_write(&buf, "ng %s %s\n", + cmd->ref_name, cmd->error_string); + } + packet_buf_flush(&buf); + + if (use_sideband) + send_sideband(1, 1, buf.buf, buf.len, use_sideband); + else + safe_write(1, buf.buf, buf.len); + strbuf_release(&buf); +} + +static int delete_only(struct command *commands) +{ + struct command *cmd; + for (cmd = commands; cmd; cmd = cmd->next) { + if (!is_null_sha1(cmd->new_sha1)) + return 0; + } + return 1; +} + +int cmd_receive_pack(int argc, const char **argv, const char *prefix) +{ + int advertise_refs = 0; + int stateless_rpc = 0; + int i; + char *dir = NULL; + struct command *commands; + + packet_trace_identity("receive-pack"); + + argv++; + for (i = 1; i < argc; i++) { + const char *arg = *argv++; + + if (*arg == '-') { + if (!strcmp(arg, "--quiet")) { + quiet = 1; + continue; + } + + if (!strcmp(arg, "--advertise-refs")) { + advertise_refs = 1; + continue; + } + if (!strcmp(arg, "--stateless-rpc")) { + stateless_rpc = 1; + continue; + } + + usage(receive_pack_usage); + } + if (dir) + usage(receive_pack_usage); + dir = xstrdup(arg); + } + if (!dir) + usage(receive_pack_usage); + + setup_path(); + + if (!enter_repo(dir, 0)) + die("'%s' does not appear to be a git repository", dir); + + if (is_repository_shallow()) + die("attempt to push into a shallow repository"); + + git_config(receive_pack_config, NULL); + + if (0 <= transfer_unpack_limit) + unpack_limit = transfer_unpack_limit; + else if (0 <= receive_unpack_limit) + unpack_limit = receive_unpack_limit; + + if (advertise_refs || !stateless_rpc) { + write_head_info(); + } + if (advertise_refs) + return 0; + + if ((commands = read_head_info()) != NULL) { + const char *unpack_status = NULL; + + if (!delete_only(commands)) + unpack_status = unpack_with_sideband(); + execute_commands(commands, unpack_status); + if (pack_lockfile) + unlink_or_warn(pack_lockfile); + if (report_status) + report(commands, unpack_status); + run_receive_hook(commands, post_receive_hook, 1); + run_update_post_hook(commands); + if (auto_gc) { + const char *argv_gc_auto[] = { + "gc", "--auto", "--quiet", NULL, + }; + int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR; + run_command_v_opt(argv_gc_auto, opt); + } + if (auto_update_server_info) + update_server_info(0); + } + if (use_sideband) + packet_flush(1); + return 0; +} diff --git a/builtin/reflog.c b/builtin/reflog.c new file mode 100644 index 0000000000..b3c9e27bde --- /dev/null +++ b/builtin/reflog.c @@ -0,0 +1,783 @@ +#include "cache.h" +#include "builtin.h" +#include "commit.h" +#include "refs.h" +#include "dir.h" +#include "tree-walk.h" +#include "diff.h" +#include "revision.h" +#include "reachable.h" + +/* + * reflog expire + */ + +static const char reflog_expire_usage[] = +"git reflog expire [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>..."; +static const char reflog_delete_usage[] = +"git reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>..."; + +static unsigned long default_reflog_expire; +static unsigned long default_reflog_expire_unreachable; + +struct cmd_reflog_expire_cb { + struct rev_info revs; + int dry_run; + int stalefix; + int rewrite; + int updateref; + int verbose; + unsigned long expire_total; + unsigned long expire_unreachable; + int recno; +}; + +struct expire_reflog_cb { + FILE *newlog; + enum { + UE_NORMAL, + UE_ALWAYS, + UE_HEAD + } unreachable_expire_kind; + struct commit_list *mark_list; + unsigned long mark_limit; + struct cmd_reflog_expire_cb *cmd; + unsigned char last_kept_sha1[20]; +}; + +struct collected_reflog { + unsigned char sha1[20]; + char reflog[FLEX_ARRAY]; +}; +struct collect_reflog_cb { + struct collected_reflog **e; + int alloc; + int nr; +}; + +#define INCOMPLETE (1u<<10) +#define STUDYING (1u<<11) +#define REACHABLE (1u<<12) + +static int tree_is_complete(const unsigned char *sha1) +{ + struct tree_desc desc; + struct name_entry entry; + int complete; + struct tree *tree; + + tree = lookup_tree(sha1); + if (!tree) + return 0; + if (tree->object.flags & SEEN) + return 1; + if (tree->object.flags & INCOMPLETE) + return 0; + + if (!tree->buffer) { + enum object_type type; + unsigned long size; + void *data = read_sha1_file(sha1, &type, &size); + if (!data) { + tree->object.flags |= INCOMPLETE; + return 0; + } + tree->buffer = data; + tree->size = size; + } + init_tree_desc(&desc, tree->buffer, tree->size); + complete = 1; + while (tree_entry(&desc, &entry)) { + if (!has_sha1_file(entry.sha1) || + (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) { + tree->object.flags |= INCOMPLETE; + complete = 0; + } + } + free(tree->buffer); + tree->buffer = NULL; + + if (complete) + tree->object.flags |= SEEN; + return complete; +} + +static int commit_is_complete(struct commit *commit) +{ + struct object_array study; + struct object_array found; + int is_incomplete = 0; + int i; + + /* early return */ + if (commit->object.flags & SEEN) + return 1; + if (commit->object.flags & INCOMPLETE) + return 0; + /* + * Find all commits that are reachable and are not marked as + * SEEN. Then make sure the trees and blobs contained are + * complete. After that, mark these commits also as SEEN. + * If some of the objects that are needed to complete this + * commit are missing, mark this commit as INCOMPLETE. + */ + memset(&study, 0, sizeof(study)); + memset(&found, 0, sizeof(found)); + add_object_array(&commit->object, NULL, &study); + add_object_array(&commit->object, NULL, &found); + commit->object.flags |= STUDYING; + while (study.nr) { + struct commit *c; + struct commit_list *parent; + + c = (struct commit *)study.objects[--study.nr].item; + if (!c->object.parsed && !parse_object(c->object.sha1)) + c->object.flags |= INCOMPLETE; + + if (c->object.flags & INCOMPLETE) { + is_incomplete = 1; + break; + } + else if (c->object.flags & SEEN) + continue; + for (parent = c->parents; parent; parent = parent->next) { + struct commit *p = parent->item; + if (p->object.flags & STUDYING) + continue; + p->object.flags |= STUDYING; + add_object_array(&p->object, NULL, &study); + add_object_array(&p->object, NULL, &found); + } + } + if (!is_incomplete) { + /* + * make sure all commits in "found" array have all the + * necessary objects. + */ + for (i = 0; i < found.nr; i++) { + struct commit *c = + (struct commit *)found.objects[i].item; + if (!tree_is_complete(c->tree->object.sha1)) { + is_incomplete = 1; + c->object.flags |= INCOMPLETE; + } + } + if (!is_incomplete) { + /* mark all found commits as complete, iow SEEN */ + for (i = 0; i < found.nr; i++) + found.objects[i].item->flags |= SEEN; + } + } + /* clear flags from the objects we traversed */ + for (i = 0; i < found.nr; i++) + found.objects[i].item->flags &= ~STUDYING; + if (is_incomplete) + commit->object.flags |= INCOMPLETE; + else { + /* + * If we come here, we have (1) traversed the ancestry chain + * from the "commit" until we reach SEEN commits (which are + * known to be complete), and (2) made sure that the commits + * encountered during the above traversal refer to trees that + * are complete. Which means that we know *all* the commits + * we have seen during this process are complete. + */ + for (i = 0; i < found.nr; i++) + found.objects[i].item->flags |= SEEN; + } + /* free object arrays */ + free(study.objects); + free(found.objects); + return !is_incomplete; +} + +static int keep_entry(struct commit **it, unsigned char *sha1) +{ + struct commit *commit; + + if (is_null_sha1(sha1)) + return 1; + commit = lookup_commit_reference_gently(sha1, 1); + if (!commit) + return 0; + + /* + * Make sure everything in this commit exists. + * + * We have walked all the objects reachable from the refs + * and cache earlier. The commits reachable by this commit + * must meet SEEN commits -- and then we should mark them as + * SEEN as well. + */ + if (!commit_is_complete(commit)) + return 0; + *it = commit; + return 1; +} + +/* + * Starting from commits in the cb->mark_list, mark commits that are + * reachable from them. Stop the traversal at commits older than + * the expire_limit and queue them back, so that the caller can call + * us again to restart the traversal with longer expire_limit. + */ +static void mark_reachable(struct expire_reflog_cb *cb) +{ + struct commit *commit; + struct commit_list *pending; + unsigned long expire_limit = cb->mark_limit; + struct commit_list *leftover = NULL; + + for (pending = cb->mark_list; pending; pending = pending->next) + pending->item->object.flags &= ~REACHABLE; + + pending = cb->mark_list; + while (pending) { + struct commit_list *entry = pending; + struct commit_list *parent; + pending = entry->next; + commit = entry->item; + free(entry); + if (commit->object.flags & REACHABLE) + continue; + if (parse_commit(commit)) + continue; + commit->object.flags |= REACHABLE; + if (commit->date < expire_limit) { + commit_list_insert(commit, &leftover); + continue; + } + commit->object.flags |= REACHABLE; + parent = commit->parents; + while (parent) { + commit = parent->item; + parent = parent->next; + if (commit->object.flags & REACHABLE) + continue; + commit_list_insert(commit, &pending); + } + } + cb->mark_list = leftover; +} + +static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1) +{ + /* + * We may or may not have the commit yet - if not, look it + * up using the supplied sha1. + */ + if (!commit) { + if (is_null_sha1(sha1)) + return 0; + + commit = lookup_commit_reference_gently(sha1, 1); + + /* Not a commit -- keep it */ + if (!commit) + return 0; + } + + /* Reachable from the current ref? Don't prune. */ + if (commit->object.flags & REACHABLE) + return 0; + + if (cb->mark_list && cb->mark_limit) { + cb->mark_limit = 0; /* dig down to the root */ + mark_reachable(cb); + } + + return !(commit->object.flags & REACHABLE); +} + +static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, + const char *email, unsigned long timestamp, int tz, + const char *message, void *cb_data) +{ + struct expire_reflog_cb *cb = cb_data; + struct commit *old, *new; + + if (timestamp < cb->cmd->expire_total) + goto prune; + + if (cb->cmd->rewrite) + osha1 = cb->last_kept_sha1; + + old = new = NULL; + if (cb->cmd->stalefix && + (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1))) + goto prune; + + if (timestamp < cb->cmd->expire_unreachable) { + if (cb->unreachable_expire_kind == UE_ALWAYS) + goto prune; + if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1)) + goto prune; + } + + if (cb->cmd->recno && --(cb->cmd->recno) == 0) + goto prune; + + if (cb->newlog) { + char sign = (tz < 0) ? '-' : '+'; + int zone = (tz < 0) ? (-tz) : tz; + fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s", + sha1_to_hex(osha1), sha1_to_hex(nsha1), + email, timestamp, sign, zone, + message); + hashcpy(cb->last_kept_sha1, nsha1); + } + if (cb->cmd->verbose) + printf("keep %s", message); + return 0; + prune: + if (!cb->newlog) + printf("would prune %s", message); + else if (cb->cmd->verbose) + printf("prune %s", message); + return 0; +} + +static int push_tip_to_list(const char *refname, const unsigned char *sha1, int flags, void *cb_data) +{ + struct commit_list **list = cb_data; + struct commit *tip_commit; + if (flags & REF_ISSYMREF) + return 0; + tip_commit = lookup_commit_reference_gently(sha1, 1); + if (!tip_commit) + return 0; + commit_list_insert(tip_commit, list); + return 0; +} + +static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) +{ + struct cmd_reflog_expire_cb *cmd = cb_data; + struct expire_reflog_cb cb; + struct ref_lock *lock; + char *log_file, *newlog_path = NULL; + struct commit *tip_commit; + struct commit_list *tips; + int status = 0; + + memset(&cb, 0, sizeof(cb)); + + /* + * we take the lock for the ref itself to prevent it from + * getting updated. + */ + lock = lock_any_ref_for_update(ref, sha1, 0); + if (!lock) + return error("cannot lock ref '%s'", ref); + log_file = git_pathdup("logs/%s", ref); + if (!file_exists(log_file)) + goto finish; + if (!cmd->dry_run) { + newlog_path = git_pathdup("logs/%s.lock", ref); + cb.newlog = fopen(newlog_path, "w"); + } + + cb.cmd = cmd; + + if (!cmd->expire_unreachable || !strcmp(ref, "HEAD")) { + tip_commit = NULL; + cb.unreachable_expire_kind = UE_HEAD; + } else { + tip_commit = lookup_commit_reference_gently(sha1, 1); + if (!tip_commit) + cb.unreachable_expire_kind = UE_ALWAYS; + else + cb.unreachable_expire_kind = UE_NORMAL; + } + + if (cmd->expire_unreachable <= cmd->expire_total) + cb.unreachable_expire_kind = UE_ALWAYS; + + cb.mark_list = NULL; + tips = NULL; + if (cb.unreachable_expire_kind != UE_ALWAYS) { + if (cb.unreachable_expire_kind == UE_HEAD) { + struct commit_list *elem; + for_each_ref(push_tip_to_list, &tips); + for (elem = tips; elem; elem = elem->next) + commit_list_insert(elem->item, &cb.mark_list); + } else { + commit_list_insert(tip_commit, &cb.mark_list); + } + cb.mark_limit = cmd->expire_total; + mark_reachable(&cb); + } + + for_each_reflog_ent(ref, expire_reflog_ent, &cb); + + if (cb.unreachable_expire_kind != UE_ALWAYS) { + if (cb.unreachable_expire_kind == UE_HEAD) { + struct commit_list *elem; + for (elem = tips; elem; elem = elem->next) + clear_commit_marks(tip_commit, REACHABLE); + free_commit_list(tips); + } else { + clear_commit_marks(tip_commit, REACHABLE); + } + } + finish: + if (cb.newlog) { + if (fclose(cb.newlog)) { + status |= error("%s: %s", strerror(errno), + newlog_path); + unlink(newlog_path); + } else if (cmd->updateref && + (write_in_full(lock->lock_fd, + sha1_to_hex(cb.last_kept_sha1), 40) != 40 || + write_str_in_full(lock->lock_fd, "\n") != 1 || + close_ref(lock) < 0)) { + status |= error("Couldn't write %s", + lock->lk->filename); + unlink(newlog_path); + } else if (rename(newlog_path, log_file)) { + status |= error("cannot rename %s to %s", + newlog_path, log_file); + unlink(newlog_path); + } else if (cmd->updateref && commit_ref(lock)) { + status |= error("Couldn't set %s", lock->ref_name); + } else { + adjust_shared_perm(log_file); + } + } + free(newlog_path); + free(log_file); + unlock_ref(lock); + return status; +} + +static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) +{ + struct collected_reflog *e; + struct collect_reflog_cb *cb = cb_data; + size_t namelen = strlen(ref); + + e = xmalloc(sizeof(*e) + namelen + 1); + hashcpy(e->sha1, sha1); + memcpy(e->reflog, ref, namelen + 1); + ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc); + cb->e[cb->nr++] = e; + return 0; +} + +static struct reflog_expire_cfg { + struct reflog_expire_cfg *next; + unsigned long expire_total; + unsigned long expire_unreachable; + size_t len; + char pattern[FLEX_ARRAY]; +} *reflog_expire_cfg, **reflog_expire_cfg_tail; + +static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) +{ + struct reflog_expire_cfg *ent; + + if (!reflog_expire_cfg_tail) + reflog_expire_cfg_tail = &reflog_expire_cfg; + + for (ent = reflog_expire_cfg; ent; ent = ent->next) + if (ent->len == len && + !memcmp(ent->pattern, pattern, len)) + return ent; + + ent = xcalloc(1, (sizeof(*ent) + len)); + memcpy(ent->pattern, pattern, len); + ent->len = len; + *reflog_expire_cfg_tail = ent; + reflog_expire_cfg_tail = &(ent->next); + return ent; +} + +static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire) +{ + if (!value) + return config_error_nonbool(var); + if (!strcmp(value, "never") || !strcmp(value, "false")) { + *expire = 0; + return 0; + } + *expire = approxidate(value); + return 0; +} + +/* expiry timer slot */ +#define EXPIRE_TOTAL 01 +#define EXPIRE_UNREACH 02 + +static int reflog_expire_config(const char *var, const char *value, void *cb) +{ + const char *lastdot = strrchr(var, '.'); + unsigned long expire; + int slot; + struct reflog_expire_cfg *ent; + + if (!lastdot || prefixcmp(var, "gc.")) + return git_default_config(var, value, cb); + + if (!strcmp(lastdot, ".reflogexpire")) { + slot = EXPIRE_TOTAL; + if (parse_expire_cfg_value(var, value, &expire)) + return -1; + } else if (!strcmp(lastdot, ".reflogexpireunreachable")) { + slot = EXPIRE_UNREACH; + if (parse_expire_cfg_value(var, value, &expire)) + return -1; + } else + return git_default_config(var, value, cb); + + if (lastdot == var + 2) { + switch (slot) { + case EXPIRE_TOTAL: + default_reflog_expire = expire; + break; + case EXPIRE_UNREACH: + default_reflog_expire_unreachable = expire; + break; + } + return 0; + } + + ent = find_cfg_ent(var + 3, lastdot - (var+3)); + if (!ent) + return -1; + switch (slot) { + case EXPIRE_TOTAL: + ent->expire_total = expire; + break; + case EXPIRE_UNREACH: + ent->expire_unreachable = expire; + break; + } + return 0; +} + +static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref) +{ + struct reflog_expire_cfg *ent; + + if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH)) + return; /* both given explicitly -- nothing to tweak */ + + for (ent = reflog_expire_cfg; ent; ent = ent->next) { + if (!fnmatch(ent->pattern, ref, 0)) { + if (!(slot & EXPIRE_TOTAL)) + cb->expire_total = ent->expire_total; + if (!(slot & EXPIRE_UNREACH)) + cb->expire_unreachable = ent->expire_unreachable; + return; + } + } + + /* + * If unconfigured, make stash never expire + */ + if (!strcmp(ref, "refs/stash")) { + if (!(slot & EXPIRE_TOTAL)) + cb->expire_total = 0; + if (!(slot & EXPIRE_UNREACH)) + cb->expire_unreachable = 0; + return; + } + + /* Nothing matched -- use the default value */ + if (!(slot & EXPIRE_TOTAL)) + cb->expire_total = default_reflog_expire; + if (!(slot & EXPIRE_UNREACH)) + cb->expire_unreachable = default_reflog_expire_unreachable; +} + +static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) +{ + struct cmd_reflog_expire_cb cb; + unsigned long now = time(NULL); + int i, status, do_all; + int explicit_expiry = 0; + + default_reflog_expire_unreachable = now - 30 * 24 * 3600; + default_reflog_expire = now - 90 * 24 * 3600; + git_config(reflog_expire_config, NULL); + + save_commit_buffer = 0; + do_all = status = 0; + memset(&cb, 0, sizeof(cb)); + + cb.expire_total = default_reflog_expire; + cb.expire_unreachable = default_reflog_expire_unreachable; + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) + cb.dry_run = 1; + else if (!prefixcmp(arg, "--expire=")) { + cb.expire_total = approxidate(arg + 9); + explicit_expiry |= EXPIRE_TOTAL; + } + else if (!prefixcmp(arg, "--expire-unreachable=")) { + cb.expire_unreachable = approxidate(arg + 21); + explicit_expiry |= EXPIRE_UNREACH; + } + else if (!strcmp(arg, "--stale-fix")) + cb.stalefix = 1; + else if (!strcmp(arg, "--rewrite")) + cb.rewrite = 1; + else if (!strcmp(arg, "--updateref")) + cb.updateref = 1; + else if (!strcmp(arg, "--all")) + do_all = 1; + else if (!strcmp(arg, "--verbose")) + cb.verbose = 1; + else if (!strcmp(arg, "--")) { + i++; + break; + } + else if (arg[0] == '-') + usage(reflog_expire_usage); + else + break; + } + + /* + * We can trust the commits and objects reachable from refs + * even in older repository. We cannot trust what's reachable + * from reflog if the repository was pruned with older git. + */ + if (cb.stalefix) { + init_revisions(&cb.revs, prefix); + if (cb.verbose) + printf("Marking reachable objects..."); + mark_reachable_objects(&cb.revs, 0, NULL); + if (cb.verbose) + putchar('\n'); + } + + if (do_all) { + struct collect_reflog_cb collected; + int i; + + memset(&collected, 0, sizeof(collected)); + for_each_reflog(collect_reflog, &collected); + for (i = 0; i < collected.nr; i++) { + struct collected_reflog *e = collected.e[i]; + set_reflog_expiry_param(&cb, explicit_expiry, e->reflog); + status |= expire_reflog(e->reflog, e->sha1, 0, &cb); + free(e); + } + free(collected.e); + } + + for (; i < argc; i++) { + char *ref; + unsigned char sha1[20]; + if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) { + status |= error("%s points nowhere!", argv[i]); + continue; + } + set_reflog_expiry_param(&cb, explicit_expiry, ref); + status |= expire_reflog(ref, sha1, 0, &cb); + } + return status; +} + +static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1, + const char *email, unsigned long timestamp, int tz, + const char *message, void *cb_data) +{ + struct cmd_reflog_expire_cb *cb = cb_data; + if (!cb->expire_total || timestamp < cb->expire_total) + cb->recno++; + return 0; +} + +static int cmd_reflog_delete(int argc, const char **argv, const char *prefix) +{ + struct cmd_reflog_expire_cb cb; + int i, status = 0; + + memset(&cb, 0, sizeof(cb)); + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) + cb.dry_run = 1; + else if (!strcmp(arg, "--rewrite")) + cb.rewrite = 1; + else if (!strcmp(arg, "--updateref")) + cb.updateref = 1; + else if (!strcmp(arg, "--verbose")) + cb.verbose = 1; + else if (!strcmp(arg, "--")) { + i++; + break; + } + else if (arg[0] == '-') + usage(reflog_delete_usage); + else + break; + } + + if (argc - i < 1) + return error("Nothing to delete?"); + + for ( ; i < argc; i++) { + const char *spec = strstr(argv[i], "@{"); + unsigned char sha1[20]; + char *ep, *ref; + int recno; + + if (!spec) { + status |= error("Not a reflog: %s", argv[i]); + continue; + } + + if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) { + status |= error("no reflog for '%s'", argv[i]); + continue; + } + + recno = strtoul(spec + 2, &ep, 10); + if (*ep == '}') { + cb.recno = -recno; + for_each_reflog_ent(ref, count_reflog_ent, &cb); + } else { + cb.expire_total = approxidate(spec + 2); + for_each_reflog_ent(ref, count_reflog_ent, &cb); + cb.expire_total = 0; + } + + status |= expire_reflog(ref, sha1, 0, &cb); + free(ref); + } + return status; +} + +/* + * main "reflog" + */ + +static const char reflog_usage[] = +"git reflog [ show | expire | delete ]"; + +int cmd_reflog(int argc, const char **argv, const char *prefix) +{ + if (argc > 1 && !strcmp(argv[1], "-h")) + usage(reflog_usage); + + /* With no command, we default to showing it. */ + if (argc < 2 || *argv[1] == '-') + return cmd_log_reflog(argc, argv, prefix); + + if (!strcmp(argv[1], "show")) + return cmd_log_reflog(argc - 1, argv + 1, prefix); + + if (!strcmp(argv[1], "expire")) + return cmd_reflog_expire(argc - 1, argv + 1, prefix); + + if (!strcmp(argv[1], "delete")) + return cmd_reflog_delete(argc - 1, argv + 1, prefix); + + return cmd_log_reflog(argc, argv, prefix); +} diff --git a/builtin/remote-ext.c b/builtin/remote-ext.c new file mode 100644 index 0000000000..692c834d9d --- /dev/null +++ b/builtin/remote-ext.c @@ -0,0 +1,242 @@ +#include "builtin.h" +#include "transport.h" +#include "run-command.h" + +/* + * URL syntax: + * 'command [arg1 [arg2 [...]]]' Invoke command with given arguments. + * Special characters: + * '% ': Literal space in argument. + * '%%': Literal percent sign. + * '%S': Name of service (git-upload-pack/git-upload-archive/ + * git-receive-pack. + * '%s': Same as \s, but with possible git- prefix stripped. + * '%G': Only allowed as first 'character' of argument. Do not pass this + * Argument to command, instead send this as name of repository + * in in-line git://-style request (also activates sending this + * style of request). + * '%V': Only allowed as first 'character' of argument. Used in + * conjunction with '%G': Do not pass this argument to command, + * instead send this as vhost in git://-style request (note: does + * not activate sending git:// style request). + */ + +static char *git_req; +static char *git_req_vhost; + +static char *strip_escapes(const char *str, const char *service, + const char **next) +{ + size_t rpos = 0; + int escape = 0; + char special = 0; + size_t psoff = 0; + struct strbuf ret = STRBUF_INIT; + + /* Calculate prefix length for \s and lengths for \s and \S */ + if (!strncmp(service, "git-", 4)) + psoff = 4; + + /* Pass the service to command. */ + setenv("GIT_EXT_SERVICE", service, 1); + setenv("GIT_EXT_SERVICE_NOPREFIX", service + psoff, 1); + + /* Scan the length of argument. */ + while (str[rpos] && (escape || str[rpos] != ' ')) { + if (escape) { + switch (str[rpos]) { + case ' ': + case '%': + case 's': + case 'S': + break; + case 'G': + case 'V': + special = str[rpos]; + if (rpos == 1) + break; + /* Fall-through to error. */ + default: + die("Bad remote-ext placeholder '%%%c'.", + str[rpos]); + } + escape = 0; + } else + escape = (str[rpos] == '%'); + rpos++; + } + if (escape && !str[rpos]) + die("remote-ext command has incomplete placeholder"); + *next = str + rpos; + if (**next == ' ') + ++*next; /* Skip over space */ + + /* + * Do the actual placeholder substitution. The string will be short + * enough not to overflow integers. + */ + rpos = special ? 2 : 0; /* Skip first 2 bytes in specials. */ + escape = 0; + while (str[rpos] && (escape || str[rpos] != ' ')) { + if (escape) { + switch (str[rpos]) { + case ' ': + case '%': + strbuf_addch(&ret, str[rpos]); + break; + case 's': + strbuf_addstr(&ret, service + psoff); + break; + case 'S': + strbuf_addstr(&ret, service); + break; + } + escape = 0; + } else + switch (str[rpos]) { + case '%': + escape = 1; + break; + default: + strbuf_addch(&ret, str[rpos]); + break; + } + rpos++; + } + switch (special) { + case 'G': + git_req = strbuf_detach(&ret, NULL); + return NULL; + case 'V': + git_req_vhost = strbuf_detach(&ret, NULL); + return NULL; + default: + return strbuf_detach(&ret, NULL); + } +} + +/* Should be enough... */ +#define MAXARGUMENTS 256 + +static const char **parse_argv(const char *arg, const char *service) +{ + int arguments = 0; + int i; + const char **ret; + char *temparray[MAXARGUMENTS + 1]; + + while (*arg) { + char *expanded; + if (arguments == MAXARGUMENTS) + die("remote-ext command has too many arguments"); + expanded = strip_escapes(arg, service, &arg); + if (expanded) + temparray[arguments++] = expanded; + } + + ret = xmalloc((arguments + 1) * sizeof(char *)); + for (i = 0; i < arguments; i++) + ret[i] = temparray[i]; + ret[arguments] = NULL; + return ret; +} + +static void send_git_request(int stdin_fd, const char *serv, const char *repo, + const char *vhost) +{ + size_t bufferspace; + size_t wpos = 0; + char *buffer; + + /* + * Request needs 12 bytes extra if there is vhost (xxxx \0host=\0) and + * 6 bytes extra (xxxx \0) if there is no vhost. + */ + if (vhost) + bufferspace = strlen(serv) + strlen(repo) + strlen(vhost) + 12; + else + bufferspace = strlen(serv) + strlen(repo) + 6; + + if (bufferspace > 0xFFFF) + die("Request too large to send"); + buffer = xmalloc(bufferspace); + + /* Make the packet. */ + wpos = sprintf(buffer, "%04x%s %s%c", (unsigned)bufferspace, + serv, repo, 0); + + /* Add vhost if any. */ + if (vhost) + sprintf(buffer + wpos, "host=%s%c", vhost, 0); + + /* Send the request */ + if (write_in_full(stdin_fd, buffer, bufferspace) < 0) + die_errno("Failed to send request"); + + free(buffer); +} + +static int run_child(const char *arg, const char *service) +{ + int r; + struct child_process child; + + memset(&child, 0, sizeof(child)); + child.in = -1; + child.out = -1; + child.err = 0; + child.argv = parse_argv(arg, service); + + if (start_command(&child) < 0) + die("Can't run specified command"); + + if (git_req) + send_git_request(child.in, service, git_req, git_req_vhost); + + r = bidirectional_transfer_loop(child.out, child.in); + if (!r) + r = finish_command(&child); + else + finish_command(&child); + return r; +} + +#define MAXCOMMAND 4096 + +static int command_loop(const char *child) +{ + char buffer[MAXCOMMAND]; + + while (1) { + size_t i; + if (!fgets(buffer, MAXCOMMAND - 1, stdin)) { + if (ferror(stdin)) + die("Comammand input error"); + exit(0); + } + /* Strip end of line characters. */ + i = strlen(buffer); + while (i > 0 && isspace(buffer[i - 1])) + buffer[--i] = 0; + + if (!strcmp(buffer, "capabilities")) { + printf("*connect\n\n"); + fflush(stdout); + } else if (!strncmp(buffer, "connect ", 8)) { + printf("\n"); + fflush(stdout); + return run_child(child, buffer + 8); + } else { + fprintf(stderr, "Bad command"); + return 1; + } + } +} + +int cmd_remote_ext(int argc, const char **argv, const char *prefix) +{ + if (argc != 3) + die("Expected two arguments"); + + return command_loop(argv[2]); +} diff --git a/builtin/remote-fd.c b/builtin/remote-fd.c new file mode 100644 index 0000000000..08d7121b6d --- /dev/null +++ b/builtin/remote-fd.c @@ -0,0 +1,79 @@ +#include "builtin.h" +#include "transport.h" + +/* + * URL syntax: + * 'fd::<inoutfd>[/<anything>]' Read/write socket pair + * <inoutfd>. + * 'fd::<infd>,<outfd>[/<anything>]' Read pipe <infd> and write + * pipe <outfd>. + * [foo] indicates 'foo' is optional. <anything> is any string. + * + * The data output to <outfd>/<inoutfd> should be passed unmolested to + * git-receive-pack/git-upload-pack/git-upload-archive and output of + * git-receive-pack/git-upload-pack/git-upload-archive should be passed + * unmolested to <infd>/<inoutfd>. + * + */ + +#define MAXCOMMAND 4096 + +static void command_loop(int input_fd, int output_fd) +{ + char buffer[MAXCOMMAND]; + + while (1) { + size_t i; + if (!fgets(buffer, MAXCOMMAND - 1, stdin)) { + if (ferror(stdin)) + die("Input error"); + return; + } + /* Strip end of line characters. */ + i = strlen(buffer); + while (i > 0 && isspace(buffer[i - 1])) + buffer[--i] = 0; + + if (!strcmp(buffer, "capabilities")) { + printf("*connect\n\n"); + fflush(stdout); + } else if (!strncmp(buffer, "connect ", 8)) { + printf("\n"); + fflush(stdout); + if (bidirectional_transfer_loop(input_fd, + output_fd)) + die("Copying data between file descriptors failed"); + return; + } else { + die("Bad command: %s", buffer); + } + } +} + +int cmd_remote_fd(int argc, const char **argv, const char *prefix) +{ + int input_fd = -1; + int output_fd = -1; + char *end; + + if (argc != 3) + die("Expected two arguments"); + + input_fd = (int)strtoul(argv[2], &end, 10); + + if ((end == argv[2]) || (*end != ',' && *end != '/' && *end)) + die("Bad URL syntax"); + + if (*end == '/' || !*end) { + output_fd = input_fd; + } else { + char *end2; + output_fd = (int)strtoul(end + 1, &end2, 10); + + if ((end2 == end + 1) || (*end2 != '/' && *end2)) + die("Bad URL syntax"); + } + + command_loop(input_fd, output_fd); + return 0; +} diff --git a/builtin/remote.c b/builtin/remote.c new file mode 100644 index 0000000000..937484d7c7 --- /dev/null +++ b/builtin/remote.c @@ -0,0 +1,1603 @@ +#include "builtin.h" +#include "parse-options.h" +#include "transport.h" +#include "remote.h" +#include "string-list.h" +#include "strbuf.h" +#include "run-command.h" +#include "refs.h" + +static const char * const builtin_remote_usage[] = { + N_("git remote [-v | --verbose]"), + N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags|--no-tags] [--mirror=<fetch|push>] <name> <url>"), + N_("git remote rename <old> <new>"), + N_("git remote remove <name>"), + N_("git remote set-head <name> (-a | -d | <branch>)"), + N_("git remote [-v | --verbose] show [-n] <name>"), + N_("git remote prune [-n | --dry-run] <name>"), + N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"), + N_("git remote set-branches [--add] <name> <branch>..."), + N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), + N_("git remote set-url --add <name> <newurl>"), + N_("git remote set-url --delete <name> <url>"), + NULL +}; + +static const char * const builtin_remote_add_usage[] = { + N_("git remote add [<options>] <name> <url>"), + NULL +}; + +static const char * const builtin_remote_rename_usage[] = { + N_("git remote rename <old> <new>"), + NULL +}; + +static const char * const builtin_remote_rm_usage[] = { + N_("git remote remove <name>"), + NULL +}; + +static const char * const builtin_remote_sethead_usage[] = { + N_("git remote set-head <name> (-a | -d | <branch>)"), + NULL +}; + +static const char * const builtin_remote_setbranches_usage[] = { + N_("git remote set-branches <name> <branch>..."), + N_("git remote set-branches --add <name> <branch>..."), + NULL +}; + +static const char * const builtin_remote_show_usage[] = { + N_("git remote show [<options>] <name>"), + NULL +}; + +static const char * const builtin_remote_prune_usage[] = { + N_("git remote prune [<options>] <name>"), + NULL +}; + +static const char * const builtin_remote_update_usage[] = { + N_("git remote update [<options>] [<group> | <remote>]..."), + NULL +}; + +static const char * const builtin_remote_seturl_usage[] = { + N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), + N_("git remote set-url --add <name> <newurl>"), + N_("git remote set-url --delete <name> <url>"), + NULL +}; + +#define GET_REF_STATES (1<<0) +#define GET_HEAD_NAMES (1<<1) +#define GET_PUSH_REF_STATES (1<<2) + +static int verbose; + +static int show_all(void); +static int prune_remote(const char *remote, int dry_run); + +static inline int postfixcmp(const char *string, const char *postfix) +{ + int len1 = strlen(string), len2 = strlen(postfix); + if (len1 < len2) + return 1; + return strcmp(string + len1 - len2, postfix); +} + +static int fetch_remote(const char *name) +{ + const char *argv[] = { "fetch", name, NULL, NULL }; + if (verbose) { + argv[1] = "-v"; + argv[2] = name; + } + printf_ln(_("Updating %s"), name); + if (run_command_v_opt(argv, RUN_GIT_CMD)) + return error(_("Could not fetch %s"), name); + return 0; +} + +enum { + TAGS_UNSET = 0, + TAGS_DEFAULT = 1, + TAGS_SET = 2 +}; + +#define MIRROR_NONE 0 +#define MIRROR_FETCH 1 +#define MIRROR_PUSH 2 +#define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH) + +static int add_branch(const char *key, const char *branchname, + const char *remotename, int mirror, struct strbuf *tmp) +{ + strbuf_reset(tmp); + strbuf_addch(tmp, '+'); + if (mirror) + strbuf_addf(tmp, "refs/%s:refs/%s", + branchname, branchname); + else + strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s", + branchname, remotename, branchname); + return git_config_set_multivar(key, tmp->buf, "^$", 0); +} + +static const char mirror_advice[] = +N_("--mirror is dangerous and deprecated; please\n" + "\t use --mirror=fetch or --mirror=push instead"); + +static int parse_mirror_opt(const struct option *opt, const char *arg, int not) +{ + unsigned *mirror = opt->value; + if (not) + *mirror = MIRROR_NONE; + else if (!arg) { + warning("%s", _(mirror_advice)); + *mirror = MIRROR_BOTH; + } + else if (!strcmp(arg, "fetch")) + *mirror = MIRROR_FETCH; + else if (!strcmp(arg, "push")) + *mirror = MIRROR_PUSH; + else + return error(_("unknown mirror argument: %s"), arg); + return 0; +} + +static int add(int argc, const char **argv) +{ + int fetch = 0, fetch_tags = TAGS_DEFAULT; + unsigned mirror = MIRROR_NONE; + struct string_list track = STRING_LIST_INIT_NODUP; + const char *master = NULL; + struct remote *remote; + struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; + const char *name, *url; + int i; + + struct option options[] = { + OPT_BOOLEAN('f', "fetch", &fetch, N_("fetch the remote branches")), + OPT_SET_INT(0, "tags", &fetch_tags, + N_("import all tags and associated objects when fetching"), + TAGS_SET), + OPT_SET_INT(0, NULL, &fetch_tags, + N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET), + OPT_STRING_LIST('t', "track", &track, N_("branch"), + N_("branch(es) to track")), + OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")), + { OPTION_CALLBACK, 0, "mirror", &mirror, N_("push|fetch"), + N_("set up remote as a mirror to push to or fetch from"), + PARSE_OPT_OPTARG, parse_mirror_opt }, + OPT_END() + }; + + argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage, + 0); + + if (argc < 2) + usage_with_options(builtin_remote_add_usage, options); + + if (mirror && master) + die(_("specifying a master branch makes no sense with --mirror")); + if (mirror && !(mirror & MIRROR_FETCH) && track.nr) + die(_("specifying branches to track makes sense only with fetch mirrors")); + + name = argv[0]; + url = argv[1]; + + remote = remote_get(name); + if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) || + remote->fetch_refspec_nr)) + die(_("remote %s already exists."), name); + + strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name); + if (!valid_fetch_refspec(buf2.buf)) + die(_("'%s' is not a valid remote name"), name); + + strbuf_addf(&buf, "remote.%s.url", name); + if (git_config_set(buf.buf, url)) + return 1; + + if (!mirror || mirror & MIRROR_FETCH) { + strbuf_reset(&buf); + strbuf_addf(&buf, "remote.%s.fetch", name); + if (track.nr == 0) + string_list_append(&track, "*"); + for (i = 0; i < track.nr; i++) { + if (add_branch(buf.buf, track.items[i].string, + name, mirror, &buf2)) + return 1; + } + } + + if (mirror & MIRROR_PUSH) { + strbuf_reset(&buf); + strbuf_addf(&buf, "remote.%s.mirror", name); + if (git_config_set(buf.buf, "true")) + return 1; + } + + if (fetch_tags != TAGS_DEFAULT) { + strbuf_reset(&buf); + strbuf_addf(&buf, "remote.%s.tagopt", name); + if (git_config_set(buf.buf, + fetch_tags == TAGS_SET ? "--tags" : "--no-tags")) + return 1; + } + + if (fetch && fetch_remote(name)) + return 1; + + if (master) { + strbuf_reset(&buf); + strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); + + strbuf_reset(&buf2); + strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); + + if (create_symref(buf.buf, buf2.buf, "remote add")) + return error(_("Could not setup master '%s'"), master); + } + + strbuf_release(&buf); + strbuf_release(&buf2); + string_list_clear(&track, 0); + + return 0; +} + +struct branch_info { + char *remote_name; + struct string_list merge; + int rebase; +}; + +static struct string_list branch_list; + +static const char *abbrev_ref(const char *name, const char *prefix) +{ + const char *abbrev = skip_prefix(name, prefix); + if (abbrev) + return abbrev; + return name; +} +#define abbrev_branch(name) abbrev_ref((name), "refs/heads/") + +static int config_read_branches(const char *key, const char *value, void *cb) +{ + if (!prefixcmp(key, "branch.")) { + const char *orig_key = key; + char *name; + struct string_list_item *item; + struct branch_info *info; + enum { REMOTE, MERGE, REBASE } type; + + key += 7; + if (!postfixcmp(key, ".remote")) { + name = xstrndup(key, strlen(key) - 7); + type = REMOTE; + } else if (!postfixcmp(key, ".merge")) { + name = xstrndup(key, strlen(key) - 6); + type = MERGE; + } else if (!postfixcmp(key, ".rebase")) { + name = xstrndup(key, strlen(key) - 7); + type = REBASE; + } else + return 0; + + item = string_list_insert(&branch_list, name); + + if (!item->util) + item->util = xcalloc(sizeof(struct branch_info), 1); + info = item->util; + if (type == REMOTE) { + if (info->remote_name) + warning(_("more than one %s"), orig_key); + info->remote_name = xstrdup(value); + } else if (type == MERGE) { + char *space = strchr(value, ' '); + value = abbrev_branch(value); + while (space) { + char *merge; + merge = xstrndup(value, space - value); + string_list_append(&info->merge, merge); + value = abbrev_branch(space + 1); + space = strchr(value, ' '); + } + string_list_append(&info->merge, xstrdup(value)); + } else + info->rebase = git_config_bool(orig_key, value); + } + return 0; +} + +static void read_branches(void) +{ + if (branch_list.nr) + return; + git_config(config_read_branches, NULL); +} + +struct ref_states { + struct remote *remote; + struct string_list new, stale, tracked, heads, push; + int queried; +}; + +static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) +{ + struct ref *fetch_map = NULL, **tail = &fetch_map; + struct ref *ref, *stale_refs; + int i; + + for (i = 0; i < states->remote->fetch_refspec_nr; i++) + if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1)) + die(_("Could not get fetch map for refspec %s"), + states->remote->fetch_refspec[i]); + + states->new.strdup_strings = 1; + states->tracked.strdup_strings = 1; + states->stale.strdup_strings = 1; + for (ref = fetch_map; ref; ref = ref->next) { + if (!ref->peer_ref || !ref_exists(ref->peer_ref->name)) + string_list_append(&states->new, abbrev_branch(ref->name)); + else + string_list_append(&states->tracked, abbrev_branch(ref->name)); + } + stale_refs = get_stale_heads(states->remote->fetch, + states->remote->fetch_refspec_nr, fetch_map); + for (ref = stale_refs; ref; ref = ref->next) { + struct string_list_item *item = + string_list_append(&states->stale, abbrev_branch(ref->name)); + item->util = xstrdup(ref->name); + } + free_refs(stale_refs); + free_refs(fetch_map); + + sort_string_list(&states->new); + sort_string_list(&states->tracked); + sort_string_list(&states->stale); + + return 0; +} + +struct push_info { + char *dest; + int forced; + enum { + PUSH_STATUS_CREATE = 0, + PUSH_STATUS_DELETE, + PUSH_STATUS_UPTODATE, + PUSH_STATUS_FASTFORWARD, + PUSH_STATUS_OUTOFDATE, + PUSH_STATUS_NOTQUERIED + } status; +}; + +static int get_push_ref_states(const struct ref *remote_refs, + struct ref_states *states) +{ + struct remote *remote = states->remote; + struct ref *ref, *local_refs, *push_map; + if (remote->mirror) + return 0; + + local_refs = get_local_heads(); + push_map = copy_ref_list(remote_refs); + + match_push_refs(local_refs, &push_map, remote->push_refspec_nr, + remote->push_refspec, MATCH_REFS_NONE); + + states->push.strdup_strings = 1; + for (ref = push_map; ref; ref = ref->next) { + struct string_list_item *item; + struct push_info *info; + + if (!ref->peer_ref) + continue; + hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); + + item = string_list_append(&states->push, + abbrev_branch(ref->peer_ref->name)); + item->util = xcalloc(sizeof(struct push_info), 1); + info = item->util; + info->forced = ref->force; + info->dest = xstrdup(abbrev_branch(ref->name)); + + if (is_null_sha1(ref->new_sha1)) { + info->status = PUSH_STATUS_DELETE; + } else if (!hashcmp(ref->old_sha1, ref->new_sha1)) + info->status = PUSH_STATUS_UPTODATE; + else if (is_null_sha1(ref->old_sha1)) + info->status = PUSH_STATUS_CREATE; + else if (has_sha1_file(ref->old_sha1) && + ref_newer(ref->new_sha1, ref->old_sha1)) + info->status = PUSH_STATUS_FASTFORWARD; + else + info->status = PUSH_STATUS_OUTOFDATE; + } + free_refs(local_refs); + free_refs(push_map); + return 0; +} + +static int get_push_ref_states_noquery(struct ref_states *states) +{ + int i; + struct remote *remote = states->remote; + struct string_list_item *item; + struct push_info *info; + + if (remote->mirror) + return 0; + + states->push.strdup_strings = 1; + if (!remote->push_refspec_nr) { + item = string_list_append(&states->push, _("(matching)")); + info = item->util = xcalloc(sizeof(struct push_info), 1); + info->status = PUSH_STATUS_NOTQUERIED; + info->dest = xstrdup(item->string); + } + for (i = 0; i < remote->push_refspec_nr; i++) { + struct refspec *spec = remote->push + i; + if (spec->matching) + item = string_list_append(&states->push, _("(matching)")); + else if (strlen(spec->src)) + item = string_list_append(&states->push, spec->src); + else + item = string_list_append(&states->push, _("(delete)")); + + info = item->util = xcalloc(sizeof(struct push_info), 1); + info->forced = spec->force; + info->status = PUSH_STATUS_NOTQUERIED; + info->dest = xstrdup(spec->dst ? spec->dst : item->string); + } + return 0; +} + +static int get_head_names(const struct ref *remote_refs, struct ref_states *states) +{ + struct ref *ref, *matches; + struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; + struct refspec refspec; + + refspec.force = 0; + refspec.pattern = 1; + refspec.src = refspec.dst = "refs/heads/*"; + states->heads.strdup_strings = 1; + get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); + matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), + fetch_map, 1); + for (ref = matches; ref; ref = ref->next) + string_list_append(&states->heads, abbrev_branch(ref->name)); + + free_refs(fetch_map); + free_refs(matches); + + return 0; +} + +struct known_remote { + struct known_remote *next; + struct remote *remote; +}; + +struct known_remotes { + struct remote *to_delete; + struct known_remote *list; +}; + +static int add_known_remote(struct remote *remote, void *cb_data) +{ + struct known_remotes *all = cb_data; + struct known_remote *r; + + if (!strcmp(all->to_delete->name, remote->name)) + return 0; + + r = xmalloc(sizeof(*r)); + r->remote = remote; + r->next = all->list; + all->list = r; + return 0; +} + +struct branches_for_remote { + struct remote *remote; + struct string_list *branches, *skipped; + struct known_remotes *keep; +}; + +static int add_branch_for_removal(const char *refname, + const unsigned char *sha1, int flags, void *cb_data) +{ + struct branches_for_remote *branches = cb_data; + struct refspec refspec; + struct string_list_item *item; + struct known_remote *kr; + + memset(&refspec, 0, sizeof(refspec)); + refspec.dst = (char *)refname; + if (remote_find_tracking(branches->remote, &refspec)) + return 0; + + /* don't delete a branch if another remote also uses it */ + for (kr = branches->keep->list; kr; kr = kr->next) { + memset(&refspec, 0, sizeof(refspec)); + refspec.dst = (char *)refname; + if (!remote_find_tracking(kr->remote, &refspec)) + return 0; + } + + /* don't delete non-remote-tracking refs */ + if (prefixcmp(refname, "refs/remotes/")) { + /* advise user how to delete local branches */ + if (!prefixcmp(refname, "refs/heads/")) + string_list_append(branches->skipped, + abbrev_branch(refname)); + /* silently skip over other non-remote refs */ + return 0; + } + + /* make sure that symrefs are deleted */ + if (flags & REF_ISSYMREF) + return unlink(git_path("%s", refname)); + + item = string_list_append(branches->branches, refname); + item->util = xmalloc(20); + hashcpy(item->util, sha1); + + return 0; +} + +struct rename_info { + const char *old; + const char *new; + struct string_list *remote_branches; +}; + +static int read_remote_branches(const char *refname, + const unsigned char *sha1, int flags, void *cb_data) +{ + struct rename_info *rename = cb_data; + struct strbuf buf = STRBUF_INIT; + struct string_list_item *item; + int flag; + unsigned char orig_sha1[20]; + const char *symref; + + strbuf_addf(&buf, "refs/remotes/%s/", rename->old); + if (!prefixcmp(refname, buf.buf)) { + item = string_list_append(rename->remote_branches, xstrdup(refname)); + symref = resolve_ref_unsafe(refname, orig_sha1, 1, &flag); + if (flag & REF_ISSYMREF) + item->util = xstrdup(symref); + else + item->util = NULL; + } + + return 0; +} + +static int migrate_file(struct remote *remote) +{ + struct strbuf buf = STRBUF_INIT; + int i; + char *path = NULL; + + strbuf_addf(&buf, "remote.%s.url", remote->name); + for (i = 0; i < remote->url_nr; i++) + if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0)) + return error(_("Could not append '%s' to '%s'"), + remote->url[i], buf.buf); + strbuf_reset(&buf); + strbuf_addf(&buf, "remote.%s.push", remote->name); + for (i = 0; i < remote->push_refspec_nr; i++) + if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0)) + return error(_("Could not append '%s' to '%s'"), + remote->push_refspec[i], buf.buf); + strbuf_reset(&buf); + strbuf_addf(&buf, "remote.%s.fetch", remote->name); + for (i = 0; i < remote->fetch_refspec_nr; i++) + if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0)) + return error(_("Could not append '%s' to '%s'"), + remote->fetch_refspec[i], buf.buf); + if (remote->origin == REMOTE_REMOTES) + path = git_path("remotes/%s", remote->name); + else if (remote->origin == REMOTE_BRANCHES) + path = git_path("branches/%s", remote->name); + if (path) + unlink_or_warn(path); + return 0; +} + +static int mv(int argc, const char **argv) +{ + struct option options[] = { + OPT_END() + }; + struct remote *oldremote, *newremote; + struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT, + old_remote_context = STRBUF_INIT; + struct string_list remote_branches = STRING_LIST_INIT_NODUP; + struct rename_info rename; + int i, refspec_updated = 0; + + if (argc != 3) + usage_with_options(builtin_remote_rename_usage, options); + + rename.old = argv[1]; + rename.new = argv[2]; + rename.remote_branches = &remote_branches; + + oldremote = remote_get(rename.old); + if (!oldremote) + die(_("No such remote: %s"), rename.old); + + if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG) + return migrate_file(oldremote); + + newremote = remote_get(rename.new); + if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr)) + die(_("remote %s already exists."), rename.new); + + strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new); + if (!valid_fetch_refspec(buf.buf)) + die(_("'%s' is not a valid remote name"), rename.new); + + strbuf_reset(&buf); + strbuf_addf(&buf, "remote.%s", rename.old); + strbuf_addf(&buf2, "remote.%s", rename.new); + if (git_config_rename_section(buf.buf, buf2.buf) < 1) + return error(_("Could not rename config section '%s' to '%s'"), + buf.buf, buf2.buf); + + strbuf_reset(&buf); + strbuf_addf(&buf, "remote.%s.fetch", rename.new); + if (git_config_set_multivar(buf.buf, NULL, NULL, 1)) + return error(_("Could not remove config section '%s'"), buf.buf); + strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old); + for (i = 0; i < oldremote->fetch_refspec_nr; i++) { + char *ptr; + + strbuf_reset(&buf2); + strbuf_addstr(&buf2, oldremote->fetch_refspec[i]); + ptr = strstr(buf2.buf, old_remote_context.buf); + if (ptr) { + refspec_updated = 1; + strbuf_splice(&buf2, + ptr-buf2.buf + strlen(":refs/remotes/"), + strlen(rename.old), rename.new, + strlen(rename.new)); + } else + warning(_("Not updating non-default fetch refspec\n" + "\t%s\n" + "\tPlease update the configuration manually if necessary."), + buf2.buf); + + if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0)) + return error(_("Could not append '%s'"), buf.buf); + } + + read_branches(); + for (i = 0; i < branch_list.nr; i++) { + struct string_list_item *item = branch_list.items + i; + struct branch_info *info = item->util; + if (info->remote_name && !strcmp(info->remote_name, rename.old)) { + strbuf_reset(&buf); + strbuf_addf(&buf, "branch.%s.remote", item->string); + if (git_config_set(buf.buf, rename.new)) { + return error(_("Could not set '%s'"), buf.buf); + } + } + } + + if (!refspec_updated) + return 0; + + /* + * First remove symrefs, then rename the rest, finally create + * the new symrefs. + */ + for_each_ref(read_remote_branches, &rename); + for (i = 0; i < remote_branches.nr; i++) { + struct string_list_item *item = remote_branches.items + i; + int flag = 0; + unsigned char sha1[20]; + + read_ref_full(item->string, sha1, 1, &flag); + if (!(flag & REF_ISSYMREF)) + continue; + if (delete_ref(item->string, NULL, REF_NODEREF)) + die(_("deleting '%s' failed"), item->string); + } + for (i = 0; i < remote_branches.nr; i++) { + struct string_list_item *item = remote_branches.items + i; + + if (item->util) + continue; + strbuf_reset(&buf); + strbuf_addstr(&buf, item->string); + strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), + rename.new, strlen(rename.new)); + strbuf_reset(&buf2); + strbuf_addf(&buf2, "remote: renamed %s to %s", + item->string, buf.buf); + if (rename_ref(item->string, buf.buf, buf2.buf)) + die(_("renaming '%s' failed"), item->string); + } + for (i = 0; i < remote_branches.nr; i++) { + struct string_list_item *item = remote_branches.items + i; + + if (!item->util) + continue; + strbuf_reset(&buf); + strbuf_addstr(&buf, item->string); + strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), + rename.new, strlen(rename.new)); + strbuf_reset(&buf2); + strbuf_addstr(&buf2, item->util); + strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old), + rename.new, strlen(rename.new)); + strbuf_reset(&buf3); + strbuf_addf(&buf3, "remote: renamed %s to %s", + item->string, buf.buf); + if (create_symref(buf.buf, buf2.buf, buf3.buf)) + die(_("creating '%s' failed"), buf.buf); + } + return 0; +} + +static int remove_branches(struct string_list *branches) +{ + int i, result = 0; + for (i = 0; i < branches->nr; i++) { + struct string_list_item *item = branches->items + i; + const char *refname = item->string; + unsigned char *sha1 = item->util; + + if (delete_ref(refname, sha1, 0)) + result |= error(_("Could not remove branch %s"), refname); + } + return result; +} + +static int rm(int argc, const char **argv) +{ + struct option options[] = { + OPT_END() + }; + struct remote *remote; + struct strbuf buf = STRBUF_INIT; + struct known_remotes known_remotes = { NULL, NULL }; + struct string_list branches = STRING_LIST_INIT_DUP; + struct string_list skipped = STRING_LIST_INIT_DUP; + struct branches_for_remote cb_data; + int i, result; + + memset(&cb_data, 0, sizeof(cb_data)); + cb_data.branches = &branches; + cb_data.skipped = &skipped; + cb_data.keep = &known_remotes; + + if (argc != 2) + usage_with_options(builtin_remote_rm_usage, options); + + remote = remote_get(argv[1]); + if (!remote) + die(_("No such remote: %s"), argv[1]); + + known_remotes.to_delete = remote; + for_each_remote(add_known_remote, &known_remotes); + + strbuf_addf(&buf, "remote.%s", remote->name); + if (git_config_rename_section(buf.buf, NULL) < 1) + return error(_("Could not remove config section '%s'"), buf.buf); + + read_branches(); + for (i = 0; i < branch_list.nr; i++) { + struct string_list_item *item = branch_list.items + i; + struct branch_info *info = item->util; + if (info->remote_name && !strcmp(info->remote_name, remote->name)) { + const char *keys[] = { "remote", "merge", NULL }, **k; + for (k = keys; *k; k++) { + strbuf_reset(&buf); + strbuf_addf(&buf, "branch.%s.%s", + item->string, *k); + if (git_config_set(buf.buf, NULL)) { + strbuf_release(&buf); + return -1; + } + } + } + } + + /* + * We cannot just pass a function to for_each_ref() which deletes + * the branches one by one, since for_each_ref() relies on cached + * refs, which are invalidated when deleting a branch. + */ + cb_data.remote = remote; + result = for_each_ref(add_branch_for_removal, &cb_data); + strbuf_release(&buf); + + if (!result) + result = remove_branches(&branches); + string_list_clear(&branches, 1); + + if (skipped.nr) { + fprintf_ln(stderr, + Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n" + "to delete it, use:", + "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n" + "to delete them, use:", + skipped.nr)); + for (i = 0; i < skipped.nr; i++) + fprintf(stderr, " git branch -d %s\n", + skipped.items[i].string); + } + string_list_clear(&skipped, 0); + + return result; +} + +static void clear_push_info(void *util, const char *string) +{ + struct push_info *info = util; + free(info->dest); + free(info); +} + +static void free_remote_ref_states(struct ref_states *states) +{ + string_list_clear(&states->new, 0); + string_list_clear(&states->stale, 1); + string_list_clear(&states->tracked, 0); + string_list_clear(&states->heads, 0); + string_list_clear_func(&states->push, clear_push_info); +} + +static int append_ref_to_tracked_list(const char *refname, + const unsigned char *sha1, int flags, void *cb_data) +{ + struct ref_states *states = cb_data; + struct refspec refspec; + + if (flags & REF_ISSYMREF) + return 0; + + memset(&refspec, 0, sizeof(refspec)); + refspec.dst = (char *)refname; + if (!remote_find_tracking(states->remote, &refspec)) + string_list_append(&states->tracked, abbrev_branch(refspec.src)); + + return 0; +} + +static int get_remote_ref_states(const char *name, + struct ref_states *states, + int query) +{ + struct transport *transport; + const struct ref *remote_refs; + + states->remote = remote_get(name); + if (!states->remote) + return error(_("No such remote: %s"), name); + + read_branches(); + + if (query) { + transport = transport_get(states->remote, states->remote->url_nr > 0 ? + states->remote->url[0] : NULL); + remote_refs = transport_get_remote_refs(transport); + transport_disconnect(transport); + + states->queried = 1; + if (query & GET_REF_STATES) + get_ref_states(remote_refs, states); + if (query & GET_HEAD_NAMES) + get_head_names(remote_refs, states); + if (query & GET_PUSH_REF_STATES) + get_push_ref_states(remote_refs, states); + } else { + for_each_ref(append_ref_to_tracked_list, states); + sort_string_list(&states->tracked); + get_push_ref_states_noquery(states); + } + + return 0; +} + +struct show_info { + struct string_list *list; + struct ref_states *states; + int width, width2; + int any_rebase; +}; + +static int add_remote_to_show_info(struct string_list_item *item, void *cb_data) +{ + struct show_info *info = cb_data; + int n = strlen(item->string); + if (n > info->width) + info->width = n; + string_list_insert(info->list, item->string); + return 0; +} + +static int show_remote_info_item(struct string_list_item *item, void *cb_data) +{ + struct show_info *info = cb_data; + struct ref_states *states = info->states; + const char *name = item->string; + + if (states->queried) { + const char *fmt = "%s"; + const char *arg = ""; + if (string_list_has_string(&states->new, name)) { + fmt = _(" new (next fetch will store in remotes/%s)"); + arg = states->remote->name; + } else if (string_list_has_string(&states->tracked, name)) + arg = _(" tracked"); + else if (string_list_has_string(&states->stale, name)) + arg = _(" stale (use 'git remote prune' to remove)"); + else + arg = _(" ???"); + printf(" %-*s", info->width, name); + printf(fmt, arg); + printf("\n"); + } else + printf(" %s\n", name); + + return 0; +} + +static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data) +{ + struct show_info *show_info = cb_data; + struct ref_states *states = show_info->states; + struct branch_info *branch_info = branch_item->util; + struct string_list_item *item; + int n; + + if (!branch_info->merge.nr || !branch_info->remote_name || + strcmp(states->remote->name, branch_info->remote_name)) + return 0; + if ((n = strlen(branch_item->string)) > show_info->width) + show_info->width = n; + if (branch_info->rebase) + show_info->any_rebase = 1; + + item = string_list_insert(show_info->list, branch_item->string); + item->util = branch_info; + + return 0; +} + +static int show_local_info_item(struct string_list_item *item, void *cb_data) +{ + struct show_info *show_info = cb_data; + struct branch_info *branch_info = item->util; + struct string_list *merge = &branch_info->merge; + const char *also; + int i; + + if (branch_info->rebase && branch_info->merge.nr > 1) { + error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"), + item->string); + return 0; + } + + printf(" %-*s ", show_info->width, item->string); + if (branch_info->rebase) { + printf_ln(_("rebases onto remote %s"), merge->items[0].string); + return 0; + } else if (show_info->any_rebase) { + printf_ln(_(" merges with remote %s"), merge->items[0].string); + also = _(" and with remote"); + } else { + printf_ln(_("merges with remote %s"), merge->items[0].string); + also = _(" and with remote"); + } + for (i = 1; i < merge->nr; i++) + printf(" %-*s %s %s\n", show_info->width, "", also, + merge->items[i].string); + + return 0; +} + +static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data) +{ + struct show_info *show_info = cb_data; + struct push_info *push_info = push_item->util; + struct string_list_item *item; + int n; + if ((n = strlen(push_item->string)) > show_info->width) + show_info->width = n; + if ((n = strlen(push_info->dest)) > show_info->width2) + show_info->width2 = n; + item = string_list_append(show_info->list, push_item->string); + item->util = push_item->util; + return 0; +} + +/* + * Sorting comparison for a string list that has push_info + * structs in its util field + */ +static int cmp_string_with_push(const void *va, const void *vb) +{ + const struct string_list_item *a = va; + const struct string_list_item *b = vb; + const struct push_info *a_push = a->util; + const struct push_info *b_push = b->util; + int cmp = strcmp(a->string, b->string); + return cmp ? cmp : strcmp(a_push->dest, b_push->dest); +} + +static int show_push_info_item(struct string_list_item *item, void *cb_data) +{ + struct show_info *show_info = cb_data; + struct push_info *push_info = item->util; + const char *src = item->string, *status = NULL; + + switch (push_info->status) { + case PUSH_STATUS_CREATE: + status = _("create"); + break; + case PUSH_STATUS_DELETE: + status = _("delete"); + src = _("(none)"); + break; + case PUSH_STATUS_UPTODATE: + status = _("up to date"); + break; + case PUSH_STATUS_FASTFORWARD: + status = _("fast-forwardable"); + break; + case PUSH_STATUS_OUTOFDATE: + status = _("local out of date"); + break; + case PUSH_STATUS_NOTQUERIED: + break; + } + if (status) { + if (push_info->forced) + printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src, + show_info->width2, push_info->dest, status); + else + printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src, + show_info->width2, push_info->dest, status); + } else { + if (push_info->forced) + printf_ln(_(" %-*s forces to %s"), show_info->width, src, + push_info->dest); + else + printf_ln(_(" %-*s pushes to %s"), show_info->width, src, + push_info->dest); + } + return 0; +} + +static int show(int argc, const char **argv) +{ + int no_query = 0, result = 0, query_flag = 0; + struct option options[] = { + OPT_BOOLEAN('n', NULL, &no_query, N_("do not query remotes")), + OPT_END() + }; + struct ref_states states; + struct string_list info_list = STRING_LIST_INIT_NODUP; + struct show_info info; + + argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage, + 0); + + if (argc < 1) + return show_all(); + + if (!no_query) + query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES); + + memset(&states, 0, sizeof(states)); + memset(&info, 0, sizeof(info)); + info.states = &states; + info.list = &info_list; + for (; argc; argc--, argv++) { + int i; + const char **url; + int url_nr; + + get_remote_ref_states(*argv, &states, query_flag); + + printf_ln(_("* remote %s"), *argv); + printf_ln(_(" Fetch URL: %s"), states.remote->url_nr > 0 ? + states.remote->url[0] : _("(no URL)")); + if (states.remote->pushurl_nr) { + url = states.remote->pushurl; + url_nr = states.remote->pushurl_nr; + } else { + url = states.remote->url; + url_nr = states.remote->url_nr; + } + for (i = 0; i < url_nr; i++) + printf_ln(_(" Push URL: %s"), url[i]); + if (!i) + printf_ln(_(" Push URL: %s"), "(no URL)"); + if (no_query) + printf_ln(_(" HEAD branch: %s"), "(not queried)"); + else if (!states.heads.nr) + printf_ln(_(" HEAD branch: %s"), "(unknown)"); + else if (states.heads.nr == 1) + printf_ln(_(" HEAD branch: %s"), states.heads.items[0].string); + else { + printf(_(" HEAD branch (remote HEAD is ambiguous," + " may be one of the following):\n")); + for (i = 0; i < states.heads.nr; i++) + printf(" %s\n", states.heads.items[i].string); + } + + /* remote branch info */ + info.width = 0; + for_each_string_list(&states.new, add_remote_to_show_info, &info); + for_each_string_list(&states.tracked, add_remote_to_show_info, &info); + for_each_string_list(&states.stale, add_remote_to_show_info, &info); + if (info.list->nr) + printf_ln(Q_(" Remote branch:%s", + " Remote branches:%s", + info.list->nr), + no_query ? _(" (status not queried)") : ""); + for_each_string_list(info.list, show_remote_info_item, &info); + string_list_clear(info.list, 0); + + /* git pull info */ + info.width = 0; + info.any_rebase = 0; + for_each_string_list(&branch_list, add_local_to_show_info, &info); + if (info.list->nr) + printf_ln(Q_(" Local branch configured for 'git pull':", + " Local branches configured for 'git pull':", + info.list->nr)); + for_each_string_list(info.list, show_local_info_item, &info); + string_list_clear(info.list, 0); + + /* git push info */ + if (states.remote->mirror) + printf_ln(_(" Local refs will be mirrored by 'git push'")); + + info.width = info.width2 = 0; + for_each_string_list(&states.push, add_push_to_show_info, &info); + qsort(info.list->items, info.list->nr, + sizeof(*info.list->items), cmp_string_with_push); + if (info.list->nr) + printf_ln(Q_(" Local ref configured for 'git push'%s:", + " Local refs configured for 'git push'%s:", + info.list->nr), + no_query ? _(" (status not queried)") : ""); + for_each_string_list(info.list, show_push_info_item, &info); + string_list_clear(info.list, 0); + + free_remote_ref_states(&states); + } + + return result; +} + +static int set_head(int argc, const char **argv) +{ + int i, opt_a = 0, opt_d = 0, result = 0; + struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; + char *head_name = NULL; + + struct option options[] = { + OPT_BOOLEAN('a', "auto", &opt_a, + N_("set refs/remotes/<name>/HEAD according to remote")), + OPT_BOOLEAN('d', "delete", &opt_d, + N_("delete refs/remotes/<name>/HEAD")), + OPT_END() + }; + argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage, + 0); + if (argc) + strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]); + + if (!opt_a && !opt_d && argc == 2) { + head_name = xstrdup(argv[1]); + } else if (opt_a && !opt_d && argc == 1) { + struct ref_states states; + memset(&states, 0, sizeof(states)); + get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES); + if (!states.heads.nr) + result |= error(_("Cannot determine remote HEAD")); + else if (states.heads.nr > 1) { + result |= error(_("Multiple remote HEAD branches. " + "Please choose one explicitly with:")); + for (i = 0; i < states.heads.nr; i++) + fprintf(stderr, " git remote set-head %s %s\n", + argv[0], states.heads.items[i].string); + } else + head_name = xstrdup(states.heads.items[0].string); + free_remote_ref_states(&states); + } else if (opt_d && !opt_a && argc == 1) { + if (delete_ref(buf.buf, NULL, REF_NODEREF)) + result |= error(_("Could not delete %s"), buf.buf); + } else + usage_with_options(builtin_remote_sethead_usage, options); + + if (head_name) { + strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name); + /* make sure it's valid */ + if (!ref_exists(buf2.buf)) + result |= error(_("Not a valid ref: %s"), buf2.buf); + else if (create_symref(buf.buf, buf2.buf, "remote set-head")) + result |= error(_("Could not setup %s"), buf.buf); + if (opt_a) + printf("%s/HEAD set to %s\n", argv[0], head_name); + free(head_name); + } + + strbuf_release(&buf); + strbuf_release(&buf2); + return result; +} + +static int prune(int argc, const char **argv) +{ + int dry_run = 0, result = 0; + struct option options[] = { + OPT__DRY_RUN(&dry_run, N_("dry run")), + OPT_END() + }; + + argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage, + 0); + + if (argc < 1) + usage_with_options(builtin_remote_prune_usage, options); + + for (; argc; argc--, argv++) + result |= prune_remote(*argv, dry_run); + + return result; +} + +static int prune_remote(const char *remote, int dry_run) +{ + int result = 0, i; + struct ref_states states; + const char *dangling_msg = dry_run + ? _(" %s will become dangling!") + : _(" %s has become dangling!"); + + memset(&states, 0, sizeof(states)); + get_remote_ref_states(remote, &states, GET_REF_STATES); + + if (states.stale.nr) { + printf_ln(_("Pruning %s"), remote); + printf_ln(_("URL: %s"), + states.remote->url_nr + ? states.remote->url[0] + : _("(no URL)")); + } + + for (i = 0; i < states.stale.nr; i++) { + const char *refname = states.stale.items[i].util; + + if (!dry_run) + result |= delete_ref(refname, NULL, 0); + + if (dry_run) + printf_ln(_(" * [would prune] %s"), + abbrev_ref(refname, "refs/remotes/")); + else + printf_ln(_(" * [pruned] %s"), + abbrev_ref(refname, "refs/remotes/")); + warn_dangling_symref(stdout, dangling_msg, refname); + } + + free_remote_ref_states(&states); + return result; +} + +static int get_remote_default(const char *key, const char *value, void *priv) +{ + if (strcmp(key, "remotes.default") == 0) { + int *found = priv; + *found = 1; + } + return 0; +} + +static int update(int argc, const char **argv) +{ + int i, prune = 0; + struct option options[] = { + OPT_BOOLEAN('p', "prune", &prune, + N_("prune remotes after fetching")), + OPT_END() + }; + const char **fetch_argv; + int fetch_argc = 0; + int default_defined = 0; + + fetch_argv = xmalloc(sizeof(char *) * (argc+5)); + + argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage, + PARSE_OPT_KEEP_ARGV0); + + fetch_argv[fetch_argc++] = "fetch"; + + if (prune) + fetch_argv[fetch_argc++] = "--prune"; + if (verbose) + fetch_argv[fetch_argc++] = "-v"; + fetch_argv[fetch_argc++] = "--multiple"; + if (argc < 2) + fetch_argv[fetch_argc++] = "default"; + for (i = 1; i < argc; i++) + fetch_argv[fetch_argc++] = argv[i]; + + if (strcmp(fetch_argv[fetch_argc-1], "default") == 0) { + git_config(get_remote_default, &default_defined); + if (!default_defined) + fetch_argv[fetch_argc-1] = "--all"; + } + + fetch_argv[fetch_argc] = NULL; + + return run_command_v_opt(fetch_argv, RUN_GIT_CMD); +} + +static int remove_all_fetch_refspecs(const char *remote, const char *key) +{ + return git_config_set_multivar(key, NULL, NULL, 1); +} + +static int add_branches(struct remote *remote, const char **branches, + const char *key) +{ + const char *remotename = remote->name; + int mirror = remote->mirror; + struct strbuf refspec = STRBUF_INIT; + + for (; *branches; branches++) + if (add_branch(key, *branches, remotename, mirror, &refspec)) { + strbuf_release(&refspec); + return 1; + } + + strbuf_release(&refspec); + return 0; +} + +static int set_remote_branches(const char *remotename, const char **branches, + int add_mode) +{ + struct strbuf key = STRBUF_INIT; + struct remote *remote; + + strbuf_addf(&key, "remote.%s.fetch", remotename); + + if (!remote_is_configured(remotename)) + die(_("No such remote '%s'"), remotename); + remote = remote_get(remotename); + + if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) { + strbuf_release(&key); + return 1; + } + if (add_branches(remote, branches, key.buf)) { + strbuf_release(&key); + return 1; + } + + strbuf_release(&key); + return 0; +} + +static int set_branches(int argc, const char **argv) +{ + int add_mode = 0; + struct option options[] = { + OPT_BOOLEAN('\0', "add", &add_mode, N_("add branch")), + OPT_END() + }; + + argc = parse_options(argc, argv, NULL, options, + builtin_remote_setbranches_usage, 0); + if (argc == 0) { + error(_("no remote specified")); + usage_with_options(builtin_remote_setbranches_usage, options); + } + argv[argc] = NULL; + + return set_remote_branches(argv[0], argv + 1, add_mode); +} + +static int set_url(int argc, const char **argv) +{ + int i, push_mode = 0, add_mode = 0, delete_mode = 0; + int matches = 0, negative_matches = 0; + const char *remotename = NULL; + const char *newurl = NULL; + const char *oldurl = NULL; + struct remote *remote; + regex_t old_regex; + const char **urlset; + int urlset_nr; + struct strbuf name_buf = STRBUF_INIT; + struct option options[] = { + OPT_BOOLEAN('\0', "push", &push_mode, + N_("manipulate push URLs")), + OPT_BOOLEAN('\0', "add", &add_mode, + N_("add URL")), + OPT_BOOLEAN('\0', "delete", &delete_mode, + N_("delete URLs")), + OPT_END() + }; + argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage, + PARSE_OPT_KEEP_ARGV0); + + if (add_mode && delete_mode) + die(_("--add --delete doesn't make sense")); + + if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3)) + usage_with_options(builtin_remote_seturl_usage, options); + + remotename = argv[1]; + newurl = argv[2]; + if (argc > 3) + oldurl = argv[3]; + + if (delete_mode) + oldurl = newurl; + + if (!remote_is_configured(remotename)) + die(_("No such remote '%s'"), remotename); + remote = remote_get(remotename); + + if (push_mode) { + strbuf_addf(&name_buf, "remote.%s.pushurl", remotename); + urlset = remote->pushurl; + urlset_nr = remote->pushurl_nr; + } else { + strbuf_addf(&name_buf, "remote.%s.url", remotename); + urlset = remote->url; + urlset_nr = remote->url_nr; + } + + /* Special cases that add new entry. */ + if ((!oldurl && !delete_mode) || add_mode) { + if (add_mode) + git_config_set_multivar(name_buf.buf, newurl, + "^$", 0); + else + git_config_set(name_buf.buf, newurl); + strbuf_release(&name_buf); + return 0; + } + + /* Old URL specified. Demand that one matches. */ + if (regcomp(&old_regex, oldurl, REG_EXTENDED)) + die(_("Invalid old URL pattern: %s"), oldurl); + + for (i = 0; i < urlset_nr; i++) + if (!regexec(&old_regex, urlset[i], 0, NULL, 0)) + matches++; + else + negative_matches++; + if (!delete_mode && !matches) + die(_("No such URL found: %s"), oldurl); + if (delete_mode && !negative_matches && !push_mode) + die(_("Will not delete all non-push URLs")); + + regfree(&old_regex); + + if (!delete_mode) + git_config_set_multivar(name_buf.buf, newurl, oldurl, 0); + else + git_config_set_multivar(name_buf.buf, NULL, oldurl, 1); + return 0; +} + +static int get_one_entry(struct remote *remote, void *priv) +{ + struct string_list *list = priv; + struct strbuf url_buf = STRBUF_INIT; + const char **url; + int i, url_nr; + + if (remote->url_nr > 0) { + strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]); + string_list_append(list, remote->name)->util = + strbuf_detach(&url_buf, NULL); + } else + string_list_append(list, remote->name)->util = NULL; + if (remote->pushurl_nr) { + url = remote->pushurl; + url_nr = remote->pushurl_nr; + } else { + url = remote->url; + url_nr = remote->url_nr; + } + for (i = 0; i < url_nr; i++) + { + strbuf_addf(&url_buf, "%s (push)", url[i]); + string_list_append(list, remote->name)->util = + strbuf_detach(&url_buf, NULL); + } + + return 0; +} + +static int show_all(void) +{ + struct string_list list = STRING_LIST_INIT_NODUP; + int result; + + list.strdup_strings = 1; + result = for_each_remote(get_one_entry, &list); + + if (!result) { + int i; + + sort_string_list(&list); + for (i = 0; i < list.nr; i++) { + struct string_list_item *item = list.items + i; + if (verbose) + printf("%s\t%s\n", item->string, + item->util ? (const char *)item->util : ""); + else { + if (i && !strcmp((item - 1)->string, item->string)) + continue; + printf("%s\n", item->string); + } + } + } + string_list_clear(&list, 1); + return result; +} + +int cmd_remote(int argc, const char **argv, const char *prefix) +{ + struct option options[] = { + OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")), + OPT_END() + }; + int result; + + argc = parse_options(argc, argv, prefix, options, builtin_remote_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + + if (argc < 1) + result = show_all(); + else if (!strcmp(argv[0], "add")) + result = add(argc, argv); + else if (!strcmp(argv[0], "rename")) + result = mv(argc, argv); + else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove")) + result = rm(argc, argv); + else if (!strcmp(argv[0], "set-head")) + result = set_head(argc, argv); + else if (!strcmp(argv[0], "set-branches")) + result = set_branches(argc, argv); + else if (!strcmp(argv[0], "set-url")) + result = set_url(argc, argv); + else if (!strcmp(argv[0], "show")) + result = show(argc, argv); + else if (!strcmp(argv[0], "prune")) + result = prune(argc, argv); + else if (!strcmp(argv[0], "update")) + result = update(argc, argv); + else { + error(_("Unknown subcommand: %s"), argv[0]); + usage_with_options(builtin_remote_usage, options); + } + + return result ? 1 : 0; +} diff --git a/builtin/replace.c b/builtin/replace.c new file mode 100644 index 0000000000..398ccd5eaa --- /dev/null +++ b/builtin/replace.c @@ -0,0 +1,162 @@ +/* + * Builtin "git replace" + * + * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org> + * + * Based on builtin-tag.c by Kristian Høgsberg <krh@redhat.com> + * and Carlos Rica <jasampler@gmail.com> that was itself based on + * git-tag.sh and mktag.c by Linus Torvalds. + */ + +#include "cache.h" +#include "builtin.h" +#include "refs.h" +#include "parse-options.h" + +static const char * const git_replace_usage[] = { + N_("git replace [-f] <object> <replacement>"), + N_("git replace -d <object>..."), + N_("git replace -l [<pattern>]"), + NULL +}; + +static int show_reference(const char *refname, const unsigned char *sha1, + int flag, void *cb_data) +{ + const char *pattern = cb_data; + + if (!fnmatch(pattern, refname, 0)) + printf("%s\n", refname); + + return 0; +} + +static int list_replace_refs(const char *pattern) +{ + if (pattern == NULL) + pattern = "*"; + + for_each_replace_ref(show_reference, (void *) pattern); + + return 0; +} + +typedef int (*each_replace_name_fn)(const char *name, const char *ref, + const unsigned char *sha1); + +static int for_each_replace_name(const char **argv, each_replace_name_fn fn) +{ + const char **p, *full_hex; + char ref[PATH_MAX]; + int had_error = 0; + unsigned char sha1[20]; + + for (p = argv; *p; p++) { + if (get_sha1(*p, sha1)) { + error("Failed to resolve '%s' as a valid ref.", *p); + had_error = 1; + continue; + } + full_hex = sha1_to_hex(sha1); + snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex); + /* read_ref() may reuse the buffer */ + full_hex = ref + strlen("refs/replace/"); + if (read_ref(ref, sha1)) { + error("replace ref '%s' not found.", full_hex); + had_error = 1; + continue; + } + if (fn(full_hex, ref, sha1)) + had_error = 1; + } + return had_error; +} + +static int delete_replace_ref(const char *name, const char *ref, + const unsigned char *sha1) +{ + if (delete_ref(ref, sha1, 0)) + return 1; + printf("Deleted replace ref '%s'\n", name); + return 0; +} + +static int replace_object(const char *object_ref, const char *replace_ref, + int force) +{ + unsigned char object[20], prev[20], repl[20]; + char ref[PATH_MAX]; + struct ref_lock *lock; + + if (get_sha1(object_ref, object)) + die("Failed to resolve '%s' as a valid ref.", object_ref); + if (get_sha1(replace_ref, repl)) + die("Failed to resolve '%s' as a valid ref.", replace_ref); + + if (snprintf(ref, sizeof(ref), + "refs/replace/%s", + sha1_to_hex(object)) > sizeof(ref) - 1) + die("replace ref name too long: %.*s...", 50, ref); + if (check_refname_format(ref, 0)) + die("'%s' is not a valid ref name.", ref); + + if (read_ref(ref, prev)) + hashclr(prev); + else if (!force) + die("replace ref '%s' already exists", ref); + + lock = lock_any_ref_for_update(ref, prev, 0); + if (!lock) + die("%s: cannot lock the ref", ref); + if (write_ref_sha1(lock, repl, NULL) < 0) + die("%s: cannot update the ref", ref); + + return 0; +} + +int cmd_replace(int argc, const char **argv, const char *prefix) +{ + int list = 0, delete = 0, force = 0; + struct option options[] = { + OPT_BOOLEAN('l', NULL, &list, N_("list replace refs")), + OPT_BOOLEAN('d', NULL, &delete, N_("delete replace refs")), + OPT_BOOLEAN('f', NULL, &force, N_("replace the ref if it exists")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0); + + if (list && delete) + usage_msg_opt("-l and -d cannot be used together", + git_replace_usage, options); + + if (force && (list || delete)) + usage_msg_opt("-f cannot be used with -d or -l", + git_replace_usage, options); + + /* Delete refs */ + if (delete) { + if (argc < 1) + usage_msg_opt("-d needs at least one argument", + git_replace_usage, options); + return for_each_replace_name(argv, delete_replace_ref); + } + + /* Replace object */ + if (!list && argc) { + if (argc != 2) + usage_msg_opt("bad number of arguments", + git_replace_usage, options); + return replace_object(argv[0], argv[1], force); + } + + /* List refs, even if "list" is not set */ + if (argc > 1) + usage_msg_opt("only one pattern can be given with -l", + git_replace_usage, options); + if (force) + usage_msg_opt("-f needs some arguments", + git_replace_usage, options); + + return list_replace_refs(argv[0]); +} diff --git a/builtin/rerere.c b/builtin/rerere.c new file mode 100644 index 0000000000..dc1708e6d6 --- /dev/null +++ b/builtin/rerere.c @@ -0,0 +1,110 @@ +#include "builtin.h" +#include "cache.h" +#include "dir.h" +#include "parse-options.h" +#include "string-list.h" +#include "rerere.h" +#include "xdiff/xdiff.h" +#include "xdiff-interface.h" + +static const char * const rerere_usage[] = { + N_("git rerere [clear | forget path... | status | remaining | diff | gc]"), + NULL, +}; + +static int outf(void *dummy, mmbuffer_t *ptr, int nbuf) +{ + int i; + for (i = 0; i < nbuf; i++) + if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size) + return -1; + return 0; +} + +static int diff_two(const char *file1, const char *label1, + const char *file2, const char *label2) +{ + xpparam_t xpp; + xdemitconf_t xecfg; + xdemitcb_t ecb; + mmfile_t minus, plus; + + if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2)) + return 1; + + printf("--- a/%s\n+++ b/%s\n", label1, label2); + fflush(stdout); + memset(&xpp, 0, sizeof(xpp)); + xpp.flags = 0; + memset(&xecfg, 0, sizeof(xecfg)); + xecfg.ctxlen = 3; + ecb.outf = outf; + xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb); + + free(minus.ptr); + free(plus.ptr); + return 0; +} + +int cmd_rerere(int argc, const char **argv, const char *prefix) +{ + struct string_list merge_rr = STRING_LIST_INIT_DUP; + int i, fd, autoupdate = -1, flags = 0; + + struct option options[] = { + OPT_SET_INT(0, "rerere-autoupdate", &autoupdate, + N_("register clean resolutions in index"), 1), + OPT_END(), + }; + + argc = parse_options(argc, argv, prefix, options, rerere_usage, 0); + + if (autoupdate == 1) + flags = RERERE_AUTOUPDATE; + if (autoupdate == 0) + flags = RERERE_NOAUTOUPDATE; + + if (argc < 1) + return rerere(flags); + + if (!strcmp(argv[0], "forget")) { + const char **pathspec; + if (argc < 2) + warning("'git rerere forget' without paths is deprecated"); + pathspec = get_pathspec(prefix, argv + 1); + return rerere_forget(pathspec); + } + + fd = setup_rerere(&merge_rr, flags); + if (fd < 0) + return 0; + + if (!strcmp(argv[0], "clear")) { + rerere_clear(&merge_rr); + } else if (!strcmp(argv[0], "gc")) + rerere_gc(&merge_rr); + else if (!strcmp(argv[0], "status")) + for (i = 0; i < merge_rr.nr; i++) + printf("%s\n", merge_rr.items[i].string); + else if (!strcmp(argv[0], "remaining")) { + rerere_remaining(&merge_rr); + for (i = 0; i < merge_rr.nr; i++) { + if (merge_rr.items[i].util != RERERE_RESOLVED) + printf("%s\n", merge_rr.items[i].string); + else + /* prepare for later call to + * string_list_clear() */ + merge_rr.items[i].util = NULL; + } + } else if (!strcmp(argv[0], "diff")) + for (i = 0; i < merge_rr.nr; i++) { + const char *path = merge_rr.items[i].string; + const char *name = (const char *)merge_rr.items[i].util; + diff_two(rerere_path(name, "preimage"), path, path, path); + } + else + usage_with_options(rerere_usage, options); + + string_list_clear(&merge_rr, 1); + return 0; +} diff --git a/builtin/reset.c b/builtin/reset.c new file mode 100644 index 0000000000..6032131a90 --- /dev/null +++ b/builtin/reset.c @@ -0,0 +1,359 @@ +/* + * "git reset" builtin command + * + * Copyright (c) 2007 Carlos Rica + * + * Based on git-reset.sh, which is + * + * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano + */ +#include "builtin.h" +#include "tag.h" +#include "object.h" +#include "commit.h" +#include "run-command.h" +#include "refs.h" +#include "diff.h" +#include "diffcore.h" +#include "tree.h" +#include "branch.h" +#include "parse-options.h" +#include "unpack-trees.h" +#include "cache-tree.h" + +static const char * const git_reset_usage[] = { + N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"), + N_("git reset [-q] <tree-ish> [--] <paths>..."), + N_("git reset --patch [<tree-ish>] [--] [<paths>...]"), + NULL +}; + +enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE }; +static const char *reset_type_names[] = { + N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL +}; + +static inline int is_merge(void) +{ + return !access(git_path("MERGE_HEAD"), F_OK); +} + +static int reset_index(const unsigned char *sha1, int reset_type, int quiet) +{ + int nr = 1; + struct tree_desc desc[2]; + struct tree *tree; + struct unpack_trees_options opts; + + memset(&opts, 0, sizeof(opts)); + opts.head_idx = 1; + opts.src_index = &the_index; + opts.dst_index = &the_index; + opts.fn = oneway_merge; + opts.merge = 1; + if (!quiet) + opts.verbose_update = 1; + switch (reset_type) { + case KEEP: + case MERGE: + opts.update = 1; + break; + case HARD: + opts.update = 1; + /* fallthrough */ + default: + opts.reset = 1; + } + + read_cache_unmerged(); + + if (reset_type == KEEP) { + unsigned char head_sha1[20]; + if (get_sha1("HEAD", head_sha1)) + return error(_("You do not have a valid HEAD.")); + if (!fill_tree_descriptor(desc, head_sha1)) + return error(_("Failed to find tree of HEAD.")); + nr++; + opts.fn = twoway_merge; + } + + if (!fill_tree_descriptor(desc + nr - 1, sha1)) + return error(_("Failed to find tree of %s."), sha1_to_hex(sha1)); + if (unpack_trees(nr, desc, &opts)) + return -1; + + if (reset_type == MIXED || reset_type == HARD) { + tree = parse_tree_indirect(sha1); + prime_cache_tree(&active_cache_tree, tree); + } + + return 0; +} + +static void print_new_head_line(struct commit *commit) +{ + const char *hex, *body; + + hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); + printf(_("HEAD is now at %s"), hex); + body = strstr(commit->buffer, "\n\n"); + if (body) { + const char *eol; + size_t len; + body += 2; + eol = strchr(body, '\n'); + len = eol ? eol - body : strlen(body); + printf(" %.*s\n", (int) len, body); + } + else + printf("\n"); +} + +static void update_index_from_diff(struct diff_queue_struct *q, + struct diff_options *opt, void *data) +{ + int i; + + for (i = 0; i < q->nr; i++) { + struct diff_filespec *one = q->queue[i]->one; + if (one->mode && !is_null_sha1(one->sha1)) { + struct cache_entry *ce; + ce = make_cache_entry(one->mode, one->sha1, one->path, + 0, 0); + if (!ce) + die(_("make_cache_entry failed for path '%s'"), + one->path); + add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | + ADD_CACHE_OK_TO_REPLACE); + } else + remove_file_from_cache(one->path); + } +} + +static int read_from_tree(const char **pathspec, unsigned char *tree_sha1) +{ + struct diff_options opt; + + memset(&opt, 0, sizeof(opt)); + diff_tree_setup_paths(pathspec, &opt); + opt.output_format = DIFF_FORMAT_CALLBACK; + opt.format_callback = update_index_from_diff; + + read_cache(); + if (do_diff_cache(tree_sha1, &opt)) + return 1; + diffcore_std(&opt); + diff_flush(&opt); + diff_tree_release_paths(&opt); + + return 0; +} + +static void set_reflog_message(struct strbuf *sb, const char *action, + const char *rev) +{ + const char *rla = getenv("GIT_REFLOG_ACTION"); + + strbuf_reset(sb); + if (rla) + strbuf_addf(sb, "%s: %s", rla, action); + else if (rev) + strbuf_addf(sb, "reset: moving to %s", rev); + else + strbuf_addf(sb, "reset: %s", action); +} + +static void die_if_unmerged_cache(int reset_type) +{ + if (is_merge() || read_cache() < 0 || unmerged_cache()) + die(_("Cannot do a %s reset in the middle of a merge."), + _(reset_type_names[reset_type])); + +} + +static const char **parse_args(const char **argv, const char *prefix, const char **rev_ret) +{ + const char *rev = "HEAD"; + unsigned char unused[20]; + /* + * Possible arguments are: + * + * git reset [-opts] [<rev>] + * git reset [-opts] <tree> [<paths>...] + * git reset [-opts] <tree> -- [<paths>...] + * git reset [-opts] -- [<paths>...] + * git reset [-opts] <paths>... + * + * At this point, argv points immediately after [-opts]. + */ + + if (argv[0]) { + if (!strcmp(argv[0], "--")) { + argv++; /* reset to HEAD, possibly with paths */ + } else if (argv[1] && !strcmp(argv[1], "--")) { + rev = argv[0]; + argv += 2; + } + /* + * Otherwise, argv[0] could be either <rev> or <paths> and + * has to be unambiguous. If there is a single argument, it + * can not be a tree + */ + else if ((!argv[1] && !get_sha1_committish(argv[0], unused)) || + (argv[1] && !get_sha1_treeish(argv[0], unused))) { + /* + * Ok, argv[0] looks like a commit/tree; it should not + * be a filename. + */ + verify_non_filename(prefix, argv[0]); + rev = *argv++; + } else { + /* Otherwise we treat this as a filename */ + verify_filename(prefix, argv[0], 1); + } + } + *rev_ret = rev; + return argv[0] ? get_pathspec(prefix, argv) : NULL; +} + +static int update_refs(const char *rev, const unsigned char *sha1) +{ + int update_ref_status; + struct strbuf msg = STRBUF_INIT; + unsigned char *orig = NULL, sha1_orig[20], + *old_orig = NULL, sha1_old_orig[20]; + + if (!get_sha1("ORIG_HEAD", sha1_old_orig)) + old_orig = sha1_old_orig; + if (!get_sha1("HEAD", sha1_orig)) { + orig = sha1_orig; + set_reflog_message(&msg, "updating ORIG_HEAD", NULL); + update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR); + } else if (old_orig) + delete_ref("ORIG_HEAD", old_orig, 0); + set_reflog_message(&msg, "updating HEAD", rev); + update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR); + strbuf_release(&msg); + return update_ref_status; +} + +int cmd_reset(int argc, const char **argv, const char *prefix) +{ + int reset_type = NONE, update_ref_status = 0, quiet = 0; + int patch_mode = 0, unborn; + const char *rev; + unsigned char sha1[20]; + const char **pathspec = NULL; + const struct option options[] = { + OPT__QUIET(&quiet, N_("be quiet, only report errors")), + OPT_SET_INT(0, "mixed", &reset_type, + N_("reset HEAD and index"), MIXED), + OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT), + OPT_SET_INT(0, "hard", &reset_type, + N_("reset HEAD, index and working tree"), HARD), + OPT_SET_INT(0, "merge", &reset_type, + N_("reset HEAD, index and working tree"), MERGE), + OPT_SET_INT(0, "keep", &reset_type, + N_("reset HEAD but keep local changes"), KEEP), + OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")), + OPT_END() + }; + + git_config(git_default_config, NULL); + + argc = parse_options(argc, argv, prefix, options, git_reset_usage, + PARSE_OPT_KEEP_DASHDASH); + pathspec = parse_args(argv, prefix, &rev); + + unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1); + if (unborn) { + /* reset on unborn branch: treat as reset to empty tree */ + hashcpy(sha1, EMPTY_TREE_SHA1_BIN); + } else if (!pathspec) { + struct commit *commit; + if (get_sha1_committish(rev, sha1)) + die(_("Failed to resolve '%s' as a valid revision."), rev); + commit = lookup_commit_reference(sha1); + if (!commit) + die(_("Could not parse object '%s'."), rev); + hashcpy(sha1, commit->object.sha1); + } else { + struct tree *tree; + if (get_sha1_treeish(rev, sha1)) + die(_("Failed to resolve '%s' as a valid tree."), rev); + tree = parse_tree_indirect(sha1); + if (!tree) + die(_("Could not parse object '%s'."), rev); + hashcpy(sha1, tree->object.sha1); + } + + if (patch_mode) { + if (reset_type != NONE) + die(_("--patch is incompatible with --{hard,mixed,soft}")); + return run_add_interactive(sha1_to_hex(sha1), "--patch=reset", pathspec); + } + + /* git reset tree [--] paths... can be used to + * load chosen paths from the tree into the index without + * affecting the working tree nor HEAD. */ + if (pathspec) { + if (reset_type == MIXED) + warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead.")); + else if (reset_type != NONE) + die(_("Cannot do %s reset with paths."), + _(reset_type_names[reset_type])); + } + if (reset_type == NONE) + reset_type = MIXED; /* by default */ + + if (reset_type != SOFT && reset_type != MIXED) + setup_work_tree(); + + if (reset_type == MIXED && is_bare_repository()) + die(_("%s reset is not allowed in a bare repository"), + _(reset_type_names[reset_type])); + + /* Soft reset does not touch the index file nor the working tree + * at all, but requires them in a good order. Other resets reset + * the index file to the tree object we are switching to. */ + if (reset_type == SOFT || reset_type == KEEP) + die_if_unmerged_cache(reset_type); + + if (reset_type != SOFT) { + struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); + int newfd = hold_locked_index(lock, 1); + if (reset_type == MIXED) { + if (read_from_tree(pathspec, sha1)) + return 1; + } else { + int err = reset_index(sha1, reset_type, quiet); + if (reset_type == KEEP && !err) + err = reset_index(sha1, MIXED, quiet); + if (err) + die(_("Could not reset index file to revision '%s'."), rev); + } + + if (reset_type == MIXED) { /* Report what has not been updated. */ + int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN; + refresh_index(&the_index, flags, NULL, NULL, + _("Unstaged changes after reset:")); + } + + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(lock)) + die(_("Could not write new index file.")); + } + + if (!pathspec && !unborn) { + /* Any resets without paths update HEAD to the head being + * switched to, saving the previous head in ORIG_HEAD before. */ + update_ref_status = update_refs(rev, sha1); + + if (reset_type == HARD && !update_ref_status && !quiet) + print_new_head_line(lookup_commit_reference(sha1)); + } + if (!pathspec) + remove_branch_state(); + + return update_ref_status; +} diff --git a/builtin/rev-list.c b/builtin/rev-list.c new file mode 100644 index 0000000000..67701be551 --- /dev/null +++ b/builtin/rev-list.c @@ -0,0 +1,364 @@ +#include "cache.h" +#include "commit.h" +#include "diff.h" +#include "revision.h" +#include "list-objects.h" +#include "builtin.h" +#include "log-tree.h" +#include "graph.h" +#include "bisect.h" + +static const char rev_list_usage[] = +"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n" +" limiting output:\n" +" --max-count=<n>\n" +" --max-age=<epoch>\n" +" --min-age=<epoch>\n" +" --sparse\n" +" --no-merges\n" +" --min-parents=<n>\n" +" --no-min-parents\n" +" --max-parents=<n>\n" +" --no-max-parents\n" +" --remove-empty\n" +" --all\n" +" --branches\n" +" --tags\n" +" --remotes\n" +" --stdin\n" +" --quiet\n" +" ordering output:\n" +" --topo-order\n" +" --date-order\n" +" --reverse\n" +" formatting output:\n" +" --parents\n" +" --children\n" +" --objects | --objects-edge\n" +" --unpacked\n" +" --header | --pretty\n" +" --abbrev=<n> | --no-abbrev\n" +" --abbrev-commit\n" +" --left-right\n" +" special purpose:\n" +" --bisect\n" +" --bisect-vars\n" +" --bisect-all" +; + +static void finish_commit(struct commit *commit, void *data); +static void show_commit(struct commit *commit, void *data) +{ + struct rev_list_info *info = data; + struct rev_info *revs = info->revs; + + if (info->flags & REV_LIST_QUIET) { + finish_commit(commit, data); + return; + } + + graph_show_commit(revs->graph); + + if (revs->count) { + if (commit->object.flags & PATCHSAME) + revs->count_same++; + else if (commit->object.flags & SYMMETRIC_LEFT) + revs->count_left++; + else + revs->count_right++; + finish_commit(commit, data); + return; + } + + if (info->show_timestamp) + printf("%lu ", commit->date); + if (info->header_prefix) + fputs(info->header_prefix, stdout); + + if (!revs->graph) + fputs(get_revision_mark(revs, commit), stdout); + if (revs->abbrev_commit && revs->abbrev) + fputs(find_unique_abbrev(commit->object.sha1, revs->abbrev), + stdout); + else + fputs(sha1_to_hex(commit->object.sha1), stdout); + if (revs->print_parents) { + struct commit_list *parents = commit->parents; + while (parents) { + printf(" %s", sha1_to_hex(parents->item->object.sha1)); + parents = parents->next; + } + } + if (revs->children.name) { + struct commit_list *children; + + children = lookup_decoration(&revs->children, &commit->object); + while (children) { + printf(" %s", sha1_to_hex(children->item->object.sha1)); + children = children->next; + } + } + show_decorations(revs, commit); + if (revs->commit_format == CMIT_FMT_ONELINE) + putchar(' '); + else + putchar('\n'); + + if (revs->verbose_header && commit->buffer) { + struct strbuf buf = STRBUF_INIT; + struct pretty_print_context ctx = {0}; + ctx.abbrev = revs->abbrev; + ctx.date_mode = revs->date_mode; + ctx.date_mode_explicit = revs->date_mode_explicit; + ctx.fmt = revs->commit_format; + pretty_print_commit(&ctx, commit, &buf); + if (revs->graph) { + if (buf.len) { + if (revs->commit_format != CMIT_FMT_ONELINE) + graph_show_oneline(revs->graph); + + graph_show_commit_msg(revs->graph, &buf); + + /* + * Add a newline after the commit message. + * + * Usually, this newline produces a blank + * padding line between entries, in which case + * we need to add graph padding on this line. + * + * However, the commit message may not end in a + * newline. In this case the newline simply + * ends the last line of the commit message, + * and we don't need any graph output. (This + * always happens with CMIT_FMT_ONELINE, and it + * happens with CMIT_FMT_USERFORMAT when the + * format doesn't explicitly end in a newline.) + */ + if (buf.len && buf.buf[buf.len - 1] == '\n') + graph_show_padding(revs->graph); + putchar('\n'); + } else { + /* + * If the message buffer is empty, just show + * the rest of the graph output for this + * commit. + */ + if (graph_show_remainder(revs->graph)) + putchar('\n'); + if (revs->commit_format == CMIT_FMT_ONELINE) + putchar('\n'); + } + } else { + if (revs->commit_format != CMIT_FMT_USERFORMAT || + buf.len) { + fwrite(buf.buf, 1, buf.len, stdout); + putchar(info->hdr_termination); + } + } + strbuf_release(&buf); + } else { + if (graph_show_remainder(revs->graph)) + putchar('\n'); + } + maybe_flush_or_die(stdout, "stdout"); + finish_commit(commit, data); +} + +static void finish_commit(struct commit *commit, void *data) +{ + if (commit->parents) { + free_commit_list(commit->parents); + commit->parents = NULL; + } + free(commit->buffer); + commit->buffer = NULL; +} + +static void finish_object(struct object *obj, + const struct name_path *path, const char *name, + void *cb_data) +{ + struct rev_list_info *info = cb_data; + if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1)) + die("missing blob object '%s'", sha1_to_hex(obj->sha1)); + if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT) + parse_object(obj->sha1); +} + +static void show_object(struct object *obj, + const struct name_path *path, const char *component, + void *cb_data) +{ + struct rev_list_info *info = cb_data; + finish_object(obj, path, component, cb_data); + if (info->flags & REV_LIST_QUIET) + return; + show_object_with_name(stdout, obj, path, component); +} + +static void show_edge(struct commit *commit) +{ + printf("-%s\n", sha1_to_hex(commit->object.sha1)); +} + +static void print_var_str(const char *var, const char *val) +{ + printf("%s='%s'\n", var, val); +} + +static void print_var_int(const char *var, int val) +{ + printf("%s=%d\n", var, val); +} + +static int show_bisect_vars(struct rev_list_info *info, int reaches, int all) +{ + int cnt, flags = info->flags; + char hex[41] = ""; + struct commit_list *tried; + struct rev_info *revs = info->revs; + + if (!revs->commits) + return 1; + + revs->commits = filter_skipped(revs->commits, &tried, + flags & BISECT_SHOW_ALL, + NULL, NULL); + + /* + * revs->commits can reach "reaches" commits among + * "all" commits. If it is good, then there are + * (all-reaches) commits left to be bisected. + * On the other hand, if it is bad, then the set + * to bisect is "reaches". + * A bisect set of size N has (N-1) commits further + * to test, as we already know one bad one. + */ + cnt = all - reaches; + if (cnt < reaches) + cnt = reaches; + + if (revs->commits) + strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1)); + + if (flags & BISECT_SHOW_ALL) { + traverse_commit_list(revs, show_commit, show_object, info); + printf("------\n"); + } + + print_var_str("bisect_rev", hex); + print_var_int("bisect_nr", cnt - 1); + print_var_int("bisect_good", all - reaches - 1); + print_var_int("bisect_bad", reaches - 1); + print_var_int("bisect_all", all); + print_var_int("bisect_steps", estimate_bisect_steps(all)); + + return 0; +} + +int cmd_rev_list(int argc, const char **argv, const char *prefix) +{ + struct rev_info revs; + struct rev_list_info info; + int i; + int bisect_list = 0; + int bisect_show_vars = 0; + int bisect_find_all = 0; + + git_config(git_default_config, NULL); + init_revisions(&revs, prefix); + revs.abbrev = DEFAULT_ABBREV; + revs.commit_format = CMIT_FMT_UNSPECIFIED; + argc = setup_revisions(argc, argv, &revs, NULL); + + memset(&info, 0, sizeof(info)); + info.revs = &revs; + if (revs.bisect) + bisect_list = 1; + + if (DIFF_OPT_TST(&revs.diffopt, QUICK)) + info.flags |= REV_LIST_QUIET; + for (i = 1 ; i < argc; i++) { + const char *arg = argv[i]; + + if (!strcmp(arg, "--header")) { + revs.verbose_header = 1; + continue; + } + if (!strcmp(arg, "--timestamp")) { + info.show_timestamp = 1; + continue; + } + if (!strcmp(arg, "--bisect")) { + bisect_list = 1; + continue; + } + if (!strcmp(arg, "--bisect-all")) { + bisect_list = 1; + bisect_find_all = 1; + info.flags |= BISECT_SHOW_ALL; + revs.show_decorations = 1; + continue; + } + if (!strcmp(arg, "--bisect-vars")) { + bisect_list = 1; + bisect_show_vars = 1; + continue; + } + usage(rev_list_usage); + + } + if (revs.commit_format != CMIT_FMT_UNSPECIFIED) { + /* The command line has a --pretty */ + info.hdr_termination = '\n'; + if (revs.commit_format == CMIT_FMT_ONELINE) + info.header_prefix = ""; + else + info.header_prefix = "commit "; + } + else if (revs.verbose_header) + /* Only --header was specified */ + revs.commit_format = CMIT_FMT_RAW; + + if ((!revs.commits && + (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && + !revs.pending.nr)) || + revs.diff) + usage(rev_list_usage); + + save_commit_buffer = (revs.verbose_header || + revs.grep_filter.pattern_list || + revs.grep_filter.header_list); + if (bisect_list) + revs.limited = 1; + + if (prepare_revision_walk(&revs)) + die("revision walk setup failed"); + if (revs.tree_objects) + mark_edges_uninteresting(revs.commits, &revs, show_edge); + + if (bisect_list) { + int reaches = reaches, all = all; + + revs.commits = find_bisection(revs.commits, &reaches, &all, + bisect_find_all); + + if (bisect_show_vars) + return show_bisect_vars(&info, reaches, all); + } + + traverse_commit_list(&revs, show_commit, show_object, &info); + + if (revs.count) { + if (revs.left_right && revs.cherry_mark) + printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same); + else if (revs.left_right) + printf("%d\t%d\n", revs.count_left, revs.count_right); + else if (revs.cherry_mark) + printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same); + else + printf("%d\n", revs.count_left + revs.count_right); + } + + return 0; +} diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c new file mode 100644 index 0000000000..f267a1d3b5 --- /dev/null +++ b/builtin/rev-parse.c @@ -0,0 +1,771 @@ +/* + * rev-parse.c + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "commit.h" +#include "refs.h" +#include "quote.h" +#include "builtin.h" +#include "parse-options.h" + +#define DO_REVS 1 +#define DO_NOREV 2 +#define DO_FLAGS 4 +#define DO_NONFLAGS 8 +static int filter = ~0; + +static const char *def; + +#define NORMAL 0 +#define REVERSED 1 +static int show_type = NORMAL; + +#define SHOW_SYMBOLIC_ASIS 1 +#define SHOW_SYMBOLIC_FULL 2 +static int symbolic; +static int abbrev; +static int abbrev_ref; +static int abbrev_ref_strict; +static int output_sq; + +/* + * Some arguments are relevant "revision" arguments, + * others are about output format or other details. + * This sorts it all out. + */ +static int is_rev_argument(const char *arg) +{ + static const char *rev_args[] = { + "--all", + "--bisect", + "--dense", + "--branches=", + "--branches", + "--header", + "--ignore-missing", + "--max-age=", + "--max-count=", + "--min-age=", + "--no-merges", + "--min-parents=", + "--no-min-parents", + "--max-parents=", + "--no-max-parents", + "--objects", + "--objects-edge", + "--parents", + "--pretty", + "--remotes=", + "--remotes", + "--glob=", + "--sparse", + "--tags=", + "--tags", + "--topo-order", + "--date-order", + "--unpacked", + NULL + }; + const char **p = rev_args; + + /* accept -<digit>, like traditional "head" */ + if ((*arg == '-') && isdigit(arg[1])) + return 1; + + for (;;) { + const char *str = *p++; + int len; + if (!str) + return 0; + len = strlen(str); + if (!strcmp(arg, str) || + (str[len-1] == '=' && !strncmp(arg, str, len))) + return 1; + } +} + +/* Output argument as a string, either SQ or normal */ +static void show(const char *arg) +{ + if (output_sq) { + int sq = '\'', ch; + + putchar(sq); + while ((ch = *arg++)) { + if (ch == sq) + fputs("'\\'", stdout); + putchar(ch); + } + putchar(sq); + putchar(' '); + } + else + puts(arg); +} + +/* Like show(), but with a negation prefix according to type */ +static void show_with_type(int type, const char *arg) +{ + if (type != show_type) + putchar('^'); + show(arg); +} + +/* Output a revision, only if filter allows it */ +static void show_rev(int type, const unsigned char *sha1, const char *name) +{ + if (!(filter & DO_REVS)) + return; + def = NULL; + + if ((symbolic || abbrev_ref) && name) { + if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) { + unsigned char discard[20]; + char *full; + + switch (dwim_ref(name, strlen(name), discard, &full)) { + case 0: + /* + * Not found -- not a ref. We could + * emit "name" here, but symbolic-full + * users are interested in finding the + * refs spelled in full, and they would + * need to filter non-refs if we did so. + */ + break; + case 1: /* happy */ + if (abbrev_ref) + full = shorten_unambiguous_ref(full, + abbrev_ref_strict); + show_with_type(type, full); + break; + default: /* ambiguous */ + error("refname '%s' is ambiguous", name); + break; + } + } else { + show_with_type(type, name); + } + } + else if (abbrev) + show_with_type(type, find_unique_abbrev(sha1, abbrev)); + else + show_with_type(type, sha1_to_hex(sha1)); +} + +/* Output a flag, only if filter allows it. */ +static int show_flag(const char *arg) +{ + if (!(filter & DO_FLAGS)) + return 0; + if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { + show(arg); + return 1; + } + return 0; +} + +static int show_default(void) +{ + const char *s = def; + + if (s) { + unsigned char sha1[20]; + + def = NULL; + if (!get_sha1(s, sha1)) { + show_rev(NORMAL, sha1, s); + return 1; + } + } + return 0; +} + +static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + show_rev(NORMAL, sha1, refname); + return 0; +} + +static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + show_rev(REVERSED, sha1, refname); + return 0; +} + +static int show_abbrev(const unsigned char *sha1, void *cb_data) +{ + show_rev(NORMAL, sha1, NULL); + return 0; +} + +static void show_datestring(const char *flag, const char *datestr) +{ + static char buffer[100]; + + /* date handling requires both flags and revs */ + if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) + return; + snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr)); + show(buffer); +} + +static int show_file(const char *arg) +{ + show_default(); + if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { + show(arg); + return 1; + } + return 0; +} + +static int try_difference(const char *arg) +{ + char *dotdot; + unsigned char sha1[20]; + unsigned char end[20]; + const char *next; + const char *this; + int symmetric; + static const char head_by_default[] = "HEAD"; + + if (!(dotdot = strstr(arg, ".."))) + return 0; + next = dotdot + 2; + this = arg; + symmetric = (*next == '.'); + + *dotdot = 0; + next += symmetric; + + if (!*next) + next = head_by_default; + if (dotdot == arg) + this = head_by_default; + + if (this == head_by_default && next == head_by_default && + !symmetric) { + /* + * Just ".."? That is not a range but the + * pathspec for the parent directory. + */ + *dotdot = '.'; + return 0; + } + + if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) { + show_rev(NORMAL, end, next); + show_rev(symmetric ? NORMAL : REVERSED, sha1, this); + if (symmetric) { + struct commit_list *exclude; + struct commit *a, *b; + a = lookup_commit_reference(sha1); + b = lookup_commit_reference(end); + exclude = get_merge_bases(a, b, 1); + while (exclude) { + struct commit_list *n = exclude->next; + show_rev(REVERSED, + exclude->item->object.sha1,NULL); + free(exclude); + exclude = n; + } + } + return 1; + } + *dotdot = '.'; + return 0; +} + +static int try_parent_shorthands(const char *arg) +{ + char *dotdot; + unsigned char sha1[20]; + struct commit *commit; + struct commit_list *parents; + int parents_only; + + if ((dotdot = strstr(arg, "^!"))) + parents_only = 0; + else if ((dotdot = strstr(arg, "^@"))) + parents_only = 1; + + if (!dotdot || dotdot[2]) + return 0; + + *dotdot = 0; + if (get_sha1_committish(arg, sha1)) + return 0; + + if (!parents_only) + show_rev(NORMAL, sha1, arg); + commit = lookup_commit_reference(sha1); + for (parents = commit->parents; parents; parents = parents->next) + show_rev(parents_only ? NORMAL : REVERSED, + parents->item->object.sha1, arg); + + return 1; +} + +static int parseopt_dump(const struct option *o, const char *arg, int unset) +{ + struct strbuf *parsed = o->value; + if (unset) + strbuf_addf(parsed, " --no-%s", o->long_name); + else if (o->short_name) + strbuf_addf(parsed, " -%c", o->short_name); + else + strbuf_addf(parsed, " --%s", o->long_name); + if (arg) { + strbuf_addch(parsed, ' '); + sq_quote_buf(parsed, arg); + } + return 0; +} + +static const char *skipspaces(const char *s) +{ + while (isspace(*s)) + s++; + return s; +} + +static int cmd_parseopt(int argc, const char **argv, const char *prefix) +{ + static int keep_dashdash = 0, stop_at_non_option = 0; + static char const * const parseopt_usage[] = { + N_("git rev-parse --parseopt [options] -- [<args>...]"), + NULL + }; + static struct option parseopt_opts[] = { + OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash, + N_("keep the `--` passed as an arg")), + OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option, + N_("stop parsing after the " + "first non-option argument")), + OPT_END(), + }; + + struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; + const char **usage = NULL; + struct option *opts = NULL; + int onb = 0, osz = 0, unb = 0, usz = 0; + + strbuf_addstr(&parsed, "set --"); + argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, + PARSE_OPT_KEEP_DASHDASH); + if (argc < 1 || strcmp(argv[0], "--")) + usage_with_options(parseopt_usage, parseopt_opts); + + /* get the usage up to the first line with a -- on it */ + for (;;) { + if (strbuf_getline(&sb, stdin, '\n') == EOF) + die("premature end of input"); + ALLOC_GROW(usage, unb + 1, usz); + if (!strcmp("--", sb.buf)) { + if (unb < 1) + die("no usage string given before the `--' separator"); + usage[unb] = NULL; + break; + } + usage[unb++] = strbuf_detach(&sb, NULL); + } + + /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */ + while (strbuf_getline(&sb, stdin, '\n') != EOF) { + const char *s; + struct option *o; + + if (!sb.len) + continue; + + ALLOC_GROW(opts, onb + 1, osz); + memset(opts + onb, 0, sizeof(opts[onb])); + + o = &opts[onb++]; + s = strchr(sb.buf, ' '); + if (!s || *sb.buf == ' ') { + o->type = OPTION_GROUP; + o->help = xstrdup(skipspaces(sb.buf)); + continue; + } + + o->type = OPTION_CALLBACK; + o->help = xstrdup(skipspaces(s)); + o->value = &parsed; + o->flags = PARSE_OPT_NOARG; + o->callback = &parseopt_dump; + while (s > sb.buf && strchr("*=?!", s[-1])) { + switch (*--s) { + case '=': + o->flags &= ~PARSE_OPT_NOARG; + break; + case '?': + o->flags &= ~PARSE_OPT_NOARG; + o->flags |= PARSE_OPT_OPTARG; + break; + case '!': + o->flags |= PARSE_OPT_NONEG; + break; + case '*': + o->flags |= PARSE_OPT_HIDDEN; + break; + } + } + + if (s - sb.buf == 1) /* short option only */ + o->short_name = *sb.buf; + else if (sb.buf[1] != ',') /* long option only */ + o->long_name = xmemdupz(sb.buf, s - sb.buf); + else { + o->short_name = *sb.buf; + o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2); + } + } + strbuf_release(&sb); + + /* put an OPT_END() */ + ALLOC_GROW(opts, onb + 1, osz); + memset(opts + onb, 0, sizeof(opts[onb])); + argc = parse_options(argc, argv, prefix, opts, usage, + (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) | + (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) | + PARSE_OPT_SHELL_EVAL); + + strbuf_addf(&parsed, " --"); + sq_quote_argv(&parsed, argv, 0); + puts(parsed.buf); + return 0; +} + +static int cmd_sq_quote(int argc, const char **argv) +{ + struct strbuf buf = STRBUF_INIT; + + if (argc) + sq_quote_argv(&buf, argv, 0); + printf("%s\n", buf.buf); + strbuf_release(&buf); + + return 0; +} + +static void die_no_single_rev(int quiet) +{ + if (quiet) + exit(1); + else + die("Needed a single revision"); +} + +static const char builtin_rev_parse_usage[] = +N_("git rev-parse --parseopt [options] -- [<args>...]\n" + " or: git rev-parse --sq-quote [<arg>...]\n" + " or: git rev-parse [options] [<arg>...]\n" + "\n" + "Run \"git rev-parse --parseopt -h\" for more information on the first usage."); + +int cmd_rev_parse(int argc, const char **argv, const char *prefix) +{ + int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; + unsigned char sha1[20]; + const char *name = NULL; + + if (argc > 1 && !strcmp("--parseopt", argv[1])) + return cmd_parseopt(argc - 1, argv + 1, prefix); + + if (argc > 1 && !strcmp("--sq-quote", argv[1])) + return cmd_sq_quote(argc - 2, argv + 2); + + if (argc == 2 && !strcmp("--local-env-vars", argv[1])) { + int i; + for (i = 0; local_repo_env[i]; i++) + printf("%s\n", local_repo_env[i]); + return 0; + } + + if (argc > 2 && !strcmp(argv[1], "--resolve-git-dir")) { + const char *gitdir = resolve_gitdir(argv[2]); + if (!gitdir) + die("not a gitdir '%s'", argv[2]); + puts(gitdir); + return 0; + } + + if (argc > 1 && !strcmp("-h", argv[1])) + usage(builtin_rev_parse_usage); + + prefix = setup_git_directory(); + git_config(git_default_config, NULL); + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + + if (as_is) { + if (show_file(arg) && as_is < 2) + verify_filename(prefix, arg, 0); + continue; + } + if (!strcmp(arg,"-n")) { + if (++i >= argc) + die("-n requires an argument"); + if ((filter & DO_FLAGS) && (filter & DO_REVS)) { + show(arg); + show(argv[i]); + } + continue; + } + if (!prefixcmp(arg, "-n")) { + if ((filter & DO_FLAGS) && (filter & DO_REVS)) + show(arg); + continue; + } + + if (*arg == '-') { + if (!strcmp(arg, "--")) { + as_is = 2; + /* Pass on the "--" if we show anything but files.. */ + if (filter & (DO_FLAGS | DO_REVS)) + show_file(arg); + continue; + } + if (!strcmp(arg, "--default")) { + def = argv[i+1]; + i++; + continue; + } + if (!strcmp(arg, "--revs-only")) { + filter &= ~DO_NOREV; + continue; + } + if (!strcmp(arg, "--no-revs")) { + filter &= ~DO_REVS; + continue; + } + if (!strcmp(arg, "--flags")) { + filter &= ~DO_NONFLAGS; + continue; + } + if (!strcmp(arg, "--no-flags")) { + filter &= ~DO_FLAGS; + continue; + } + if (!strcmp(arg, "--verify")) { + filter &= ~(DO_FLAGS|DO_NOREV); + verify = 1; + continue; + } + if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { + quiet = 1; + continue; + } + if (!strcmp(arg, "--short") || + !prefixcmp(arg, "--short=")) { + filter &= ~(DO_FLAGS|DO_NOREV); + verify = 1; + abbrev = DEFAULT_ABBREV; + if (arg[7] == '=') + abbrev = strtoul(arg + 8, NULL, 10); + if (abbrev < MINIMUM_ABBREV) + abbrev = MINIMUM_ABBREV; + else if (40 <= abbrev) + abbrev = 40; + continue; + } + if (!strcmp(arg, "--sq")) { + output_sq = 1; + continue; + } + if (!strcmp(arg, "--not")) { + show_type ^= REVERSED; + continue; + } + if (!strcmp(arg, "--symbolic")) { + symbolic = SHOW_SYMBOLIC_ASIS; + continue; + } + if (!strcmp(arg, "--symbolic-full-name")) { + symbolic = SHOW_SYMBOLIC_FULL; + continue; + } + if (!prefixcmp(arg, "--abbrev-ref") && + (!arg[12] || arg[12] == '=')) { + abbrev_ref = 1; + abbrev_ref_strict = warn_ambiguous_refs; + if (arg[12] == '=') { + if (!strcmp(arg + 13, "strict")) + abbrev_ref_strict = 1; + else if (!strcmp(arg + 13, "loose")) + abbrev_ref_strict = 0; + else + die("unknown mode for %s", arg); + } + continue; + } + if (!strcmp(arg, "--all")) { + for_each_ref(show_reference, NULL); + continue; + } + if (!prefixcmp(arg, "--disambiguate=")) { + for_each_abbrev(arg + 15, show_abbrev, NULL); + continue; + } + if (!strcmp(arg, "--bisect")) { + for_each_ref_in("refs/bisect/bad", show_reference, NULL); + for_each_ref_in("refs/bisect/good", anti_reference, NULL); + continue; + } + if (!prefixcmp(arg, "--branches=")) { + for_each_glob_ref_in(show_reference, arg + 11, + "refs/heads/", NULL); + continue; + } + if (!strcmp(arg, "--branches")) { + for_each_branch_ref(show_reference, NULL); + continue; + } + if (!prefixcmp(arg, "--tags=")) { + for_each_glob_ref_in(show_reference, arg + 7, + "refs/tags/", NULL); + continue; + } + if (!strcmp(arg, "--tags")) { + for_each_tag_ref(show_reference, NULL); + continue; + } + if (!prefixcmp(arg, "--glob=")) { + for_each_glob_ref(show_reference, arg + 7, NULL); + continue; + } + if (!prefixcmp(arg, "--remotes=")) { + for_each_glob_ref_in(show_reference, arg + 10, + "refs/remotes/", NULL); + continue; + } + if (!strcmp(arg, "--remotes")) { + for_each_remote_ref(show_reference, NULL); + continue; + } + if (!strcmp(arg, "--show-toplevel")) { + const char *work_tree = get_git_work_tree(); + if (work_tree) + puts(work_tree); + continue; + } + if (!strcmp(arg, "--show-prefix")) { + if (prefix) + puts(prefix); + else + putchar('\n'); + continue; + } + if (!strcmp(arg, "--show-cdup")) { + const char *pfx = prefix; + if (!is_inside_work_tree()) { + const char *work_tree = + get_git_work_tree(); + if (work_tree) + printf("%s\n", work_tree); + continue; + } + while (pfx) { + pfx = strchr(pfx, '/'); + if (pfx) { + pfx++; + printf("../"); + } + } + putchar('\n'); + continue; + } + if (!strcmp(arg, "--git-dir")) { + const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); + static char cwd[PATH_MAX]; + int len; + if (gitdir) { + puts(gitdir); + continue; + } + if (!prefix) { + puts(".git"); + continue; + } + if (!getcwd(cwd, PATH_MAX)) + die_errno("unable to get current working directory"); + len = strlen(cwd); + printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : ""); + continue; + } + if (!strcmp(arg, "--is-inside-git-dir")) { + printf("%s\n", is_inside_git_dir() ? "true" + : "false"); + continue; + } + if (!strcmp(arg, "--is-inside-work-tree")) { + printf("%s\n", is_inside_work_tree() ? "true" + : "false"); + continue; + } + if (!strcmp(arg, "--is-bare-repository")) { + printf("%s\n", is_bare_repository() ? "true" + : "false"); + continue; + } + if (!prefixcmp(arg, "--since=")) { + show_datestring("--max-age=", arg+8); + continue; + } + if (!prefixcmp(arg, "--after=")) { + show_datestring("--max-age=", arg+8); + continue; + } + if (!prefixcmp(arg, "--before=")) { + show_datestring("--min-age=", arg+9); + continue; + } + if (!prefixcmp(arg, "--until=")) { + show_datestring("--min-age=", arg+8); + continue; + } + if (show_flag(arg) && verify) + die_no_single_rev(quiet); + continue; + } + + /* Not a flag argument */ + if (try_difference(arg)) + continue; + if (try_parent_shorthands(arg)) + continue; + name = arg; + type = NORMAL; + if (*arg == '^') { + name++; + type = REVERSED; + } + if (!get_sha1(name, sha1)) { + if (verify) + revs_count++; + else + show_rev(type, sha1, name); + continue; + } + if (verify) + die_no_single_rev(quiet); + as_is = 1; + if (!show_file(arg)) + continue; + verify_filename(prefix, arg, 1); + } + if (verify) { + if (revs_count == 1) { + show_rev(type, sha1, name); + return 0; + } else if (revs_count == 0 && show_default()) + return 0; + die_no_single_rev(quiet); + } else + show_default(); + return 0; +} diff --git a/builtin/revert.c b/builtin/revert.c new file mode 100644 index 0000000000..c5e36b94c0 --- /dev/null +++ b/builtin/revert.c @@ -0,0 +1,240 @@ +#include "cache.h" +#include "builtin.h" +#include "parse-options.h" +#include "diff.h" +#include "revision.h" +#include "rerere.h" +#include "dir.h" +#include "sequencer.h" + +/* + * This implements the builtins revert and cherry-pick. + * + * Copyright (c) 2007 Johannes E. Schindelin + * + * Based on git-revert.sh, which is + * + * Copyright (c) 2005 Linus Torvalds + * Copyright (c) 2005 Junio C Hamano + */ + +static const char * const revert_usage[] = { + N_("git revert [options] <commit-ish>"), + N_("git revert <subcommand>"), + NULL +}; + +static const char * const cherry_pick_usage[] = { + N_("git cherry-pick [options] <commit-ish>"), + N_("git cherry-pick <subcommand>"), + NULL +}; + +static const char *action_name(const struct replay_opts *opts) +{ + return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; +} + +static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts) +{ + return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage; +} + +static int option_parse_x(const struct option *opt, + const char *arg, int unset) +{ + struct replay_opts **opts_ptr = opt->value; + struct replay_opts *opts = *opts_ptr; + + if (unset) + return 0; + + ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); + opts->xopts[opts->xopts_nr++] = xstrdup(arg); + return 0; +} + +static void verify_opt_compatible(const char *me, const char *base_opt, ...) +{ + const char *this_opt; + va_list ap; + + va_start(ap, base_opt); + while ((this_opt = va_arg(ap, const char *))) { + if (va_arg(ap, int)) + break; + } + va_end(ap); + + if (this_opt) + die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt); +} + +static void verify_opt_mutually_compatible(const char *me, ...) +{ + const char *opt1, *opt2 = NULL; + va_list ap; + + va_start(ap, me); + while ((opt1 = va_arg(ap, const char *))) { + if (va_arg(ap, int)) + break; + } + if (opt1) { + while ((opt2 = va_arg(ap, const char *))) { + if (va_arg(ap, int)) + break; + } + } + va_end(ap); + + if (opt1 && opt2) + die(_("%s: %s cannot be used with %s"), me, opt1, opt2); +} + +static void parse_args(int argc, const char **argv, struct replay_opts *opts) +{ + const char * const * usage_str = revert_or_cherry_pick_usage(opts); + const char *me = action_name(opts); + int remove_state = 0; + int contin = 0; + int rollback = 0; + struct option options[] = { + OPT_BOOLEAN(0, "quit", &remove_state, N_("end revert or cherry-pick sequence")), + OPT_BOOLEAN(0, "continue", &contin, N_("resume revert or cherry-pick sequence")), + OPT_BOOLEAN(0, "abort", &rollback, N_("cancel revert or cherry-pick sequence")), + OPT_BOOLEAN('n', "no-commit", &opts->no_commit, N_("don't automatically commit")), + OPT_BOOLEAN('e', "edit", &opts->edit, N_("edit the commit message")), + OPT_NOOP_NOARG('r', NULL), + OPT_BOOLEAN('s', "signoff", &opts->signoff, N_("add Signed-off-by:")), + OPT_INTEGER('m', "mainline", &opts->mainline, N_("parent number")), + OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto), + OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")), + OPT_CALLBACK('X', "strategy-option", &opts, N_("option"), + N_("option for merge strategy"), option_parse_x), + OPT_END(), + OPT_END(), + OPT_END(), + OPT_END(), + OPT_END(), + OPT_END(), + }; + + if (opts->action == REPLAY_PICK) { + struct option cp_extra[] = { + OPT_BOOLEAN('x', NULL, &opts->record_origin, N_("append commit name")), + OPT_BOOLEAN(0, "ff", &opts->allow_ff, N_("allow fast-forward")), + OPT_BOOLEAN(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")), + OPT_BOOLEAN(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")), + OPT_BOOLEAN(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")), + OPT_END(), + }; + if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra)) + die(_("program error")); + } + + argc = parse_options(argc, argv, NULL, options, usage_str, + PARSE_OPT_KEEP_ARGV0 | + PARSE_OPT_KEEP_UNKNOWN); + + /* Check for incompatible subcommands */ + verify_opt_mutually_compatible(me, + "--quit", remove_state, + "--continue", contin, + "--abort", rollback, + NULL); + + /* implies allow_empty */ + if (opts->keep_redundant_commits) + opts->allow_empty = 1; + + /* Set the subcommand */ + if (remove_state) + opts->subcommand = REPLAY_REMOVE_STATE; + else if (contin) + opts->subcommand = REPLAY_CONTINUE; + else if (rollback) + opts->subcommand = REPLAY_ROLLBACK; + else + opts->subcommand = REPLAY_NONE; + + /* Check for incompatible command line arguments */ + if (opts->subcommand != REPLAY_NONE) { + char *this_operation; + if (opts->subcommand == REPLAY_REMOVE_STATE) + this_operation = "--quit"; + else if (opts->subcommand == REPLAY_CONTINUE) + this_operation = "--continue"; + else { + assert(opts->subcommand == REPLAY_ROLLBACK); + this_operation = "--abort"; + } + + verify_opt_compatible(me, this_operation, + "--no-commit", opts->no_commit, + "--signoff", opts->signoff, + "--mainline", opts->mainline, + "--strategy", opts->strategy ? 1 : 0, + "--strategy-option", opts->xopts ? 1 : 0, + "-x", opts->record_origin, + "--ff", opts->allow_ff, + NULL); + } + + if (opts->allow_ff) + verify_opt_compatible(me, "--ff", + "--signoff", opts->signoff, + "--no-commit", opts->no_commit, + "-x", opts->record_origin, + "--edit", opts->edit, + NULL); + + if (opts->subcommand != REPLAY_NONE) { + opts->revs = NULL; + } else { + struct setup_revision_opt s_r_opt; + opts->revs = xmalloc(sizeof(*opts->revs)); + init_revisions(opts->revs, NULL); + opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED; + if (argc < 2) + usage_with_options(usage_str, options); + memset(&s_r_opt, 0, sizeof(s_r_opt)); + s_r_opt.assume_dashdash = 1; + argc = setup_revisions(argc, argv, opts->revs, &s_r_opt); + } + + if (argc > 1) + usage_with_options(usage_str, options); +} + +int cmd_revert(int argc, const char **argv, const char *prefix) +{ + struct replay_opts opts; + int res; + + memset(&opts, 0, sizeof(opts)); + if (isatty(0)) + opts.edit = 1; + opts.action = REPLAY_REVERT; + git_config(git_default_config, NULL); + parse_args(argc, argv, &opts); + res = sequencer_pick_revisions(&opts); + if (res < 0) + die(_("revert failed")); + return res; +} + +int cmd_cherry_pick(int argc, const char **argv, const char *prefix) +{ + struct replay_opts opts; + int res; + + memset(&opts, 0, sizeof(opts)); + opts.action = REPLAY_PICK; + git_config(git_default_config, NULL); + parse_args(argc, argv, &opts); + res = sequencer_pick_revisions(&opts); + if (res < 0) + die(_("cherry-pick failed")); + return res; +} diff --git a/builtin/rm.c b/builtin/rm.c new file mode 100644 index 0000000000..dabfcf6890 --- /dev/null +++ b/builtin/rm.c @@ -0,0 +1,374 @@ +/* + * "git rm" builtin command + * + * Copyright (C) Linus Torvalds 2006 + */ +#include "cache.h" +#include "builtin.h" +#include "dir.h" +#include "cache-tree.h" +#include "tree-walk.h" +#include "parse-options.h" +#include "submodule.h" + +static const char * const builtin_rm_usage[] = { + N_("git rm [options] [--] <file>..."), + NULL +}; + +static struct { + int nr, alloc; + struct { + const char *name; + char is_submodule; + } *entry; +} list; + +static int get_ours_cache_pos(const char *path, int pos) +{ + int i = -pos - 1; + + while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) { + if (ce_stage(active_cache[i]) == 2) + return i; + i++; + } + return -1; +} + +static int check_submodules_use_gitfiles(void) +{ + int i; + int errs = 0; + + for (i = 0; i < list.nr; i++) { + const char *name = list.entry[i].name; + int pos; + struct cache_entry *ce; + struct stat st; + + pos = cache_name_pos(name, strlen(name)); + if (pos < 0) { + pos = get_ours_cache_pos(name, pos); + if (pos < 0) + continue; + } + ce = active_cache[pos]; + + if (!S_ISGITLINK(ce->ce_mode) || + (lstat(ce->name, &st) < 0) || + is_empty_dir(name)) + continue; + + if (!submodule_uses_gitfile(name)) + errs = error(_("submodule '%s' (or one of its nested " + "submodules) uses a .git directory\n" + "(use 'rm -rf' if you really want to remove " + "it including all of its history)"), name); + } + + return errs; +} + +static int check_local_mod(unsigned char *head, int index_only) +{ + /* + * Items in list are already sorted in the cache order, + * so we could do this a lot more efficiently by using + * tree_desc based traversal if we wanted to, but I am + * lazy, and who cares if removal of files is a tad + * slower than the theoretical maximum speed? + */ + int i, no_head; + int errs = 0; + + no_head = is_null_sha1(head); + for (i = 0; i < list.nr; i++) { + struct stat st; + int pos; + struct cache_entry *ce; + const char *name = list.entry[i].name; + unsigned char sha1[20]; + unsigned mode; + int local_changes = 0; + int staged_changes = 0; + + pos = cache_name_pos(name, strlen(name)); + if (pos < 0) { + /* + * Skip unmerged entries except for populated submodules + * that could lose history when removed. + */ + pos = get_ours_cache_pos(name, pos); + if (pos < 0) + continue; + + if (!S_ISGITLINK(active_cache[pos]->ce_mode) || + is_empty_dir(name)) + continue; + } + ce = active_cache[pos]; + + if (lstat(ce->name, &st) < 0) { + if (errno != ENOENT) + warning("'%s': %s", ce->name, strerror(errno)); + /* It already vanished from the working tree */ + continue; + } + else if (S_ISDIR(st.st_mode)) { + /* if a file was removed and it is now a + * directory, that is the same as ENOENT as + * far as git is concerned; we do not track + * directories unless they are submodules. + */ + if (!S_ISGITLINK(ce->ce_mode)) + continue; + } + + /* + * "rm" of a path that has changes need to be treated + * carefully not to allow losing local changes + * accidentally. A local change could be (1) file in + * work tree is different since the index; and/or (2) + * the user staged a content that is different from + * the current commit in the index. + * + * In such a case, you would need to --force the + * removal. However, "rm --cached" (remove only from + * the index) is safe if the index matches the file in + * the work tree or the HEAD commit, as it means that + * the content being removed is available elsewhere. + */ + + /* + * Is the index different from the file in the work tree? + * If it's a submodule, is its work tree modified? + */ + if (ce_match_stat(ce, &st, 0) || + (S_ISGITLINK(ce->ce_mode) && + !ok_to_remove_submodule(ce->name))) + local_changes = 1; + + /* + * Is the index different from the HEAD commit? By + * definition, before the very initial commit, + * anything staged in the index is treated by the same + * way as changed from the HEAD. + */ + if (no_head + || get_tree_entry(head, name, sha1, &mode) + || ce->ce_mode != create_ce_mode(mode) + || hashcmp(ce->sha1, sha1)) + staged_changes = 1; + + /* + * If the index does not match the file in the work + * tree and if it does not match the HEAD commit + * either, (1) "git rm" without --cached definitely + * will lose information; (2) "git rm --cached" will + * lose information unless it is about removing an + * "intent to add" entry. + */ + if (local_changes && staged_changes) { + if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) + errs = error(_("'%s' has staged content different " + "from both the file and the HEAD\n" + "(use -f to force removal)"), name); + } + else if (!index_only) { + if (staged_changes) + errs = error(_("'%s' has changes staged in the index\n" + "(use --cached to keep the file, " + "or -f to force removal)"), name); + if (local_changes) { + if (S_ISGITLINK(ce->ce_mode) && + !submodule_uses_gitfile(name)) { + errs = error(_("submodule '%s' (or one of its nested " + "submodules) uses a .git directory\n" + "(use 'rm -rf' if you really want to remove " + "it including all of its history)"), name); + } else + errs = error(_("'%s' has local modifications\n" + "(use --cached to keep the file, " + "or -f to force removal)"), name); + } + } + } + return errs; +} + +static struct lock_file lock_file; + +static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; +static int ignore_unmatch = 0; + +static struct option builtin_rm_options[] = { + OPT__DRY_RUN(&show_only, N_("dry run")), + OPT__QUIET(&quiet, N_("do not list removed files")), + OPT_BOOLEAN( 0 , "cached", &index_only, N_("only remove from the index")), + OPT__FORCE(&force, N_("override the up-to-date check")), + OPT_BOOLEAN('r', NULL, &recursive, N_("allow recursive removal")), + OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch, + N_("exit with a zero status even if nothing matched")), + OPT_END(), +}; + +int cmd_rm(int argc, const char **argv, const char *prefix) +{ + int i, newfd; + const char **pathspec; + char *seen; + + git_config(git_default_config, NULL); + + argc = parse_options(argc, argv, prefix, builtin_rm_options, + builtin_rm_usage, 0); + if (!argc) + usage_with_options(builtin_rm_usage, builtin_rm_options); + + if (!index_only) + setup_work_tree(); + + newfd = hold_locked_index(&lock_file, 1); + + if (read_cache() < 0) + die(_("index file corrupt")); + + /* + * Drop trailing directory separators from directories so we'll find + * submodules in the index. + */ + for (i = 0; i < argc; i++) { + size_t pathlen = strlen(argv[i]); + if (pathlen && is_dir_sep(argv[i][pathlen - 1]) && + is_directory(argv[i])) { + do { + pathlen--; + } while (pathlen && is_dir_sep(argv[i][pathlen - 1])); + argv[i] = xmemdupz(argv[i], pathlen); + } + } + + pathspec = get_pathspec(prefix, argv); + refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL); + + seen = NULL; + for (i = 0; pathspec[i] ; i++) + /* nothing */; + seen = xcalloc(i, 1); + + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen)) + continue; + ALLOC_GROW(list.entry, list.nr + 1, list.alloc); + list.entry[list.nr].name = ce->name; + list.entry[list.nr++].is_submodule = S_ISGITLINK(ce->ce_mode); + } + + if (pathspec) { + const char *match; + int seen_any = 0; + for (i = 0; (match = pathspec[i]) != NULL ; i++) { + if (!seen[i]) { + if (!ignore_unmatch) { + die(_("pathspec '%s' did not match any files"), + match); + } + } + else { + seen_any = 1; + } + if (!recursive && seen[i] == MATCHED_RECURSIVELY) + die(_("not removing '%s' recursively without -r"), + *match ? match : "."); + } + + if (! seen_any) + exit(0); + } + + /* + * If not forced, the file, the index and the HEAD (if exists) + * must match; but the file can already been removed, since + * this sequence is a natural "novice" way: + * + * rm F; git rm F + * + * Further, if HEAD commit exists, "diff-index --cached" must + * report no changes unless forced. + */ + if (!force) { + unsigned char sha1[20]; + if (get_sha1("HEAD", sha1)) + hashclr(sha1); + if (check_local_mod(sha1, index_only)) + exit(1); + } else if (!index_only) { + if (check_submodules_use_gitfiles()) + exit(1); + } + + /* + * First remove the names from the index: we won't commit + * the index unless all of them succeed. + */ + for (i = 0; i < list.nr; i++) { + const char *path = list.entry[i].name; + if (!quiet) + printf("rm '%s'\n", path); + + if (remove_file_from_cache(path)) + die(_("git rm: unable to remove %s"), path); + } + + if (show_only) + return 0; + + /* + * Then, unless we used "--cached", remove the filenames from + * the workspace. If we fail to remove the first one, we + * abort the "git rm" (but once we've successfully removed + * any file at all, we'll go ahead and commit to it all: + * by then we've already committed ourselves and can't fail + * in the middle) + */ + if (!index_only) { + int removed = 0; + for (i = 0; i < list.nr; i++) { + const char *path = list.entry[i].name; + if (list.entry[i].is_submodule) { + if (is_empty_dir(path)) { + if (!rmdir(path)) { + removed = 1; + continue; + } + } else { + struct strbuf buf = STRBUF_INIT; + strbuf_addstr(&buf, path); + if (!remove_dir_recursively(&buf, 0)) { + removed = 1; + strbuf_release(&buf); + continue; + } + strbuf_release(&buf); + /* Fallthrough and let remove_path() fail. */ + } + } + if (!remove_path(path)) { + removed = 1; + continue; + } + if (!removed) + die_errno("git rm: '%s'", path); + } + } + + if (active_cache_changed) { + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(&lock_file)) + die(_("Unable to write new index file")); + } + + return 0; +} diff --git a/builtin/send-pack.c b/builtin/send-pack.c new file mode 100644 index 0000000000..f849e0a4a0 --- /dev/null +++ b/builtin/send-pack.c @@ -0,0 +1,243 @@ +#include "builtin.h" +#include "commit.h" +#include "refs.h" +#include "pkt-line.h" +#include "sideband.h" +#include "run-command.h" +#include "remote.h" +#include "send-pack.h" +#include "quote.h" +#include "transport.h" +#include "version.h" + +static const char send_pack_usage[] = +"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n" +" --all and explicit <ref> specification are mutually exclusive."; + +static struct send_pack_args args; + +static void print_helper_status(struct ref *ref) +{ + struct strbuf buf = STRBUF_INIT; + + for (; ref; ref = ref->next) { + const char *msg = NULL; + const char *res; + + switch(ref->status) { + case REF_STATUS_NONE: + res = "error"; + msg = "no match"; + break; + + case REF_STATUS_OK: + res = "ok"; + break; + + case REF_STATUS_UPTODATE: + res = "ok"; + msg = "up to date"; + break; + + case REF_STATUS_REJECT_NONFASTFORWARD: + res = "error"; + msg = "non-fast forward"; + break; + + case REF_STATUS_REJECT_ALREADY_EXISTS: + res = "error"; + msg = "already exists"; + break; + + case REF_STATUS_REJECT_NODELETE: + case REF_STATUS_REMOTE_REJECT: + res = "error"; + break; + + case REF_STATUS_EXPECTING_REPORT: + default: + continue; + } + + strbuf_reset(&buf); + strbuf_addf(&buf, "%s %s", res, ref->name); + if (ref->remote_status) + msg = ref->remote_status; + if (msg) { + strbuf_addch(&buf, ' '); + quote_two_c_style(&buf, "", msg, 0); + } + strbuf_addch(&buf, '\n'); + + safe_write(1, buf.buf, buf.len); + } + strbuf_release(&buf); +} + +int cmd_send_pack(int argc, const char **argv, const char *prefix) +{ + int i, nr_refspecs = 0; + const char **refspecs = NULL; + const char *remote_name = NULL; + struct remote *remote = NULL; + const char *dest = NULL; + int fd[2]; + struct child_process *conn; + struct extra_have_objects extra_have; + struct ref *remote_refs, *local_refs; + int ret; + int helper_status = 0; + int send_all = 0; + const char *receivepack = "git-receive-pack"; + int flags; + unsigned int reject_reasons; + int progress = -1; + + argv++; + for (i = 1; i < argc; i++, argv++) { + const char *arg = *argv; + + if (*arg == '-') { + if (!prefixcmp(arg, "--receive-pack=")) { + receivepack = arg + 15; + continue; + } + if (!prefixcmp(arg, "--exec=")) { + receivepack = arg + 7; + continue; + } + if (!prefixcmp(arg, "--remote=")) { + remote_name = arg + 9; + continue; + } + if (!strcmp(arg, "--all")) { + send_all = 1; + continue; + } + if (!strcmp(arg, "--dry-run")) { + args.dry_run = 1; + continue; + } + if (!strcmp(arg, "--mirror")) { + args.send_mirror = 1; + continue; + } + if (!strcmp(arg, "--force")) { + args.force_update = 1; + continue; + } + if (!strcmp(arg, "--quiet")) { + args.quiet = 1; + continue; + } + if (!strcmp(arg, "--verbose")) { + args.verbose = 1; + continue; + } + if (!strcmp(arg, "--progress")) { + progress = 1; + continue; + } + if (!strcmp(arg, "--no-progress")) { + progress = 0; + continue; + } + if (!strcmp(arg, "--thin")) { + args.use_thin_pack = 1; + continue; + } + if (!strcmp(arg, "--stateless-rpc")) { + args.stateless_rpc = 1; + continue; + } + if (!strcmp(arg, "--helper-status")) { + helper_status = 1; + continue; + } + usage(send_pack_usage); + } + if (!dest) { + dest = arg; + continue; + } + refspecs = (const char **) argv; + nr_refspecs = argc - i; + break; + } + if (!dest) + usage(send_pack_usage); + /* + * --all and --mirror are incompatible; neither makes sense + * with any refspecs. + */ + if ((refspecs && (send_all || args.send_mirror)) || + (send_all && args.send_mirror)) + usage(send_pack_usage); + + if (remote_name) { + remote = remote_get(remote_name); + if (!remote_has_url(remote, dest)) { + die("Destination %s is not a uri for %s", + dest, remote_name); + } + } + + if (progress == -1) + progress = !args.quiet && isatty(2); + args.progress = progress; + + if (args.stateless_rpc) { + conn = NULL; + fd[0] = 0; + fd[1] = 1; + } else { + conn = git_connect(fd, dest, receivepack, + args.verbose ? CONNECT_VERBOSE : 0); + } + + memset(&extra_have, 0, sizeof(extra_have)); + + get_remote_heads(fd[0], &remote_refs, REF_NORMAL, &extra_have); + + transport_verify_remote_names(nr_refspecs, refspecs); + + local_refs = get_local_heads(); + + flags = MATCH_REFS_NONE; + + if (send_all) + flags |= MATCH_REFS_ALL; + if (args.send_mirror) + flags |= MATCH_REFS_MIRROR; + + /* match them up */ + if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags)) + return -1; + + set_ref_status_for_push(remote_refs, args.send_mirror, + args.force_update); + + ret = send_pack(&args, fd, conn, remote_refs, &extra_have); + + if (helper_status) + print_helper_status(remote_refs); + + close(fd[1]); + close(fd[0]); + + ret |= finish_connect(conn); + + if (!helper_status) + transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons); + + if (!args.dry_run && remote) { + struct ref *ref; + for (ref = remote_refs; ref; ref = ref->next) + transport_update_tracking_ref(remote, ref, args.verbose); + } + + if (!ret && !transport_refs_pushed(remote_refs)) + fprintf(stderr, "Everything up-to-date\n"); + + return ret; +} diff --git a/builtin/shortlog.c b/builtin/shortlog.c new file mode 100644 index 0000000000..240bff3efa --- /dev/null +++ b/builtin/shortlog.c @@ -0,0 +1,328 @@ +#include "builtin.h" +#include "cache.h" +#include "commit.h" +#include "diff.h" +#include "string-list.h" +#include "revision.h" +#include "utf8.h" +#include "mailmap.h" +#include "shortlog.h" +#include "parse-options.h" + +static char const * const shortlog_usage[] = { + N_("git shortlog [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]"), + "", + N_("[rev-opts] are documented in git-rev-list(1)"), + NULL +}; + +static int compare_by_number(const void *a1, const void *a2) +{ + const struct string_list_item *i1 = a1, *i2 = a2; + const struct string_list *l1 = i1->util, *l2 = i2->util; + + if (l1->nr < l2->nr) + return 1; + else if (l1->nr == l2->nr) + return 0; + else + return -1; +} + +static void insert_one_record(struct shortlog *log, + const char *author, + const char *oneline) +{ + const char *dot3 = log->common_repo_prefix; + char *buffer, *p; + struct string_list_item *item; + const char *mailbuf, *namebuf; + size_t namelen, maillen; + const char *eol; + struct strbuf subject = STRBUF_INIT; + struct strbuf namemailbuf = STRBUF_INIT; + struct ident_split ident; + + if (split_ident_line(&ident, author, strlen(author))) + return; + + namebuf = ident.name_begin; + mailbuf = ident.mail_begin; + namelen = ident.name_end - ident.name_begin; + maillen = ident.mail_end - ident.mail_begin; + + map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen); + strbuf_add(&namemailbuf, namebuf, namelen); + + if (log->email) + strbuf_addf(&namemailbuf, " <%.*s>", (int)maillen, mailbuf); + + item = string_list_insert(&log->list, namemailbuf.buf); + if (item->util == NULL) + item->util = xcalloc(1, sizeof(struct string_list)); + + /* Skip any leading whitespace, including any blank lines. */ + while (*oneline && isspace(*oneline)) + oneline++; + eol = strchr(oneline, '\n'); + if (!eol) + eol = oneline + strlen(oneline); + if (!prefixcmp(oneline, "[PATCH")) { + char *eob = strchr(oneline, ']'); + if (eob && (!eol || eob < eol)) + oneline = eob + 1; + } + while (*oneline && isspace(*oneline) && *oneline != '\n') + oneline++; + format_subject(&subject, oneline, " "); + buffer = strbuf_detach(&subject, NULL); + + if (dot3) { + int dot3len = strlen(dot3); + if (dot3len > 5) { + while ((p = strstr(buffer, dot3)) != NULL) { + int taillen = strlen(p) - dot3len; + memcpy(p, "/.../", 5); + memmove(p + 5, p + dot3len, taillen + 1); + } + } + } + + string_list_append(item->util, buffer); +} + +static void read_from_stdin(struct shortlog *log) +{ + char author[1024], oneline[1024]; + + while (fgets(author, sizeof(author), stdin) != NULL) { + if (!(author[0] == 'A' || author[0] == 'a') || + prefixcmp(author + 1, "uthor: ")) + continue; + while (fgets(oneline, sizeof(oneline), stdin) && + oneline[0] != '\n') + ; /* discard headers */ + while (fgets(oneline, sizeof(oneline), stdin) && + oneline[0] == '\n') + ; /* discard blanks */ + insert_one_record(log, author + 8, oneline); + } +} + +void shortlog_add_commit(struct shortlog *log, struct commit *commit) +{ + const char *author = NULL, *buffer; + struct strbuf buf = STRBUF_INIT; + struct strbuf ufbuf = STRBUF_INIT; + + pp_commit_easy(CMIT_FMT_RAW, commit, &buf); + buffer = buf.buf; + while (*buffer && *buffer != '\n') { + const char *eol = strchr(buffer, '\n'); + + if (eol == NULL) + eol = buffer + strlen(buffer); + else + eol++; + + if (!prefixcmp(buffer, "author ")) + author = buffer + 7; + buffer = eol; + } + if (!author) + die(_("Missing author: %s"), + sha1_to_hex(commit->object.sha1)); + if (log->user_format) { + struct pretty_print_context ctx = {0}; + ctx.fmt = CMIT_FMT_USERFORMAT; + ctx.abbrev = log->abbrev; + ctx.subject = ""; + ctx.after_subject = ""; + ctx.date_mode = DATE_NORMAL; + pretty_print_commit(&ctx, commit, &ufbuf); + buffer = ufbuf.buf; + } else if (*buffer) { + buffer++; + } + insert_one_record(log, author, !*buffer ? "<none>" : buffer); + strbuf_release(&ufbuf); + strbuf_release(&buf); +} + +static void get_from_rev(struct rev_info *rev, struct shortlog *log) +{ + struct commit *commit; + + if (prepare_revision_walk(rev)) + die(_("revision walk setup failed")); + while ((commit = get_revision(rev)) != NULL) + shortlog_add_commit(log, commit); +} + +static int parse_uint(char const **arg, int comma, int defval) +{ + unsigned long ul; + int ret; + char *endp; + + ul = strtoul(*arg, &endp, 10); + if (*endp && *endp != comma) + return -1; + if (ul > INT_MAX) + return -1; + ret = *arg == endp ? defval : (int)ul; + *arg = *endp ? endp + 1 : endp; + return ret; +} + +static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]"; +#define DEFAULT_WRAPLEN 76 +#define DEFAULT_INDENT1 6 +#define DEFAULT_INDENT2 9 + +static int parse_wrap_args(const struct option *opt, const char *arg, int unset) +{ + struct shortlog *log = opt->value; + + log->wrap_lines = !unset; + if (unset) + return 0; + if (!arg) { + log->wrap = DEFAULT_WRAPLEN; + log->in1 = DEFAULT_INDENT1; + log->in2 = DEFAULT_INDENT2; + return 0; + } + + log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN); + log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1); + log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2); + if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0) + return error(wrap_arg_usage); + if (log->wrap && + ((log->in1 && log->wrap <= log->in1) || + (log->in2 && log->wrap <= log->in2))) + return error(wrap_arg_usage); + return 0; +} + +void shortlog_init(struct shortlog *log) +{ + memset(log, 0, sizeof(*log)); + + read_mailmap(&log->mailmap, &log->common_repo_prefix); + + log->list.strdup_strings = 1; + log->wrap = DEFAULT_WRAPLEN; + log->in1 = DEFAULT_INDENT1; + log->in2 = DEFAULT_INDENT2; +} + +int cmd_shortlog(int argc, const char **argv, const char *prefix) +{ + static struct shortlog log; + static struct rev_info rev; + int nongit = !startup_info->have_repository; + + static const struct option options[] = { + OPT_BOOLEAN('n', "numbered", &log.sort_by_number, + N_("sort output according to the number of commits per author")), + OPT_BOOLEAN('s', "summary", &log.summary, + N_("Suppress commit descriptions, only provides commit count")), + OPT_BOOLEAN('e', "email", &log.email, + N_("Show the email address of each author")), + { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"), + N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args }, + OPT_END(), + }; + + struct parse_opt_ctx_t ctx; + + git_config(git_default_config, NULL); + shortlog_init(&log); + init_revisions(&rev, prefix); + parse_options_start(&ctx, argc, argv, prefix, options, + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); + + for (;;) { + switch (parse_options_step(&ctx, options, shortlog_usage)) { + case PARSE_OPT_HELP: + exit(129); + case PARSE_OPT_DONE: + goto parse_done; + } + parse_revision_opt(&rev, &ctx, options, shortlog_usage); + } +parse_done: + argc = parse_options_end(&ctx); + + if (setup_revisions(argc, argv, &rev, NULL) != 1) { + error(_("unrecognized argument: %s"), argv[1]); + usage_with_options(shortlog_usage, options); + } + + log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT; + log.abbrev = rev.abbrev; + + /* assume HEAD if from a tty */ + if (!nongit && !rev.pending.nr && isatty(0)) + add_head_to_pending(&rev); + if (rev.pending.nr == 0) { + if (isatty(0)) + fprintf(stderr, _("(reading log message from standard input)\n")); + read_from_stdin(&log); + } + else + get_from_rev(&rev, &log); + + shortlog_output(&log); + return 0; +} + +static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s, + const struct shortlog *log) +{ + strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap); + strbuf_addch(sb, '\n'); +} + +void shortlog_output(struct shortlog *log) +{ + int i, j; + struct strbuf sb = STRBUF_INIT; + + if (log->sort_by_number) + qsort(log->list.items, log->list.nr, sizeof(struct string_list_item), + compare_by_number); + for (i = 0; i < log->list.nr; i++) { + struct string_list *onelines = log->list.items[i].util; + + if (log->summary) { + printf("%6d\t%s\n", onelines->nr, log->list.items[i].string); + } else { + printf("%s (%d):\n", log->list.items[i].string, onelines->nr); + for (j = onelines->nr - 1; j >= 0; j--) { + const char *msg = onelines->items[j].string; + + if (log->wrap_lines) { + strbuf_reset(&sb); + add_wrapped_shortlog_msg(&sb, msg, log); + fwrite(sb.buf, sb.len, 1, stdout); + } + else + printf(" %s\n", msg); + } + putchar('\n'); + } + + onelines->strdup_strings = 1; + string_list_clear(onelines, 0); + free(onelines); + log->list.items[i].util = NULL; + } + + strbuf_release(&sb); + log->list.strdup_strings = 1; + string_list_clear(&log->list, 1); + clear_mailmap(&log->mailmap); +} diff --git a/builtin/show-branch.c b/builtin/show-branch.c new file mode 100644 index 0000000000..d208fd6c68 --- /dev/null +++ b/builtin/show-branch.c @@ -0,0 +1,952 @@ +#include "cache.h" +#include "commit.h" +#include "refs.h" +#include "builtin.h" +#include "color.h" +#include "parse-options.h" + +static const char* show_branch_usage[] = { + N_("git show-branch [-a|--all] [-r|--remotes] [--topo-order | --date-order] [--current] [--color[=<when>] | --no-color] [--sparse] [--more=<n> | --list | --independent | --merge-base] [--no-name | --sha1-name] [--topics] [(<rev> | <glob>)...]"), + N_("git show-branch (-g|--reflog)[=<n>[,<base>]] [--list] [<ref>]"), + NULL +}; + +static int showbranch_use_color = -1; + +static int default_num; +static int default_alloc; +static const char **default_arg; + +#define UNINTERESTING 01 + +#define REV_SHIFT 2 +#define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */ + +#define DEFAULT_REFLOG 4 + +static const char *get_color_code(int idx) +{ + if (want_color(showbranch_use_color)) + return column_colors_ansi[idx % column_colors_ansi_max]; + return ""; +} + +static const char *get_color_reset_code(void) +{ + if (want_color(showbranch_use_color)) + return GIT_COLOR_RESET; + return ""; +} + +static struct commit *interesting(struct commit_list *list) +{ + while (list) { + struct commit *commit = list->item; + list = list->next; + if (commit->object.flags & UNINTERESTING) + continue; + return commit; + } + return NULL; +} + +static struct commit *pop_one_commit(struct commit_list **list_p) +{ + struct commit *commit; + struct commit_list *list; + list = *list_p; + commit = list->item; + *list_p = list->next; + free(list); + return commit; +} + +struct commit_name { + const char *head_name; /* which head's ancestor? */ + int generation; /* how many parents away from head_name */ +}; + +/* Name the commit as nth generation ancestor of head_name; + * we count only the first-parent relationship for naming purposes. + */ +static void name_commit(struct commit *commit, const char *head_name, int nth) +{ + struct commit_name *name; + if (!commit->util) + commit->util = xmalloc(sizeof(struct commit_name)); + name = commit->util; + name->head_name = head_name; + name->generation = nth; +} + +/* Parent is the first parent of the commit. We may name it + * as (n+1)th generation ancestor of the same head_name as + * commit is nth generation ancestor of, if that generation + * number is better than the name it already has. + */ +static void name_parent(struct commit *commit, struct commit *parent) +{ + struct commit_name *commit_name = commit->util; + struct commit_name *parent_name = parent->util; + if (!commit_name) + return; + if (!parent_name || + commit_name->generation + 1 < parent_name->generation) + name_commit(parent, commit_name->head_name, + commit_name->generation + 1); +} + +static int name_first_parent_chain(struct commit *c) +{ + int i = 0; + while (c) { + struct commit *p; + if (!c->util) + break; + if (!c->parents) + break; + p = c->parents->item; + if (!p->util) { + name_parent(c, p); + i++; + } + else + break; + c = p; + } + return i; +} + +static void name_commits(struct commit_list *list, + struct commit **rev, + char **ref_name, + int num_rev) +{ + struct commit_list *cl; + struct commit *c; + int i; + + /* First give names to the given heads */ + for (cl = list; cl; cl = cl->next) { + c = cl->item; + if (c->util) + continue; + for (i = 0; i < num_rev; i++) { + if (rev[i] == c) { + name_commit(c, ref_name[i], 0); + break; + } + } + } + + /* Then commits on the first parent ancestry chain */ + do { + i = 0; + for (cl = list; cl; cl = cl->next) { + i += name_first_parent_chain(cl->item); + } + } while (i); + + /* Finally, any unnamed commits */ + do { + i = 0; + for (cl = list; cl; cl = cl->next) { + struct commit_list *parents; + struct commit_name *n; + int nth; + c = cl->item; + if (!c->util) + continue; + n = c->util; + parents = c->parents; + nth = 0; + while (parents) { + struct commit *p = parents->item; + char newname[1000], *en; + parents = parents->next; + nth++; + if (p->util) + continue; + en = newname; + switch (n->generation) { + case 0: + en += sprintf(en, "%s", n->head_name); + break; + case 1: + en += sprintf(en, "%s^", n->head_name); + break; + default: + en += sprintf(en, "%s~%d", + n->head_name, n->generation); + break; + } + if (nth == 1) + en += sprintf(en, "^"); + else + en += sprintf(en, "^%d", nth); + name_commit(p, xstrdup(newname), 0); + i++; + name_first_parent_chain(p); + } + } + } while (i); +} + +static int mark_seen(struct commit *commit, struct commit_list **seen_p) +{ + if (!commit->object.flags) { + commit_list_insert(commit, seen_p); + return 1; + } + return 0; +} + +static void join_revs(struct commit_list **list_p, + struct commit_list **seen_p, + int num_rev, int extra) +{ + int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); + int all_revs = all_mask & ~((1u << REV_SHIFT) - 1); + + while (*list_p) { + struct commit_list *parents; + int still_interesting = !!interesting(*list_p); + struct commit *commit = pop_one_commit(list_p); + int flags = commit->object.flags & all_mask; + + if (!still_interesting && extra <= 0) + break; + + mark_seen(commit, seen_p); + if ((flags & all_revs) == all_revs) + flags |= UNINTERESTING; + parents = commit->parents; + + while (parents) { + struct commit *p = parents->item; + int this_flag = p->object.flags; + parents = parents->next; + if ((this_flag & flags) == flags) + continue; + if (!p->object.parsed) + parse_commit(p); + if (mark_seen(p, seen_p) && !still_interesting) + extra--; + p->object.flags |= flags; + commit_list_insert_by_date(p, list_p); + } + } + + /* + * Postprocess to complete well-poisoning. + * + * At this point we have all the commits we have seen in + * seen_p list. Mark anything that can be reached from + * uninteresting commits not interesting. + */ + for (;;) { + int changed = 0; + struct commit_list *s; + for (s = *seen_p; s; s = s->next) { + struct commit *c = s->item; + struct commit_list *parents; + + if (((c->object.flags & all_revs) != all_revs) && + !(c->object.flags & UNINTERESTING)) + continue; + + /* The current commit is either a merge base or + * already uninteresting one. Mark its parents + * as uninteresting commits _only_ if they are + * already parsed. No reason to find new ones + * here. + */ + parents = c->parents; + while (parents) { + struct commit *p = parents->item; + parents = parents->next; + if (!(p->object.flags & UNINTERESTING)) { + p->object.flags |= UNINTERESTING; + changed = 1; + } + } + } + if (!changed) + break; + } +} + +static void show_one_commit(struct commit *commit, int no_name) +{ + struct strbuf pretty = STRBUF_INIT; + const char *pretty_str = "(unavailable)"; + struct commit_name *name = commit->util; + + if (commit->object.parsed) { + pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty); + pretty_str = pretty.buf; + } + if (!prefixcmp(pretty_str, "[PATCH] ")) + pretty_str += 8; + + if (!no_name) { + if (name && name->head_name) { + printf("[%s", name->head_name); + if (name->generation) { + if (name->generation == 1) + printf("^"); + else + printf("~%d", name->generation); + } + printf("] "); + } + else + printf("[%s] ", + find_unique_abbrev(commit->object.sha1, + DEFAULT_ABBREV)); + } + puts(pretty_str); + strbuf_release(&pretty); +} + +static char *ref_name[MAX_REVS + 1]; +static int ref_name_cnt; + +static const char *find_digit_prefix(const char *s, int *v) +{ + const char *p; + int ver; + char ch; + + for (p = s, ver = 0; + '0' <= (ch = *p) && ch <= '9'; + p++) + ver = ver * 10 + ch - '0'; + *v = ver; + return p; +} + + +static int version_cmp(const char *a, const char *b) +{ + while (1) { + int va, vb; + + a = find_digit_prefix(a, &va); + b = find_digit_prefix(b, &vb); + if (va != vb) + return va - vb; + + while (1) { + int ca = *a; + int cb = *b; + if ('0' <= ca && ca <= '9') + ca = 0; + if ('0' <= cb && cb <= '9') + cb = 0; + if (ca != cb) + return ca - cb; + if (!ca) + break; + a++; + b++; + } + if (!*a && !*b) + return 0; + } +} + +static int compare_ref_name(const void *a_, const void *b_) +{ + const char * const*a = a_, * const*b = b_; + return version_cmp(*a, *b); +} + +static void sort_ref_range(int bottom, int top) +{ + qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]), + compare_ref_name); +} + +static int append_ref(const char *refname, const unsigned char *sha1, + int allow_dups) +{ + struct commit *commit = lookup_commit_reference_gently(sha1, 1); + int i; + + if (!commit) + return 0; + + if (!allow_dups) { + /* Avoid adding the same thing twice */ + for (i = 0; i < ref_name_cnt; i++) + if (!strcmp(refname, ref_name[i])) + return 0; + } + if (MAX_REVS <= ref_name_cnt) { + warning("ignoring %s; cannot handle more than %d refs", + refname, MAX_REVS); + return 0; + } + ref_name[ref_name_cnt++] = xstrdup(refname); + ref_name[ref_name_cnt] = NULL; + return 0; +} + +static int append_head_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + unsigned char tmp[20]; + int ofs = 11; + if (prefixcmp(refname, "refs/heads/")) + return 0; + /* If both heads/foo and tags/foo exists, get_sha1 would + * get confused. + */ + if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1)) + ofs = 5; + return append_ref(refname + ofs, sha1, 0); +} + +static int append_remote_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + unsigned char tmp[20]; + int ofs = 13; + if (prefixcmp(refname, "refs/remotes/")) + return 0; + /* If both heads/foo and tags/foo exists, get_sha1 would + * get confused. + */ + if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1)) + ofs = 5; + return append_ref(refname + ofs, sha1, 0); +} + +static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + if (prefixcmp(refname, "refs/tags/")) + return 0; + return append_ref(refname + 5, sha1, 0); +} + +static const char *match_ref_pattern = NULL; +static int match_ref_slash = 0; +static int count_slash(const char *s) +{ + int cnt = 0; + while (*s) + if (*s++ == '/') + cnt++; + return cnt; +} + +static int append_matching_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + /* we want to allow pattern hold/<asterisk> to show all + * branches under refs/heads/hold/, and v0.99.9? to show + * refs/tags/v0.99.9a and friends. + */ + const char *tail; + int slash = count_slash(refname); + for (tail = refname; *tail && match_ref_slash < slash; ) + if (*tail++ == '/') + slash--; + if (!*tail) + return 0; + if (fnmatch(match_ref_pattern, tail, 0)) + return 0; + if (!prefixcmp(refname, "refs/heads/")) + return append_head_ref(refname, sha1, flag, cb_data); + if (!prefixcmp(refname, "refs/tags/")) + return append_tag_ref(refname, sha1, flag, cb_data); + return append_ref(refname, sha1, 0); +} + +static void snarf_refs(int head, int remotes) +{ + if (head) { + int orig_cnt = ref_name_cnt; + for_each_ref(append_head_ref, NULL); + sort_ref_range(orig_cnt, ref_name_cnt); + } + if (remotes) { + int orig_cnt = ref_name_cnt; + for_each_ref(append_remote_ref, NULL); + sort_ref_range(orig_cnt, ref_name_cnt); + } +} + +static int rev_is_head(char *head, int headlen, char *name, + unsigned char *head_sha1, unsigned char *sha1) +{ + if ((!head[0]) || + (head_sha1 && sha1 && hashcmp(head_sha1, sha1))) + return 0; + if (!prefixcmp(head, "refs/heads/")) + head += 11; + if (!prefixcmp(name, "refs/heads/")) + name += 11; + else if (!prefixcmp(name, "heads/")) + name += 6; + return !strcmp(head, name); +} + +static int show_merge_base(struct commit_list *seen, int num_rev) +{ + int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); + int all_revs = all_mask & ~((1u << REV_SHIFT) - 1); + int exit_status = 1; + + while (seen) { + struct commit *commit = pop_one_commit(&seen); + int flags = commit->object.flags & all_mask; + if (!(flags & UNINTERESTING) && + ((flags & all_revs) == all_revs)) { + puts(sha1_to_hex(commit->object.sha1)); + exit_status = 0; + commit->object.flags |= UNINTERESTING; + } + } + return exit_status; +} + +static int show_independent(struct commit **rev, + int num_rev, + char **ref_name, + unsigned int *rev_mask) +{ + int i; + + for (i = 0; i < num_rev; i++) { + struct commit *commit = rev[i]; + unsigned int flag = rev_mask[i]; + + if (commit->object.flags == flag) + puts(sha1_to_hex(commit->object.sha1)); + commit->object.flags |= UNINTERESTING; + } + return 0; +} + +static void append_one_rev(const char *av) +{ + unsigned char revkey[20]; + if (!get_sha1(av, revkey)) { + append_ref(av, revkey, 0); + return; + } + if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) { + /* glob style match */ + int saved_matches = ref_name_cnt; + match_ref_pattern = av; + match_ref_slash = count_slash(av); + for_each_ref(append_matching_ref, NULL); + if (saved_matches == ref_name_cnt && + ref_name_cnt < MAX_REVS) + error("no matching refs with %s", av); + if (saved_matches + 1 < ref_name_cnt) + sort_ref_range(saved_matches, ref_name_cnt); + return; + } + die("bad sha1 reference %s", av); +} + +static int git_show_branch_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "showbranch.default")) { + if (!value) + return config_error_nonbool(var); + /* + * default_arg is now passed to parse_options(), so we need to + * mimic the real argv a bit better. + */ + if (!default_num) { + default_alloc = 20; + default_arg = xcalloc(default_alloc, sizeof(*default_arg)); + default_arg[default_num++] = "show-branch"; + } else if (default_alloc <= default_num + 1) { + default_alloc = default_alloc * 3 / 2 + 20; + default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc); + } + default_arg[default_num++] = xstrdup(value); + default_arg[default_num] = NULL; + return 0; + } + + if (!strcmp(var, "color.showbranch")) { + showbranch_use_color = git_config_colorbool(var, value); + return 0; + } + + return git_color_default_config(var, value, cb); +} + +static int omit_in_dense(struct commit *commit, struct commit **rev, int n) +{ + /* If the commit is tip of the named branches, do not + * omit it. + * Otherwise, if it is a merge that is reachable from only one + * tip, it is not that interesting. + */ + int i, flag, count; + for (i = 0; i < n; i++) + if (rev[i] == commit) + return 0; + flag = commit->object.flags; + for (i = count = 0; i < n; i++) { + if (flag & (1u << (i + REV_SHIFT))) + count++; + } + if (count == 1) + return 1; + return 0; +} + +static int reflog = 0; + +static int parse_reflog_param(const struct option *opt, const char *arg, + int unset) +{ + char *ep; + const char **base = (const char **)opt->value; + if (!arg) + arg = ""; + reflog = strtoul(arg, &ep, 10); + if (*ep == ',') + *base = ep + 1; + else if (*ep) + return error("unrecognized reflog param '%s'", arg); + else + *base = NULL; + if (reflog <= 0) + reflog = DEFAULT_REFLOG; + return 0; +} + +int cmd_show_branch(int ac, const char **av, const char *prefix) +{ + struct commit *rev[MAX_REVS], *commit; + char *reflog_msg[MAX_REVS]; + struct commit_list *list = NULL, *seen = NULL; + unsigned int rev_mask[MAX_REVS]; + int num_rev, i, extra = 0; + int all_heads = 0, all_remotes = 0; + int all_mask, all_revs; + int lifo = 1; + char head[128]; + const char *head_p; + int head_len; + unsigned char head_sha1[20]; + int merge_base = 0; + int independent = 0; + int no_name = 0; + int sha1_name = 0; + int shown_merge_point = 0; + int with_current_branch = 0; + int head_at = -1; + int topics = 0; + int dense = 1; + const char *reflog_base = NULL; + struct option builtin_show_branch_options[] = { + OPT_BOOLEAN('a', "all", &all_heads, + N_("show remote-tracking and local branches")), + OPT_BOOLEAN('r', "remotes", &all_remotes, + N_("show remote-tracking branches")), + OPT__COLOR(&showbranch_use_color, + N_("color '*!+-' corresponding to the branch")), + { OPTION_INTEGER, 0, "more", &extra, N_("n"), + N_("show <n> more commits after the common ancestor"), + PARSE_OPT_OPTARG, NULL, (intptr_t)1 }, + OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1), + OPT_BOOLEAN(0, "no-name", &no_name, N_("suppress naming strings")), + OPT_BOOLEAN(0, "current", &with_current_branch, + N_("include the current branch")), + OPT_BOOLEAN(0, "sha1-name", &sha1_name, + N_("name commits with their object names")), + OPT_BOOLEAN(0, "merge-base", &merge_base, + N_("show possible merge bases")), + OPT_BOOLEAN(0, "independent", &independent, + N_("show refs unreachable from any other ref")), + OPT_BOOLEAN(0, "topo-order", &lifo, + N_("show commits in topological order")), + OPT_BOOLEAN(0, "topics", &topics, + N_("show only commits not on the first branch")), + OPT_SET_INT(0, "sparse", &dense, + N_("show merges reachable from only one tip"), 0), + OPT_SET_INT(0, "date-order", &lifo, + N_("show commits where no parent comes before its " + "children"), 0), + { OPTION_CALLBACK, 'g', "reflog", &reflog_base, N_("<n>[,<base>]"), + N_("show <n> most recent ref-log entries starting at " + "base"), + PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, + parse_reflog_param }, + OPT_END() + }; + + git_config(git_show_branch_config, NULL); + + /* If nothing is specified, try the default first */ + if (ac == 1 && default_num) { + ac = default_num; + av = default_arg; + } + + ac = parse_options(ac, av, prefix, builtin_show_branch_options, + show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION); + if (all_heads) + all_remotes = 1; + + if (extra || reflog) { + /* "listing" mode is incompatible with + * independent nor merge-base modes. + */ + if (independent || merge_base) + usage_with_options(show_branch_usage, + builtin_show_branch_options); + if (reflog && ((0 < extra) || all_heads || all_remotes)) + /* + * Asking for --more in reflog mode does not + * make sense. --list is Ok. + * + * Also --all and --remotes do not make sense either. + */ + die("--reflog is incompatible with --all, --remotes, " + "--independent or --merge-base"); + } + + /* If nothing is specified, show all branches by default */ + if (ac + all_heads + all_remotes == 0) + all_heads = 1; + + if (reflog) { + unsigned char sha1[20]; + char nth_desc[256]; + char *ref; + int base = 0; + + if (ac == 0) { + static const char *fake_av[2]; + + fake_av[0] = resolve_refdup("HEAD", sha1, 1, NULL); + fake_av[1] = NULL; + av = fake_av; + ac = 1; + } + if (ac != 1) + die("--reflog option needs one branch name"); + + if (MAX_REVS < reflog) + die("Only %d entries can be shown at one time.", + MAX_REVS); + if (!dwim_ref(*av, strlen(*av), sha1, &ref)) + die("No such ref %s", *av); + + /* Has the base been specified? */ + if (reflog_base) { + char *ep; + base = strtoul(reflog_base, &ep, 10); + if (*ep) { + /* Ah, that is a date spec... */ + unsigned long at; + at = approxidate(reflog_base); + read_ref_at(ref, at, -1, sha1, NULL, + NULL, NULL, &base); + } + } + + for (i = 0; i < reflog; i++) { + char *logmsg, *m; + const char *msg; + unsigned long timestamp; + int tz; + + if (read_ref_at(ref, 0, base+i, sha1, &logmsg, + ×tamp, &tz, NULL)) { + reflog = i; + break; + } + msg = strchr(logmsg, '\t'); + if (!msg) + msg = "(none)"; + else + msg++; + m = xmalloc(strlen(msg) + 200); + sprintf(m, "(%s) %s", + show_date(timestamp, tz, 1), + msg); + reflog_msg[i] = m; + free(logmsg); + sprintf(nth_desc, "%s@{%d}", *av, base+i); + append_ref(nth_desc, sha1, 1); + } + } + else if (all_heads + all_remotes) + snarf_refs(all_heads, all_remotes); + else { + while (0 < ac) { + append_one_rev(*av); + ac--; av++; + } + } + + head_p = resolve_ref_unsafe("HEAD", head_sha1, 1, NULL); + if (head_p) { + head_len = strlen(head_p); + memcpy(head, head_p, head_len + 1); + } + else { + head_len = 0; + head[0] = 0; + } + + if (with_current_branch && head_p) { + int has_head = 0; + for (i = 0; !has_head && i < ref_name_cnt; i++) { + /* We are only interested in adding the branch + * HEAD points at. + */ + if (rev_is_head(head, + head_len, + ref_name[i], + head_sha1, NULL)) + has_head++; + } + if (!has_head) { + int offset = !prefixcmp(head, "refs/heads/") ? 11 : 0; + append_one_rev(head + offset); + } + } + + if (!ref_name_cnt) { + fprintf(stderr, "No revs to be shown.\n"); + exit(0); + } + + for (num_rev = 0; ref_name[num_rev]; num_rev++) { + unsigned char revkey[20]; + unsigned int flag = 1u << (num_rev + REV_SHIFT); + + if (MAX_REVS <= num_rev) + die("cannot handle more than %d revs.", MAX_REVS); + if (get_sha1(ref_name[num_rev], revkey)) + die("'%s' is not a valid ref.", ref_name[num_rev]); + commit = lookup_commit_reference(revkey); + if (!commit) + die("cannot find commit %s (%s)", + ref_name[num_rev], revkey); + parse_commit(commit); + mark_seen(commit, &seen); + + /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1, + * and so on. REV_SHIFT bits from bit 0 are used for + * internal bookkeeping. + */ + commit->object.flags |= flag; + if (commit->object.flags == flag) + commit_list_insert_by_date(commit, &list); + rev[num_rev] = commit; + } + for (i = 0; i < num_rev; i++) + rev_mask[i] = rev[i]->object.flags; + + if (0 <= extra) + join_revs(&list, &seen, num_rev, extra); + + commit_list_sort_by_date(&seen); + + if (merge_base) + return show_merge_base(seen, num_rev); + + if (independent) + return show_independent(rev, num_rev, ref_name, rev_mask); + + /* Show list; --more=-1 means list-only */ + if (1 < num_rev || extra < 0) { + for (i = 0; i < num_rev; i++) { + int j; + int is_head = rev_is_head(head, + head_len, + ref_name[i], + head_sha1, + rev[i]->object.sha1); + if (extra < 0) + printf("%c [%s] ", + is_head ? '*' : ' ', ref_name[i]); + else { + for (j = 0; j < i; j++) + putchar(' '); + printf("%s%c%s [%s] ", + get_color_code(i), + is_head ? '*' : '!', + get_color_reset_code(), ref_name[i]); + } + + if (!reflog) { + /* header lines never need name */ + show_one_commit(rev[i], 1); + } + else + puts(reflog_msg[i]); + + if (is_head) + head_at = i; + } + if (0 <= extra) { + for (i = 0; i < num_rev; i++) + putchar('-'); + putchar('\n'); + } + } + if (extra < 0) + exit(0); + + /* Sort topologically */ + sort_in_topological_order(&seen, lifo); + + /* Give names to commits */ + if (!sha1_name && !no_name) + name_commits(seen, rev, ref_name, num_rev); + + all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); + all_revs = all_mask & ~((1u << REV_SHIFT) - 1); + + while (seen) { + struct commit *commit = pop_one_commit(&seen); + int this_flag = commit->object.flags; + int is_merge_point = ((this_flag & all_revs) == all_revs); + + shown_merge_point |= is_merge_point; + + if (1 < num_rev) { + int is_merge = !!(commit->parents && + commit->parents->next); + if (topics && + !is_merge_point && + (this_flag & (1u << REV_SHIFT))) + continue; + if (dense && is_merge && + omit_in_dense(commit, rev, num_rev)) + continue; + for (i = 0; i < num_rev; i++) { + int mark; + if (!(this_flag & (1u << (i + REV_SHIFT)))) + mark = ' '; + else if (is_merge) + mark = '-'; + else if (i == head_at) + mark = '*'; + else + mark = '+'; + printf("%s%c%s", + get_color_code(i), + mark, get_color_reset_code()); + } + putchar(' '); + } + show_one_commit(commit, no_name); + + if (shown_merge_point && --extra < 0) + break; + } + return 0; +} diff --git a/builtin/show-ref.c b/builtin/show-ref.c new file mode 100644 index 0000000000..8d9b76a02f --- /dev/null +++ b/builtin/show-ref.c @@ -0,0 +1,233 @@ +#include "builtin.h" +#include "cache.h" +#include "refs.h" +#include "object.h" +#include "tag.h" +#include "string-list.h" +#include "parse-options.h" + +static const char * const show_ref_usage[] = { + N_("git show-ref [-q|--quiet] [--verify] [--head] [-d|--dereference] [-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [pattern*] "), + N_("git show-ref --exclude-existing[=pattern] < ref-list"), + NULL +}; + +static int deref_tags, show_head, tags_only, heads_only, found_match, verify, + quiet, hash_only, abbrev, exclude_arg; +static const char **pattern; +static const char *exclude_existing_arg; + +static void show_one(const char *refname, const unsigned char *sha1) +{ + const char *hex = find_unique_abbrev(sha1, abbrev); + if (hash_only) + printf("%s\n", hex); + else + printf("%s %s\n", hex, refname); +} + +static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata) +{ + const char *hex; + unsigned char peeled[20]; + + if (tags_only || heads_only) { + int match; + + match = heads_only && !prefixcmp(refname, "refs/heads/"); + match |= tags_only && !prefixcmp(refname, "refs/tags/"); + if (!match) + return 0; + } + if (pattern) { + int reflen = strlen(refname); + const char **p = pattern, *m; + while ((m = *p++) != NULL) { + int len = strlen(m); + if (len > reflen) + continue; + if (memcmp(m, refname + reflen - len, len)) + continue; + if (len == reflen) + goto match; + /* "--verify" requires an exact match */ + if (verify) + continue; + if (refname[reflen - len - 1] == '/') + goto match; + } + return 0; + } + +match: + found_match++; + + /* This changes the semantics slightly that even under quiet we + * detect and return error if the repository is corrupt and + * ref points at a nonexistent object. + */ + if (!has_sha1_file(sha1)) + die("git show-ref: bad ref %s (%s)", refname, + sha1_to_hex(sha1)); + + if (quiet) + return 0; + + show_one(refname, sha1); + + if (!deref_tags) + return 0; + + if (!peel_ref(refname, peeled)) { + hex = find_unique_abbrev(peeled, abbrev); + printf("%s %s^{}\n", hex, refname); + } + return 0; +} + +static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata) +{ + struct string_list *list = (struct string_list *)cbdata; + string_list_insert(list, refname); + return 0; +} + +/* + * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input, + * and + * (1) strip "^{}" at the end of line if any; + * (2) ignore if match is provided and does not head-match refname; + * (3) warn if refname is not a well-formed refname and skip; + * (4) ignore if refname is a ref that exists in the local repository; + * (5) otherwise output the line. + */ +static int exclude_existing(const char *match) +{ + static struct string_list existing_refs = STRING_LIST_INIT_NODUP; + char buf[1024]; + int matchlen = match ? strlen(match) : 0; + + for_each_ref(add_existing, &existing_refs); + while (fgets(buf, sizeof(buf), stdin)) { + char *ref; + int len = strlen(buf); + + if (len > 0 && buf[len - 1] == '\n') + buf[--len] = '\0'; + if (3 <= len && !strcmp(buf + len - 3, "^{}")) { + len -= 3; + buf[len] = '\0'; + } + for (ref = buf + len; buf < ref; ref--) + if (isspace(ref[-1])) + break; + if (match) { + int reflen = buf + len - ref; + if (reflen < matchlen) + continue; + if (strncmp(ref, match, matchlen)) + continue; + } + if (check_refname_format(ref, 0)) { + warning("ref '%s' ignored", ref); + continue; + } + if (!string_list_has_string(&existing_refs, ref)) { + printf("%s\n", buf); + } + } + return 0; +} + +static int hash_callback(const struct option *opt, const char *arg, int unset) +{ + hash_only = 1; + /* Use full length SHA1 if no argument */ + if (!arg) + return 0; + return parse_opt_abbrev_cb(opt, arg, unset); +} + +static int exclude_existing_callback(const struct option *opt, const char *arg, + int unset) +{ + exclude_arg = 1; + *(const char **)opt->value = arg; + return 0; +} + +static int help_callback(const struct option *opt, const char *arg, int unset) +{ + return -1; +} + +static const struct option show_ref_options[] = { + OPT_BOOLEAN(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")), + OPT_BOOLEAN(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")), + OPT_BOOLEAN(0, "verify", &verify, N_("stricter reference checking, " + "requires exact ref path")), + { OPTION_BOOLEAN, 'h', NULL, &show_head, NULL, + N_("show the HEAD reference"), + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, + OPT_BOOLEAN(0, "head", &show_head, N_("show the HEAD reference")), + OPT_BOOLEAN('d', "dereference", &deref_tags, + N_("dereference tags into object IDs")), + { OPTION_CALLBACK, 's', "hash", &abbrev, N_("n"), + N_("only show SHA1 hash using <n> digits"), + PARSE_OPT_OPTARG, &hash_callback }, + OPT__ABBREV(&abbrev), + OPT__QUIET(&quiet, + N_("do not print results to stdout (useful with --verify)")), + { OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg, + N_("pattern"), N_("show refs from stdin that aren't in local repository"), + PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback }, + { OPTION_CALLBACK, 0, "help-all", NULL, NULL, N_("show usage"), + PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback }, + OPT_END() +}; + +int cmd_show_ref(int argc, const char **argv, const char *prefix) +{ + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(show_ref_usage, show_ref_options); + + argc = parse_options(argc, argv, prefix, show_ref_options, + show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP); + + if (exclude_arg) + return exclude_existing(exclude_existing_arg); + + pattern = argv; + if (!*pattern) + pattern = NULL; + + if (verify) { + if (!pattern) + die("--verify requires a reference"); + while (*pattern) { + unsigned char sha1[20]; + + if (!prefixcmp(*pattern, "refs/") && + !read_ref(*pattern, sha1)) { + if (!quiet) + show_one(*pattern, sha1); + } + else if (!quiet) + die("'%s' - not a valid ref", *pattern); + else + return 1; + pattern++; + } + return 0; + } + + if (show_head) + head_ref(show_ref, NULL); + for_each_ref(show_ref, NULL); + if (!found_match) { + if (verify && !quiet) + die("No match"); + return 1; + } + return 0; +} diff --git a/builtin/stripspace.c b/builtin/stripspace.c new file mode 100644 index 0000000000..f16986c0ae --- /dev/null +++ b/builtin/stripspace.c @@ -0,0 +1,88 @@ +#include "builtin.h" +#include "cache.h" + +/* + * Returns the length of a line, without trailing spaces. + * + * If the line ends with newline, it will be removed too. + */ +static size_t cleanup(char *line, size_t len) +{ + while (len) { + unsigned char c = line[len - 1]; + if (!isspace(c)) + break; + len--; + } + + return len; +} + +/* + * Remove empty lines from the beginning and end + * and also trailing spaces from every line. + * + * Turn multiple consecutive empty lines between paragraphs + * into just one empty line. + * + * If the input has only empty lines and spaces, + * no output will be produced. + * + * If last line does not have a newline at the end, one is added. + * + * Enable skip_comments to skip every line starting with "#". + */ +void stripspace(struct strbuf *sb, int skip_comments) +{ + int empties = 0; + size_t i, j, len, newlen; + char *eol; + + /* We may have to add a newline. */ + strbuf_grow(sb, 1); + + for (i = j = 0; i < sb->len; i += len, j += newlen) { + eol = memchr(sb->buf + i, '\n', sb->len - i); + len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; + + if (skip_comments && len && sb->buf[i] == '#') { + newlen = 0; + continue; + } + newlen = cleanup(sb->buf + i, len); + + /* Not just an empty line? */ + if (newlen) { + if (empties > 0 && j > 0) + sb->buf[j++] = '\n'; + empties = 0; + memmove(sb->buf + j, sb->buf + i, newlen); + sb->buf[newlen + j++] = '\n'; + } else { + empties++; + } + } + + strbuf_setlen(sb, j); +} + +int cmd_stripspace(int argc, const char **argv, const char *prefix) +{ + struct strbuf buf = STRBUF_INIT; + int strip_comments = 0; + + if (argc == 2 && (!strcmp(argv[1], "-s") || + !strcmp(argv[1], "--strip-comments"))) + strip_comments = 1; + else if (argc > 1) + usage("git stripspace [-s | --strip-comments] < input"); + + if (strbuf_read(&buf, 0, 1024) < 0) + die_errno("could not read the input"); + + stripspace(&buf, strip_comments); + + write_or_die(1, buf.buf, buf.len); + strbuf_release(&buf); + return 0; +} diff --git a/builtin/symbolic-ref.c b/builtin/symbolic-ref.c new file mode 100644 index 0000000000..f481959421 --- /dev/null +++ b/builtin/symbolic-ref.c @@ -0,0 +1,76 @@ +#include "builtin.h" +#include "cache.h" +#include "refs.h" +#include "parse-options.h" + +static const char * const git_symbolic_ref_usage[] = { + N_("git symbolic-ref [options] name [ref]"), + N_("git symbolic-ref -d [-q] name"), + NULL +}; + +static int check_symref(const char *HEAD, int quiet, int shorten, int print) +{ + unsigned char sha1[20]; + int flag; + const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag); + + if (!refname) + die("No such ref: %s", HEAD); + else if (!(flag & REF_ISSYMREF)) { + if (!quiet) + die("ref %s is not a symbolic ref", HEAD); + else + return 1; + } + if (print) { + if (shorten) + refname = shorten_unambiguous_ref(refname, 0); + puts(refname); + } + return 0; +} + +int cmd_symbolic_ref(int argc, const char **argv, const char *prefix) +{ + int quiet = 0, delete = 0, shorten = 0, ret = 0; + const char *msg = NULL; + struct option options[] = { + OPT__QUIET(&quiet, + N_("suppress error message for non-symbolic (detached) refs")), + OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")), + OPT_BOOL(0, "short", &shorten, N_("shorten ref output")), + OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")), + OPT_END(), + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, + git_symbolic_ref_usage, 0); + if (msg &&!*msg) + die("Refusing to perform update with empty message"); + + if (delete) { + if (argc != 1) + usage_with_options(git_symbolic_ref_usage, options); + ret = check_symref(argv[0], 1, 0, 0); + if (ret) + die("Cannot delete %s, not a symbolic ref", argv[0]); + return delete_ref(argv[0], NULL, REF_NODEREF); + } + + switch (argc) { + case 1: + ret = check_symref(argv[0], quiet, shorten, 1); + break; + case 2: + if (!strcmp(argv[0], "HEAD") && + prefixcmp(argv[1], "refs/")) + die("Refusing to point HEAD outside of refs/"); + create_symref(argv[0], argv[1], msg); + break; + default: + usage_with_options(git_symbolic_ref_usage, options); + } + return ret; +} diff --git a/builtin/tag.c b/builtin/tag.c new file mode 100644 index 0000000000..9c3e0673d5 --- /dev/null +++ b/builtin/tag.c @@ -0,0 +1,593 @@ +/* + * Builtin "git tag" + * + * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>, + * Carlos Rica <jasampler@gmail.com> + * Based on git-tag.sh and mktag.c by Linus Torvalds. + */ + +#include "cache.h" +#include "builtin.h" +#include "refs.h" +#include "tag.h" +#include "run-command.h" +#include "parse-options.h" +#include "diff.h" +#include "revision.h" +#include "gpg-interface.h" +#include "sha1-array.h" +#include "column.h" + +static const char * const git_tag_usage[] = { + N_("git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]"), + N_("git tag -d <tagname>..."), + N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] " + "\n\t\t[<pattern>...]"), + N_("git tag -v <tagname>..."), + NULL +}; + +struct tag_filter { + const char **patterns; + int lines; + struct commit_list *with_commit; +}; + +static struct sha1_array points_at; +static unsigned int colopts; + +static int match_pattern(const char **patterns, const char *ref) +{ + /* no pattern means match everything */ + if (!*patterns) + return 1; + for (; *patterns; patterns++) + if (!fnmatch(*patterns, ref, 0)) + return 1; + return 0; +} + +static const unsigned char *match_points_at(const char *refname, + const unsigned char *sha1) +{ + const unsigned char *tagged_sha1 = NULL; + struct object *obj; + + if (sha1_array_lookup(&points_at, sha1) >= 0) + return sha1; + obj = parse_object(sha1); + if (!obj) + die(_("malformed object at '%s'"), refname); + if (obj->type == OBJ_TAG) + tagged_sha1 = ((struct tag *)obj)->tagged->sha1; + if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0) + return tagged_sha1; + return NULL; +} + +static int in_commit_list(const struct commit_list *want, struct commit *c) +{ + for (; want; want = want->next) + if (!hashcmp(want->item->object.sha1, c->object.sha1)) + return 1; + return 0; +} + +static int contains_recurse(struct commit *candidate, + const struct commit_list *want) +{ + struct commit_list *p; + + /* was it previously marked as containing a want commit? */ + if (candidate->object.flags & TMP_MARK) + return 1; + /* or marked as not possibly containing a want commit? */ + if (candidate->object.flags & UNINTERESTING) + return 0; + /* or are we it? */ + if (in_commit_list(want, candidate)) + return 1; + + if (parse_commit(candidate) < 0) + return 0; + + /* Otherwise recurse and mark ourselves for future traversals. */ + for (p = candidate->parents; p; p = p->next) { + if (contains_recurse(p->item, want)) { + candidate->object.flags |= TMP_MARK; + return 1; + } + } + candidate->object.flags |= UNINTERESTING; + return 0; +} + +static int contains(struct commit *candidate, const struct commit_list *want) +{ + return contains_recurse(candidate, want); +} + +static void show_tag_lines(const unsigned char *sha1, int lines) +{ + int i; + unsigned long size; + enum object_type type; + char *buf, *sp, *eol; + size_t len; + + buf = read_sha1_file(sha1, &type, &size); + if (!buf) + die_errno("unable to read object %s", sha1_to_hex(sha1)); + if (type != OBJ_COMMIT && type != OBJ_TAG) + goto free_return; + if (!size) + die("an empty %s object %s?", + typename(type), sha1_to_hex(sha1)); + + /* skip header */ + sp = strstr(buf, "\n\n"); + if (!sp) + goto free_return; + + /* only take up to "lines" lines, and strip the signature from a tag */ + if (type == OBJ_TAG) + size = parse_signature(buf, size); + for (i = 0, sp += 2; i < lines && sp < buf + size; i++) { + if (i) + printf("\n "); + eol = memchr(sp, '\n', size - (sp - buf)); + len = eol ? eol - sp : size - (sp - buf); + fwrite(sp, len, 1, stdout); + if (!eol) + break; + sp = eol + 1; + } +free_return: + free(buf); +} + +static int show_reference(const char *refname, const unsigned char *sha1, + int flag, void *cb_data) +{ + struct tag_filter *filter = cb_data; + + if (match_pattern(filter->patterns, refname)) { + if (filter->with_commit) { + struct commit *commit; + + commit = lookup_commit_reference_gently(sha1, 1); + if (!commit) + return 0; + if (!contains(commit, filter->with_commit)) + return 0; + } + + if (points_at.nr && !match_points_at(refname, sha1)) + return 0; + + if (!filter->lines) { + printf("%s\n", refname); + return 0; + } + printf("%-15s ", refname); + show_tag_lines(sha1, filter->lines); + putchar('\n'); + } + + return 0; +} + +static int list_tags(const char **patterns, int lines, + struct commit_list *with_commit) +{ + struct tag_filter filter; + + filter.patterns = patterns; + filter.lines = lines; + filter.with_commit = with_commit; + + for_each_tag_ref(show_reference, (void *) &filter); + + return 0; +} + +typedef int (*each_tag_name_fn)(const char *name, const char *ref, + const unsigned char *sha1); + +static int for_each_tag_name(const char **argv, each_tag_name_fn fn) +{ + const char **p; + char ref[PATH_MAX]; + int had_error = 0; + unsigned char sha1[20]; + + for (p = argv; *p; p++) { + if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p) + >= sizeof(ref)) { + error(_("tag name too long: %.*s..."), 50, *p); + had_error = 1; + continue; + } + if (read_ref(ref, sha1)) { + error(_("tag '%s' not found."), *p); + had_error = 1; + continue; + } + if (fn(*p, ref, sha1)) + had_error = 1; + } + return had_error; +} + +static int delete_tag(const char *name, const char *ref, + const unsigned char *sha1) +{ + if (delete_ref(ref, sha1, 0)) + return 1; + printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV)); + return 0; +} + +static int verify_tag(const char *name, const char *ref, + const unsigned char *sha1) +{ + const char *argv_verify_tag[] = {"verify-tag", + "-v", "SHA1_HEX", NULL}; + argv_verify_tag[2] = sha1_to_hex(sha1); + + if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD)) + return error(_("could not verify the tag '%s'"), name); + return 0; +} + +static int do_sign(struct strbuf *buffer) +{ + return sign_buffer(buffer, buffer, get_signing_key()); +} + +static const char tag_template[] = + N_("\n" + "#\n" + "# Write a tag message\n" + "# Lines starting with '#' will be ignored.\n" + "#\n"); + +static const char tag_template_nocleanup[] = + N_("\n" + "#\n" + "# Write a tag message\n" + "# Lines starting with '#' will be kept; you may remove them" + " yourself if you want to.\n" + "#\n"); + +static int git_tag_config(const char *var, const char *value, void *cb) +{ + int status = git_gpg_config(var, value, cb); + if (status) + return status; + if (!prefixcmp(var, "column.")) + return git_column_config(var, value, "tag", &colopts); + return git_default_config(var, value, cb); +} + +static void write_tag_body(int fd, const unsigned char *sha1) +{ + unsigned long size; + enum object_type type; + char *buf, *sp; + + buf = read_sha1_file(sha1, &type, &size); + if (!buf) + return; + /* skip header */ + sp = strstr(buf, "\n\n"); + + if (!sp || !size || type != OBJ_TAG) { + free(buf); + return; + } + sp += 2; /* skip the 2 LFs */ + write_or_die(fd, sp, parse_signature(sp, buf + size - sp)); + + free(buf); +} + +static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result) +{ + if (sign && do_sign(buf) < 0) + return error(_("unable to sign the tag")); + if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0) + return error(_("unable to write tag file")); + return 0; +} + +struct create_tag_options { + unsigned int message_given:1; + unsigned int sign; + enum { + CLEANUP_NONE, + CLEANUP_SPACE, + CLEANUP_ALL + } cleanup_mode; +}; + +static void create_tag(const unsigned char *object, const char *tag, + struct strbuf *buf, struct create_tag_options *opt, + unsigned char *prev, unsigned char *result) +{ + enum object_type type; + char header_buf[1024]; + int header_len; + char *path = NULL; + + type = sha1_object_info(object, NULL); + if (type <= OBJ_NONE) + die(_("bad object type.")); + + header_len = snprintf(header_buf, sizeof(header_buf), + "object %s\n" + "type %s\n" + "tag %s\n" + "tagger %s\n\n", + sha1_to_hex(object), + typename(type), + tag, + git_committer_info(IDENT_STRICT)); + + if (header_len > sizeof(header_buf) - 1) + die(_("tag header too big.")); + + if (!opt->message_given) { + int fd; + + /* write the template message before editing: */ + path = git_pathdup("TAG_EDITMSG"); + fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); + if (fd < 0) + die_errno(_("could not create file '%s'"), path); + + if (!is_null_sha1(prev)) + write_tag_body(fd, prev); + else if (opt->cleanup_mode == CLEANUP_ALL) + write_or_die(fd, _(tag_template), + strlen(_(tag_template))); + else + write_or_die(fd, _(tag_template_nocleanup), + strlen(_(tag_template_nocleanup))); + close(fd); + + if (launch_editor(path, buf, NULL)) { + fprintf(stderr, + _("Please supply the message using either -m or -F option.\n")); + exit(1); + } + } + + if (opt->cleanup_mode != CLEANUP_NONE) + stripspace(buf, opt->cleanup_mode == CLEANUP_ALL); + + if (!opt->message_given && !buf->len) + die(_("no tag message?")); + + strbuf_insert(buf, 0, header_buf, header_len); + + if (build_tag_object(buf, opt->sign, result) < 0) { + if (path) + fprintf(stderr, _("The tag message has been left in %s\n"), + path); + exit(128); + } + if (path) { + unlink_or_warn(path); + free(path); + } +} + +struct msg_arg { + int given; + struct strbuf buf; +}; + +static int parse_msg_arg(const struct option *opt, const char *arg, int unset) +{ + struct msg_arg *msg = opt->value; + + if (!arg) + return -1; + if (msg->buf.len) + strbuf_addstr(&(msg->buf), "\n\n"); + strbuf_addstr(&(msg->buf), arg); + msg->given = 1; + return 0; +} + +static int strbuf_check_tag_ref(struct strbuf *sb, const char *name) +{ + if (name[0] == '-') + return -1; + + strbuf_reset(sb); + strbuf_addf(sb, "refs/tags/%s", name); + + return check_refname_format(sb->buf, 0); +} + +static int parse_opt_points_at(const struct option *opt __attribute__((unused)), + const char *arg, int unset) +{ + unsigned char sha1[20]; + + if (unset) { + sha1_array_clear(&points_at); + return 0; + } + if (!arg) + return error(_("switch 'points-at' requires an object")); + if (get_sha1(arg, sha1)) + return error(_("malformed object name '%s'"), arg); + sha1_array_append(&points_at, sha1); + return 0; +} + +int cmd_tag(int argc, const char **argv, const char *prefix) +{ + struct strbuf buf = STRBUF_INIT; + struct strbuf ref = STRBUF_INIT; + unsigned char object[20], prev[20]; + const char *object_ref, *tag; + struct ref_lock *lock; + struct create_tag_options opt; + char *cleanup_arg = NULL; + int annotate = 0, force = 0, lines = -1, list = 0, + delete = 0, verify = 0; + const char *msgfile = NULL, *keyid = NULL; + struct msg_arg msg = { 0, STRBUF_INIT }; + struct commit_list *with_commit = NULL; + struct option options[] = { + OPT_BOOLEAN('l', "list", &list, N_("list tag names")), + { OPTION_INTEGER, 'n', NULL, &lines, N_("n"), + N_("print <n> lines of each tag message"), + PARSE_OPT_OPTARG, NULL, 1 }, + OPT_BOOLEAN('d', "delete", &delete, N_("delete tags")), + OPT_BOOLEAN('v', "verify", &verify, N_("verify tags")), + + OPT_GROUP(N_("Tag creation options")), + OPT_BOOLEAN('a', "annotate", &annotate, + N_("annotated tag, needs a message")), + OPT_CALLBACK('m', "message", &msg, N_("message"), + N_("tag message"), parse_msg_arg), + OPT_FILENAME('F', "file", &msgfile, N_("read message from file")), + OPT_BOOLEAN('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")), + OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"), + N_("how to strip spaces and #comments from message")), + OPT_STRING('u', "local-user", &keyid, N_("key id"), + N_("use another key to sign the tag")), + OPT__FORCE(&force, N_("replace the tag if exists")), + OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")), + + OPT_GROUP(N_("Tag listing options")), + { + OPTION_CALLBACK, 0, "contains", &with_commit, N_("commit"), + N_("print only tags that contain the commit"), + PARSE_OPT_LASTARG_DEFAULT, + parse_opt_with_commit, (intptr_t)"HEAD", + }, + { + OPTION_CALLBACK, 0, "points-at", NULL, N_("object"), + N_("print only tags of the object"), 0, parse_opt_points_at + }, + OPT_END() + }; + + git_config(git_tag_config, NULL); + + memset(&opt, 0, sizeof(opt)); + + argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0); + + if (keyid) { + opt.sign = 1; + set_signing_key(keyid); + } + if (opt.sign) + annotate = 1; + if (argc == 0 && !(delete || verify)) + list = 1; + + if ((annotate || msg.given || msgfile || force) && + (list || delete || verify)) + usage_with_options(git_tag_usage, options); + + if (list + delete + verify > 1) + usage_with_options(git_tag_usage, options); + finalize_colopts(&colopts, -1); + if (list && lines != -1) { + if (explicitly_enable_column(colopts)) + die(_("--column and -n are incompatible")); + colopts = 0; + } + if (list) { + int ret; + if (column_active(colopts)) { + struct column_options copts; + memset(&copts, 0, sizeof(copts)); + copts.padding = 2; + run_column_filter(colopts, &copts); + } + ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit); + if (column_active(colopts)) + stop_column_filter(); + return ret; + } + if (lines != -1) + die(_("-n option is only allowed with -l.")); + if (with_commit) + die(_("--contains option is only allowed with -l.")); + if (points_at.nr) + die(_("--points-at option is only allowed with -l.")); + if (delete) + return for_each_tag_name(argv, delete_tag); + if (verify) + return for_each_tag_name(argv, verify_tag); + + if (msg.given || msgfile) { + if (msg.given && msgfile) + die(_("only one -F or -m option is allowed.")); + annotate = 1; + if (msg.given) + strbuf_addbuf(&buf, &(msg.buf)); + else { + if (!strcmp(msgfile, "-")) { + if (strbuf_read(&buf, 0, 1024) < 0) + die_errno(_("cannot read '%s'"), msgfile); + } else { + if (strbuf_read_file(&buf, msgfile, 1024) < 0) + die_errno(_("could not open or read '%s'"), + msgfile); + } + } + } + + tag = argv[0]; + + object_ref = argc == 2 ? argv[1] : "HEAD"; + if (argc > 2) + die(_("too many params")); + + if (get_sha1(object_ref, object)) + die(_("Failed to resolve '%s' as a valid ref."), object_ref); + + if (strbuf_check_tag_ref(&ref, tag)) + die(_("'%s' is not a valid tag name."), tag); + + if (read_ref(ref.buf, prev)) + hashclr(prev); + else if (!force) + die(_("tag '%s' already exists"), tag); + + opt.message_given = msg.given || msgfile; + + if (!cleanup_arg || !strcmp(cleanup_arg, "strip")) + opt.cleanup_mode = CLEANUP_ALL; + else if (!strcmp(cleanup_arg, "verbatim")) + opt.cleanup_mode = CLEANUP_NONE; + else if (!strcmp(cleanup_arg, "whitespace")) + opt.cleanup_mode = CLEANUP_SPACE; + else + die(_("Invalid cleanup mode %s"), cleanup_arg); + + if (annotate) + create_tag(object, tag, &buf, &opt, prev, object); + + lock = lock_any_ref_for_update(ref.buf, prev, 0); + if (!lock) + die(_("%s: cannot lock the ref"), ref.buf); + if (write_ref_sha1(lock, object, NULL) < 0) + die(_("%s: cannot update the ref"), ref.buf); + if (force && hashcmp(prev, object)) + printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV)); + + strbuf_release(&buf); + strbuf_release(&ref); + return 0; +} diff --git a/builtin/tar-tree.c b/builtin/tar-tree.c new file mode 100644 index 0000000000..3f1e7012db --- /dev/null +++ b/builtin/tar-tree.c @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2005, 2006 Rene Scharfe + */ +#include "cache.h" +#include "commit.h" +#include "tar.h" +#include "builtin.h" +#include "quote.h" + +static const char tar_tree_usage[] = +"git tar-tree [--remote=<repo>] <tree-ish> [basedir]\n" +"*** Note that this command is now deprecated; use \"git archive\" instead."; + +static const char builtin_get_tar_commit_id_usage[] = +"git get-tar-commit-id < <tarfile>"; + +int cmd_tar_tree(int argc, const char **argv, const char *prefix) +{ + /* + * "git tar-tree" is now a wrapper around "git archive --format=tar" + * + * $0 --remote=<repo> arg... ==> + * git archive --format=tar --remote=<repo> arg... + * $0 tree-ish ==> + * git archive --format=tar tree-ish + * $0 tree-ish basedir ==> + * git archive --format-tar --prefix=basedir tree-ish + */ + int i; + const char **nargv = xcalloc(sizeof(*nargv), argc + 3); + char *basedir_arg; + int nargc = 0; + + nargv[nargc++] = "archive"; + nargv[nargc++] = "--format=tar"; + + if (2 <= argc && !prefixcmp(argv[1], "--remote=")) { + nargv[nargc++] = argv[1]; + argv++; + argc--; + } + + /* + * Because it's just a compatibility wrapper, tar-tree supports only + * the old behaviour of reading attributes from the work tree. + */ + nargv[nargc++] = "--worktree-attributes"; + + switch (argc) { + default: + usage(tar_tree_usage); + break; + case 3: + /* base-path */ + basedir_arg = xmalloc(strlen(argv[2]) + 11); + sprintf(basedir_arg, "--prefix=%s/", argv[2]); + nargv[nargc++] = basedir_arg; + /* fallthru */ + case 2: + /* tree-ish */ + nargv[nargc++] = argv[1]; + } + nargv[nargc] = NULL; + + fprintf(stderr, + "*** \"git tar-tree\" is now deprecated.\n" + "*** Running \"git archive\" instead.\n***"); + for (i = 0; i < nargc; i++) { + fputc(' ', stderr); + sq_quote_print(stderr, nargv[i]); + } + fputc('\n', stderr); + return cmd_archive(nargc, nargv, prefix); +} + +/* ustar header + extended global header content */ +#define RECORDSIZE (512) +#define HEADERSIZE (2 * RECORDSIZE) + +int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix) +{ + char buffer[HEADERSIZE]; + struct ustar_header *header = (struct ustar_header *)buffer; + char *content = buffer + RECORDSIZE; + ssize_t n; + + if (argc != 1) + usage(builtin_get_tar_commit_id_usage); + + n = read_in_full(0, buffer, HEADERSIZE); + if (n < HEADERSIZE) + die("git get-tar-commit-id: read error"); + if (header->typeflag[0] != 'g') + return 1; + if (memcmp(content, "52 comment=", 11)) + return 1; + + n = write_in_full(1, content + 11, 41); + if (n < 41) + die_errno("git get-tar-commit-id: write error"); + + return 0; +} diff --git a/builtin/unpack-file.c b/builtin/unpack-file.c new file mode 100644 index 0000000000..19200291a2 --- /dev/null +++ b/builtin/unpack-file.c @@ -0,0 +1,36 @@ +#include "builtin.h" + +static char *create_temp_file(unsigned char *sha1) +{ + static char path[50]; + void *buf; + enum object_type type; + unsigned long size; + int fd; + + buf = read_sha1_file(sha1, &type, &size); + if (!buf || type != OBJ_BLOB) + die("unable to read blob object %s", sha1_to_hex(sha1)); + + strcpy(path, ".merge_file_XXXXXX"); + fd = xmkstemp(path); + if (write_in_full(fd, buf, size) != size) + die_errno("unable to write temp-file"); + close(fd); + return path; +} + +int cmd_unpack_file(int argc, const char **argv, const char *prefix) +{ + unsigned char sha1[20]; + + if (argc != 2 || !strcmp(argv[1], "-h")) + usage("git unpack-file <sha1>"); + if (get_sha1(argv[1], sha1)) + die("Not a valid object name %s", argv[1]); + + git_config(git_default_config, NULL); + + puts(create_temp_file(sha1)); + return 0; +} diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c new file mode 100644 index 0000000000..2217d7b3ae --- /dev/null +++ b/builtin/unpack-objects.c @@ -0,0 +1,568 @@ +#include "builtin.h" +#include "cache.h" +#include "object.h" +#include "delta.h" +#include "pack.h" +#include "blob.h" +#include "commit.h" +#include "tag.h" +#include "tree.h" +#include "tree-walk.h" +#include "progress.h" +#include "decorate.h" +#include "fsck.h" + +static int dry_run, quiet, recover, has_errors, strict; +static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict] < pack-file"; + +/* We always read in 4kB chunks. */ +static unsigned char buffer[4096]; +static unsigned int offset, len; +static off_t consumed_bytes; +static git_SHA_CTX ctx; + +/* + * When running under --strict mode, objects whose reachability are + * suspect are kept in core without getting written in the object + * store. + */ +struct obj_buffer { + char *buffer; + unsigned long size; +}; + +static struct decoration obj_decorate; + +static struct obj_buffer *lookup_object_buffer(struct object *base) +{ + return lookup_decoration(&obj_decorate, base); +} + +static void add_object_buffer(struct object *object, char *buffer, unsigned long size) +{ + struct obj_buffer *obj; + obj = xcalloc(1, sizeof(struct obj_buffer)); + obj->buffer = buffer; + obj->size = size; + if (add_decoration(&obj_decorate, object, obj)) + die("object %s tried to add buffer twice!", sha1_to_hex(object->sha1)); +} + +/* + * Make sure at least "min" bytes are available in the buffer, and + * return the pointer to the buffer. + */ +static void *fill(int min) +{ + if (min <= len) + return buffer + offset; + if (min > sizeof(buffer)) + die("cannot fill %d bytes", min); + if (offset) { + git_SHA1_Update(&ctx, buffer, offset); + memmove(buffer, buffer + offset, len); + offset = 0; + } + do { + ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len); + if (ret <= 0) { + if (!ret) + die("early EOF"); + die_errno("read error on input"); + } + len += ret; + } while (len < min); + return buffer; +} + +static void use(int bytes) +{ + if (bytes > len) + die("used more bytes than were available"); + len -= bytes; + offset += bytes; + + /* make sure off_t is sufficiently large not to wrap */ + if (signed_add_overflows(consumed_bytes, bytes)) + die("pack too large for current definition of off_t"); + consumed_bytes += bytes; +} + +static void *get_data(unsigned long size) +{ + git_zstream stream; + void *buf = xmalloc(size); + + memset(&stream, 0, sizeof(stream)); + + stream.next_out = buf; + stream.avail_out = size; + stream.next_in = fill(1); + stream.avail_in = len; + git_inflate_init(&stream); + + for (;;) { + int ret = git_inflate(&stream, 0); + use(len - stream.avail_in); + if (stream.total_out == size && ret == Z_STREAM_END) + break; + if (ret != Z_OK) { + error("inflate returned %d", ret); + free(buf); + buf = NULL; + if (!recover) + exit(1); + has_errors = 1; + break; + } + stream.next_in = fill(1); + stream.avail_in = len; + } + git_inflate_end(&stream); + return buf; +} + +struct delta_info { + unsigned char base_sha1[20]; + unsigned nr; + off_t base_offset; + unsigned long size; + void *delta; + struct delta_info *next; +}; + +static struct delta_info *delta_list; + +static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1, + off_t base_offset, + void *delta, unsigned long size) +{ + struct delta_info *info = xmalloc(sizeof(*info)); + + hashcpy(info->base_sha1, base_sha1); + info->base_offset = base_offset; + info->size = size; + info->delta = delta; + info->nr = nr; + info->next = delta_list; + delta_list = info; +} + +struct obj_info { + off_t offset; + unsigned char sha1[20]; + struct object *obj; +}; + +#define FLAG_OPEN (1u<<20) +#define FLAG_WRITTEN (1u<<21) + +static struct obj_info *obj_list; +static unsigned nr_objects; + +/* + * Called only from check_object() after it verified this object + * is Ok. + */ +static void write_cached_object(struct object *obj) +{ + unsigned char sha1[20]; + struct obj_buffer *obj_buf = lookup_object_buffer(obj); + if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), sha1) < 0) + die("failed to write object %s", sha1_to_hex(obj->sha1)); + obj->flags |= FLAG_WRITTEN; +} + +/* + * At the very end of the processing, write_rest() scans the objects + * that have reachability requirements and calls this function. + * Verify its reachability and validity recursively and write it out. + */ +static int check_object(struct object *obj, int type, void *data) +{ + if (!obj) + return 1; + + if (obj->flags & FLAG_WRITTEN) + return 0; + + if (type != OBJ_ANY && obj->type != type) + die("object type mismatch"); + + if (!(obj->flags & FLAG_OPEN)) { + unsigned long size; + int type = sha1_object_info(obj->sha1, &size); + if (type != obj->type || type <= 0) + die("object of unexpected type"); + obj->flags |= FLAG_WRITTEN; + return 0; + } + + if (fsck_object(obj, 1, fsck_error_function)) + die("Error in object"); + if (fsck_walk(obj, check_object, NULL)) + die("Error on reachable objects of %s", sha1_to_hex(obj->sha1)); + write_cached_object(obj); + return 0; +} + +static void write_rest(void) +{ + unsigned i; + for (i = 0; i < nr_objects; i++) { + if (obj_list[i].obj) + check_object(obj_list[i].obj, OBJ_ANY, NULL); + } +} + +static void added_object(unsigned nr, enum object_type type, + void *data, unsigned long size); + +/* + * Write out nr-th object from the list, now we know the contents + * of it. Under --strict, this buffers structured objects in-core, + * to be checked at the end. + */ +static void write_object(unsigned nr, enum object_type type, + void *buf, unsigned long size) +{ + if (!strict) { + if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0) + die("failed to write object"); + added_object(nr, type, buf, size); + free(buf); + obj_list[nr].obj = NULL; + } else if (type == OBJ_BLOB) { + struct blob *blob; + if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0) + die("failed to write object"); + added_object(nr, type, buf, size); + free(buf); + + blob = lookup_blob(obj_list[nr].sha1); + if (blob) + blob->object.flags |= FLAG_WRITTEN; + else + die("invalid blob object"); + obj_list[nr].obj = NULL; + } else { + struct object *obj; + int eaten; + hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1); + added_object(nr, type, buf, size); + obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten); + if (!obj) + die("invalid %s", typename(type)); + add_object_buffer(obj, buf, size); + obj->flags |= FLAG_OPEN; + obj_list[nr].obj = obj; + } +} + +static void resolve_delta(unsigned nr, enum object_type type, + void *base, unsigned long base_size, + void *delta, unsigned long delta_size) +{ + void *result; + unsigned long result_size; + + result = patch_delta(base, base_size, + delta, delta_size, + &result_size); + if (!result) + die("failed to apply delta"); + free(delta); + write_object(nr, type, result, result_size); +} + +/* + * We now know the contents of an object (which is nr-th in the pack); + * resolve all the deltified objects that are based on it. + */ +static void added_object(unsigned nr, enum object_type type, + void *data, unsigned long size) +{ + struct delta_info **p = &delta_list; + struct delta_info *info; + + while ((info = *p) != NULL) { + if (!hashcmp(info->base_sha1, obj_list[nr].sha1) || + info->base_offset == obj_list[nr].offset) { + *p = info->next; + p = &delta_list; + resolve_delta(info->nr, type, data, size, + info->delta, info->size); + free(info); + continue; + } + p = &info->next; + } +} + +static void unpack_non_delta_entry(enum object_type type, unsigned long size, + unsigned nr) +{ + void *buf = get_data(size); + + if (!dry_run && buf) + write_object(nr, type, buf, size); + else + free(buf); +} + +static int resolve_against_held(unsigned nr, const unsigned char *base, + void *delta_data, unsigned long delta_size) +{ + struct object *obj; + struct obj_buffer *obj_buffer; + obj = lookup_object(base); + if (!obj) + return 0; + obj_buffer = lookup_object_buffer(obj); + if (!obj_buffer) + return 0; + resolve_delta(nr, obj->type, obj_buffer->buffer, + obj_buffer->size, delta_data, delta_size); + return 1; +} + +static void unpack_delta_entry(enum object_type type, unsigned long delta_size, + unsigned nr) +{ + void *delta_data, *base; + unsigned long base_size; + unsigned char base_sha1[20]; + + if (type == OBJ_REF_DELTA) { + hashcpy(base_sha1, fill(20)); + use(20); + delta_data = get_data(delta_size); + if (dry_run || !delta_data) { + free(delta_data); + return; + } + if (has_sha1_file(base_sha1)) + ; /* Ok we have this one */ + else if (resolve_against_held(nr, base_sha1, + delta_data, delta_size)) + return; /* we are done */ + else { + /* cannot resolve yet --- queue it */ + hashcpy(obj_list[nr].sha1, null_sha1); + add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size); + return; + } + } else { + unsigned base_found = 0; + unsigned char *pack, c; + off_t base_offset; + unsigned lo, mid, hi; + + pack = fill(1); + c = *pack; + use(1); + base_offset = c & 127; + while (c & 128) { + base_offset += 1; + if (!base_offset || MSB(base_offset, 7)) + die("offset value overflow for delta base object"); + pack = fill(1); + c = *pack; + use(1); + base_offset = (base_offset << 7) + (c & 127); + } + base_offset = obj_list[nr].offset - base_offset; + if (base_offset <= 0 || base_offset >= obj_list[nr].offset) + die("offset value out of bound for delta base object"); + + delta_data = get_data(delta_size); + if (dry_run || !delta_data) { + free(delta_data); + return; + } + lo = 0; + hi = nr; + while (lo < hi) { + mid = (lo + hi)/2; + if (base_offset < obj_list[mid].offset) { + hi = mid; + } else if (base_offset > obj_list[mid].offset) { + lo = mid + 1; + } else { + hashcpy(base_sha1, obj_list[mid].sha1); + base_found = !is_null_sha1(base_sha1); + break; + } + } + if (!base_found) { + /* + * The delta base object is itself a delta that + * has not been resolved yet. + */ + hashcpy(obj_list[nr].sha1, null_sha1); + add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size); + return; + } + } + + if (resolve_against_held(nr, base_sha1, delta_data, delta_size)) + return; + + base = read_sha1_file(base_sha1, &type, &base_size); + if (!base) { + error("failed to read delta-pack base object %s", + sha1_to_hex(base_sha1)); + if (!recover) + exit(1); + has_errors = 1; + return; + } + resolve_delta(nr, type, base, base_size, delta_data, delta_size); + free(base); +} + +static void unpack_one(unsigned nr) +{ + unsigned shift; + unsigned char *pack; + unsigned long size, c; + enum object_type type; + + obj_list[nr].offset = consumed_bytes; + + pack = fill(1); + c = *pack; + use(1); + type = (c >> 4) & 7; + size = (c & 15); + shift = 4; + while (c & 0x80) { + pack = fill(1); + c = *pack; + use(1); + size += (c & 0x7f) << shift; + shift += 7; + } + + switch (type) { + case OBJ_COMMIT: + case OBJ_TREE: + case OBJ_BLOB: + case OBJ_TAG: + unpack_non_delta_entry(type, size, nr); + return; + case OBJ_REF_DELTA: + case OBJ_OFS_DELTA: + unpack_delta_entry(type, size, nr); + return; + default: + error("bad object type %d", type); + has_errors = 1; + if (recover) + return; + exit(1); + } +} + +static void unpack_all(void) +{ + int i; + struct progress *progress = NULL; + struct pack_header *hdr = fill(sizeof(struct pack_header)); + + nr_objects = ntohl(hdr->hdr_entries); + + if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE) + die("bad pack file"); + if (!pack_version_ok(hdr->hdr_version)) + die("unknown pack file version %"PRIu32, + ntohl(hdr->hdr_version)); + use(sizeof(struct pack_header)); + + if (!quiet) + progress = start_progress("Unpacking objects", nr_objects); + obj_list = xcalloc(nr_objects, sizeof(*obj_list)); + for (i = 0; i < nr_objects; i++) { + unpack_one(i); + display_progress(progress, i + 1); + } + stop_progress(&progress); + + if (delta_list) + die("unresolved deltas left after unpacking"); +} + +int cmd_unpack_objects(int argc, const char **argv, const char *prefix) +{ + int i; + unsigned char sha1[20]; + + read_replace_refs = 0; + + git_config(git_default_config, NULL); + + quiet = !isatty(2); + + for (i = 1 ; i < argc; i++) { + const char *arg = argv[i]; + + if (*arg == '-') { + if (!strcmp(arg, "-n")) { + dry_run = 1; + continue; + } + if (!strcmp(arg, "-q")) { + quiet = 1; + continue; + } + if (!strcmp(arg, "-r")) { + recover = 1; + continue; + } + if (!strcmp(arg, "--strict")) { + strict = 1; + continue; + } + if (!prefixcmp(arg, "--pack_header=")) { + struct pack_header *hdr; + char *c; + + hdr = (struct pack_header *)buffer; + hdr->hdr_signature = htonl(PACK_SIGNATURE); + hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); + if (*c != ',') + die("bad %s", arg); + hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); + if (*c) + die("bad %s", arg); + len = sizeof(*hdr); + continue; + } + usage(unpack_usage); + } + + /* We don't take any non-flag arguments now.. Maybe some day */ + usage(unpack_usage); + } + git_SHA1_Init(&ctx); + unpack_all(); + git_SHA1_Update(&ctx, buffer, offset); + git_SHA1_Final(sha1, &ctx); + if (strict) + write_rest(); + if (hashcmp(fill(20), sha1)) + die("final sha1 did not match"); + use(20); + + /* Write the last part of the buffer to stdout */ + while (len) { + int ret = xwrite(1, buffer + offset, len); + if (ret <= 0) + break; + len -= ret; + offset += ret; + } + + /* All done */ + return has_errors; +} diff --git a/builtin/update-index.c b/builtin/update-index.c new file mode 100644 index 0000000000..ada1dff846 --- /dev/null +++ b/builtin/update-index.c @@ -0,0 +1,904 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "cache.h" +#include "quote.h" +#include "cache-tree.h" +#include "tree-walk.h" +#include "builtin.h" +#include "refs.h" +#include "resolve-undo.h" +#include "parse-options.h" + +/* + * Default to not allowing changes to the list of files. The + * tool doesn't actually care, but this makes it harder to add + * files to the revision control by mistake by doing something + * like "git update-index *" and suddenly having all the object + * files be revision controlled. + */ +static int allow_add; +static int allow_remove; +static int allow_replace; +static int info_only; +static int force_remove; +static int verbose; +static int mark_valid_only; +static int mark_skip_worktree_only; +#define MARK_FLAG 1 +#define UNMARK_FLAG 2 + +__attribute__((format (printf, 1, 2))) +static void report(const char *fmt, ...) +{ + va_list vp; + + if (!verbose) + return; + + va_start(vp, fmt); + vprintf(fmt, vp); + putchar('\n'); + va_end(vp); +} + +static int mark_ce_flags(const char *path, int flag, int mark) +{ + int namelen = strlen(path); + int pos = cache_name_pos(path, namelen); + if (0 <= pos) { + if (mark) + active_cache[pos]->ce_flags |= flag; + else + active_cache[pos]->ce_flags &= ~flag; + cache_tree_invalidate_path(active_cache_tree, path); + active_cache_changed = 1; + return 0; + } + return -1; +} + +static int remove_one_path(const char *path) +{ + if (!allow_remove) + return error("%s: does not exist and --remove not passed", path); + if (remove_file_from_cache(path)) + return error("%s: cannot remove from the index", path); + return 0; +} + +/* + * Handle a path that couldn't be lstat'ed. It's either: + * - missing file (ENOENT or ENOTDIR). That's ok if we're + * supposed to be removing it and the removal actually + * succeeds. + * - permission error. That's never ok. + */ +static int process_lstat_error(const char *path, int err) +{ + if (err == ENOENT || err == ENOTDIR) + return remove_one_path(path); + return error("lstat(\"%s\"): %s", path, strerror(errno)); +} + +static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st) +{ + int option, size; + struct cache_entry *ce; + + /* Was the old index entry already up-to-date? */ + if (old && !ce_stage(old) && !ce_match_stat(old, st, 0)) + return 0; + + size = cache_entry_size(len); + ce = xcalloc(1, size); + memcpy(ce->name, path, len); + ce->ce_flags = create_ce_flags(0); + ce->ce_namelen = len; + fill_stat_cache_info(ce, st); + ce->ce_mode = ce_mode_from_stat(old, st->st_mode); + + if (index_path(ce->sha1, path, st, + info_only ? 0 : HASH_WRITE_OBJECT)) { + free(ce); + return -1; + } + option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; + option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; + if (add_cache_entry(ce, option)) + return error("%s: cannot add to the index - missing --add option?", path); + return 0; +} + +/* + * Handle a path that was a directory. Four cases: + * + * - it's already a gitlink in the index, and we keep it that + * way, and update it if we can (if we cannot find the HEAD, + * we're going to keep it unchanged in the index!) + * + * - it's a *file* in the index, in which case it should be + * removed as a file if removal is allowed, since it doesn't + * exist as such any more. If removal isn't allowed, it's + * an error. + * + * (NOTE! This is old and arguably fairly strange behaviour. + * We might want to make this an error unconditionally, and + * use "--force-remove" if you actually want to force removal). + * + * - it used to exist as a subdirectory (ie multiple files with + * this particular prefix) in the index, in which case it's wrong + * to try to update it as a directory. + * + * - it doesn't exist at all in the index, but it is a valid + * git directory, and it should be *added* as a gitlink. + */ +static int process_directory(const char *path, int len, struct stat *st) +{ + unsigned char sha1[20]; + int pos = cache_name_pos(path, len); + + /* Exact match: file or existing gitlink */ + if (pos >= 0) { + struct cache_entry *ce = active_cache[pos]; + if (S_ISGITLINK(ce->ce_mode)) { + + /* Do nothing to the index if there is no HEAD! */ + if (resolve_gitlink_ref(path, "HEAD", sha1) < 0) + return 0; + + return add_one_path(ce, path, len, st); + } + /* Should this be an unconditional error? */ + return remove_one_path(path); + } + + /* Inexact match: is there perhaps a subdirectory match? */ + pos = -pos-1; + while (pos < active_nr) { + struct cache_entry *ce = active_cache[pos++]; + + if (strncmp(ce->name, path, len)) + break; + if (ce->name[len] > '/') + break; + if (ce->name[len] < '/') + continue; + + /* Subdirectory match - error out */ + return error("%s: is a directory - add individual files instead", path); + } + + /* No match - should we add it as a gitlink? */ + if (!resolve_gitlink_ref(path, "HEAD", sha1)) + return add_one_path(NULL, path, len, st); + + /* Error out. */ + return error("%s: is a directory - add files inside instead", path); +} + +static int process_path(const char *path) +{ + int pos, len; + struct stat st; + struct cache_entry *ce; + + len = strlen(path); + if (has_symlink_leading_path(path, len)) + return error("'%s' is beyond a symbolic link", path); + + pos = cache_name_pos(path, len); + ce = pos < 0 ? NULL : active_cache[pos]; + if (ce && ce_skip_worktree(ce)) { + /* + * working directory version is assumed "good" + * so updating it does not make sense. + * On the other hand, removing it from index should work + */ + if (allow_remove && remove_file_from_cache(path)) + return error("%s: cannot remove from the index", path); + return 0; + } + + /* + * First things first: get the stat information, to decide + * what to do about the pathname! + */ + if (lstat(path, &st) < 0) + return process_lstat_error(path, errno); + + if (S_ISDIR(st.st_mode)) + return process_directory(path, len, &st); + + return add_one_path(ce, path, len, &st); +} + +static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, + const char *path, int stage) +{ + int size, len, option; + struct cache_entry *ce; + + if (!verify_path(path)) + return error("Invalid path '%s'", path); + + len = strlen(path); + size = cache_entry_size(len); + ce = xcalloc(1, size); + + hashcpy(ce->sha1, sha1); + memcpy(ce->name, path, len); + ce->ce_flags = create_ce_flags(stage); + ce->ce_namelen = len; + ce->ce_mode = create_ce_mode(mode); + if (assume_unchanged) + ce->ce_flags |= CE_VALID; + option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; + option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; + if (add_cache_entry(ce, option)) + return error("%s: cannot add to the index - missing --add option?", + path); + report("add '%s'", path); + return 0; +} + +static void chmod_path(int flip, const char *path) +{ + int pos; + struct cache_entry *ce; + unsigned int mode; + + pos = cache_name_pos(path, strlen(path)); + if (pos < 0) + goto fail; + ce = active_cache[pos]; + mode = ce->ce_mode; + if (!S_ISREG(mode)) + goto fail; + switch (flip) { + case '+': + ce->ce_mode |= 0111; break; + case '-': + ce->ce_mode &= ~0111; break; + default: + goto fail; + } + cache_tree_invalidate_path(active_cache_tree, path); + active_cache_changed = 1; + report("chmod %cx '%s'", flip, path); + return; + fail: + die("git update-index: cannot chmod %cx '%s'", flip, path); +} + +static void update_one(const char *path, const char *prefix, int prefix_length) +{ + const char *p = prefix_path(prefix, prefix_length, path); + if (!verify_path(p)) { + fprintf(stderr, "Ignoring path %s\n", path); + goto free_return; + } + if (mark_valid_only) { + if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG)) + die("Unable to mark file %s", path); + goto free_return; + } + if (mark_skip_worktree_only) { + if (mark_ce_flags(p, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG)) + die("Unable to mark file %s", path); + goto free_return; + } + + if (force_remove) { + if (remove_file_from_cache(p)) + die("git update-index: unable to remove %s", path); + report("remove '%s'", path); + goto free_return; + } + if (process_path(p)) + die("Unable to process path %s", path); + report("add '%s'", path); + free_return: + if (p < path || p > path + strlen(path)) + free((char *)p); +} + +static void read_index_info(int line_termination) +{ + struct strbuf buf = STRBUF_INIT; + struct strbuf uq = STRBUF_INIT; + + while (strbuf_getline(&buf, stdin, line_termination) != EOF) { + char *ptr, *tab; + char *path_name; + unsigned char sha1[20]; + unsigned int mode; + unsigned long ul; + int stage; + + /* This reads lines formatted in one of three formats: + * + * (1) mode SP sha1 TAB path + * The first format is what "git apply --index-info" + * reports, and used to reconstruct a partial tree + * that is used for phony merge base tree when falling + * back on 3-way merge. + * + * (2) mode SP type SP sha1 TAB path + * The second format is to stuff "git ls-tree" output + * into the index file. + * + * (3) mode SP sha1 SP stage TAB path + * This format is to put higher order stages into the + * index file and matches "git ls-files --stage" output. + */ + errno = 0; + ul = strtoul(buf.buf, &ptr, 8); + if (ptr == buf.buf || *ptr != ' ' + || errno || (unsigned int) ul != ul) + goto bad_line; + mode = ul; + + tab = strchr(ptr, '\t'); + if (!tab || tab - ptr < 41) + goto bad_line; + + if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') { + stage = tab[-1] - '0'; + ptr = tab + 1; /* point at the head of path */ + tab = tab - 2; /* point at tail of sha1 */ + } + else { + stage = 0; + ptr = tab + 1; /* point at the head of path */ + } + + if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ') + goto bad_line; + + path_name = ptr; + if (line_termination && path_name[0] == '"') { + strbuf_reset(&uq); + if (unquote_c_style(&uq, path_name, NULL)) { + die("git update-index: bad quoting of path name"); + } + path_name = uq.buf; + } + + if (!verify_path(path_name)) { + fprintf(stderr, "Ignoring path %s\n", path_name); + continue; + } + + if (!mode) { + /* mode == 0 means there is no such path -- remove */ + if (remove_file_from_cache(path_name)) + die("git update-index: unable to remove %s", + ptr); + } + else { + /* mode ' ' sha1 '\t' name + * ptr[-1] points at tab, + * ptr[-41] is at the beginning of sha1 + */ + ptr[-42] = ptr[-1] = 0; + if (add_cacheinfo(mode, sha1, path_name, stage)) + die("git update-index: unable to update %s", + path_name); + } + continue; + + bad_line: + die("malformed index info %s", buf.buf); + } + strbuf_release(&buf); + strbuf_release(&uq); +} + +static const char * const update_index_usage[] = { + N_("git update-index [options] [--] [<file>...]"), + NULL +}; + +static unsigned char head_sha1[20]; +static unsigned char merge_head_sha1[20]; + +static struct cache_entry *read_one_ent(const char *which, + unsigned char *ent, const char *path, + int namelen, int stage) +{ + unsigned mode; + unsigned char sha1[20]; + int size; + struct cache_entry *ce; + + if (get_tree_entry(ent, path, sha1, &mode)) { + if (which) + error("%s: not in %s branch.", path, which); + return NULL; + } + if (mode == S_IFDIR) { + if (which) + error("%s: not a blob in %s branch.", path, which); + return NULL; + } + size = cache_entry_size(namelen); + ce = xcalloc(1, size); + + hashcpy(ce->sha1, sha1); + memcpy(ce->name, path, namelen); + ce->ce_flags = create_ce_flags(stage); + ce->ce_namelen = namelen; + ce->ce_mode = create_ce_mode(mode); + return ce; +} + +static int unresolve_one(const char *path) +{ + int namelen = strlen(path); + int pos; + int ret = 0; + struct cache_entry *ce_2 = NULL, *ce_3 = NULL; + + /* See if there is such entry in the index. */ + pos = cache_name_pos(path, namelen); + if (0 <= pos) { + /* already merged */ + pos = unmerge_cache_entry_at(pos); + if (pos < active_nr) { + struct cache_entry *ce = active_cache[pos]; + if (ce_stage(ce) && + ce_namelen(ce) == namelen && + !memcmp(ce->name, path, namelen)) + return 0; + } + /* no resolve-undo information; fall back */ + } else { + /* If there isn't, either it is unmerged, or + * resolved as "removed" by mistake. We do not + * want to do anything in the former case. + */ + pos = -pos-1; + if (pos < active_nr) { + struct cache_entry *ce = active_cache[pos]; + if (ce_namelen(ce) == namelen && + !memcmp(ce->name, path, namelen)) { + fprintf(stderr, + "%s: skipping still unmerged path.\n", + path); + goto free_return; + } + } + } + + /* Grab blobs from given path from HEAD and MERGE_HEAD, + * stuff HEAD version in stage #2, + * stuff MERGE_HEAD version in stage #3. + */ + ce_2 = read_one_ent("our", head_sha1, path, namelen, 2); + ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3); + + if (!ce_2 || !ce_3) { + ret = -1; + goto free_return; + } + if (!hashcmp(ce_2->sha1, ce_3->sha1) && + ce_2->ce_mode == ce_3->ce_mode) { + fprintf(stderr, "%s: identical in both, skipping.\n", + path); + goto free_return; + } + + remove_file_from_cache(path); + if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) { + error("%s: cannot add our version to the index.", path); + ret = -1; + goto free_return; + } + if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD)) + return 0; + error("%s: cannot add their version to the index.", path); + ret = -1; + free_return: + free(ce_2); + free(ce_3); + return ret; +} + +static void read_head_pointers(void) +{ + if (read_ref("HEAD", head_sha1)) + die("No HEAD -- no initial commit yet?"); + if (read_ref("MERGE_HEAD", merge_head_sha1)) { + fprintf(stderr, "Not in the middle of a merge.\n"); + exit(0); + } +} + +static int do_unresolve(int ac, const char **av, + const char *prefix, int prefix_length) +{ + int i; + int err = 0; + + /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we + * are not doing a merge, so exit with success status. + */ + read_head_pointers(); + + for (i = 1; i < ac; i++) { + const char *arg = av[i]; + const char *p = prefix_path(prefix, prefix_length, arg); + err |= unresolve_one(p); + if (p < arg || p > arg + strlen(arg)) + free((char *)p); + } + return err; +} + +static int do_reupdate(int ac, const char **av, + const char *prefix, int prefix_length) +{ + /* Read HEAD and run update-index on paths that are + * merged and already different between index and HEAD. + */ + int pos; + int has_head = 1; + const char **paths = get_pathspec(prefix, av + 1); + struct pathspec pathspec; + + init_pathspec(&pathspec, paths); + + if (read_ref("HEAD", head_sha1)) + /* If there is no HEAD, that means it is an initial + * commit. Update everything in the index. + */ + has_head = 0; + redo: + for (pos = 0; pos < active_nr; pos++) { + struct cache_entry *ce = active_cache[pos]; + struct cache_entry *old = NULL; + int save_nr; + + if (ce_stage(ce) || !ce_path_match(ce, &pathspec)) + continue; + if (has_head) + old = read_one_ent(NULL, head_sha1, + ce->name, ce_namelen(ce), 0); + if (old && ce->ce_mode == old->ce_mode && + !hashcmp(ce->sha1, old->sha1)) { + free(old); + continue; /* unchanged */ + } + /* Be careful. The working tree may not have the + * path anymore, in which case, under 'allow_remove', + * or worse yet 'allow_replace', active_nr may decrease. + */ + save_nr = active_nr; + update_one(ce->name + prefix_length, prefix, prefix_length); + if (save_nr != active_nr) + goto redo; + } + free_pathspec(&pathspec); + return 0; +} + +struct refresh_params { + unsigned int flags; + int *has_errors; +}; + +static int refresh(struct refresh_params *o, unsigned int flag) +{ + setup_work_tree(); + read_cache_preload(NULL); + *o->has_errors |= refresh_cache(o->flags | flag); + return 0; +} + +static int refresh_callback(const struct option *opt, + const char *arg, int unset) +{ + return refresh(opt->value, 0); +} + +static int really_refresh_callback(const struct option *opt, + const char *arg, int unset) +{ + return refresh(opt->value, REFRESH_REALLY); +} + +static int chmod_callback(const struct option *opt, + const char *arg, int unset) +{ + char *flip = opt->value; + if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2]) + return error("option 'chmod' expects \"+x\" or \"-x\""); + *flip = arg[0]; + return 0; +} + +static int resolve_undo_clear_callback(const struct option *opt, + const char *arg, int unset) +{ + resolve_undo_clear(); + return 0; +} + +static int cacheinfo_callback(struct parse_opt_ctx_t *ctx, + const struct option *opt, int unset) +{ + unsigned char sha1[20]; + unsigned int mode; + + if (ctx->argc <= 3) + return error("option 'cacheinfo' expects three arguments"); + if (strtoul_ui(*++ctx->argv, 8, &mode) || + get_sha1_hex(*++ctx->argv, sha1) || + add_cacheinfo(mode, sha1, *++ctx->argv, 0)) + die("git update-index: --cacheinfo cannot add %s", *ctx->argv); + ctx->argc -= 3; + return 0; +} + +static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx, + const struct option *opt, int unset) +{ + int *line_termination = opt->value; + + if (ctx->argc != 1) + return error("option '%s' must be the last argument", opt->long_name); + allow_add = allow_replace = allow_remove = 1; + read_index_info(*line_termination); + return 0; +} + +static int stdin_callback(struct parse_opt_ctx_t *ctx, + const struct option *opt, int unset) +{ + int *read_from_stdin = opt->value; + + if (ctx->argc != 1) + return error("option '%s' must be the last argument", opt->long_name); + *read_from_stdin = 1; + return 0; +} + +static int unresolve_callback(struct parse_opt_ctx_t *ctx, + const struct option *opt, int flags) +{ + int *has_errors = opt->value; + const char *prefix = startup_info->prefix; + + /* consume remaining arguments. */ + *has_errors = do_unresolve(ctx->argc, ctx->argv, + prefix, prefix ? strlen(prefix) : 0); + if (*has_errors) + active_cache_changed = 0; + + ctx->argv += ctx->argc - 1; + ctx->argc = 1; + return 0; +} + +static int reupdate_callback(struct parse_opt_ctx_t *ctx, + const struct option *opt, int flags) +{ + int *has_errors = opt->value; + const char *prefix = startup_info->prefix; + + /* consume remaining arguments. */ + setup_work_tree(); + *has_errors = do_reupdate(ctx->argc, ctx->argv, + prefix, prefix ? strlen(prefix) : 0); + if (*has_errors) + active_cache_changed = 0; + + ctx->argv += ctx->argc - 1; + ctx->argc = 1; + return 0; +} + +int cmd_update_index(int argc, const char **argv, const char *prefix) +{ + int newfd, entries, has_errors = 0, line_termination = '\n'; + int read_from_stdin = 0; + int prefix_length = prefix ? strlen(prefix) : 0; + int preferred_index_format = 0; + char set_executable_bit = 0; + struct refresh_params refresh_args = {0, &has_errors}; + int lock_error = 0; + struct lock_file *lock_file; + struct parse_opt_ctx_t ctx; + int parseopt_state = PARSE_OPT_UNKNOWN; + struct option options[] = { + OPT_BIT('q', NULL, &refresh_args.flags, + N_("continue refresh even when index needs update"), + REFRESH_QUIET), + OPT_BIT(0, "ignore-submodules", &refresh_args.flags, + N_("refresh: ignore submodules"), + REFRESH_IGNORE_SUBMODULES), + OPT_SET_INT(0, "add", &allow_add, + N_("do not ignore new files"), 1), + OPT_SET_INT(0, "replace", &allow_replace, + N_("let files replace directories and vice-versa"), 1), + OPT_SET_INT(0, "remove", &allow_remove, + N_("notice files missing from worktree"), 1), + OPT_BIT(0, "unmerged", &refresh_args.flags, + N_("refresh even if index contains unmerged entries"), + REFRESH_UNMERGED), + {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL, + N_("refresh stat information"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, + refresh_callback}, + {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL, + N_("like --refresh, but ignore assume-unchanged setting"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, + really_refresh_callback}, + {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL, + N_("<mode> <object> <path>"), + N_("add the specified entry to the index"), + PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */ + PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, + (parse_opt_cb *) cacheinfo_callback}, + {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"), + N_("override the executable bit of the listed files"), + PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, + chmod_callback}, + {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL, + N_("mark files as \"not changing\""), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG}, + {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL, + N_("clear assumed-unchanged bit"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG}, + {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL, + N_("mark files as \"index-only\""), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG}, + {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL, + N_("clear skip-worktree bit"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG}, + OPT_SET_INT(0, "info-only", &info_only, + N_("add to index only; do not add content to object database"), 1), + OPT_SET_INT(0, "force-remove", &force_remove, + N_("remove named paths even if present in worktree"), 1), + OPT_SET_INT('z', NULL, &line_termination, + N_("with --stdin: input lines are terminated by null bytes"), '\0'), + {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL, + N_("read list of paths to be updated from standard input"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG, + (parse_opt_cb *) stdin_callback}, + {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL, + N_("add entries from standard input to the index"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG, + (parse_opt_cb *) stdin_cacheinfo_callback}, + {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL, + N_("repopulate stages #2 and #3 for the listed paths"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG, + (parse_opt_cb *) unresolve_callback}, + {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL, + N_("only update entries that differ from HEAD"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG, + (parse_opt_cb *) reupdate_callback}, + OPT_BIT(0, "ignore-missing", &refresh_args.flags, + N_("ignore files missing from worktree"), + REFRESH_IGNORE_MISSING), + OPT_SET_INT(0, "verbose", &verbose, + N_("report actions to standard output"), 1), + {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL, + N_("(for porcelains) forget saved unresolved conflicts"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, + resolve_undo_clear_callback}, + OPT_INTEGER(0, "index-version", &preferred_index_format, + N_("write index in this format")), + OPT_END() + }; + + if (argc == 2 && !strcmp(argv[1], "-h")) + usage(update_index_usage[0]); + + git_config(git_default_config, NULL); + + /* We can't free this memory, it becomes part of a linked list parsed atexit() */ + lock_file = xcalloc(1, sizeof(struct lock_file)); + + newfd = hold_locked_index(lock_file, 0); + if (newfd < 0) + lock_error = errno; + + entries = read_cache(); + if (entries < 0) + die("cache corrupted"); + + /* + * Custom copy of parse_options() because we want to handle + * filename arguments as they come. + */ + parse_options_start(&ctx, argc, argv, prefix, + options, PARSE_OPT_STOP_AT_NON_OPTION); + while (ctx.argc) { + if (parseopt_state != PARSE_OPT_DONE) + parseopt_state = parse_options_step(&ctx, options, + update_index_usage); + if (!ctx.argc) + break; + switch (parseopt_state) { + case PARSE_OPT_HELP: + exit(129); + case PARSE_OPT_NON_OPTION: + case PARSE_OPT_DONE: + { + const char *path = ctx.argv[0]; + const char *p; + + setup_work_tree(); + p = prefix_path(prefix, prefix_length, path); + update_one(p, NULL, 0); + if (set_executable_bit) + chmod_path(set_executable_bit, p); + if (p < path || p > path + strlen(path)) + free((char *)p); + ctx.argc--; + ctx.argv++; + break; + } + case PARSE_OPT_UNKNOWN: + if (ctx.argv[0][1] == '-') + error("unknown option '%s'", ctx.argv[0] + 2); + else + error("unknown switch '%c'", *ctx.opt); + usage_with_options(update_index_usage, options); + } + } + argc = parse_options_end(&ctx); + if (preferred_index_format) { + if (preferred_index_format < INDEX_FORMAT_LB || + INDEX_FORMAT_UB < preferred_index_format) + die("index-version %d not in range: %d..%d", + preferred_index_format, + INDEX_FORMAT_LB, INDEX_FORMAT_UB); + + if (the_index.version != preferred_index_format) + active_cache_changed = 1; + the_index.version = preferred_index_format; + } + + if (read_from_stdin) { + struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; + + setup_work_tree(); + while (strbuf_getline(&buf, stdin, line_termination) != EOF) { + const char *p; + if (line_termination && buf.buf[0] == '"') { + strbuf_reset(&nbuf); + if (unquote_c_style(&nbuf, buf.buf, NULL)) + die("line is badly quoted"); + strbuf_swap(&buf, &nbuf); + } + p = prefix_path(prefix, prefix_length, buf.buf); + update_one(p, NULL, 0); + if (set_executable_bit) + chmod_path(set_executable_bit, p); + if (p < buf.buf || p > buf.buf + buf.len) + free((char *)p); + } + strbuf_release(&nbuf); + strbuf_release(&buf); + } + + if (active_cache_changed) { + if (newfd < 0) { + if (refresh_args.flags & REFRESH_QUIET) + exit(128); + unable_to_lock_index_die(get_index_file(), lock_error); + } + if (write_cache(newfd, active_cache, active_nr) || + commit_locked_index(lock_file)) + die("Unable to write new index file"); + } + + rollback_lock_file(lock_file); + + return has_errors ? 1 : 0; +} diff --git a/builtin/update-ref.c b/builtin/update-ref.c new file mode 100644 index 0000000000..51d2684859 --- /dev/null +++ b/builtin/update-ref.c @@ -0,0 +1,58 @@ +#include "cache.h" +#include "refs.h" +#include "builtin.h" +#include "parse-options.h" + +static const char * const git_update_ref_usage[] = { + N_("git update-ref [options] -d <refname> [<oldval>]"), + N_("git update-ref [options] <refname> <newval> [<oldval>]"), + NULL +}; + +int cmd_update_ref(int argc, const char **argv, const char *prefix) +{ + const char *refname, *oldval, *msg = NULL; + unsigned char sha1[20], oldsha1[20]; + int delete = 0, no_deref = 0, flags = 0; + struct option options[] = { + OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")), + OPT_BOOLEAN('d', NULL, &delete, N_("delete the reference")), + OPT_BOOLEAN( 0 , "no-deref", &no_deref, + N_("update <refname> not the one it points to")), + OPT_END(), + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, git_update_ref_usage, + 0); + if (msg && !*msg) + die("Refusing to perform update with empty message."); + + if (delete) { + if (argc < 1 || argc > 2) + usage_with_options(git_update_ref_usage, options); + refname = argv[0]; + oldval = argv[1]; + } else { + const char *value; + if (argc < 2 || argc > 3) + usage_with_options(git_update_ref_usage, options); + refname = argv[0]; + value = argv[1]; + oldval = argv[2]; + if (get_sha1(value, sha1)) + die("%s: not a valid SHA1", value); + } + + hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */ + if (oldval && *oldval && get_sha1(oldval, oldsha1)) + die("%s: not a valid old SHA1", oldval); + + if (no_deref) + flags = REF_NODEREF; + if (delete) + return delete_ref(refname, oldval ? oldsha1 : NULL, flags); + else + return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL, + flags, DIE_ON_ERR); +} diff --git a/builtin/update-server-info.c b/builtin/update-server-info.c new file mode 100644 index 0000000000..6c8cc3edc1 --- /dev/null +++ b/builtin/update-server-info.c @@ -0,0 +1,25 @@ +#include "cache.h" +#include "builtin.h" +#include "parse-options.h" + +static const char * const update_server_info_usage[] = { + N_("git update-server-info [--force]"), + NULL +}; + +int cmd_update_server_info(int argc, const char **argv, const char *prefix) +{ + int force = 0; + struct option options[] = { + OPT__FORCE(&force, N_("update the info files from scratch")), + OPT_END() + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, options, + update_server_info_usage, 0); + if (argc > 0) + usage_with_options(update_server_info_usage, options); + + return !!update_server_info(force); +} diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c new file mode 100644 index 0000000000..b928beb8ed --- /dev/null +++ b/builtin/upload-archive.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2006 Franck Bui-Huu + */ +#include "cache.h" +#include "builtin.h" +#include "archive.h" +#include "pkt-line.h" +#include "sideband.h" +#include "run-command.h" + +static const char upload_archive_usage[] = + "git upload-archive <repo>"; + +static const char deadchild[] = +"git upload-archive: archiver died with error"; + +#define MAX_ARGS (64) + +int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix) +{ + const char *sent_argv[MAX_ARGS]; + const char *arg_cmd = "argument "; + char *p, buf[4096]; + int sent_argc; + int len; + + if (argc != 2) + usage(upload_archive_usage); + + if (strlen(argv[1]) + 1 > sizeof(buf)) + die("insanely long repository name"); + + strcpy(buf, argv[1]); /* enter-repo smudges its argument */ + + if (!enter_repo(buf, 0)) + die("'%s' does not appear to be a git repository", buf); + + /* put received options in sent_argv[] */ + sent_argc = 1; + sent_argv[0] = "git-upload-archive"; + for (p = buf;;) { + /* This will die if not enough free space in buf */ + len = packet_read_line(0, p, (buf + sizeof buf) - p); + if (len == 0) + break; /* got a flush */ + if (sent_argc > MAX_ARGS - 2) + die("Too many options (>%d)", MAX_ARGS - 2); + + if (p[len-1] == '\n') { + p[--len] = 0; + } + if (len < strlen(arg_cmd) || + strncmp(arg_cmd, p, strlen(arg_cmd))) + die("'argument' token or flush expected"); + + len -= strlen(arg_cmd); + memmove(p, p + strlen(arg_cmd), len); + sent_argv[sent_argc++] = p; + p += len; + *p++ = 0; + } + sent_argv[sent_argc] = NULL; + + /* parse all options sent by the client */ + return write_archive(sent_argc, sent_argv, prefix, 0, NULL, 1); +} + +__attribute__((format (printf, 1, 2))) +static void error_clnt(const char *fmt, ...) +{ + char buf[1024]; + va_list params; + int len; + + va_start(params, fmt); + len = vsprintf(buf, fmt, params); + va_end(params); + send_sideband(1, 3, buf, len, LARGE_PACKET_MAX); + die("sent error to the client: %s", buf); +} + +static ssize_t process_input(int child_fd, int band) +{ + char buf[16384]; + ssize_t sz = read(child_fd, buf, sizeof(buf)); + if (sz < 0) { + if (errno != EAGAIN && errno != EINTR) + error_clnt("read error: %s\n", strerror(errno)); + return sz; + } + send_sideband(1, band, buf, sz, LARGE_PACKET_MAX); + return sz; +} + +int cmd_upload_archive(int argc, const char **argv, const char *prefix) +{ + struct child_process writer = { argv }; + + /* + * Set up sideband subprocess. + * + * We (parent) monitor and read from child, sending its fd#1 and fd#2 + * multiplexed out to our fd#1. If the child dies, we tell the other + * end over channel #3. + */ + argv[0] = "upload-archive--writer"; + writer.out = writer.err = -1; + writer.git_cmd = 1; + if (start_command(&writer)) { + int err = errno; + packet_write(1, "NACK unable to spawn subprocess\n"); + die("upload-archive: %s", strerror(err)); + } + + packet_write(1, "ACK\n"); + packet_flush(1); + + while (1) { + struct pollfd pfd[2]; + + pfd[0].fd = writer.out; + pfd[0].events = POLLIN; + pfd[1].fd = writer.err; + pfd[1].events = POLLIN; + if (poll(pfd, 2, -1) < 0) { + if (errno != EINTR) { + error("poll failed resuming: %s", + strerror(errno)); + sleep(1); + } + continue; + } + if (pfd[1].revents & POLLIN) + /* Status stream ready */ + if (process_input(pfd[1].fd, 2)) + continue; + if (pfd[0].revents & POLLIN) + /* Data stream ready */ + if (process_input(pfd[0].fd, 1)) + continue; + + if (finish_command(&writer)) + error_clnt("%s", deadchild); + packet_flush(1); + break; + } + return 0; +} diff --git a/builtin/var.c b/builtin/var.c new file mode 100644 index 0000000000..aedbb53a2d --- /dev/null +++ b/builtin/var.c @@ -0,0 +1,93 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Eric Biederman, 2005 + */ +#include "builtin.h" + +static const char var_usage[] = "git var (-l | <variable>)"; + +static const char *editor(int flag) +{ + const char *pgm = git_editor(); + + if (!pgm && flag & IDENT_STRICT) + die("Terminal is dumb, but EDITOR unset"); + + return pgm; +} + +static const char *pager(int flag) +{ + const char *pgm = git_pager(1); + + if (!pgm) + pgm = "cat"; + return pgm; +} + +struct git_var { + const char *name; + const char *(*read)(int); +}; +static struct git_var git_vars[] = { + { "GIT_COMMITTER_IDENT", git_committer_info }, + { "GIT_AUTHOR_IDENT", git_author_info }, + { "GIT_EDITOR", editor }, + { "GIT_PAGER", pager }, + { "", NULL }, +}; + +static void list_vars(void) +{ + struct git_var *ptr; + const char *val; + + for (ptr = git_vars; ptr->read; ptr++) + if ((val = ptr->read(0))) + printf("%s=%s\n", ptr->name, val); +} + +static const char *read_var(const char *var) +{ + struct git_var *ptr; + const char *val; + val = NULL; + for (ptr = git_vars; ptr->read; ptr++) { + if (strcmp(var, ptr->name) == 0) { + val = ptr->read(IDENT_STRICT); + break; + } + } + return val; +} + +static int show_config(const char *var, const char *value, void *cb) +{ + if (value) + printf("%s=%s\n", var, value); + else + printf("%s\n", var); + return git_default_config(var, value, cb); +} + +int cmd_var(int argc, const char **argv, const char *prefix) +{ + const char *val = NULL; + if (argc != 2) + usage(var_usage); + + if (strcmp(argv[1], "-l") == 0) { + git_config(show_config, NULL); + list_vars(); + return 0; + } + git_config(git_default_config, NULL); + val = read_var(argv[1]); + if (!val) + usage(var_usage); + + printf("%s\n", val); + + return 0; +} diff --git a/builtin/verify-pack.c b/builtin/verify-pack.c new file mode 100644 index 0000000000..66cd2df0f8 --- /dev/null +++ b/builtin/verify-pack.c @@ -0,0 +1,84 @@ +#include "builtin.h" +#include "cache.h" +#include "run-command.h" +#include "parse-options.h" + +#define VERIFY_PACK_VERBOSE 01 +#define VERIFY_PACK_STAT_ONLY 02 + +static int verify_one_pack(const char *path, unsigned int flags) +{ + struct child_process index_pack; + const char *argv[] = {"index-pack", NULL, NULL, NULL }; + struct strbuf arg = STRBUF_INIT; + int verbose = flags & VERIFY_PACK_VERBOSE; + int stat_only = flags & VERIFY_PACK_STAT_ONLY; + int err; + + if (stat_only) + argv[1] = "--verify-stat-only"; + else if (verbose) + argv[1] = "--verify-stat"; + else + argv[1] = "--verify"; + + /* + * In addition to "foo.pack" we accept "foo.idx" and "foo"; + * normalize these forms to "foo.pack" for "index-pack --verify". + */ + strbuf_addstr(&arg, path); + if (has_extension(arg.buf, ".idx")) + strbuf_splice(&arg, arg.len - 3, 3, "pack", 4); + else if (!has_extension(arg.buf, ".pack")) + strbuf_add(&arg, ".pack", 5); + argv[2] = arg.buf; + + memset(&index_pack, 0, sizeof(index_pack)); + index_pack.argv = argv; + index_pack.git_cmd = 1; + + err = run_command(&index_pack); + + if (verbose || stat_only) { + if (err) + printf("%s: bad\n", arg.buf); + else { + if (!stat_only) + printf("%s: ok\n", arg.buf); + } + } + strbuf_release(&arg); + + return err; +} + +static const char * const verify_pack_usage[] = { + N_("git verify-pack [-v|--verbose] [-s|--stat-only] <pack>..."), + NULL +}; + +int cmd_verify_pack(int argc, const char **argv, const char *prefix) +{ + int err = 0; + unsigned int flags = 0; + int i; + const struct option verify_pack_options[] = { + OPT_BIT('v', "verbose", &flags, N_("verbose"), + VERIFY_PACK_VERBOSE), + OPT_BIT('s', "stat-only", &flags, N_("show statistics only"), + VERIFY_PACK_STAT_ONLY), + OPT_END() + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, verify_pack_options, + verify_pack_usage, 0); + if (argc < 1) + usage_with_options(verify_pack_usage, verify_pack_options); + for (i = 0; i < argc; i++) { + if (verify_one_pack(argv[i], flags)) + err = 1; + } + + return err; +} diff --git a/builtin/verify-tag.c b/builtin/verify-tag.c new file mode 100644 index 0000000000..a8eee886a5 --- /dev/null +++ b/builtin/verify-tag.c @@ -0,0 +1,91 @@ +/* + * Builtin "git verify-tag" + * + * Copyright (c) 2007 Carlos Rica <jasampler@gmail.com> + * + * Based on git-verify-tag.sh + */ +#include "cache.h" +#include "builtin.h" +#include "tag.h" +#include "run-command.h" +#include <signal.h> +#include "parse-options.h" +#include "gpg-interface.h" + +static const char * const verify_tag_usage[] = { + N_("git verify-tag [-v|--verbose] <tag>..."), + NULL +}; + +static int run_gpg_verify(const char *buf, unsigned long size, int verbose) +{ + int len; + + len = parse_signature(buf, size); + if (verbose) + write_in_full(1, buf, len); + + if (size == len) + return error("no signature found"); + + return verify_signed_buffer(buf, len, buf + len, size - len, NULL); +} + +static int verify_tag(const char *name, int verbose) +{ + enum object_type type; + unsigned char sha1[20]; + char *buf; + unsigned long size; + int ret; + + if (get_sha1(name, sha1)) + return error("tag '%s' not found.", name); + + type = sha1_object_info(sha1, NULL); + if (type != OBJ_TAG) + return error("%s: cannot verify a non-tag object of type %s.", + name, typename(type)); + + buf = read_sha1_file(sha1, &type, &size); + if (!buf) + return error("%s: unable to read file.", name); + + ret = run_gpg_verify(buf, size, verbose); + + free(buf); + return ret; +} + +static int git_verify_tag_config(const char *var, const char *value, void *cb) +{ + int status = git_gpg_config(var, value, cb); + if (status) + return status; + return git_default_config(var, value, cb); +} + +int cmd_verify_tag(int argc, const char **argv, const char *prefix) +{ + int i = 1, verbose = 0, had_error = 0; + const struct option verify_tag_options[] = { + OPT__VERBOSE(&verbose, N_("print tag contents")), + OPT_END() + }; + + git_config(git_verify_tag_config, NULL); + + argc = parse_options(argc, argv, prefix, verify_tag_options, + verify_tag_usage, PARSE_OPT_KEEP_ARGV0); + if (argc <= i) + usage_with_options(verify_tag_usage, verify_tag_options); + + /* sometimes the program was terminated because this signal + * was received in the process of writing the gpg input: */ + signal(SIGPIPE, SIG_IGN); + while (i < argc) + if (verify_tag(argv[i++], verbose)) + had_error = 1; + return had_error; +} diff --git a/builtin/write-tree.c b/builtin/write-tree.c new file mode 100644 index 0000000000..084c0df783 --- /dev/null +++ b/builtin/write-tree.c @@ -0,0 +1,56 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ +#include "builtin.h" +#include "cache.h" +#include "tree.h" +#include "cache-tree.h" +#include "parse-options.h" + +static const char * const write_tree_usage[] = { + N_("git write-tree [--missing-ok] [--prefix=<prefix>/]"), + NULL +}; + +int cmd_write_tree(int argc, const char **argv, const char *unused_prefix) +{ + int flags = 0, ret; + const char *prefix = NULL; + unsigned char sha1[20]; + const char *me = "git-write-tree"; + struct option write_tree_options[] = { + OPT_BIT(0, "missing-ok", &flags, N_("allow missing objects"), + WRITE_TREE_MISSING_OK), + { OPTION_STRING, 0, "prefix", &prefix, N_("<prefix>/"), + N_("write tree object for a subdirectory <prefix>") , + PARSE_OPT_LITERAL_ARGHELP }, + { OPTION_BIT, 0, "ignore-cache-tree", &flags, NULL, + N_("only useful for debugging"), + PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, NULL, + WRITE_TREE_IGNORE_CACHE_TREE }, + OPT_END() + }; + + git_config(git_default_config, NULL); + argc = parse_options(argc, argv, unused_prefix, write_tree_options, + write_tree_usage, 0); + + ret = write_cache_as_tree(sha1, flags, prefix); + switch (ret) { + case 0: + printf("%s\n", sha1_to_hex(sha1)); + break; + case WRITE_TREE_UNREADABLE_INDEX: + die("%s: error reading the index", me); + break; + case WRITE_TREE_UNMERGED_INDEX: + die("%s: error building trees", me); + break; + case WRITE_TREE_PREFIX_ERROR: + die("%s: prefix %s not found", me, prefix); + break; + } + return ret; +} |