sg 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. 'upstream-url=s',
  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. --reset-from-upstream [ --upstream-url ]
  80. If upstream is defined will reset local branch to match upstream ( does not push changes by default )
  81. * Assumes you have an upstream configured
  82. * Pass SSH/HTTPS URL to --upstream-url to add an upstream
  83. --configure-local-user [--user,--email]
  84. Configure local git user
  85. * Can be used with interactive mode
  86. EOF
  87. ;
  88. print "$help\n";
  89. }
  90. if ( scalar keys %args < 1 ) {
  91. printHelp();
  92. }
  93. ###
  94. # TODO: This args processing is better and more predictable than it was, bit there is still
  95. # likely a lot of room for improvement...
  96. ###
  97. sub parseArgs {
  98. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  99. print "Can't pass other args with --view\n";
  100. exit 1;
  101. }
  102. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  103. print "Can't pass other args with --dump-config\n";
  104. exit 1;
  105. }
  106. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  107. print "Can't pass other args with --reset-from-master\n";
  108. exit 1;
  109. }
  110. if ( defined $args{'push-all'} ) {
  111. foreach my $arg ( keys %args ) {
  112. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
  113. next;
  114. } else {
  115. print "Can only pass --interactive and --commit-msg with --push-all\n";
  116. exit 1;
  117. }
  118. }
  119. }
  120. if ( defined $args{'configure-local-user'} ) {
  121. if ( scalar keys %args < 2 ) {
  122. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  123. exit 1;
  124. }
  125. foreach my $arg ( keys %args ) {
  126. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  127. next;
  128. } else {
  129. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  130. exit 1;
  131. }
  132. }
  133. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  134. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  135. exit 1;
  136. }
  137. }
  138. if ( defined $args{'reset-from-upstream'} ) {
  139. if ( scalar keys %args > 2 ) {
  140. print "Can only pass --upstream-url with --reset-from-upstream\n";
  141. exit 1;
  142. }
  143. foreach my $arg ( keys %args ) {
  144. if ( $arg eq "reset-from-upstream" || $arg eq "upstream-url" ) {
  145. next;
  146. } else {
  147. print "Can only pass --upstream-url with --reset-from-upstream\n";
  148. exit 1;
  149. }
  150. }
  151. }
  152. }
  153. parseArgs();
  154. #print "Args parsed successfully\n";
  155. #exit 0;
  156. # TODO: This sub could be more concise with a sub to print array refs
  157. if ( defined $args{'view'} ) {
  158. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  159. my $refs = shellex("$gitCmd show-ref",$logger);
  160. my $branch = shellex("$gitCmd show-branch",$logger);
  161. my $name = shellex("$gitCmd config --get user.name",$logger);
  162. chomp $name;
  163. my $email = shellex("$gitCmd config --get user.email",$logger);
  164. chomp $email;
  165. print "-->Username: $name\n-->Email: $email\n";
  166. print "On [branch] @ commit: $branch";
  167. print "$refs\n";
  168. my $swpWarning = "\t# Likely a Vi .swp file";
  169. my $untrackedTotal = scalar @$untrackedRef;
  170. print "* $untrackedTotal untracked file(s):\n";
  171. foreach my $file ( @$untrackedRef ) {
  172. if ( $file =~ m/.swp/ ) {
  173. print "\t$file $swpWarning\n";
  174. } else {
  175. print "\t$file\n";
  176. }
  177. }
  178. my $modifiedTotal = scalar @$modifiedRef;
  179. print "* $modifiedTotal modified file(s):\n";
  180. foreach my $file ( @$modifiedRef ) {
  181. if ( $file =~ m/.swp/ ) {
  182. print "\t$file $swpWarning\n";
  183. } else {
  184. print "\t$file\n";
  185. }
  186. }
  187. my $commitTotal = scalar @$addedRef;
  188. print "* $commitTotal file(s) added to commit:\n";
  189. foreach my $file ( @$addedRef ) {
  190. if ( $file =~ m/.swp/ ) {
  191. print "\t$file $swpWarning\n";
  192. } else {
  193. print "\t$file\n";
  194. }
  195. }
  196. my $deletedTotal = scalar @$deletedRef;
  197. print "* $deletedTotal file(s) to be deleted from commit:\n";
  198. foreach my $file ( @$deletedRef ) {
  199. if ( $file =~ m/.swp/ ) {
  200. print "\t$file $swpWarning\n";
  201. } else {
  202. print "\t$file\n";
  203. }
  204. }
  205. }
  206. if ( defined $args{'push-all'} ) {
  207. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  208. my @files;
  209. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  210. my @filesToCommit;
  211. if ( defined $args{'interactive'} ) {
  212. foreach my $file ( @files ) {
  213. print "Add $file to commit (y/n): ";
  214. my $input = <STDIN>;
  215. chomp $input;
  216. if ( $input =~ m/^Y|^y/ ) {
  217. push(@filesToCommit,$file);
  218. } else {
  219. next;
  220. }
  221. }
  222. } else {
  223. @filesToCommit = @files;
  224. }
  225. print "Commiting the following files:\n";
  226. foreach my $file ( @filesToCommit ) {
  227. print "\t$file\n";
  228. }
  229. if ( defined $args{'interactive'} ) {
  230. print "Does this look correct (y/n) : ";
  231. my $input = <STDIN>;
  232. chomp $input;
  233. if ( $input !~ m/^Y|^y/ ) {
  234. print "Canceling...\n";
  235. exit 1;
  236. }
  237. }
  238. addFiles(\@filesToCommit,$logger);
  239. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  240. print "Enter a commit message: ";
  241. my $input = <STDIN>;
  242. chomp $input;
  243. commitChanges($input,$logger);
  244. } elsif ( defined $args{'commit-msg'} ) {
  245. my $commitMsg = "$args{'commit-msg'}";
  246. commitChanges($commitMsg,$logger);
  247. } else {
  248. my $epoch = time();
  249. my $commitMsg = "Generic Commit at $epoch";
  250. commitChanges($commitMsg,$logger);
  251. }
  252. if ( defined $args{'interactive'} ) {
  253. print "Push changes? (y/n): ";
  254. my $input = <STDIN>;
  255. chomp $input;
  256. if ( $input !~ m/^Y|^y/ ) {
  257. # TODO: Unstage changes?
  258. print "Canceling...\n";
  259. exit 1;
  260. }
  261. my $gitOutput = pushChanges($logger);
  262. print "Git returned:\n$gitOutput";
  263. } else {
  264. pushChanges($logger);
  265. my $gitOutput = pushChanges($logger);
  266. print "Git returned:\n$gitOutput";
  267. }
  268. }
  269. if ( defined $args{'reset-from-master'} ) {
  270. stashAndReset($logger);
  271. }
  272. if ( defined $args{'reset-from-upstream'} ) {
  273. if ( defined $args{'upstream-url'} ) {
  274. print "Setting upstream to $args{'upstream-url'}\n";
  275. chomp $args{'upstream-url'};
  276. shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
  277. shellex("$gitCmd fetch upstream",$logger);
  278. resetFromUpstream($logger);
  279. } else {
  280. resetFromUpstream($logger);
  281. }
  282. }
  283. if ( defined $args{'dump-config'} ) {
  284. my %configHash = readConfig(".",$logger);
  285. foreach my $key ( keys %configHash ) {
  286. my $hRef = $configHash{$key};
  287. print "[$key]\n";
  288. foreach my $ckey ( keys %$hRef ) {
  289. print "\t$ckey = ${$hRef}{$ckey}\n";
  290. }
  291. }
  292. }
  293. if ( defined $args{'configure-local-user'} ) {
  294. if ( defined $args{'interactive'} ) {
  295. print "Enter user to set: ";
  296. my $desiredName = <STDIN>;
  297. chomp $desiredName;
  298. print "Enter email to set: ";
  299. my $desiredEmail = <STDIN>;
  300. chomp $desiredEmail;
  301. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  302. } else {
  303. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  304. }
  305. }