/contrib/cvs/src/release.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 334 lines · 212 code · 34 blank · 88 comment · 45 complexity · 438fd1921d8257ad9bed64e9dd02c06b MD5 · raw file

  1. /*
  2. * Copyright (C) 1994-2005 The Free Software Foundation, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. /*
  15. * Release: "cancel" a checkout in the history log.
  16. *
  17. * - Enter a line in the history log indicating the "release". - If asked to,
  18. * delete the local working directory.
  19. */
  20. #include "cvs.h"
  21. #include "savecwd.h"
  22. #include "getline.h"
  23. static const char *const release_usage[] =
  24. {
  25. "Usage: %s %s [-d] directories...\n",
  26. "\t-d\tDelete the given directory.\n",
  27. "(Specify the --help global option for a list of other help options)\n",
  28. NULL
  29. };
  30. #ifdef SERVER_SUPPORT
  31. static int release_server PROTO ((int argc, char **argv));
  32. /* This is the server side of cvs release. */
  33. static int
  34. release_server (argc, argv)
  35. int argc;
  36. char **argv;
  37. {
  38. int i;
  39. /* Note that we skip argv[0]. */
  40. for (i = 1; i < argc; ++i)
  41. history_write ('F', argv[i], "", argv[i], "");
  42. return 0;
  43. }
  44. #endif /* SERVER_SUPPORT */
  45. /* There are various things to improve about this implementation:
  46. 1. Using run_popen to run "cvs update" could be replaced by a
  47. fairly simple start_recursion/classify_file loop--a win for
  48. portability, performance, and cleanliness. In particular, there is
  49. no particularly good way to find the right "cvs".
  50. 2. The fact that "cvs update" contacts the server slows things down;
  51. it undermines the case for using "cvs release" rather than "rm -rf".
  52. However, for correctly printing "? foo" and correctly handling
  53. CVSROOTADM_IGNORE, we currently need to contact the server. (One
  54. idea for how to fix this is to stash a copy of CVSROOTADM_IGNORE in
  55. the working directories; see comment at base_* in entries.c for a
  56. few thoughts on that).
  57. 3. Would be nice to take processing things on the client side one step
  58. further, and making it like edit/unedit in terms of working well if
  59. disconnected from the network, and then sending a delayed
  60. notification.
  61. 4. Having separate network turnarounds for the "Notify" request
  62. which we do as part of unedit, and for the "release" itself, is slow
  63. and unnecessary. */
  64. int
  65. release (argc, argv)
  66. int argc;
  67. char **argv;
  68. {
  69. FILE *fp;
  70. int i, c;
  71. char *line = NULL;
  72. size_t line_allocated = 0;
  73. char *update_cmd;
  74. char *thisarg;
  75. int arg_start_idx;
  76. int err = 0;
  77. short delete_flag = 0;
  78. struct saved_cwd cwd;
  79. #ifdef SERVER_SUPPORT
  80. if (server_active)
  81. return release_server (argc, argv);
  82. #endif
  83. /* Everything from here on is client or local. */
  84. if (argc == -1)
  85. usage (release_usage);
  86. optind = 0;
  87. while ((c = getopt (argc, argv, "+Qdq")) != -1)
  88. {
  89. switch (c)
  90. {
  91. case 'Q':
  92. case 'q':
  93. error (1, 0,
  94. "-q or -Q must be specified before \"%s\"",
  95. cvs_cmd_name);
  96. break;
  97. case 'd':
  98. delete_flag++;
  99. break;
  100. case '?':
  101. default:
  102. usage (release_usage);
  103. break;
  104. }
  105. }
  106. argc -= optind;
  107. argv += optind;
  108. /* We're going to run "cvs -n -q update" and check its output; if
  109. * the output is sufficiently unalarming, then we release with no
  110. * questions asked. Else we prompt, then maybe release.
  111. * (Well, actually we ask no matter what. Our notion of "sufficiently
  112. * unalarming" doesn't take into account "? foo.c" files, so it is
  113. * up to the user to take note of them, at least currently
  114. * (ignore-193 in testsuite)).
  115. */
  116. /* Construct the update command. Be sure to add authentication and
  117. encryption if we are using them currently, else our child process may
  118. not be able to communicate with the server. */
  119. update_cmd = xmalloc (strlen (program_path)
  120. + strlen (current_parsed_root->original)
  121. + 1 + 3 + 3 + 16 + 1);
  122. sprintf (update_cmd, "%s %s%s-n -q -d %s update",
  123. program_path,
  124. #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
  125. cvsauthenticate ? "-a " : "",
  126. cvsencrypt ? "-x " : "",
  127. #else
  128. "", "",
  129. #endif
  130. current_parsed_root->original);
  131. #ifdef CLIENT_SUPPORT
  132. /* Start the server; we'll close it after looping. */
  133. if (current_parsed_root->isremote)
  134. {
  135. start_server ();
  136. ign_setup ();
  137. }
  138. #endif /* CLIENT_SUPPORT */
  139. /* Remember the directory where "cvs release" was invoked because
  140. all args are relative to this directory and we chdir around.
  141. */
  142. if (save_cwd (&cwd))
  143. error_exit ();
  144. arg_start_idx = 0;
  145. for (i = arg_start_idx; i < argc; i++)
  146. {
  147. thisarg = argv[i];
  148. if (isdir (thisarg))
  149. {
  150. if (CVS_CHDIR (thisarg) < 0)
  151. {
  152. if (!really_quiet)
  153. error (0, errno, "can't chdir to: %s", thisarg);
  154. continue;
  155. }
  156. if (!isdir (CVSADM))
  157. {
  158. if (!really_quiet)
  159. error (0, 0, "no repository directory: %s", thisarg);
  160. if (restore_cwd (&cwd, NULL))
  161. error_exit ();
  162. continue;
  163. }
  164. }
  165. else
  166. {
  167. if (!really_quiet)
  168. error (0, 0, "no such directory: %s", thisarg);
  169. continue;
  170. }
  171. if (!really_quiet)
  172. {
  173. int line_length, status;
  174. /* The "release" command piggybacks on "update", which
  175. does the real work of finding out if anything is not
  176. up-to-date with the repository. Then "release" prompts
  177. the user, telling her how many files have been
  178. modified, and asking if she still wants to do the
  179. release. */
  180. fp = run_popen (update_cmd, "r");
  181. if (fp == NULL)
  182. error (1, 0, "cannot run command %s", update_cmd);
  183. c = 0;
  184. while ((line_length = getline (&line, &line_allocated, fp)) >= 0)
  185. {
  186. if (strchr ("MARCZ", *line))
  187. c++;
  188. (void) fputs (line, stdout);
  189. }
  190. if (line_length < 0 && !feof (fp))
  191. error (0, errno, "cannot read from subprocess");
  192. /* If the update exited with an error, then we just want to
  193. complain and go on to the next arg. Especially, we do
  194. not want to delete the local copy, since it's obviously
  195. not what the user thinks it is. */
  196. status = pclose (fp);
  197. if (status != 0)
  198. {
  199. error (0, 0, "unable to release `%s' (%d)", thisarg, status);
  200. if (restore_cwd (&cwd, NULL))
  201. error_exit ();
  202. continue;
  203. }
  204. printf ("You have [%d] altered files in this repository.\n",
  205. c);
  206. printf ("Are you sure you want to release %sdirectory `%s': ",
  207. delete_flag ? "(and delete) " : "", thisarg);
  208. c = !yesno ();
  209. if (c) /* "No" */
  210. {
  211. (void) fprintf (stderr, "** `%s' aborted by user choice.\n",
  212. cvs_cmd_name);
  213. if (restore_cwd (&cwd, NULL))
  214. error_exit ();
  215. continue;
  216. }
  217. }
  218. /* Note: client.c doesn't like to have other code
  219. changing the current directory on it. So a fair amount
  220. of effort is needed to make sure it doesn't get confused
  221. about the directory and (for example) overwrite
  222. CVS/Entries file in the wrong directory. See release-17
  223. through release-23. */
  224. if (restore_cwd (&cwd, NULL))
  225. error_exit ();
  226. if (1
  227. #ifdef CLIENT_SUPPORT
  228. && !(current_parsed_root->isremote
  229. && (!supported_request ("noop")
  230. || !supported_request ("Notify")))
  231. #endif
  232. )
  233. {
  234. int argc = 2;
  235. char *argv[3];
  236. argv[0] = "dummy";
  237. argv[1] = thisarg;
  238. argv[2] = NULL;
  239. err += unedit (argc, argv);
  240. if (restore_cwd (&cwd, NULL))
  241. error_exit ();
  242. }
  243. #ifdef CLIENT_SUPPORT
  244. if (current_parsed_root->isremote)
  245. {
  246. send_to_server ("Argument ", 0);
  247. send_to_server (thisarg, 0);
  248. send_to_server ("\012", 1);
  249. send_to_server ("release\012", 0);
  250. }
  251. else
  252. #endif /* CLIENT_SUPPORT */
  253. {
  254. history_write ('F', thisarg, "", thisarg, ""); /* F == Free */
  255. }
  256. if (delete_flag)
  257. {
  258. /* FIXME? Shouldn't this just delete the CVS-controlled
  259. files and, perhaps, the files that would normally be
  260. ignored and leave everything else? */
  261. if (unlink_file_dir (thisarg) < 0)
  262. error (0, errno, "deletion of directory %s failed", thisarg);
  263. }
  264. #ifdef CLIENT_SUPPORT
  265. if (current_parsed_root->isremote)
  266. {
  267. /* FIXME:
  268. * Is there a good reason why get_server_responses() isn't
  269. * responsible for restoring its initial directory itself when
  270. * finished?
  271. */
  272. err += get_server_responses ();
  273. if (restore_cwd (&cwd, NULL))
  274. error_exit ();
  275. }
  276. #endif /* CLIENT_SUPPORT */
  277. }
  278. if (restore_cwd (&cwd, NULL))
  279. error_exit ();
  280. free_cwd (&cwd);
  281. #ifdef CLIENT_SUPPORT
  282. if (current_parsed_root->isremote)
  283. {
  284. /* Unfortunately, client.c doesn't offer a way to close
  285. the connection without waiting for responses. The extra
  286. network turnaround here is quite unnecessary other than
  287. that.... */
  288. send_to_server ("noop\012", 0);
  289. err += get_responses_and_close ();
  290. }
  291. #endif /* CLIENT_SUPPORT */
  292. free (update_cmd);
  293. if (line != NULL)
  294. free (line);
  295. return err;
  296. }