Skip to content
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

Diff assets against their revision on origin/develop #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 126 additions & 2 deletions Source/GitSourceControl/Private/GitSourceControlModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@

#include "GitSourceControlModule.h"

#include "AssetToolsModule.h"
#include "EditorStyleSet.h"
#include "Misc/App.h"
#include "Modules/ModuleManager.h"
#include "Features/IModularFeatures.h"

#include "ContentBrowserModule.h"
#include "ContentBrowserDelegates.h"

#include "GitSourceControlOperations.h"
#include "GitSourceControlUtils.h"
#include "ISourceControlModule.h"
#include "SourceControlHelpers.h"
#include "Framework/Commands/UIAction.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"

#if ENGINE_MAJOR_VERSION >= 5
#include "ContentBrowser/Public/ContentBrowserModule.h"
Expand Down Expand Up @@ -59,6 +69,10 @@ void FGitSourceControlModule::StartupModule()
CbdHandle_OnSearchBoxChanged = ContentBrowserModule.GetOnSearchBoxChanged().AddLambda( [this]( const FText&, bool ){ GitSourceControlProvider.TicksUntilNextForcedUpdate = 1; } );
CbdHandle_OnAssetSelectionChanged = ContentBrowserModule.GetOnAssetSelectionChanged().AddLambda( [this]( const TArray<FAssetData>&, bool ) { GitSourceControlProvider.TicksUntilNextForcedUpdate = 1; } );
CbdHandle_OnAssetPathChanged = ContentBrowserModule.GetOnAssetPathChanged().AddLambda( [this]( const FString& ) { GitSourceControlProvider.TicksUntilNextForcedUpdate = 2; } );

auto & extenders = ContentBrowserModule.GetAllAssetViewContextMenuExtenders();
extenders.Add( FContentBrowserMenuExtender_SelectedAssets::CreateRaw( this, &FGitSourceControlModule::OnExtendContentBrowserAssetSelectionMenu ) );
ContentBrowserAssetExtenderDelegateHandle = extenders.Last().GetHandle();
#endif
}

Expand All @@ -72,11 +86,16 @@ void FGitSourceControlModule::ShutdownModule()

#if ENGINE_MAJOR_VERSION >= 5
// Unregister ContentBrowserDelegate Handles
FContentBrowserModule& ContentBrowserModule = FModuleManager::Get().LoadModuleChecked<FContentBrowserModule>("ContentBrowser");
FContentBrowserModule & ContentBrowserModule = FModuleManager::Get().LoadModuleChecked< FContentBrowserModule >( "ContentBrowser" );
ContentBrowserModule.GetOnFilterChanged().Remove( CbdHandle_OnFilterChanged );
ContentBrowserModule.GetOnSearchBoxChanged().Remove( CbdHandle_OnSearchBoxChanged );
ContentBrowserModule.GetOnAssetSelectionChanged().Remove( CbdHandle_OnAssetSelectionChanged );
ContentBrowserModule.GetOnAssetPathChanged().Remove( CbdHandle_OnAssetPathChanged );

auto & extenders = ContentBrowserModule.GetAllAssetViewContextMenuExtenders();
extenders.RemoveAll( [ &extender_delegate = ContentBrowserAssetExtenderDelegateHandle ]( const FContentBrowserMenuExtender_SelectedAssets & delegate ) {
return delegate.GetHandle() == extender_delegate;
} );
#endif
}

Expand All @@ -99,6 +118,111 @@ void FGitSourceControlModule::SetLastErrors(const TArray<FText>& InErrors)
}
}

IMPLEMENT_MODULE(FGitSourceControlModule, GitSourceControl);
TSharedRef<FExtender> FGitSourceControlModule::OnExtendContentBrowserAssetSelectionMenu( const TArray<FAssetData> & SelectedAssets )
{
TSharedRef< FExtender > extender( new FExtender() );

extender->AddMenuExtension(
"AssetSourceControlActions",
EExtensionHook::After,
nullptr,
FMenuExtensionDelegate::CreateRaw( this, &FGitSourceControlModule::CreateGitContentBrowserAssetMenu, SelectedAssets ) );

return extender;
}

