sg 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. ###
  86. # TODO: This args processing is better and more predictable than it was, bit there is still
  87. # likely a lot of room for improvement...
  88. ###
  89. sub parseArgs {
  90. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  91. print "Can't pass other args with --view\n";
  92. exit 1;
  93. }
  94. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  95. print "Can't pass other args with --dump-config\n";
  96. exit 1;
  97. }
  98. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  99. print "Can't pass other args with --reset-from-master\n";
  100. exit 1;
  101. }
  102. if ( defined $args{'push-all'} ) {
  103. foreach my $arg ( keys %args ) {
  104. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
  105. next;
  106. } else {
  107. print "Can only pass --interactive and --commit-msg with --push-all\n";
  108. exit 1;
  109. }
  110. }
  111. }
  112. }
  113. parseArgs();
  114. # TODO: This sub could be more concise with a sub to print array refs
  115. if ( defined $args{'view'} ) {
  116. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  117. my $refs = shellex("$gitCmd show-ref",$logger);
  118. my $branch = shellex("$gitCmd show-branch",$logger);
  119. print "On [branch] @ commit: $branch";
  120. print "$refs\n";
  121. my $swpWarning = "\t# Likely a Vi .swp file";
  122. my $untrackedTotal = scalar @$untrackedRef;
  123. print "* $untrackedTotal untracked file(s):\n";
  124. foreach my $file ( @$untrackedRef ) {
  125. if ( $file =~ m/.swp/ ) {
  126. print "\t$file $swpWarning\n";
  127. } else {
  128. print "\t$file\n";
  129. }
  130. }
  131. my $modifiedTotal = scalar @$modifiedRef;
  132. print "* $modifiedTotal modified file(s):\n";
  133. foreach my $file ( @$modifiedRef ) {
  134. if ( $file =~ m/.swp/ ) {
  135. print "\t$file $swpWarning\n";
  136. } else {
  137. print "\t$file\n";
  138. }
  139. }
  140. my $commitTotal = scalar @$addedRef;
  141. print "* $commitTotal file(s) added to commit:\n";
  142. foreach my $file ( @$addedRef ) {
  143. if ( $file =~ m/.swp/ ) {
  144. print "\t$file $swpWarning\n";
  145. } else {
  146. print "\t$file\n";
  147. }
  148. }
  149. my $deletedTotal = scalar @$deletedRef;
  150. print "* $deletedTotal file(s) to be deleted from commit:\n";
  151. foreach my $file ( @$deletedRef ) {
  152. if ( $file =~ m/.swp/ ) {
  153. print "\t$file $swpWarning\n";
  154. } else {
  155. print "\t$file\n";
  156. }
  157. }
  158. }
  159. if ( defined $args{'push-all'} ) {
  160. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  161. my @files;
  162. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  163. my @filesToCommit;
  164. if ( defined $args{'interactive'} ) {
  165. foreach my $file ( @files ) {
  166. print "Add $file to commit (y/n): ";
  167. my $input = <STDIN>;
  168. chomp $input;
  169. if ( $input =~ m/^Y|^y/ ) {
  170. push(@filesToCommit,$file);
  171. } else {
  172. next;
  173. }
  174. }
  175. } else {
  176. @filesToCommit = @files;
  177. }
  178. print "Commiting the following files:\n";
  179. foreach my $file ( @filesToCommit ) {
  180. print "\t$file\n";
  181. }
  182. if ( defined $args{'interactive'} ) {
  183. print "Does this look correct (y/n) : ";
  184. my $input = <STDIN>;
  185. chomp $input;
  186. if ( $input !~ m/^Y|^y/ ) {
  187. print "Canceling...\n";
  188. exit 1;
  189. }
  190. }
  191. addFiles(\@filesToCommit,$logger);
  192. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  193. print "Enter a commit message: ";
  194. my $input = <STDIN>;
  195. chomp $input;
  196. commitChanges($input,$logger);
  197. } elsif ( defined $args{'commit-msg'} ) {
  198. my $commitMsg = "$args{'commit-msg'}";
  199. commitChanges($commitMsg,$logger);
  200. } else {
  201. my $epoch = time();
  202. my $commitMsg = "Generic Commit at $epoch";
  203. commitChanges($commitMsg,$logger);
  204. }
  205. if ( defined $args{'interactive'} ) {
  206. print "Push changes? (y/n): ";
  207. my $input = <STDIN>;
  208. chomp $input;
  209. if ( $input !~ m/^Y|^y/ ) {
  210. # TODO: Unstage changes?
  211. print "Canceling...\n";
  212. exit 1;
  213. }
  214. my $gitOutput = pushChanges($logger);
  215. print "Git returned:\n$gitOutput";
  216. } else {
  217. pushChanges($logger);
  218. my $gitOutput = pushChanges($logger);
  219. print "Git returned:\n$gitOutput";
  220. }
  221. }
  222. if ( defined $args{'reset-from-master'} ) {
  223. stashAndReset($logger);
  224. }
  225. if ( defined $args{'reset-from-upstream'} ) {
  226. print "This does nothing right now\n";
  227. resetFromUpstream($logger);
  228. }
  229. if ( defined $args{'dump-config'} ) {
  230. my %configHash = readConfig(".",$logger);
  231. foreach my $key ( keys %configHash ) {
  232. my $hRef = $configHash{$key};
  233. print "[$key]\n";
  234. foreach my $ckey ( keys %$hRef ) {
  235. print "\t$ckey = ${$hRef}{$ckey}\n";
  236. }
  237. }
  238. }