4 static const char send_pack_usage
[] = "git-send-pack [--exec=other] destination [heads]*";
5 static const char *exec
= "git-receive-pack";
9 unsigned char old_sha1
[20];
10 unsigned char new_sha1
[20];
14 static void exec_pack_objects(void)
16 static char *args
[] = {
21 execvp("git-pack-objects", args
);
22 die("git-pack-objects exec failed (%s)", strerror(errno
));
25 static void exec_rev_list(struct ref
*refs
)
27 static char *args
[1000];
30 args
[i
++] = "git-rev-list"; /* 0 */
31 args
[i
++] = "--objects"; /* 1 */
33 char *buf
= malloc(100);
35 die("git-rev-list environment overflow");
37 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
40 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
44 execvp("git-rev-list", args
);
45 die("git-rev-list exec failed (%s)", strerror(errno
));
48 static void rev_list(int fd
, struct ref
*refs
)
51 pid_t pack_objects_pid
;
53 if (pipe(pipe_fd
) < 0)
54 die("rev-list setup: pipe failed");
55 pack_objects_pid
= fork();
56 if (!pack_objects_pid
) {
63 die("pack-objects setup failed");
65 if (pack_objects_pid
< 0)
66 die("pack-objects fork failed");
74 static int pack_objects(int fd
, struct ref
*refs
)
78 rev_list_pid
= fork();
81 die("rev-list setup failed");
84 die("rev-list fork failed");
86 * We don't wait for the rev-list pipeline in the parent:
87 * we end up waiting for the other end instead
92 static int read_ref(const char *ref
, unsigned char *sha1
)
95 static char pathname
[PATH_MAX
];
97 const char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
99 snprintf(pathname
, sizeof(pathname
), "%s/%s", git_dir
, ref
);
100 fd
= open(pathname
, O_RDONLY
);
104 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
105 ret
= get_sha1_hex(buffer
, sha1
);
110 static int send_pack(int in
, int out
, int nr_match
, char **match
)
112 struct ref
*ref_list
= NULL
, **last_ref
= &ref_list
;
116 unsigned char old_sha1
[20];
117 unsigned char new_sha1
[20];
118 static char buffer
[1000];
122 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
125 if (buffer
[len
-1] == '\n')
128 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
129 die("protocol error: expected sha/ref, got '%s'", buffer
);
131 if (nr_match
&& !path_match(name
, nr_match
, match
))
133 if (read_ref(name
, new_sha1
) < 0)
134 return error("no such local reference '%s'", name
);
135 if (!has_sha1_file(old_sha1
))
136 return error("remote '%s' points to object I don't have", name
);
137 if (!memcmp(old_sha1
, new_sha1
, 20)) {
138 fprintf(stderr
, "'%s' unchanged\n", name
);
141 ref
= xmalloc(sizeof(*ref
) + len
- 40);
142 memcpy(ref
->old_sha1
, old_sha1
, 20);
143 memcpy(ref
->new_sha1
, new_sha1
, 20);
144 memcpy(ref
->name
, buffer
+ 41, len
- 40);
147 last_ref
= &ref
->next
;
150 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
151 char old_hex
[60], *new_hex
;
152 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
153 new_hex
= sha1_to_hex(ref
->new_sha1
);
154 packet_write(out
, "%s %s %s", old_hex
, new_hex
, ref
->name
);
155 fprintf(stderr
, "'%s': updating from %s to %s\n", ref
->name
, old_hex
, new_hex
);
160 pack_objects(out
, ref_list
);
165 int main(int argc
, char **argv
)
174 for (i
= 1; i
< argc
; i
++) {
178 if (!strncmp(arg
, "--exec=", 7)) {
182 usage(send_pack_usage
);
186 nr_heads
= argc
- i
-1;
190 usage(send_pack_usage
);
191 pid
= git_connect(fd
, dest
, exec
);
194 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);