sg 11 KB

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