Download SSL Certificates Sort Subtasks of Markdown files Test BioRad EAF CopyDirectories json to Markdown Sort Day 2024 Q2 GitRemoteRemove Handle directoryInfo.LinkTarget better Remove StartAt Handle directoryInfo.LinkTarget
79 lines
3.5 KiB
C#
79 lines
3.5 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace File_Folder_Helper.Day;
|
|
|
|
internal static partial class Helper20240711
|
|
{
|
|
|
|
internal static void GitRemoteRemove(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string line;
|
|
string[] lines;
|
|
bool branchCheck;
|
|
bool remoteCheck;
|
|
string? directory;
|
|
string? parentDirectory;
|
|
string parentDirectoryName;
|
|
string branchName = args[8];
|
|
string searchPattern = args[2];
|
|
string remoteToAddUrl = args[6];
|
|
string remoteToRemove = args[3];
|
|
string remoteToAddName = args[5];
|
|
ReadOnlyCollection<string> messages;
|
|
string remoteToRemoveFilter = args[4];
|
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
|
string lastRemoteSegment = remoteToRemove.Split('/')[^1];
|
|
string extension = args[7].Length > 2 ? args[7] : string.Empty;
|
|
string[] files = Directory.EnumerateFiles(sourceDirectory, searchPattern, new EnumerationOptions() { IgnoreInaccessible = true, RecurseSubdirectories = true, AttributesToSkip = FileAttributes.None }).ToArray();
|
|
logger.LogInformation("Found {files} file(s)", files.Length);
|
|
foreach (string file in files)
|
|
{
|
|
branchCheck = false;
|
|
remoteCheck = false;
|
|
lines = File.ReadAllLines(file);
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
line = lines[i];
|
|
if (!line.Contains(remoteToRemove))
|
|
continue;
|
|
if (!lines[i - 1].Contains(remoteToRemoveFilter))
|
|
continue;
|
|
remoteCheck = true;
|
|
break;
|
|
}
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
line = lines[i];
|
|
if (!line.Contains(branchName))
|
|
continue;
|
|
branchCheck = true;
|
|
break;
|
|
}
|
|
if (!remoteCheck)
|
|
continue;
|
|
directory = Path.GetDirectoryName(file);
|
|
if (directory is null)
|
|
continue;
|
|
parentDirectory = Path.GetDirectoryName(directory);
|
|
if (parentDirectory is null)
|
|
continue;
|
|
parentDirectoryName = Path.GetFileName(parentDirectory).ToLower();
|
|
messages = Helpers.HelperGit.RemoteRemove(parentDirectory, lastRemoteSegment, CancellationToken.None);
|
|
foreach (string message in messages)
|
|
logger.LogInformation("{function} => {parentDirectoryName}: [{message}]", nameof(Helpers.HelperGit.RemoteRemove), parentDirectoryName, message);
|
|
messages = Helpers.HelperGit.RemoteAdd(parentDirectory, remoteToAddName, $"{remoteToAddUrl}{parentDirectoryName}{extension}", CancellationToken.None);
|
|
foreach (string message in messages)
|
|
logger.LogInformation("{function} => {parentDirectoryName}: [{message}]", nameof(Helpers.HelperGit.RemoteAdd), parentDirectoryName, message);
|
|
if (!branchCheck)
|
|
continue;
|
|
try
|
|
{ messages = Helpers.HelperGit.PushBranch(parentDirectory, remoteToAddName, branchName, CancellationToken.None); }
|
|
catch (Exception ex)
|
|
{ messages = new([ex.Message]); }
|
|
foreach (string message in messages)
|
|
logger.LogInformation("{function} => {parentDirectoryName}: [{message}]", nameof(Helpers.HelperGit.PushBranch), parentDirectoryName, message);
|
|
}
|
|
}
|
|
|
|
} |