fetch-pack: Use a strbuf to compose the want list
This change is being offered as a refactoring to make later commits in the smart HTTP series easier. By changing the enabled capabilities to be formatted in a strbuf it is easier to add a new capability to the set of supported capabilities. By formatting the want portion of the request into a strbuf and writing it as a whole block we can later decide to hold onto the req_buf (instead of releasing it) to recycle in stateless communications. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Shawn O. Pearce committed
Oct 30, 2009 at 17:47 UTC
edace6f02eeae6f4a06ed1e4f6308703523d8535
3 files changed
+39
-25
builtin-fetch-pack.c
+34
-18
@@ -165,6 +165,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
165
const unsigned char *sha1;
166
unsigned in_vain = 0;
167
int got_continue = 0;
168
+ struct strbuf req_buf = STRBUF_INIT;
169
170
if (marked)
171
for_each_ref(clear_marks, NULL);
@@ -175,6 +176,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
176
fetching = 0;
177
for ( ; refs ; refs = refs->next) {
178
unsigned char *remote = refs->old_sha1;
179
+ const char *remote_hex;
180
struct object *o;
181
182
/*
@@ -192,27 +194,36 @@ static int find_common(int fd[2], unsigned char *result_sha1,
194
continue;
195
}
196
195
- if (!fetching)
196
- packet_write(fd[1], "want %s%s%s%s%s%s%s%s\n",
197
- sha1_to_hex(remote),
198
- (multi_ack ? " multi_ack" : ""),
199
- (use_sideband == 2 ? " side-band-64k" : ""),
200
- (use_sideband == 1 ? " side-band" : ""),
201
- (args.use_thin_pack ? " thin-pack" : ""),
202
- (args.no_progress ? " no-progress" : ""),
203
- (args.include_tag ? " include-tag" : ""),
204
- (prefer_ofs_delta ? " ofs-delta" : ""));
205
- else
206
- packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
197
+ remote_hex = sha1_to_hex(remote);
198
+ if (!fetching) {
199
+ struct strbuf c = STRBUF_INIT;
200
+ if (multi_ack) strbuf_addstr(&c, " multi_ack");
201
+ if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
202
+ if (use_sideband == 1) strbuf_addstr(&c, " side-band");
203
+ if (args.use_thin_pack) strbuf_addstr(&c, " thin-pack");
204
+ if (args.no_progress) strbuf_addstr(&c, " no-progress");
205
+ if (args.include_tag) strbuf_addstr(&c, " include-tag");
206
+ if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
207
+ packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
208
+ strbuf_release(&c);
209
+ } else
210
+ packet_buf_write(&req_buf, "want %s\n", remote_hex);
211
fetching++;
212
}
213
+
214
+ if (!fetching) {
215
+ strbuf_release(&req_buf);
216
+ packet_flush(fd[1]);
217
+ return 1;
218
+ }
219
+
220
if (is_repository_shallow())
210
- write_shallow_commits(fd[1], 1);
221
+ write_shallow_commits(&req_buf, 1);
222
if (args.depth > 0)
212
- packet_write(fd[1], "deepen %d", args.depth);
213
- packet_flush(fd[1]);
214
- if (!fetching)
215
- return 1;
223
+ packet_buf_write(&req_buf, "deepen %d", args.depth);
224
+ packet_buf_flush(&req_buf);
225
+
226
+ safe_write(fd[1], req_buf.buf, req_buf.len);
227
228
if (args.depth > 0) {
229
char line[1024];
@@ -296,6 +307,8 @@ done:
307
multi_ack = 0;
308
flushes++;
309
}
310
+ strbuf_release(&req_buf);
311
+
312
while (flushes || multi_ack) {
313
int ack = get_ack(fd[0], result_sha1);
314
if (ack) {
@@ -809,6 +822,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
822
823
if (args.depth > 0) {
824
struct cache_time mtime;
825
+ struct strbuf sb = STRBUF_INIT;
826
char *shallow = git_path("shallow");
827
int fd;
828
@@ -826,12 +840,14 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
840
841
fd = hold_lock_file_for_update(&lock, shallow,
842
LOCK_DIE_ON_ERROR);
829
- if (!write_shallow_commits(fd, 0)) {
843
+ if (!write_shallow_commits(&sb, 0)
844
+ || write_in_full(fd, sb.buf, sb.len) != sb.len) {
845
unlink_or_warn(shallow);
846
rollback_lock_file(&lock);
847
} else {
848
commit_lock_file(&lock);
849
}
850
+ strbuf_release(&sb);
851
}
852
853
reprepare_packed_git();
commit.c
+4
-6
@@ -199,7 +199,7 @@ struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
199
return commit_graft[pos];
200
}
201
202
-int write_shallow_commits(int fd, int use_pack_protocol)
202
+int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
203
{
204
int i, count = 0;
205
for (i = 0; i < commit_graft_nr; i++)
@@ -208,12 +208,10 @@ int write_shallow_commits(int fd, int use_pack_protocol)
208
sha1_to_hex(commit_graft[i]->sha1);
209
count++;
210
if (use_pack_protocol)
211
- packet_write(fd, "shallow %s", hex);
211
+ packet_buf_write(out, "shallow %s", hex);
212
else {
213
- if (write_in_full(fd, hex, 40) != 40)
214
- break;
215
- if (write_str_in_full(fd, "\n") != 1)
216
- break;
213
+ strbuf_addstr(out, hex);
214
+ strbuf_addch(out, '\n');
215
}
216
}
217
return count;
commit.h
+1
-1
@@ -131,7 +131,7 @@ extern struct commit_list *get_octopus_merge_bases(struct commit_list *in);
131
132
extern int register_shallow(const unsigned char *sha1);
133
extern int unregister_shallow(const unsigned char *sha1);
134
-extern int write_shallow_commits(int fd, int use_pack_protocol);
134
+extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol);
135
extern int is_repository_shallow(void);
136
extern struct commit_list *get_shallow_commits(struct object_array *heads,
137
int depth, int shallow_flag, int not_shallow_flag);