upload-pack.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "cache.h"
  2. #include "builtin.h"
  3. #include "exec-cmd.h"
  4. #include "pkt-line.h"
  5. #include "parse-options.h"
  6. #include "protocol.h"
  7. #include "upload-pack.h"
  8. #include "serve.h"
  9. static const char * const upload_pack_usage[] = {
  10. N_("git upload-pack [<options>] <dir>"),
  11. NULL
  12. };
  13. int cmd_upload_pack(int argc, const char **argv, const char *prefix)
  14. {
  15. const char *dir;
  16. int strict = 0;
  17. struct upload_pack_options opts = { 0 };
  18. struct serve_options serve_opts = SERVE_OPTIONS_INIT;
  19. struct option options[] = {
  20. OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc,
  21. N_("quit after a single request/response exchange")),
  22. OPT_BOOL(0, "advertise-refs", &opts.advertise_refs,
  23. N_("exit immediately after initial ref advertisement")),
  24. OPT_BOOL(0, "strict", &strict,
  25. N_("do not try <directory>/.git/ if <directory> is no Git directory")),
  26. OPT_INTEGER(0, "timeout", &opts.timeout,
  27. N_("interrupt transfer after <n> seconds of inactivity")),
  28. OPT_END()
  29. };
  30. packet_trace_identity("upload-pack");
  31. read_replace_refs = 0;
  32. argc = parse_options(argc, argv, prefix, options, upload_pack_usage, 0);
  33. if (argc != 1)
  34. usage_with_options(upload_pack_usage, options);
  35. if (opts.timeout)
  36. opts.daemon_mode = 1;
  37. setup_path();
  38. dir = argv[0];
  39. if (!enter_repo(dir, strict))
  40. die("'%s' does not appear to be a git repository", dir);
  41. switch (determine_protocol_version_server()) {
  42. case protocol_v2:
  43. serve_opts.advertise_capabilities = opts.advertise_refs;
  44. serve_opts.stateless_rpc = opts.stateless_rpc;
  45. serve(&serve_opts);
  46. break;
  47. case protocol_v1:
  48. /*
  49. * v1 is just the original protocol with a version string,
  50. * so just fall through after writing the version string.
  51. */
  52. if (opts.advertise_refs || !opts.stateless_rpc)
  53. packet_write_fmt(1, "version 1\n");
  54. /* fallthrough */
  55. case protocol_v0:
  56. upload_pack(&opts);
  57. break;
  58. case protocol_unknown_version:
  59. BUG("unknown protocol version");
  60. }
  61. return 0;
  62. }