void FGitSourceControlModule::CreateGitContentBrowserAssetMenu( FMenuBuilder & MenuBuilder, const TArray< FAssetData > SelectedAssets )
{
if (!FGitSourceControlModule::Get().GetProvider().GetStatusBranchNames().Num())
{
return;
}

const FString& BranchName = FGitSourceControlModule::Get().GetProvider().GetStatusBranchNames()[0];
{
MenuBuilder.AddMenuEntry(
// Directly call FInternationalization instead of using LOCTEXT as the macro requires literal strings and as such does not accept runtime constructed strings
FInternationalization::ForUseOnlyByLocMacroAndGraphNodeTextLiterals_CreateText( *FString::Printf( TEXT( "Diff against %s" ), *BranchName ), TEXT( LOCTEXT_NAMESPACE ), TEXT( "GitPlugin" ) ),
FInternationalization::ForUseOnlyByLocMacroAndGraphNodeTextLiterals_CreateText( *FString::Printf( TEXT( "Diff that asset against the version on %s" ), *BranchName ), TEXT( LOCTEXT_NAMESPACE ), TEXT( "GitPlugin" ) ),
FSlateIcon( FAppStyle::GetAppStyleSetName(), "SourceControl.Actions.Diff" ),
FUIAction( FExecuteAction::CreateRaw( this, &FGitSourceControlModule::DiffAssetAgainstGitOriginBranch, SelectedAssets, BranchName ) ) );
}
}

void FGitSourceControlModule::DiffAssetAgainstGitOriginBranch( const TArray< FAssetData > SelectedAssets, FString BranchName ) const
{
for ( int32 AssetIdx = 0; AssetIdx < SelectedAssets.Num(); AssetIdx++ )
{
// Get the actual asset (will load it)
const FAssetData & AssetData = SelectedAssets[ AssetIdx ];

if ( UObject * CurrentObject = AssetData.GetAsset() )
{
const FString PackagePath = AssetData.PackageName.ToString();
const FString PackageName = AssetData.AssetName.ToString();
DiffAgainstOriginBranch( CurrentObject, PackagePath, PackageName, BranchName );
}
}
}

void FGitSourceControlModule::DiffAgainstOriginBranch( UObject * InObject, const FString & InPackagePath, const FString & InPackageName, const FString & BranchName ) const
{
check( InObject );

const FGitSourceControlModule & GitSourceControl = FModuleManager::GetModuleChecked< FGitSourceControlModule >( "GitSourceControl" );
const auto PathToGitBinary = GitSourceControl.AccessSettings().GetBinaryPath();
const auto PathToRepositoryRoot = GitSourceControl.GetProvider().GetPathToRepositoryRoot();

ISourceControlProvider & SourceControlProvider = ISourceControlModule::Get().GetProvider();

const FAssetToolsModule & AssetToolsModule = FModuleManager::GetModuleChecked< FAssetToolsModule >( "AssetTools" );

// Get the SCC state
const FSourceControlStatePtr SourceControlState = SourceControlProvider.GetState( SourceControlHelpers::PackageFilename( InPackagePath ), EStateCacheUsage::Use );

// If we have an asset and its in SCC..
if ( SourceControlState.IsValid() && InObject != nullptr && SourceControlState->IsSourceControlled() )
{
// Get the file name of package
FString RelativeFileName;
if ( FPackageName::DoesPackageExist( InPackagePath, &RelativeFileName ) )
{
// if(SourceControlState->GetHistorySize() > 0)
{
TArray< FString > Errors;
const auto Revision = GitSourceControlUtils::GetOriginRevisionOnBranch( PathToGitBinary, PathToRepositoryRoot, RelativeFileName, Errors, BranchName );

check( Revision.IsValid() );

FString TempFileName;
if ( Revision->Get( TempFileName ) )
{
// Try and load that package
UPackage * TempPackage = LoadPackage( nullptr, *TempFileName, LOAD_ForDiff | LOAD_DisableCompileOnLoad );
if ( TempPackage != nullptr )
{
// Grab the old asset from that old package
UObject * OldObject = FindObject< UObject >( TempPackage, *InPackageName );
if ( OldObject != nullptr )
{
/* Set the revision information*/
FRevisionInfo OldRevision;
OldRevision.Changelist = Revision->GetCheckInIdentifier();
OldRevision.Date = Revision->GetDate();
OldRevision.Revision = Revision->GetRevision();

FRevisionInfo NewRevision;
NewRevision.Revision = TEXT( "" );

AssetToolsModule.Get().DiffAssets( OldObject, InObject, OldRevision, NewRevision );
}
}
}
}
}
}
}

IMPLEMENT_MODULE( FGitSourceControlModule, GitSourceControl );

#undef LOCTEXT_NAMESPACE
8 changes: 8 additions & 0 deletions Source/GitSourceControl/Private/GitSourceControlModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

#include "GitSourceControlSettings.h"
#include "GitSourceControlProvider.h"
#include "Framework/MultiBox/MultiBoxExtender.h"

