Skip to content

Commit 92588d7

Browse files
committed
Support local changes for composite mode
1 parent a27900c commit 92588d7

File tree

6 files changed

+101
-37
lines changed

6 files changed

+101
-37
lines changed

qgitc/commitsource.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from .common import Commit
4+
5+
6+
class CommitSource:
7+
8+
def findCommitIndex(self, sha1: str, begin=0, findNext=True) -> int:
9+
raise NotImplemented
10+
11+
def getCommit(self, index: int) -> Commit:
12+
raise NotImplemented
13+
14+
def getCount(self) -> int:
15+
raise NotImplemented

qgitc/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def fromRawString(cls, string):
5959

6060
return commit
6161

62+
def isValid(self):
63+
return len(self.sha1) > 0
64+
6265

6366
class MyProfile():
6467

qgitc/diffview.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
QPointF,
3838
QMimeData)
3939

40+
from .commitsource import CommitSource
4041
from .common import *
4142
from .gitutils import Git, GitProcess
4243
from .findwidget import FindWidget
@@ -150,6 +151,8 @@ def __init__(self, parent=None):
150151
# sub commit to fetch
151152
self._commitList: List[Commit] = []
152153

154+
self._commitSource: CommitSource = None
155+
153156
self.twMenu.addAction(self.tr("External &diff"),
154157
self.__onExternalDiff)
155158
self.twMenu.addSeparator()
@@ -478,7 +481,8 @@ def __commitDesc(self, sha1, repoDir=None):
478481
return " (" + subject + ")"
479482

480483
def __commitToTextLines(self, commit: Commit):
481-
if not commit.sha1 in [Git.LUC_SHA1, Git.LCC_SHA1]:
484+
isLocalChanges = commit.sha1 in [Git.LUC_SHA1, Git.LCC_SHA1]
485+
if not isLocalChanges:
482486
content = self.tr("Author: ") + commit.author + \
483487
" " + commit.authorDate
484488
self.viewer.addAuthorLine(content)
@@ -489,7 +493,14 @@ def __commitToTextLines(self, commit: Commit):
489493

490494
for parent in commit.parents:
491495
content = self.tr("Parent: ") + parent
492-
content += self.__commitDesc(parent, commit.repoDir)
496+
repoDir = commit.repoDir
497+
if isLocalChanges and repoDir:
498+
index = self.commitSource.findCommitIndex(parent)
499+
if index != -1:
500+
parentCommit = self.commitSource.getCommit(index)
501+
repoDir = parentCommit.repoDir
502+
503+
content += self.__commitDesc(parent, repoDir)
493504
self.viewer.addSHA1Line(content, True)
494505

495506
for child in commit.children:
@@ -606,6 +617,9 @@ def queryClose(self):
606617
self.fetcher.cancel()
607618
return True
608619

620+
def setCommitSource(self, source: CommitSource):
621+
self.commitSource = source
622+
609623

610624
class DiffTextLine(SourceTextLineBase):
611625

qgitc/gitutils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,12 @@ def undoMerge(path):
351351
return process.returncode == 0
352352

353353
@staticmethod
354-
def hasLocalChanges(branch, cached=False):
354+
def hasLocalChanges(branch, cached=False, repoDir=None):
355355
# A remote branch should never have local changes
356356
if branch.startswith("remotes/"):
357357
return False
358358

359-
dir = Git.branchDir(branch)
359+
dir = Git.branchDir(branch, repoDir)
360360
# only branch checked out can have local changes
361361
if not dir:
362362
return False
@@ -371,7 +371,7 @@ def hasLocalChanges(branch, cached=False):
371371
return process.returncode == 1
372372

373373
@staticmethod
374-
def branchDir(branch):
374+
def branchDir(branch, repoDir=None):
375375
"""returned the branch directory if it checked out
376376
otherwise returned an empty string"""
377377

@@ -384,7 +384,7 @@ def branchDir(branch):
384384
return Git.REPO_DIR
385385

