sg 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. sub printHelp {
  49. my $help = <<EOF
  50. simply-git
  51. Usage:
  52. --view
  53. Display git status of files and other information
  54. --push-all
  55. Push all untracked and modified files (can be used with interactive mode)
  56. --interactive
  57. Enable interactive mode with supported opts
  58. --reset-from-master
  59. Reset all current changes so that the file tree matches upstream/master
  60. --branch-from-master
  61. Create a new clean branch from upstream/master
  62. EOF
  63. ;
  64. print "$help\n";
  65. }
  66. if ( scalar keys %args < 1 ) {
  67. printHelp();
  68. }
  69. if ( defined $args{'view'} ) {
  70. my ( $untrackedRef, $modifiedRef, $addedRef ) = returnState($logger);
  71. my $swpWarning = "\t# Likely a Vi .swp file";
  72. print "Untracked files:\n";
  73. foreach my $file ( @$untrackedRef ) {
  74. if ( $file =~ m/.swp/ ) {
  75. print "\t$file $swpWarning\n";
  76. } else {
  77. print "\t$file\n";
  78. }
  79. }
  80. print "Modified files:\n";
  81. foreach my $file ( @$modifiedRef ) {
  82. if ( $file =~ m/.swp/ ) {
  83. print "\t$file $swpWarning\n";
  84. } else {
  85. print "\t$file\n";
  86. }
  87. }
  88. print "Files added to commit:\n";
  89. foreach my $file ( @$addedRef ) {
  90. if ( $file =~ m/.swp/ ) {
  91. print "\t$file $swpWarning\n";
  92. } else {
  93. print "\t$file\n";
  94. }
  95. }
  96. }
  97. if ( defined $args{'push-all'} ) {
  98. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  99. my @files;
  100. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  101. my @filesToCommit;
  102. if ( defined $args{'interactive'} ) {
  103. foreach my $file ( @files ) {
  104. print "Add $file to commit (y/n): ";
  105. my $input = <STDIN>;
  106. chomp $input;
  107. if ( $input =~ m/^Y|^y/ ) {
  108. push(@filesToCommit,$file);
  109. } else {
  110. next;
  111. }
  112. }
  113. } else {
  114. @filesToCommit = @files;
  115. }
  116. print "Commiting the following files:\n";
  117. foreach my $file ( @filesToCommit ) {
  118. print "\t$file\n";
  119. }
  120. if ( defined $args{'interactive'} ) {
  121. print "Does this look correct (y/n) : ";
  122. my $input = <STDIN>;
  123. chomp $input;
  124. if ( $input !~ m/^Y|^y/ ) {
  125. print "Canceling...\n";
  126. exit 1;
  127. }
  128. }
  129. addFiles(\@filesToCommit,$logger);
  130. if ( defined $args{'interactive'} ) {
  131. print "Enter a commit message: ";
  132. my $input = <STDIN>;
  133. chomp $input;
  134. commitChanges($input,$logger);
  135. } else {
  136. my $epoch = time();
  137. my $commitMsg = "Generic Commit at $epoch";
  138. commitChanges($commitMsg,$logger);
  139. }
  140. if ( defined $args{'interactive'} ) {
  141. print "Push changes? (y/n): ";
  142. my $input = <STDIN>;
  143. chomp $input;
  144. if ( $input !~ m/^Y|^y/ ) {
  145. # TODO: Unstage changes?
  146. print "Canceling...\n";
  147. exit 1;
  148. }
  149. my $gitOutput = pushChanges($logger);
  150. print "Git returned:\n$gitOutput\n";
  151. } else {
  152. pushChanges($logger);
  153. my $gitOutput = pushChanges($logger);
  154. print "Git returned:\n$gitOutput\n";
  155. }
  156. }