builtin/mv.c cleanup
v2 cover builtin/mv.c cleanup
Interdiff on v1 below. I'm not going with MOVE_UP_BY_ONE because [1] seems to agree with the name CLOSE_GAP, but that should be a separate topic (touching more than just mv.c)
[1] http://thread.gmane.org/gmane.comp.version-control.git/243675/focus=244390
-- 8< --
builtin/mv.c +14 -15
@@ -58,11 +58,6 @@ static const char *add_slash(const char *path)return path; } -static void move_up_one (void *p,int nmemb,int size) -{ -memmove (p, (char *)p + size, nmemb * size); -} -static struct lock_file lock_file;#define SUBMODULE_WITH_GITDIR ((const char *)1)
@@ -71,9 +66,9 @@ static void prepare_move_submodule(const char *src, int first, {struct strbuf submodule_dotgit =STRBUF_INIT ;if (!S_ISGITLINK (active_cache[first]->ce_mode)) -die (_ ("Directory %s is in index and no submodule?" ), src); +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" )); +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)
@@ -91,7 +86,7 @@ static int index_range_of_same_dir(const char *src, int length, first =cache_name_pos (src_w_slash, len_w_slash);if (first >=0 ) -die (_ ("%.*s is in index" ), len_w_slash, src_w_slash); +die (_ ("%.*s is in index" ), len_w_slash, src_w_slash); first =-1 - first;for (last = first; last < active_nr; last++) {
@@ -235,14 +230,18 @@ 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; -move_up_one (source + i, n,sizeof (*source)); -move_up_one (destination + i, n,sizeof (*destination)); -move_up_one (modes + i, n,sizeof (*modes)); -move_up_one (submodule_gitfile + i, n,sizeof (*submodule_gitfile)); +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--; } }
@@ -255,7 +254,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]);
@@ -278,7 +277,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (active_cache_changed &&write_locked_index (&the_index, &lock_file,COMMIT_LOCK )) -die (_ ("Unable to write new index file" )); +die (_ ("Unable to write new index file" ));return 0 ; }
-- 8< --
Nguyễn Thái Ngọc Duy (8):
mv: mark strings for translations
mv: flatten error handling code block
mv: split submodule move preparation code out
mv: remove an "if" that's always true
mv: move index search code out
mv: unindent one level for directory move code
mv: combine two if(s)
mv: no SP between function name and the first opening parenthese
builtin/mv.c | 171 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 85 insertions(+), 86 deletions(-)
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 1/8 mv: mark strings for translations
--- builtin/mv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/mv.c +1 -1
@@ -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; }
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 2/8 mv: flatten error handling code block
In reply to Nguyễn Thái Ngọc Duy--- builtin/mv.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)
builtin/mv.c +16 -18
@@ -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--; } }
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 3/8 mv: split submodule move preparation code out
In reply to Nguyễn Thái Ngọc Duy"Huh?" is removed from die() message.
--- builtin/mv.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
builtin/mv.c +21 -14
@@ -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,10 @@ 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 ;
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 4/8 mv: remove an "if" that's always true
In reply to Nguyễn Thái Ngọc DuyThis 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.
--- builtin/mv.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-)
builtin/mv.c +7 -15
@@ -175,22 +175,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);
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 5/8 mv: move index search code out
In reply to Nguyễn Thái Ngọc Duy"Huh?" is removed from die() message.
--- builtin/mv.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-)
builtin/mv.c +25 -17
@@ -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 ;
@@ -153,25 +176,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 {
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 6/8 mv: unindent one level for directory move code
In reply to Nguyễn Thái Ngọc Duy--- builtin/mv.c | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-)
builtin/mv.c +21 -26
@@ -171,42 +171,37 @@ 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 ) { 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; + }else {/* last - first >= 1 */ +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 *)); + modes[i] =WORKING_DIRECTORY ; + 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" );
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 7/8 mv: combine two if(s)
In reply to Nguyễn Thái Ngọc Duy--- builtin/mv.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
builtin/mv.c +3 -4
@@ -275,10 +275,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 ; }
Signature · 1 line
2.1.0.rc0.78.gc0d8480
PATCH v2 8/8 mv: no SP between function name and the first opening parenthese
In reply to Nguyễn Thái Ngọc Duy--- builtin/mv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
builtin/mv.c +2 -2
@@ -230,7 +230,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;
@@ -254,7 +254,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]);
Signature · 1 line
2.1.0.rc0.78.gc0d8480
Re: [PATCH v2 6/8] mv: unindent one level for directory move code
In reply to Nguyễn Thái Ngọc DuyQuoted 10 lines · Nguyễn Thái Ngọc Duy
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- builtin/mv.c | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-)builtin/mv.c +0
-0
builtin/mv.c int cmd_mv(int argc, const char **argv, const char *prefix) · line 179
Quoted 9 lines
builtin/mv.c +3
-0@@ -171,42 +171,37 @@ 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), last;if (first >=0 )prepare_move_submodule (src, first, submodule_gitfile + i); +else if (index_range_of_same_dir (src, length, + &first, &last) <1 ) {
The function returns (last - first), so (last - first) < 1 holds inside this block, right?
builtin/mv.c int cmd_mv(int argc, const char **argv, const char *prefix) · line 182
Quoted 3 lines
builtin/mv.c +0
-0modes[i] =WORKING_DIRECTORY ;if (last - first <1 ) bad =_ ("source directory is empty" );
Then do you need this conditional, or it is always bad here?
If it is always bad, then modes[i] do not need to be assigned to, either, I think.
Am I missing something?
builtin/mv.c int cmd_mv(int argc, const char **argv, const char *prefix) · line 187
Quoted 6 lines
builtin/mv.c +4
-0+ }else {/* last - first >= 1 */ +int j, dst_len, n; + modes[i] =WORKING_DIRECTORY ; + n = argc + last - first;
Otherwise, perhaps squash this in?
builtin/mv.c +4 -5
@@ -172,15 +172,14 @@ 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), last; +if (first >=0 )prepare_move_submodule (src, first, submodule_gitfile + i);else if (index_range_of_same_dir (src, length, - &first, &last) <1 ) { - modes[i] =WORKING_DIRECTORY ; -if (last - first <1 ) - bad =_ ("source directory is empty" ); - }else {/* last - first >= 1 */ + &first, &last) <1 ) + bad =_ ("source directory is empty" ); +else {/* last - first >= 1 */ int j, dst_len, n; modes[i] =WORKING_DIRECTORY ;
Re: [PATCH v2 6/8] mv: unindent one level for directory move code
Quoted 29 lines · Junio C Hamano
Quoted 10 lines · Nguyễn Thái Ngọc Duy
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- builtin/mv.c | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-)builtin/mv.c +0
-0builtin/mv.c int cmd_mv(int argc, const char **argv, const char *prefix) · line 179
Quoted 9 lines
builtin/mv.c +3
-0@@ -171,42 +171,37 @@ 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), last;if (first >=0 )prepare_move_submodule (src, first, submodule_gitfile + i); +else if (index_range_of_same_dir (src, length, + &first, &last) <1 ) {The function returns (last - first), so (last - first) < 1 holds inside this block, right?
builtin/mv.c int cmd_mv(int argc, const char **argv, const char *prefix) · line 182
Quoted 3 lines
builtin/mv.c +0
-0modes[i] =WORKING_DIRECTORY ;if (last - first <1 ) bad =_ ("source directory is empty" );
Quoted 6 lines
Then do you need this conditional, or it is always bad here?
If it is always bad, then modes[i] do not need to be assigned to, either, I think.
Am I missing something?
No you're right.
Quoted 8 lines
builtin/mv.c int cmd_mv(int argc, const char **argv, const char *prefix) · line 187
Quoted 6 lines
builtin/mv.c +4
-0+ }else {/* last - first >= 1 */ +int j, dst_len, n; + modes[i] =WORKING_DIRECTORY ; + n = argc + last - first;Otherwise, perhaps squash this in?
Sure. But I'm having second thought about this series.
mv.c is centered around files on worktree, which makes it pretty hard if we want to make it more like rm.c where index and worktree entries are treated more equally and pathspec is applied. Doing that in mv.c would require a lot more reorganization that makes this series obsolete. But on the other hand, I'm not even sure if I have time to pursue that. So..
Signature · 1 line
Duy
Re: [PATCH v2 6/8] mv: unindent one level for directory move code
Quoted 9 lines · Duy Nguyen
Quoted 1 line
Otherwise, perhaps squash this in?
Sure. But I'm having second thought about this series.
mv.cis centered around files on worktree, which makes it pretty hard if we want to make it more likerm.cwhere index and worktree entries are treated more equally and pathspec is applied. Doing that inmv.cwould require a lot more reorganization that makes this series obsolete.
Well, you may have started these as preparatory clean-up, but taken alone these are purely making the result more readable without changing any behaviour, so let's get them right first.
Thanks.