38
39
static void exec_rev_list(struct ref *refs)
40
{
41
- struct ref *ref;
42
- static const char *args[1000];
43
- int i = 0, j;
41
+ static const char *args[4];
42
+ int i = 0;
43
44
args[i++] = "rev-list"; /* 0 */
45
if (use_thin_pack) /* 1 */
47
else
48
args[i++] = "--objects";
49
51
- /* First send the ones we care about most */
52
- for (ref = refs; ref; ref = ref->next) {
53
- if (900 < i)
54
- die("git-rev-list environment overflow");
55
- if (!is_zero_sha1(ref->new_sha1)) {
56
- char *buf = xmalloc(100);
57
- args[i++] = buf;
58
- snprintf(buf, 50, "%s", sha1_to_hex(ref->new_sha1));
59
- buf += 50;
60
- if (!is_zero_sha1(ref->old_sha1) &&
61
- has_sha1_file(ref->old_sha1)) {
62
- args[i++] = buf;
63
- snprintf(buf, 50, "^%s",
64
- sha1_to_hex(ref->old_sha1));
65
- }
66
- }
67
- }
50
+ args[i++] = "--stdin";
51
69
- /* Then a handful of the remainder
70
- * NEEDSWORK: we would be better off if used the newer ones first.
71
- */
72
- for (ref = refs, j = i + 16;
73
- i < 900 && i < j && ref;
74
- ref = ref->next) {
75
- if (is_zero_sha1(ref->new_sha1) &&
76
- !is_zero_sha1(ref->old_sha1) &&
77
- has_sha1_file(ref->old_sha1)) {
78
- char *buf = xmalloc(42);
79
- args[i++] = buf;
80
- snprintf(buf, 42, "^%s", sha1_to_hex(ref->old_sha1));
81
- }
82
- }
52
args[i] = NULL;
53
execv_git_cmd(args);
54
die("git-rev-list exec failed (%s)", strerror(errno));
55
}
56
57
+/*
58
+ * Run "rev-list --stdin | pack-objects" pipe.
59
+ */
60
static void rev_list(int fd, struct ref *refs)
61
{
62
int pipe_fd[2];
66
die("rev-list setup: pipe failed");
67
pack_objects_pid = fork();
68
if (!pack_objects_pid) {
69
+ /* The child becomes pack-objects; reads from pipe
70
+ * and writes to the original fd
71
+ */
72
dup2(pipe_fd[0], 0);
73
dup2(fd, 1);
74
close(pipe_fd[0]);
79
}
80
if (pack_objects_pid < 0)
81
die("pack-objects fork failed");
82
+
83
+ /* We become rev-list --stdin; output goes to pipe. */
84
dup2(pipe_fd[1], 1);
85
close(pipe_fd[0]);
86
close(pipe_fd[1]);
88
exec_rev_list(refs);
89
}
90
91
+/*
92
+ * Create "rev-list --stdin | pack-objects" pipe and feed
93
+ * the refs into the pipeline.
94
+ */
95
+static void rev_list_generate(int fd, struct ref *refs)
96
+{
97
+ int pipe_fd[2];
98
+ pid_t rev_list_generate_pid;
99
+
100
+ if (pipe(pipe_fd) < 0)
101
+ die("rev-list-generate setup: pipe failed");
102
+ rev_list_generate_pid = fork();
103
+ if (!rev_list_generate_pid) {
104
+ /* The child becomes the "rev-list | pack-objects"
105
+ * pipeline. It takes input from us, and its output
106
+ * goes to fd.
107
+ */
108
+ dup2(pipe_fd[0], 0);
109
+ dup2(fd, 1);
110
+ close(pipe_fd[0]);
111
+ close(pipe_fd[1]);
112
+ close(fd);
113
+ rev_list(fd, refs);
114
+ die("rev-list setup failed");
115
+ }
116
+ if (rev_list_generate_pid < 0)
117
+ die("rev-list-generate fork failed");
118
+
119
+ /* We feed the rev parameters to them. We do not write into
120
+ * fd nor read from the pipe.
121
+ */
122
+ close(pipe_fd[0]);
123
+ close(fd);
124
+ while (refs) {
125
+ char buf[42];
126
+
127
+ if (!is_null_sha1(refs->old_sha1) &&
128
+ has_sha1_file(refs->old_sha1)) {
129
+ memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
130
+ buf[0] = '^';
131
+ buf[41] = '\n';
132
+ write(pipe_fd[1], buf, 42);
133
+ }
134
+ if (!is_null_sha1(refs->new_sha1)) {
135
+ memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
136
+ buf[40] = '\n';
137
+ write(pipe_fd[1], buf, 41);
138
+ }
139
+ refs = refs->next;
140
+ }
141
+ close(pipe_fd[1]);
142
+ // waitpid(rev_list_generate_pid);
143
+ exit(0);
144
+}
145
+
146
+/*
147
+ * Make a pack stream and spit it out into file descriptor fd
148
+ */
149
static void pack_objects(int fd, struct ref *refs)
150
{
151
pid_t rev_list_pid;
152
153
rev_list_pid = fork();
154
if (!rev_list_pid) {
120
- rev_list(fd, refs);
155
+ rev_list_generate(fd, refs);
156
die("rev-list setup failed");
157
}
158
if (rev_list_pid < 0)