sg 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 stashAndReset resetFromUpstream updateGitIgnore);
  10. # TODO: This should maybe be more robust?
  11. if ( ! -d ".git" ) {
  12. print "Not a git dir, exiting...\n";
  13. exit 1;
  14. }
  15. sub initSG($) {
  16. my $sgDir = shift;
  17. my $pwdCmd = findBin("pwd");
  18. my $pwd = shellex($pwdCmd);
  19. chomp $pwd;
  20. my $path = $pwd . "/" . $sgDir;
  21. my $logFile = $pwd . "/" . $sgDir . "/" . "sgLog.txt";
  22. if ( ! -d $path ) {
  23. print "Creating $path\n";
  24. shellex("mkdir $path");
  25. }
  26. if ( ! -f $logFile ) {
  27. print "Creating $logFile\n";
  28. shellex("touch $logFile");
  29. }
  30. return ( $path, $logFile );
  31. }
  32. my ( $sgPath, $sgLogFile ) = initSG(".sg");
  33. sub getLogName { return $sgLogFile; };
  34. my $log_conf = q(
  35. log4perl.rootLogger = ERROR, LOG1
  36. log4perl.appender.LOG1 = Log::Log4perl::Appender::File
  37. log4perl.appender.LOG1.filename = sub { getLogName(); }
  38. log4perl.appender.LOG1.mode = append
  39. log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
  40. log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
  41. );
  42. Log::Log4perl::init(\$log_conf);
  43. my $logger = get_logger();
  44. my $gitCmd = findBin("git",$logger);
  45. # Create or append to .gitignore so that we don't push the log
  46. # Maybe this should be an opt?
  47. updateGitIgnore(".","/.sg",$logger);
  48. my %args;
  49. GetOptions(
  50. \%args,
  51. 'push-all',
  52. 'interactive',
  53. 'view',
  54. 'reset-from-master',
  55. 'reset-from-upstream',
  56. 'branch-from-master',
  57. 'commit-msg=s',
  58. 'dump-config',
  59. );
  60. sub printHelp {
  61. my $help = <<EOF
  62. simply-git
  63. Usage:
  64. --view
  65. Display git status of files and other information
  66. --dump-config
  67. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  68. --push-all [--commit-msg]
  69. Push all untracked and modified files
  70. * (can be used with interactive mode)
  71. * can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  72. --interactive
  73. Enable interactive mode with supported opts
  74. --reset-from-master
  75. Reset all current changes so that the file tree matches upstream/master
  76. --branch-from-master
  77. Create a new clean branch from upstream/master
  78. EOF
  79. ;
  80. print "$help\n";
  81. }
  82. if ( scalar keys %args < 1 ) {
  83. printHelp();
  84. }
  85. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  86. if ( defined $args{'view'} ) {
  87. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  88. my $refs = shellex("$gitCmd show-ref",$logger);
  89. my $branch = shellex("$gitCmd show-branch",$logger);
  90. print "On [branch] @ commit: $branch";
  91. print "$refs\n";
  92. my $swpWarning = "\t# Likely a Vi .swp file";
  93. my $untrackedTotal = scalar @$untrackedRef;
  94. print "* $untrackedTotal untracked file(s):\n";
  95. foreach my $file ( @$untrackedRef ) {
  96. if ( $file =~ m/.swp/ ) {
  97. print "\t$file $swpWarning\n";
  98. } else {
  99. print "\t$file\n";
  100. }
  101. }
  102. my $modifiedTotal = scalar @$modifiedRef;
  103. print "* $modifiedTotal modified file(s):\n";
  104. foreach my $file ( @$modifiedRef ) {
  105. if ( $file =~ m/.swp/ ) {
  106. print "\t$file $swpWarning\n";
  107. } else {
  108. print "\t$file\n";
  109. }
  110. }
  111. my $commitTotal = scalar @$addedRef;
  112. print "* $commitTotal file(s) added to commit:\n";
  113. foreach my $file ( @$addedRef ) {
  114. if ( $file =~ m/.swp/ ) {
  115. print "\t$file $swpWarning\n";
  116. } else {
  117. print "\t$file\n";
  118. }
  119. }
  120. my $deletedTotal = scalar @$deletedRef;
  121. print "* $deletedTotal file(s) to be deleted from commit:\n";
  122. foreach my $file ( @$deletedRef ) {
  123. if ( $file =~ m/.swp/ ) {
  124. print "\t$file $swpWarning\n";
  125. } else {
  126. print "\t$file\n";
  127. }
  128. }
  129. }
  130. ###
  131. # TODO: This args processing is dangerous and incomplete, need to write an args processor function
  132. ###
  133. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  134. if ( defined $args{'push-all'} ) {
  135. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  136. my @files;
  137. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  138. my @filesToCommit;
  139. if ( defined $args{'interactive'} ) {
  140. foreach my $file ( @files ) {
  141. print "Add $file to commit (y/n): ";
  142. my $input = <STDIN>;
  143. chomp $input;
  144. if ( $input =~ m/^Y|^y/ ) {
  145. push(@filesToCommit,$file);
  146. } else {
  147. next;
  148. }
  149. }
  150. } else {
  151. @filesToCommit = @files;
  152. }
  153. print "Commiting the following files:\n";
  154. foreach my $file ( @filesToCommit ) {
  155. print "\t$file\n";
  156. }
  157. if ( defined $args{'interactive'} ) {
  158. print "Does this look correct (y/n) : ";
  159. my $input = <STDIN>;
  160. chomp $input;
  161. if ( $input !~ m/^Y|^y/ ) {
  162. print "Canceling...\n";
  163. exit 1;
  164. }
  165. }
  166. addFiles(\@filesToCommit,$logger);
  167. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  168. print "Enter a commit message: ";
  169. my $input = <STDIN>;
  170. chomp $input;
  171. commitChanges($input,$logger);
  172. } elsif ( defined $args{'commit-msg'} ) {
  173. my $commitMsg = "$args{'commit-msg'}";
  174. commitChanges($commitMsg,$logger);
  175. } else {
  176. my $epoch = time();
  177. my $commitMsg = "Generic Commit at $epoch";
  178. commitChanges($commitMsg,$logger);
  179. }
  180. if ( defined $args{'interactive'} ) {
  181. print "Push changes? (y/n): ";
  182. my $input = <STDIN>;
  183. chomp $input;
  184. if ( $input !~ m/^Y|^y/ ) {
  185. # TODO: Unstage changes?
  186. print "Canceling...\n";
  187. exit 1;
  188. }
  189. my $gitOutput = pushChanges($logger);
  190. print "Git returned:\n$gitOutput";
  191. } else {
  192. pushChanges($logger);
  193. my $gitOutput = pushChanges($logger);
  194. print "Git returned:\n$gitOutput";
  195. }
  196. }
  197. if ( defined $args{'reset-from-master'} ) {
  198. stashAndReset($logger);
  199. }
  200. if ( defined $args{'reset-from-upstream'} ) {
  201. print "This does nothing right now\n";
  202. resetFromUpstream($logger);
  203. }
  204. if ( defined $args{'dump-config'} ) {
  205. my %configHash = readConfig(".",$logger);
  206. foreach my $key ( keys %configHash ) {
  207. my $hRef = $configHash{$key};
  208. print "[$key]\n";
  209. foreach my $ckey ( keys %$hRef ) {
  210. print "\t$ckey = ${$hRef}{$ckey}\n";
  211. }
  212. }
  213. }