struct FAssetData;
/**

UEGitPlugin is a simple Git Source Control Plugin for Unreal Engine
Expand Down Expand Up @@ -122,6 +124,11 @@ class FGitSourceControlModule : public IModuleInterface
static void SetLastErrors(const TArray<FText>& InErrors);

private:
TSharedRef< FExtender > OnExtendContentBrowserAssetSelectionMenu( const TArray< FAssetData > & SelectedAssets );
void CreateGitContentBrowserAssetMenu( FMenuBuilder & MenuBuilder, const TArray< FAssetData > SelectedAssets );
void DiffAssetAgainstGitOriginBranch( const TArray< FAssetData > SelectedAssets, FString BranchName ) const;
void DiffAgainstOriginBranch( UObject * InObject, const FString & InPackagePath, const FString & InPackageName, const FString & BranchName ) const;

/** The one and only Git source control provider */
FGitSourceControlProvider GitSourceControlProvider;

Expand All @@ -137,5 +144,6 @@ class FGitSourceControlModule : public IModuleInterface
FDelegateHandle CbdHandle_OnAssetSelectionChanged;
FDelegateHandle CbdHandle_OnSourcesViewChanged;
FDelegateHandle CbdHandle_OnAssetPathChanged;
FDelegateHandle ContentBrowserAssetExtenderDelegateHandle;
#endif
};
45 changes: 44 additions & 1 deletion Source/GitSourceControl/Private/GitSourceControlUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,13 @@ void GetUserConfig(const FString& InPathToGitBinary, const FString& InRepository

bool GetBranchName(const FString& InPathToGitBinary, const FString& InRepositoryRoot, FString& OutBranchName)
{
const FGitSourceControlProvider& Provider = FGitSourceControlModule::Get().GetProvider();
auto * git_module = FModuleManager::Get().LoadModulePtr< FGitSourceControlModule >( "GitSourceControl" );
if ( git_module == nullptr )
{
return false;
}

const FGitSourceControlProvider & Provider = git_module->GetProvider();
if (!Provider.GetBranchName().IsEmpty())
{
OutBranchName = Provider.GetBranchName();
Expand Down Expand Up @@ -2157,6 +2163,43 @@ bool PullOrigin(const FString& InPathToGitBinary, const FString& InPathToReposit
return bSuccess;
}

TSharedPtr<ISourceControlRevision, ESPMode::ThreadSafe> GetOriginRevisionOnBranch( const FString & InPathToGitBinary, const FString & InRepositoryRoot, const FString & InRelativeFileName, TArray<FString> & OutErrorMessages, const FString & BranchName )
{
TGitSourceControlHistory OutHistory;

TArray< FString > Results;
TArray< FString > Parameters;
Parameters.Add( BranchName );
Parameters.Add( TEXT( "--date=raw" ) );
Parameters.Add( TEXT( "--pretty=medium" ) ); // make sure format matches expected in ParseLogResults

TArray< FString > Files;
const auto bResults = RunCommand( TEXT( "show" ), InPathToGitBinary, InRepositoryRoot, Parameters, Files, Results, OutErrorMessages );

if ( bResults )
{
ParseLogResults( Results, OutHistory );
}

if ( OutHistory.Num() > 0 )
{
auto AbsoluteFileName = FPaths::ConvertRelativePathToFull( InRelativeFileName );

AbsoluteFileName.RemoveFromStart( InRepositoryRoot );

if ( AbsoluteFileName[ 0 ] == '/' )
{
AbsoluteFileName.RemoveAt( 0 );
}


OutHistory[ 0 ]->Filename = AbsoluteFileName;

return OutHistory[ 0 ];
}

return nullptr;
}
} // namespace GitSourceControlUtils

#undef LOCTEXT_NAMESPACE
3 changes: 3 additions & 0 deletions Source/GitSourceControl/Private/GitSourceControlUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,7 @@ bool FetchRemote(const FString& InPathToGitBinary, const FString& InPathToReposi
bool PullOrigin(const FString& InPathToGitBinary, const FString& InPathToRepositoryRoot, const TArray<FString>& InFiles, TArray<FString>& OutFiles,
TArray<FString>& OutResults, TArray<FString>& OutErrorMessages);


TSharedPtr< class ISourceControlRevision, ESPMode::ThreadSafe > GetOriginRevisionOnBranch( const FString & InPathToGitBinary, const FString & InRepositoryRoot, const FString & InRelativeFileName, TArray< FString > & OutErrorMessages, const FString & BranchName );

}