From eac0ccc2cd0022546b5037a5aabef8ec687bb60a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:29 +0700 Subject: mv: mark strings for translations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/mv.c b/builtin/mv.c index 6ffe540c20..b892f63df9 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -108,7 +108,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME); } else { if (argc != 1) - die("destination '%s' is not a directory", dest_path[0]); + die(_("destination '%s' is not a directory"), dest_path[0]); destination = dest_path; } -- cgit v1.2.1 From ad1a19d0e7cdd69fd4902b80ab691d43b102e3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:30 +0700 Subject: mv: flatten error handling code block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index b892f63df9..c48129e301 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -225,24 +225,22 @@ int cmd_mv(int argc, const char **argv, const char *prefix) 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 *)); - memmove(modes + i, modes + i + 1, - (argc - i) * sizeof(enum update_mode)); - memmove(submodule_gitfile + i, - submodule_gitfile + i + 1, - (argc - i) * sizeof(char *)); - i--; - } - } else - die (_("%s, source=%s, destination=%s"), - bad, src, dst); + if (!bad) + continue; + if (!ignore_errors) + die (_("%s, source=%s, destination=%s"), + bad, src, dst); + if (--argc > 0) { + int n = argc - i; + memmove(source + i, source + i + 1, + n * sizeof(char *)); + memmove(destination + i, destination + i + 1, + n * sizeof(char *)); + memmove(modes + i, modes + i + 1, + n * sizeof(enum update_mode)); + memmove(submodule_gitfile + i, submodule_gitfile + i + 1, + n * sizeof(char *)); + i--; } } -- cgit v1.2.1 From 3af05a6d0da3addbcc050f3b1a1ac379e6c4025c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:31 +0700 Subject: mv: split submodule move preparation code out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Huh?" is removed from die() message. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index c48129e301..5a8ff00047 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -61,6 +61,23 @@ static const char *add_slash(const char *path) static struct lock_file lock_file; #define SUBMODULE_WITH_GITDIR ((const char *)1) +static void prepare_move_submodule(const char *src, int first, + const char **submodule_gitfile) +{ + struct strbuf submodule_dotgit = STRBUF_INIT; + if (!S_ISGITLINK(active_cache[first]->ce_mode)) + die(_("Directory %s is in index and no submodule?"), src); + if (!is_staging_gitmodules_ok()) + die(_("Please stage your changes to .gitmodules or stash them to proceed")); + strbuf_addf(&submodule_dotgit, "%s/.git", src); + *submodule_gitfile = read_gitfile(submodule_dotgit.buf); + if (*submodule_gitfile) + *submodule_gitfile = xstrdup(*submodule_gitfile); + else + *submodule_gitfile = SUBMODULE_WITH_GITDIR; + strbuf_release(&submodule_dotgit); +} + int cmd_mv(int argc, const char **argv, const char *prefix) { int i, gitmodules_modified = 0; @@ -132,20 +149,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix) bad = _("cannot move directory over file"); else if (src_is_dir) { int first = cache_name_pos(src, length); - if (first >= 0) { - struct strbuf submodule_dotgit = STRBUF_INIT; - if (!S_ISGITLINK(active_cache[first]->ce_mode)) - die (_("Huh? Directory %s is in index and no submodule?"), src); - if (!is_staging_gitmodules_ok()) - die (_("Please, stage your changes to .gitmodules or stash them to proceed")); - strbuf_addf(&submodule_dotgit, "%s/.git", src); - submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf); - if (submodule_gitfile[i]) - submodule_gitfile[i] = xstrdup(submodule_gitfile[i]); - else - submodule_gitfile[i] = SUBMODULE_WITH_GITDIR; - strbuf_release(&submodule_dotgit); - } else { + + if (first >= 0) + prepare_move_submodule(src, first, + submodule_gitfile + i); + else { const char *src_w_slash = add_slash(src); int last, len_w_slash = length + 1; -- cgit v1.2.1 From 42de4b169c5de9d3f22eb1f988aa5b69447951b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:32 +0700 Subject: mv: remove an "if" that's always true MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is inside an "else" block of "if (last - first < 1)", so we know that "last - first >= 1" when we come here. No need to check "last - first > 0". While at there, save "argc + last - first" to a variable to shorten the statements a bit. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 5a8ff00047..3b19ca26b2 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -176,22 +176,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix) if (last - first < 1) bad = _("source directory is empty"); else { - int j, dst_len; + int j, dst_len, n; - 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)); - submodule_gitfile = xrealloc(submodule_gitfile, - (argc + last - first) - * sizeof(char *)); - } + n = argc + last - first; + source = xrealloc(source, n * sizeof(char *)); + destination = xrealloc(destination, n * sizeof(char *)); + modes = xrealloc(modes, n * sizeof(enum update_mode)); + submodule_gitfile = + xrealloc(submodule_gitfile, n * sizeof(char *)); dst = add_slash(dst); dst_len = strlen(dst); -- cgit v1.2.1 From e2b6cfa02e29c4a8f91ede15026b5eaa790de329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:33 +0700 Subject: mv: move index search code out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Huh?" is removed from die() message. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 3b19ca26b2..42a04d2b8b 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -78,6 +78,29 @@ static void prepare_move_submodule(const char *src, int first, strbuf_release(&submodule_dotgit); } +static int index_range_of_same_dir(const char *src, int length, + int *first_p, int *last_p) +{ + const char *src_w_slash = add_slash(src); + int first, last, len_w_slash = length + 1; + + first = cache_name_pos(src_w_slash, len_w_slash); + if (first >= 0) + die(_("%.*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; + } + if (src_w_slash != src) + free((char *)src_w_slash); + *first_p = first; + *last_p = last; + return last - first; +} + int cmd_mv(int argc, const char **argv, const char *prefix) { int i, gitmodules_modified = 0; @@ -154,25 +177,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix) prepare_move_submodule(src, first, submodule_gitfile + i); else { - const char *src_w_slash = add_slash(src); - int last, len_w_slash = length + 1; + int 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; - } - if (src_w_slash != src) - free((char *)src_w_slash); - + index_range_of_same_dir(src, length, &first, &last); if (last - first < 1) bad = _("source directory is empty"); else { -- cgit v1.2.1 From b46b15dea01a898888fac977aeed6055a761c3ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:34 +0700 Subject: mv: unindent one level for directory move code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 42a04d2b8b..4f324aea1d 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -171,43 +171,36 @@ int cmd_mv(int argc, const char **argv, const char *prefix) && lstat(dst, &st) == 0) bad = _("cannot move directory over file"); else if (src_is_dir) { - int first = cache_name_pos(src, length); + int first = cache_name_pos(src, length), last; if (first >= 0) prepare_move_submodule(src, first, submodule_gitfile + i); - else { - int last; + else if (index_range_of_same_dir(src, length, + &first, &last) < 1) + bad = _("source directory is empty"); + else { /* last - first >= 1 */ + int j, dst_len, n; modes[i] = WORKING_DIRECTORY; - index_range_of_same_dir(src, length, &first, &last); - if (last - first < 1) - bad = _("source directory is empty"); - else { - int j, dst_len, n; + n = argc + last - first; + source = xrealloc(source, n * sizeof(char *)); + destination = xrealloc(destination, n * sizeof(char *)); + modes = xrealloc(modes, n * sizeof(enum update_mode)); + submodule_gitfile = xrealloc(submodule_gitfile, n * sizeof(char *)); - n = argc + last - first; - source = xrealloc(source, n * sizeof(char *)); - destination = xrealloc(destination, n * sizeof(char *)); - modes = xrealloc(modes, n * sizeof(enum update_mode)); - submodule_gitfile = - xrealloc(submodule_gitfile, n * sizeof(char *)); + dst = add_slash(dst); + dst_len = strlen(dst); - 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; - submodule_gitfile[argc + j] = NULL; - } - argc += last - first; + 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; + submodule_gitfile[argc + j] = NULL; } + argc += last - first; } } else if (cache_name_pos(src, length) < 0) bad = _("not under version control"); -- cgit v1.2.1 From dcadc8b80609f6857ab35163a8a6e08cdaf35b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:35 +0700 Subject: mv: combine two if(s) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 4f324aea1d..e253550551 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -274,10 +274,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix) if (gitmodules_modified) stage_updated_gitmodules(); - if (active_cache_changed) { - if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) - die(_("Unable to write new index file")); - } + if (active_cache_changed && + write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) + die(_("Unable to write new index file")); return 0; } -- cgit v1.2.1 From 4f1bbd23af48e875420a22fa5fa23542f17d6e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 10 Aug 2014 09:29:36 +0700 Subject: mv: no SP between function name and the first opening parenthese MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/mv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index e253550551..bf784cb943 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -229,7 +229,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) if (!bad) continue; if (!ignore_errors) - die (_("%s, source=%s, destination=%s"), + die(_("%s, source=%s, destination=%s"), bad, src, dst); if (--argc > 0) { int n = argc - i; @@ -253,7 +253,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) printf(_("Renaming %s to %s\n"), src, dst); if (!show_only && mode != INDEX) { if (rename(src, dst) < 0 && !ignore_errors) - die_errno (_("renaming '%s' failed"), src); + die_errno(_("renaming '%s' failed"), src); if (submodule_gitfile[i]) { if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR) connect_work_tree_and_git_dir(dst, submodule_gitfile[i]); -- cgit v1.2.1