Skip to content

Commit 8ce1ed2

Browse files
committed
git: better gitcfg error and trace messages
1 parent 16ee304 commit 8ce1ed2

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/shared/Microsoft.Git.CredentialManager/GitConfiguration.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ public void Enumerate(GitConfigurationEnumerationCallback cb)
126126
case 0: // OK
127127
break;
128128
default:
129-
throw new Exception(
130-
$"Failed to enumerate all Git configuration entries. Exit code '{git.ExitCode}' (level={_filterLevel})");
129+
_trace.WriteLine($"Failed to enumerate config entries (exit={git.ExitCode}, level={_filterLevel})");
130+
throw CreateGitException(git, "Failed to enumerate all Git configuration entries");
131131
}
132132

133133
IEnumerable<string> entries = data.Split('\0').Where(x => !string.IsNullOrWhiteSpace(x));
@@ -159,8 +159,7 @@ public bool TryGet(string name, out string value)
159159
value = null;
160160
return false;
161161
default: // Error
162-
_trace.WriteLine(
163-
$"Failed to read Git configuration entry '{name}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
162+
_trace.WriteLine($"Failed to read Git configuration entry '{name}'. (exit={git.ExitCode}, level={_filterLevel})");
164163
value = null;
165164
return false;
166165
}
@@ -196,8 +195,8 @@ public void Set(string name, string value)
196195
case 0: // OK
197196
break;
198197
default:
199-
throw new Exception(
200-
$"Failed to set Git configuration entry '{name}' to '{value}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
198+
_trace.WriteLine($"Failed to set config entry '{name}' to value '{value}' (exit={git.ExitCode}, level={_filterLevel})");
199+
throw CreateGitException(git, $"Failed to set Git configuration entry '{name}'");
201200
}
202201
}
203202
}
@@ -220,8 +219,8 @@ public void Add(string name, string value)
220219
case 0: // OK
221220
break;
222221
default:
223-
throw new Exception(
224-
$"Failed to add Git configuration entry '{name}' with value '{value}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
222+
_trace.WriteLine($"Failed to add config entry '{name}' with value '{value}' (exit={git.ExitCode}, level={_filterLevel})");
223+
throw CreateGitException(git, $"Failed to add Git configuration entry '{name}'");
225224
}
226225
}
227226
}
@@ -242,10 +241,11 @@ public void Unset(string name)
242241
switch (git.ExitCode)
243242
{
244243
case 0: // OK
244+
case 5: // Trying to unset a value that does not exist
245245
break;
246246
default:
247-
throw new Exception(
248-
$"Failed to unset Git configuration entry '{name}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
247+
_trace.WriteLine($"Failed to unset config entry '{name}' (exit={git.ExitCode}, level={_filterLevel})");
248+
throw CreateGitException(git, $"Failed to unset Git configuration entry '{name}'");
249249
}
250250
}
251251
}
@@ -270,8 +270,8 @@ public IEnumerable<string> GetAll(string name)
270270
case 1: // No results
271271
break;
272272
default:
273-
throw new Exception(
274-
$"Failed to get Git configuration multi-valued entry '{name}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
273+
_trace.WriteLine($"Failed to get all config entries '{name}' (exit={git.ExitCode}, level={_filterLevel})");
274+
throw CreateGitException(git, $"Failed to get all Git configuration entries '{name}'");
275275
}
276276

277277
string[] entries = data.Split('\0');
@@ -306,8 +306,8 @@ public IEnumerable<string> GetRegex(string nameRegex, string valueRegex)
306306
case 1: // No results
307307
break;
308308
default:
309-
throw new Exception(
310-
$"Failed to get Git configuration multi-valued entry '{nameRegex}' with value regex '{valueRegex}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
309+
_trace.WriteLine($"Failed to get all multivar regex '{nameRegex}' and value regex '{valueRegex}' (exit={git.ExitCode}, level={_filterLevel})");
310+
throw CreateGitException(git, $"Failed to get Git configuration multi-valued entries with name regex '{nameRegex}'");
311311
}
312312

313313
string[] entries = data.Split('\0');
@@ -346,8 +346,8 @@ public void ReplaceAll(string name, string valueRegex, string value)
346346
case 0: // OK
347347
break;
348348
default:
349-
throw new Exception(
350-
$"Failed to set Git configuration multi-valued entry '{name}' with value regex '{valueRegex}' to value '{value}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
349+
_trace.WriteLine($"Failed to replace all multivar '{name}' and value regex '{valueRegex}' with new value '{value}' (exit={git.ExitCode}, level={_filterLevel})");
350+
throw CreateGitException(git, $"Failed to replace all Git configuration multi-valued entries '{name}'");
351351
}
352352
}
353353
}
@@ -377,12 +377,29 @@ public void UnsetAll(string name, string valueRegex)
377377
case 5: // Trying to unset a value that does not exist
378378
break;
379379
default:
380-
throw new Exception(
381-
$"Failed to unset all Git configuration multi-valued entries '{name}' with value regex '{valueRegex}'. Exit code '{git.ExitCode}' (level={_filterLevel})");
380+
_trace.WriteLine($"Failed to unset all multivar '{name}' with value regex '{valueRegex}' (exit={git.ExitCode}, level={_filterLevel})");
381+
throw CreateGitException(git, $"Failed to unset all Git configuration multi-valued entries '{name}'");
382382
}
383383
}
384384
}
385385

386+
private Exception CreateGitException(Process git, string message)
387+
{
388+
var exceptionMessage = new StringBuilder();
389+
string gitMessage = git.StandardError.ReadToEnd();
390+
391+
if (!string.IsNullOrWhiteSpace(gitMessage))
392+
{
393+
exceptionMessage.AppendLine(gitMessage);
394+
}
395+
396+
exceptionMessage.AppendLine(message);
397+
exceptionMessage.AppendLine($"Exit code: '{git.ExitCode}'");
398+
exceptionMessage.AppendLine($"Configuration level: {_filterLevel}");
399+
400+
throw new Exception(exceptionMessage.ToString());
401+
}
402+
386403
private string GetLevelFilterArg()
387404
{
388405
switch (_filterLevel)

0 commit comments

Comments
 (0)