Code

Merge branch 'maint'
authorShawn O. Pearce <spearce@spearce.org>
Sun, 12 Oct 2008 20:21:17 +0000 (13:21 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Sun, 12 Oct 2008 20:21:17 +0000 (13:21 -0700)
* maint:
  test-lib: fix broken printf
  git apply --directory broken for new files

1  2 
builtin-apply.c
t/test-lib.sh

diff --combined builtin-apply.c
index f1f675819f22ec1b47fb7b25ed050dc4ddcad378,e9d49f133afd559d2df3f09997c08a61dfc2ff86..cfd8fceb9033cf5c4c7924d96437b94a9c4fe226
@@@ -321,12 -321,13 +321,12 @@@ static char *find_name(const char *line
        const char *start = line;
  
        if (*line == '"') {
 -              struct strbuf name;
 +              struct strbuf name = STRBUF_INIT;
  
                /*
                 * Proposed "new-style" GNU patch/diff format; see
                 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
                 */
 -              strbuf_init(&name, 0);
                if (!unquote_c_style(&name, line, NULL)) {
                        char *cp;
  
@@@ -674,8 -675,11 +674,8 @@@ static char *git_header_name(char *line
  
        if (*line == '"') {
                const char *cp;
 -              struct strbuf first;
 -              struct strbuf sp;
 -
 -              strbuf_init(&first, 0);
 -              strbuf_init(&sp, 0);
 +              struct strbuf first = STRBUF_INIT;
 +              struct strbuf sp = STRBUF_INIT;
  
                if (unquote_c_style(&first, line, &second))
                        goto free_and_fail1;
         */
        for (second = name; second < line + llen; second++) {
                if (*second == '"') {
 -                      struct strbuf sp;
 +                      struct strbuf sp = STRBUF_INIT;
                        const char *np;
  
 -                      strbuf_init(&sp, 0);
                        if (unquote_c_style(&sp, second, NULL))
                                goto free_and_fail2;
  
@@@ -805,6 -810,13 +805,13 @@@ static int parse_git_header(char *line
         * 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;
@@@ -1503,10 -1515,11 +1510,10 @@@ static const char minuses[]
  
  static void show_stats(struct patch *patch)
  {
 -      struct strbuf qname;
 +      struct strbuf qname = STRBUF_INIT;
        char *cp = patch->new_name ? patch->new_name : patch->old_name;
        int max, add, del;
  
 -      strbuf_init(&qname, 0);
        quote_c_style(cp, &qname, NULL, 0);
  
        /*
@@@ -2286,12 -2299,14 +2293,12 @@@ static void add_to_fn_table(struct patc
  
  static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
  {
 -      struct strbuf buf;
 +      struct strbuf buf = STRBUF_INIT;
        struct image image;
        size_t len;
        char *img;
        struct patch *tpatch;
  
 -      strbuf_init(&buf, 0);
 -
        if (!(patch->is_copy || patch->is_rename) &&
            ((tpatch = in_fn_table(patch->old_name)) != NULL)) {
                if (tpatch == (struct patch *) -1) {
@@@ -2771,7 -2786,7 +2778,7 @@@ static void add_index_file(const char *
  static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
  {
        int fd;
 -      struct strbuf nbuf;
 +      struct strbuf nbuf = STRBUF_INIT;
  
        if (S_ISGITLINK(mode)) {
                struct stat st;
        if (fd < 0)
                return -1;
  
 -      strbuf_init(&nbuf, 0);
        if (convert_to_working_tree(path, buf, size, &nbuf)) {
                size = nbuf.len;
                buf  = nbuf.buf;
@@@ -2980,45 -2996,29 +2987,45 @@@ static int write_out_results(struct pat
  
  static struct lock_file lock_file;
  
 -static struct excludes {
 -      struct excludes *next;
 -      const char *path;
 -} *excludes;
 +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(name, &limit_by_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;
 -      struct excludes *x = excludes;
 -      while (x) {
 -              if (fnmatch(x->path, pathname, 0) == 0)
 -                      return 0;
 -              x = x->next;
 -      }
 +      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;
        }
 -      return 1;
 +
 +      /* 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;
@@@ -3051,12 -3051,13 +3058,12 @@@ static void prefix_patches(struct patc
  static int apply_patch(int fd, const char *filename, int options)
  {
        size_t offset;
 -      struct strbuf buf;
 +      struct strbuf buf = STRBUF_INIT;
        struct patch *list = NULL, **listp = &list;
        int skipped_patch = 0;
  
        /* FIXME - memory leak when using multiple patch files as inputs */
        memset(&fn_table, 0, sizeof(struct string_list));
 -      strbuf_init(&buf, 0);
        patch_input_file = filename;
        read_patch_file(&buf, fd);
        offset = 0;
@@@ -3158,12 -3159,10 +3165,12 @@@ int cmd_apply(int argc, const char **ar
                        continue;
                }
                if (!prefixcmp(arg, "--exclude=")) {
 -                      struct excludes *x = xmalloc(sizeof(*x));
 -                      x->path = arg + 10;
 -                      x->next = excludes;
 -                      excludes = x;
 +                      add_name_limit(arg + 10, 1);
 +                      continue;
 +              }
 +              if (!prefixcmp(arg, "--include=")) {
 +                      add_name_limit(arg + 10, 0);
 +                      has_include = 1;
                        continue;
                }
                if (!prefixcmp(arg, "-p")) {
diff --combined t/test-lib.sh
index fb8974112598fa46f0eefb8d84be9bcf9a1eee09,689ac2f4b4eb45ee5f45f74e4c5d13c3e7c17e84..8936173ee204c589fb299837586b4866722b2388
@@@ -112,7 -112,7 +112,7 @@@ if test -n "$color"; the
                        *) test -n "$quiet" && return;;
                esac
                shift
-               printf "* $*"
+               printf "* %s" "$*"
                tput sgr0
                echo
                )
@@@ -407,7 -407,7 +407,7 @@@ test_create_repo () 
        error "bug in the test script: not 1 parameter to test-create-repo"
        owd=`pwd`
        repo="$1"
 -      mkdir "$repo"
 +      mkdir -p "$repo"
        cd "$repo" || error "Cannot setup test environment"
        "$GIT_EXEC_PATH/git" init "--template=$GIT_EXEC_PATH/templates/blt/" >&3 2>&4 ||
        error "cannot run git init -- have you built things yet?"
@@@ -450,11 -450,6 +450,11 @@@ test_done () 
                # we will leave things as they are.
  
                say_color pass "passed all $msg"
 +
 +              test -d "$remove_trash" &&
 +              cd "$(dirname "$remove_trash")" &&
 +              rm -rf "$(basename "$remove_trash")"
 +
                exit 0 ;;
  
        *)
@@@ -491,8 -486,7 +491,8 @@@ f
  . ../GIT-BUILD-OPTIONS
  
  # Test repository
 -test="trash directory"
 +test="trash directory.$(basename "$0" .sh)"
 +test ! -z "$debug" || remove_trash="$TEST_DIRECTORY/$test"
  rm -fr "$test" || {
        trap - exit
        echo >&5 "FATAL: Cannot prepare test area"