#!/usr/bin/perl use strict; use warnings; use Getopt::Long qw(GetOptions); use Log::Log4perl qw(:easy); # TODO: This needs to be scoped properly use lib "/usr/local/lib"; use SimplyGit::Shellex qw(shellex findBin); use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore); # TODO: This should maybe be more robust? if ( ! -d ".git" ) { print "Not a git dir, exiting...\n"; exit 1; } sub initSG($) { my $sgDir = shift; my $pwdCmd = findBin("pwd"); my $pwd = shellex($pwdCmd); chomp $pwd; my $path = $pwd . "/" . $sgDir; my $logFile = $pwd . "/" . $sgDir . "/" . "sgLog.txt"; if ( ! -d $path ) { print "Creating $path\n"; shellex("mkdir $path"); } if ( ! -f $logFile ) { print "Creating $logFile\n"; shellex("touch $logFile"); } return ( $path, $logFile ); } my ( $sgPath, $sgLogFile ) = initSG(".sg"); sub getLogName { return $sgLogFile; }; my $log_conf = q( log4perl.rootLogger = ERROR, LOG1 log4perl.appender.LOG1 = Log::Log4perl::Appender::File log4perl.appender.LOG1.filename = sub { getLogName(); } log4perl.appender.LOG1.mode = append log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n ); Log::Log4perl::init(\$log_conf); my $logger = get_logger(); my $gitCmd = findBin("git",$logger); # Create or append to .gitignore so that we don't push the log # Maybe this should be an opt? updateGitIgnore(".","/.sg",$logger); my %args; GetOptions( \%args, 'push-all', 'interactive', 'view', 'reset-from-master', 'reset-from-upstream', 'branch-from-master', 'commit-msg=s', 'dump-config', ); sub printHelp { my $help = <; chomp $input; if ( $input =~ m/^Y|^y/ ) { push(@filesToCommit,$file); } else { next; } } } else { @filesToCommit = @files; } print "Commiting the following files:\n"; foreach my $file ( @filesToCommit ) { print "\t$file\n"; } if ( defined $args{'interactive'} ) { print "Does this look correct (y/n) : "; my $input = ; chomp $input; if ( $input !~ m/^Y|^y/ ) { print "Canceling...\n"; exit 1; } } addFiles(\@filesToCommit,$logger); if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) { print "Enter a commit message: "; my $input = ; chomp $input; commitChanges($input,$logger); } elsif ( defined $args{'commit-msg'} ) { my $commitMsg = "$args{'commit-msg'}"; commitChanges($commitMsg,$logger); } else { my $epoch = time(); my $commitMsg = "Generic Commit at $epoch"; commitChanges($commitMsg,$logger); } if ( defined $args{'interactive'} ) { print "Push changes? (y/n): "; my $input = ; chomp $input; if ( $input !~ m/^Y|^y/ ) { # TODO: Unstage changes? print "Canceling...\n"; exit 1; } my $gitOutput = pushChanges($logger); print "Git returned:\n$gitOutput"; } else { pushChanges($logger); my $gitOutput = pushChanges($logger); print "Git returned:\n$gitOutput"; } } if ( defined $args{'reset-from-master'} ) { stashAndReset($logger); } if ( defined $args{'reset-from-upstream'} ) { print "This does nothing right now\n"; resetFromUpstream($logger); } if ( defined $args{'dump-config'} ) { my %configHash = readConfig(".",$logger); foreach my $key ( keys %configHash ) { my $hRef = $configHash{$key}; print "[$key]\n"; foreach my $ckey ( keys %$hRef ) { print "\t$ckey = ${$hRef}{$ckey}\n"; } } }