[1] | 1 | |
---|
| 2 | #include "cmt_std.h" |
---|
| 3 | #include "cmt_system.h" |
---|
| 4 | |
---|
| 5 | //---------------------------------------------------------- |
---|
| 6 | class FileScanner |
---|
| 7 | { |
---|
| 8 | public: |
---|
| 9 | class actor |
---|
| 10 | { |
---|
| 11 | public: |
---|
| 12 | virtual void run () |
---|
| 13 | { |
---|
| 14 | } |
---|
| 15 | }; |
---|
| 16 | |
---|
| 17 | FileScanner (const cmt_string& new_root); |
---|
| 18 | bool scan (actor& a); |
---|
| 19 | |
---|
| 20 | private: |
---|
| 21 | void scan (int level, actor& a); |
---|
| 22 | |
---|
| 23 | cmt_string m_root; |
---|
| 24 | cmt_string m_full_root; |
---|
| 25 | }; |
---|
| 26 | //---------------------------------------------------------- |
---|
| 27 | |
---|
| 28 | //---------------------------------------------------------- |
---|
| 29 | FileScanner::FileScanner (const cmt_string& new_root) : |
---|
| 30 | m_root(new_root), |
---|
| 31 | m_full_root(new_root) |
---|
| 32 | //---------------------------------------------------------- |
---|
| 33 | { |
---|
| 34 | if (m_root[0] == ':') |
---|
| 35 | { |
---|
| 36 | // |
---|
| 37 | // The new CVS root points to a remote repository. |
---|
| 38 | // |
---|
| 39 | int pos = m_root.find_last_of (":"); |
---|
| 40 | if (pos != cmt_string::npos) |
---|
| 41 | { |
---|
| 42 | m_root.erase (0, pos); |
---|
| 43 | } |
---|
| 44 | else |
---|
| 45 | { |
---|
| 46 | // A syntax error ??? |
---|
| 47 | cout << "The new CVS root is wrongly specified :[" << m_full_root << "]" << endl; |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | //---------------------------------------------------------- |
---|
| 53 | bool FileScanner::scan (actor& a) |
---|
| 54 | //---------------------------------------------------------- |
---|
| 55 | { |
---|
| 56 | scan (0, a); |
---|
| 57 | |
---|
| 58 | return (true); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | //---------------------------------------------------------- |
---|
| 62 | void FileScanner::scan (int level, actor& a) |
---|
| 63 | //---------------------------------------------------------- |
---|
| 64 | { |
---|
| 65 | CmtSystem::cmt_string_vector list; |
---|
| 66 | |
---|
| 67 | CmtSystem::scan_dir ("*", list); |
---|
| 68 | |
---|
| 69 | if (list.size () == 0) return; |
---|
| 70 | |
---|
| 71 | // Will be set if at least one sub-directory is a CVS directory |
---|
| 72 | bool has_cvs = false; |
---|
| 73 | |
---|
| 74 | if (CmtSystem::test_directory ("CVS")) has_cvs = true; |
---|
| 75 | |
---|
| 76 | if (!has_cvs) return; |
---|
| 77 | |
---|
| 78 | cmt_string cvs_root; |
---|
| 79 | cmt_string cvs_repository; |
---|
| 80 | |
---|
| 81 | cvs_root.read ("CVS/Root"); |
---|
| 82 | cvs_repository.read ("CVS/Repository"); |
---|
| 83 | |
---|
| 84 | int pos; |
---|
| 85 | |
---|
| 86 | if (cvs_root[0] == ':') |
---|
| 87 | { |
---|
| 88 | // |
---|
| 89 | // The current CVS root points to a remote repository. |
---|
| 90 | // |
---|
| 91 | pos = cvs_root.find_last_of (":"); |
---|
| 92 | if (pos != cmt_string::npos) |
---|
| 93 | { |
---|
| 94 | cvs_root.erase (0, pos); |
---|
| 95 | } |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | // A syntax error ??? |
---|
| 99 | cout << "The CVS/Root file is corrupted" << endl; |
---|
| 100 | return; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | pos = cvs_repository.find (cvs_root); |
---|
| 105 | if (pos == 0) |
---|
| 106 | { |
---|
| 107 | cvs_repository.replace (cvs_root, m_root); |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | // This case should never happen !!! |
---|
| 112 | cout << "The CVS/Repository file does not match CVS/Root" << endl; |
---|
| 113 | return; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if (m_full_root.write ("CVS/Root")) |
---|
| 117 | { |
---|
| 118 | cout << "CVS root changed from " << cvs_root << " to " << m_full_root << endl; |
---|
| 119 | } |
---|
| 120 | else |
---|
| 121 | { |
---|
| 122 | cout << "cannot change CVS root in " << CmtSystem::pwd () << endl; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if (cvs_repository.write ("CVS/Repository")) |
---|
| 126 | { |
---|
| 127 | cout << "CVS repository changed from " << cvs_root << " to " << m_root << endl; |
---|
| 128 | } |
---|
| 129 | else |
---|
| 130 | { |
---|
| 131 | cout << "cannot change CVS repository in " << CmtSystem::pwd () << endl; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | int i; |
---|
| 135 | for (i = 0; i < list.size (); i++) |
---|
| 136 | { |
---|
| 137 | cmt_string& name = list[i]; |
---|
| 138 | |
---|
| 139 | if (name[0] == CmtSystem::file_separator ()) name.erase (0, 1); |
---|
| 140 | |
---|
| 141 | if (CmtSystem::test_directory (name)) |
---|
| 142 | { |
---|
| 143 | CmtSystem::cd (name); |
---|
| 144 | scan (level + 1, a); |
---|
| 145 | CmtSystem::cd (".."); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | int main (int argc, char* argv[]) |
---|
| 151 | { |
---|
| 152 | if (argc < 2) |
---|
| 153 | { |
---|
| 154 | printf ("cvsmove <new-repository>\n"); |
---|
| 155 | return (1); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | cmt_string new_repository = argv[1]; |
---|
| 159 | |
---|
| 160 | FileScanner scanner (new_repository); |
---|
| 161 | FileScanner::actor actor; |
---|
| 162 | |
---|
| 163 | CmtSystem::cd (".."); |
---|
| 164 | scanner.scan (actor); |
---|
| 165 | |
---|
| 166 | return (0); |
---|
| 167 | } |
---|