- 
                Notifications
    You must be signed in to change notification settings 
- Fork 55
CM-30526 - Make Git executable optional #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            MarshalX
  merged 9 commits into
  main
from
CM-30526-unhandled-error-when-git-is-not-installed
  
      
      
   
  May 28, 2024 
      
    
  
     Merged
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f98ed01
              
                CM-30526 - Make Git executable optional
              
              
                MarshalX 896bfc1
              
                fix low python versions
              
              
                MarshalX 750e57e
              
                add printer support for unknown errors
              
              
                MarshalX aced3e7
              
                fix low python versions
              
              
                MarshalX 0420b58
              
                add article
              
              
                MarshalX ce1290e
              
                Merge branch 'main' into CM-30526-unhandled-error-when-git-is-not-ins…
              
              
                MarshalX 34027e0
              
                cover with tests; fix error handling
              
              
                MarshalX 4bd0281
              
                Merge branch 'main' into CM-30526-unhandled-error-when-git-is-not-ins…
              
              
                MarshalX 21a75c6
              
                remove forgotten comments
              
              
                MarshalX File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| from abc import ABC, abstractmethod | ||
| from functools import lru_cache | ||
| from typing import TYPE_CHECKING, Optional, Type | ||
|  | ||
| _GIT_ERROR_MESSAGE = """ | ||
| Cycode CLI needs the git executable to be installed on the system. | ||
| Git executable must be available in the PATH. | ||
| Git 1.7.x or newer is required. | ||
| You can help Cycode CLI to locate the Git executable | ||
| by setting GIT_PYTHON_GIT_EXECUTABLE=<path/to/git> environment variable. | ||
| """.strip().replace('\n', ' ') | ||
|  | ||
| try: | ||
| import git | ||
| except ImportError: | ||
| git = None | ||
|  | ||
| if TYPE_CHECKING: | ||
| from git import PathLike, Repo | ||
|  | ||
|  | ||
| class GitProxyError(Exception): | ||
| pass | ||
|  | ||
|  | ||
| class _AbstractGitProxy(ABC): | ||
| @abstractmethod | ||
| def get_repo(self, path: Optional['PathLike'] = None) -> 'Repo': | ||
| ... | ||
|  | ||
| @abstractmethod | ||
| def get_null_tree(self) -> object: | ||
| ... | ||
|  | ||
| @abstractmethod | ||
| def get_invalid_git_repository_error(self) -> Type[GitProxyError]: | ||
| ... | ||
|  | ||
| @abstractmethod | ||
| def get_git_command_error(self) -> Type[GitProxyError]: | ||
| ... | ||
|  | ||
|  | ||
| class _DummyGitProxy(_AbstractGitProxy): | ||
| def get_repo(self, path: Optional['PathLike'] = None) -> 'Repo': | ||
| raise RuntimeError(_GIT_ERROR_MESSAGE) | ||
|  | ||
| def get_null_tree(self) -> object: | ||
| return object() | ||
|  | ||
| def get_invalid_git_repository_error(self) -> Type[GitProxyError]: | ||
| return GitProxyError | ||
|  | ||
| def get_git_command_error(self) -> Type[GitProxyError]: | ||
| return GitProxyError | ||
|  | ||
|  | ||
| class _GitProxy(_AbstractGitProxy): | ||
| def get_repo(self, path: Optional['PathLike'] = None) -> 'Repo': | ||
| return git.Repo(path) | ||
|  | ||
| def get_null_tree(self) -> object: | ||
| return git.NULL_TREE | ||
|  | ||
| @lru_cache(maxsize=None) # noqa: B019 | ||
| def get_invalid_git_repository_error(self) -> Type[GitProxyError]: | ||
| # we must cache it because we want to return the same class every time | ||
| class InvalidGitRepositoryError(GitProxyError, git.InvalidGitRepositoryError): | ||
| ... | ||
|  | ||
| return InvalidGitRepositoryError | ||
|  | ||
| @lru_cache(maxsize=None) # noqa: B019 | ||
| def get_git_command_error(self) -> Type[GitProxyError]: | ||
| # we must cache it because we want to return the same class every time | ||
| class GitCommandError(GitProxyError, git.GitCommandError): | ||
| ... | ||
|  | ||
| return GitCommandError | ||
|  | ||
|  | ||
| git_proxy = _GitProxy() if git else _DummyGitProxy() | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.