Skip to content

Commit

Permalink
Improve error reporting and recovery (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Sep 30, 2018
1 parent ec1c395 commit c092238
Show file tree
Hide file tree
Showing 52 changed files with 428 additions and 180 deletions.
3 changes: 3 additions & 0 deletions src/Common/CommonResources.resx
Expand Up @@ -138,4 +138,7 @@
<data name="AtLeastOneRepositoryHostIsRequired" xml:space="preserve">
<value>{0} item group is empty. At least one {1} repository host is required in order to generate SourceLink.</value>
</data>
<data name="UnableToDetermineRepositoryUrl" xml:space="preserve">
<value>Unable to determine repository url, the source code won't be available via source link.</value>
</data>
</root>
7 changes: 7 additions & 0 deletions src/Common/GetSourceLinkUrlGitTask.cs
Expand Up @@ -71,6 +71,13 @@ private void ExecuteImpl()
}

var gitUrl = SourceRoot.GetMetadata(Names.SourceRoot.RepositoryUrl);
if (string.IsNullOrEmpty(gitUrl))
{
SourceLinkUrl = NotApplicableValue;
Log.LogWarning(CommonResources.UnableToDetermineRepositoryUrl);
return;
}

if (!Uri.TryCreate(gitUrl, UriKind.Absolute, out var gitUri))
{
Log.LogError(CommonResources.ValueOfWithIdentityIsInvalid, Names.SourceRoot.RepositoryUrlFullName, SourceRoot.ItemSpec, gitUrl);
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.cs.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Položka {0} ve skupině položek {1} musí uvádět metadata {2}.</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">Hodnota {0} s identitou {1} je neplatná: {2}</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.de.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Das Element "{0}" der Elementgruppe "{1}" muss die Metadaten "{2}" angeben.</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">Der Wert von "{0}" mit der Identität "{1}" ist ungültig: "{2}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.es.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">El elemento "{0}" del grupo de elementos "{1}" debe especificar los metadatos "{2}"</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">El valor de {0} con la identidad "{1}" no es válido: "{2}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.fr.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">L'élément '{0}' du groupe d'éléments '{1}' doit spécifier les métadonnées '{2}'</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">La valeur de {0} avec l'identité '{1}' n’est pas valide : '{2}'</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.it.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">L'elemento '{0}' del gruppo di elementi '{1}' deve specificare i metadati '{2}'</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">Il valore di {0} con identità '{1}' non è valido: '{2}'</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.ja.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">項目グループ '{1}' の項目 '{0}' には、メタデータ '{2}' を指定する必要があります</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">ID '{1}' の {0} の値が無効です: '{2}'</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.ko.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">항목 그룹 '{1}'의 '{0}' 항목은 '{2}' 메타데이터를 지정해야 합니다.</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">ID가 '{1}'인 {0}의 값은 잘못되었습니다. '{2}'</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.pl.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Element „{0}” grupy elementów „{1}” musi określać metadane „{2}”.</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">Wartość {0} z tożsamością „{1}” jest nieprawidłowa: „{2}”</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.pt-BR.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Item '{0}' do grupo de item '{1}' deve especificar metadados '{2}'</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">O valor de {0} com identidade '{1}' é inválido: '{2}'</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.ru.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Необходимо указать метаданные '{2}' для элемента '{0}' в группе элементов '{1}'</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">Значение {0} с идентификатором '{1}' является недопустимым: '{2}'</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.tr.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">'{1}' öğe grubunun '{0}' öğesi, '{2}' meta verilerini belirtmelidir</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">{1} kimliğine sahip {0} değeri geçerli değil: '{2}'</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.zh-Hans.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">项组 "{1}" 的项 "{0}" 必须指定元数据 "{2}"</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">具有标识 "{1}" 的 {0} 的值无效: "{2}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/xlf/CommonResources.zh-Hant.xlf
Expand Up @@ -12,6 +12,11 @@
<target state="translated">項目群組 '{1}' 的項目 '{0}' 必須指定中繼資料 '{2}'</target>
<note />
</trans-unit>
<trans-unit id="UnableToDetermineRepositoryUrl">
<source>Unable to determine repository url, the source code won't be available via source link.</source>
<target state="new">Unable to determine repository url, the source code won't be available via source link.</target>
<note />
</trans-unit>
<trans-unit id="ValueOfWithIdentityIsInvalid">
<source>The value of {0} with identity '{1}' is invalid: '{2}'</source>
<target state="translated">識別碼為 '{1}' 的 {0} 值無效: '{2}'</target>
Expand Down
20 changes: 16 additions & 4 deletions src/Microsoft.Build.Tasks.Git.Operations/GitOperations.cs
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using LibGit2Sharp;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks.SourceControl;
Expand All @@ -16,6 +17,7 @@ internal static class GitOperations
{
private const string SourceControlName = "git";

[MethodImpl(MethodImplOptions.NoInlining)]
public static string LocateRepository(string directory)
{
// Repository.Discover returns the path to .git directory for repositories with a working directory.
Expand All @@ -24,16 +26,23 @@ public static string LocateRepository(string directory)
return Repository.Discover(directory);
}

public static string GetRepositoryUrl(IRepository repository, string remoteName = null)
public static string GetRepositoryUrl(IRepository repository, Action<string, object[]> logWarning = null, string remoteName = null)
{
var remotes = repository.Network.Remotes;
var remote = string.IsNullOrEmpty(remoteName) ? (remotes["origin"] ?? remotes.FirstOrDefault()) : remotes[remoteName];
if (remote == null)
{
logWarning?.Invoke(Resources.RepositoryHasNoRemote, Array.Empty<string>());
return null;
}

return NormalizeUrl(remote.Url, repository.Info.WorkingDirectory);
var url = NormalizeUrl(remote.Url, repository.Info.WorkingDirectory);
if (url == null)
{
logWarning?.Invoke(Resources.InvalidRepositoryRemoteUrl, new[] { remote.Name, remote.Url });
}

return url;
}

internal static string NormalizeUrl(string url, string root)
Expand Down Expand Up @@ -140,15 +149,18 @@ public static ITaskItem[] GetSourceRoots(IRepository repository, Action<string,
var revisionId = GetRevisionId(repository);
if (revisionId != null)
{
// Don't report a warning since it has already been reported by GetRepositoryUrl task.
string repositoryUrl = GetRepositoryUrl(repository);

var item = new TaskItem(repoRoot);
item.SetMetadata(Names.SourceRoot.SourceControl, SourceControlName);
item.SetMetadata(Names.SourceRoot.ScmRepositoryUrl, GetRepositoryUrl(repository));
item.SetMetadata(Names.SourceRoot.ScmRepositoryUrl, repositoryUrl);
item.SetMetadata(Names.SourceRoot.RevisionId, revisionId);
result.Add(item);
}
else
{
logWarning(Resources.RepositoryWithoutCommit_SourceLink, Array.Empty<object>());
logWarning(Resources.RepositoryHasNoCommit, Array.Empty<object>());
}

if (SubmodulesSupported(repository, fileExists))
Expand Down
31 changes: 26 additions & 5 deletions src/Microsoft.Build.Tasks.Git.Operations/RepositoryTasks.cs
Expand Up @@ -13,6 +13,12 @@ private static bool Execute<T>(T task, Action<Repository, T> action)
{
var log = task.Log;

// Unable to determine repository root, warning has already been reported.
if (string.IsNullOrEmpty(task.Root))
{
return true;
}

Repository repo;
try
{
Expand All @@ -26,8 +32,8 @@ private static bool Execute<T>(T task, Action<Repository, T> action)

if (repo.Info.IsBare)
{
log.LogError(Resources.BareRepositoriesNotSupported, task.Root);
return false;
log.LogWarning(Resources.BareRepositoriesNotSupported, task.Root);
return true;
}

using (repo)
Expand All @@ -47,11 +53,26 @@ private static bool Execute<T>(T task, Action<Repository, T> action)

public static bool LocateRepository(LocateRepository task)
{
task.Id = GitOperations.LocateRepository(task.Directory);
try
{
task.Id = GitOperations.LocateRepository(task.Directory);
}
catch (Exception e)
{
#if NET461
foreach (var message in TaskImplementation.GetLog())
{
task.Log.LogMessage(message);
}
#endif
task.Log.LogWarningFromException(e, showStackTrace: true);

return true;
}

if (task.Id == null)
{
task.Log.LogError(Resources.UnableToLocateRepository, task.Directory);
task.Log.LogWarning(Resources.UnableToLocateRepository, task.Directory);
}

return !task.Log.HasLoggedErrors;
Expand All @@ -60,7 +81,7 @@ public static bool LocateRepository(LocateRepository task)
public static bool GetRepositoryUrl(GetRepositoryUrl task) =>
Execute(task, (repo, t) =>
{
t.Url = GitOperations.GetRepositoryUrl(repo, t.RemoteName);
t.Url = GitOperations.GetRepositoryUrl(repo, t.Log.LogWarning, t.RemoteName);
});

public static bool GetSourceRevisionId(GetSourceRevisionId task) =>
Expand Down

0 comments on commit c092238

Please sign in to comment.