Make "git clone" less of a deathly quiet experience
[git/jnareb-git.git] / fetch-clone.c
blobb67d9764978cc5758612884e2a906237d349795e
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include <sys/wait.h>
4 #include <sys/time.h>
6 static int finish_pack(const char *pack_tmp_name, const char *me)
8 int pipe_fd[2];
9 pid_t pid;
10 char idx[PATH_MAX];
11 char final[PATH_MAX];
12 char hash[41];
13 unsigned char sha1[20];
14 char *cp;
15 int err = 0;
17 if (pipe(pipe_fd) < 0)
18 die("%s: unable to set up pipe", me);
20 strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
21 cp = strrchr(idx, '/');
22 memcpy(cp, "/pidx", 5);
24 pid = fork();
25 if (pid < 0)
26 die("git-clone-pack: unable to fork off git-index-pack");
27 if (!pid) {
28 close(0);
29 dup2(pipe_fd[1], 1);
30 close(pipe_fd[0]);
31 close(pipe_fd[1]);
32 execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
33 error("cannot exec git-index-pack <%s> <%s>",
34 idx, pack_tmp_name);
35 exit(1);
37 close(pipe_fd[1]);
38 if (read(pipe_fd[0], hash, 40) != 40) {
39 error("%s: unable to read from git-index-pack", me);
40 err = 1;
42 close(pipe_fd[0]);
44 for (;;) {
45 int status, code;
46 int retval = waitpid(pid, &status, 0);
48 if (retval < 0) {
49 if (errno == EINTR)
50 continue;
51 error("waitpid failed (%s)", strerror(errno));
52 goto error_die;
54 if (WIFSIGNALED(status)) {
55 int sig = WTERMSIG(status);
56 error("git-index-pack died of signal %d", sig);
57 goto error_die;
59 if (!WIFEXITED(status)) {
60 error("git-index-pack died of unnatural causes %d",
61 status);
62 goto error_die;
64 code = WEXITSTATUS(status);
65 if (code) {
66 error("git-index-pack died with error code %d", code);
67 goto error_die;
69 if (err)
70 goto error_die;
71 break;
73 hash[40] = 0;
74 if (get_sha1_hex(hash, sha1)) {
75 error("git-index-pack reported nonsense '%s'", hash);
76 goto error_die;
78 /* Now we have pack in pack_tmp_name[], and
79 * idx in idx[]; rename them to their final names.
81 snprintf(final, sizeof(final),
82 "%s/pack/pack-%s.pack", get_object_directory(), hash);
83 move_temp_to_file(pack_tmp_name, final);
84 chmod(final, 0444);
85 snprintf(final, sizeof(final),
86 "%s/pack/pack-%s.idx", get_object_directory(), hash);
87 move_temp_to_file(idx, final);
88 chmod(final, 0444);
89 return 0;
91 error_die:
92 unlink(idx);
93 unlink(pack_tmp_name);
94 exit(1);
97 int receive_unpack_pack(int fd[2], const char *me, int quiet)
99 int status;
100 pid_t pid;
102 pid = fork();
103 if (pid < 0)
104 die("%s: unable to fork off git-unpack-objects", me);
105 if (!pid) {
106 dup2(fd[0], 0);
107 close(fd[0]);
108 close(fd[1]);
109 execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
110 die("git-unpack-objects exec failed");
112 close(fd[0]);
113 close(fd[1]);
114 while (waitpid(pid, &status, 0) < 0) {
115 if (errno != EINTR)
116 die("waiting for git-unpack-objects: %s",
117 strerror(errno));
119 if (WIFEXITED(status)) {
120 int code = WEXITSTATUS(status);
121 if (code)
122 die("git-unpack-objects died with error code %d",
123 code);
124 return 0;
126 if (WIFSIGNALED(status)) {
127 int sig = WTERMSIG(status);
128 die("git-unpack-objects died of signal %d", sig);
130 die("git-unpack-objects died of unnatural causes %d", status);
133 int receive_keep_pack(int fd[2], const char *me, int quiet)
135 char tmpfile[PATH_MAX];
136 int ofd, ifd;
137 unsigned long total;
138 static struct timeval prev_tv;
140 ifd = fd[0];
141 snprintf(tmpfile, sizeof(tmpfile),
142 "%s/pack/tmp-XXXXXX", get_object_directory());
143 ofd = mkstemp(tmpfile);
144 if (ofd < 0)
145 return error("unable to create temporary file %s", tmpfile);
147 gettimeofday(&prev_tv, NULL);
148 total = 0;
149 while (1) {
150 char buf[8192];
151 ssize_t sz, wsz, pos;
152 sz = read(ifd, buf, sizeof(buf));
153 if (sz == 0)
154 break;
155 if (sz < 0) {
156 error("error reading pack (%s)", strerror(errno));
157 close(ofd);
158 unlink(tmpfile);
159 return -1;
161 pos = 0;
162 while (pos < sz) {
163 wsz = write(ofd, buf + pos, sz - pos);
164 if (wsz < 0) {
165 error("error writing pack (%s)",
166 strerror(errno));
167 close(ofd);
168 unlink(tmpfile);
169 return -1;
171 pos += wsz;
173 total += sz;
174 if (!quiet) {
175 static unsigned long last;
176 struct timeval tv;
177 unsigned long diff = total - last;
178 /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
179 unsigned long msecs;
181 gettimeofday(&tv, NULL);
182 msecs = tv.tv_sec - prev_tv.tv_sec;
183 msecs <<= 10;
184 msecs += (int)(tv.tv_usec - prev_tv.tv_usec) >> 10;
185 if (msecs > 500) {
186 prev_tv = tv;
187 last = total;
188 fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
189 total >> 20,
190 1000*((total >> 10) & 1023)>>10,
191 diff / msecs );
195 close(ofd);
196 return finish_pack(tmpfile, me);