summaryrefslogtreecommitdiff
path: root/git-remote.perl
blob: 576379912793c9740620892cd3ee1cf7d2f18cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/perl -w
 
use Git;
my $git = Git->repository();
 
sub add_remote_config {
	my ($hash, $name, $what, $value) = @_;
	if ($what eq 'url') {
		if (exists $hash->{$name}{'URL'}) {
			print STDERR "Warning: more than one remote.$name.url\n";
		}
		$hash->{$name}{'URL'} = $value;
	}
	elsif ($what eq 'fetch') {
		$hash->{$name}{'FETCH'} ||= [];
		push @{$hash->{$name}{'FETCH'}}, $value;
	}
	elsif ($what eq 'push') {
		$hash->{$name}{'PUSH'} ||= [];
		push @{$hash->{$name}{'PUSH'}}, $value;
	}
	if (!exists $hash->{$name}{'SOURCE'}) {
		$hash->{$name}{'SOURCE'} = 'config';
	}
}
 
sub add_remote_remotes {
	my ($hash, $file, $name) = @_;
 
	if (exists $hash->{$name}) {
		$hash->{$name}{'WARNING'} = 'ignored due to config';
		return;
	}
 
	my $fh;
	if (!open($fh, '<', $file)) {
		print STDERR "Warning: cannot open $file\n";
		return;
	}
	my $it = { 'SOURCE' => 'remotes' };
	$hash->{$name} = $it;
	while (<$fh>) {
		chomp;
		if (/^URL:\s*(.*)$/) {
			# Having more than one is Ok -- it is used for push.
			if (! exists $it->{'URL'}) {
				$it->{'URL'} = $1;
			}
		}
		elsif (/^Push:\s*(.*)$/) {
			$it->{'PUSH'} ||= [];
			push @{$it->{'PUSH'}}, $1;
		}
		elsif (/^Pull:\s*(.*)$/) {
			$it->{'FETCH'} ||= [];
			push @{$it->{'FETCH'}}, $1;
		}
		elsif (/^\#/) {
			; # ignore
		}
		else {
			print STDERR "Warning: funny line in $file: $_\n";
		}
	}
	close($fh);
}
 
sub list_remote {
	my ($git) = @_;
	my %seen = ();
	my @remotes = eval {
		$git->command(qw(config --get-regexp), '^remote\.');
	};
	for (@remotes) {
		if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) {
			add_remote_config(\%seen, $1, $2, $3);
		}
	}
 
	my $dir = $git->repo_path() . "/remotes";
	if (opendir(my $dh, $dir)) {
		local $_;
		while ($_ = readdir($dh)) {
			chomp;
			next if (! -f "$dir/$_" || ! -r _);
			add_remote_remotes(\%seen, "$dir/$_", $_);
		}
	}
 
	return \%seen;
}
 
sub add_branch_config {
	my ($hash, $name, $what, $value) = @_;
	if ($what eq 'remote') {
		if (exists $hash->{$name}{'REMOTE'}) {
			print STDERR "Warning: more than one branch.$name.remote\n";
		}
		$hash->{$name}{'REMOTE'} = $value;
	}
	elsif ($what eq 'merge') {
		$hash->{$name}{'MERGE'} ||= [];
		push @{$hash->{$name}{'MERGE'}}, $value;
	}
}
 
sub list_branch {
	my ($git) = @_;
	my %seen = ();
	my @branches = eval {
		$git->command(qw(config --get-regexp), '^branch\.');
	};
	for (@branches) {
		if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {
			add_branch_config(\%seen, $1, $2, $3);
		}
	}
 
	return \%seen;
}
 
my $remote = list_remote($git);
my $branch = list_branch($git);
 
sub update_ls_remote {
	my ($harder, $info) = @_;
 
	return if (($harder == 0) ||
		   (($harder == 1) && exists $info->{'LS_REMOTE'}));
 
	my @ref = map {
		s|^[0-9a-f]{40}\s+refs/heads/||;
		$_;
	} $git->command(qw(ls-remote --heads), $info->{'URL'});
	$info->{'LS_REMOTE'} = \@ref;
}
 
