Skip to content

Commit

Permalink
Add modified file check
Browse files Browse the repository at this point in the history
  • Loading branch information
chillpert committed Feb 18, 2024
1 parent e96fdc4 commit be67536
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
12 changes: 8 additions & 4 deletions file_tree_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import lfs_lock_parser
import pyqt_helpers
from settings import Settings
import utility
from settings import Settings


class FileTreeWidgetItem(QTreeWidgetItem):
Expand Down Expand Up @@ -401,9 +401,9 @@ class UnlockingFileTreeWidget(FileTreeWidgetBase):
def __init__(self):
super().__init__()

self.setColumnCount(3)
self.setColumnCount(4)
self.setColumnWidth(0, 600)
self.setHeaderLabels(["Files", "Owner", "Id"])
self.setHeaderLabels(["Files", "Owner", "Id", "Diff"])

self.selected_git_user = ""

Expand Down Expand Up @@ -472,7 +472,11 @@ def populate(self, lock_data: [lfs_lock_parser.LfsLockData], selected_git_user:
matched_filter = True

if not requires_filter or (requires_filter and matched_filter):
item = LockDataFileTreeWidgetItem(parent_item, [text, owner, lock_id])
is_file_newer = "Unmodified" if not data.is_file_newer else ""
item = LockDataFileTreeWidgetItem(
parent_item,
[text, owner, lock_id, is_file_newer]
)
item.lock_data = data
item.is_directory = False
item.relative_path = file_path
Expand Down
8 changes: 6 additions & 2 deletions lfs_lock_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" This module implements a parser for Git LFS lock data and related types. """
import os
import dataclasses
import os

import utility
from worker_thread import WorkerThread
Expand All @@ -18,6 +18,8 @@ class LfsLockData:
is_orphaned: bool
# True, if file exists locally
is_local_file: bool
# True, if file is modified or was updated (via commit)
is_file_newer: bool


class LfsLockParser:
Expand Down Expand Up @@ -137,7 +139,9 @@ def _parse_locks():
# if is_orphaned:
# print("File '%s' is orphaned." % file_path)

data.append(LfsLockData(lock_id, lock_owner, file_path, is_orphaned, is_local_file))
is_file_newer = utility.is_file_newer(file_path)
data.append(LfsLockData(
lock_id, lock_owner, file_path, is_orphaned, is_local_file, is_file_newer))

# Keep a copy of the parsed data
LfsLockParser.lock_data = data
Expand Down
2 changes: 2 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Settings:
custom_git_lfs_path = ""
default_mode = "Unlock"
lock_mode_file_filter = ""
tracking_branch = "main"


def load_settings():
Expand All @@ -39,3 +40,4 @@ def load_settings():
Settings.custom_git_lfs_path = config.get('Settings', 'customGitLfsExecutable')
Settings.default_mode = config.get('Settings', 'defaultMode')
Settings.lock_mode_file_filter = config.get('Settings', 'lockModeFileFilter')
Settings.tracking_branch = config.get('Settings', 'trackingBranch')
34 changes: 34 additions & 0 deletions utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def run_command(command: list, cwd: str, print_output=False):
:param print_output: Optionally prints the command's output if successful for debugging purposes
:return: Returns the command's output if successful, otherwise an empty string
"""
# if not hasattr(run_command, "opened_subprocess"):
# run_command.opened_subprocess = False

# If not root directory was specified, we will default to the Git project's root
if cwd == "":
cwd = get_project_root_directory()
Expand Down Expand Up @@ -206,6 +209,37 @@ def is_git_user_admin():
return False


def is_file_newer(relative_path: str):
"""
Checks if the file at the given relative path is modified or newer compared to the main branch
:return: Returns true, if file is newer
"""
# Check if file exists
project_root = get_project_root_directory()
if not os.path.isfile(os.path.join(project_root, relative_path)):
return ""

# Check if branch exists
git_check_branch_cmd = ['git', 'branch', '--list', Settings.tracking_branch]
output = run_command(git_check_branch_cmd, project_root)
if output == "":
return False

# Check if file was modified
git_status_cmd = ['git', 'status', '--porcelain', relative_path]
output = run_command(git_status_cmd, project_root)
if output != "":
return True

# Check if file newer version
git_diff_cmd = ['git', 'diff', '--name-status', Settings.tracking_branch, '--', relative_path]
output = run_command(git_diff_cmd, project_root)
if output != "":
return True

return False


def resource_path(relative_path: str):
"""
Get absolute path to resource, works for dev and for PyInstaller
Expand Down

0 comments on commit be67536

Please sign in to comment.