quote_path_relative(): remove redundant parameter
authorJiang Xin <worldhello.net@gmail.com>
Tue, 25 Jun 2013 15:53:45 +0000 (23:53 +0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 26 Jun 2013 18:16:48 +0000 (11:16 -0700)
quote_path_relative() used to take a counted string as its parameter
(the string to be quoted). With an earlier change, it now uses
relative_path() that does not take a counted string, and we have
been passing only the pointer to the string since then.

Remove the length parameter from quote_path_relative() to show that
this parameter was redundant. All the changed lines show that the
caller passed either -1 (to ask the function run strlen() on the
string), or the length of the string, so the earlier conversion was
safe.

All the callers of quote_path_relative() that used to take counted string
have been audited to make sure that they are passing length of the actual
string (or -1 to ask the callee run strlen())

Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clean.c
builtin/grep.c
builtin/ls-files.c
quote.c
quote.h
wt-status.c
index 04e396b17acc2443a663dd085ef812c3c2746d27..f77f95d7adca738907adbd7acd54ed9fb64eae46 100644 (file)
@@ -56,7 +56,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
        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), &quoted, prefix);
+                       quote_path_relative(path->buf, prefix, &quoted);
                        printf(dry_run ?  _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
                                        quoted.buf);
                }
@@ -70,7 +70,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                /* 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), &quoted, prefix);
+                       quote_path_relative(path->buf, prefix, &quoted);
                        warning(_(msg_warn_remove_failed), quoted.buf);
                        *dir_gone = 0;
                }
@@ -94,7 +94,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                        if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone))
                                ret = 1;
                        if (gone) {
-                               quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
+                               quote_path_relative(path->buf, prefix, &quoted);
                                string_list_append(&dels, quoted.buf);
                        } else
                                *dir_gone = 0;
@@ -102,10 +102,10 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                } else {
                        res = dry_run ? 0 : unlink(path->buf);
                        if (!res) {
-                               quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
+                               quote_path_relative(path->buf, prefix, &quoted);
                                string_list_append(&dels, quoted.buf);
                        } else {
-                               quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
+                               quote_path_relative(path->buf, prefix, &quoted);
                                warning(_(msg_warn_remove_failed), quoted.buf);
                                *dir_gone = 0;
                                ret = 1;
@@ -127,7 +127,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                if (!res)
                        *dir_gone = 1;
                else {
-                       quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
+                       quote_path_relative(path->buf, prefix, &quoted);
                        warning(_(msg_warn_remove_failed), quoted.buf);
                        *dir_gone = 0;
                        ret = 1;
@@ -262,7 +262,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                                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);
+                                       qname = quote_path_relative(directory.buf, prefix, &buf);
                                        printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
                                }
                        }
@@ -272,11 +272,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                                continue;
                        res = dry_run ? 0 : unlink(ent->name);
                        if (res) {
-                               qname = quote_path_relative(ent->name, -1, &buf, prefix);
+                               qname = quote_path_relative(ent->name, prefix, &buf);
                                warning(_(msg_warn_remove_failed), qname);
                                errors++;
                        } else if (!quiet) {
-                               qname = quote_path_relative(ent->name, -1, &buf, prefix);
+                               qname = quote_path_relative(ent->name, prefix, &buf);
                                printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
                        }
                }
index 159e65d47a41b56634bc3fb480f9c051d40e692c..a419cda729c24453427067dd9e14a8850d9e7075 100644 (file)
@@ -286,8 +286,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
        struct strbuf pathbuf = STRBUF_INIT;
 
        if (opt->relative && opt->prefix_length) {
-               quote_path_relative(filename + tree_name_len, -1, &pathbuf,
-                                   opt->prefix);
+               quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
                strbuf_insert(&pathbuf, 0, filename, tree_name_len);
        } else {
                strbuf_addstr(&pathbuf, filename);
@@ -318,7 +317,7 @@ 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);
+               quote_path_relative(filename, opt->prefix, &buf);
        else
                strbuf_addstr(&buf, filename);
 
index 67e3713cd331dd878f579400c17964ce4c31e9ce..48c82e8e9b02907bcc88156b52805dcc514da28b 100644 (file)
@@ -394,7 +394,7 @@ int report_path_error(const char *ps_matched, const char **pathspec, const char
                if (found_dup)
                        continue;
 
-               name = quote_path_relative(pathspec[num], -1, &sb, prefix);
+               name = quote_path_relative(pathspec[num], prefix, &sb);
                error("pathspec '%s' did not match any file(s) known to git.",
                      name);
                errors++;
diff --git a/quote.c b/quote.c
index 64ff3441585780434b7de36ae06a1ce8aa35f44b..ebb835923f70b9709d3052ecc0e1ccf1df8bfeeb 100644 (file)
--- a/quote.c
+++ b/quote.c
@@ -325,8 +325,8 @@ void write_name_quoted_relative(const char *name, size_t len,
 }
 
 /* quote path as relative to the given prefix */
