4 #include "run-command.h"
13 struct child_process
*helper
;
16 /* These go from remote name (as in "list") to private name */
17 struct refspec
*refspecs
;
21 static struct child_process
*get_helper(struct transport
*transport
)
23 struct helper_data
*data
= transport
->data
;
24 struct strbuf buf
= STRBUF_INIT
;
25 struct child_process
*helper
;
27 const char **refspecs
= NULL
;
29 int refspec_alloc
= 0;
34 helper
= xcalloc(1, sizeof(*helper
));
38 helper
->argv
= xcalloc(4, sizeof(*helper
->argv
));
39 strbuf_addf(&buf
, "remote-%s", data
->name
);
40 helper
->argv
[0] = strbuf_detach(&buf
, NULL
);
41 helper
->argv
[1] = transport
->remote
->name
;
42 helper
->argv
[2] = transport
->url
;
44 if (start_command(helper
))
45 die("Unable to run helper: git %s", helper
->argv
[0]);
46 data
->helper
= helper
;
48 write_str_in_full(helper
->in
, "capabilities\n");
50 file
= xfdopen(helper
->out
, "r");
52 if (strbuf_getline(&buf
, file
, '\n') == EOF
)
53 exit(128); /* child died, message supplied already */
57 if (!strcmp(buf
.buf
, "fetch"))
59 if (!strcmp(buf
.buf
, "import"))
61 if (!data
->refspecs
&& !prefixcmp(buf
.buf
, "refspec ")) {
65 refspecs
[refspec_nr
++] = strdup(buf
.buf
+ strlen("refspec "));
70 data
->refspec_nr
= refspec_nr
;
71 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
72 for (i
= 0; i
< refspec_nr
; i
++) {
73 free((char *)refspecs
[i
]);
80 static int disconnect_helper(struct transport
*transport
)
82 struct helper_data
*data
= transport
->data
;
84 write_str_in_full(data
->helper
->in
, "\n");
85 close(data
->helper
->in
);
86 finish_command(data
->helper
);
87 free((char *)data
->helper
->argv
[0]);
88 free(data
->helper
->argv
);
95 static int release_helper(struct transport
*transport
)
97 struct helper_data
*data
= transport
->data
;
98 free_refspec(data
->refspec_nr
, data
->refspecs
);
99 data
->refspecs
= NULL
;
100 disconnect_helper(transport
);
101 free(transport
->data
);
105 static int fetch_with_fetch(struct transport
*transport
,
106 int nr_heads
, struct ref
**to_fetch
)
108 struct child_process
*helper
= get_helper(transport
);
109 FILE *file
= xfdopen(helper
->out
, "r");
111 struct strbuf buf
= STRBUF_INIT
;
113 for (i
= 0; i
< nr_heads
; i
++) {
114 const struct ref
*posn
= to_fetch
[i
];
115 if (posn
->status
& REF_STATUS_UPTODATE
)
118 strbuf_addf(&buf
, "fetch %s %s\n",
119 sha1_to_hex(posn
->old_sha1
), posn
->name
);
120 write_in_full(helper
->in
, buf
.buf
, buf
.len
);
123 if (strbuf_getline(&buf
, file
, '\n') == EOF
)
124 exit(128); /* child died, message supplied already */
129 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
131 struct child_process
*helper
= get_helper(transport
);
132 memset(fastimport
, 0, sizeof(*fastimport
));
133 fastimport
->in
= helper
->out
;
134 fastimport
->argv
= xcalloc(5, sizeof(*fastimport
->argv
));
135 fastimport
->argv
[0] = "fast-import";
136 fastimport
->argv
[1] = "--quiet";
138 fastimport
->git_cmd
= 1;
139 return start_command(fastimport
);
142 static int fetch_with_import(struct transport
*transport
,
143 int nr_heads
, struct ref
**to_fetch
)
145 struct child_process fastimport
;
146 struct child_process
*helper
= get_helper(transport
);
147 struct helper_data
*data
= transport
->data
;
150 struct strbuf buf
= STRBUF_INIT
;
152 if (get_importer(transport
, &fastimport
))
153 die("Couldn't run fast-import");
155 for (i
= 0; i
< nr_heads
; i
++) {
157 if (posn
->status
& REF_STATUS_UPTODATE
)
160 strbuf_addf(&buf
, "import %s\n", posn
->name
);
161 write_in_full(helper
->in
, buf
.buf
, buf
.len
);
164 disconnect_helper(transport
);
165 finish_command(&fastimport
);
167 for (i
= 0; i
< nr_heads
; i
++) {
170 if (posn
->status
& REF_STATUS_UPTODATE
)
173 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
175 private = strdup(posn
->name
);
176 read_ref(private, posn
->old_sha1
);
182 static int fetch(struct transport
*transport
,
183 int nr_heads
, struct ref
**to_fetch
)
185 struct helper_data
*data
= transport
->data
;
189 for (i
= 0; i
< nr_heads
; i
++)
190 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
197 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
200 return fetch_with_import(transport
, nr_heads
, to_fetch
);
205 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
207 struct child_process
*helper
;
208 struct ref
*ret
= NULL
;
209 struct ref
**tail
= &ret
;
211 struct strbuf buf
= STRBUF_INIT
;
214 helper
= get_helper(transport
);
216 write_str_in_full(helper
->in
, "list\n");
218 file
= xfdopen(helper
->out
, "r");
221 if (strbuf_getline(&buf
, file
, '\n') == EOF
)
222 exit(128); /* child died, message supplied already */
227 eov
= strchr(buf
.buf
, ' ');
229 die("Malformed response in ref list: %s", buf
.buf
);
230 eon
= strchr(eov
+ 1, ' ');
234 *tail
= alloc_ref(eov
+ 1);
235 if (buf
.buf
[0] == '@')
236 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
237 else if (buf
.buf
[0] != '?')
238 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
239 tail
= &((*tail
)->next
);
241 strbuf_release(&buf
);
243 for (posn
= ret
; posn
; posn
= posn
->next
)
244 resolve_remote_symref(posn
, ret
);
249 int transport_helper_init(struct transport
*transport
, const char *name
)
251 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
254 transport
->data
= data
;
255 transport
->get_refs_list
= get_refs_list
;
256 transport
->fetch
= fetch
;
257 transport
->disconnect
= release_helper
;