/// <summary> /// Gets the git information. /// </summary> /// <param name="assemblyPath">The assembly path.</param> /// <returns></returns> private static GitInformation GetGitInformation(string assemblyPath) { var directories = GetAncestors(assemblyPath); using (var repository = GitRepository.TryLoad(directories.ToArray())) { if (repository == null) return null; var gitInformation = new GitInformation(); var currentBranch = repository.Repository.Head; gitInformation.BranchName = currentBranch.Name; gitInformation.BranchRemoteName = currentBranch.Remote?.Name; var latestCommit = currentBranch.Commits.First(); gitInformation.CommitID = latestCommit.Id.Sha; gitInformation.CommitShortID = latestCommit.Id.Sha.Substring(0, 8); gitInformation.CommitMessage = latestCommit.Message.Trim(); gitInformation.CommitAuthor = latestCommit.Author.ToString(); gitInformation.CommitTime = latestCommit.Author.When.LocalDateTime; gitInformation.CommitTimeIso = gitInformation.CommitTime.ToString("o"); var repositoryStatus = repository.Repository.RetrieveStatus(); gitInformation.IsDirty = repositoryStatus.IsDirty; gitInformation.IsDirtyLiteral = repositoryStatus.IsDirty ? "dirty" : ""; var tags = repository.Repository.Tags.Where(t => t.Target.Id == latestCommit.Id).OrderBy(t => t.Name).ToArray(); gitInformation.CommitTags = string.Join(" ", tags.Select(t => t.Name)); FillBuildInformation(gitInformation); return gitInformation; } }
/// <summary> /// Gets the git information. /// </summary> /// <param name="assemblyPath">The assembly path.</param> /// <returns></returns> private static GitInformation GetGitInformation(string assemblyPath) { var directories = GetAncestors(assemblyPath); Exception rethrow = null; // for some unknown reason, it fails sometimes on CI for (int retry = 0; retry < 3; retry++) { try { using (var repository = GitRepository.TryLoad(directories.ToArray())) { if (repository == null) { return(null); } var gitInformation = new GitInformation(); var currentBranch = repository.Repository.Head; gitInformation.BranchName = currentBranch.Name; gitInformation.BranchRemoteName = currentBranch.Remote?.Name; var latestCommit = currentBranch.Commits.First(); gitInformation.CommitID = latestCommit.Id.Sha; gitInformation.CommitShortID = latestCommit.Id.Sha.Substring(0, 8); gitInformation.CommitMessage = latestCommit.Message.Trim(); gitInformation.CommitAuthor = latestCommit.Author.ToString(); gitInformation.CommitTime = latestCommit.Author.When.LocalDateTime; gitInformation.CommitTimeIso = gitInformation.CommitTime.ToString("o"); var repositoryStatus = repository.Repository.RetrieveStatus(); gitInformation.IsDirty = repositoryStatus.IsDirty; gitInformation.IsDirtyLiteral = repositoryStatus.IsDirty ? "dirty" : ""; var tags = repository.Repository.Tags.Where(t => t.Target.Id == latestCommit.Id).OrderBy(t => t.Name).ToArray(); gitInformation.CommitTags = string.Join(" ", tags.Select(t => t.Name)); FillBuildInformation(gitInformation); return(gitInformation); } } catch (LibGit2SharpException e) { rethrow = e; Thread.Sleep(200); } } if (rethrow != null) { throw rethrow; // funny dude } throw new Exception("If you read this message, the programmer did something wrong"); }