-char *quote_path_relative(const char *in, int len,
-                         struct strbuf *out, const char *prefix)
+char *quote_path_relative(const char *in, const char *prefix,
+                         struct strbuf *out)
 {
        struct strbuf sb = STRBUF_INIT;
        const char *rel = relative_path(in, prefix, &sb);
@@ -334,9 +334,6 @@ char *quote_path_relative(const char *in, int len,
        quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
        strbuf_release(&sb);
 
-       if (!out->len)
-               strbuf_addstr(out, "./");
-
        return out->buf;
 }
 
diff --git a/quote.h b/quote.h
index 133155a48b4baa56cd70c07af8a477771f373105..5610159c591bbb04ac2f1b0608eb974aa9ca74dd 100644 (file)
--- a/quote.h
+++ b/quote.h
@@ -65,8 +65,8 @@ extern void write_name_quoted_relative(const char *name, size_t len,
                FILE *fp, int terminator);
 
 /* quote path as relative to the given prefix */
-extern char *quote_path_relative(const char *in, int len,
-                         struct strbuf *out, const char *prefix);
+extern char *quote_path_relative(const char *in, const char *prefix,
+                         struct strbuf *out);
 
 /* quoting as a string literal for other languages */
 extern void perl_quote_print(FILE *stream, const char *src);
index bf84a86ee3d6b136bd83115f0bf15a55835817fc..ef0fc4bb49009dc2cf6215734a89e1b8eb2f3849 100644 (file)
@@ -243,7 +243,7 @@ static void wt_status_print_unmerged_data(struct wt_status *s,
        struct strbuf onebuf = STRBUF_INIT;
        const char *one, *how = _("bug");
 
-       one = quote_path(it->string, -1, &onebuf, s->prefix);
+       one = quote_path(it->string, s->prefix, &onebuf);
        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
        switch (d->stagemask) {
        case 1: how = _("both deleted:"); break;
@@ -297,8 +297,8 @@ static void wt_status_print_change_data(struct wt_status *s,
                    change_type);
        }
 
-       one = quote_path(one_name, -1, &onebuf, s->prefix);
-       two = quote_path(two_name, -1, &twobuf, s->prefix);
+       one = quote_path(one_name, s->prefix, &onebuf);
+       two = quote_path(two_name, s->prefix, &twobuf);
 
        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
        switch (status) {
@@ -706,8 +706,7 @@ static void wt_status_print_other(struct wt_status *s,
                struct string_list_item *it;
                const char *path;
                it = &(l->items[i]);
-               path = quote_path(it->string, strlen(it->string),
-                                 &buf, s->prefix);
+               path = quote_path(it->string, s->prefix, &buf);
                if (column_active(s->colopts)) {
                        string_list_append(&output, path);
                        continue;
@@ -1289,7 +1288,7 @@ static void wt_shortstatus_unmerged(struct string_list_item *it,
        } else {
                struct strbuf onebuf = STRBUF_INIT;
                const char *one;
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
+               one = quote_path(it->string, s->prefix, &onebuf);
                printf(" %s\n", one);
                strbuf_release(&onebuf);
        }
@@ -1317,7 +1316,7 @@ static void wt_shortstatus_status(struct string_list_item *it,
                struct strbuf onebuf = STRBUF_INIT;
                const char *one;
                if (d->head_path) {
-                       one = quote_path(d->head_path, -1, &onebuf, s->prefix);
+                       one = quote_path(d->head_path, s->prefix, &onebuf);
                        if (*one != '"' && strchr(one, ' ') != NULL) {
                                putchar('"');
                                strbuf_addch(&onebuf, '"');
@@ -1326,7 +1325,7 @@ static void wt_shortstatus_status(struct string_list_item *it,
                        printf("%s -> ", one);
                        strbuf_release(&onebuf);
                }
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
+               one = quote_path(it->string, s->prefix, &onebuf);
                if (*one != '"' && strchr(one, ' ') != NULL) {
                        putchar('"');
                        strbuf_addch(&onebuf, '"');
@@ -1345,7 +1344,7 @@ static void wt_shortstatus_other(struct string_list_item *it,
        } else {
                struct strbuf onebuf = STRBUF_INIT;
                const char *one;
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
+               one = quote_path(it->string, s->prefix, &onebuf);
                color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
                printf(" %s\n", one);
                strbuf_release(&onebuf);