1
0

patch.ck 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*++
  2. Copyright (c) 2017 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. patch.ck
  9. Abstract:
  10. This module implements the patch command, which contains a suite of
  11. subcommands to help the user manage a set of patches for a package build.
  12. Author:
  13. Evan Green 10-Jul-2017
  14. Environment:
  15. Chalk
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. from getopt import gnuGetopt;
  21. from santa.config import config;
  22. from santa.lib.patchman import PatchManager;
  23. //
  24. // --------------------------------------------------------------------- Macros
  25. //
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. //
  33. // ----------------------------------------------- Internal Function Prototypes
  34. //
  35. //
  36. // -------------------------------------------------------------------- Globals
  37. //
  38. var description = "Patch management command suite";
  39. var markShortOptions = "hu";
  40. var markLongOptions = [
  41. "help",
  42. "unapplied"
  43. ];
  44. var deleteShortOptions = "hs";
  45. var deleteLongOptions = [
  46. "help",
  47. "shift"
  48. ];
  49. var commitShortOptions = "fhm:";
  50. var commitLongOptions = [
  51. "force",
  52. "help",
  53. "message="
  54. ];
  55. var usage =
  56. "usage: santa patch start <src_dir> <patch_dir>\n"
  57. " santa patch add <files...>\n"
  58. " santa patch remove <files...>\n"
  59. " santa patch apply <numbers...>\n"
  60. " santa patch unapply <numbers...>\n"
  61. " santa patch mark [--unapplied] <numbers...>\n"
  62. " santa patch goto <number>\n"
  63. " santa patch commit [options] <name>\n"
  64. " santa patch delete [--shift] [numbers...]\n"
  65. " santa patch info\n"
  66. " santa patch edit <number>\n"
  67. "The santa patch command is a suite of commands designed to help manage a\n"
  68. "set of patches for a package.\n"
  69. "Use santa patch start to begin working on a new source directory,\n"
  70. "also specifying where patches should go (or already are).\n"
  71. "Use santa patch add to add files to the current patch being developed.\n"
  72. "Files must be explicitly added before modification for inclusion during\n"
  73. "commit.\n"
  74. "Use santa remove to remove files previously added from the uncommitted\n"
  75. "patch.\n"
  76. "Use santa patch apply to apply one or more patches in the patch "
  77. "directory.\n"
  78. "Use santa patch unapply to remove the effects of one or more patches in\n"
  79. "the patch directory.\n"
  80. "Use santa patch mark to mark a set of patches as applied or unapplied \n"
  81. "without actually making any changes. Useful for manual adjustments.\n"
  82. "Use santa patch goto to apply all patches up to and including the \n"
  83. "specified patch number.\n"
  84. "Use santa patch commit to create a new patch file in the patch \n"
  85. "directory containing the differences between all added files and their \n"
  86. "current contents in the source directory. The new patch is marked as \n"
  87. "applied, and the set of files added is cleared.\n"
  88. "Specify -m to add a commit message.\n"
  89. "Specify -f to replace a patch that already exists for that number.\n"
  90. "Use santa patch delete to remove one or more patches from the patch\n"
  91. "directory. If --shift is specified, the remaining patches will be\n"
  92. "renamed to fill in the gap.\n"
  93. "Use santa patch info to get information on the current patch\n"
  94. "Use santa patch edit to revert to the change before the specified \n"
  95. "number, add the files from the numbered patch, and apply the patch. \n"
  96. "This is useful if you want to modify an existing patch, as the working \n"
  97. "state can be changed and the committed.\n";
  98. //
  99. // ------------------------------------------------------------------ Functions
  100. //
  101. function
  102. command (
  103. args
  104. )
  105. /*++
  106. Routine Description:
  107. This routine implements the archive command.
  108. Arguments:
  109. args - Supplies the arguments to the function.
  110. Return Value:
  111. Returns an exit code.
  112. --*/
  113. {
  114. var applied = true;
  115. var command;
  116. var force = false;
  117. var manager = PatchManager.load();
  118. var message;
  119. var name;
  120. var options;
  121. var shift = false;
  122. var value;
  123. if (args.length() < 2) {
  124. Core.raise(ValueError("Expected a command. See --help for usage"));
  125. }
  126. command = args[1];
  127. //
  128. // Don't ignore a cry for help.
  129. //
  130. if ((command == "-h") || (command == "--help")) {
  131. Core.print(usage);
  132. return 1;
  133. //
  134. // Add new files to the current patch.
  135. //
  136. } else if (command == "add") {
  137. if (args.length() <= 2) {
  138. manager.add(manager.srcdir);
  139. } else {
  140. for (arg in args[2...-1]) {
  141. manager.add(arg);
  142. }
  143. }
  144. //
  145. // Remove files from the current pending patch.
  146. //
  147. } else if (command == "remove") {
  148. if (args.length() <= 2) {
  149. manager.remove(manager.srcdir);
  150. } else {
  151. for (arg in args[2...-1]) {
  152. manager.remove(arg);
  153. }
  154. }
  155. //
  156. // Reset the patch manager state.
  157. //
  158. } else if (command == "start") {
  159. if (args.length() != 4) {
  160. Core.raise(ValueError("Expected 2 arguments. "
  161. "Try --help for usage"));
  162. }
  163. manager = PatchManager(args[2], args[3]);
  164. //
  165. // Apply or remove one or more patches.
  166. //
  167. } else if ((command == "apply") || (command == "unapply")) {
  168. for (arg in args[2...-1]) {
  169. arg = Int.fromString(arg);
  170. if (command == "unapply") {
  171. arg = -arg;
  172. }
  173. manager.applyPatch(arg);
  174. manager.save();
  175. }
  176. //
  177. // Apply patches up to or down to a given number.
  178. //
  179. } else if (command == "goto") {
  180. if (args.length() != 3) {
  181. Core.raise(ValueError("Expected exactly one argument. "
  182. "Try --help for usage"));
  183. }
  184. manager.applyTo(Int.fromString(args[2]));
  185. //
  186. // Mark patches as applied without doing anything.
  187. //
  188. } else if (command == "mark") {
  189. options = gnuGetopt(args[2...-1],
  190. markShortOptions,
  191. markLongOptions);
  192. for (option in options[0]) {
  193. name = option[0];
  194. value = option[1];
  195. if ((name == "-h") || (name == "--help")) {
  196. Core.print(usage);
  197. return 1;
  198. } else if ((name == "-u") || (name == "--unapplied")) {
  199. applied = false;
  200. } else {
  201. Core.raise(ValueError("Invalid option '%s'" % name));
  202. }
  203. }
  204. if (options[1].length() == 0) {
  205. manager.markPatches(-1, applied);
  206. } else {
  207. for (arg in options[1]) {
  208. manager.markPatches(Int.fromString(arg), applied);
  209. }
  210. }
  211. //
  212. // Create a patch from the currently added files.
  213. //
  214. } else if (command == "commit") {
  215. options = gnuGetopt(args[2...-1],
  216. commitShortOptions,
  217. commitLongOptions);
  218. for (option in options[0]) {
  219. name = option[0];
  220. value = option[1];
  221. if ((name == "-f") || (name == "--force")) {
  222. force = true;
  223. } else if ((name == "-h") || (name == "--help")) {
  224. Core.print(usage);
  225. return 1;
  226. } else if ((name == "-m") || (name == "--message")) {
  227. message = value;
  228. } else {
  229. Core.raise(ValueError("Invalid option '%s'" % name));
  230. }
  231. }
  232. args = options[1];
  233. if (args.length() != 1) {
  234. Core.raise(ValueError("Expected an argument. "
  235. "Try --help for usage"));
  236. }
  237. manager.commit(args[0], message, force);
  238. //
  239. // Remove a patch from the directory.
  240. //
  241. } else if (command == "delete") {
  242. options = gnuGetopt(args[2...-1],
  243. deleteShortOptions,
  244. deleteLongOptions);
  245. for (option in options[0]) {
  246. name = option[0];
  247. value = option[1];
  248. if ((name == "-h") || (name == "--help")) {
  249. Core.print(usage);
  250. return 1;
  251. } else if ((name == "-s") || (name == "--shift")) {
  252. shift = true;
  253. } else {
  254. Core.raise(ValueError("Invalid option '%s'" % name));
  255. }
  256. }
  257. args = options[1];
  258. for (arg in args) {
  259. manager.deletePatch(Int.fromString(arg), shift);
  260. manager.save();
  261. }
  262. //
  263. // Print information about the current patch.
  264. //
  265. } else if (command == "info") {
  266. Core.print("Source Directory: %s\nPatch Directory: %s\nFiles:" %
  267. [manager.srcdir, manager.patchdir]);
  268. for (file in manager.files) {
  269. Core.print("\t%s" % file);
  270. }
  271. Core.print("Current Diff:");
  272. Core.print(manager.currentDiffSet().unifiedDiff());
  273. //
  274. // Revert to before the specified path, then re-add the changes of the
  275. // patch.
  276. //
  277. } else if (command == "edit") {
  278. if (args.length() != 3) {
  279. Core.raise(ValueError("Expected exactly one argument. "
  280. "Try --help for usage"));
  281. }
  282. manager.edit(Int.fromString(args[2]));
  283. } else {
  284. Core.raise(ValueError("Unknown command %s" % command));
  285. }
  286. //
  287. // Save the patch manager state for the next run.
  288. //
  289. manager.save();
  290. return 0;
  291. }
  292. //
  293. // --------------------------------------------------------- Internal Functions
  294. //