cvsSourceTree.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // Filename: cvsSourceTree.cxx
  2. // Created by: drose (31Oct00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "cvsSourceTree.h"
  6. #include "cvsSourceDirectory.h"
  7. #include <filename.h>
  8. #include <notify.h>
  9. #include <algorithm>
  10. #include <ctype.h>
  11. #include <stdio.h> // for perror
  12. #include <errno.h>
  13. bool CVSSourceTree::_got_start_fullpath = false;
  14. string CVSSourceTree::_start_fullpath;
  15. ////////////////////////////////////////////////////////////////////
  16. // Function: CVSSourceTree::Constructor
  17. // Access: Public
  18. // Description:
  19. ////////////////////////////////////////////////////////////////////
  20. CVSSourceTree::
  21. CVSSourceTree() {
  22. _root = (CVSSourceDirectory *)NULL;
  23. _got_root_fullpath = false;
  24. }
  25. ////////////////////////////////////////////////////////////////////
  26. // Function: CVSSourceTree::Destructor
  27. // Access: Public
  28. // Description:
  29. ////////////////////////////////////////////////////////////////////
  30. CVSSourceTree::
  31. ~CVSSourceTree() {
  32. if (_root != (CVSSourceDirectory *)NULL) {
  33. delete _root;
  34. }
  35. }
  36. ////////////////////////////////////////////////////////////////////
  37. // Function: CVSSourceTree::set_root
  38. // Access: Public
  39. // Description: Sets the root of the source directory. This must be
  40. // called before scan(), and should not be called more
  41. // than once.
  42. ////////////////////////////////////////////////////////////////////
  43. void CVSSourceTree::
  44. set_root(const string &root_path) {
  45. nassertv(_path.empty());
  46. _path = root_path;
  47. }
  48. ////////////////////////////////////////////////////////////////////
  49. // Function: CVSSourceTree::scan
  50. // Access: Public
  51. // Description: Scans the complete source directory starting at the
  52. // indicated pathname. It is an error to call this more
  53. // than once. Returns true on success, false if there
  54. // is an error.
  55. ////////////////////////////////////////////////////////////////////
  56. bool CVSSourceTree::
  57. scan(const string &key_filename) {
  58. nassertr(_root == (CVSSourceDirectory *)NULL, false);
  59. Filename root_fullpath = get_root_fullpath();
  60. _root = new CVSSourceDirectory(this, NULL, root_fullpath.get_basename());
  61. return _root->scan(_path, key_filename);
  62. }
  63. ////////////////////////////////////////////////////////////////////
  64. // Function: CVSSourceTree::get_root
  65. // Access: Public
  66. // Description: Returns the root directory of the hierarchy.
  67. ////////////////////////////////////////////////////////////////////
  68. CVSSourceDirectory *CVSSourceTree::
  69. get_root() const {
  70. return _root;
  71. }
  72. ////////////////////////////////////////////////////////////////////
  73. // Function: CVSSourceTree::find_directory
  74. // Access: Public
  75. // Description: Returns the source directory that corresponds to the
  76. // given path, or NULL if there is no such directory in
  77. // the source tree.
  78. ////////////////////////////////////////////////////////////////////
  79. CVSSourceDirectory *CVSSourceTree::
  80. find_directory(const string &path) {
  81. string root_fullpath = get_root_fullpath();
  82. string fullpath = get_actual_fullpath(path);
  83. // path is a subdirectory within the source hierarchy if and only if
  84. // root_fullpath is an initial prefix of fullpath.
  85. if (root_fullpath.length() > fullpath.length() ||
  86. fullpath.substr(0, root_fullpath.length()) != root_fullpath) {
  87. // Nope!
  88. return (CVSSourceDirectory *)NULL;
  89. }
  90. // The relative name is the part of fullpath not in root_fullpath.
  91. string relpath = fullpath.substr(root_fullpath.length());
  92. return _root->find_relpath(relpath);
  93. }
  94. ////////////////////////////////////////////////////////////////////
  95. // Function: CVSSourceTree::find_relpath
  96. // Access: Public
  97. // Description: Returns the source directory that corresponds to the
  98. // given relative path from the root, or NULL if there
  99. // is no match. The relative path may or may not
  100. // include the name of the root directory itself.
  101. ////////////////////////////////////////////////////////////////////
  102. CVSSourceDirectory *CVSSourceTree::
  103. find_relpath(const string &relpath) {
  104. CVSSourceDirectory *result = _root->find_relpath(relpath);
  105. if (result != (CVSSourceDirectory *)NULL) {
  106. return result;
  107. }
  108. // Check for the root dirname at the front of the path, and remove
  109. // it if it's there.
  110. size_t slash = relpath.find('/');
  111. string first = relpath.substr(0, slash);
  112. string rest;
  113. if (slash != string::npos) {
  114. rest = relpath.substr(slash + 1);
  115. }
  116. if (first == _root->get_dirname()) {
  117. return _root->find_relpath(rest);
  118. }
  119. return (CVSSourceDirectory *)NULL;
  120. }
  121. ////////////////////////////////////////////////////////////////////
  122. // Function: CVSSourceTree::find_dirname
  123. // Access: Public
  124. // Description: Returns the source directory that corresponds to the
  125. // given local directory name, or NULL if there
  126. // is no match.
  127. ////////////////////////////////////////////////////////////////////
  128. CVSSourceDirectory *CVSSourceTree::
  129. find_dirname(const string &dirname) {
  130. return _root->find_dirname(dirname);
  131. }
  132. ////////////////////////////////////////////////////////////////////
  133. // Function: CVSSourceTree::choose_directory
  134. // Access: Public
  135. // Description: Determines where an externally referenced model file
  136. // of the indicated name should go. It does this by
  137. // looking for an existing model file of the same name;
  138. // if a matching model is not found, or if multiple
  139. // matching files are found, prompts the user for the
  140. // directory, or uses suggested_dir.
  141. ////////////////////////////////////////////////////////////////////
  142. CVSSourceDirectory *CVSSourceTree::
  143. choose_directory(const string &filename, CVSSourceDirectory *suggested_dir,
  144. bool force, bool interactive) {
  145. static Directories empty_dirs;
  146. Filenames::const_iterator fi;
  147. fi = _filenames.find(filename);
  148. if (fi != _filenames.end()) {
  149. // The filename already exists somewhere.
  150. const Directories &dirs = (*fi).second;
  151. return prompt_user(filename, suggested_dir, dirs,
  152. force, interactive);
  153. }
  154. // Now we have to prompt the user for a suitable place to put it.
  155. return prompt_user(filename, suggested_dir, empty_dirs,
  156. force, interactive);
  157. }
  158. ////////////////////////////////////////////////////////////////////
  159. // Function: CVSSourceTree::get_root_fullpath
  160. // Access: Public
  161. // Description: Returns the full path from the root to the top of
  162. // the source hierarchy.
  163. ////////////////////////////////////////////////////////////////////
  164. string CVSSourceTree::
  165. get_root_fullpath() {
  166. nassertr(!_path.empty(), string());
  167. if (!_got_root_fullpath) {
  168. _root_fullpath = get_actual_fullpath(_path);
  169. _got_root_fullpath = true;
  170. }
  171. return _root_fullpath;
  172. }
  173. ////////////////////////////////////////////////////////////////////
  174. // Function: CVSSourceTree::get_root_dirname
  175. // Access: Public
  176. // Description: Returns the local directory name of the root of the
  177. // tree.
  178. ////////////////////////////////////////////////////////////////////
  179. string CVSSourceTree::
  180. get_root_dirname() const {
  181. nassertr(_root != (CVSSourceDirectory *)NULL, string());
  182. return _root->get_dirname();
  183. }
  184. ////////////////////////////////////////////////////////////////////
  185. // Function: CVSSourceTree::add_file
  186. // Access: Public
  187. // Description: Adds a new file to the set of known files. This is
  188. // normally called from CVSSourceDirectory::scan() and
  189. // should not be called directly by the user.
  190. ////////////////////////////////////////////////////////////////////
  191. void CVSSourceTree::
  192. add_file(const string &filename, CVSSourceDirectory *dir) {
  193. _filenames[filename].push_back(dir);
  194. }
  195. ////////////////////////////////////////////////////////////////////
  196. // Function: CVSSourceTree::temp_chdir
  197. // Access: Public, Static
  198. // Description: Temporarily changes the current directory to the
  199. // named path. Returns true on success, false on
  200. // failure. Call restore_cwd() to restore to the
  201. // original directory later.
  202. ////////////////////////////////////////////////////////////////////
  203. bool CVSSourceTree::
  204. temp_chdir(const string &path) {
  205. // We have to call this first to guarantee that we have already
  206. // determined our starting directory.
  207. get_start_fullpath();
  208. if (chdir(path.c_str()) < 0) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. ////////////////////////////////////////////////////////////////////
  214. // Function: CVSSourceTree::restore_cwd
  215. // Access: Public, Static
  216. // Description: Restores the current directory after changing it from
  217. // temp_chdir().
  218. ////////////////////////////////////////////////////////////////////
  219. void CVSSourceTree::
  220. restore_cwd() {
  221. string start_fullpath = get_start_fullpath();
  222. if (chdir(start_fullpath.c_str()) < 0) {
  223. // Hey! We can't get back to the directory we started from!
  224. perror(start_fullpath.c_str());
  225. nout << "Can't continue, aborting.\n";
  226. exit(1);
  227. }
  228. }
  229. ////////////////////////////////////////////////////////////////////
  230. // Function: CVSSourceTree::prompt_user
  231. // Access: Private
  232. // Description: Prompts the user, if necessary, to choose a directory
  233. // to import the given file into.
  234. ////////////////////////////////////////////////////////////////////
  235. CVSSourceDirectory *CVSSourceTree::
  236. prompt_user(const string &filename, CVSSourceDirectory *suggested_dir,
  237. const CVSSourceTree::Directories &dirs,
  238. bool force, bool interactive) {
  239. if (dirs.size() == 1) {
  240. // The file already exists in exactly one place.
  241. if (!interactive) {
  242. return dirs[0];
  243. }
  244. CVSSourceDirectory *result = ask_existing(filename, dirs[0]);
  245. if (result != (CVSSourceDirectory *)NULL) {
  246. return result;
  247. }
  248. } else if (dirs.size() > 1) {
  249. // The file already exists in multiple places.
  250. if (force && !interactive) {
  251. return dirs[0];
  252. }
  253. CVSSourceDirectory *result = ask_existing(filename, dirs, suggested_dir);
  254. if (result != (CVSSourceDirectory *)NULL) {
  255. return result;
  256. }
  257. } else { // dirs.empty()
  258. nassertr(dirs.empty(), (CVSSourceDirectory *)NULL);
  259. // The file does not already exist.
  260. if (!interactive) {
  261. return suggested_dir;
  262. }
  263. }
  264. // The file does not already exist, or the user declined to replace
  265. // an existing file.
  266. if (force && !interactive) {
  267. return suggested_dir;
  268. }
  269. if (find(dirs.begin(), dirs.end(), suggested_dir) == dirs.end()) {
  270. CVSSourceDirectory *result = ask_new(filename, suggested_dir);
  271. if (result != (CVSSourceDirectory *)NULL) {
  272. return result;
  273. }
  274. }
  275. // Ask the user where the damn thing should go.
  276. return ask_any(filename);
  277. }
  278. ////////////////////////////////////////////////////////////////////
  279. // Function: CVSSourceTree::ask_existing
  280. // Access: Private
  281. // Description: Asks the user if he wants to replace an existing
  282. // file.
  283. ////////////////////////////////////////////////////////////////////
  284. CVSSourceDirectory *CVSSourceTree::
  285. ask_existing(const string &filename, CVSSourceDirectory *dir) {
  286. while (true) {
  287. nout << filename << " found in tree at "
  288. << dir->get_path() + "/" + filename << ".\n";
  289. string result = prompt("Overwrite this file (y/n)? ");
  290. nassertr(!result.empty(), (CVSSourceDirectory *)NULL);
  291. if (result.size() == 1) {
  292. if (tolower(result[0]) == 'y') {
  293. return dir;
  294. } else if (tolower(result[0]) == 'n') {
  295. return NULL;
  296. }
  297. }
  298. nout << "*** Invalid response: " << result << "\n\n";
  299. }
  300. }
  301. ////////////////////////////////////////////////////////////////////
  302. // Function: CVSSourceTree::ask_existing
  303. // Access: Private
  304. // Description: Asks the user which of several existing files he
  305. // wants to replace.
  306. ////////////////////////////////////////////////////////////////////
  307. CVSSourceDirectory *CVSSourceTree::
  308. ask_existing(const string &filename, const CVSSourceTree::Directories &dirs,
  309. CVSSourceDirectory *suggested_dir) {
  310. while (true) {
  311. nout << filename << " found in tree at more than one place:\n";
  312. bool any_suggested = false;
  313. for (int i = 0; i < (int)dirs.size(); i++) {
  314. nout << " " << (i + 1) << ". "
  315. << dirs[i]->get_path() + "/" + filename << "\n";
  316. if (dirs[i] == suggested_dir) {
  317. any_suggested = true;
  318. }
  319. }
  320. int next_option = dirs.size() + 1;
  321. int suggested_option = -1;
  322. if (!any_suggested) {
  323. suggested_option = next_option;
  324. next_option++;
  325. nout << "\n" << suggested_option
  326. << ". create "
  327. << suggested_dir->get_path() + "/" + filename
  328. << "\n";
  329. }
  330. int other_option = next_option;
  331. nout << other_option << ". Other\n";
  332. string result = prompt("Choose an option: ");
  333. nassertr(!result.empty(), (CVSSourceDirectory *)NULL);
  334. const char *nptr = result.c_str();
  335. char *endptr;
  336. int option = strtol(nptr, &endptr, 10);
  337. if (*endptr == '\0') {
  338. if (option >= 1 && option <= (int)dirs.size()) {
  339. return dirs[option - 1];
  340. } else if (option == suggested_option) {
  341. return suggested_dir;
  342. } else if (option == other_option) {
  343. return NULL;
  344. }
  345. }
  346. nout << "*** Invalid response: " << result << "\n\n";
  347. }
  348. }
  349. ////////////////////////////////////////////////////////////////////
  350. // Function: CVSSourceTree::ask_new
  351. // Access: Private
  352. // Description: Asks the user if he wants to replace an existing
  353. // file.
  354. ////////////////////////////////////////////////////////////////////
  355. CVSSourceDirectory *CVSSourceTree::
  356. ask_new(const string &filename, CVSSourceDirectory *dir) {
  357. while (true) {
  358. nout << filename << " will be created at "
  359. << dir->get_path() + "/" + filename << ".\n";
  360. string result = prompt("Create this file (y/n)? ");
  361. nassertr(!result.empty(), (CVSSourceDirectory *)NULL);
  362. if (result.size() == 1) {
  363. if (tolower(result[0]) == 'y') {
  364. return dir;
  365. } else if (tolower(result[0]) == 'n') {
  366. return NULL;
  367. }
  368. }
  369. nout << "*** Invalid response: " << result << "\n\n";
  370. }
  371. }
  372. ////////////////////////////////////////////////////////////////////
  373. // Function: CVSSourceTree::ask_any
  374. // Access: Private
  375. // Description: Asks the user to type in the name of the directory in
  376. // which to store the file.
  377. ////////////////////////////////////////////////////////////////////
  378. CVSSourceDirectory *CVSSourceTree::
  379. ask_any(const string &filename) {
  380. while (true) {
  381. string result =
  382. prompt("Enter the name of the directory to copy " + filename + " to: ");
  383. nassertr(!result.empty(), (CVSSourceDirectory *)NULL);
  384. // The user might enter a fully-qualified path to the directory,
  385. // or a relative path from the root (with or without the root's
  386. // dirname), or the dirname of the particular directory.
  387. CVSSourceDirectory *dir = find_directory(result);
  388. if (dir == (CVSSourceDirectory *)NULL) {
  389. dir = find_relpath(result);
  390. }
  391. if (dir == (CVSSourceDirectory *)NULL) {
  392. dir = find_dirname(result);
  393. }
  394. if (dir != (CVSSourceDirectory *)NULL) {
  395. return dir;
  396. }
  397. nout << "*** Not a valid directory name: " << result << "\n\n";
  398. }
  399. }
  400. ////////////////////////////////////////////////////////////////////
  401. // Function: CVSSourceTree::prompt
  402. // Access: Private
  403. // Description: Issues a prompt to the user and waits for a typed
  404. // response. Returns the response (which will not be
  405. // empty).
  406. ////////////////////////////////////////////////////////////////////
  407. string CVSSourceTree::
  408. prompt(const string &message) {
  409. nout << flush;
  410. while (true) {
  411. cerr << message << flush;
  412. string response;
  413. getline(cin, response);
  414. // Remove leading and trailing whitespace.
  415. size_t p = 0;
  416. while (p < response.length() && isspace(response[p])) {
  417. p++;
  418. }
  419. size_t q = response.length();
  420. while (q > p && isspace(response[q - 1])) {
  421. q--;
  422. }
  423. if (q > p) {
  424. return response.substr(p, q - p);
  425. }
  426. }
  427. }
  428. ////////////////////////////////////////////////////////////////////
  429. // Function: CVSSourceTree::get_actual_fullpath
  430. // Access: Private, Static
  431. // Description: Determines the actual full path from the root to the
  432. // named directory. It does this essentially by cd'ing
  433. // to the directory and doing pwd, then cd'ing back.
  434. // Returns the empty string if the directory is invalid
  435. // or cannot be cd'ed into.
  436. ////////////////////////////////////////////////////////////////////
  437. string CVSSourceTree::
  438. get_actual_fullpath(const string &path) {
  439. if (!temp_chdir(path)) {
  440. return string();
  441. }
  442. string cwd = get_cwd();
  443. restore_cwd();
  444. return cwd;
  445. }
  446. ////////////////////////////////////////////////////////////////////
  447. // Function: CVSSourceTree::get_start_fullpath
  448. // Access: Private, Static
  449. // Description: Returns the full path from the root to the directory
  450. // in which the user started the program.
  451. ////////////////////////////////////////////////////////////////////
  452. string CVSSourceTree::
  453. get_start_fullpath() {
  454. if (!_got_start_fullpath) {
  455. _start_fullpath = get_cwd();
  456. _got_start_fullpath = true;
  457. }
  458. return _start_fullpath;
  459. }
  460. ////////////////////////////////////////////////////////////////////
  461. // Function: CVSSourceTree::get_cwd
  462. // Access: Private, Static
  463. // Description: Calls the system getcwd(), automatically allocating a
  464. // large enough string.
  465. ////////////////////////////////////////////////////////////////////
  466. string CVSSourceTree::
  467. get_cwd() {
  468. static size_t bufsize = 1024;
  469. static char *buffer = NULL;
  470. if (buffer == (char *)NULL) {
  471. buffer = new char[bufsize];
  472. }
  473. while (getcwd(buffer, bufsize) == (char *)NULL) {
  474. if (errno != ERANGE) {
  475. perror("getcwd");
  476. return string();
  477. }
  478. delete[] buffer;
  479. bufsize = bufsize * 2;
  480. buffer = new char[bufsize];
  481. nassertr(buffer != (char *)NULL, string());
  482. }
  483. return string(buffer);
  484. }