| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 | 
							- #!/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);
 
- 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);
 
- my %args;
 
- GetOptions(
 
- 	\%args,
 
- 	'push-all',
 
- 	'interactive',
 
- 	'view',
 
- 	'reset-from-master',
 
- 	'branch-from-master',
 
- );
 
- sub printHelp {
 
- 	my $help = <<EOF
 
- simply-git
 
- Usage:
 
- 	--view
 
- 	Display git status of files and other information
 
- 	--push-all
 
- 	Push all untracked and modified files (can be used with interactive mode)
 
- 	--interactive
 
- 	Enable interactive mode with supported opts
 
- 	--reset-from-master
 
- 	Reset all current changes so that the file tree matches upstream/master
 
- 	--branch-from-master
 
- 	Create a new clean branch from upstream/master
 
- EOF
 
- ;
 
- 	print "$help\n";
 
- }
 
- if ( scalar keys %args < 1 ) {
 
- 	printHelp();
 
- }
 
- # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
 
- if ( defined $args{'view'} ) {
 
- 	my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
 
- 	my $refs = shellex("$gitCmd show-ref",$logger);
 
- 	my $branch = shellex("$gitCmd show-branch",$logger);
 
- 	print "On [branch] @ commit: $branch";
 
- 	print "$refs\n";
 
- 	my $swpWarning = "\t# Likely a Vi .swp file";
 
- 	my $untrackedTotal = scalar @$untrackedRef;
 
- 	print "* $untrackedTotal untracked file(s):\n";
 
- 	foreach my $file ( @$untrackedRef ) {
 
- 		if ( $file =~ m/.swp/ ) {
 
- 			print "\t$file $swpWarning\n";
 
- 		} else {
 
- 			print "\t$file\n";
 
- 		}
 
- 	}
 
- 	my $modifiedTotal = scalar @$modifiedRef;
 
- 	print "* $modifiedTotal modified file(s):\n";
 
- 	foreach my $file ( @$modifiedRef ) {
 
- 		if ( $file =~ m/.swp/ ) {
 
- 			print "\t$file $swpWarning\n";
 
- 		} else {
 
- 			print "\t$file\n";
 
- 		}
 
- 	}
 
- 	my $commitTotal = scalar @$addedRef;
 
- 	print "* $commitTotal file(s) added to commit:\n";
 
- 	foreach my $file ( @$addedRef ) {
 
- 		if ( $file =~ m/.swp/ ) {
 
- 			print "\t$file $swpWarning\n";
 
- 		} else {
 
- 			print "\t$file\n";
 
- 		}
 
- 	}
 
- 	my $deletedTotal = scalar @$deletedRef;
 
- 	print "* $deletedTotal file(s) to be deleted from commit:\n";
 
- 	foreach my $file ( @$deletedRef ) {
 
- 		if ( $file =~ m/.swp/ ) {
 
- 			print "\t$file $swpWarning\n";
 
- 		} else {
 
- 			print "\t$file\n";
 
- 		}
 
- 	}
 
- }
 
- # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
 
- if ( defined $args{'push-all'} ) {
 
- 	my ( $untrackedRef, $modifiedRef ) = returnState($logger);
 
- 	my @files;
 
- 	push(@files,@$untrackedRef); push(@files,@$modifiedRef);
 
- 	my @filesToCommit;
 
- 	if ( defined $args{'interactive'} ) {
 
- 		foreach my $file ( @files ) {
 
- 			print "Add $file to commit (y/n): ";
 
- 			my $input = <STDIN>;
 
- 			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 = <STDIN>;
 
- 		chomp $input;
 
- 		if ( $input !~ m/^Y|^y/ ) {
 
- 			print "Canceling...\n";
 
- 			exit 1;
 
- 		}
 
- 	}
 
- 	
 
- 	addFiles(\@filesToCommit,$logger);
 
- 	if ( defined $args{'interactive'} ) {
 
- 		print "Enter a commit message: ";
 
- 		my $input = <STDIN>;
 
- 		chomp $input;
 
- 		commitChanges($input,$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 = <STDIN>;
 
- 		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";
 
- 	}
 
- }
 
 
  |