Linux Format - UK (2020-03)

(Antfer) #1
http://www.techradar.com/pro/linux March 2020 LXF260 91

Python & Qt5 CODING ACADEMY


GIT STATUS – PORCELAIN


changes that are not yet staged for commit:
if sizeOfList == 0: # no problems to report
item = QListWidgetItem(‘no modified, deleted or
untracked files’)problems
tem.setData(Qt.UserRole, COLOR_GREEN)
item.setForeground(QColor(COLOR_GREEN))
self.listWidgetStatus.addItem(item)
else:
while i < sizeOfList:
line = strArray[i].decode().strip()
i += 1
x = line[0];
y = line[1];
if x == ‘M’ or y == ‘M’: # modified
s = “modified: “
color = COLOR_RED
elif x == ‘D’ or y == ‘D’: # deleted
s = “deleted: “
color = COLOR_ORANGE
elif x == ‘A’ or y == ‘A’: # added
s = “added: “
color = COLOR_ORANGE
elif x == ‘R’ or y == ‘R’: # renamed
s = “renamed: “
color = COLOR_ORANGE
elif x == ‘C’ or y == ‘C’: # copied
s = “copied: “
color = COLOR_ORANGE
elif x == ‘?’ or y == ‘?’: # untracked
s = “untracked: “
color = COLOR_ORANGE

item = QListWidgetItem(s + line[2:])
item.setData(Qt.UserRole, color)
item.setForeground(QColor(color))
self.listWidgetStatus.addItem(item)
We have somewhat arbitrarily decided that a
modified file is a serious problem and everything else
is a minor problem. If you have a different idea about
what constitutes a serious problem, this function and
quickCheckPriority() are where you should go to make
your changes.
That ends the code for repoSelectionChanged. We
also capture the signal emitted by listWidgetStatus
when its selection is changed by the keyboard or the
mouse, but we have little to do in response to that
signal. The only thing we need to place in
statusSelectionChanged() is the listWidgetStatus
selected style sheet, so that we’ll preserve the text
colour when the listWidgetStatus selection is changed:
def statusSelectionChanged(self):
““”
listWidgetStatus selection has changed


  • modify the listWidgetStatus stylesheet
    ““”
    self.listWidgetStatus.setStyleSheet(“””
    QListWidget::item:selected { color: ““” +
    self.listWidgetStatus.currentItem().data(Qt.UserRole)



  • ““”; }
    QListWidget::item:selected { background-color:
    white; }
    QListWidget::item:selected { border: 2px solid red; }


““”)
When pushButtonRefresh is clicked we capture the
signal it emits and then can invoke the refresh()
member function.
As you can see we clear everything, so before clicking
pushButtonRefresh you can modify gitStatus.ini and
refresh() will detect the changes. You can also open a
console and use git commit to commit files before
clicking refresh().
The refresh() method will get the text of the current
selection, and then after deleting and restoring the
contents of listWidgetRepo it will try to find that text. If it
finds it, it will select the same item that was selected
before refresh() was called.
And that brings us to the conclusion of our
description of gitStatus.py. We hope you find it to be a
useful tool. We also hope that it encourages you to build
other tools. Have fun!

In order to determine the status of the files in a git working directory,
we interrogate the repository using the git status --porcelain
command. The --porcelain option makes it a bit easier for a computer
to read the output. It simply proceeds the file path with an M for
modified, D for deleted, R for renamed, A for added and ?? for
untracked. A modified file has a different version in the repository
from the version in the working directory, so your repository is not up
to date: you haven’t committed your latest changes.
An untracked file is present in the working directory but doesn’t
exist in your repository. Perhaps it’s a file that you meant to add to the
repository, but you haven’t gotten around to it yet.
A deleted file exists in the repository but isn’t present in the working
directory. It can be recovered from git, but perhaps it should no longer
be part of the repository and should be removed.
Note that there may be files present on the disk that are not meant
to be part of the repository. For example, the compilation process
may produce build artefacts. Any files like this should be listed in a
.gitignore file in your working directory. Everything in a .gitignore file
is simply ignored by git status.

DON’T MAKE US TURN INTO GITS Subscribe now at http://bit.ly/LinuxFormat


Figure 4: Connecting the pushButtonClose(bool) clicked SIGNAL to the Dialog accept() slot. Notice
the line dragged between the pushButtonClose and the dialog’s X button.

