From: Stephen Boyd <bebarino@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 2/2] read-tree: migrate to parse-options
Date: Tue, 23 Jun 2009 21:27:52 -0700 [thread overview]
Message-ID: <1245817672-25483-2-git-send-email-bebarino@gmail.com> (raw)
In-Reply-To: <1245817672-25483-1-git-send-email-bebarino@gmail.com>
Cleanup the documentation to explicitly state that --exclude-directory
is only meaningful when used with -u. Also make the documentation more
consistent with the usage message printed with read-tree --help-all.
Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---
Documentation/git-read-tree.txt | 5 +-
builtin-read-tree.c | 220 +++++++++++++++++++++-----------------
2 files changed, 126 insertions(+), 99 deletions(-)
diff --git a/Documentation/git-read-tree.txt b/Documentation/git-read-tree.txt
index 7160fa1..1e0cc8f 100644
--- a/Documentation/git-read-tree.txt
+++ b/Documentation/git-read-tree.txt
@@ -8,7 +8,10 @@ git-read-tree - Reads tree information into the index
SYNOPSIS
--------
-'git read-tree' (<tree-ish> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]])
+'git read-tree' [--index-output=<file>] <treeish>
+'git read-tree' [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>]
+ [-u [--exclude-per-directory=<gitignore>] | -i]]
+ [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]
DESCRIPTION
diff --git a/builtin-read-tree.c b/builtin-read-tree.c
index 887e177..adca739 100644
--- a/builtin-read-tree.c
+++ b/builtin-read-tree.c
@@ -12,6 +12,7 @@
#include "unpack-trees.h"
#include "dir.h"
#include "builtin.h"
+#include "parse-options.h"
static int nr_trees;
static struct tree *trees[MAX_UNPACK_TREES];
@@ -29,7 +30,83 @@ static int list_tree(unsigned char *sha1)
return 0;
}
-static const char read_tree_usage[] = "git read-tree (<sha> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";
+static const char * const read_tree_usage[] = {
+ "git read-tree [--index-output=<file>] <treeish>",
+ "git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u [--exclude-per-directory=<gitignore>] | -i]] [--index-output=<file>] <treeish1> [<treeish2> [<treeish3>]]",
+ NULL
+};
+
+static int index_output_cb(const struct option *opt, const char *arg,
+ int unset)
+{
+ set_alternate_index_output(arg);
+ return 0;
+}
+
+static int prefix_cb(const struct option *opt, const char *arg, int unset)
+{
+ struct unpack_trees_options *opts;
+ opts = (struct unpack_trees_options *)opt->value;
+
+ if (opts->merge || opts->prefix)
+ return 1;
+ opts->prefix = arg;
+ opts->merge = 1;
+ if (read_cache_unmerged())
+ die("you need to resolve your current index first");
+
+ return 0;
+}
+
+static int reset_cb(const struct option *opt, const char *arg, int unset)
+{
+ struct unpack_trees_options *opts;
+ opts = (struct unpack_trees_options *)opt->value;
+
+ if (opts->merge || opts->prefix)
+ return -1;
+ opts->reset = 1;
+ opts->merge = 1;
+ read_cache_unmerged();
+
+ return 0;
+}
+
+static int merge_cb(const struct option *opt, const char *arg, int unset)
+{
+ struct unpack_trees_options *opts;
+ opts = (struct unpack_trees_options *)opt->value;
+
+ if (opts->merge || opts->prefix)
+ return -1;
+ if (read_cache_unmerged())
+ die("you need to resolve your current index first");
+ opts->merge = 1;
+
+ return 0;
+}
+
+static int exclude_per_directory_cb(const struct option *opt, const char *arg,
+ int unset)
+{
+ struct dir_struct *dir;
+ struct unpack_trees_options *opts;
+
+ opts = (struct unpack_trees_options *)opt->value;
+
+ if (opts->dir)
+ die("more than one --exclude-per-directory given.");
+
+ dir = xcalloc(1, sizeof(*opts->dir));
+ dir->flags |= DIR_SHOW_IGNORED;
+ dir->exclude_per_dir = arg;
+ opts->dir = dir;
+ /* We do not need to nor want to do read-directory
+ * here; we are merely interested in reusing the
+ * per directory ignore stack mechanism.
+ */
+ return 0;
+}
static struct lock_file lock_file;
@@ -39,6 +116,37 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
unsigned char sha1[20];
struct tree_desc t[MAX_UNPACK_TREES];
struct unpack_trees_options opts;
+ int update = 0, index_only = 0, trivial_merges_only = 0, aggressive = 0,
+ verbose = 0;
+ const struct option read_tree_options[] = {
+ { OPTION_CALLBACK, 0, "index-output", NULL, "FILE",
+ "write resulting index to <FILE>",
+ PARSE_OPT_NONEG, index_output_cb },
+ { OPTION_CALLBACK, 0, "prefix", &opts, "<subdirectory>/",
+ "read the tree into the index under <subdirectory>",
+ PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, prefix_cb },
+ { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
+ "gitignore",
+ "allow explicitly ignored files to be overwritten",
+ PARSE_OPT_NONEG, exclude_per_directory_cb },
+ OPT__VERBOSE(&verbose),
+ OPT_GROUP("Merging"),
+ { OPTION_CALLBACK, 'm', NULL, &opts, NULL,
+ "perform a merge in addition to a read",
+ PARSE_OPT_NOARG | PARSE_OPT_NONEG, merge_cb },
+ { OPTION_CALLBACK, 0, "reset", &opts, NULL,
+ "same as -m, except unmerged entries are discarded",
+ PARSE_OPT_NOARG | PARSE_OPT_NONEG, reset_cb },
+ OPT_BOOLEAN('u', NULL, &update,
+ "update working tree with merge result"),
+ OPT_BOOLEAN('i', NULL, &index_only,
+ "don't check the working tree after merging"),
+ OPT_BOOLEAN(0, "trivial", &trivial_merges_only,
+ "3-way merge if no file level merging required"),
+ OPT_BOOLEAN(0, "aggressive", &aggressive,
+ "3-way merge in presence of adds and removes"),
+ OPT_END()
+ };
memset(&opts, 0, sizeof(opts));
opts.head_idx = -1;
@@ -49,104 +157,18 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
newfd = hold_locked_index(&lock_file, 1);
- for (i = 1; i < argc; i++) {
- const char *arg = argv[i];
-
- /* "-u" means "update", meaning that a merge will update
- * the working tree.
- */
- if (!strcmp(arg, "-u")) {
- opts.update = 1;
- continue;
- }
-
- if (!strcmp(arg, "-v")) {
- opts.verbose_update = 1;
- continue;
- }
-
- /* "-i" means "index only", meaning that a merge will
- * not even look at the working tree.
- */
- if (!strcmp(arg, "-i")) {
- opts.index_only = 1;
- continue;
- }
-
- if (!prefixcmp(arg, "--index-output=")) {
- set_alternate_index_output(arg + 15);
- continue;
- }
-
- /* "--prefix=<subdirectory>/" means keep the current index
- * entries and put the entries from the tree under the
- * given subdirectory.
- */
- if (!prefixcmp(arg, "--prefix=")) {
- if (stage || opts.merge || opts.prefix)
- usage(read_tree_usage);
- opts.prefix = arg + 9;
- opts.merge = 1;
- stage = 1;
- if (read_cache_unmerged())
- die("you need to resolve your current index first");
- continue;
- }
+ argc = parse_options(argc, argv, unused_prefix, read_tree_options,
+ read_tree_usage, 0);
- /* This differs from "-m" in that we'll silently ignore
- * unmerged entries and overwrite working tree files that
- * correspond to them.
- */
- if (!strcmp(arg, "--reset")) {
- if (stage || opts.merge || opts.prefix)
- usage(read_tree_usage);
- opts.reset = 1;
- opts.merge = 1;
- stage = 1;
- read_cache_unmerged();
- continue;
- }
+ opts.update = update ? 1 : 0;
+ opts.index_only = index_only ? 1 : 0;
+ opts.trivial_merges_only = trivial_merges_only ? 1 : 0;
+ opts.aggressive = aggressive ? 1 : 0;
+ opts.verbose_update = verbose ? 1 : 0;
+ stage = opts.merge;
- if (!strcmp(arg, "--trivial")) {
- opts.trivial_merges_only = 1;
- continue;
- }
-
- if (!strcmp(arg, "--aggressive")) {
- opts.aggressive = 1;
- continue;
- }
-
- /* "-m" stands for "merge", meaning we start in stage 1 */
- if (!strcmp(arg, "-m")) {
- if (stage || opts.merge || opts.prefix)
- usage(read_tree_usage);
- if (read_cache_unmerged())
- die("you need to resolve your current index first");
- stage = 1;
- opts.merge = 1;
- continue;
- }
-
- if (!prefixcmp(arg, "--exclude-per-directory=")) {
- struct dir_struct *dir;
-
- if (opts.dir)
- die("more than one --exclude-per-directory are given.");
-
- dir = xcalloc(1, sizeof(*opts.dir));
- dir->flags |= DIR_SHOW_IGNORED;
- dir->exclude_per_dir = arg + 24;
- opts.dir = dir;
- /* We do not need to nor want to do read-directory
- * here; we are merely interested in reusing the
- * per directory ignore stack mechanism.
- */
- continue;
- }
-
- if (1 < opts.index_only + opts.update)
- die("-u and -i at the same time makes no sense");
+ for (i = 0; i < argc; i++) {
+ const char *arg = argv[i];
if (get_sha1(arg, sha1))
die("Not a valid object name %s", arg);
@@ -154,6 +176,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
die("failed to unpack tree object %s", arg);
stage++;
}
+ if (1 < opts.index_only + opts.update)
+ die("-u and -i at the same time makes no sense");
if ((opts.update||opts.index_only) && !opts.merge)
die("%s is meaningless without -m",
opts.update ? "-u" : "-i");
--
1.6.3.3.334.g916e1
next prev parent reply other threads:[~2009-06-24 4:30 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-06-24 4:27 [PATCH 1/2] read-tree: convert unhelpful usage()'s to helpful die()'s Stephen Boyd
2009-06-24 4:27 ` Stephen Boyd [this message]
2009-06-24 5:08 ` [PATCH 2/2] read-tree: migrate to parse-options Junio C Hamano
2009-06-25 1:36 ` Stephen Boyd
2009-06-25 5:06 ` [PATCHv2 " Stephen Boyd
2009-06-25 6:55 ` Johannes Sixt
2009-06-26 3:15 ` Stephen Boyd
2009-06-26 5:14 ` [PATCHv3 1/2] read-tree: convert unhelpful usage()'s to helpful die()'s Stephen Boyd
2009-06-26 5:14 ` [PATCHv3 2/2] read-tree: migrate to parse-options Stephen Boyd
2009-06-26 5:29 ` Stephen Boyd
2009-06-26 17:23 ` Junio C Hamano
2009-06-27 2:00 ` Stephen Boyd
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1245817672-25483-2-git-send-email-bebarino@gmail.com \
--to=bebarino@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).