sg 9.5 KB

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