Skip to content

how to fix clone with progress and input username/password #1563

@xiaoyu1095

Description

@xiaoyu1095

import git
from rich import console, progress
import logging

class GitRemoteProgress(git.RemoteProgress):
OP_CODES = [
"BEGIN",
"CHECKING_OUT",
"COMPRESSING",
"COUNTING",
"END",
"FINDING_SOURCES",
"RECEIVING",
"RESOLVING",
"WRITING",
]
OP_CODE_MAP = {
getattr(git.RemoteProgress, _op_code): _op_code for _op_code in OP_CODES
}

def __init__(self) -> None:
    super().__init__()
    self.progressbar = progress.Progress(
        progress.SpinnerColumn(),
        # *progress.Progress.get_default_columns(),
        progress.TextColumn("[progress.description]{task.description}"),
        progress.BarColumn(),
        progress.TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
        "eta",
        progress.TimeRemainingColumn(),
        progress.TextColumn("{task.fields[message]}"),
        console=console.Console(),
        transient=False,
    )
    self.progressbar.start()
    self.active_task = None

def __del__(self) -> None:
    # logger.info("Destroying bar...")
    self.progressbar.stop()

@classmethod
def get_curr_op(cls, op_code: int) -> str:
    """Get OP name from OP code."""
    # Remove BEGIN- and END-flag and get op name
    op_code_masked = op_code & cls.OP_MASK
    return cls.OP_CODE_MAP.get(op_code_masked, "?").title()

def update(self, op_code, cur_count, max_count, message) -> None:

    # Start new bar on each BEGIN-flag
    if op_code & self.BEGIN:
        self.curr_op = self.get_curr_op(op_code)
        # logger.info("Next: %s", self.curr_op)
        self.active_task = self.progressbar.add_task(
            description=self.curr_op,
            total=max_count,
            message=message,
        )

    self.progressbar.update(
        task_id=self.active_task,
        completed=cur_count,
        message=message,
    )

    # End progress monitoring on each END-flag
    if op_code & self.END:
        # logger.info("Done: %s", self.curr_op)
        self.progressbar.update(
            task_id=self.active_task,
            message=f"[bright_black]{message}",
        )

without progress , the input request is printed on the command line

repo = git.Repo.clone_from(url="",to_path="./demo")
image

with progress, the input request is printed on the command line but it will turn into a black box instantly
repo = git.Repo.clone_from(url="",to_path="./demo",progress=GitRemoteProgress())
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions