Files
UnrealEngineUWP/Engine/Extras/P4VUtils/Commands/SnapShotCommand.cs
andrew firth cabe530066 [P4VUtils] - small feedback changes, addition of refresh and shortcut for all custom tools.
- snapshot now more descriptive
- backout now more descriptive
- preflightAndSubmit reverted to old behavior, new command to MoveWriteableFilesthenPreflightAndSubmit added
#review @Ben.Marsh

[CL 16596338 by andrew firth in ue5-main branch]
2021-06-08 19:06:41 -04:00

102 lines
3.4 KiB
C#

// Copyright Epic Games, Inc. All Rights Reserved.
using EpicGames.Core;
using EpicGames.Perforce;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace P4VUtils.Commands
{
class SnapshotCommand : Command
{
public override string Description => "Creates a shelved copy of the CL";
public override CustomToolInfo CustomTool => new CustomToolInfo("Create a Snapshot shelf of this CL", "%p");
public override async Task<int> Execute(string[] Args, IReadOnlyDictionary<string, string> ConfigValues, ILogger Logger)
{
int Change;
if (Args.Length < 2)
{
Logger.LogError("Missing changelist number");
return 1;
}
else if (!int.TryParse(Args[1], out Change))
{
Logger.LogError("'{Argument}' is not a numbered changelist", Args[1]);
return 1;
}
string? ClientName = Environment.GetEnvironmentVariable("P4CLIENT");
PerforceConnection Perforce = new PerforceConnection(null, null, ClientName, Logger);
ClientRecord Client = await Perforce.GetClientAsync(ClientName, CancellationToken.None);
if(Client.Stream == null)
{
Logger.LogError("Not being run from a stream client");
return 1;
}
// Create the copy of the CL IFF it has files opened
List<FStatRecord> OpenedRecords = await Perforce.GetOpenFilesAsync(OpenedOptions.None, Change, null, null, -1, null, CancellationToken.None);
if (OpenedRecords.Count > 0)
{
InfoRecord Info = await Perforce.GetInfoAsync(InfoOptions.None, CancellationToken.None);
DescribeRecord ExistingChangeRecord = await Perforce.DescribeAsync(Change, CancellationToken.None);
var DateNow = DateTime.Now.ToString();
ChangeRecord NewChangeRecord = new ChangeRecord();
NewChangeRecord.User = Info.UserName;
NewChangeRecord.Client = Info.ClientName;
NewChangeRecord.Description = $"{ExistingChangeRecord.Description.TrimEnd()}\n[snapshot CL{Change} - {DateNow}]";
NewChangeRecord = await Perforce.CreateChangeAsync(NewChangeRecord, CancellationToken.None);
Logger.LogInformation("Created pending changelist {0}", NewChangeRecord.Number);
// Move the files to the new CL
foreach (FStatRecord OpenedRecord in OpenedRecords)
{
if (OpenedRecord.ClientFile != null)
{
await Perforce.ReopenAsync(NewChangeRecord.Number, OpenedRecord.Type, OpenedRecord.ClientFile!, CancellationToken.None);
Logger.LogInformation("moving opened {file} to CL {CL}", OpenedRecord.ClientFile.ToString(), NewChangeRecord.Number);
}
}
// Shelve that CL
await Perforce.ShelveAsync(NewChangeRecord.Number, ShelveOptions.Overwrite, new[] { "//..." }, CancellationToken.None);
// Move the files BACK to the old CL
foreach (FStatRecord OpenedRecord in OpenedRecords)
{
if (OpenedRecord.ClientFile != null)
{
await Perforce.ReopenAsync(Change, OpenedRecord.Type, OpenedRecord.ClientFile!, CancellationToken.None);
Logger.LogInformation("moving opened {file} to CL {CL}", OpenedRecord.ClientFile.ToString(), Change);
}
}
}
else
{
Logger.LogInformation("No files opened, snapshot not created");
}
return 0;
}
}
}