sub list_wildcard_mapping {
	my ($forced, $ours, $ls) = @_;
	my %refs;
	for (@$ls) {
		$refs{$_} = 01; # bit #0 to say "they have"
	}
	for ($git->command('for-each-ref', "refs/remotes/$ours")) {
		chomp;
		next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||);
		next if ($_ eq 'HEAD');
		$refs{$_} ||= 0;
		$refs{$_} |= 02; # bit #1 to say "we have"
	}
	my (@new, @stale, @tracked);
	for (sort keys %refs) {
		my $have = $refs{$_};
		if ($have == 1) {
			push @new, $_;
		}
		elsif ($have == 2) {
			push @stale, $_;
		}
		elsif ($have == 3) {
			push @tracked, $_;
		}
	}
	return \@new, \@stale, \@tracked;
}
 
sub list_mapping {
	my ($name, $info) = @_;
	my $fetch = $info->{'FETCH'};
	my $ls = $info->{'LS_REMOTE'};
	my (@new, @stale, @tracked);
 
	for (@$fetch) {
		next unless (/(\+)?([^:]+):(.*)/);
		my ($forced, $theirs, $ours) = ($1, $2, $3);
		if ($theirs eq 'refs/heads/*' &&
		    $ours =~ /^refs\/remotes\/(.*)\/\*$/) {
			# wildcard mapping
			my ($w_new, $w_stale, $w_tracked)
				= list_wildcard_mapping($forced, $1, $ls);
			push @new, @$w_new;
			push @stale, @$w_stale;
			push @tracked, @$w_tracked;
		}
		elsif ($theirs =~ /\*/ || $ours =~ /\*/) {
			print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n";
		}
		elsif ($theirs =~ s|^refs/heads/||) {
			if (!grep { $_ eq $theirs } @$ls) {
				push @stale, $theirs;
			}
			elsif ($ours ne '') {
				push @tracked, $theirs;
			}
		}
	}
	return \@new, \@stale, \@tracked;
}
 
sub show_mapping {
	my ($name, $info) = @_;
	my ($new, $stale, $tracked) = list_mapping($name, $info);
	if (@$new) {
		print "  New remote branches (next fetch will store in remotes/$name)\n";
		print "    @$new\n";
	}
	if (@$stale) {
		print "  Stale tracking branches in remotes/$name (use 'git remote prune')\n";
		print "    @$stale\n";
	}
	if (@$tracked) {
		print "  Tracked remote branches\n";
		print "    @$tracked\n";
	}
}
 
sub prune_remote {
	my ($name, $ls_remote) = @_;
	if (!exists $remote->{$name}) {
		print STDERR "No such remote $name\n";
		return;
	}
	my $info = $remote->{$name};
	update_ls_remote($ls_remote, $info);
 
	my ($new, $stale, $tracked) = list_mapping($name, $info);
	my $prefix = "refs/remotes/$name";
	foreach my $to_prune (@$stale) {
		my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
		$git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
	}
}
 
sub show_remote {
	my ($name, $ls_remote) = @_;
	if (!exists $remote->{$name}) {
		print STDERR "No such remote $name\n";
		return;
	}
	my $info = $remote->{$name};
	update_ls_remote($ls_remote, $info);
 
	print "* remote $name\n";
	print "  URL: $info->{'URL'}\n";
	for my $branchname (sort keys %$branch) {
		next if ($branch->{$branchname}{'REMOTE'} ne $name);
		my @merged = map {
			s|^refs/heads/||;
			$_;
		} split(' ',"@{$branch->{$branchname}{'MERGE'}}");
		next unless (@merged);
		print "  Remote branch(es) merged with 'git pull' while on branch $branchname\n";
		print "    @merged\n";
	}
	if ($info->{'LS_REMOTE'}) {
		show_mapping($name, $info);
	}
	if ($info->{'PUSH'}) {
		my @pushed = map {
			s|^refs/heads/||;
			s|:refs/heads/|:|;
			$_;
		} @{$info->{'PUSH'}};
		print "  Local branch(es) pushed with 'git push'\n";
		print "    @pushed\n";
	}
}
 
