OPT_END()
};
-static void check_attr(int cnt, struct git_attr_check *check,
- const char** name, const char *file)
+static void output_attr(int cnt, struct git_attr_check *check,
+ const char *file)
{
int j;
- if (git_checkattr(file, cnt, check))
- die("git_checkattr died");
for (j = 0; j < cnt; j++) {
const char *value = check[j].value;
value = "unspecified";
quote_c_style(file, NULL, stdout, 0);
- printf(": %s: %s\n", name[j], value);
+ printf(": %s: %s\n", git_attr_name(check[j].attr), value);
}
}
-static void check_attr_stdin_paths(int cnt, struct git_attr_check *check,
- const char** name)
+static void check_attr(int cnt, struct git_attr_check *check,
+ const char *file)
+{
+ if (git_checkattr(file, cnt, check))
+ die("git_checkattr died");
+ output_attr(cnt, check, file);
+}
+
+static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
{
struct strbuf buf, nbuf;
int line_termination = null_term_line ? 0 : '\n';
die("line is badly quoted");
strbuf_swap(&buf, &nbuf);
}
- check_attr(cnt, check, name, buf.buf);
+ check_attr(cnt, check, buf.buf);
maybe_flush_or_die(stdout, "attribute to stdout");
}
strbuf_release(&buf);
strbuf_release(&nbuf);
}
+static NORETURN void error_with_usage(const char *msg)
+{
+ error("%s", msg);
+ usage_with_options(check_attr_usage, check_attr_options);
+}
+
int cmd_check_attr(int argc, const char **argv, const char *prefix)
{
struct git_attr_check *check;
- int cnt, i, doubledash;
- const char *errstr = NULL;
+ int cnt, i, doubledash, filei;
argc = parse_options(argc, argv, prefix, check_attr_options,
check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
- if (!argc)
- usage_with_options(check_attr_usage, check_attr_options);
if (read_cache() < 0) {
die("invalid cache");
doubledash = i;
}
- /* If there is no double dash, we handle only one attribute */
- if (doubledash < 0) {
+ /* Check attribute argument(s): */
+ if (doubledash == 0) {
+ error_with_usage("No attribute specified");
+ } else if (doubledash < 0) {
+ /*
+ * There is no double dash; treat the first
+ * argument as an attribute.
+ */
+ if (!argc)
+ error_with_usage("No attribute specified");
+
cnt = 1;
- doubledash = 0;
- } else
+ filei = 1;
+ } else {
cnt = doubledash;
- doubledash++;
-
- if (cnt <= 0)
- errstr = "No attribute specified";
- else if (stdin_paths && doubledash < argc)
- errstr = "Can't specify files with --stdin";
- if (errstr) {
- error("%s", errstr);
- usage_with_options(check_attr_usage, check_attr_options);
+ filei = doubledash + 1;
}
+ /* Check file argument(s): */
+ if (stdin_paths && filei < argc)
+ error_with_usage("Can't specify files with --stdin");
+
check = xcalloc(cnt, sizeof(*check));
for (i = 0; i < cnt; i++) {
const char *name;
}
if (stdin_paths)
- check_attr_stdin_paths(cnt, check, argv);
+ check_attr_stdin_paths(cnt, check);
else {
- for (i = doubledash; i < argc; i++)
- check_attr(cnt, check, argv, argv[i]);
+ for (i = filei; i < argc; i++)
+ check_attr(cnt, check, argv[i]);
maybe_flush_or_die(stdout, "attribute to stdout");
}
return 0;