386386
args = ["worktree", "list"]
387-
data = Git.checkOutput(args)
387+
data = Git.checkOutput(args, repoDir=repoDir)
388388
if not data:
389389
return ""
390390

qgitc/gitview.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def __init__(self, parent=None):
6262
self._delayTimer = QTimer(self)
6363
self._delayTimer.setSingleShot(True)
6464

65+
self.ui.diffView.setCommitSource(self.ui.logView)
66+
6567
self.__setupSignals()
6668

6769
def __setupSignals(self):

qgitc/logview.py

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
QObject,
3333
SIGNAL)
3434

35+
from .commitsource import CommitSource
3536
from .common import *
3637
from .gitutils import *
3738
from .logsfetcher import LogsFetcher
@@ -455,23 +456,55 @@ def nextResult(self):
455456

456457
class CheckLocalChangesThread(QThread):
457458

458-
checkFinished = Signal(bool, bool)
459+
checkFinished = Signal(Commit, Commit)
459460

460-
def __init__(self, branch, parent=None):
461+
def __init__(self, branch, submodules, parent=None):
461462
super().__init__(parent)
462463
self._branch = branch
464+
self._submodules = submodules or [None]
463465

464466
def run(self):
465467
if self.isInterruptionRequested():
466468
return
467-
hasLCC = Git.hasLocalChanges(self._branch, True)
468469

469-
if self.isInterruptionRequested():
470-
return
471-
hasLUC = Git.hasLocalChanges(self._branch)
470+
lccCommit = Commit()
471+
lucCommit = Commit()
472+
473+
for submodule in self._submodules:
474+
if self.isInterruptionRequested():
475+
return
476+
hasLCC, hasLUC = self.fetchLocalChanges(submodule)
477+
if hasLCC:
478+
lccCommit.sha1 = Git.LCC_SHA1
479+
if not lccCommit.repoDir:
480+
lccCommit.repoDir = submodule
481+
else:
482+
subCommit = Commit()
483+
subCommit.sha1 = Git.LCC_SHA1
484+
subCommit.repoDir = submodule
485+
lccCommit.subCommits.append(subCommit)
486+
487+
if hasLUC:
488+
lucCommit.sha1 = Git.LUC_SHA1
489+
if not lucCommit.repoDir:
490+
lucCommit.repoDir = submodule
491+
else:
492+
subCommit = Commit()
493+
subCommit.sha1 = Git.LUC_SHA1
494+
subCommit.repoDir = submodule
495+
lucCommit.subCommits.append(subCommit)
472496

473497
if not self.isInterruptionRequested():
474-
self.checkFinished.emit(hasLCC, hasLUC)
498+
self.checkFinished.emit(lccCommit, lucCommit)
499+
500+
def fetchLocalChanges(self, repoDir=None):
501+
hasLCC = Git.hasLocalChanges(self._branch, True, repoDir)
502+
if self.isInterruptionRequested():
503+
return False, False
504+
hasLUC = Git.hasLocalChanges(self._branch, repoDir=repoDir)
505+
if self.isInterruptionRequested():
506+
return False, False
507+
return hasLCC, hasLUC
475508

476509

477510
class LogGraph(QWidget):
@@ -498,7 +531,7 @@ def paintEvent(self, event):
498531
painter.drawPixmap(0, 0, self._graphImage)
499532

500533

501-
class LogView(QAbstractScrollArea):
534+
class LogView(QAbstractScrollArea, CommitSource):
502535
currentIndexChanged = Signal(int)
503536
findFinished = Signal(int)
504537
findProgress = Signal(int)
@@ -638,24 +671,22 @@ def showLogs(self, branch, args=None):
638671
self.curBranch = branch
639672
self.args = args
640673

674+
submodules = []
641675
if qApp.settings().isCompositeMode():
642676
submodules = self.window().submodules()
643-
self.fetcher.setSubmodules(submodules)
644-
else:
645-
self.fetcher.setSubmodules([])
677+
self.fetcher.setSubmodules(submodules)
646678

