@@ -24,6 +24,7 @@ def __init__(self, command, cwd, env, username, password):
2424 self .env = env
2525 self .username = username
2626 self .password = password
27+
2728 def communicate (self ):
2829 try :
2930 p = pexpect .spawn (
@@ -64,8 +65,9 @@ class Git:
6465 A single parent class containing all of the individual git methods in it.
6566 """
6667
67- def __init__ (self , root_dir ):
68- self .root_dir = os .path .expanduser (root_dir )
68+ def __init__ (self , contents_manager ):
69+ self .contents_manager = contents_manager
70+ self .root_dir = os .path .realpath (os .path .expanduser (contents_manager .root_dir ))
6971
7072 def config (self , top_repo_path , ** kwargs ):
7173 """Get or set Git options.
@@ -892,3 +894,55 @@ def _get_tag(self, current_path, commit_sha):
892894 error .decode ("utf-8" ), " " .join (command )
893895 )
894896 )
897+
898+ def show (self , filename , ref , top_repo_path ):
899+ """
900+ Execute git show <ref:filename> command & return the result.
901+ """
902+ command = ["git" , "show" , '{}:{}' .format (ref , filename )]
903+ p = subprocess .Popen (
904+ command ,
905+ stdout = PIPE ,
906+ stderr = PIPE ,
907+ cwd = top_repo_path
908+ )
909+ output , error = p .communicate ()
910+
911+ error_messages = map (lambda n : n .lower (), [
912+ "fatal: Path '{}' exists on disk, but not in '{}'" .format (filename , ref ),
913+ "fatal: Path '{}' does not exist (neither on disk nor in the index)" .format (filename )
914+ ])
915+ lower_error = error .decode ('utf-8' ).lower ()
916+ if p .returncode == 0 :
917+ return output .decode ('utf-8' )
918+ elif any ([msg in lower_error for msg in error_messages ]):
919+ return ""
920+ else :
921+ raise HTTPError (log_message = 'Error [{}] occurred while executing [{}] command to retrieve plaintext diff.' .format (
922+ error .decode ('utf-8' ),
923+ ' ' .join (command )
924+ ))
925+
926+ def get_content (self , filename , top_repo_path ):
927+ """
928+ Get the file content of filename.
929+ """
930+ relative_repo = os .path .relpath (top_repo_path , self .root_dir )
931+ model = self .contents_manager .get (path = os .path .join (relative_repo , filename ))
932+ return model ['content' ]
933+
934+ def diff_content (self , filename , prev_ref , curr_ref , top_repo_path ):
935+ """
936+ Collect get content of prev and curr and return.
937+ """
938+ prev_content = self .show (filename , prev_ref ["git" ], top_repo_path )
939+ if "special" in curr_ref :
940+ if curr_ref ["special" ] == "WORKING" :
941+ curr_content = self .get_content (filename , top_repo_path )
942+ elif curr_ref ["special" ] == "INDEX" :
943+ curr_content = self .show (filename , "" , top_repo_path )
944+ else :
945+ raise HTTPError (log_message = "Error while retrieving plaintext diff, unknown special ref '{}'." .format (curr_ref ["special" ]))
946+ else :
947+ curr_content = self .show (filename , curr_ref ["git" ], top_repo_path )
948+ return {"prev_content" : prev_content , "curr_content" : curr_content }
0 commit comments