from typing import Optional


def handle_git_feedback(current_feedback: Optional[str], message: str) -> str:
    """Function to handle git output in a reasonable way for UI rendering for the user

    Args:
        current_feedback: current string of feedback stored in the background job
        message: new message output from git

    Returns:
        string containing all messages separated by newlines
    """
    # We manage whitespace for git feedback in this function
    message = message.strip()

    if not current_feedback:
        # This is the first line provided as feedback so just return the message.
        return message

    # Clean up some of the output that we know we want to ignore for now
    # that isn't super useful to sent back to the user.
    lines_to_skip = (".git/info/lfs.locksverify true",
                     "Locking support detected on remote",
                     "hint:")
    if message.startswith(lines_to_skip):
        return current_feedback

    # Check if the new message is actually an update to the previous message (e.g. a progress indicator)
    lines = current_feedback.split('\n')
    last_line = lines[-1]
    msg_parts = message.split(':')
    if len(msg_parts) > 1:
        # You might have a progress update vs. a new message
        last_line_parts = last_line.split(':')
        if len(msg_parts) == len(last_line_parts):
            if msg_parts[0] == last_line_parts[0]:
                # It's an update! Remove last line to update instead of just append
                _ = lines.pop()

    lines.append(message)
    return "\n".join(lines)