@matheusbsbs / matheus-bagliano / commits / c7a20c117f
Fork 0

Make builtin-mv.c use parse-options

Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Pierre Habouzit committed Oct 7, 2007 at 14:19 UTC c7a20c117fedd40652d446e58b3eba7e2420fa47
1 file changed +35 -49
builtin-mv.c
+35 -49
@@ -8,9 +8,12 @@
8 #include "dir.h"
9 #include "cache-tree.h"
10 #include "path-list.h"
11 +#include "parse-options.h"
12
12 -static const char builtin_mv_usage[] =
13 -"git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)";
13 +static const char * const builtin_mv_usage[] = {
14 + "git-mv [options] <source>... <destination>",
15 + NULL
16 +};
17
18 static const char **copy_pathspec(const char *prefix, const char **pathspec,
19 int count, int base_name)
@@ -61,8 +64,14 @@ static struct lock_file lock_file;
64
65 int cmd_mv(int argc, const char **argv, const char *prefix)
66 {
64 - int i, newfd, count;
67 + int i, newfd;
68 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
69 + struct option builtin_mv_options[] = {
70 + OPT__DRY_RUN(&show_only),
71 + OPT_BOOLEAN('f', NULL, &force, "force move/rename even if target exists"),
72 + OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
73 + OPT_END(),
74 + };
75 const char **source, **destination, **dest_path;
76 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
77 struct stat st;
@@ -78,52 +87,29 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
87 if (read_cache() < 0)
88 die("index file corrupt");
89
81 - for (i = 1; i < argc; i++) {
82 - const char *arg = argv[i];
90 + argc = parse_options(argc, argv, builtin_mv_options, builtin_mv_usage, 0);
91 + if (--argc < 1)
92 + usage_with_options(builtin_mv_usage, builtin_mv_options);
93
84 - if (arg[0] != '-')
85 - break;
86 - if (!strcmp(arg, "--")) {
87 - i++;
88 - break;
89 - }
90 - if (!strcmp(arg, "-n")) {
91 - show_only = 1;
92 - continue;
93 - }
94 - if (!strcmp(arg, "-f")) {
95 - force = 1;
96 - continue;
97 - }
98 - if (!strcmp(arg, "-k")) {
99 - ignore_errors = 1;
100 - continue;
101 - }
102 - usage(builtin_mv_usage);
103 - }
104 - count = argc - i - 1;
105 - if (count < 1)
106 - usage(builtin_mv_usage);
107 -
108 - source = copy_pathspec(prefix, argv + i, count, 0);
109 - modes = xcalloc(count, sizeof(enum update_mode));
110 - dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
94 + source = copy_pathspec(prefix, argv, argc, 0);
95 + modes = xcalloc(argc, sizeof(enum update_mode));
96 + dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
97
98 if (dest_path[0][0] == '\0')
99 /* special case: "." was normalized to "" */
114 - destination = copy_pathspec(dest_path[0], argv + i, count, 1);
100 + destination = copy_pathspec(dest_path[0], argv, argc, 1);
101 else if (!lstat(dest_path[0], &st) &&
102 S_ISDIR(st.st_mode)) {
103 dest_path[0] = add_slash(dest_path[0]);
118 - destination = copy_pathspec(dest_path[0], argv + i, count, 1);
104 + destination = copy_pathspec(dest_path[0], argv, argc, 1);
105 } else {
120 - if (count != 1)
121 - usage(builtin_mv_usage);
106 + if (argc != 1)
107 + usage_with_options(builtin_mv_usage, builtin_mv_options);
108 destination = dest_path;
109 }
110
111 /* Checking */
126 - for (i = 0; i < count; i++) {
112 + for (i = 0; i < argc; i++) {
113 const char *src = source[i], *dst = destination[i];
114 int length, src_is_dir;
115 const char *bad = NULL;
@@ -167,13 +153,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
153
154 if (last - first > 0) {
155 source = xrealloc(source,
170 - (count + last - first)
156 + (argc + last - first)
157 * sizeof(char *));
158 destination = xrealloc(destination,
173 - (count + last - first)
159 + (argc + last - first)
160 * sizeof(char *));
161 modes = xrealloc(modes,
176 - (count + last - first)
162 + (argc + last - first)
163 * sizeof(enum update_mode));
164 }
165
@@ -183,13 +169,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
169 for (j = 0; j < last - first; j++) {
170 const char *path =
171 active_cache[first + j]->name;
186 - source[count + j] = path;
187 - destination[count + j] =
172 + source[argc + j] = path;
173 + destination[argc + j] =
174 prefix_path(dst, dst_len,
175 path + length);
190 - modes[count + j] = INDEX;
176 + modes[argc + j] = INDEX;
177 }
192 - count += last - first;
178 + argc += last - first;
179 }
180 } else if (lstat(dst, &st) == 0) {
181 bad = "destination exists";
@@ -216,12 +202,12 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
202
203 if (bad) {
204 if (ignore_errors) {
219 - if (--count > 0) {
205 + if (--argc > 0) {
206 memmove(source + i, source + i + 1,
221 - (count - i) * sizeof(char *));
207 + (argc - i) * sizeof(char *));
208 memmove(destination + i,
209 destination + i + 1,
224 - (count - i) * sizeof(char *));
210 + (argc - i) * sizeof(char *));
211 }
212 } else
213 die ("%s, source=%s, destination=%s",
@@ -229,7 +215,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
215 }
216 }
217
232 - for (i = 0; i < count; i++) {
218 + for (i = 0; i < argc; i++) {
219 const char *src = source[i], *dst = destination[i];
220 enum update_mode mode = modes[i];
221 if (show_only || verbose)
@@ -253,7 +239,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
239 path_list_insert(dst, &added);
240 }
241
256 - if (show_only) {
242 + if (show_only) {
243 show_list("Changed : ", &changed);
244 show_list("Adding : ", &added);
245 show_list("Deleting : ", &deleted);