sg 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 appendRepoUserConfig);
  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. 'configure-local-user',
  60. 'user=s',
  61. 'email=s',
  62. );
  63. sub printHelp {
  64. my $help = <<EOF
  65. simply-git
  66. Usage:
  67. --view
  68. Display git status of files and other information
  69. --dump-config
  70. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  71. --push-all [--commit-msg]
  72. Push all untracked and modified files
  73. * (can be used with interactive mode)
  74. * can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  75. --interactive
  76. Enable interactive mode with supported opts
  77. --reset-from-master
  78. Reset all current changes so that the file tree matches upstream/master
  79. --branch-from-master
  80. Create a new clean branch from upstream/master
  81. --configure-local-user [--user,--email]
  82. Configure local git user
  83. EOF
  84. ;
  85. print "$help\n";
  86. }
  87. if ( scalar keys %args < 1 ) {
  88. printHelp();
  89. }
  90. ###
  91. # TODO: This args processing is better and more predictable than it was, bit there is still
  92. # likely a lot of room for improvement...
  93. ###
  94. sub parseArgs {
  95. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  96. print "Can't pass other args with --view\n";
  97. exit 1;
  98. }
  99. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  100. print "Can't pass other args with --dump-config\n";
  101. exit 1;
  102. }
  103. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  104. print "Can't pass other args with --reset-from-master\n";
  105. exit 1;
  106. }
  107. if ( defined $args{'push-all'} ) {
  108. foreach my $arg ( keys %args ) {
  109. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
  110. next;
  111. } else {
  112. print "Can only pass --interactive and --commit-msg with --push-all\n";
  113. exit 1;
  114. }
  115. }
  116. }
  117. if ( defined $args{'configure-local-user'} ) {
  118. if ( scalar keys %args < 2 ) {
  119. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  120. exit 1;
  121. }
  122. foreach my $arg ( keys %args ) {
  123. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  124. next;
  125. } else {
  126. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  127. exit 1;
  128. }
  129. }
  130. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  131. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  132. exit 1;
  133. }
  134. }
  135. }
  136. parseArgs();
  137. #print "Args parsed successfully\n";
  138. #exit 0;
  139. # TODO: This sub could be more concise with a sub to print array refs
  140. if ( defined $args{'view'} ) {
  141. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  142. my $refs = shellex("$gitCmd show-ref",$logger);
  143. my $branch = shellex("$gitCmd show-branch",$logger);
  144. my $name = shellex("$gitCmd config --get user.name",$logger);
  145. chomp $name;
  146. my $email = shellex("$gitCmd config --get user.email",$logger);
  147. chomp $email;
  148. print "Username: $name\nEmail: $email\n";
  149. print "On [branch] @ commit: $branch";
  150. print "$refs\n";
  151. my $swpWarning = "\t# Likely a Vi .swp file";
  152. my $untrackedTotal = scalar @$untrackedRef;
  153. print "* $untrackedTotal untracked file(s):\n";
  154. foreach my $file ( @$untrackedRef ) {
  155. if ( $file =~ m/.swp/ ) {
  156. print "\t$file $swpWarning\n";
  157. } else {
  158. print "\t$file\n";
  159. }
  160. }
  161. my $modifiedTotal = scalar @$modifiedRef;
  162. print "* $modifiedTotal modified file(s):\n";
  163. foreach my $file ( @$modifiedRef ) {
  164. if ( $file =~ m/.swp/ ) {
  165. print "\t$file $swpWarning\n";
  166. } else {
  167. print "\t$file\n";
  168. }
  169. }
  170. my $commitTotal = scalar @$addedRef;
  171. print "* $commitTotal file(s) added to commit:\n";
  172. foreach my $file ( @$addedRef ) {
  173. if ( $file =~ m/.swp/ ) {
  174. print "\t$file $swpWarning\n";
  175. } else {
  176. print "\t$file\n";
  177. }
  178. }
  179. my $deletedTotal = scalar @$deletedRef;
  180. print "* $deletedTotal file(s) to be deleted from commit:\n";
  181. foreach my $file ( @$deletedRef ) {
  182. if ( $file =~ m/.swp/ ) {
  183. print "\t$file $swpWarning\n";
  184. } else {
  185. print "\t$file\n";
  186. }
  187. }
  188. }
  189. if ( defined $args{'push-all'} ) {
  190. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  191. my @files;
  192. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  193. my @filesToCommit;
  194. if ( defined $args{'interactive'} ) {
  195. foreach my $file ( @files ) {
  196. print "Add $file to commit (y/n): ";
  197. my $input = <STDIN>;
  198. chomp $input;
  199. if ( $input =~ m/^Y|^y/ ) {
  200. push(@filesToCommit,$file);
  201. } else {
  202. next;
  203. }
  204. }
  205. } else {
  206. @filesToCommit = @files;
  207. }
  208. print "Commiting the following files:\n";
  209. foreach my $file ( @filesToCommit ) {
  210. print "\t$file\n";
  211. }
  212. if ( defined $args{'interactive'} ) {
  213. print "Does this look correct (y/n) : ";
  214. my $input = <STDIN>;
  215. chomp $input;
  216. if ( $input !~ m/^Y|^y/ ) {
  217. print "Canceling...\n";
  218. exit 1;
  219. }
  220. }
  221. addFiles(\@filesToCommit,$logger);
  222. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  223. print "Enter a commit message: ";
  224. my $input = <STDIN>;
  225. chomp $input;
  226. commitChanges($input,$logger);
  227. } elsif ( defined $args{'commit-msg'} ) {
  228. my $commitMsg = "$args{'commit-msg'}";
  229. commitChanges($commitMsg,$logger);
  230. } else {
  231. my $epoch = time();
  232. my $commitMsg = "Generic Commit at $epoch";
  233. commitChanges($commitMsg,$logger);
  234. }
  235. if ( defined $args{'interactive'} ) {
  236. print "Push changes? (y/n): ";
  237. my $input = <STDIN>;
  238. chomp $input;
  239. if ( $input !~ m/^Y|^y/ ) {
  240. # TODO: Unstage changes?
  241. print "Canceling...\n";
  242. exit 1;
  243. }
  244. my $gitOutput = pushChanges($logger);
  245. print "Git returned:\n$gitOutput";
  246. } else {
  247. pushChanges($logger);
  248. my $gitOutput = pushChanges($logger);
  249. print "Git returned:\n$gitOutput";
  250. }
  251. }
  252. if ( defined $args{'reset-from-master'} ) {
  253. stashAndReset($logger);
  254. }
  255. if ( defined $args{'reset-from-upstream'} ) {
  256. print "This does nothing right now\n";
  257. resetFromUpstream($logger);
  258. }
  259. if ( defined $args{'dump-config'} ) {
  260. my %configHash = readConfig(".",$logger);
  261. foreach my $key ( keys %configHash ) {
  262. my $hRef = $configHash{$key};
  263. print "[$key]\n";
  264. foreach my $ckey ( keys %$hRef ) {
  265. print "\t$ckey = ${$hRef}{$ckey}\n";
  266. }
  267. }
  268. }
  269. if ( defined $args{'configure-local-user'} ) {
  270. if ( defined $args{'interactive'} ) {
  271. print "Enter user to set: ";
  272. my $desiredName = <STDIN>;
  273. chomp $desiredName;
  274. print "Enter email to set: ";
  275. my $desiredEmail = <STDIN>;
  276. chomp $desiredEmail;
  277. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  278. } else {
  279. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  280. }
  281. }