From 08a07e21c2ea7947851efd44a1102d71e0047317 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Fri, 11 Nov 2016 17:57:26 -0800 Subject: [PATCH] projects: consolidate and refactor project archive code Create a new archive_and_delete method on the Project class that contains the original logic from delproj.cgi which is capable of handling multiple deletions of same-named projects. Replace the delproj.cgi logic with a call to the new function. Replace the inferior logic in trash-project.pl with a call to the new function. As a result, trash-project.pl can now handle multiple deletions of same-named projects. Signed-off-by: Kyle J. McKay --- Girocco/Project.pm | 47 +++++++++++++++++++++++++++++++++++++++++++++++ cgi/delproj.cgi | 36 +++--------------------------------- toolbox/trash-project.pl | 25 +++++-------------------- 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/Girocco/Project.pm b/Girocco/Project.pm index 507af94..22918ed 100644 --- a/Girocco/Project.pm +++ b/Girocco/Project.pm @@ -1044,6 +1044,53 @@ sub delete { 1; } +# If the project's directory actually exists archive it before deleting it +# Return full path to archived project ("" if none) +sub archive_and_delete { + my $self = shift; + + unless (-d $self->{path}) { + $self->delete; + return ""; + } + + # archive the project before deletion + use POSIX qw(strftime); + my $destdir = $self->{base_path}; + $destdir =~ s,(?<=[^/])/+$,,; + $destdir .= "/_recyclebin/"; + $destdir .= $1 if $self->{name} =~ m,^(.*/)[^/]+$,; + my $destbase = $self->{name}; + $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,; + my $oldmask = umask(); + umask($oldmask & ~0070); + system('mkdir', '-p', $destdir) == 0 && -d $destdir + or die "mkdir -p \"$destdir\" failed: $?"; + umask($oldmask); + my $suffix = ''; + if (-e "$destdir$destbase.git") { + $suffix = 1; + while (-e "$destdir$destbase~$suffix.git") { + ++$suffix; + last if $suffix >= 10000; # don't get too carried away + } + $suffix = '~'.$suffix; + } + not -e "$destdir$destbase$suffix.git" + or die "Unable to compute suitable archive path"; + system('mv', $self->{path}, "$destdir$destbase$suffix.git") == 0 + or die "mv \"$self->{path}\" \"$destdir$destbase$suffix.git\" failed: $?"; + my ($S,$M,$H,$d,$m,$y) = gmtime(time()); + my $recycletime = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1); + # We ought to do something if this fails, but the project has already been moved + # so there's really nothing to be done at this point. + system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git", + 'config', 'girocco.recycletime', $recycletime); + + $self->delete; + return $destdir.$destbase.$suffix.".git"; +} + sub _contains_files { my $dir = shift; (-d $dir) or return 0; diff --git a/cgi/delproj.cgi b/cgi/delproj.cgi index 8ba2fab..82190b9 100755 --- a/cgi/delproj.cgi +++ b/cgi/delproj.cgi @@ -127,40 +127,10 @@ EOT } if (!$proj->{mirror} && !$isempty) { # archive the non-empty, non-mirror project before calling delete - - use POSIX qw(strftime); - my $destdir = $proj->{base_path}; - $destdir =~ s,(?<=[^/])/+$,,; - $destdir .= "/_recyclebin/"; - $destdir .= $1 if $proj->{name} =~ m,^(.*/)[^/]+$,; - my $destbase = $proj->{name}; - $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,; - my $oldmask = umask(); - umask($oldmask & ~0070); - system('mkdir', '-p', $destdir) == 0 && -d $destdir - or die "mkdir -p $destdir failed: $?"; - umask($oldmask); - my $suffix = ''; - if (-e "$destdir$destbase.git") { - $suffix = 1; - while (-e "$destdir$destbase~$suffix.git") { - ++$suffix; - last if $suffix >= 10000; # don't get too carried away - } - $suffix = '~'.$suffix; - } - not -e "$destdir$destbase$suffix.git" - or die "Unable to compute suitable archive path"; - system('mv', $proj->{path}, "$destdir$destbase$suffix.git") == 0 - or die "mv $proj->{path} $destdir$destbase$suffix.git failed: $?"; - my ($S,$M,$H,$d,$m,$y) = gmtime(time()); - my $recycletime = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1); - # We ought to do something if this fails, but the project has already been moved - # so there's really nothing to be done at this point. - system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git", - 'config', 'girocco.recycletime', $recycletime); + $proj->archive_and_delete; + } else { + $proj->delete; } - $proj->delete; print "

Project successfully removed. Have a nice day.

\n"; exit; } diff --git a/toolbox/trash-project.pl b/toolbox/trash-project.pl index 5b55003..b93134c 100755 --- a/toolbox/trash-project.pl +++ b/toolbox/trash-project.pl @@ -11,9 +11,6 @@ use Girocco::Project; use Girocco::Util qw(noFatalsToBrowser); BEGIN {noFatalsToBrowser} -my $bin = $Girocco::Config::reporoot.'/_recyclebin'; -mkdir $bin; - my $project = shift @ARGV || die "Please give project name on command line."; # WARNING! @@ -49,23 +46,11 @@ if ($keep_forks) { if (-d "$Girocco::Config::reporoot/$project/"); } +my $archived = ""; if (!$delete) { - use POSIX qw(strftime); - die "Already have a deleted version of the project. You need to make a decision now." - if (-d "$bin/$project.git"); - - if ($project =~ m!/!) { - my $parent = $project; $parent =~ s{^(.+)/.*$}{$1}; - system('mkdir', '-p', "$bin/$parent") == 0 or - die "Problem creating directory hierarchy in recycle bin: $!"; - } - - rename $p->{'path'}, "$bin/$project.git" || die "Problem moving project data: $!"; - my ($S,$M,$H,$d,$m,$y) = gmtime(time()); - my $recycletime = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1); - system($Girocco::Config::git_bin, "--git-dir=$bin/$project.git", 'config', - 'girocco.recycletime', $recycletime); + $archived = $p->archive_and_delete(); +} else { + $p->delete(); } -$p->delete(); print "Project '$project' removed from $Girocco::Config::name". - ($delete ? '' : ", backup in $bin/$project.git") .".\n"; + ($delete ? '' : ", backup in '$archived'") .".\n"; -- 2.11.4.GIT