Skip to content

Commit 42526b4

Browse files
spearcegitster
authored andcommitted
Add stateless RPC options to upload-pack, receive-pack
When --stateless-rpc is passed as a command line parameter to upload-pack or receive-pack the programs now assume they may perform only a single read-write cycle with stdin and stdout. This fits with the HTTP POST request processing model where a program may read the request, write a response, and must exit. When --advertise-refs is passed as a command line parameter only the initial ref advertisement is output, and the program exits immediately. This fits with the HTTP GET request model, where no request content is received but a response must be produced. HTTP headers and/or environment are not processed here, but instead are assumed to be handled by the program invoking either service backend. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2f4038a commit 42526b4

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

builtin-receive-pack.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ static void add_alternate_refs(void)
615615

616616
int cmd_receive_pack(int argc, const char **argv, const char *prefix)
617617
{
618+
int advertise_refs = 0;
619+
int stateless_rpc = 0;
618620
int i;
619621
char *dir = NULL;
620622

@@ -623,7 +625,15 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
623625
const char *arg = *argv++;
624626

625627
if (*arg == '-') {
626-
/* Do flag handling here */
628+
if (!strcmp(arg, "--advertise-refs")) {
629+
advertise_refs = 1;
630+
continue;
631+
}
632+
if (!strcmp(arg, "--stateless-rpc")) {
633+
stateless_rpc = 1;
634+
continue;
635+
}
636+
627637
usage(receive_pack_usage);
628638
}
629639
if (dir)
@@ -652,12 +662,16 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
652662
" report-status delete-refs ofs-delta " :
653663
" report-status delete-refs ";
654664

655-
add_alternate_refs();
656-
write_head_info();
657-
clear_extra_refs();
665+
if (advertise_refs || !stateless_rpc) {
666+
add_alternate_refs();
667+
write_head_info();
668+
clear_extra_refs();
658669

659-
/* EOF */
660-
packet_flush(1);
670+
/* EOF */
671+
packet_flush(1);
672+
}
673+
if (advertise_refs)
674+
return 0;
661675

662676
read_head_info();
663677
if (commands) {

upload-pack.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static unsigned int timeout;
3939
*/
4040
static int use_sideband;
4141
static int debug_fd;
42+
static int advertise_refs;
43+
static int stateless_rpc;
4244

4345
static void reset_timeout(void)
4446
{
@@ -509,6 +511,8 @@ static int get_common_commits(void)
509511
if (!len) {
510512
if (have_obj.nr == 0 || multi_ack)
511513
packet_write(1, "NAK\n");
514+
if (stateless_rpc)
515+
exit(0);
512516
continue;
513517
}
514518
strip(line, len);
@@ -710,12 +714,32 @@ static int send_ref(const char *refname, const unsigned char *sha1, int flag, vo
710714
return 0;
711715
}
712716

717+
static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
718+
{
719+
struct object *o = parse_object(sha1);
720+
if (!o)
721+
die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
722+
if (!(o->flags & OUR_REF)) {
723+
o->flags |= OUR_REF;
724+
nr_our_refs++;
725+
}
726+
return 0;
727+
}
728+
713729
static void upload_pack(void)
714730
{
715-
reset_timeout();
716-
head_ref(send_ref, NULL);
717-
for_each_ref(send_ref, NULL);
718-
packet_flush(1);
731+
if (advertise_refs || !stateless_rpc) {
732+
reset_timeout();
733+
head_ref(send_ref, NULL);
734+
for_each_ref(send_ref, NULL);
735+
packet_flush(1);
736+
} else {
737+
head_ref(mark_our_ref, NULL);
738+
for_each_ref(mark_our_ref, NULL);
739+
}
740+
if (advertise_refs)
741+
return;
742+
719743
receive_needs();
720744
if (want_obj.nr) {
721745
get_common_commits();
@@ -737,6 +761,14 @@ int main(int argc, char **argv)
737761

738762
if (arg[0] != '-')
739763
break;
764+
if (!strcmp(arg, "--advertise-refs")) {
765+
advertise_refs = 1;
766+
continue;
767+
}
768+
if (!strcmp(arg, "--stateless-rpc")) {
769+
stateless_rpc = 1;
770+
continue;
771+
}
740772
if (!strcmp(arg, "--strict")) {
741773
strict = 1;
742774
continue;

0 commit comments

Comments
 (0)