ctcvs.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. # given a possibly empty string, format it into a comment or -nc
  2. # input is in:
  3. # $_[0] = possible comment string
  4. #
  5. # output is:
  6. # string for use by CVS functions
  7. sub CTCvsFormatComment {
  8. local( $ret ) = "" ;
  9. if ( $_[0] ne "" ) {
  10. $ret = "-m \"" . $_[0] . "\"" ;
  11. }
  12. $ret ;
  13. }
  14. # given a project and spec line, compute the server line
  15. # input is in:
  16. # $_[0] = project
  17. # $_[1] = spec line
  18. #
  19. # output:
  20. # return a sever line, or "" if not a croot
  21. sub CTCvsServerLine {
  22. &CTUDebug( "in CTCvsServerLine\n" ) ;
  23. local( $ret ) = "" ;
  24. local( $type ) = &CTSpecType( $_[1] ) ;
  25. if ( $type eq "croot" ) {
  26. local( $options ) = &CTSpecOptions( $_[1] ) ;
  27. local( $sline ) = &CTSpecFindOption( $options, "server" ) ;
  28. if ( $sline ne "" ) {
  29. $ret = join( ":", split( /,/, $sline ));
  30. }
  31. }
  32. &CTUDebug( "out of CTCvsServerLine\n" ) ;
  33. $ret ;
  34. }
  35. # if needed log into a cvs server
  36. # input is in:
  37. # $_[0] = server line
  38. #
  39. # output:
  40. # return success or failure
  41. sub CTCvsLogin {
  42. &CTUDebug( "in CTCvsLogin\n" ) ;
  43. local( $ret ) = 0 ;
  44. &CTUDebug( "server line is '" . $_[0] . "'\n" ) ;
  45. if ( $_[0] ne "" ) {
  46. # ok. we actually have something, lets look in .cvspass
  47. local( $path ) ;
  48. local( *PASSFILE ) ;
  49. if ( $ENV{"PENV"} eq "WIN32" ) {
  50. $path = $ENV{"HOME"} . "/.cvspass" ;
  51. } else {
  52. # $path = "~/.cvspass" ;
  53. $path = $ENV{"HOME"} . "/.cvspass" ;
  54. }
  55. &CTUDebug( "looking for '" . $path . "'\n" ) ;
  56. if ( -e $path ) {
  57. local( $passdone ) = 0 ;
  58. local( $ok ) = 0 ;
  59. open( PASSFILE, "< $path" ) ;
  60. while ( <PASSFILE> ) {
  61. s/\n$// ;
  62. local( @line ) = split ;
  63. # ok, the server line is in [0] and the password in [1].
  64. &CTUDebug( "server line from .cvspass is '" . $line[0] .
  65. "'\n" ) ;
  66. if ( $line[0] eq $_[0] ) {
  67. # we're fine, we're already logged in to that
  68. $ret = 1 ;
  69. $passdone = 1;
  70. }
  71. }
  72. if ( ! $passdone ) {
  73. # ran out of lines in the file
  74. local( $line ) = "cvs -d " . $_[0] . " login >/dev/null" ;
  75. &CTUDebug( "about to run '" . $line . "'\n" ) ;
  76. $ret = &CTURetCode( system( $line )) ;
  77. }
  78. } else {
  79. &CTUDebug( $path . " file does not exist\n" ) ;
  80. local( $line ) = "cvs -d " . $_[0] . " login >/dev/null" ;
  81. &CTUDebug( "about to run '" . $line . "'\n" ) ;
  82. $ret = &CTURetCode( system( $line )) ;
  83. }
  84. }
  85. &CTUDebug( "out of CTCvsLogin\n" ) ;
  86. $ret ;
  87. }
  88. require "$tool/include/ctproj.pl" ;
  89. # add a versioned element to the repository
  90. # input is in:
  91. # $_[0] = element
  92. # $_[1] = project
  93. # $_[2] = spec line
  94. # $_[3] = possible comment
  95. #
  96. # output:
  97. # return success or failure
  98. sub CTCvsAdd {
  99. &CTUDebug( "in CTCvsAdd\n" ) ;
  100. # first we need to 'login' to the repository
  101. local( $comment ) = &CTCvsFormatComment( $_[3] ) ;
  102. local( $serve ) = &CTCvsServerLine( $_[1], $_[2] ) ;
  103. local( $ret ) = &CTCvsLogin( $serve ) ;
  104. if ( $ret ) {
  105. # now issue the add command
  106. local( $root ) = &CTProjRoot( $_[1] ) ;
  107. local( $line ) = "" ;
  108. local( $elem ) = $_[0] ;
  109. if ( $elem =~ /^\// ) {
  110. local( $proj ) = $_[1] ;
  111. $proj =~ tr/a-z/A-Z/ ;
  112. $line = "cd \$" . $proj . "; " ;
  113. $elem =~ s/^$root\/// ;
  114. }
  115. $line = $line . "cvs -d " . $serve . " add " . $comment . " $elem" ;
  116. &CTUDebug( "about to execute '" . $line . "'\n" ) ;
  117. $ret = &CTURetCode( system( $line )) ;
  118. }
  119. &CTUDebug( "out of CTCvsAdd\n" ) ;
  120. $ret ;
  121. }
  122. # ci a versioned element to the repository
  123. # input is in:
  124. # $_[0] = element
  125. # $_[1] = project
  126. # $_[2] = spec line
  127. # $_[3] = possible comment
  128. #
  129. # output:
  130. # return success or failure
  131. sub CTCvsCi {
  132. &CTUDebug( "in CTCvsCi\n" ) ;
  133. # first we need to 'login' to the repository
  134. local( $comment ) = &CTCvsFormatComment( $_[3] ) ;
  135. local( $serve ) = &CTCvsServerLine( $_[1], $_[2] ) ;
  136. local( $ret ) = &CTCvsLogin( $serve ) ;
  137. if ( $ret ) {
  138. # now issue the add command
  139. local( $root ) = &CTProjRoot( $_[1] ) ;
  140. local( $line ) = "" ;
  141. local( $elem ) = $_[0] ;
  142. if ( $elem =~ /^\// ) {
  143. local ( $proj ) = $_[1] ;
  144. $proj =~ tr/a-z/A-Z/ ;
  145. $line = "cd \$" . $proj . "; " ;
  146. $elem =~ s/^$root\/// ;
  147. }
  148. $line = $line . "cvs -d " . $serve . " ci " . $comment . " $elem" ;
  149. &CTUDebug( "about to execute '" . $line . "'\n" ) ;
  150. $ret = &CTURetCode( system( $line )) ;
  151. }
  152. &CTUDebug( "out of CTCvsCi\n" ) ;
  153. $ret ;
  154. }
  155. # rm a versioned element from the repository
  156. # input is in:
  157. # $_[0] = element
  158. # $_[1] = project
  159. # $_[2] = spec line
  160. #
  161. # output:
  162. # return success or failure
  163. sub CTCvsRm {
  164. &CTUDebug( "in CTCvsRm\n" ) ;
  165. # first we need to 'login' to the repository
  166. local( $serve ) = &CTCvsServerLine( $_[1], $_[2] ) ;
  167. local( $ret ) = &CTCvsLogin( $serve ) ;
  168. if ( $ret ) {
  169. # now issue the add command
  170. $ret = &CTURetCode( system( "cvs -d " . $serve . " rm $_[0]\n" )) ;
  171. }
  172. &CTUDebug( "out of CTCvsRm\n" ) ;
  173. $ret ;
  174. }
  175. # make a versioned directory
  176. # input is in:
  177. # $_[0] = directory to create
  178. # $_[1] = project
  179. # $_[2] = spec line
  180. # $_[3] = possible comment
  181. #
  182. # output:
  183. # return success or failure
  184. sub CTCvsMkdir {
  185. &CTUDebug( "in CTCvsMkdir\n" ) ;
  186. local( $ret ) = 0 ;
  187. # first make the dir
  188. $ret = &CTURetCode( system( "mkdir $_[0]\n" )) ;
  189. if ( $ret ) {
  190. # now version it
  191. $ret = &CTCvsAdd( $_[0], $_[1], $_[2], $_[3] ) ;
  192. } else {
  193. &CTUDebug( "could not create directory '" . $_[0] . "'\n" ) ;
  194. $ret = 0 ;
  195. }
  196. &CTUDebug( "out of CTCvsMkdir\n" ) ;
  197. $ret ;
  198. }
  199. # make a versioned element
  200. # input is in:
  201. # $_[0] = element to version
  202. # $_[1] = project
  203. # $_[2] = spec line
  204. # $_[3] = possible comment
  205. #
  206. # output:
  207. # return success or failure
  208. sub CTCvsMkelem {
  209. &CTUDebug( "in CTCvsMkelem\n" ) ;
  210. # first cvs add the file
  211. local( $ret ) = &CTCvsAdd( $_[0], $_[1], $_[2], $_[3] ) ;
  212. if ( $ret ) {
  213. # now commit it
  214. $ret = &CTCvsCi( $_[0], $_[1], $_[2], $_[3] ) ;
  215. } else {
  216. &CTUDebug( "could not CVS add '" . $_[0] . "'\n" ) ;
  217. $ret = 0 ;
  218. }
  219. &CTUDebug( "out of CTCvsMkelem\n" ) ;
  220. $ret ;
  221. }
  222. # delta an element
  223. # input is in:
  224. # $_[0] = element to delta
  225. # $_[1] = project
  226. # $_[2] = spec line
  227. #
  228. # output:
  229. # return success or failure
  230. sub CTCvsDelta {
  231. &CTUDebug( "in CTCvsDelta\n" ) ;
  232. local( $ret ) = 0 ;
  233. # for lack of better idea, this is going to be just checkin for now
  234. if ( -d $_[0] ) {
  235. # we don't version directories in CVS
  236. $ret = 1 ;
  237. } else {
  238. $ret = &CTCvsCi( $_[0], $_[1], $_[2] ) ;
  239. }
  240. &CTUDebug( "out of CTCvsDelta\n" ) ;
  241. $ret ;
  242. }
  243. # checkout an element
  244. # input is in:
  245. # $_[0] = element to checkout
  246. # $_[1] = project
  247. # $_[2] = spec line
  248. # $_[3] = possible comment
  249. #
  250. # output:
  251. # return success or failure
  252. sub CTCvsCheckout {
  253. &CTUDebug( "in CTCvsCheckout\n" ) ;
  254. local( $ret ) = 1 ;
  255. # for my limited understanding of CVS, there doesn't seem to be any
  256. # 'checkout' for it.
  257. &CTUDebug( "out of CTCvsCheckout\n" ) ;
  258. $ret ;
  259. }
  260. # checkin an element
  261. # input is in:
  262. # $_[0] = element to checkin
  263. # $_[1] = project
  264. # $_[2] = spec line
  265. # $_[3] = possible comment
  266. #
  267. # output:
  268. # return success or failure
  269. sub CTCvsCheckin {
  270. &CTUDebug( "in CTCvsCheckin\n" ) ;
  271. local( $ret ) = 0 ;
  272. if ( -d $_[0] ) {
  273. # we don't version directories in CVS
  274. $ret = 1 ;
  275. } else {
  276. $ret = &CTCvsCi( $_[0], $_[1], $_[2], $_[3] ) ;
  277. }
  278. &CTUDebug( "out of CTCvsCheckin\n" ) ;
  279. $ret ;
  280. }
  281. # uncheckout an element
  282. # input is in:
  283. # $_[0] = element to uncheckout
  284. # $_[1] = project
  285. # $_[2] = spec line
  286. #
  287. # output:
  288. # return success or failure
  289. sub CTCvsUncheckout {
  290. &CTUDebug( "in CTCvsUncheckout\n" ) ;
  291. local( $ret ) = 0 ;
  292. if ( -d $_[0] ) {
  293. # we don't version directories in CVS
  294. $ret = 1 ;
  295. } else {
  296. $ret = &CTURetCode( system( "rm $_[0]" ) ) ;
  297. if ( $ret ) {
  298. local( $serve ) = &CTCvsServerLine( $_[1], $_[2] ) ;
  299. $ret = &CTCvsLogin( $serve ) ;
  300. if ( $ret ) {
  301. $ret = &CTURetCode( system( "cvs -d " . $serve . " update " .
  302. $_[0] )) ;
  303. }
  304. }
  305. }
  306. &CTUDebug( "out of CTCvsUncheckout\n" ) ;
  307. $ret ;
  308. }
  309. # figure out what all I have checked out
  310. # input is in:
  311. # $_[0] = project
  312. # $_[1] = flavor
  313. # $_[2] = spec line
  314. #
  315. # output:
  316. # return a \n serperated list of elements checked out
  317. sub CTCvsIHave {
  318. &CTUDebug( "in CTCvsIHave\n" ) ;
  319. local( $ret ) = "" ;
  320. local( $proj ) = $_[0] ;
  321. $proj =~ tr/a-z/A-Z/ ;
  322. local( $line ) = "cd \$" . $proj . "; " ;
  323. local( $serve ) = &CTCvsServerLine( $_[0], $_[2] ) ;
  324. local( $ok ) = &CTCvsLogin( $serve ) ;
  325. if ( $ok ) {
  326. $line = $line . "cvs -n -d " . $serve . " update 2>/dev/null" ;
  327. local( $hold ) = "";
  328. local( *OUTPUT ) ;
  329. open( OUTPUT, $line . " |" ) ;
  330. while ( <OUTPUT> ) {
  331. $hold = $hold . $_ ;
  332. }
  333. close( OUTPUT ) ;
  334. local( @lines ) = split( /\n/, $hold ) ;
  335. local( $item ) ;
  336. foreach $item ( @lines ) {
  337. if ( $item =~ /^\?/ ) {
  338. # things that start with a ? are ignored
  339. } elsif ( $item =~ /^cvs/ ) {
  340. # messages from the server are also ignored
  341. } elsif ( $item =~ /^P/ ) {
  342. # new files are ignored
  343. } elsif ( $item =~ /^U/ ) {
  344. # updates are ignored
  345. } elsif ( $item =~ /^M/ ) {
  346. # here's one we modified
  347. local( @foo ) = split( / /, $item ) ;
  348. $ret = $ret . $foo[1] . "\n" ;
  349. } else {
  350. # don't what this means, better complain
  351. local( @foo ) = split( / /, $item ) ;
  352. print STDERR "got unknown update code '" . $foo[0] .
  353. "' for file '" . $foo[1] . "'\n" ;
  354. }
  355. }
  356. }
  357. &CTUDebug( "out of CTCvsIHave\n" ) ;
  358. $ret ;
  359. }
  360. # remove an element from the repository
  361. # input is in:
  362. # $_[0] = element to uncheckout
  363. # $_[1] = project
  364. # $_[2] = spec line
  365. #
  366. # output:
  367. # return success or failure
  368. sub CTCvsRmElem {
  369. &CTUDebug( "in CTCvsRmElem\n" ) ;
  370. local( $ret ) = 0 ;
  371. if ( -d $_[0] ) {
  372. # CVS doesn't really do this. If there are no files in the directory,
  373. # the next time an update -P is run, it will be deleted.
  374. $ret = 1 ;
  375. } else {
  376. $ret = &CTURetCode( system( "rm $_[0]" ) ) ;
  377. if ( $ret ) {
  378. $ret = &CTCvsRm( $_[0], $_[1], $_[2] ) ;
  379. if ( $ret ) {
  380. $ret = &CTCvsCi( $_[0], $_[1], $_[2] ) ;
  381. }
  382. }
  383. }
  384. &CTUDebug( "out of CTCvsRmElem\n" ) ;
  385. $ret ;
  386. }
  387. # move a versioned element from one name to another
  388. # input is in:
  389. # $_[0] = from element
  390. # $_[1] = to element
  391. # $_[2] = project
  392. # $_[3] = spec line
  393. #
  394. # output:
  395. # return success or failure
  396. sub CTCvsMv {
  397. &CTUDebug( "in CTCvsMv\n" ) ;
  398. local( $ret ) = 0 ;
  399. if ( -d $_[0] ) {
  400. # don't have code to do directories yet. See pp 54 of the CVS book
  401. $ret = 0 ;
  402. } else {
  403. $ret = &CTURetCode( system( "mv $_[0] $_[1]" ) ) ;
  404. if ( $ret ) {
  405. $ret = &CTCvsRm( $_[0], $_[2], $_[3] ) ;
  406. if ( $ret ) {
  407. $ret = &CTCvsAdd( $_[1], $_[2], $_[3] );
  408. if ( $ret ) {
  409. $ret = &CTCvsCi( $_[0], $_[2], $_[3] ) ;
  410. if ( $ret ) {
  411. $ret = &CTCvsCi( $_[1], $_[2], $_[3] ) ;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. &CTUDebug( "out of CTCvsMv\n" ) ;
  418. $ret ;
  419. }
  420. # build a list of targets
  421. # input is in:
  422. # $_[0] = targets
  423. #
  424. # output:
  425. # return success or failure
  426. sub CTCvsMake {
  427. &CTUDebug( "in CTCvsMake\n" ) ;
  428. local( $ret ) = 0 ;
  429. local( $line ) = "make " . $_[0] . "\n" ;
  430. $ret = &CTURetCode( system( $line )) ;
  431. &CTUDebug( "out of CTCvsMake\n" ) ;
  432. $ret ;
  433. }
  434. 1;