public string GetShaHashOfCurrentBranch(Context context, TemporaryFilesContext temporaryFilesContext) { Argument.IsNotNull(() => context); string commitSha = null; var repositoryDirectory = context.SolutionDirectory; if (_repositoryPreparer.IsPreparationRequired(context)) { Log.Info("No local repository is found in '{0}', creating a temporary one", repositoryDirectory); repositoryDirectory = _repositoryPreparer.Prepare(context, temporaryFilesContext); } repositoryDirectory = GitDirFinder.TreeWalkForGitDir(repositoryDirectory); using (var repository = new Repository(repositoryDirectory)) { if (string.IsNullOrEmpty(context.ShaHash)) { Log.Info("No sha hash is available on the context, retrieving latest commit of current branch"); var lastCommit = repository.Commits.First(); commitSha = lastCommit.Sha; } else { Log.Info("Checking if commit with sha hash '{0}' exists on the repository", context.ShaHash); var commit = repository.Commits.FirstOrDefault(c => string.Equals(c.Sha, context.ShaHash, StringComparison.OrdinalIgnoreCase)); if (commit != null) { commitSha = commit.Sha; } } } if (commitSha == null) { throw Log.ErrorAndCreateException <GitLinkException>("Cannot find commit '{0}' in repo.", context.ShaHash); } return(commitSha); }
public string GetShaHashOfCurrentBranch(Context context) { Argument.IsNotNull(() => context); string commitSha = null; var deleteTempRepository = false; var repositoryDirectory = context.SolutionDirectory; if (_repositoryPreparer.IsPreparationRequired(context)) { Log.Info("No local repository is found in '{0}', creating a temporary one", repositoryDirectory); repositoryDirectory = _repositoryPreparer.Prepare(context); deleteTempRepository = true; } using (var repository = new Repository(repositoryDirectory)) { if (string.IsNullOrEmpty(context.ShaHash)) { Log.Info("No sha hash is available on the context, retrieving latest commit of current branch"); var lastCommit = repository.Commits.First(); commitSha = lastCommit.Sha; } else { Log.Info("Checking if commit with sha hash '{0}' exists on the repostory", context.ShaHash); var commit = repository.Commits.FirstOrDefault(c => string.Equals(c.Sha, context.ShaHash, StringComparison.OrdinalIgnoreCase)); if (commit != null) { commitSha = commit.Sha; } } } if (deleteTempRepository) { Log.Debug("Deleting temporary directory '{0}'", repositoryDirectory); try { // Always sleep 1 second to give IO a chance to release ThreadHelper.Sleep(1000); Directory.Delete(repositoryDirectory, true); } catch (Exception ex) { Log.Warning(ex, "Failed to delete temporary directory"); } } if (commitSha == null) { Log.ErrorAndThrowException <GitLinkException>("Cannot find commit '{0}' in repo.", context.ShaHash); } return(commitSha); }