| 1 | /* |
| 2 | * Copyright (c) 2002-2004 Christian Loose <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) 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 GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "cvsservice.h" |
| 22 | #include "../debug.h" |
| 23 | |
| 24 | #include <QApplication> |
| 25 | #include <QHash> |
| 26 | #include <qstring.h> |
| 27 | |
| 28 | #include <KDBusService> |
| 29 | #include <KLocalizedString> |
| 30 | #include <KSharedConfig> |
| 31 | #include <kconfig.h> |
| 32 | #include <kmessagebox.h> |
| 33 | #include <kshell.h> |
| 34 | |
| 35 | #include "cvsjob.h" |
| 36 | #include "cvsloginjob.h" |
| 37 | #include "cvsserviceadaptor.h" |
| 38 | #include "cvsserviceutils.h" |
| 39 | #include "repository.h" |
| 40 | #include "sshagent.h" |
| 41 | #include <cvsjobadaptor.h> |
| 42 | #include <kconfiggroup.h> |
| 43 | |
| 44 | static const char SINGLE_JOB_ID[] = "NonConcurrentJob"; |
| 45 | static const char REDIRECT_STDERR[] = "2>&1"; |
| 46 | |
| 47 | enum WatchEvents { None = 0, All = 1, Commits = 2, Edits = 4, Unedits = 8 }; |
| 48 | |
| 49 | struct CvsService::Private { |
| 50 | Private() |
| 51 | : singleCvsJob(0) |
| 52 | , lastJobId(0) |
| 53 | , repository(0) |
| 54 | { |
| 55 | } |
| 56 | ~Private() |
| 57 | { |
| 58 | delete repository; |
| 59 | delete singleCvsJob; |
| 60 | } |
| 61 | |
| 62 | CvsJob *singleCvsJob; // non-concurrent cvs job, like update or commit |
| 63 | QHash<int, CvsJob *> cvsJobs; // concurrent cvs jobs, like diff or annotate |
| 64 | QHash<int, CvsLoginJob *> loginJobs; |
| 65 | unsigned lastJobId; |
| 66 | |
| 67 | Repository *repository; |
| 68 | |
| 69 | CvsJob *createCvsJob(); |
| 70 | QDBusObjectPath setupNonConcurrentJob(Repository *repo = 0); |
| 71 | |
| 72 | bool hasWorkingCopy(); |
| 73 | bool hasRunningJob(); |
| 74 | }; |
| 75 | |
| 76 | CvsService::CvsService() |
| 77 | : d(new Private) |
| 78 | { |
| 79 | (void)new CvsserviceAdaptor(this); |
| 80 | QDBusConnection::sessionBus().registerObject("/CvsService", this); |
| 81 | |
| 82 | // create non-concurrent cvs job |
| 83 | d->singleCvsJob = new CvsJob(SINGLE_JOB_ID); |
| 84 | |
| 85 | // create repository manager |
| 86 | d->repository = new Repository(); |
| 87 | |
| 88 | KConfigGroup cs(KSharedConfig::openConfig(), "General"); |
| 89 | if (cs.readEntry("UseSshAgent", false)) { |
| 90 | // use the existing or start a new ssh-agent |
| 91 | SshAgent ssh; |
| 92 | // TODO CL do we need the return value? |
| 93 | // bool res = ssh.querySshAgent(); |
| 94 | ssh.querySshAgent(); |
| 95 | } |
| 96 | |
| 97 | new KDBusService(KDBusService::Multiple, this); |
| 98 | } |
| 99 | |
| 100 | CvsService::~CvsService() |
| 101 | { |
| 102 | // kill the ssh-agent (when we started it) |
| 103 | SshAgent ssh; |
| 104 | ssh.killSshAgent(); |
| 105 | |
| 106 | qDeleteAll(d->cvsJobs); |
| 107 | d->cvsJobs.clear(); |
| 108 | |
| 109 | qDeleteAll(d->loginJobs); |
| 110 | d->loginJobs.clear(); |
| 111 | |
| 112 | delete d; |
| 113 | } |
| 114 | |
| 115 | QDBusObjectPath CvsService::add(const QStringList &files, bool isBinary) |
| 116 | { |
| 117 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 118 | return {}; |
| 119 | |
| 120 | // assemble the command line |
| 121 | // cvs add [-kb] [FILES] |
| 122 | d->singleCvsJob->clearCvsCommand(); |
| 123 | |
| 124 | *d->singleCvsJob << d->repository->cvsClient() << "add"; |
| 125 | |
| 126 | if (isBinary) |
| 127 | *d->singleCvsJob << "-kb"; |
| 128 | |
| 129 | *d->singleCvsJob << CvsServiceUtils::joinFileList(files) << REDIRECT_STDERR; |
| 130 | |
| 131 | return d->setupNonConcurrentJob(); |
| 132 | } |
| 133 | |
| 134 | QDBusObjectPath CvsService::addWatch(const QStringList &files, int events) |
| 135 | { |
| 136 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 137 | return {}; |
| 138 | |
| 139 | // assemble the command line |
| 140 | d->singleCvsJob->clearCvsCommand(); |
| 141 | |
| 142 | *d->singleCvsJob << d->repository->cvsClient() << "watch add"; |
| 143 | |
| 144 | if (events != All) { |
| 145 | if (events & Commits) |
| 146 | *d->singleCvsJob << "-a commit"; |
| 147 | if (events & Edits) |
| 148 | *d->singleCvsJob << "-a edit"; |
| 149 | if (events & Unedits) |
| 150 | *d->singleCvsJob << "-a unedit"; |
| 151 | } |
| 152 | |
| 153 | *d->singleCvsJob << CvsServiceUtils::joinFileList(files); |
| 154 | |
| 155 | return d->setupNonConcurrentJob(); |
| 156 | } |
| 157 | |
| 158 | QDBusObjectPath CvsService::annotate(const QString &fileName, const QString &revision) |
| 159 | { |
| 160 | if (!d->hasWorkingCopy()) |
| 161 | return {}; |
| 162 | |
| 163 | // create a cvs job |
| 164 | CvsJob *job = d->createCvsJob(); |
| 165 | |
| 166 | // assemble the command line |
| 167 | // (cvs log [FILE] && cvs annotate [-r rev] [FILE]) |
| 168 | QString quotedName = KShell::quoteArg(fileName); |
| 169 | QString cvsClient = d->repository->cvsClient(); |
| 170 | |
| 171 | *job << "(" << cvsClient << "log" << quotedName << "&&" << cvsClient << "annotate"; |
| 172 | |
| 173 | if (!revision.isEmpty()) |
| 174 | *job << "-r" << revision; |
| 175 | |
| 176 | // *Hack* |
| 177 | // because the string "Annotations for blabla" is |
| 178 | // printed to stderr even with option -Q. |
| 179 | *job << quotedName << ")" << REDIRECT_STDERR; |
| 180 | return QDBusObjectPath(job->dbusObjectPath()); |
| 181 | } |
| 182 | |
| 183 | QDBusObjectPath CvsService::checkout(const QString &workingDir, const QString &repository, const QString &module, const QString &tag, bool pruneDirs) |
| 184 | { |
| 185 | if (d->hasRunningJob()) |
| 186 | return {}; |
| 187 | |
| 188 | Repository repo(repository); |
| 189 | |
| 190 | // assemble the command line |
| 191 | // cd [DIRECTORY] && cvs -d [REPOSITORY] checkout [-r tag] [-P] [MODULE] |
| 192 | d->singleCvsJob->clearCvsCommand(); |
| 193 | |
| 194 | *d->singleCvsJob << "cd" << KShell::quoteArg(workingDir) << "&&" << repo.cvsClient() << "-d" << repository << "checkout"; |
| 195 | |
| 196 | if (!tag.isEmpty()) |
| 197 | *d->singleCvsJob << "-r" << tag; |
| 198 | |
| 199 | if (pruneDirs) |
| 200 | *d->singleCvsJob << "-P"; |
| 201 | |
| 202 | *d->singleCvsJob << module; |
| 203 | |
| 204 | return d->setupNonConcurrentJob(&repo); |
| 205 | } |
| 206 | |
| 207 | QDBusObjectPath CvsService::checkout(const QString &workingDir, |
| 208 | const QString &repository, |
| 209 | const QString &module, |
| 210 | const QString &tag, |
| 211 | bool pruneDirs, |
| 212 | const QString &alias, |
| 213 | bool exportOnly) |
| 214 | { |
| 215 | if (d->hasRunningJob()) |
| 216 | return {}; |
| 217 | |
| 218 | Repository repo(repository); |
| 219 | |
| 220 | // assemble the command line |
| 221 | // cd [DIRECTORY] && cvs -d [REPOSITORY] co [-r tag] [-P] [-d alias] [MODULE] |
| 222 | d->singleCvsJob->clearCvsCommand(); |
| 223 | |
| 224 | *d->singleCvsJob << "cd" << KShell::quoteArg(workingDir) << "&&" << repo.cvsClient() << "-d" << repository; |
| 225 | if (exportOnly) |
| 226 | *d->singleCvsJob << "export"; |
| 227 | else |
| 228 | *d->singleCvsJob << "checkout"; |
| 229 | |
| 230 | if (!tag.isEmpty()) |
| 231 | *d->singleCvsJob << "-r" << tag; |
| 232 | |
| 233 | if (pruneDirs && !exportOnly) |
| 234 | *d->singleCvsJob << "-P"; |
| 235 | |
| 236 | if (!alias.isEmpty()) |
| 237 | *d->singleCvsJob << "-d" << alias; |
| 238 | |
| 239 | *d->singleCvsJob << module; |
| 240 | |
| 241 | return d->setupNonConcurrentJob(&repo); |
| 242 | } |
| 243 | |
| 244 | QDBusObjectPath CvsService::checkout(const QString &workingDir, |
| 245 | const QString &repository, |
| 246 | const QString &module, |
| 247 | const QString &tag, |
| 248 | bool pruneDirs, |
| 249 | const QString &alias, |
| 250 | bool exportOnly, |
| 251 | bool recursive) |
| 252 | { |
| 253 | if (d->hasRunningJob()) |
| 254 | return {}; |
| 255 | |
| 256 | Repository repo(repository); |
| 257 | |
| 258 | // assemble the command line |
| 259 | // cd [DIRECTORY] && cvs -d [REPOSITORY] co [-r tag] [-P] [-d alias] [MODULE] |
| 260 | d->singleCvsJob->clearCvsCommand(); |
| 261 | |
| 262 | *d->singleCvsJob << "cd" << KShell::quoteArg(workingDir) << "&&" << repo.cvsClient() << "-d" << repository; |
| 263 | if (exportOnly) |
| 264 | *d->singleCvsJob << "export"; |
| 265 | else |
| 266 | *d->singleCvsJob << "checkout"; |
| 267 | |
| 268 | if (!tag.isEmpty()) |
| 269 | *d->singleCvsJob << "-r" << tag; |
| 270 | |
| 271 | if (pruneDirs && !exportOnly) |
| 272 | *d->singleCvsJob << "-P"; |
| 273 | |
| 274 | if (!alias.isEmpty()) |
| 275 | *d->singleCvsJob << "-d" << alias; |
| 276 | |
| 277 | if (!recursive) |
| 278 | *d->singleCvsJob << "-l"; |
| 279 | |
| 280 | *d->singleCvsJob << module; |
| 281 | |
| 282 | return d->setupNonConcurrentJob(&repo); |
| 283 | } |
| 284 | |
| 285 | QDBusObjectPath CvsService::commit(const QStringList &files, const QString &commitMessage, bool recursive) |
| 286 | { |
| 287 | qCDebug(log_cervisia) << "d->hasWorkingCopy:" << d->hasWorkingCopy() << "d->hasRunningJob:" << d->hasRunningJob(); |
| 288 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 289 | return {}; |
| 290 | |
| 291 | // assemble the command line |
| 292 | // cvs commit [-l] [-m MESSAGE] [FILES] |
| 293 | d->singleCvsJob->clearCvsCommand(); |
| 294 | |
| 295 | *d->singleCvsJob << d->repository->cvsClient() << "commit"; |
| 296 | |
| 297 | if (!recursive) |
| 298 | *d->singleCvsJob << "-l"; |
| 299 | |
| 300 | *d->singleCvsJob << "-m" << KShell::quoteArg(commitMessage) << CvsServiceUtils::joinFileList(files) << REDIRECT_STDERR; |
| 301 | |
| 302 | qCDebug(log_cervisia) << "end"; |
| 303 | return d->setupNonConcurrentJob(); |
| 304 | } |
| 305 | |
| 306 | QDBusObjectPath CvsService::createRepository(const QString &repository) |
| 307 | { |
| 308 | if (d->hasRunningJob()) |
| 309 | return {}; |
| 310 | |
| 311 | // assemble the command line |
| 312 | // cvs -d [REPOSITORY] init |
| 313 | d->singleCvsJob->clearCvsCommand(); |
| 314 | |
| 315 | *d->singleCvsJob << "mkdir -p" << KShell::quoteArg(repository) << "&&" << d->repository->cvsClient() << "-d" << KShell::quoteArg(repository) << "init"; |
| 316 | |
| 317 | return d->setupNonConcurrentJob(); |
| 318 | } |
| 319 | |
| 320 | QDBusObjectPath CvsService::createTag(const QStringList &files, const QString &tag, bool branch, bool force) |
| 321 | { |
| 322 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 323 | return {}; |
| 324 | |
| 325 | // assemble the command line |
| 326 | // cvs tag [-b] [-F] [TAG] [FILES] |
| 327 | d->singleCvsJob->clearCvsCommand(); |
| 328 | |
| 329 | *d->singleCvsJob << d->repository->cvsClient() << "tag"; |
| 330 | |
| 331 | if (branch) |
| 332 | *d->singleCvsJob << "-b"; |
| 333 | |
| 334 | if (force) |
| 335 | *d->singleCvsJob << "-F"; |
| 336 | |
| 337 | *d->singleCvsJob << KShell::quoteArg(tag) << CvsServiceUtils::joinFileList(files); |
| 338 | |
| 339 | return d->setupNonConcurrentJob(); |
| 340 | } |
| 341 | |
| 342 | QDBusObjectPath CvsService::deleteTag(const QStringList &files, const QString &tag, bool branch, bool force) |
| 343 | { |
| 344 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 345 | return {}; |
| 346 | |
| 347 | // assemble the command line |
| 348 | // cvs tag -d [-b] [-F] [TAG] [FILES] |
| 349 | d->singleCvsJob->clearCvsCommand(); |
| 350 | |
| 351 | *d->singleCvsJob << d->repository->cvsClient() << "tag" |
| 352 | << "-d"; |
| 353 | |
| 354 | if (branch) |
| 355 | *d->singleCvsJob << "-b"; |
| 356 | |
| 357 | if (force) |
| 358 | *d->singleCvsJob << "-F"; |
| 359 | |
| 360 | *d->singleCvsJob << KShell::quoteArg(tag) << CvsServiceUtils::joinFileList(files); |
| 361 | |
| 362 | return d->setupNonConcurrentJob(); |
| 363 | } |
| 364 | |
| 365 | QDBusObjectPath CvsService::downloadCvsIgnoreFile(const QString &repository, const QString &outputFile) |
| 366 | { |
| 367 | Repository repo(repository); |
| 368 | |
| 369 | // create a cvs job |
| 370 | CvsJob *job = d->createCvsJob(); |
| 371 | |
| 372 | // assemble the command line |
| 373 | // cvs -d [REPOSITORY] -q checkout -p CVSROOT/cvsignore > [OUTPUTFILE] |
| 374 | *job << repo.cvsClient() << "-d" << repository << "-q checkout -p CVSROOT/cvsignore >" << KShell::quoteArg(outputFile); |
| 375 | |
| 376 | // return a reference to the cvs job |
| 377 | return QDBusObjectPath(job->dbusObjectPath()); |
| 378 | } |
| 379 | |
| 380 | QDBusObjectPath CvsService::downloadRevision(const QString &fileName, const QString &revision, const QString &outputFile) |
| 381 | { |
| 382 | if (!d->hasWorkingCopy()) |
| 383 | return {}; |
| 384 | |
| 385 | // create a cvs job |
| 386 | CvsJob *job = d->createCvsJob(); |
| 387 | |
| 388 | // assemble the command line |
| 389 | // cvs update -p -r [REV] [FILE] > [OUTPUTFILE] |
| 390 | *job << d->repository->cvsClient() << "update -p"; |
| 391 | |
| 392 | if (!revision.isEmpty()) |
| 393 | *job << "-r" << KShell::quoteArg(revision); |
| 394 | |
| 395 | *job << KShell::quoteArg(fileName) << ">" << KShell::quoteArg(outputFile); |
| 396 | |
| 397 | // return a reference to the cvs job |
| 398 | return QDBusObjectPath(job->dbusObjectPath()); |
| 399 | } |
| 400 | |
| 401 | QDBusObjectPath |
| 402 | CvsService::downloadRevision(const QString &fileName, const QString &revA, const QString &outputFileA, const QString &revB, const QString &outputFileB) |
| 403 | { |
| 404 | if (!d->hasWorkingCopy()) |
| 405 | return {}; |
| 406 | |
| 407 | // create a cvs job |
| 408 | CvsJob *job = d->createCvsJob(); |
| 409 | |
| 410 | // assemble the command line |
| 411 | // cvs update -p -r [REVA] [FILE] > [OUTPUTFILEA] ; |
| 412 | // cvs update -p -r [REVB] [FILE] > [OUTPUTFILEB] |
| 413 | *job << d->repository->cvsClient() << "update -p" |
| 414 | << "-r" << KShell::quoteArg(revA) << KShell::quoteArg(fileName) << ">" << KShell::quoteArg(outputFileA) << ";" << d->repository->cvsClient() |
| 415 | << "update -p" |
| 416 | << "-r" << KShell::quoteArg(revB) << KShell::quoteArg(fileName) << ">" << KShell::quoteArg(outputFileB); |
| 417 | |
| 418 | // return a reference to the cvs job |
| 419 | return QDBusObjectPath(job->dbusObjectPath()); |
| 420 | } |
| 421 | |
| 422 | QDBusObjectPath CvsService::diff(const QString &fileName, const QString &revA, const QString &revB, const QString &diffOptions, unsigned contextLines) |
| 423 | { |
| 424 | // cvs diff [DIFFOPTIONS] -U CONTEXTLINES [-r REVA] {-r REVB] [FILE] |
| 425 | QString format = "-U" + QString::number(contextLines); |
| 426 | return diff(fileName, revA, revB, diffOptions, format); |
| 427 | } |
| 428 | |
| 429 | QDBusObjectPath CvsService::diff(const QString &fileName, const QString &revA, const QString &revB, const QString &diffOptions, const QString &format) |
| 430 | { |
| 431 | if (!d->hasWorkingCopy()) |
| 432 | return {}; |
| 433 | |
| 434 | // create a cvs job |
| 435 | CvsJob *job = d->createCvsJob(); |
| 436 | |
| 437 | // assemble the command line |
| 438 | // cvs diff [DIFFOPTIONS] [FORMAT] [-r REVA] {-r REVB] [FILE] |
| 439 | *job << d->repository->cvsClient() << "diff" << diffOptions << format; |
| 440 | |
| 441 | if (!revA.isEmpty()) |
| 442 | *job << "-r" << KShell::quoteArg(revA); |
| 443 | |
| 444 | if (!revB.isEmpty()) |
| 445 | *job << "-r" << KShell::quoteArg(revB); |
| 446 | |
| 447 | *job << KShell::quoteArg(fileName); |
| 448 | |
| 449 | // return a reference to the cvs job |
| 450 | return QDBusObjectPath(job->dbusObjectPath()); |
| 451 | } |
| 452 | |
| 453 | QDBusObjectPath CvsService::edit(const QStringList &files) |
| 454 | { |
| 455 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 456 | return {}; |
| 457 | |
| 458 | // assemble the command line |
| 459 | // cvs edit [FILES] |
| 460 | d->singleCvsJob->clearCvsCommand(); |
| 461 | |
| 462 | *d->singleCvsJob << d->repository->cvsClient() << "edit" << CvsServiceUtils::joinFileList(files); |
| 463 | |
| 464 | return d->setupNonConcurrentJob(); |
| 465 | } |
| 466 | |
| 467 | QDBusObjectPath CvsService::editors(const QStringList &files) |
| 468 | { |
| 469 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 470 | return {}; |
| 471 | |
| 472 | // assemble the command line |
| 473 | // cvs editors [FILES] |
| 474 | d->singleCvsJob->clearCvsCommand(); |
| 475 | |
| 476 | *d->singleCvsJob << d->repository->cvsClient() << "editors" << CvsServiceUtils::joinFileList(files); |
| 477 | |
| 478 | return d->setupNonConcurrentJob(); |
| 479 | } |
| 480 | |
| 481 | QDBusObjectPath CvsService::history() |
| 482 | { |
| 483 | if (!d->hasWorkingCopy()) |
| 484 | return {}; |
| 485 | |
| 486 | // create a cvs job |
| 487 | CvsJob *job = d->createCvsJob(); |
| 488 | |
| 489 | // assemble the command line |
| 490 | // cvs history -e -a |
| 491 | *job << d->repository->cvsClient() << "history -e -a"; |
| 492 | |
| 493 | // return a reference to the cvs job |
| 494 | return QDBusObjectPath(job->dbusObjectPath()); |
| 495 | } |
| 496 | |
| 497 | QDBusObjectPath CvsService::import(const QString &workingDir, |
| 498 | const QString &repository, |
| 499 | const QString &module, |
| 500 | const QString &ignoreList, |
| 501 | const QString &comment, |
| 502 | const QString &vendorTag, |
| 503 | const QString &releaseTag, |
| 504 | bool importAsBinary, |
| 505 | bool useModificationTime) |
| 506 | { |
| 507 | if (d->hasRunningJob()) |
| 508 | return {}; |
| 509 | |
| 510 | Repository repo(repository); |
| 511 | |
| 512 | // assemble the command line |
| 513 | d->singleCvsJob->clearCvsCommand(); |
| 514 | |
| 515 | *d->singleCvsJob << "cd" << KShell::quoteArg(workingDir) << "&&" << repo.cvsClient() << "-d" << repository << "import"; |
| 516 | |
| 517 | if (importAsBinary) |
| 518 | *d->singleCvsJob << "-kb"; |
| 519 | |
| 520 | if (useModificationTime) |
| 521 | *d->singleCvsJob << "-d"; |
| 522 | |
| 523 | const QString ignore = ignoreList.trimmed(); |
| 524 | if (!ignore.isEmpty()) |
| 525 | *d->singleCvsJob << "-I" << KShell::quoteArg(ignore); |
| 526 | |
| 527 | QString logMessage = comment.trimmed(); |
| 528 | logMessage.prepend("\""); |
| 529 | logMessage.append("\""); |
| 530 | *d->singleCvsJob << "-m" << logMessage; |
| 531 | |
| 532 | *d->singleCvsJob << module << vendorTag << releaseTag; |
| 533 | |
| 534 | return d->setupNonConcurrentJob(&repo); |
| 535 | } |
| 536 | |
| 537 | QDBusObjectPath CvsService::lock(const QStringList &files) |
| 538 | { |
| 539 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 540 | return {}; |
| 541 | |
| 542 | // assemble the command line |
| 543 | // cvs admin -l [FILES] |
| 544 | d->singleCvsJob->clearCvsCommand(); |
| 545 | |
| 546 | *d->singleCvsJob << d->repository->cvsClient() << "admin -l" << CvsServiceUtils::joinFileList(files); |
| 547 | |
| 548 | return d->setupNonConcurrentJob(); |
| 549 | } |
| 550 | |
| 551 | QDBusObjectPath CvsService::log(const QString &fileName) |
| 552 | { |
| 553 | if (!d->hasWorkingCopy()) |
| 554 | return {}; |
| 555 | |
| 556 | // create a cvs job |
| 557 | CvsJob *job = d->createCvsJob(); |
| 558 | |
| 559 | // assemble the command line |
| 560 | // cvs log [FILE] |
| 561 | *job << d->repository->cvsClient() << "log" << KShell::quoteArg(fileName); |
| 562 | |
| 563 | // return a reference to the cvs job |
| 564 | return QDBusObjectPath(job->dbusObjectPath()); |
| 565 | } |
| 566 | |
| 567 | QDBusObjectPath CvsService::login(const QString &repository) |
| 568 | { |
| 569 | if (repository.isEmpty()) |
| 570 | return {}; |
| 571 | |
| 572 | Repository repo(repository); |
| 573 | |
| 574 | // create a cvs job |
| 575 | ++(d->lastJobId); |
| 576 | |
| 577 | auto job = new CvsLoginJob(d->lastJobId); |
| 578 | d->loginJobs.insert(d->lastJobId, job); |
| 579 | |
| 580 | // TODO: CVS_SERVER doesn't work ATM |
| 581 | // job->setServer(repo.server()); |
| 582 | |
| 583 | // assemble the command line |
| 584 | // cvs -d [REPOSITORY] login |
| 585 | job->setCvsClient(repo.clientOnly().toLocal8Bit()); |
| 586 | job->setRepository(repository.toLocal8Bit()); |
| 587 | |
| 588 | // return a reference to the cvs job |
| 589 | return QDBusObjectPath(job->dbusObjectPath()); |
| 590 | } |
| 591 | |
| 592 | QDBusObjectPath CvsService::logout(const QString &repository) |
| 593 | { |
| 594 | if (repository.isEmpty()) |
| 595 | return {}; |
| 596 | |
| 597 | Repository repo(repository); |
| 598 | |
| 599 | // create a cvs job |
| 600 | ++(d->lastJobId); |
| 601 | |
| 602 | auto job = new CvsJob(d->lastJobId); |
| 603 | d->cvsJobs.insert(d->lastJobId, job); |
| 604 | |
| 605 | job->setRSH(repo.rsh()); |
| 606 | job->setServer(repo.server()); |
| 607 | job->setDirectory(repo.workingCopy()); |
| 608 | |
| 609 | // assemble the command line |
| 610 | // cvs -d [REPOSITORY] logout |
| 611 | *job << repo.cvsClient() << "-d" << repository << "logout"; |
| 612 | |
| 613 | // return a reference to the cvs job |
| 614 | return QDBusObjectPath(job->dbusObjectPath()); |
| 615 | } |
| 616 | |
| 617 | QDBusObjectPath CvsService::makePatch() |
| 618 | { |
| 619 | return makePatch("", "-u"); |
| 620 | } |
| 621 | |
| 622 | QDBusObjectPath CvsService::makePatch(const QString &diffOptions, const QString &format) |
| 623 | { |
| 624 | if (!d->hasWorkingCopy()) |
| 625 | return {}; |
| 626 | |
| 627 | // create a cvs job |
| 628 | CvsJob *job = d->createCvsJob(); |
| 629 | |
| 630 | // assemble the command line |
| 631 | // cvs diff [DIFFOPTIONS] [FORMAT] -R 2>/dev/null |
| 632 | *job << d->repository->cvsClient() << "diff" << diffOptions << format << "-R" |
| 633 | << "2>/dev/null"; |
| 634 | |
| 635 | // return a reference to the cvs job |
| 636 | return QDBusObjectPath(job->dbusObjectPath()); |
| 637 | } |
| 638 | |
| 639 | QDBusObjectPath CvsService::moduleList(const QString &repository) |
| 640 | { |
| 641 | Repository repo(repository); |
| 642 | |
| 643 | // create a cvs job |
| 644 | ++(d->lastJobId); |
| 645 | |
| 646 | auto job = new CvsJob(d->lastJobId); |
| 647 | d->cvsJobs.insert(d->lastJobId, job); |
| 648 | |
| 649 | job->setRSH(repo.rsh()); |
| 650 | job->setServer(repo.server()); |
| 651 | job->setDirectory(repo.workingCopy()); |
| 652 | |
| 653 | // assemble the command line |
| 654 | // cvs -d [REPOSITORY] checkout -c |
| 655 | *job << repo.cvsClient() << "-d" << repository << "checkout -c"; |
| 656 | |
| 657 | // return a reference to the cvs job |
| 658 | return QDBusObjectPath(job->dbusObjectPath()); |
| 659 | } |
| 660 | |
| 661 | QDBusObjectPath CvsService::remove(const QStringList &files, bool recursive) |
| 662 | { |
| 663 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 664 | return {}; |
| 665 | |
| 666 | // assemble the command line |
| 667 | // cvs remove -f [-l] [FILES] |
| 668 | d->singleCvsJob->clearCvsCommand(); |
| 669 | |
| 670 | *d->singleCvsJob << d->repository->cvsClient() << "remove -f"; |
| 671 | |
| 672 | if (!recursive) |
| 673 | *d->singleCvsJob << "-l"; |
| 674 | |
| 675 | *d->singleCvsJob << CvsServiceUtils::joinFileList(files) << REDIRECT_STDERR; |
| 676 | |
| 677 | return d->setupNonConcurrentJob(); |
| 678 | } |
| 679 | |
| 680 | QDBusObjectPath CvsService::removeWatch(const QStringList &files, int events) |
| 681 | { |
| 682 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 683 | return {}; |
| 684 | |
| 685 | // assemble the command line |
| 686 | d->singleCvsJob->clearCvsCommand(); |
| 687 | |
| 688 | *d->singleCvsJob << d->repository->cvsClient() << "watch remove"; |
| 689 | |
| 690 | if (events != All) { |
| 691 | if (events & Commits) |
| 692 | *d->singleCvsJob << "-a commit"; |
| 693 | if (events & Edits) |
| 694 | *d->singleCvsJob << "-a edit"; |
| 695 | if (events & Unedits) |
| 696 | *d->singleCvsJob << "-a unedit"; |
| 697 | } |
| 698 | |
| 699 | *d->singleCvsJob << CvsServiceUtils::joinFileList(files); |
| 700 | |
| 701 | return d->setupNonConcurrentJob(); |
| 702 | } |
| 703 | |
| 704 | QDBusObjectPath CvsService::rlog(const QString &repository, const QString &module, bool recursive) |
| 705 | { |
| 706 | Repository repo(repository); |
| 707 | |
| 708 | // create a cvs job |
| 709 | ++(d->lastJobId); |
| 710 | |
| 711 | auto job = new CvsJob(d->lastJobId); |
| 712 | d->cvsJobs.insert(d->lastJobId, job); |
| 713 | |
| 714 | job->setRSH(repo.rsh()); |
| 715 | job->setServer(repo.server()); |
| 716 | |
| 717 | // assemble the command line |
| 718 | // cvs -d [REPOSITORY] rlog [-l] [MODULE] |
| 719 | *job << repo.cvsClient() << "-d" << repository << "rlog"; |
| 720 | |
| 721 | if (!recursive) |
| 722 | *job << "-l"; |
| 723 | |
| 724 | *job << module; |
| 725 | |
| 726 | // return a reference to the cvs job |
| 727 | return QDBusObjectPath(job->dbusObjectPath()); |
| 728 | } |
| 729 | |
| 730 | QDBusObjectPath CvsService::simulateUpdate(const QStringList &files, bool recursive, bool createDirs, bool pruneDirs) |
| 731 | { |
| 732 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 733 | return {}; |
| 734 | |
| 735 | // assemble the command line |
| 736 | // cvs -n update [-l] [-d] [-P] [FILES] |
| 737 | d->singleCvsJob->clearCvsCommand(); |
| 738 | |
| 739 | *d->singleCvsJob << d->repository->cvsClient() << "-n -q update"; |
| 740 | |
| 741 | if (!recursive) |
| 742 | *d->singleCvsJob << "-l"; |
| 743 | |
| 744 | if (createDirs) |
| 745 | *d->singleCvsJob << "-d"; |
| 746 | |
| 747 | if (pruneDirs) |
| 748 | *d->singleCvsJob << "-P"; |
| 749 | |
| 750 | *d->singleCvsJob << CvsServiceUtils::joinFileList(files) << REDIRECT_STDERR; |
| 751 | |
| 752 | return d->setupNonConcurrentJob(); |
| 753 | } |
| 754 | |
| 755 | QDBusObjectPath CvsService::status(const QStringList &files, bool recursive, bool tagInfo) |
| 756 | { |
| 757 | if (!d->hasWorkingCopy()) |
| 758 | return {}; |
| 759 | |
| 760 | // create a cvs job |
| 761 | CvsJob *job = d->createCvsJob(); |
| 762 | |
| 763 | // assemble the command line |
| 764 | // cvs status [-l] [-v] [FILES] |
| 765 | *job << d->repository->cvsClient() << "status"; |
| 766 | |
| 767 | if (!recursive) |
| 768 | *job << "-l"; |
| 769 | |
| 770 | if (tagInfo) |
| 771 | *job << "-v"; |
| 772 | |
| 773 | *job << CvsServiceUtils::joinFileList(files); |
| 774 | |
| 775 | // return a reference to the cvs job |
| 776 | return QDBusObjectPath(job->dbusObjectPath()); |
| 777 | } |
| 778 | |
| 779 | QDBusObjectPath CvsService::unedit(const QStringList &files) |
| 780 | { |
| 781 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 782 | return {}; |
| 783 | |
| 784 | // assemble the command line |
| 785 | // echo y | cvs unedit [FILES] |
| 786 | d->singleCvsJob->clearCvsCommand(); |
| 787 | |
| 788 | *d->singleCvsJob << "echo y |" << d->repository->cvsClient() << "unedit" << CvsServiceUtils::joinFileList(files); |
| 789 | |
| 790 | return d->setupNonConcurrentJob(); |
| 791 | } |
| 792 | |
| 793 | QDBusObjectPath CvsService::unlock(const QStringList &files) |
| 794 | { |
| 795 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 796 | return {}; |
| 797 | |
| 798 | // assemble the command line |
| 799 | // cvs admin -u [FILES] |
| 800 | d->singleCvsJob->clearCvsCommand(); |
| 801 | |
| 802 | *d->singleCvsJob << d->repository->cvsClient() << "admin -u" << CvsServiceUtils::joinFileList(files); |
| 803 | |
| 804 | return d->setupNonConcurrentJob(); |
| 805 | } |
| 806 | |
| 807 | QDBusObjectPath CvsService::update(const QStringList &files, bool recursive, bool createDirs, bool pruneDirs, const QString &extraOpt) |
| 808 | { |
| 809 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 810 | return {}; |
| 811 | |
| 812 | // assemble the command line |
| 813 | // cvs update [-l] [-d] [-P] [EXTRAOPTIONS] [FILES] |
| 814 | d->singleCvsJob->clearCvsCommand(); |
| 815 | |
| 816 | *d->singleCvsJob << d->repository->cvsClient() << "-q update"; |
| 817 | |
| 818 | if (!recursive) |
| 819 | *d->singleCvsJob << "-l"; |
| 820 | |
| 821 | if (createDirs) |
| 822 | *d->singleCvsJob << "-d"; |
| 823 | |
| 824 | if (pruneDirs) |
| 825 | *d->singleCvsJob << "-P"; |
| 826 | |
| 827 | *d->singleCvsJob << extraOpt << CvsServiceUtils::joinFileList(files) << REDIRECT_STDERR; |
| 828 | |
| 829 | return d->setupNonConcurrentJob(); |
| 830 | } |
| 831 | |
| 832 | QDBusObjectPath CvsService::watchers(const QStringList &files) |
| 833 | { |
| 834 | if (!d->hasWorkingCopy() || d->hasRunningJob()) |
| 835 | return {}; |
| 836 | |
| 837 | // assemble the command line |
| 838 | // cvs watchers [FILES] |
| 839 | d->singleCvsJob->clearCvsCommand(); |
| 840 | |
| 841 | *d->singleCvsJob << d->repository->cvsClient() << "watchers" << CvsServiceUtils::joinFileList(files); |
| 842 | |
| 843 | return d->setupNonConcurrentJob(); |
| 844 | } |
| 845 | |
| 846 | void CvsService::quit() |
| 847 | { |
| 848 | qApp->quit(); |
| 849 | } |
| 850 | |
| 851 | CvsJob *CvsService::Private::createCvsJob() |
| 852 | { |
| 853 | ++lastJobId; |
| 854 | |
| 855 | // create a cvs job |
| 856 | auto job = new CvsJob(lastJobId); |
| 857 | cvsJobs.insert(lastJobId, job); |
| 858 | |
| 859 | job->setRSH(repository->rsh()); |
| 860 | job->setServer(repository->server()); |
| 861 | job->setDirectory(repository->workingCopy()); |
| 862 | |
| 863 | return job; |
| 864 | } |
| 865 | |
| 866 | QDBusObjectPath CvsService::Private::setupNonConcurrentJob(Repository *repo) |
| 867 | { |
| 868 | // no explicit repository provided? |
| 869 | if (!repo) |
| 870 | repo = repository; |
| 871 | |
| 872 | singleCvsJob->setRSH(repo->rsh()); |
| 873 | singleCvsJob->setServer(repo->server()); |
| 874 | singleCvsJob->setDirectory(repo->workingCopy()); |
| 875 | |
| 876 | return QDBusObjectPath(singleCvsJob->dbusObjectPath()); |
| 877 | } |
| 878 | |
| 879 | bool CvsService::Private::hasWorkingCopy() |
| 880 | { |
| 881 | if (repository->workingCopy().isEmpty()) { |
| 882 | KMessageBox::error(0, |
| 883 | i18n("You have to set a local working copy " |
| 884 | "directory before you can use this function!")); |
| 885 | return false; |
| 886 | } |
| 887 | |
| 888 | return true; |
| 889 | } |
| 890 | |
| 891 | bool CvsService::Private::hasRunningJob() |
| 892 | { |
| 893 | bool result = singleCvsJob->isRunning(); |
| 894 | |
| 895 | if (result) |
| 896 | KMessageBox::error(0, i18n("There is already a job running")); |
| 897 | |
| 898 | return result; |
| 899 | } |