commit aacad1502bdc25fce80270271b31ead69c9c95b7 from: Izzy Blacklock date: Sun Sep 17 01:24:51 2023 UTC moved repo code to subs in ConfigNow.pm Now checks if the repo is clean before creating files. Added $self->{repo} to store the repo object. commit - 7b334ef113973160fc72ab52809a495b42ae3816 commit + aacad1502bdc25fce80270271b31ead69c9c95b7 blob - 303b7c6a83de68f58b2afb8405acb8104d405a6a blob + 75d6a2cd8392a418297e7d4e3d4c24a652381759 --- bin/configNow.pl +++ bin/configNow.pl @@ -7,9 +7,6 @@ use IRCNOW::ConfigNow; use File::Basename; use File::Path qw(make_path); -use Git::Repository; -use Git::Repository::Command; - my $shellname = shift || 'bnsnet'; my $username = shift || 'izzyb'; my $domain = 'user.planetofnix.com'; @@ -19,57 +16,23 @@ my %config=( username => $username, gitAuthor => $shellname, gitEmail => $shellname . "@" . $domain, - configNow => './configNow', + gitWorkDir => './configNow', + # configNow => './configNow', ipv4 => '38.87.162.191', ipv6 => '2602:fccf:1:1191::', domain => $domain, ); my $shellConfig = new IRCNOW::ConfigNow( %config ); -for my $module ($shellConfig->list()) { - for my $file ($shellConfig->list($module)) { - my $filename = $shellConfig->filename($file); - my $path = dirname($filename); - print "Making Directory: $path\n"; - make_path($path); - print "Writing: $filename\n"; - open my $FH, ">>$filename"; - print $FH $shellConfig->output($file); - close $FH; - } +if ($shellConfig->repo_ready()) { + $shellConfig->write_config(); + $shellConfig->repo_commit(); } -# Initialize the config repo if it hasn't been created yet. -if (not -d $config{configNow}."/.git") { - Git::Repository->run(init => $config{configNow}); -} +# get a list of commits - one line per commit with id +#$r->run(qw(git log --oneline)); -# Check status of untracked files. -my $update=0; # Flag for changes to commit -my $r = Git::Repository->new( - work_tree => $config{configNow}, - { - env => { - GIT_COMMITTER_EMAIL => $config{gitEmail}, - GIT_COMMITTER_NAME => $config{gitAuthor}, - } - } -); -my @output = $r->run(qw(status -su)); -for my $file (@output) { - # XXX need to verify the file is managed - # XXX what should happen with unmanaged files - if ($file =~ /^\?\?\s+(.*)/) { - $r->run('add', $1); - print $config{configNow}."/$1\n"; - $update = 1; - } elsif ($file =~ /^\s+M\s+/) { - $update = 1; - } -} +# Get list of files changed in a diff +#$r->run(qw(git diff 0cd562e --name-only)); -if ($update) { - $r->run('commit', '-a', '-m', 'configNow Auto Commit'); -} - blob - 0dcfca6d31b41f8534031d26c0fdb20ec5ada45c blob + 6c24bf169e69c41dde01da8fd89c8663cd5a2aee --- lib/IRCNOW/ConfigNow.pm +++ lib/IRCNOW/ConfigNow.pm @@ -6,6 +6,13 @@ package IRCNOW::ConfigNow; use strict; use warnings; use Carp; + +use File::Basename; +use File::Path qw(make_path); + +use Git::Repository; + + sub new { my $class = shift; my $options = {@_}; @@ -14,6 +21,7 @@ sub new { my $self = { vars =>$options, # options could be hash template values modules =>{}, # ConfigNow Modules + repo =>undef, # }; bless $self, $class; # Load any modules passed in @@ -77,11 +85,9 @@ sub mod_load { } else { # Lets assume they passed us a full module name. if (not defined eval "require $module") { -warn $@; # Failed so try prepending IRCNOW::ConfigNow:: $module = "IRCNOW::ConfigNow::$module"; if (not defined eval "require $module") { -warn $@; croak "Failed to load $module"; } } @@ -102,5 +108,86 @@ sub vars { } +sub write_config { + my $self=shift; + return 0 unless $self->repo_ready(); + for my $module ($self->list()) { + for my $file ($self->list($module)) { + my $filename = $self->filename($file); + my $path = dirname($filename); + print "Making Directory: $path\n"; + make_path($path); + print "Writing: $filename\n"; + #XXX add proper error handling + open my $FH, ">>$filename"; + print $FH $self->output($file); + close $FH; + } + } + return 1; +} + +sub repo_ready { + my $self=shift; + my $r = $self->{repo}; + my $workDir=$self->{vars}->{gitWorkDir}; + if( not defined $r ) { + if (not -d $workDir or not -d "$workDir/.git") { + # Initialize the config repo if it hasn't been created yet. + make_path($workDir); + Git::Repository->run(init => $workDir); + } + $r = $self->repo_connect(); + return 0 unless defined $r; + } + # Have a repo created make sure its clean + my @status = $r->run(qw(status -su)); + # Not ready if uncommited changes + return 0 if (scalar @status >0); + # Repo ready + return 1; +} + +sub repo_connect { + my $self = shift; + my $config=$self->{vars}; + return $self->{repo} if defined $self->{repo}; + $self->{repo} = Git::Repository->new( + work_tree => $config->{gitWorkDir}, + { + env => { + GIT_COMMITTER_EMAIL => $config->{gitEmail}, + GIT_COMMITTER_NAME => $config->{gitAuthor}, + }, + }, + ); + # Ready for changes. + return $self->{repo}; +} + +sub repo_commit() { + my $self=shift; + my $r = $self->{repo}; + my $workDir = $self->{vars}->{gitWorkDir}; + # Check status of untracked files. + my $update=0; # Flag for changes to commit + my @output = $r->run(qw(status -su)); + for my $file (@output) { + # XXX need to verify the file is managed + # XXX what should happen with unmanaged files + if ($file =~ /^\?\?\s+(.*)/) { + $r->run('add', $1); + print "$workDir/$1\n"; + $update = 1; + } elsif ($file =~ /^\s+M\s+/) { + $update = 1; + } + } + if ($update) { + $r->run('commit', '-a', '-m', 'configNow Auto Commit'); + } +} + + 1;