Files
profilarr/backend/app/git/status/status.py
santiagosayshey d8f944af11 improvements: whole lotta stuff (#18)
* feat: initialise task scheduler

* feat: add "next run" field to task status

* fix: adjust status route path

* feat: task dashboard + api functions

* fix: return sucess object for alert

* refactor: turn task cards into seperate objects

* fix: change task names

* feat: implement compile and import module
- only working for custom formats for now

* refactor/feature: refactor compilation module
- seperate file for mappings
- seperate format compiler into new file
- add compiler for profiles

* feat: add import logic for quality profiles

* fix: properly resolve cutoff IDs for singular qualities

* fix: remux mappings for sonarr

* fix: retain quality group order
- stop groups first, then singular

* fix: dynamically find next group ID to stop duplicate IDs from occuring

* fix: normalise quality letter case

* feat: add api functions for import functionality

* fix: adjust validation for import.js

* feat: add mass selection tool componnet
- keyboard shortcuts to enter state
- mass delete / import

* feat: add loading indicator on import

* style: improve selected card styling

* fix: append extra custom formats with 0 score

* perf: add git status caching to improve load times

* fix: adjust mass import handling and selection logic
- use content.name rather than content.id

* fix: enhance quality name mapping with alternate names and case-insensitive lookups

* feat: add description truncation to ProfileCard for improved readability
- also remove qualitites

* fix: update upgrade quality selection logic to handle disabling scenarios

* feat: expand language mappings with additional languages and identifiers

* feat: enhance profile conversion logging with language handling

* fix: clarify language setting impact in ProfileLanguagesTab component

* feat: add Sonarr language mappings and update language selection logic

* feat: implement language normalization and enhance logging in format conversion

* feat: enhance logging in format conversion and add language specification handling

* feat: add Afrikaans and Albanian languages to the language constants

* refactor: remove language strictness feature and update language handling in ProfileModal

* feat: add logging setup for improved debugging in mappings module

* feat: enhance language compilation to work with new system

* feat: update language handling in ProfileLanguagesTab to support 'any' behavior

* fix: remove redundant import statement in profile.py

* feat: implement in-memory format import functionality and update profile compiler to utilize it for non english language compilation

* feat: add comparison for tweaks in quality profile changes

* feat: add functions to convert display names to filenames and vice versa

* fix: remove unnecessary filename modification in formats data

* feat: add process_tweaks function to handle profile tweaks and import formats before compilation

* feat: refactor process_tweaks to modularize format import and scoring
- include lossless audio tweak handling

* feat: add support for Dolby Vision no fallback and bleeding edge codecs in process_tweaks

* feat: add support for disabling prereleases based on profile tweaks

* fix: reduce cache TTL from 30 seconds to 1 second for quicker updates

* feat: add resetState function to initialize profile modal state

* feat: enhance save_yaml_file function to optionally use data name for filename

* feat: enhance handle_rename function to account for staged renames

* feat: enhance revert functionality to handle untracked files and staged deletions

* chore: update timezone setting in docker-compose.yml

* feat: increase maximum description length in ProfileCard component

* refactor: generic YAML comparison and change summary functionality

* refactor: incoming changes now uses generic comparison logic

* refactor: add YAML conflict comparison functionality with detailed summary generation for merge conflicts

* refactor: heavily simplified resolve conflict modal to work with new generic conflict parsing

* refactor: implement GitStatusManager for improved repository status handling and sync task updates

* refactor: integrate GitStatusManager to update remote status after pull operations

* refactor: remove isDevMode prop from ChangeRow, ConflictRow, ConflictTable, ChangeTable, RepoContainer, and ActionButtons components

* refactor: migrate settings handling from settings_utils to db module and enhance settings management

* refactor: remove deprecated authentication methods and streamline push operations with SSH access

* refactor: enhance error handling and authentication for Git operations, including PAT support

* refactor: remove old settings_utils

* refactor: add settings prop to StatusContainer and update database initialization for profilarr_pat

* refactor: simplify format name handling in ViewChanges component by removing old API call

* fix: update fetchSettings to handle cases with no git repository and ensure settings is null

* fix: enhance get_git_status to return a valid status object when no git repository is found

* style: various improvements to repo container
- add database stats
- add organisation / profile avatar
- parse organisation / repo name
- improvements to branch button

* feat: implement authentication setup and middleware for secure session management

* fix: auto login after setting up authentication

* feat: enhance authentication setup with GET method and track failed attempts

* feat: add authentication setup and login components with API integration

* feat: redesign SetupPage component with improved layout and user guidance

* feat: enhance LoginPage layout with improved design and user guidance

* refactor: remove unused API functions for regex and format management

* improvements: whole lot more stuff (#17)

* feat: implement configuration management for directory paths and session settings

* feat: implement backup management with API endpoints for backup operations

* feat: add backup import functionality with zip file validation and restoration

* feat: implement backup API with endpoints for listing, creating, downloading, restoring, deleting, and importing backups

* feat: enhance backup listing with file size and last modified time

* feat: add backup management interface with listing, creation, restoration, and deletion functionalities

* fix: refresh backups list after successful deletion

* feat: create BackupCard component for displaying backup details and actions

* fix: status parsing improvements
- now properly shows outgoing changes with / without developer mode

* fix: remove authentication bypass for backup routes during testing

* feat: add logging configuration and ensure log directory creation

* feat: implement application-wide logging configuration and ensure log directory creation

* feat: add logging blueprint with endpoints for retrieving and searching log files

* feat: add git logging configuration to enhance logging capabilities

* refactor: enhance logging details and improve error handling in repository cloning and file processing

* refactor: update tab labels for clarity in settings page

* refactor: remove unused tasks and streamline task scheduler

* refactor: improve repository settings handling and UI updates in RepoContainer

* style: add slight gradient to modal

* feat: enhance footer with GitHub repository info and organization avatar

* refactor: update imports and enhance modal layout for linking Git repository

* refactor: remove documentation and issue links from footer component

* refactor: git settings refactor.
- new git container to contain repo / status
- split repo container into active / empty components
- split status into seperate sections for incoming, outgoing, conflict

* chore: simplify environment configuration by using .env file in docker-compose

* fix: update remote status after commit and push operations

* refactor: restructure application initialization and configure Git user settings on startup

* refactor: improve default Git user configuration handling in initialization

* style: enhance UI styling and structure for Git status display

* style: enhance UI layout and styling for ChangeRow and ChangeTable components

* style: enhance UI layout and styling for CommitMessage component

* style: improve selected row styling

* style: enhance UI styling for ConflictRow and ConflictTable components

* style: update merge process to include remote status update after finalization

* style: update noChangesMessages for improved clarity and engagement

* feat: add animated Logo component and integrate it into Navbar

* feat: enhance selection handling
- add shift selection
- add will be selected state and styling

* feat: enhance mouse tracking for shift selection

* refactor: remove deprecated ActionButtons

* style: unify button colors and update tooltips in IncomingChanges and OutgoingChanges components

* feat: add auto-pull feature with toggle in settings and backend support

* fix: update auto-pull implementation to use integer values

* feat: implement auto-pull functionality in remote status update

* file: improve value formatting for new files

* fix: change logger level to DEBUG and add debug message for profile import attempts

* refactor: remove logging statements and streamline exception handling in format compiler

* feat: enhance format import process with detailed logging and error handling

* feat: enhance profile import process with detailed logging and error handling

* refactor: remove non error logging statements for profile compilation

* feat: enhance logging for memory-based format import with detailed success and error messages

* feat: add logging for language settings and compiled profile data in profile import

* feat: add dedicated logging for importarr with separate log file and configuration

* feat: add logging API with functions to fetch logs, search, and filter by level

* feat: add logs tab to settings page with LogContainer component

* feat: add LogContainer and LogViewer components for enhanced log management

* fix: dynamic vertical height for log viewer

* fix: reduce log file size and increase backup count for improved log management

* fix: enhance error logging with exception type and full traceback for better debugging

* fix: use mapped cutoff name for profile conversion to ensure correct quality mapping

* fix: add validation for git repository existence before syncing

* fix: implement delete constraints check before item deletion to prevent breaking references

* fix: enhance delete constraints check with improved logging and name normalization

* fix: add protection against deletion of required custom formats in delete constraints check

* feat: implement ANSI color parsing in LogViewer for improved log readability

* feat: enhance ViewChanges component with improved key parsing and rendering of changes

* refactor: improve styling and structure of ResolveConflicts component for better readability and usability

* fix: improve error handling and response for arr config saving

* feat: extend arr configuration with additional fields and sync methods

* feat: add DataSelectorModal component for selecting data to sync

* feat: enhance ArrModal and DataSelectorModal with improved layout and data display

* feat: update ArrContainer to use new API import and include additional arrConfig fields

* feat: enhance ArrCard component with sync details and improved layout

* feat: add AddButton component with custom positioning and animations

* feat: replace Add New Card section with AddButton component for improved UX

* feat: reposition AddButton in ArrContainer for better visibility

* feat: replace AddNewCard with AddButton in RegexPage for improved UX

* feat: replace AddNewCard with AddButton in FormatPage for improved UX

* feat: replace AddNewCard with AddButton in ProfilePage for improved UX
2025-02-05 16:09:59 +10:30

254 lines
9.3 KiB
Python

# git/status/status.py
import git
from git.exc import GitCommandError, InvalidGitRepositoryError
import logging
from .incoming_changes import get_incoming_changes
from .outgoing_changes import get_outgoing_changes
from .merge_conflicts import get_merge_conflicts
from .utils import determine_type
import os
import yaml
import threading
from datetime import datetime
from ...db import get_settings
logger = logging.getLogger(__name__)
class GitStatusManager:
_instance = None
_lock = threading.Lock()
def __init__(self, repo_path):
self.repo_path = repo_path
self.repo = git.Repo(repo_path)
self.status = {
# Local status
"branch": "",
"outgoing_changes": [],
"is_merging": False,
"merge_conflicts": [],
"has_conflicts": False,
# Remote status
"remote_branch_exists": False,
"commits_behind": 0,
"commits_ahead": 0,
"incoming_changes": [],
"has_unpushed_commits": False,
"unpushed_files": [],
# Metadata
"last_local_update": None,
"last_remote_update": None
}
@classmethod
def get_instance(cls, repo_path=None):
if not cls._instance and repo_path:
with cls._lock:
if not cls._instance:
cls._instance = cls(repo_path)
return cls._instance
def update_local_status(self):
"""Update only local repository status"""
try:
self.repo = git.Repo(self.repo_path) # Refresh repo instance
with self._lock:
# Update branch
self.status["branch"] = self.repo.active_branch.name
# Check merge status
self.status["is_merging"] = os.path.exists(
os.path.join(self.repo.git_dir, 'MERGE_HEAD'))
# Get local changes
self.status["outgoing_changes"] = get_outgoing_changes(
self.repo)
# Get merge conflicts if merging
self.status["merge_conflicts"] = (get_merge_conflicts(
self.repo) if self.status["is_merging"] else [])
self.status["has_conflicts"] = bool(
self.status["merge_conflicts"])
# Update timestamp
self.status["last_local_update"] = datetime.now().isoformat()
return True
except Exception as e:
logger.error(f"Error updating local status: {str(e)}")
return False
def update_remote_status(self):
"""Update remote repository status - called by scheduled task"""
try:
logger.info(
f"Updating remote status for branch: {self.status['branch']}")
# Do the fetch outside the lock
self.repo.remotes.origin.fetch()
# Get branch name safely
with self._lock:
branch = self.status["branch"]
# Do git operations outside lock
remote_refs = [ref.name for ref in self.repo.remotes.origin.refs]
remote_branch_exists = f"origin/{branch}" in remote_refs
if remote_branch_exists:
commits_behind = list(
self.repo.iter_commits(f'{branch}..origin/{branch}'))
commits_ahead = list(
self.repo.iter_commits(f'origin/{branch}..{branch}'))
# Handle auto-pull before updating status
if len(commits_behind) > 0:
logger.info(
f"Branch is {len(commits_behind)} commits behind")
try:
settings = get_settings()
if int(settings.get('auto_pull_enabled', 0)):
logger.info("Auto-pull enabled, pulling changes")
from ..operations.manager import GitOperations
git_ops = GitOperations(self.repo_path)
pull_result = git_ops.pull(branch)
logger.info(f"Auto-pull result: {pull_result}")
success, message = pull_result
if not success:
logger.error(f"Auto-pull failed: {message}")
# Refresh counts after pull
commits_behind = list(
self.repo.iter_commits(
f'{branch}..origin/{branch}'))
commits_ahead = list(
self.repo.iter_commits(
f'origin/{branch}..{branch}'))
except Exception as e:
logger.error(f"Error during auto-pull: {str(e)}")
# Prepare the status update
incoming = get_incoming_changes(self.repo, branch)
unpushed = self._get_unpushed_changes(
branch) if commits_ahead else []
# Only lock when updating the status
with self._lock:
self.status.update({
"remote_branch_exists":
remote_branch_exists,
"commits_behind":
len(commits_behind),
"commits_ahead":
len(commits_ahead),
"has_unpushed_commits":
len(commits_ahead) > 0,
"incoming_changes":
incoming,
"unpushed_files":
unpushed,
"last_remote_update":
datetime.now().isoformat()
})
else:
with self._lock:
self.status.update({
"remote_branch_exists":
False,
"commits_behind":
0,
"commits_ahead":
0,
"has_unpushed_commits":
False,
"incoming_changes": [],
"unpushed_files": [],
"last_remote_update":
datetime.now().isoformat()
})
return True
except Exception as e:
logger.error(f"Error updating remote status: {str(e)}")
return False
def _get_unpushed_changes(self, branch):
"""Get detailed info about files modified in unpushed commits"""
try:
unpushed_files = self.repo.git.diff(f'origin/{branch}..{branch}',
'--name-only').split('\n')
unpushed_files = [f for f in unpushed_files if f]
detailed_changes = []
for file_path in unpushed_files:
try:
with open(os.path.join(self.repo.working_dir, file_path),
'r') as f:
content = yaml.safe_load(f.read())
detailed_changes.append({
'type':
determine_type(file_path),
'name':
content.get('name', os.path.basename(file_path)),
'file_path':
file_path
})
except Exception as e:
logger.warning(
f"Could not get details for {file_path}: {str(e)}")
detailed_changes.append({
'type': determine_type(file_path),
'name': os.path.basename(file_path),
'file_path': file_path
})
return detailed_changes
except Exception as e:
logger.error(f"Error getting unpushed changes: {str(e)}")
return []
def get_status(self):
"""Get the current status without updating"""
with self._lock:
return self.status.copy()
def get_git_status(repo_path):
try:
status_manager = GitStatusManager.get_instance(repo_path)
status_manager.update_local_status()
return True, status_manager.get_status()
except git.exc.InvalidGitRepositoryError:
logger.info(f"No git repository found at {repo_path}")
# Return a valid status object indicating no repo
return True, {
# Local status - empty/false everything
"branch": "",
"outgoing_changes": [],
"is_merging": False,
"merge_conflicts": [],
"has_conflicts": False,
# Remote status - explicitly show no repo/remote
"remote_branch_exists": False,
"commits_behind": 0,
"commits_ahead": 0,
"incoming_changes": [],
"has_unpushed_commits": False,
"unpushed_files": [],
# Metadata
"last_local_update": None,
"last_remote_update": None,
# New flag to explicitly indicate no repo
"has_repo": False
}
except Exception as e:
logger.error(f"Error in get_git_status: {str(e)}", exc_info=True)
return False, str(e)