Skip to content

Commit edace6f

Browse files
spearcegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 743c4b7 commit edace6f

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

builtin-fetch-pack.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
165165
const unsigned char *sha1;
166166
unsigned in_vain = 0;
167167
int got_continue = 0;
168+
struct strbuf req_buf = STRBUF_INIT;
168169

169170
if (marked)
170171
for_each_ref(clear_marks, NULL);
@@ -175,6 +176,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
175176
fetching = 0;
176177
for ( ; refs ; refs = refs->next) {
177178
unsigned char *remote = refs->old_sha1;
179+
const char *remote_hex;
178180
struct object *o;
179181

180182
/*
@@ -192,27 +194,36 @@ static int find_common(int fd[2], unsigned char *result_sha1,
192194
continue;
193195
}
194196

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);
207211
fetching++;
208212
}
213+
214+
if (!fetching) {
215+
strbuf_release(&req_buf);
216+
packet_flush(fd[1]);
217+
return 1;
218+
}
219+
209220
if (is_repository_shallow())
210-
write_shallow_commits(fd[1], 1);
221+
write_shallow_commits(&req_buf, 1);
211222
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);
216227

217228
if (args.depth > 0) {
218229
char line[1024];
@@ -296,6 +307,8 @@ static int find_common(int fd[2], unsigned char *result_sha1,
296307
multi_ack = 0;
297308
flushes++;
298309
}
310+
strbuf_release(&req_buf);
311+
299312
while (flushes || multi_ack) {
300313
int ack = get_ack(fd[0], result_sha1);
301314
if (ack) {
@@ -809,6 +822,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
809822

810823
if (args.depth > 0) {
811824
struct cache_time mtime;
825+
struct strbuf sb = STRBUF_INIT;
812826
char *shallow = git_path("shallow");
813827
int fd;
814828

@@ -826,12 +840,14 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
826840

827841
fd = hold_lock_file_for_update(&lock, shallow,
828842
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) {
830845
unlink_or_warn(shallow);
831846
rollback_lock_file(&lock);
832847
} else {
833848
commit_lock_file(&lock);
834849
}
850+
strbuf_release(&sb);
835851
}
836852

837853
reprepare_packed_git();

commit.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
199199
return commit_graft[pos];
200200
}
201201

202-
int write_shallow_commits(int fd, int use_pack_protocol)
202+
int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
203203
{
204204
int i, count = 0;
205205
for (i = 0; i < commit_graft_nr; i++)
@@ -208,12 +208,10 @@ int write_shallow_commits(int fd, int use_pack_protocol)
208208
sha1_to_hex(commit_graft[i]->sha1);
209209
count++;
210210
if (use_pack_protocol)
211-
packet_write(fd, "shallow %s", hex);
211+
packet_buf_write(out, "shallow %s", hex);
212212
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');
217215
}
218216
}
219217
return count;

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extern struct commit_list *get_octopus_merge_bases(struct commit_list *in);
131131

132132
extern int register_shallow(const unsigned char *sha1);
133133
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);
135135
extern int is_repository_shallow(void);
136136
extern struct commit_list *get_shallow_commits(struct object_array *heads,
137137
int depth, int shallow_flag, int not_shallow_flag);

0 commit comments

Comments
 (0)