647679
self.fetcher.fetch(branch, args)
648680
self.beginFetch.emit()
649681

650-
# TODO:
651682
if self.checkThread:
652683
self.checkThread.disconnect(self)
653684
self.checkThread.requestInterruption()
654685
self.checkThread.wait()
655686
self.checkThread = None
656687

657688
if not args:
658-
self.checkThread = CheckLocalChangesThread(self.curBranch, self)
689+
self.checkThread = CheckLocalChangesThread(self.curBranch, submodules, self)
659690
self.checkThread.checkFinished.connect(self.__onCheckFinished)
660691
self.checkThread.start()
661692

@@ -1028,47 +1059,46 @@ def __onFetchFinished(self, exitCode):
10281059
else:
10291060
self.viewport().update()
10301061

1031-
def __onCheckFinished(self, hasLCC, hasLUC):
1062+
def __onCheckFinished(self, lccCommit: Commit, lucCommit: Commit):
10321063
parent_sha1 = self.data[0].sha1 if self.data else None
10331064

10341065
self.delayUpdateParents = False
1066+
hasLCC = lccCommit.isValid()
1067+
hasLUC = lucCommit.isValid()
1068+
10351069
if hasLCC:
1036-
lcc_cmit = Commit()
1037-
lcc_cmit.sha1 = Git.LCC_SHA1
1038-
lcc_cmit.comments = self.tr(
1070+
lccCommit.comments = self.tr(
10391071
"Local changes checked in to index but not committed")
1040-
lcc_cmit.parents = [parent_sha1] if parent_sha1 else []
1041-
lcc_cmit.children = [Git.LUC_SHA1] if hasLUC else []
1072+
lccCommit.parents = [parent_sha1] if parent_sha1 else []
1073+
lccCommit.children = [Git.LUC_SHA1] if hasLUC else []
10421074

1043-
self.data.insert(0, lcc_cmit)
1044-
parent_sha1 = lcc_cmit.sha1
1045-
self.delayUpdateParents = len(lcc_cmit.parents) == 0
1075+
self.data.insert(0, lccCommit)
1076+
parent_sha1 = lccCommit.sha1
1077+
self.delayUpdateParents = len(lccCommit.parents) == 0
10461078

10471079
if not self.delayUpdateParents:
10481080
if self.data[1].children is None:
10491081
self.data[1].children = []
10501082

1051-
self.data[1].children.append(lcc_cmit.sha1)
1083+
self.data[1].children.append(lccCommit.sha1)
10521084

10531085
if self.curIdx > 0:
10541086
self.curIdx += 1
10551087

10561088
if hasLUC:
1057-
luc_cmit = Commit()
1058-
luc_cmit.sha1 = Git.LUC_SHA1
1059-
luc_cmit.comments = self.tr(
1089+
lucCommit.comments = self.tr(
10601090
"Local uncommitted changes, not checked in to index")
1061-
luc_cmit.parents = [parent_sha1] if parent_sha1 else []
1062-
luc_cmit.children = []
1091+
lucCommit.parents = [parent_sha1] if parent_sha1 else []
1092+
lucCommit.children = []
10631093

1064-
self.data.insert(0, luc_cmit)
1094+
self.data.insert(0, lucCommit)
10651095
self.delayUpdateParents = self.delayUpdateParents or len(
1066-
luc_cmit.parents) == 0
1096+
lucCommit.parents) == 0
10671097

10681098
if not self.delayUpdateParents and not hasLCC:
10691099
if self.data[1].children is None:
10701100
self.data[1].children = []
1071-
self.data[1].children.append(luc_cmit.sha1)
1101+
self.data[1].children.append(lucCommit.sha1)
10721102

10731103
if self.curIdx > 0:
10741104
self.curIdx += 1

0 commit comments

Comments
 (0)