sub add_remote {
	my ($name, $url, $opts) = @_;
	if (exists $remote->{$name}) {
		print STDERR "remote $name already exists.\n";
		exit(1);
	}
	$git->command('config', "remote.$name.url", $url);
	my $track = $opts->{'track'} || ["*"];
 
	for (@$track) {
		$git->command('config', '--add', "remote.$name.fetch",
			      "+refs/heads/$_:refs/remotes/$name/$_");
	}
	if ($opts->{'fetch'}) {
		$git->command('fetch', $name);
	}
	if (exists $opts->{'master'}) {
		$git->command('symbolic-ref', "refs/remotes/$name/HEAD",
			      "refs/remotes/$name/$opts->{'master'}");
	}
}
 
sub update_remote {
	my ($name) = @_;
 
        my $conf = $git->config("remotes." . $name);
	if (defined($conf)) {
		@remotes = split(' ', $conf);
	} elsif ($name eq 'default') {
		undef @remotes;
		for (sort keys %$remote) {
			my $do_fetch = $git->config_bool("remote." . $_ .
						    ".skipDefaultUpdate");
			unless ($do_fetch) {
				push @remotes, $_;
			}
		}
	} else {
		print STDERR "Remote group $name does not exists.\n";
		exit(1);
	}
	for (@remotes) {
		print "Updating $_\n";
		$git->command('fetch', "$_");
	}
}
 
sub add_usage {
	print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
	exit(1);
}
 
if (!@ARGV) {
	for (sort keys %$remote) {
		print "$_\n";
	}
}
elsif ($ARGV[0] eq 'show') {
	my $ls_remote = 1;
	my $i;
	for ($i = 1; $i < @ARGV; $i++) {
		if ($ARGV[$i] eq '-n') {
			$ls_remote = 0;
		}
		else {
			last;
		}
	}
	if ($i >= @ARGV) {
		print STDERR "Usage: git remote show <remote>\n";
		exit(1);
	}
	for (; $i < @ARGV; $i++) {
		show_remote($ARGV[$i], $ls_remote);
	}
}
elsif ($ARGV[0] eq 'update') {
	if (@ARGV <= 1) {
		update_remote("default");
		exit(1);
	}
	for ($i = 1; $i < @ARGV; $i++) {
		update_remote($ARGV[$i]);
	}
}
elsif ($ARGV[0] eq 'prune') {
	my $ls_remote = 1;
	my $i;
	for ($i = 1; $i < @ARGV; $i++) {
		if ($ARGV[$i] eq '-n') {
			$ls_remote = 0;
		}
		else {
			last;
		}
	}
	if ($i >= @ARGV) {
		print STDERR "Usage: git remote prune <remote>\n";
		exit(1);
	}
	for (; $i < @ARGV; $i++) {
		prune_remote($ARGV[$i], $ls_remote);
	}
}
elsif ($ARGV[0] eq 'add') {
	my %opts = ();
	while (1 < @ARGV && $ARGV[1] =~ /^-/) {
		my $opt = $ARGV[1];
		shift @ARGV;
		if ($opt eq '-f' || $opt eq '--fetch') {
			$opts{'fetch'} = 1;
			next;
		}
		if ($opt eq '-t' || $opt eq '--track') {
			if (@ARGV < 1) {
				add_usage();
			}
			$opts{'track'} ||= [];
			push @{$opts{'track'}}, $ARGV[1];
			shift @ARGV;
			next;
		}
		if ($opt eq '-m' || $opt eq '--master') {
			if ((@ARGV < 1) || exists $opts{'master'}) {
				add_usage();
			}
			$opts{'master'} = $ARGV[1];
			shift @ARGV;
			next;
		}
		add_usage();
	}
	if (@ARGV != 3) {
		add_usage();
	}
	add_remote($ARGV[1], $ARGV[2], \%opts);
}
else {
	print STDERR "Usage: git remote\n";
	print STDERR "       git remote add <name> <url>\n";
	print STDERR "       git remote show <name>\n";
	print STDERR "       git remote prune <name>\n";
	print STDERR "       git remote update [group]\n";
	exit(1);
}