Update local tracking refs when pushing
This also adds a --remote option to send-pack, which specifies the configured remote being used. It is provided automatically by git-push, and must match the url (which is still needed, since there could be multiple urls). Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Daniel Barkalow committed
May 15, 2007 at 22:50 UTC
b516968ff62ec153e008d033c153affd7ba9ddc6
2 files changed
+53
-11
builtin-push.c
+7
-2
@@ -178,8 +178,13 @@ static int do_push(const char *repo)
178
if (!prefixcmp(dest, "http://") ||
179
!prefixcmp(dest, "https://"))
180
sender = "http-push";
181
- else if (thin)
182
- argv[dest_argc++] = "--thin";
181
+ else {
182
+ char *rem = xmalloc(strlen(remote->name) + 10);
183
+ sprintf(rem, "--remote=%s", remote->name);
184
+ argv[dest_argc++] = rem;
185
+ if (thin)
186
+ argv[dest_argc++] = "--thin";
187
+ }
188
argv[0] = sender;
189
argv[dest_argc++] = dest;
190
while (dest_refspec_nr--)
send-pack.c
+46
-9
@@ -177,7 +177,7 @@ static int receive_status(int in)
177
return ret;
178
}
179
180
-static int send_pack(int in, int out, int nr_refspec, char **refspec)
180
+static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec)
181
{
182
struct ref *ref;
183
int new_refs;
@@ -214,18 +214,19 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec)
214
new_refs = 0;
215
for (ref = remote_refs; ref; ref = ref->next) {
216
char old_hex[60], *new_hex;
217
- int delete_ref;
217
+ int will_delete_ref;
218
219
if (!ref->peer_ref)
220
continue;
221
222
- delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
223
- if (delete_ref && !allow_deleting_refs) {
222
+
223
+ will_delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
224
+ if (will_delete_ref && !allow_deleting_refs) {
225
error("remote does not support deleting refs");
226
ret = -2;
227
continue;
228
}
228
- if (!delete_ref &&
229
+ if (!will_delete_ref &&
230
!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
231
if (verbose)
232
fprintf(stderr, "'%s': up-to-date\n", ref->name);
@@ -252,7 +253,7 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec)
253
*/
254
255
if (!force_update &&
255
- !delete_ref &&
256
+ !will_delete_ref &&
257
!is_null_sha1(ref->old_sha1) &&
258
!ref->force) {
259
if (!has_sha1_file(ref->old_sha1) ||
@@ -276,7 +277,7 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec)
277
}
278
}
279
hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
279
- if (!delete_ref)
280
+ if (!will_delete_ref)
281
new_refs++;
282
strcpy(old_hex, sha1_to_hex(ref->old_sha1));
283
new_hex = sha1_to_hex(ref->new_sha1);
@@ -291,7 +292,7 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec)
292
else
293
packet_write(out, "%s %s %s",
294
old_hex, new_hex, ref->name);
294
- if (delete_ref)
295
+ if (will_delete_ref)
296
fprintf(stderr, "deleting '%s'\n", ref->name);
297
else {
298
fprintf(stderr, "updating '%s'", ref->name);
@@ -301,6 +302,28 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec)
302
fprintf(stderr, "\n from %s\n to %s\n",
303
old_hex, new_hex);
304
}
305
+ if (remote) {
306
+ struct refspec rs;
307
+ rs.src = ref->name;
308
+ remote_find_tracking(remote, &rs);
309
+ if (rs.dst) {
310
+ struct ref_lock *lock;
311
+ fprintf(stderr, " Also local %s\n", rs.dst);
312
+ if (will_delete_ref) {
313
+ if (delete_ref(rs.dst, NULL)) {
314
+ error("Failed to delete");
315
+ }
316
+ } else {
317
+ lock = lock_any_ref_for_update(rs.dst, NULL, 0);
318
+ if (!lock)
319
+ error("Failed to lock");
320
+ else
321
+ write_ref_sha1(lock, ref->new_sha1,
322
+ "update by push");
323
+ }
324
+ free(rs.dst);
325
+ }
326
+ }
327
}
328
329
packet_flush(out);
@@ -345,6 +368,8 @@ int main(int argc, char **argv)
368
char **heads = NULL;
369
int fd[2], ret;
370
pid_t pid;
371
+ char *remote_name = NULL;
372
+ struct remote *remote = NULL;
373
374
setup_git_directory();
375
git_config(git_default_config);
@@ -362,6 +387,10 @@ int main(int argc, char **argv)
387
receivepack = arg + 7;
388
continue;
389
}
390
+ if (!prefixcmp(arg, "--remote=")) {
391
+ remote_name = arg + 9;
392
+ continue;
393
+ }
394
if (!strcmp(arg, "--all")) {
395
send_all = 1;
396
continue;
@@ -394,10 +423,18 @@ int main(int argc, char **argv)
423
usage(send_pack_usage);
424
verify_remote_names(nr_heads, heads);
425
426
+ if (remote_name) {
427
+ remote = remote_get(remote_name);
428
+ if (!remote_has_uri(remote, dest)) {
429
+ die("Destination %s is not a uri for %s",
430
+ dest, remote_name);
431
+ }
432
+ }
433
+
434
pid = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0);
435
if (pid < 0)
436
return 1;
400
- ret = send_pack(fd[0], fd[1], nr_heads, heads);
437
+ ret = send_pack(fd[0], fd[1], remote, nr_heads, heads);
438
close(fd[0]);
439
close(fd[1]);
440
ret |= finish_connect(pid);