@matheusbsbs / matheus-bagliano / commits / d47eed3272
Fork 0

Teach the update-paranoid to look at file differences

In some applications of the update hook a user may be allowed to modify a branch, but only if the file level difference is also an allowed change. This is the commonly requested feature of allowing users to modify only certain files. A new repository.*.allow syntax permits granting the three basic file level operations: A: file is added relative to the other tree M: file exists in both trees, but its SHA-1 or mode differs D: file is removed relative to the other tree on a per-branch and path-name basis. The user must also have a branch level allow line already granting them access to create, rewind or update (CRU) that branch before the hook will consult any file level rules. In order for a branch change to succeed _all_ files that differ relative to some base (by default the old value of this branch, but it can also be any valid tree-ish) must be allowed by file level allow rules. A push is rejected if any diff exists that is not covered by at least one allow rule. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Aug 9, 2007 at 02:38 UTC d47eed3272311acf16f136c49b0bb341c9a6e39c
1 file changed +105 -7
contrib/hooks/update-paranoid
+105 -7
@@ -102,6 +102,8 @@ my ($this_user) = getpwuid $<; # REAL_USER_ID
102 my $repository_name;
103 my %user_committer;
104 my @allow_rules;
105 +my @path_rules;
106 +my %diff_cache;
107
108 sub deny ($) {
109 print STDERR "-Deny- $_[0]\n" if $debug;
@@ -122,6 +124,13 @@ sub git_value (@) {
124 open(T,'-|','git',@_); local $_ = <T>; chop; close T; $_;
125 }
126
127 +sub match_string ($$) {
128 + my ($acl_n, $ref) = @_;
129 + ($acl_n eq $ref)
130 + || ($acl_n =~ m,/$, && substr($ref,0,length $acl_n) eq $acl_n)
131 + || ($acl_n =~ m,^\^, && $ref =~ m:$acl_n:);
132 +}
133 +
134 sub parse_config ($$$$) {
135 my $data = shift;
136 local $ENV{GIT_DIR} = shift;
@@ -209,6 +218,31 @@ sub check_committers (@) {
218 }
219 }
220
221 +sub load_diff ($) {
222 + my $base = shift;
223 + my $d = $diff_cache{$base};
224 + unless ($d) {
225 + local $/ = "\0";
226 + open(T,'-|','git','diff-tree',
227 + '-r','--name-status','-z',
228 + $base,$new) or return undef;
229 + my %this_diff;
230 + while (<T>) {
231 + my $op = $_;
232 + chop $op;
233 +
234 + my $path = <T>;
235 + chop $path;
236 +
237 + $this_diff{$path} = $op;
238 + }
239 + close T or return undef;
240 + $d = \%this_diff;
241 + $diff_cache{$base} = $d;
242 + }
243 + return $d;
244 +}
245 +
246 deny "No GIT_DIR inherited from caller" unless $git_dir;
247 deny "Need a ref name" unless $ref;
248 deny "Refusing funny ref $ref" unless $ref =~ s,^refs/,,;
@@ -266,7 +300,19 @@ RULE:
300 s/\${user\.$k}/$v->[0]/g;
301 }
302
269 - if (/^([CDRU ]+)\s+for\s+([^\s]+)$/) {
303 + if (/^([AMD ]+)\s+of\s+([^\s]+)\s+for\s+([^\s]+)\s+diff\s+([^\s]+)$/) {
304 + my ($ops, $pth, $ref, $bst) = ($1, $2, $3, $4);
305 + $ops =~ s/ //g;
306 + $pth =~ s/\\\\/\\/g;
307 + $ref =~ s/\\\\/\\/g;
308 + push @path_rules, [$ops, $pth, $ref, $bst];
309 + } elsif (/^([AMD ]+)\s+of\s+([^\s]+)\s+for\s+([^\s]+)$/) {
310 + my ($ops, $pth, $ref) = ($1, $2, $3);
311 + $ops =~ s/ //g;
312 + $pth =~ s/\\\\/\\/g;
313 + $ref =~ s/\\\\/\\/g;
314 + push @path_rules, [$ops, $pth, $ref, $old];
315 + } elsif (/^([CDRU ]+)\s+for\s+([^\s]+)$/) {
316 my $ops = $1;
317 my $ref = $2;
318 $ops =~ s/ //g;
@@ -300,13 +346,65 @@ foreach my $acl_entry (@allow_rules) {
346 next unless $acl_ops =~ /^[CDRU]+$/; # Uhh.... shouldn't happen.
347 next unless $acl_n;
348 next unless $op =~ /^[$acl_ops]$/;
349 + next unless match_string $acl_n, $ref;
350 +
351 + # Don't test path rules on branch deletes.
352 + #
353 + grant "Allowed by: $acl_ops for $acl_n" if $op eq 'D';
354 +
355 + # Aggregate matching path rules; allow if there aren't
356 + # any matching this ref.
357 + #
358 + my %pr;
359 + foreach my $p_entry (@path_rules) {
360 + my ($p_ops, $p_n, $p_ref, $p_bst) = @$p_entry;
361 + next unless $p_ref;
362 + push @{$pr{$p_bst}}, $p_entry if match_string $p_ref, $ref;
363 + }
364 + grant "Allowed by: $acl_ops for $acl_n" unless %pr;
365
304 - grant "Allowed by: $acl_ops for $acl_n"
305 - if (
306 - ($acl_n eq $ref)
307 - || ($acl_n =~ m,/$, && substr($ref,0,length $acl_n) eq $acl_n)
308 - || ($acl_n =~ m,^\^, && $ref =~ m:$acl_n:)
309 - );
366 + # Allow only if all changes against a single base are
367 + # allowed by file path rules.
368 + #
369 + my @bad;
370 + foreach my $p_bst (keys %pr) {
371 + my $diff_ref = load_diff $p_bst;
372 + deny "Cannot difference trees." unless ref $diff_ref;
373 +
374 + my %fd = %$diff_ref;
375 + foreach my $p_entry (@{$pr{$p_bst}}) {
376 + my ($p_ops, $p_n, $p_ref, $p_bst) = @$p_entry;
377 + next unless $p_ops =~ /^[AMD]+$/;
378 + next unless $p_n;
379 +
380 + foreach my $f_n (keys %fd) {
381 + my $f_op = $fd{$f_n};
382 + next unless $f_op;
383 + next unless $f_op =~ /^[$p_ops]$/;
384 + delete $fd{$f_n} if match_string $p_n, $f_n;
385 + }
386 + last unless %fd;
387 + }
388 +
389 + if (%fd) {
390 + push @bad, [$p_bst, \%fd];
391 + } else {
392 + # All changes relative to $p_bst were allowed.
393 + #
394 + grant "Allowed by: $acl_ops for $acl_n diff $p_bst";
395 + }
396 + }
397 +
398 + foreach my $bad_ref (@bad) {
399 + my ($p_bst, $fd) = @$bad_ref;
400 + print STDERR "\n";
401 + print STDERR "Not allowed to make the following changes:\n";
402 + print STDERR "(base: $p_bst)\n";
403 + foreach my $f_n (sort keys %$fd) {
404 + print STDERR " $fd->{$f_n} $f_n\n";
405 + }
406 + }
407 + deny "You are not permitted to $op $ref";
408 }
409 close A;
410 deny "You are not permitted to $op $ref";