Skip to content

Commit 430899b

Browse files
committed
Git: git diff of files, was sometimes failing (161)
The core of the problem is that the base of the git repo, was assumed to be the root of the current project. This assumption failed in so many scenarios. Instead, we use `git -C $dir` with the directory of current file. This also lead to a "minor" refactor, and all git commands now use that git switch. One change is that now we need to maintain the repo dir used for the git logs command. closes #161
1 parent f162ae8 commit 430899b

3 files changed

Lines changed: 43 additions & 56 deletions

File tree

src/plugins/git/CreateGitBranch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void CreateGitBranch::verifyBranchName(const QString &newText) {
6060
}
6161

6262
void CreateGitBranch::findLocalBranches() {
63-
auto res = plugin->runGit({"branch"}, false);
63+
auto res = plugin->runGit({"branch"});
6464
if (res.isEmpty()) {
6565
return;
6666
}
@@ -100,5 +100,5 @@ QString CreateGitBranch::createBranchImplementation(const QString &branchName, b
100100
} else {
101101
args = {"branch", branchName};
102102
}
103-
return plugin->runGit(args, false);
103+
return plugin->runGit(args);
104104
}

src/plugins/git/GitPlugin.cpp

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void GitPlugin::on_client_merged(qmdiHost *host) {
111111
stash = new QAction(tr("git stash"), this);
112112
branches = new QAction(tr("git branch"), this);
113113

114-
diffFile->setToolTip(tr("GIT: Show changes (current file)"));
114+
diffFile->setToolTip(tr("git: Show changes (current file)"));
115115
diffFile->setShortcut(QKeySequence("Ctrl+G, D"));
116116
logFile->setToolTip(tr("Show commits (current file)"));
117117
logFile->setShortcut(QKeySequence("Ctrl+G, F"));
@@ -192,9 +192,7 @@ void GitPlugin::diffFileHandler() {
192192
auto manager = getManager();
193193
auto client = manager->getMdiServer()->getCurrentClient();
194194
auto filename = client->mdiClientFileName();
195-
if (repoRoot.isEmpty()) {
196-
repoRoot = detectRepoRoot(filename);
197-
}
195+
auto repoRoot = QFileInfo(filename).absolutePath();
198196
auto const diff = getDiff(filename);
199197
if (diff.isEmpty()) {
200198
return;
@@ -213,9 +211,6 @@ void GitPlugin::revertFileHandler() {
213211
auto manager = getManager();
214212
auto client = manager->getMdiServer()->getCurrentClient();
215213
auto filename = client->mdiClientFileName();
216-
if (repoRoot.isEmpty()) {
217-
repoRoot = detectRepoRoot(filename);
218-
}
219214
auto const diff = getDiff(filename);
220215
if (diff.isEmpty()) {
221216
return;
@@ -232,25 +227,26 @@ void GitPlugin::revertFileHandler() {
232227
return;
233228
}
234229
auto args = QStringList{"restore", client->mdiClientFileName()};
235-
auto output = runGit(args, false);
230+
auto output = runGit(args);
236231
if (auto editor = dynamic_cast<qmdiEditor *>(client)) {
237232
editor->loadFile(filename);
238233
editor->loadContent(false);
239234
}
240235
}
241236

242237
void GitPlugin::refreshBranchesHandler() {
243-
if (repoRoot.isEmpty()) {
244-
return;
245-
}
246-
auto output = runGit({"branch", "-a"}, false);
238+
auto manager = getManager();
239+
auto client = manager->getMdiServer()->getCurrentClient();
240+
auto filename = client->mdiClientFileName();
241+
auto repoRoot = getConfig().getGitLastDir();
242+
auto output = runGit({"-C", repoRoot, "branch", "-a"});
247243
auto branches = output.split('\n', Qt::SkipEmptyParts);
248244
form->branchListCombo->clear();
249-
int activeIndex = -1;
245+
auto activeIndex = -1;
250246
auto delegate = static_cast<BoldItemDelegate *>(form->branchListCombo->itemDelegate());
251247
for (auto const &line : branches) {
252-
bool isActive = line.startsWith('*');
253-
QString branchName = line.mid(2).trimmed();
248+
auto isActive = line.startsWith('*');
249+
auto branchName = line.mid(2).trimmed();
254250
if (branchName.isEmpty()) {
255251
continue;
256252
}
@@ -272,21 +268,16 @@ void GitPlugin::refreshBranchesHandler() {
272268
}
273269

274270
void GitPlugin::diffBranchHandler() {
275-
if (repoRoot.isEmpty()) {
276-
return;
277-
}
278-
271+
auto manager = getManager();
272+
auto client = manager->getMdiServer()->getCurrentClient();
273+
auto filename = client->mdiClientFileName();
274+
auto repoRoot = QFileInfo(filename).absolutePath();
279275
auto branch = form->branchListCombo->currentText();
280-
if (branch.isEmpty()) {
281-
return;
282-
}
283-
284-
auto diff = runGit({"diff", branch}, false);
276+
auto diff = runGit({"diff", branch});
285277
if (diff.isEmpty()) {
286278
return;
287279
}
288280

289-
auto manager = getManager();
290281
CommandArgs args = {
291282
{GlobalArguments::FileName, QString("diff-%1.diff").arg(branch)},
292283
{GlobalArguments::Content, diff},
@@ -320,27 +311,26 @@ void GitPlugin::deleteBranchHandler() {
320311
if (reply == QMessageBox::Yes) {
321312
auto deleteBranchArg = cb->isChecked() ? "-D" : "-d";
322313
auto args = QStringList{"branch", deleteBranchArg, branch};
323-
auto res = runGit(args, false);
314+
auto res = runGit(args);
324315
form->gitOutput->setText(res);
325316
form->gitOutput->setToolTip(res);
326317
refreshBranchesHandler();
327318
}
328319
}
329320

330321
void GitPlugin::logHandler(GitLog log, const QString &filename) {
331-
auto model = new CommitModel(this);
332-
repoRoot = detectRepoRoot(filename);
333-
322+
auto repoRoot = QFileInfo(filename).absolutePath();
323+
repoRoot = detectRepoRoot(repoRoot);
334324
if (repoRoot.isEmpty()) {
335325
form->label->setText(tr("No commits or not a git repo"));
336326
form->diffBranchButton->setEnabled(true);
337327
form->newBranchButton->setEnabled(true);
338328
form->deleteBranchButton->setEnabled(true);
339-
delete model;
340329
return;
341330
}
342331

343-
auto args = QStringList{"log", "--graph", "--pretty=format:%x01%H%x02%P%x02%an%x02%ai%x02%s"};
332+
auto args = QStringList{"-C", repoRoot, "log", "--graph",
333+
"--pretty=format:%x01%H%x02%P%x02%an%x02%ai%x02%s"};
344334
auto labelText = QString();
345335
switch (log) {
346336
case GitPlugin::GitLog::File:
@@ -354,8 +344,14 @@ void GitPlugin::logHandler(GitLog log, const QString &filename) {
354344
break;
355345
}
356346

347+
getConfig().setGitLastDir(repoRoot);
348+
auto model = new CommitModel(this);
357349
form->label->setText(labelText);
358-
auto output = runGit(args, true);
350+
351+
getConfig().setGitLastDir(repoRoot);
352+
getConfig().setGitLastCommand(args.join(" "));
353+
354+
auto output = runGit(args);
359355
model->setContent(output);
360356
form->listView->setModel(model);
361357
gitDock->raise();
@@ -380,7 +376,8 @@ void GitPlugin::on_gitCommitClicked(const QModelIndex &mi) {
380376
[this, widget](const QModelIndex &i) {
381377
auto manager = getManager();
382378
auto filename = i.data().toString();
383-
auto diff = runGit({"show", widget->currentSha1, "--", filename}, false);
379+
auto diff = runGit({"-C", getConfig().getGitLastDir(), "show",
380+
widget->currentSha1, "--", filename});
384381
auto shortSha1 = shortGitSha1(widget->currentSha1);
385382
auto displayName = QString("%1-%2.diff").arg(shortSha1).arg(filename);
386383
CommandArgs args = {
@@ -424,24 +421,12 @@ void GitPlugin::on_gitCommitDoubleClicked(const QModelIndex &mi) {
424421
manager->handleCommandAsync(GlobalCommands::DisplayText, args);
425422
}
426423

427-
QString GitPlugin::runGit(const QStringList &args, bool saveConfig) {
428-
if (repoRoot.isEmpty()) {
429-
qDebug() << "Repository is not configured, doing nothing.";
430-
return {};
431-
}
432-
433-
// qDebug() << "git: repo is at" << repoRoot;
424+
QString GitPlugin::runGit(const QStringList &args) {
434425
// qDebug() << "git " << args.join(" ");
435426
QProcess p;
436427
p.setProcessChannelMode(QProcess::ProcessChannelMode::MergedChannels);
437-
p.setWorkingDirectory(repoRoot);
438428
p.start(gitBinary, args);
439429
p.waitForFinished();
440-
if (saveConfig) {
441-
getConfig().setGitLastCommand(args.join(" "));
442-
getConfig().setGitLastDir(repoRoot);
443-
getManager()->saveSettings();
444-
}
445430
return QString::fromUtf8(p.readAllStandardOutput());
446431
}
447432

@@ -453,26 +438,29 @@ QString GitPlugin::detectRepoRoot(const QString &filePath) {
453438
return QString::fromUtf8(p.readAllStandardOutput()).trimmed();
454439
}
455440

456-
QString GitPlugin::getDiff(const QString &path) { return runGit({"diff", path}, false); }
441+
QString GitPlugin::getDiff(const QString &path) {
442+
auto fi = QFileInfo(path);
443+
return runGit({"-C", fi.absolutePath(), "diff"});
444+
}
457445

458-
QString GitPlugin::getRawCommit(const QString &sha1) { return runGit({"show", sha1}, false); }
446+
QString GitPlugin::getRawCommit(const QString &sha1) {
447+
return runGit({"-C", getConfig().getGitLastDir(), "show", sha1});
448+
}
459449

460450
void GitPlugin::restoreGitLog() {
461451
if (!form) {
462452
return;
463453
}
464454

465455
auto cmd = getConfig().getGitLastCommand();
466-
auto dir = getConfig().getGitLastDir();
467-
if (cmd.isEmpty() || dir.isEmpty()) {
456+
if (cmd.isEmpty()) {
468457
return;
469458
}
470459

471-
repoRoot = dir;
472460
auto args = cmd.split(" ");
473461
auto model = new CommitModel(this);
474462
form->label->setText(cmd);
475-
auto output = runGit(args, false);
463+
auto output = runGit(args);
476464
model->setContent(output);
477465
form->listView->setModel(model);
478466

src/plugins/git/GitPlugin.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class GitPlugin : public IPlugin {
4646
void on_gitCommitDoubleClicked(const QModelIndex &mi);
4747

4848
public slots:
49-
QString runGit(const QStringList &args, bool saveConfig);
49+
QString runGit(const QStringList &args);
5050
QString detectRepoRoot(const QString &path);
5151
QString getDiff(const QString &path);
5252
QString getRawCommit(const QString &sha1);
@@ -61,7 +61,6 @@ class GitPlugin : public IPlugin {
6161
QAction *stash = nullptr;
6262
QAction *branches = nullptr;
6363
QString gitBinary = "git";
64-
QString repoRoot;
6564
QDockWidget *gitDock = nullptr;
6665
Ui::GitCommandsForm *form = nullptr;
6766
};

0 commit comments

Comments
 (0)