sg 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 Shellex::Shellex qw(shellex findBin);
  9. use SimplyGit::Git qw(
  10. readConfig getStatus returnState addFiles
  11. commitChanges pushChanges stashAndReset resetFromUpstream
  12. updateGitIgnore appendRepoUserConfig parseSGConfig
  13. warnOnUser basicClone basicPull knocker
  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 $configFile\n";
  32. shellex("touch $configFile");
  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. 'knock',
  71. 'knock-clone=s',
  72. 'help',
  73. 'knock-pull',
  74. );
  75. # TODO: This should maybe be more robust?
  76. if ( ! -d ".git" && ( ! defined $args{'knock-clone'} && ! defined $args{'knock'} && ! defined $args{'help'} ) ) {
  77. print "Not a git dir, exiting...\n";
  78. exit 1;
  79. }
  80. sub printHelp {
  81. my $help = <<EOF
  82. simply-git
  83. Usage:
  84. --view
  85. Display git status of files and other information
  86. --dump-config
  87. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  88. --push-all [--commit-msg]
  89. Push all untracked and modified files
  90. * Can be used with interactive mode
  91. * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  92. --interactive
  93. Enable interactive mode with supported opts
  94. --reset-from-master
  95. Reset all current changes so that the file tree matches origin master
  96. --reset-from-upstream [ --upstream-url ]
  97. If upstream is defined will reset local branch to match upstream ( does not push changes to origin )
  98. * Assumes you have an upstream configured
  99. * Pass SSH/HTTPS URL to --upstream-url to add an upstream
  100. --configure-local-user [--user,--email]
  101. Configure local git user
  102. * Can be used with interactive mode
  103. --config-file
  104. Default is ~/.sg/sg.config, can use this opt to use another file
  105. * See example.config
  106. --knock
  107. Will try and knock the defined git server at the defined ports before any operation
  108. * See example.config
  109. * Can pass this by itself to perform a knock and exit
  110. --knock-clone
  111. Will try and knock the defined git server and clone the provided repo
  112. * Will not check if you're in a git dir
  113. --knock-pull
  114. Will try and knock the defined git server and git pull
  115. EOF
  116. ;
  117. print "$help\n";
  118. }
  119. if ( scalar keys %args < 1 ) {
  120. printHelp();
  121. }
  122. ###
  123. # TODO: This args processing is better and more predictable than it was, bit there is still
  124. # likely a lot of room for improvement...
  125. ###
  126. sub parseArgs {
  127. if ( defined $args{'help'} ) {
  128. printHelp();
  129. exit 0;
  130. }
  131. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  132. print "Can't pass other args with --view\n";
  133. exit 1;
  134. }
  135. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  136. print "Can't pass other args with --dump-config\n";
  137. exit 1;
  138. }
  139. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  140. print "Can't pass other args with --reset-from-master\n";
  141. exit 1;
  142. }
  143. if ( defined $args{'push-all'} ) {
  144. foreach my $arg ( keys %args ) {
  145. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" || $arg eq "knock" ) {
  146. next;
  147. } else {
  148. print "Can only pass --interactive and --commit-msg with --push-all\n";
  149. exit 1;
  150. }
  151. }
  152. }
  153. if ( defined $args{'configure-local-user'} ) {
  154. if ( scalar keys %args < 2 ) {
  155. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  156. exit 1;
  157. }
  158. foreach my $arg ( keys %args ) {
  159. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  160. next;
  161. } else {
  162. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  163. exit 1;
  164. }
  165. }
  166. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  167. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  168. exit 1;
  169. }
  170. }
  171. if ( defined $args{'reset-from-upstream'} ) {
  172. if ( scalar keys %args > 2 ) {
  173. print "Can only pass --upstream-url with --reset-from-upstream\n";
  174. exit 1;
  175. }
  176. foreach my $arg ( keys %args ) {
  177. if ( $arg eq "reset-from-upstream" || $arg eq "upstream-url" ) {
  178. next;
  179. } else {
  180. print "Can only pass --upstream-url with --reset-from-upstream\n";
  181. exit 1;
  182. }
  183. }
  184. }
  185. if ( ! defined $args{'config-file'} ) {
  186. $args{'config-file'} = $sgConfigFile;
  187. }
  188. if ( defined $args{'knock-clone'} ) {
  189. if ( scalar keys %args > 2 ) {
  190. print "--knock-clone accepts no other args\n";
  191. exit 1;
  192. }
  193. }
  194. if ( defined $args{'knock-pull'} ) {
  195. if ( scalar keys %args > 2 ) {
  196. print "--knock-pull accepts no other args\n";
  197. exit 1;
  198. }
  199. }
  200. }
  201. parseArgs();
  202. my %sgConfig = parseSGConfig($args{'config-file'},$logger);
  203. if ( defined $sgConfig{'UserWarn'} && -d ".git" ) {
  204. warnOnUser($sgConfig{'user.name'},$sgConfig{'user.email'},$logger);
  205. }
  206. sub knock() {
  207. if ( defined $sgConfig{'Knock'} && ( defined $args{'knock'} || defined $args{'knock-clone'} || defined $args{'knock-pull'} ) ) {
  208. knocker($sgConfig{'knock.target'},$sgConfig{'ports'},$logger);
  209. }
  210. }
  211. if ( defined $args{'knock'} && scalar keys %args == 2 ) {
  212. print "Just knocking then exiting...\n";
  213. knock();
  214. exit 1;
  215. }
  216. # TODO: This sub could be more concise with a sub to print array refs
  217. if ( defined $args{'view'} ) {
  218. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  219. my $refs = shellex("$gitCmd show-ref",$logger);
  220. my $branch = shellex("$gitCmd show-branch",$logger);
  221. my $name = shellex("$gitCmd config --get user.name",$logger);
  222. chomp $name;
  223. my $email = shellex("$gitCmd config --get user.email",$logger);
  224. chomp $email;
  225. print "-->Username: $name\n-->Email: $email\n";
  226. print "On [branch] @ commit: $branch\n";
  227. print "$refs\n";
  228. my $swpWarning = "\t# Likely a Vi .swp file";
  229. my $untrackedTotal = scalar @$untrackedRef;
  230. print "* $untrackedTotal untracked file(s):\n";
  231. foreach my $file ( @$untrackedRef ) {
  232. if ( $file =~ m/.swp/ ) {
  233. print "\t$file $swpWarning\n";
  234. } else {
  235. print "\t$file\n";
  236. }
  237. }
  238. my $modifiedTotal = scalar @$modifiedRef;
  239. print "* $modifiedTotal modified file(s):\n";
  240. foreach my $file ( @$modifiedRef ) {
  241. if ( $file =~ m/.swp/ ) {
  242. print "\t$file $swpWarning\n";
  243. } else {
  244. print "\t$file\n";
  245. }
  246. }
  247. my $commitTotal = scalar @$addedRef;
  248. print "* $commitTotal file(s) added to commit:\n";
  249. foreach my $file ( @$addedRef ) {
  250. if ( $file =~ m/.swp/ ) {
  251. print "\t$file $swpWarning\n";
  252. } else {
  253. print "\t$file\n";
  254. }
  255. }
  256. my $deletedTotal = scalar @$deletedRef;
  257. print "* $deletedTotal file(s) to be deleted from commit:\n";
  258. foreach my $file ( @$deletedRef ) {
  259. if ( $file =~ m/.swp/ ) {
  260. print "\t$file $swpWarning\n";
  261. } else {
  262. print "\t$file\n";
  263. }
  264. }
  265. }
  266. if ( defined $args{'push-all'} ) {
  267. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  268. my @files;
  269. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  270. my @filesToCommit;
  271. if ( defined $args{'interactive'} ) {
  272. foreach my $file ( @files ) {
  273. print "Add $file to commit (y/n): ";
  274. my $input = <STDIN>;
  275. chomp $input;
  276. if ( $input =~ m/^Y|^y/ ) {
  277. push(@filesToCommit,$file);
  278. } else {
  279. next;
  280. }
  281. }
  282. } else {
  283. @filesToCommit = @files;
  284. }
  285. print "Commiting the following files:\n";
  286. foreach my $file ( @filesToCommit ) {
  287. print "\t$file\n";
  288. }
  289. if ( defined $args{'interactive'} ) {
  290. print "Does this look correct (y/n) : ";
  291. my $input = <STDIN>;
  292. chomp $input;
  293. if ( $input !~ m/^Y|^y/ ) {
  294. print "Canceling...\n";
  295. exit 1;
  296. }
  297. }
  298. addFiles(\@filesToCommit,$logger);
  299. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  300. print "Enter a commit message: ";
  301. my $input = <STDIN>;
  302. chomp $input;
  303. commitChanges($input,$logger);
  304. } elsif ( defined $args{'commit-msg'} ) {
  305. my $commitMsg = "$args{'commit-msg'}";
  306. commitChanges($commitMsg,$logger);
  307. } else {
  308. my $epoch = time();
  309. my $commitMsg = "Generic Commit at $epoch";
  310. commitChanges($commitMsg,$logger);
  311. }
  312. if ( defined $args{'interactive'} ) {
  313. print "Push changes? (y/n): ";
  314. my $input = <STDIN>;
  315. chomp $input;
  316. if ( $input !~ m/^Y|^y/ ) {
  317. # TODO: Unstage changes?
  318. print "Canceling...\n";
  319. exit 1;
  320. }
  321. knock();
  322. my $gitOutput = pushChanges($logger);
  323. print "Git returned:\n$gitOutput\n";
  324. }
  325. else {
  326. knock();
  327. pushChanges($logger);
  328. my $gitOutput = pushChanges($logger);
  329. print "Git returned:\n$gitOutput\n";
  330. }
  331. }
  332. if ( defined $args{'reset-from-master'} ) {
  333. knock();
  334. stashAndReset($logger);
  335. }
  336. if ( defined $args{'reset-from-upstream'} ) {
  337. if ( defined $args{'upstream-url'} ) {
  338. print "Setting upstream to $args{'upstream-url'}\n";
  339. chomp $args{'upstream-url'};
  340. shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
  341. shellex("$gitCmd fetch upstream",$logger);
  342. knock();
  343. resetFromUpstream($logger);
  344. } else {
  345. knock();
  346. resetFromUpstream($logger);
  347. }
  348. }
  349. if ( defined $args{'dump-config'} ) {
  350. my %configHash = readConfig(".",$logger);
  351. foreach my $key ( keys %configHash ) {
  352. my $hRef = $configHash{$key};
  353. print "[$key]\n";
  354. foreach my $ckey ( keys %$hRef ) {
  355. print "\t$ckey = ${$hRef}{$ckey}\n";
  356. }
  357. }
  358. }
  359. if ( defined $args{'configure-local-user'} ) {
  360. if ( defined $args{'interactive'} ) {
  361. print "Enter user to set: ";
  362. my $desiredName = <STDIN>;
  363. chomp $desiredName;
  364. print "Enter email to set: ";
  365. my $desiredEmail = <STDIN>;
  366. chomp $desiredEmail;
  367. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  368. } else {
  369. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  370. }
  371. }
  372. if ( defined $args{'knock-clone'} ) {
  373. knock();
  374. basicClone($args{'knock-clone'},$logger);
  375. }
  376. if ( defined $args{'knock-pull'} ) {
  377. knock();
  378. basicPull($logger);
  379. }