sg 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long qw(GetOptions);
  5. use Log::Log4perl qw(:easy);
  6. # TODO: This needs to be scoped properly
  7. use lib "/usr/local/lib";
  8. use SimplyGit::Shellex qw(shellex findBin);
  9. use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges);
  10. sub initSG($) {
  11. my $sgDir = shift;
  12. my $pwdCmd = findBin("pwd");
  13. my $pwd = shellex($pwdCmd);
  14. chomp $pwd;
  15. my $path = $pwd . "/" . $sgDir;
  16. my $logFile = $pwd . "/" . $sgDir . "/" . "sgLog.txt";
  17. if ( ! -d $path ) {
  18. print "Creating $path\n";
  19. shellex("mkdir $path");
  20. }
  21. if ( ! -f $logFile ) {
  22. print "Creating $logFile\n";
  23. shellex("touch $logFile");
  24. }
  25. return ( $path, $logFile );
  26. }
  27. my ( $sgPath, $sgLogFile ) = initSG(".sg");
  28. sub getLogName { return $sgLogFile; };
  29. my $log_conf = q(
  30. log4perl.rootLogger = INFO, LOG1
  31. log4perl.appender.LOG1 = Log::Log4perl::Appender::File
  32. log4perl.appender.LOG1.filename = sub { getLogName(); }
  33. log4perl.appender.LOG1.mode = append
  34. log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
  35. log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
  36. );
  37. Log::Log4perl::init(\$log_conf);
  38. my $logger = get_logger();
  39. my %args;
  40. GetOptions(
  41. \%args,
  42. 'push-all',
  43. 'interactive',
  44. 'view',
  45. 'reset-from-master',
  46. 'branch-from-master',
  47. );
  48. if ( defined $args{'view'} ) {
  49. my ( $untrackedRef, $modifiedRef, $addedRef ) = returnState($logger);
  50. my $swpWarning = "\t# Likely a Vi .swp file";
  51. print "Untracked files:\n";
  52. foreach my $file ( @$untrackedRef ) {
  53. if ( $file =~ m/.swp/ ) {
  54. print "\t$file $swpWarning\n";
  55. } else {
  56. print "\t$file\n";
  57. }
  58. }
  59. print "Modified files:\n";
  60. foreach my $file ( @$modifiedRef ) {
  61. if ( $file =~ m/.swp/ ) {
  62. print "\t$file $swpWarning\n";
  63. } else {
  64. print "\t$file\n";
  65. }
  66. }
  67. print "Files added to commit:\n";
  68. foreach my $file ( @$addedRef ) {
  69. if ( $file =~ m/.swp/ ) {
  70. print "\t$file $swpWarning\n";
  71. } else {
  72. print "\t$file\n";
  73. }
  74. }
  75. }
  76. if ( defined $args{'push-all'} ) {
  77. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  78. my @files;
  79. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  80. my @filesToCommit;
  81. if ( defined $args{'interactive'} ) {
  82. foreach my $file ( @files ) {
  83. print "Add $file to commit (y/n): ";
  84. my $input = <STDIN>;
  85. chomp $input;
  86. if ( $input =~ m/^Y|^y/ ) {
  87. push(@filesToCommit,$file);
  88. } else {
  89. next;
  90. }
  91. }
  92. } else {
  93. @filesToCommit = @files;
  94. }
  95. print "Commiting the following files:\n";
  96. foreach my $file ( @filesToCommit ) {
  97. print "\t$file\n";
  98. }
  99. if ( defined $args{'interactive'} ) {
  100. print "Does this look correct (y/n) : ";
  101. my $input = <STDIN>;
  102. chomp $input;
  103. if ( $input !~ m/^Y|^y/ ) {
  104. print "Canceling...\n";
  105. exit 1;
  106. }
  107. }
  108. addFiles(\@filesToCommit,$logger);
  109. if ( defined $args{'interactive'} ) {
  110. print "Enter a commit message: ";
  111. my $input = <STDIN>;
  112. chomp $input;
  113. commitChanges($input,$logger);
  114. } else {
  115. my $epoch = time();
  116. my $commitMsg = "Generic Commit at $epoch";
  117. commitChanges($commitMsg,$logger);
  118. }
  119. if ( defined $args{'interactive'} ) {
  120. print "Push changes? (y/n): ";
  121. my $input = <STDIN>;
  122. chomp $input;
  123. if ( $input !~ m/^Y|^y/ ) {
  124. # TODO: Unstage changes?
  125. print "Canceling...\n";
  126. exit 1;
  127. }
  128. pushChanges($logger);
  129. } else {
  130. pushChanges($logger);
  131. }
  132. }