Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. public static string GetBranchFromAssemblyInfo(bool isMaster = true)
  2.         {
  3.             var assembly = Assembly.GetExecutingAssembly();
  4.             var location = assembly.Location;
  5.             if (location == null)
  6.                 return string.Empty;
  7.  
  8.             var productVersion = FileVersionInfo.GetVersionInfo(location).ProductVersion;
  9.  
  10.             if (string.Equals(productVersion, _manualVersionString))
  11.                 try
  12.                 {
  13.                     var solutionPath = string.Empty;
  14.                     var path = AppDomain.CurrentDomain.BaseDirectory;
  15.                     for (var i = 0; i < 6; i++)
  16.                     {
  17.                         if (Directory.Exists(path + ".git"))
  18.                         {
  19.                             solutionPath = path;
  20.                             break;
  21.                         }
  22.                         path += @"..\";
  23.                     }
  24.  
  25.                     using (var repository = new Repository(solutionPath))
  26.                     {
  27.                         var branch = repository.Head.ToString();
  28.                         return branch.Replace("refs/heads/", string.Empty);
  29.                     }
  30.                 }
  31.                 catch
  32.                 {
  33.                     return "default branch";
  34.                 }
  35.  
  36.             var datePattern = @"_[\d]{8}_[\d]{4}";
  37.             var shortBranch = productVersion.Replace("refs/heads/", string.Empty);
  38.             shortBranch = Regex.Replace(shortBranch, datePattern, string.Empty);
  39.  
  40.             return shortBranch;
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement