]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-archive.c
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
10 #include "tree-walk.h"
14 static const char archive_usage
[] = \
15 "git-archive --format=<fmt> [--prefix=<prefix>/] [<extra>] <tree-ish> [path...]";
17 struct archiver archivers
[] = {
21 static int run_remote_archiver(struct archiver
*ar
, int argc
,
25 int fd
[2], i
, len
, rv
;
28 sprintf(buf
, "git-upload-archive");
30 url
= xstrdup(ar
->remote
);
31 pid
= git_connect(fd
, url
, buf
);
35 for (i
= 1; i
< argc
; i
++) {
36 if (!strncmp(argv
[i
], "--remote=", 9))
38 packet_write(fd
[1], "argument %s\n", argv
[i
]);
42 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
44 die("git-archive: expected ACK/NAK, got EOF");
45 if (buf
[len
-1] == '\n')
47 if (strcmp(buf
, "ACK")) {
48 if (len
> 5 && !strncmp(buf
, "NACK ", 5))
49 die("git-archive: NACK %s", buf
+ 5);
50 die("git-archive: protocol error");
53 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
55 die("git-archive: expected a flush");
57 /* Now, start reading from fd[0] and spit it out to stdout */
58 rv
= copy_fd(fd
[0], 1);
61 rv
|= finish_connect(pid
);
66 static int init_archiver(const char *name
, struct archiver
*ar
)
70 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
71 if (!strcmp(name
, archivers
[i
].name
)) {
72 memcpy(ar
, &archivers
[i
], sizeof(struct archiver
));
80 void parse_pathspec_arg(const char **pathspec
, struct archiver_args
*ar_args
)
82 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
85 void parse_treeish_arg(const char **argv
, struct archiver_args
*ar_args
,
88 const char *name
= argv
[0];
89 const unsigned char *commit_sha1
;
92 struct commit
*commit
;
93 unsigned char sha1
[20];
95 if (get_sha1(name
, sha1
))
96 die("Not a valid object name");
98 commit
= lookup_commit_reference_gently(sha1
, 1);
100 commit_sha1
= commit
->object
.sha1
;
101 archive_time
= commit
->date
;
104 archive_time
= time(NULL
);
107 tree
= parse_tree_indirect(sha1
);
109 die("not a tree object");
112 unsigned char tree_sha1
[20];
116 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
118 if (err
|| !S_ISDIR(mode
))
119 die("current working directory is untracked");
122 tree
= parse_tree_indirect(tree_sha1
);
124 ar_args
->tree
= tree
;
125 ar_args
->commit_sha1
= commit_sha1
;
126 ar_args
->time
= archive_time
;
129 static const char *default_parse_extra(struct archiver
*ar
,
134 snprintf(msg
, sizeof(msg
) - 4, "'%s' format does not handle %s",
137 return strcat(msg
, "...");
140 int parse_archive_args(int argc
, const char **argv
, struct archiver
*ar
)
142 const char *extra_argv
[MAX_EXTRA_ARGS
];
144 const char *format
= NULL
; /* might want to default to "tar" */
145 const char *remote
= NULL
;
146 const char *base
= "";
150 for (i
= 1; i
< argc
; i
++) {
151 const char *arg
= argv
[i
];
153 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
157 if (!strncmp(arg
, "--format=", 9)) {
161 if (!strncmp(arg
, "--prefix=", 9)) {
165 if (!strncmp(arg
, "--remote=", 9)) {
169 if (!strcmp(arg
, "--")) {
174 if (extra_argc
> MAX_EXTRA_ARGS
- 1)
175 die("Too many extra options");
176 extra_argv
[extra_argc
++] = arg
;
184 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
185 printf("%s\n", archivers
[i
].name
);
188 die("--list and --remote are mutually exclusive");
192 usage(archive_usage
);
194 die("You must specify an archive format");
195 if (init_archiver(format
, ar
) < 0)
196 die("Unknown archive format '%s'", format
);
198 if (extra_argc
&& !remote
) {
199 if (!ar
->parse_extra
) {
200 die("%s", default_parse_extra(ar
, extra_argv
));
202 ar
->args
.extra
= ar
->parse_extra(extra_argc
, extra_argv
);
205 ar
->args
.base
= base
;
210 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
215 tree_idx
= parse_archive_args(argc
, argv
, &ar
);
218 return run_remote_archiver(&ar
, argc
, argv
);
221 prefix
= setup_git_directory();
224 parse_treeish_arg(argv
, &ar
.args
, prefix
);
225 parse_pathspec_arg(argv
+ 1, &ar
.args
);
227 return ar
.write_archive(&ar
.args
);