Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
ownership/permission behaviour tweaks
Browse files Browse the repository at this point in the history
- chown *then* chmod. the other way round causes sticky bits to be lost.
- add a -n option to save symbolic user/group names instead of numeric ids
  • Loading branch information
Ximin Luo committed Feb 11, 2012
1 parent 8526899 commit cfdeb2c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions setgitperms.perl
Expand Up @@ -37,14 +37,15 @@
-r, --read Reads perms/etc from working dir into a .gitmeta file
-s, --stdout Output to stdout instead of .gitmeta
-d, --diff Show unified diff of perms file (XOR with --stdout)
-n, --names Use user/group names instead of numeric ids
---------------------------------Write Mode------------------------------------
-w, --write Modify perms/etc in working dir to match the .gitmeta file
-v, --verbose Be verbose
\n";

my ( $stdout, $showdiff, $verbose, $read_mode, $write_mode );
my ( $stdout, $showdiff, $verbose, $read_mode, $write_mode, $use_names );

if ((@ARGV < 0) || !GetOptions(

Expand All @@ -53,6 +54,7 @@
"read", \$read_mode,
"write", \$write_mode,
"verbose", \$verbose,
"names", \$use_names,

)) { die $usage; }

Expand All @@ -72,19 +74,14 @@

chomp;

if ( /^(.*) mode=(\S+)\s+uid=(\d+)\s+gid=(\d+)/ ) {
if ( /^(.*) mode=(\S+)\s+uid=(\w+)\s+gid=(\w+)/ ) {

# Compare recorded perms to actual perms in the working dir
my ( $path, $mode, $uid, $gid ) = ( $1, $2, $3, $4 );
my $fullpath = $topdir . $path;
my ( undef, undef, $wmode, undef, $wuid, $wgid ) = lstat( $fullpath );
$wmode = sprintf "%04o", $wmode & 07777;

if ( $mode ne $wmode ) {
$verbose && print "Updating permissions on $path: old=$wmode, new=$mode\n";
chmod oct( $mode ), $fullpath;
}

if ( $uid != $wuid || $gid != $wgid ) {
if ( $verbose ) {

Expand All @@ -104,6 +101,11 @@
chown $uid, $gid, $fullpath;

}

if ( $mode ne $wmode ) {
$verbose && print "Updating permissions on $path: old=$wmode, new=$mode\n";
chmod oct( $mode ), $fullpath;
}
} else {
warn "Invalid input format in $gitmeta:\n\t$_\n";
}
Expand Down Expand Up @@ -241,6 +243,10 @@ sub printstats {
$path =~ s/@/\@/g;
my ( undef, undef, $mode, undef, $uid, $gid ) = lstat( $path );
$path =~ s/%/\%/g;
if ( $use_names ) {
$uid = getpwuid($uid);
$gid = getgrgid($gid);
}

if ( $stdout ) {
printf "%s mode=%04o uid=$uid gid=$gid\n", $path, $mode & 07777;
Expand Down

0 comments on commit cfdeb2c

Please sign in to comment.