4 #include "run-command.h"
12 struct child_process
*helper
;
16 static struct child_process
*get_helper(struct transport
*transport
)
18 struct helper_data
*data
= transport
->data
;
19 struct strbuf buf
= STRBUF_INIT
;
20 struct child_process
*helper
;
26 helper
= xcalloc(1, sizeof(*helper
));
30 helper
->argv
= xcalloc(4, sizeof(*helper
->argv
));
31 strbuf_addf(&buf
, "remote-%s", data
->name
);
32 helper
->argv
[0] = strbuf_detach(&buf
, NULL
);
33 helper
->argv
[1] = transport
->remote
->name
;
34 helper
->argv
[2] = transport
->url
;
36 if (start_command(helper
))
37 die("Unable to run helper: git %s", helper
->argv
[0]);
38 data
->helper
= helper
;
40 write_in_full(data
->helper
->in
, "capabilities\n", 13);
41 file
= fdopen(helper
->out
, "r");
43 if (strbuf_getline(&buf
, file
, '\n') == EOF
)
44 exit(128); /* child died, message supplied already */
48 if (!strcmp(buf
.buf
, "fetch"))
54 static int disconnect_helper(struct transport
*transport
)
56 struct helper_data
*data
= transport
->data
;
58 write_in_full(data
->helper
->in
, "\n", 1);
59 close(data
->helper
->in
);
60 finish_command(data
->helper
);
61 free((char *)data
->helper
->argv
[0]);
62 free(data
->helper
->argv
);
69 static int fetch_with_fetch(struct transport
*transport
,
70 int nr_heads
, const struct ref
**to_fetch
)
72 struct child_process
*helper
= get_helper(transport
);
73 FILE *file
= fdopen(helper
->out
, "r");
75 struct strbuf buf
= STRBUF_INIT
;
77 for (i
= 0; i
< nr_heads
; i
++) {
78 const struct ref
*posn
= to_fetch
[i
];
79 if (posn
->status
& REF_STATUS_UPTODATE
)
81 write_in_full(helper
->in
, "fetch ", 6);
82 write_in_full(helper
->in
, sha1_to_hex(posn
->old_sha1
), 40);
83 write_in_full(helper
->in
, " ", 1);
84 write_in_full(helper
->in
, posn
->name
, strlen(posn
->name
));
85 write_in_full(helper
->in
, "\n", 1);
86 if (strbuf_getline(&buf
, file
, '\n') == EOF
)
87 exit(128); /* child died, message supplied already */
92 static int fetch(struct transport
*transport
,
93 int nr_heads
, const struct ref
**to_fetch
)
95 struct helper_data
*data
= transport
->data
;
99 for (i
= 0; i
< nr_heads
; i
++)
100 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
107 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
112 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
114 struct child_process
*helper
;
115 struct ref
*ret
= NULL
;
116 struct ref
**tail
= &ret
;
118 struct strbuf buf
= STRBUF_INIT
;
121 helper
= get_helper(transport
);
122 write_in_full(helper
->in
, "list\n", 5);
124 file
= fdopen(helper
->out
, "r");
127 if (strbuf_getline(&buf
, file
, '\n') == EOF
)
128 exit(128); /* child died, message supplied already */
133 eov
= strchr(buf
.buf
, ' ');
135 die("Malformed response in ref list: %s", buf
.buf
);
136 eon
= strchr(eov
+ 1, ' ');
140 *tail
= alloc_ref(eov
+ 1);
141 if (buf
.buf
[0] == '@')
142 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
143 else if (buf
.buf
[0] != '?')
144 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
145 tail
= &((*tail
)->next
);
147 strbuf_release(&buf
);
149 for (posn
= ret
; posn
; posn
= posn
->next
)
150 resolve_remote_symref(posn
, ret
);
155 int transport_helper_init(struct transport
*transport
)
157 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
158 char *eom
= strchr(transport
->url
, ':');
161 data
->name
= xstrndup(transport
->url
, eom
- transport
->url
);
163 transport
->data
= data
;
164 transport
->get_refs_list
= get_refs_list
;
165 transport
->fetch
= fetch
;
166 transport
->disconnect
= disconnect_helper
;