Changeset View
Changeset View
Standalone View
Standalone View
svn/svncommands.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019-2020 * | ||||
3 | * by Nikolai Krasheninnikov <nkrasheninnikov@yandex.ru> * | ||||
4 | * * | ||||
5 | * This program is free software; you can redistribute it and/or modify * | ||||
6 | * it under the terms of the GNU General Public License as published by * | ||||
7 | * the Free Software Foundation; either version 2 of the License, or * | ||||
8 | * (at your option) any later version. * | ||||
9 | * * | ||||
10 | * This program is distributed in the hope that it will be useful, * | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
13 | * GNU General Public License for more details. * | ||||
14 | * * | ||||
15 | * You should have received a copy of the GNU General Public License * | ||||
16 | * along with this program; if not, write to the * | ||||
17 | * Free Software Foundation, Inc., * | ||||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
19 | ***************************************************************************/ | ||||
20 | | ||||
21 | #include "svncommands.h" | ||||
22 | | ||||
23 | #include <QProcess> | ||||
24 | #include <QTextStream> | ||||
25 | #include <QTemporaryFile> | ||||
26 | #include <QDir> | ||||
27 | | ||||
28 | namespace { | ||||
29 | | ||||
30 | // Helper function: returns template file name for QTemporaryFile. | ||||
31 | QString templateFileName(const QString& url, ulong rev) | ||||
32 | { | ||||
33 | const QString tmpFileName = url.section('/', -1); | ||||
34 | | ||||
35 | return QDir::tempPath() + QString("/%1.r%2.XXXXXX").arg(tmpFileName).arg(rev); | ||||
36 | } | ||||
37 | | ||||
38 | } | ||||
39 | | ||||
40 | ulong SVNCommands::localRevision(const QString& filePath) | ||||
41 | { | ||||
42 | QProcess process; | ||||
43 | | ||||
44 | process.start( | ||||
45 | QLatin1String("svn"), | ||||
46 | QStringList { | ||||
47 | QStringLiteral("info"), | ||||
48 | QStringLiteral("--show-item"), | ||||
49 | QStringLiteral("last-changed-revision"), | ||||
50 | filePath | ||||
51 | } | ||||
52 | ); | ||||
53 | | ||||
54 | if (!process.waitForFinished() || process.exitCode() != 0) { | ||||
55 | return 0; | ||||
56 | } | ||||
57 | | ||||
58 | QTextStream stream(&process); | ||||
59 | ulong revision = 0; | ||||
60 | stream >> revision; | ||||
61 | | ||||
62 | if (stream.status() == QTextStream::Ok) { | ||||
63 | return revision; | ||||
64 | } else { | ||||
65 | return 0; | ||||
66 | } | ||||
67 | } | ||||
68 | | ||||
69 | QString SVNCommands::remoteItemUrl(const QString& filePath) | ||||
70 | { | ||||
71 | QProcess process; | ||||
72 | | ||||
73 | process.start( | ||||
74 | QLatin1String("svn"), | ||||
75 | QStringList { | ||||
76 | QStringLiteral("info"), | ||||
77 | QStringLiteral("--show-item"), | ||||
78 | QStringLiteral("url"), | ||||
79 | filePath | ||||
80 | } | ||||
81 | ); | ||||
82 | | ||||
83 | if (!process.waitForFinished() || process.exitCode() != 0) { | ||||
84 | return 0; | ||||
85 | } | ||||
86 | | ||||
87 | QTextStream stream(&process); | ||||
88 | QString url; | ||||
89 | stream >> url; | ||||
90 | | ||||
91 | if (stream.status() == QTextStream::Ok) { | ||||
92 | return url; | ||||
93 | } else { | ||||
94 | return QString(); | ||||
95 | } | ||||
96 | } | ||||
97 | | ||||
98 | bool SVNCommands::exportRemoteFile(const QString& remoteUrl, ulong rev, QFileDevice *file) | ||||
99 | { | ||||
100 | if (file == nullptr) { | ||||
101 | return false; | ||||
102 | } | ||||
103 | if (!file->isOpen() && !file->open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text)) { | ||||
104 | return false; | ||||
105 | } | ||||
106 | | ||||
107 | QProcess process; | ||||
108 | | ||||
109 | process.start( | ||||
110 | QLatin1String("svn"), | ||||
111 | QStringList { | ||||
112 | QStringLiteral("export"), | ||||
113 | QStringLiteral("--force"), | ||||
114 | QStringLiteral("-r%1").arg(rev), | ||||
115 | remoteUrl, | ||||
116 | file->fileName() | ||||
117 | } | ||||
118 | ); | ||||
119 | | ||||
120 | if (!process.waitForFinished() || process.exitCode() != 0) { | ||||
121 | return false; | ||||
122 | } else { | ||||
123 | return true; | ||||
124 | } | ||||
125 | } | ||||
126 | | ||||
127 | bool SVNCommands::exportRemoteFile(const QString& remoteUrl, ulong rev, QTemporaryFile *file) | ||||
128 | { | ||||
129 | if (file == nullptr) { | ||||
130 | return false; | ||||
131 | } | ||||
132 | | ||||
133 | file->setFileTemplate( templateFileName(remoteUrl, rev) ); | ||||
134 | | ||||
135 | return exportRemoteFile(remoteUrl, rev, dynamic_cast<QFileDevice*>(file)); | ||||
136 | } | ||||
137 | | ||||
138 | bool SVNCommands::exportLocalFile(const QString& filePath, ulong rev, QFileDevice *file) | ||||
139 | { | ||||
140 | if (file == nullptr) { | ||||
141 | return false; | ||||
142 | } | ||||
143 | | ||||
144 | const QString fileUrl = remoteItemUrl(filePath); | ||||
145 | if (fileUrl.isEmpty()) { | ||||
146 | return false; | ||||
147 | } | ||||
148 | | ||||
149 | if (!exportRemoteFile(fileUrl, rev, file)) { | ||||
150 | return false; | ||||
151 | } else { | ||||
152 | return true; | ||||
153 | } | ||||
154 | } | ||||
155 | | ||||
156 | bool SVNCommands::exportLocalFile(const QString& filePath, ulong rev, QTemporaryFile *file) | ||||
157 | { | ||||
158 | if (file == nullptr) { | ||||
159 | return false; | ||||
160 | } | ||||
161 | | ||||
162 | file->setFileTemplate( templateFileName(filePath, rev) ); | ||||
163 | | ||||
164 | return exportLocalFile(filePath, rev, dynamic_cast<QFileDevice*>(file)); | ||||
165 | } |