archive.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006 Franck Bui-Huu
  3. * Copyright (c) 2006 Rene Scharfe
  4. */
  5. #include "cache.h"
  6. #include "builtin.h"
  7. #include "archive.h"
  8. #include "transport.h"
  9. #include "parse-options.h"
  10. #include "pkt-line.h"
  11. #include "sideband.h"
  12. static void create_output_file(const char *output_file)
  13. {
  14. int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  15. if (output_fd < 0)
  16. die_errno(_("could not create archive file '%s'"), output_file);
  17. if (output_fd != 1) {
  18. if (dup2(output_fd, 1) < 0)
  19. die_errno(_("could not redirect output"));
  20. else
  21. close(output_fd);
  22. }
  23. }
  24. static int run_remote_archiver(int argc, const char **argv,
  25. const char *remote, const char *exec,
  26. const char *name_hint)
  27. {
  28. int fd[2], i, rv;
  29. struct transport *transport;
  30. struct remote *_remote;
  31. struct packet_reader reader;
  32. _remote = remote_get(remote);
  33. if (!_remote->url[0])
  34. die(_("git archive: Remote with no URL"));
  35. transport = transport_get(_remote, _remote->url[0]);
  36. transport_connect(transport, "git-upload-archive", exec, fd);
  37. /*
  38. * Inject a fake --format field at the beginning of the
  39. * arguments, with the format inferred from our output
  40. * filename. This way explicit --format options can override
  41. * it.
  42. */
  43. if (name_hint) {
  44. const char *format = archive_format_from_filename(name_hint);
  45. if (format)
  46. packet_write_fmt(fd[1], "argument --format=%s\n", format);
  47. }
  48. for (i = 1; i < argc; i++)
  49. packet_write_fmt(fd[1], "argument %s\n", argv[i]);
  50. packet_flush(fd[1]);
  51. packet_reader_init(&reader, fd[0], NULL, 0,
  52. PACKET_READ_CHOMP_NEWLINE |
  53. PACKET_READ_DIE_ON_ERR_PACKET);
  54. if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
  55. die(_("git archive: expected ACK/NAK, got a flush packet"));
  56. if (strcmp(reader.line, "ACK")) {
  57. if (starts_with(reader.line, "NACK "))
  58. die(_("git archive: NACK %s"), reader.line + 5);
  59. die(_("git archive: protocol error"));
  60. }
  61. if (packet_reader_read(&reader) != PACKET_READ_FLUSH)
  62. die(_("git archive: expected a flush"));
  63. /* Now, start reading from fd[0] and spit it out to stdout */
  64. rv = recv_sideband("archive", fd[0], 1);
  65. rv |= transport_disconnect(transport);
  66. return !!rv;
  67. }
  68. #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
  69. PARSE_OPT_KEEP_ARGV0 | \
  70. PARSE_OPT_KEEP_UNKNOWN | \
  71. PARSE_OPT_NO_INTERNAL_HELP )
  72. int cmd_archive(int argc, const char **argv, const char *prefix)
  73. {
  74. const char *exec = "git-upload-archive";
  75. const char *output = NULL;
  76. const char *remote = NULL;
  77. struct option local_opts[] = {
  78. OPT_FILENAME('o', "output", &output,
  79. N_("write the archive to this file")),
  80. OPT_STRING(0, "remote", &remote, N_("repo"),
  81. N_("retrieve the archive from remote repository <repo>")),
  82. OPT_STRING(0, "exec", &exec, N_("command"),
  83. N_("path to the remote git-upload-archive command")),
  84. OPT_END()
  85. };
  86. argc = parse_options(argc, argv, prefix, local_opts, NULL,
  87. PARSE_OPT_KEEP_ALL);
  88. init_archivers();
  89. if (output)
  90. create_output_file(output);
  91. if (remote)
  92. return run_remote_archiver(argc, argv, remote, exec, output);
  93. setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
  94. return write_archive(argc, argv, prefix, the_repository, output, 0);
  95. }