Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@57
|
2
|
Chris@57
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@57
|
8 Copyright (c) 2010 Chris Cannam
|
Chris@57
|
9 Copyright (c) 2010 Queen Mary, University of London
|
Chris@57
|
10
|
Chris@57
|
11 This program is free software; you can redistribute it and/or
|
Chris@57
|
12 modify it under the terms of the GNU General Public License as
|
Chris@57
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@57
|
14 License, or (at your option) any later version. See the file
|
Chris@57
|
15 COPYING included with this distribution for more information.
|
Chris@57
|
16 */
|
jtkorhonen@0
|
17
|
jtkorhonen@0
|
18 #include "hgrunner.h"
|
Chris@62
|
19 #include "common.h"
|
Chris@57
|
20 #include "debug.h"
|
Chris@50
|
21
|
Chris@50
|
22 #include <QPushButton>
|
Chris@50
|
23 #include <QListWidget>
|
Chris@50
|
24 #include <QDialog>
|
Chris@50
|
25 #include <QLabel>
|
Chris@50
|
26 #include <QVBoxLayout>
|
Chris@62
|
27 #include <QSettings>
|
Chris@75
|
28 #include <QInputDialog>
|
jtkorhonen@0
|
29
|
Chris@43
|
30 #include <iostream>
|
jtkorhonen@0
|
31 #include <unistd.h>
|
Chris@75
|
32 #include <errno.h>
|
Chris@75
|
33 #include <stdio.h>
|
jtkorhonen@0
|
34
|
Chris@76
|
35 #ifndef Q_OS_WIN32
|
Chris@76
|
36 #include <pty.h>
|
Chris@76
|
37 #endif
|
Chris@76
|
38
|
jtkorhonen@0
|
39 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
|
jtkorhonen@0
|
40 {
|
jtkorhonen@0
|
41 proc = new QProcess(this);
|
jtkorhonen@0
|
42
|
cannam@45
|
43 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
cannam@45
|
44 env.insert("LANG", "en_US.utf8");
|
cannam@45
|
45 env.insert("LC_ALL", "en_US.utf8");
|
cannam@45
|
46 proc->setProcessEnvironment(env);
|
cannam@45
|
47
|
jtkorhonen@0
|
48 setTextVisible(false);
|
jtkorhonen@0
|
49 setVisible(false);
|
jtkorhonen@0
|
50 isRunning = false;
|
jtkorhonen@0
|
51
|
jtkorhonen@0
|
52 stdOut.clear();
|
jtkorhonen@0
|
53 stdErr.clear();
|
jtkorhonen@0
|
54
|
Chris@75
|
55 procInput = 0;
|
Chris@76
|
56 #ifndef Q_OS_WIN32
|
Chris@75
|
57 char name[1024];
|
Chris@75
|
58 if (openpty(&ptyMasterFd, &ptySlaveFd, name, NULL, NULL)) {
|
Chris@75
|
59 perror("openpty failed");
|
Chris@75
|
60 } else {
|
Chris@75
|
61 DEBUG << "openpty succeeded: master " << ptyMasterFd
|
Chris@75
|
62 << " slave " << ptySlaveFd << " filename " << name << endl;
|
Chris@75
|
63 procInput = new QFile;
|
Chris@75
|
64 procInput->open(ptyMasterFd, QFile::WriteOnly);
|
Chris@75
|
65 ptySlaveFilename = name;
|
Chris@75
|
66 proc->setStandardInputFile(ptySlaveFilename);
|
Chris@75
|
67 ::close(ptySlaveFd);
|
Chris@75
|
68 }
|
Chris@76
|
69 #endif
|
jtkorhonen@0
|
70 connect(proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@62
|
71 connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@62
|
72 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@75
|
73 connect(proc, SIGNAL(readyReadStandardOutput()),
|
Chris@75
|
74 this, SLOT(stdOutReady()));
|
Chris@75
|
75 connect(proc, SIGNAL(readyReadStandardError()),
|
Chris@75
|
76 this, SLOT(stdErrReady()));
|
Chris@62
|
77
|
Chris@62
|
78 reportErrors = false;
|
jtkorhonen@0
|
79 }
|
jtkorhonen@0
|
80
|
jtkorhonen@0
|
81 HgRunner::~HgRunner()
|
jtkorhonen@0
|
82 {
|
Chris@75
|
83 if (ptySlaveFilename != "") {
|
Chris@75
|
84 ::close(ptyMasterFd);
|
Chris@75
|
85 // ::close(ptySlaveFd);
|
Chris@75
|
86 }
|
jtkorhonen@0
|
87 delete proc;
|
jtkorhonen@0
|
88 }
|
jtkorhonen@0
|
89
|
Chris@62
|
90 QString HgRunner::getHgBinaryName()
|
Chris@62
|
91 {
|
Chris@62
|
92 QSettings settings;
|
Chris@77
|
93 QString hg = settings.value("hgbinary", "").toString();
|
Chris@77
|
94 if (hg == "") {
|
Chris@77
|
95 hg = findExecutable("hg");
|
Chris@77
|
96 }
|
Chris@77
|
97 if (hg != "hg") {
|
Chris@77
|
98 settings.setValue("hgbinary", hg);
|
Chris@77
|
99 }
|
Chris@62
|
100 return hg;
|
Chris@62
|
101 }
|
Chris@62
|
102
|
jtkorhonen@0
|
103 void HgRunner::started()
|
jtkorhonen@0
|
104 {
|
Chris@75
|
105 /*
|
Chris@75
|
106 if (procInput) procInput->write("blah\n");
|
Chris@75
|
107 if (procInput) procInput->write("blah\n");
|
Chris@75
|
108 if (procInput) {
|
Chris@75
|
109 procInput->close();
|
Chris@75
|
110 // ::close(ptyMasterFd);
|
Chris@75
|
111 }
|
jtkorhonen@0
|
112 proc -> closeWriteChannel();
|
Chris@75
|
113 */
|
jtkorhonen@0
|
114 }
|
jtkorhonen@0
|
115
|
Chris@62
|
116 void HgRunner::saveOutput()
|
Chris@62
|
117 {
|
Chris@75
|
118 stdOut += QString::fromUtf8(proc -> readAllStandardOutput());
|
Chris@75
|
119 stdErr += QString::fromUtf8(proc -> readAllStandardError());
|
Chris@64
|
120
|
Chris@64
|
121 DEBUG << "saveOutput: " << stdOut.split("\n").size() << " line(s) of stdout, " << stdErr.split("\n").size() << " line(s) of stderr" << endl;
|
Chris@64
|
122
|
Chris@64
|
123 // std::cerr << "stdout was " << stdOut.toStdString() << std::endl;
|
Chris@62
|
124 }
|
Chris@62
|
125
|
jtkorhonen@0
|
126 void HgRunner::setProcExitInfo(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
127 {
|
jtkorhonen@0
|
128 exitCode = procExitCode;
|
jtkorhonen@0
|
129 exitStatus = procExitStatus;
|
jtkorhonen@0
|
130 }
|
jtkorhonen@0
|
131
|
jtkorhonen@0
|
132 QString HgRunner::getLastCommandLine()
|
jtkorhonen@0
|
133 {
|
jtkorhonen@0
|
134 return QString("Command line: " + lastHgCommand + " " + lastParams);
|
jtkorhonen@0
|
135 }
|
jtkorhonen@0
|
136
|
Chris@75
|
137 void HgRunner::stdOutReady()
|
Chris@75
|
138 {
|
Chris@75
|
139 DEBUG << "stdOutReady" << endl;
|
Chris@75
|
140 QString chunk = QString::fromUtf8(proc->readAllStandardOutput());
|
Chris@75
|
141 DEBUG << "stdout was " << chunk << endl;
|
Chris@75
|
142 stdOut += chunk;
|
Chris@75
|
143 }
|
Chris@75
|
144
|
Chris@75
|
145 void HgRunner::stdErrReady()
|
Chris@75
|
146 {
|
Chris@75
|
147 DEBUG << "stdErrReady" << endl;
|
Chris@75
|
148 QString chunk = QString::fromUtf8(proc->readAllStandardError());
|
Chris@75
|
149 DEBUG << "stderr was " << chunk << endl;
|
Chris@75
|
150 stdErr += chunk;
|
Chris@75
|
151 if (procInput) {
|
Chris@75
|
152 if (chunk.toLower().trimmed() == "password:") {
|
Chris@75
|
153 bool ok = false;
|
Chris@75
|
154 QString pwd = QInputDialog::getText
|
Chris@75
|
155 (qobject_cast<QWidget *>(parent()),
|
Chris@75
|
156 tr("Enter password"), tr("Password (but for what user name and repository??"),
|
Chris@75
|
157 QLineEdit::Password, QString(), &ok);
|
Chris@75
|
158 if (ok) {
|
Chris@75
|
159 procInput->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@75
|
160 procInput->flush();
|
Chris@75
|
161 } else {
|
Chris@75
|
162 //!!! do what? close the terminal?
|
Chris@75
|
163 }
|
Chris@75
|
164 }
|
Chris@75
|
165 }
|
Chris@75
|
166 }
|
Chris@75
|
167
|
jtkorhonen@0
|
168 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
169 {
|
jtkorhonen@0
|
170 setProcExitInfo(procExitCode, procExitStatus);
|
Chris@64
|
171 saveOutput();
|
Chris@62
|
172 isRunning = false;
|
jtkorhonen@0
|
173
|
Chris@74
|
174 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
|
Chris@74
|
175 DEBUG << "HgRunner::finished: Command completed successfully: stderr says: " << stdErr << endl;
|
Chris@62
|
176 emit commandCompleted();
|
Chris@62
|
177 } else {
|
Chris@74
|
178 DEBUG << "HgRunner::finished: Command failed: stderr says: " << stdErr << endl;
|
Chris@62
|
179 emit commandFailed();
|
jtkorhonen@0
|
180 }
|
jtkorhonen@0
|
181 }
|
jtkorhonen@0
|
182
|
Chris@62
|
183 bool HgRunner::isCommandRunning()
|
jtkorhonen@0
|
184 {
|
jtkorhonen@0
|
185 return isRunning;
|
jtkorhonen@0
|
186 }
|
jtkorhonen@0
|
187
|
Chris@62
|
188 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
189 {
|
Chris@62
|
190 if (isCommandRunning()) {
|
jtkorhonen@0
|
191 proc -> kill();
|
jtkorhonen@0
|
192 }
|
jtkorhonen@0
|
193 }
|
jtkorhonen@0
|
194
|
Chris@62
|
195 void HgRunner::startHgCommand(QString workingDir, QStringList params)
|
Chris@62
|
196 {
|
Chris@62
|
197 startCommand(getHgBinaryName(), workingDir, params);
|
Chris@62
|
198 }
|
jtkorhonen@0
|
199
|
Chris@62
|
200 void HgRunner::startCommand(QString command, QString workingDir, QStringList params)
|
jtkorhonen@0
|
201 {
|
jtkorhonen@0
|
202 isRunning = true;
|
jtkorhonen@0
|
203 setRange(0, 0);
|
jtkorhonen@0
|
204 setVisible(true);
|
jtkorhonen@0
|
205 stdOut.clear();
|
jtkorhonen@0
|
206 stdErr.clear();
|
jtkorhonen@0
|
207 exitCode = 0;
|
jtkorhonen@0
|
208 exitStatus = QProcess::NormalExit;
|
jtkorhonen@0
|
209
|
jtkorhonen@0
|
210 if (!workingDir.isEmpty())
|
jtkorhonen@0
|
211 {
|
jtkorhonen@0
|
212 proc -> setWorkingDirectory(workingDir);
|
jtkorhonen@0
|
213 }
|
jtkorhonen@0
|
214
|
Chris@62
|
215 lastHgCommand = command;
|
jtkorhonen@0
|
216 lastParams = params.join(" ");
|
jtkorhonen@0
|
217
|
Chris@62
|
218 QString cmdline = command;
|
Chris@57
|
219 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
220 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@64
|
221 << workingDir << endl;
|
Chris@43
|
222
|
Chris@62
|
223 proc -> start(command, params);
|
jtkorhonen@0
|
224 }
|
jtkorhonen@0
|
225
|
jtkorhonen@0
|
226 int HgRunner::getExitCode()
|
jtkorhonen@0
|
227 {
|
jtkorhonen@0
|
228 return exitCode;
|
jtkorhonen@0
|
229 }
|
jtkorhonen@0
|
230
|
jtkorhonen@0
|
231 QString HgRunner::getStdOut()
|
jtkorhonen@0
|
232 {
|
jtkorhonen@0
|
233 return stdOut;
|
jtkorhonen@0
|
234 }
|
jtkorhonen@0
|
235
|
jtkorhonen@0
|
236 void HgRunner::hideProgBar()
|
jtkorhonen@0
|
237 {
|
jtkorhonen@0
|
238 setVisible(false);
|
jtkorhonen@0
|
239 }
|
jtkorhonen@0
|
240
|
jtkorhonen@0
|
241
|