888March 202 Mcdisu dsePyQt March 2020 LXF260 91


Python & Qt5 CODING ACADEMY


GITSTATUS– PORCELAIN


changes that are not yet staged for commit:
if sizeOfList == 0: # no problems to report
item = QListWidgetItem(‘no modified, deleted or
untracked files’)problems
tem.setData(Qt.UserRole, COLOR_GREEN)
item.setForeground(QColor(COLOR_GREEN))
self.listWidgetStatus.addItem(item)
else:
while i < sizeOfList:
line = strArray[i].decode().strip()
i += 1
x = line[0];
y = line[1];
if x == ‘M’ or y == ‘M’: # modified
s = “modified: “
color = COLOR_RED
elif x == ‘D’ or y == ‘D’: # deleted
s = “deleted: “
color = COLOR_ORANGE
elif x == ‘A’ or y == ‘A’: # added
s = “added: “
color = COLOR_ORANGE
elif x == ‘R’ or y == ‘R’: # renamed
s = “renamed: “
color = COLOR_ORANGE
elif x == ‘C’ or y == ‘C’: # copied
s = “copied: “
color = COLOR_ORANGE
elif x == ‘?’ or y == ‘?’: # untracked
s = “untracked: “
color = COLOR_ORANGE

item = QListWidgetItem(s + line[2:])
item.setData(Qt.UserRole, color)
item.setForeground(QColor(color))
self.listWidgetStatus.addItem(item)
We have somewhat arbitrarily decided that a
modified file is a serious problem and everything else
is a minor problem. If you have a different idea about
what constitutes a serious problem, this function and
quickCheckPriority() are where you should go to make
your changes.
That ends the code for repoSelectionChanged. We
also capture the signal emitted by listWidgetStatus
when its selection is changed by the keyboard or the
mouse, but we have little to do in response to that
signal. The only thing we need to place in
statusSelectionChanged() is the listWidgetStatus
selected style sheet, so that we’ll preserve the text
colour when the listWidgetStatus selection is changed:
def statusSelectionChanged(self):
““”
listWidgetStatus selection has changed


  • modify the listWidgetStatus stylesheet
    ““”
    self.listWidgetStatus.setStyleSheet(“””
    QListWidget::item:selected { color: ““” +
    self.listWidgetStatus.currentItem().data(Qt.UserRole)



  • ““”; }
    QListWidget::item:selected { background-color:
    white; }
    QListWidget::item:selected { border: 2px solid red; }


““”)
When pushButtonRefresh is clicked we capture the
signal it emits and then can invoke the refresh()
member function.
As you can see we clear everything, so before clicking
pushButtonRefresh you can modify gitStatus.ini and
refresh() will detect the changes. You can also open a
console and use git commit to commit files before
clicking refresh().
The refresh() method will get the text of the current
selection, and then after deleting and restoring the
contents of listWidgetRepo it will try to find that text. If it
finds it, it will select the same item that was selected
before refresh() was called.
And that brings us to the conclusion of our
description of gitStatus.py. We hope you find it to be a
useful tool. We also hope that it encourages you to build
other tools. Have fun!

In order to determine the status of the files in a git working directory,
we interrogate the repository using the git status --porcelain
command. The --porcelain option makes it a bit easier for a computer
to read the output. It simply proceeds the file path with an M for
modified, D for deleted, R for renamed, A for added and ?? for
untracked. A modified file has a different version in the repository
from the version in the working directory, so your repository is not up
to date: you haven’t committed your latest changes.
An untracked file is present in the working directory but doesn’t
exist in your repository. Perhaps it’s a file that you meant to add to the
repository, but you haven’t gotten around to it yet.
A deleted file exists in the repository but isn’t present in the working
directory. It can be recovered from git, but perhaps it should no longer
be part of the repository and should be removed.
Note that there may be files present on the disk that are not meant
to be part of the repository. For example, the compilation process
may produce build artefacts. Any files like this should be listed in a
.gitignore file in your working directory. Everything in a .gitignore file
is simply ignored by git status.

DON’T MAKE US TURN INTO GITS Subscribe now at http://bit.ly/LinuxFormat


Figure 4: Connecting the pushButtonClose(bool) clicked SIGNAL to the Dialog accept() slot. Notice
the line dragged between the pushButtonClose and the dialog’s X button.
Free download pdf