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.

302 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. const struct option *orig = o;
  143. struct option *opts;
  144. int nr = 0;
  145. while (o && o->type != OPTION_END) {
  146. nr++;
  147. o++;
  148. }
  149. ALLOC_ARRAY(opts, nr + 1);
  150. COPY_ARRAY(opts, orig, nr);
  151. memset(opts + nr, 0, sizeof(*opts));
  152. opts[nr].type = OPTION_END;
  153. return opts;
  154. }
  155. struct option *parse_options_concat(struct option *a, struct option *b)
  156. {
  157. struct option *ret;
  158. size_t i, a_len = 0, b_len = 0;
  159. for (i = 0; a[i].type != OPTION_END; i++)
  160. a_len++;
  161. for (i = 0; b[i].type != OPTION_END; i++)
  162. b_len++;
  163. ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
  164. for (i = 0; i < a_len; i++)
  165. ret[i] = a[i];
  166. for (i = 0; i < b_len; i++)
  167. ret[a_len + i] = b[i];
  168. ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
  169. return ret;
  170. }
  171. int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
  172. {
  173. struct string_list *v = opt->value;
  174. if (unset) {
  175. string_list_clear(v, 0);
  176. return 0;
  177. }
  178. if (!arg)
  179. return -1;
  180. string_list_append(v, arg);
  181. return 0;
  182. }
  183. int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
  184. {
  185. return 0;
  186. }
  187. /**
  188. * Report that the option is unknown, so that other code can handle
  189. * it. This can be used as a callback together with
  190. * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
  191. * "-h" output even if it's not being handled directly by
  192. * parse_options().
  193. */
  194. enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
  195. const struct option *opt,
  196. const char *arg, int unset)
  197. {
  198. BUG_ON_OPT_ARG(arg);
  199. return PARSE_OPT_UNKNOWN;
  200. }
  201. /**
  202. * Recreates the command-line option in the strbuf.
  203. */
  204. static int recreate_opt(struct strbuf *sb, const struct option *opt,
  205. const char *arg, int unset)
  206. {
  207. strbuf_reset(sb);
  208. if (opt->long_name) {
  209. strbuf_addstr(sb, unset ? "--no-" : "--");
  210. strbuf_addstr(sb, opt->long_name);
  211. if (arg) {
  212. strbuf_addch(sb, '=');
  213. strbuf_addstr(sb, arg);
  214. }
  215. } else if (opt->short_name && !unset) {
  216. strbuf_addch(sb, '-');
  217. strbuf_addch(sb, opt->short_name);
  218. if (arg)
  219. strbuf_addstr(sb, arg);
  220. } else
  221. return -1;
  222. return 0;
  223. }
  224. /**
  225. * For an option opt, recreates the command-line option in opt->value which
  226. * must be an char* initialized to NULL. This is useful when we need to pass
  227. * the command-line option to another command. Since any previous value will be
  228. * overwritten, this callback should only be used for options where the last
  229. * one wins.
  230. */
  231. int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
  232. {
  233. static struct strbuf sb = STRBUF_INIT;
  234. char **opt_value = opt->value;
  235. if (recreate_opt(&sb, opt, arg, unset) < 0)
  236. return -1;
  237. free(*opt_value);
  238. *opt_value = strbuf_detach(&sb, NULL);
  239. return 0;
  240. }
  241. /**
  242. * For an option opt, recreate the command-line option, appending it to
  243. * opt->value which must be a argv_array. This is useful when we need to pass
  244. * the command-line option, which can be specified multiple times, to another
  245. * command.
  246. */
  247. int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
  248. {
  249. static struct strbuf sb = STRBUF_INIT;
  250. struct argv_array *opt_value = opt->value;
  251. if (recreate_opt(&sb, opt, arg, unset) < 0)
  252. return -1;
  253. argv_array_push(opt_value, sb.buf);
  254. return 0;
  255. }