checkout-index: handle "--no-prefix" option
[gitweb.git] / builtin / checkout-index.c
index 8028c3768f3ef09035ba9092b4e34cdb498405e9..43beddee13ab0f9d72993ec084756c358f981879 100644 (file)
@@ -11,7 +11,7 @@
 #include "parse-options.h"
 
 #define CHECKOUT_ALL 4
-static int line_termination = '\n';
+static int nul_term_line;
 static int checkout_stage; /* default to checkout stage0 */
 static int to_tempfile;
 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
@@ -35,7 +35,8 @@ static void write_tempfile_record(const char *name, const char *prefix)
                fputs(topath[checkout_stage], stdout);
 
        putchar('\t');
-       write_name_quoted_relative(name, prefix, stdout, line_termination);
+       write_name_quoted_relative(name, prefix, stdout,
+                                  nul_term_line ? '\0' : '\n');
 
        for (i = 0; i < 4; i++) {
                topath[i][0] = 0;
@@ -141,24 +142,6 @@ static int option_parse_u(const struct option *opt,
        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)
 {
@@ -194,16 +177,14 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
                { 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_BOOL('z', NULL, &nul_term_line,
+                       N_("paths are separated with NUL character")),
                OPT_BOOL(0, "stdin", &read_from_stdin,
                        N_("read list of paths from the standard input")),
                OPT_BOOL(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_STRING(0, "prefix", &state.base_dir, N_("string"),
+                       N_("when creating files, prepend <string>")),
                OPT_CALLBACK(0, "stage", NULL, NULL,
                        N_("copy out the files from named stage"),
                        option_parse_stage),
@@ -214,7 +195,6 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
                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) {
@@ -227,6 +207,10 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
        state.quiet = quiet;
        state.not_new = not_new;
 
+       if (!state.base_dir)
+               state.base_dir = "";
+       state.base_dir_len = strlen(state.base_dir);
+
        if (state.base_dir_len || to_tempfile) {
                /* when --prefix is specified we do not
                 * want to update cache.
@@ -253,24 +237,27 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
        }
 
        if (read_from_stdin) {
-               struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
+               struct strbuf buf = STRBUF_INIT;
+               struct strbuf unquoted = STRBUF_INIT;
+               strbuf_getline_fn getline_fn;
 
                if (all)
                        die("git checkout-index: don't mix '--all' and '--stdin'");
 
-               while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
+               getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
+               while (getline_fn(&buf, stdin) != EOF) {
                        char *p;
-                       if (line_termination && buf.buf[0] == '"') {
-                               strbuf_reset(&nbuf);
-                               if (unquote_c_style(&nbuf, buf.buf, NULL))
+                       if (!nul_term_line && buf.buf[0] == '"') {
+                               strbuf_reset(&unquoted);
+                               if (unquote_c_style(&unquoted, buf.buf, NULL))
                                        die("line is badly quoted");
-                               strbuf_swap(&buf, &nbuf);
+                               strbuf_swap(&buf, &unquoted);
                        }
                        p = prefix_path(prefix, prefix_length, buf.buf);
                        checkout_file(p, prefix);
                        free(p);
                }
-               strbuf_release(&nbuf);
+               strbuf_release(&unquoted);
                strbuf_release(&buf);
        }