[PATCH] Make builtin-mv use parse-options

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pierre Habouzit <madcoder@xxxxxxxxxx>
---
 builtin-mv.c |   55 ++++++++++++++++++++++---------------------------------
 1 files changed, 22 insertions(+), 33 deletions(-)

diff --git a/builtin-mv.c b/builtin-mv.c
index b944651..17c3655 100644
--- a/builtin-mv.c
+++ b/builtin-mv.c
@@ -8,9 +8,12 @@
 #include "dir.h"
 #include "cache-tree.h"
 #include "path-list.h"
+#include "parse-options.h"
 
-static const char builtin_mv_usage[] =
-"git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)";
+static const char * const builtin_mv_usage[] = {
+	"git-mv [options] <source>... <destination>",
+	NULL
+};
 
 static const char **copy_pathspec(const char *prefix, const char **pathspec,
 				  int count, int base_name)
@@ -63,6 +66,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 {
 	int i, newfd, count;
 	int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
+	struct option builtin_mv_options[] = {
+		OPT_BOOLEAN('n', NULL, &show_only,"dry-run"),
+		OPT_BOOLEAN('f', NULL, &force, "force move/rename even if target exists"),
+		OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
+	};
 	const char **source, **destination, **dest_path;
 	enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
 	struct stat st;
@@ -78,47 +86,28 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 	if (read_cache() < 0)
 		die("index file corrupt");
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
+	count = parse_options(argc, argv, builtin_mv_options,
+	                      ARRAY_SIZE(builtin_mv_options),
+	                      builtin_mv_usage, 0);
+	if (--count < 1)
+		make_usage(builtin_mv_usage, builtin_mv_options,
+				   ARRAY_SIZE(builtin_mv_options));
 
-		if (arg[0] != '-')
-			break;
-		if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		if (!strcmp(arg, "-n")) {
-			show_only = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-f")) {
-			force = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-k")) {
-			ignore_errors = 1;
-			continue;
-		}
-		usage(builtin_mv_usage);
-	}
-	count = argc - i - 1;
-	if (count < 1)
-		usage(builtin_mv_usage);
-
-	source = copy_pathspec(prefix, argv + i, count, 0);
+	source = copy_pathspec(prefix, argv, count, 0);
 	modes = xcalloc(count, sizeof(enum update_mode));
-	dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
+	dest_path = copy_pathspec(prefix, argv + count, 1, 0);
 
 	if (dest_path[0][0] == '\0')
 		/* special case: "." was normalized to "" */
-		destination = copy_pathspec(dest_path[0], argv + i, count, 1);
+		destination = copy_pathspec(dest_path[0], argv, count, 1);
 	else if (!lstat(dest_path[0], &st) &&
 			S_ISDIR(st.st_mode)) {
 		dest_path[0] = add_slash(dest_path[0]);
-		destination = copy_pathspec(dest_path[0], argv + i, count, 1);
+		destination = copy_pathspec(dest_path[0], argv, count, 1);
 	} else {
 		if (count != 1)
-			usage(builtin_mv_usage);
+			make_usage(builtin_mv_usage, builtin_mv_options,
+					   ARRAY_SIZE(builtin_mv_options));
 		destination = dest_path;
 	}
 
-- 
1.5.3.GIT

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux