THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
6.5KB

  1. #include "git-compat-util.h"
  2. #include "parse-options.h"
  3. #include "cache.h"
  4. #include "commit.h"
  5. #include "color.h"
  6. #include "string-list.h"
  7. #include "argv-array.h"
  8. #include "sha1-array.h"
  9. /*----- some often used options -----*/
  10. int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
  11. {
  12. int v;
  13. if (!arg) {
  14. v = unset ? 0 : DEFAULT_ABBREV;
  15. } else {
  16. if (!*arg)
  17. return error(_("option `%s' expects a numerical value"),
  18. opt->long_name);
  19. v = strtol(arg, (char **)&arg, 10);
  20. if (*arg)
  21. return error(_("option `%s' expects a numerical value"),
  22. opt->long_name);
  23. if (v && v < MINIMUM_ABBREV)
  24. v = MINIMUM_ABBREV;
  25. else if (v > the_hash_algo->hexsz)
  26. v = the_hash_algo->hexsz;
  27. }
  28. *(int *)(opt->value) = v;
  29. return 0;
  30. }
  31. int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
  32. int unset)
  33. {
  34. if (unset)
  35. arg = "never";
  36. if (parse_expiry_date(arg, (timestamp_t *)opt->value))
  37. die(_("malformed expiration date '%s'"), arg);
  38. return 0;
  39. }
  40. int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
  41. int unset)
  42. {
  43. int value;
  44. if (!arg)
  45. arg = unset ? "never" : (const char *)opt->defval;
  46. value = git_config_colorbool(NULL, arg);
  47. if (value < 0)
  48. return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
  49. opt->long_name);
  50. *(int *)opt->value = value;
  51. return 0;
  52. }
  53. int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
  54. int unset)
  55. {
  56. int *target = opt->value;
  57. BUG_ON_OPT_ARG(arg);
  58. if (unset)
  59. /* --no-quiet, --no-verbose */
  60. *target = 0;
  61. else if (opt->short_name == 'v') {
  62. if (*target >= 0)
  63. (*target)++;
  64. else
  65. *target = 1;
  66. } else {
  67. if (*target <= 0)
  68. (*target)--;
  69. else
  70. *target = -1;
  71. }
  72. return 0;
  73. }
  74. int parse_opt_commits(const struct option *opt, const char *arg, int unset)
  75. {
  76. struct object_id oid;
  77. struct commit *commit;
  78. BUG_ON_OPT_NEG(unset);
  79. if (!arg)
  80. return -1;
  81. if (get_oid(arg, &oid))
  82. return error("malformed object name %s", arg);
  83. commit = lookup_commit_reference(the_repository, &oid);
  84. if (!commit)
  85. return error("no such commit %s", arg);
  86. commit_list_insert(commit, opt->value);
  87. return 0;
  88. }
  89. int parse_opt_commit(const struct option *opt, const char *arg, int unset)
  90. {
  91. struct object_id oid;
  92. struct commit *commit;
  93. struct commit **target = opt->value;
  94. if (!arg)
  95. return -1;
  96. if (get_oid(arg, &oid))
  97. return error("malformed object name %s", arg);
  98. commit = lookup_commit_reference(the_repository, &oid);
  99. if (!commit)
  100. return error("no such commit %s", arg);
  101. *target = commit;
  102. return 0;
  103. }
  104. int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
  105. {
  106. struct object_id oid;
  107. if (unset) {
  108. oid_array_clear(opt->value);
  109. return 0;
  110. }
  111. if (!arg)
  112. return -1;
  113. if (get_oid(arg, &oid))
  114. return error(_("malformed object name '%s'"), arg);
  115. oid_array_append(opt->value, &oid);
  116. return 0;
  117. }
  118. int parse_opt_object_id(const struct option *opt, const char *arg, int unset)
  119. {
  120. struct object_id oid;
  121. struct object_id *target = opt->value;
  122. if (unset) {
  123. *target = null_oid;
  124. return 0;
  125. }
  126. if (!arg)
  127. return -1;
  128. if (get_oid(arg, &oid))
  129. return error(_("malformed object name '%s'"), arg);
  130. *target = oid;
  131. return 0;
  132. }
  133. int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
  134. {
  135. int *target = opt->value;
  136. BUG_ON_OPT_ARG(arg);
  137. *target = unset ? 2 : 1;
  138. return 0;
  139. }
  140. struct option *parse_options_dup(const struct option *o)
  141. {
  142. struct option *opts;
  143. int nr = 0;
  144. while (o && o->type != OPTION_END) {
  145. nr++;
  146. o++;
  147. }
  148. ALLOC_ARRAY(opts, nr + 1);
  149. memcpy(opts, o - nr, sizeof(*o) * nr);
  150. memset(opts + nr, 0, sizeof(*opts));
  151. opts[nr].type = OPTION_END;
  152. return opts;
  153. }
  154. struct option *parse_options_concat(struct option *a, struct option *b)
  155. {
  156. struct option *ret;
  157. size_t i, a_len = 0, b_len = 0;
  158. for (i = 0; a[i].type != OPTION_END; i++)
  159. a_len++;
  160. for (i = 0; b[i].type != OPTION_END; i++)
  161. b_len++;
  162. ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
  163. for (i = 0; i < a_len; i++)
  164. ret[i] = a[i];
  165. for (i = 0; i < b_len; i++)
  166. ret[a_len + i] = b[i];
  167. ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
  168. return ret;
  169. }
  170. int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
  171. {
  172. struct string_list *v = opt->value;
  173. if (unset) {
  174. string_list_clear(v, 0);
  175. return 0;
  176. }
  177. if (!arg)
  178. return -1;
  179. string_list_append(v, arg);
  180. return 0;
  181. }
  182. int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
  183. {
  184. return 0;
  185. }
  186. /**
  187. * Report that the option is unknown, so that other code can handle
  188. * it. This can be used as a callback together with
  189. * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
  190. * "-h" output even if it's not being handled directly by
  191. * parse_options().
  192. */
  193. enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
  194. const struct option *opt,
  195. const char *arg, int unset)
  196. {
  197. BUG_ON_OPT_ARG(arg);
  198. return PARSE_OPT_UNKNOWN;
  199. }
  200. /**
  201. * Recreates the command-line option in the strbuf.
  202. */
  203. static int recreate_opt(struct strbuf *sb, const struct option *opt,
  204. const char *arg, int unset)
  205. {
  206. strbuf_reset(sb);
  207. if (opt->long_name) {
  208. strbuf_addstr(sb, unset ? "--no-" : "--");
  209. strbuf_addstr(sb, opt->long_name);
  210. if (arg) {
  211. strbuf_addch(sb, '=');
  212. strbuf_addstr(sb, arg);
  213. }
  214. } else if (opt->short_name && !unset) {
  215. strbuf_addch(sb, '-');
  216. strbuf_addch(sb, opt->short_name);
  217. if (arg)
  218. strbuf_addstr(sb, arg);
  219. } else
  220. return -1;
  221. return 0;
  222. }
  223. /**
  224. * For an option opt, recreates the command-line option in opt->value which
  225. * must be an char* initialized to NULL. This is useful when we need to pass
  226. * the command-line option to another command. Since any previous value will be
  227. * overwritten, this callback should only be used for options where the last
  228. * one wins.
  229. */
  230. int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
  231. {
  232. static struct strbuf sb = STRBUF_INIT;
  233. char **opt_value = opt->value;
  234. if (recreate_opt(&sb, opt, arg, unset) < 0)
  235. return -1;
  236. free(*opt_value);
  237. *opt_value = strbuf_detach(&sb, NULL);
  238. return 0;
  239. }
  240. /**
  241. * For an option opt, recreate the command-line option, appending it to
  242. * opt->value which must be a argv_array. This is useful when we need to pass
  243. * the command-line option, which can be specified multiple times, to another
  244. * command.
  245. */
  246. int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
  247. {
  248. static struct strbuf sb = STRBUF_INIT;
  249. struct argv_array *opt_value = opt->value;
  250. if (recreate_opt(&sb, opt, arg, unset) < 0)
  251. return -1;
  252. argv_array_push(opt_value, sb.buf);
  253. return 0;
  254. }