sg 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 $homeDir = shellex("echo \$HOME");
  18. chomp $homeDir;
  19. my $path = $homeDir . "/" . $sgDir;
  20. my $logFile = $homeDir . "/" . $sgDir . "/" . "sgLog.txt";
  21. if ( ! -d $path ) {
  22. print "Creating $path\n";
  23. shellex("mkdir $path");
  24. }
  25. if ( ! -f $logFile ) {
  26. print "Creating $logFile\n";
  27. shellex("touch $logFile");
  28. }
  29. return ( $path, $logFile );
  30. }
  31. my ( $sgPath, $sgLogFile ) = initSG(".sg");
  32. sub getLogName { return $sgLogFile; };
  33. my $log_conf = q(
  34. log4perl.rootLogger = ERROR, LOG1
  35. log4perl.appender.LOG1 = Log::Log4perl::Appender::File
  36. log4perl.appender.LOG1.filename = sub { getLogName(); }
  37. log4perl.appender.LOG1.mode = append
  38. log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
  39. log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
  40. );
  41. Log::Log4perl::init(\$log_conf);
  42. my $logger = get_logger();
  43. my $gitCmd = findBin("git",$logger);
  44. # Removed .sg from repo dir to home, don't need this sub right now
  45. # updateGitIgnore(".","/.sg",$logger);
  46. my %args;
  47. GetOptions(
  48. \%args,
  49. 'push-all',
  50. 'interactive',
  51. 'view',
  52. 'reset-from-master',
  53. 'reset-from-upstream',
  54. 'upstream-url=s',
  55. 'commit-msg=s',
  56. 'dump-config',
  57. 'configure-local-user',
  58. 'user=s',
  59. 'email=s',
  60. );
  61. sub printHelp {
  62. my $help = <<EOF
  63. simply-git
  64. Usage:
  65. --view
  66. Display git status of files and other information
  67. --dump-config
  68. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  69. --push-all [--commit-msg]
  70. Push all untracked and modified files
  71. * Can be used with interactive mode
  72. * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  73. --interactive
  74. Enable interactive mode with supported opts
  75. --reset-from-master
  76. Reset all current changes so that the file tree matches origin master
  77. --reset-from-upstream [ --upstream-url ]
  78. If upstream is defined will reset local branch to match upstream ( does not push changes to origin )
  79. * Assumes you have an upstream configured
  80. * Pass SSH/HTTPS URL to --upstream-url to add an upstream
  81. --configure-local-user [--user,--email]
  82. Configure local git user
  83. * Can be used with interactive mode
  84. EOF
  85. ;
  86. print "$help\n";
  87. }
  88. if ( scalar keys %args < 1 ) {
  89. printHelp();
  90. }
  91. ###
  92. # TODO: This args processing is better and more predictable than it was, bit there is still
  93. # likely a lot of room for improvement...
  94. ###
  95. sub parseArgs {
  96. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  97. print "Can't pass other args with --view\n";
  98. exit 1;
  99. }
  100. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  101. print "Can't pass other args with --dump-config\n";
  102. exit 1;
  103. }
  104. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  105. print "Can't pass other args with --reset-from-master\n";
  106. exit 1;
  107. }
  108. if ( defined $args{'push-all'} ) {
  109. foreach my $arg ( keys %args ) {
  110. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
  111. next;
  112. } else {
  113. print "Can only pass --interactive and --commit-msg with --push-all\n";
  114. exit 1;
  115. }
  116. }
  117. }
  118. if ( defined $args{'configure-local-user'} ) {
  119. if ( scalar keys %args < 2 ) {
  120. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  121. exit 1;
  122. }
  123. foreach my $arg ( keys %args ) {
  124. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  125. next;
  126. } else {
  127. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  128. exit 1;
  129. }
  130. }
  131. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  132. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  133. exit 1;
  134. }
  135. }
  136. if ( defined $args{'reset-from-upstream'} ) {
  137. if ( scalar keys %args > 2 ) {
  138. print "Can only pass --upstream-url with --reset-from-upstream\n";
  139. exit 1;
  140. }
  141. foreach my $arg ( keys %args ) {
  142. if ( $arg eq "reset-from-upstream" || $arg eq "upstream-url" ) {
  143. next;
  144. } else {
  145. print "Can only pass --upstream-url with --reset-from-upstream\n";
  146. exit 1;
  147. }
  148. }
  149. }
  150. }
  151. parseArgs();
  152. #print "Args parsed successfully\n";
  153. #exit 0;
  154. # TODO: This sub could be more concise with a sub to print array refs
  155. if ( defined $args{'view'} ) {
  156. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  157. my $refs = shellex("$gitCmd show-ref",$logger);
  158. my $branch = shellex("$gitCmd show-branch",$logger);
  159. my $name = shellex("$gitCmd config --get user.name",$logger);
  160. chomp $name;
  161. my $email = shellex("$gitCmd config --get user.email",$logger);
  162. chomp $email;
  163. print "-->Username: $name\n-->Email: $email\n";
  164. print "On [branch] @ commit: $branch";
  165. print "$refs\n";
  166. my $swpWarning = "\t# Likely a Vi .swp file";
  167. my $untrackedTotal = scalar @$untrackedRef;
  168. print "* $untrackedTotal untracked file(s):\n";
  169. foreach my $file ( @$untrackedRef ) {
  170. if ( $file =~ m/.swp/ ) {
  171. print "\t$file $swpWarning\n";
  172. } else {
  173. print "\t$file\n";
  174. }
  175. }
  176. my $modifiedTotal = scalar @$modifiedRef;
  177. print "* $modifiedTotal modified file(s):\n";
  178. foreach my $file ( @$modifiedRef ) {
  179. if ( $file =~ m/.swp/ ) {
  180. print "\t$file $swpWarning\n";
  181. } else {
  182. print "\t$file\n";
  183. }
  184. }
  185. my $commitTotal = scalar @$addedRef;
  186. print "* $commitTotal file(s) added to commit:\n";
  187. foreach my $file ( @$addedRef ) {
  188. if ( $file =~ m/.swp/ ) {
  189. print "\t$file $swpWarning\n";
  190. } else {
  191. print "\t$file\n";
  192. }
  193. }
  194. my $deletedTotal = scalar @$deletedRef;
  195. print "* $deletedTotal file(s) to be deleted from commit:\n";
  196. foreach my $file ( @$deletedRef ) {
  197. if ( $file =~ m/.swp/ ) {
  198. print "\t$file $swpWarning\n";
  199. } else {
  200. print "\t$file\n";
  201. }
  202. }
  203. }
  204. if ( defined $args{'push-all'} ) {
  205. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  206. my @files;
  207. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  208. my @filesToCommit;
  209. if ( defined $args{'interactive'} ) {
  210. foreach my $file ( @files ) {
  211. print "Add $file to commit (y/n): ";
  212. my $input = <STDIN>;
  213. chomp $input;
  214. if ( $input =~ m/^Y|^y/ ) {
  215. push(@filesToCommit,$file);
  216. } else {
  217. next;
  218. }
  219. }
  220. } else {
  221. @filesToCommit = @files;
  222. }
  223. print "Commiting the following files:\n";
  224. foreach my $file ( @filesToCommit ) {
  225. print "\t$file\n";
  226. }
  227. if ( defined $args{'interactive'} ) {
  228. print "Does this look correct (y/n) : ";
  229. my $input = <STDIN>;
  230. chomp $input;
  231. if ( $input !~ m/^Y|^y/ ) {
  232. print "Canceling...\n";
  233. exit 1;
  234. }
  235. }
  236. addFiles(\@filesToCommit,$logger);
  237. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  238. print "Enter a commit message: ";
  239. my $input = <STDIN>;
  240. chomp $input;
  241. commitChanges($input,$logger);
  242. } elsif ( defined $args{'commit-msg'} ) {
  243. my $commitMsg = "$args{'commit-msg'}";
  244. commitChanges($commitMsg,$logger);
  245. } else {
  246. my $epoch = time();
  247. my $commitMsg = "Generic Commit at $epoch";
  248. commitChanges($commitMsg,$logger);
  249. }
  250. if ( defined $args{'interactive'} ) {
  251. print "Push changes? (y/n): ";
  252. my $input = <STDIN>;
  253. chomp $input;
  254. if ( $input !~ m/^Y|^y/ ) {
  255. # TODO: Unstage changes?
  256. print "Canceling...\n";
  257. exit 1;
  258. }
  259. my $gitOutput = pushChanges($logger);
  260. print "Git returned:\n$gitOutput";
  261. } else {
  262. pushChanges($logger);
  263. my $gitOutput = pushChanges($logger);
  264. print "Git returned:\n$gitOutput";
  265. }
  266. }
  267. if ( defined $args{'reset-from-master'} ) {
  268. stashAndReset($logger);
  269. }
  270. if ( defined $args{'reset-from-upstream'} ) {
  271. if ( defined $args{'upstream-url'} ) {
  272. print "Setting upstream to $args{'upstream-url'}\n";
  273. chomp $args{'upstream-url'};
  274. shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
  275. shellex("$gitCmd fetch upstream",$logger);
  276. resetFromUpstream($logger);
  277. } else {
  278. resetFromUpstream($logger);
  279. }
  280. }
  281. if ( defined $args{'dump-config'} ) {
  282. my %configHash = readConfig(".",$logger);
  283. foreach my $key ( keys %configHash ) {
  284. my $hRef = $configHash{$key};
  285. print "[$key]\n";
  286. foreach my $ckey ( keys %$hRef ) {
  287. print "\t$ckey = ${$hRef}{$ckey}\n";
  288. }
  289. }
  290. }
  291. if ( defined $args{'configure-local-user'} ) {
  292. if ( defined $args{'interactive'} ) {
  293. print "Enter user to set: ";
  294. my $desiredName = <STDIN>;
  295. chomp $desiredName;
  296. print "Enter email to set: ";
  297. my $desiredEmail = <STDIN>;
  298. chomp $desiredEmail;
  299. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  300. } else {
  301. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  302. }
  303. }