Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 3333b53

Browse files
author
Andreia Gaita
authored
Merge pull request #978 from github-for-unity/unity-yaml-merge-config
Fixing UnityYamlMerge to use merge driver
2 parents 28a44fa + 06f0dbe commit 3333b53

File tree

6 files changed

+89
-10
lines changed

6 files changed

+89
-10
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void SetupGit(GitInstaller.GitInstallationState state)
190190
{
191191
if (Environment.RepositoryPath.IsInitialized)
192192
{
193-
ConfigureMergeSettings();
193+
UpdateMergeSettings();
194194

195195
GitClient.LfsInstall()
196196
.Catch(e =>
@@ -280,25 +280,58 @@ public void InitializeRepository()
280280
thread.Start();
281281
}
282282

283-
private void ConfigureMergeSettings()
283+
private void ConfigureMergeSettings(string keyName = null)
284284
{
285285
var unityYamlMergeExec =
286286
Environment.UnityApplicationContents.Combine("Tools", "UnityYAMLMerge" + Environment.ExecutableExtension);
287-
var yamlMergeCommand = Environment.IsWindows
288-
? $@"'{unityYamlMergeExec}' merge -p ""$BASE"" ""$REMOTE"" ""$LOCAL"" ""$MERGED"""
289-
: $@"'{unityYamlMergeExec}' merge -p '$BASE' '$REMOTE' '$LOCAL' '$MERGED'";
290287

291-
GitClient.SetConfig("merge.unityyamlmerge.cmd", yamlMergeCommand, GitConfigSource.Local).Catch(e => {
292-
Logger.Error(e, "Error setting merge.unityyamlmerge.cmd");
288+
var yamlMergeCommand = $"'{unityYamlMergeExec}' merge -h -p --force %O %B %A %A";
289+
290+
keyName = keyName ?? "unityyamlmerge";
291+
292+
GitClient.SetConfig($"merge.{keyName}.name", "Unity SmartMerge (UnityYamlMerge)", GitConfigSource.Local).Catch(e => {
293+
Logger.Error(e, "Error setting merge." + keyName + ".name");
294+
return true;
295+
}).RunSynchronously();
296+
297+
GitClient.SetConfig($"merge.{keyName}.driver", yamlMergeCommand, GitConfigSource.Local).Catch(e => {
298+
Logger.Error(e, "Error setting merge." + keyName + ".driver");
293299
return true;
294300
}).RunSynchronously();
295301

296-
GitClient.SetConfig("merge.unityyamlmerge.trustExitCode", "false", GitConfigSource.Local).Catch(e => {
297-
Logger.Error(e, "Error setting merge.unityyamlmerge.trustExitCode");
302+
GitClient.SetConfig($"merge.{keyName}.recursive", "binary", GitConfigSource.Local).Catch(e => {
303+
Logger.Error(e, "Error setting merge." + keyName + ".recursive");
298304
return true;
299305
}).RunSynchronously();
300306
}
301307

308+
private void UpdateMergeSettings()
309+
{
310+
var gitAttributesPath = Environment.RepositoryPath.Combine(".gitattributes");
311+
if (gitAttributesPath.FileExists())
312+
{
313+
var readAllText = gitAttributesPath.ReadAllText();
314+
var containsLegacyUnityYamlMergeError = readAllText.Contains("unityamlmerge");
315+
316+
if (containsLegacyUnityYamlMergeError)
317+
{
318+
ConfigureMergeSettings("unityamlmerge");
319+
}
320+
}
321+
322+
GitClient.UnSetConfig("merge.unityyamlmerge.cmd", GitConfigSource.Local).Catch(e => {
323+
Logger.Error(e, "Error removing merge.unityyamlmerge.cmd");
324+
return true;
325+
}).RunSynchronously();
326+
327+
GitClient.UnSetConfig("merge.unityyamlmerge.trustExitCode", GitConfigSource.Local).Catch(e => {
328+
Logger.Error(e, "Error removing merge.unityyamlmerge.trustExitCode");
329+
return true;
330+
}).RunSynchronously();
331+
332+
ConfigureMergeSettings();
333+
}
334+
302335
public void RestartRepository()
303336
{
304337
if (!Environment.RepositoryPath.IsInitialized)

src/GitHub.Api/Git/GitClient.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public interface IGitClient
6060
/// <returns>String output of git command</returns>
6161
ITask<string> SetConfig(string key, string value, GitConfigSource configSource, IOutputProcessor<string> processor = null);
6262

63+
/// <summary>
64+
/// Executes `git config --unset` to remove a configuration value.
65+
/// </summary>
66+
/// <param name="key">The configuration key to remove</param>
67+
/// <param name="configSource">The config source (unspecified, local,user,global) to use</param>
68+
/// <param name="processor">A custom output processor instance</param>
69+
/// <returns>String output of git command</returns>
70+
ITask<string> UnSetConfig(string key, GitConfigSource configSource, IOutputProcessor<string> processor = null);
71+
6372
/// <summary>
6473
/// Executes two `git config get` commands to get the git user and email.
6574
/// </summary>
@@ -367,6 +376,13 @@ public ITask<string> SetConfig(string key, string value, GitConfigSource configS
367376
.Configure(processManager);
368377
}
369378

379+
///<inheritdoc/>
380+
public ITask<string> UnSetConfig(string key, GitConfigSource configSource, IOutputProcessor<string> processor = null)
381+
{
382+
return new GitConfigUnSetTask(key, configSource, cancellationToken, processor)
383+
.Configure(processManager);
384+
}
385+
370386
///<inheritdoc/>
371387
public ITask<GitUser> GetConfigUserAndEmail()
372388
{

src/GitHub.Api/Git/Tasks/GitConfigSetTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ public GitConfigSetTask(string key, string value, GitConfigSource configSource,
2525
public override TaskAffinity Affinity { get { return TaskAffinity.Exclusive; } }
2626
public override string Message { get; set; } = "Writing configuration...";
2727
}
28-
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Threading;
3+
4+
namespace GitHub.Unity
5+
{
6+
class GitConfigUnSetTask : ProcessTask<string>
7+
{
8+
private readonly string arguments;
9+
10+
public GitConfigUnSetTask(string key, GitConfigSource configSource,
11+
CancellationToken token, IOutputProcessor<string> processor = null)
12+
: base(token, processor ?? new SimpleOutputProcessor())
13+
{
14+
var source = "";
15+
source +=
16+
configSource == GitConfigSource.NonSpecified ? "--unset" :
17+
configSource == GitConfigSource.Local ? "--local --unset" :
18+
configSource == GitConfigSource.User ? "--global --unset" :
19+
"--system --unset";
20+
arguments = String.Format("config {0} {1}", source, key);
21+
Name = String.Format("config {0} {1}", source, key);
22+
}
23+
24+
public override string ProcessArguments { get { return arguments; } }
25+
public override TaskAffinity Affinity { get { return TaskAffinity.Exclusive; } }
26+
public override string Message { get; set; } = "Writing configuration...";
27+
}
28+
}

src/GitHub.Api/GitHub.Api.45.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="Git\Tasks\GitCheckoutTask.cs" />
8383
<Compile Include="Git\GitAheadBehindStatus.cs" />
8484
<Compile Include="Git\Tasks\GitAheadBehindStatusTask.cs" />
85+
<Compile Include="Git\Tasks\GitConfigUnSetTask.cs" />
8586
<Compile Include="Git\Tasks\GitCountObjectsTask.cs" />
8687
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
8788
<Compile Include="Git\Tasks\GitVersionTask.cs" />

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="Git\Tasks\GitCheckoutTask.cs" />
9494
<Compile Include="Git\GitAheadBehindStatus.cs" />
9595
<Compile Include="Git\Tasks\GitAheadBehindStatusTask.cs" />
96+
<Compile Include="Git\Tasks\GitConfigUnSetTask.cs" />
9697
<Compile Include="Git\Tasks\GitCountObjectsTask.cs" />
9798
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
9899
<Compile Include="Git\Tasks\GitVersionTask.cs" />

0 commit comments

Comments
 (0)