Example #1
0
		public async Task<Commit> GetCommit (string optionalCommitHash, string optionalGitRepoDir)
		{
			if (NoMono) {
				// FIXME: return a dummy commit
				return null;
			}

			var info = NewProcessStartInfo ();
			/* Run without timing with --version */
			info.Arguments = "--version";

			Console.Out.WriteLine ("\t$> {0} {1} {2}", PrintableEnvironmentVariables (info), info.FileName, info.Arguments);

			var process = Process.Start (info);
			var version = Task.Run (() => new StreamReader (process.StandardOutput.BaseStream).ReadToEnd ()).Result;
			var versionError = Task.Run (() => new StreamReader (process.StandardError.BaseStream).ReadToEnd ()).Result;

			process.WaitForExit ();
			process.Close ();

			var line = version.Split (new char[] {'\n'}, 2) [0];
			var regex = new Regex ("^Mono JIT.*\\((.*)/([0-9a-f]+) (.*)\\)");
			var match = regex.Match (line);

			var commit = new Commit ();

			if (match.Success) {
				commit.Branch = match.Groups [1].Value;
				commit.Hash = match.Groups [2].Value;
				var date = match.Groups [3].Value;
				Console.WriteLine ("branch: " + commit.Branch + " hash: " + commit.Hash + " date: " + date);
			} else {
				if (optionalCommitHash == null) {
					Console.Error.WriteLine ("Error: cannot parse mono version and no commit given.");
					return null;
				}
			}

			if (commit.Branch == "(detached")
				commit.Branch = null;

			if (optionalCommitHash != null) {
				if (commit.Hash != null && !optionalCommitHash.StartsWith (commit.Hash)) {
					Console.Error.WriteLine ("Error: Commit hash specified on command line does not match the one reported with --version.");
					return null;
				}
				commit.Hash = optionalCommitHash;
			}

			try {
				var gitRepoDir = optionalGitRepoDir ?? Path.GetDirectoryName (Mono);
				var repo = new Repository (gitRepoDir);
				var gitHash = repo.RevParse (commit.Hash);
				if (gitHash == null) {
					Console.WriteLine ("Could not get commit " + commit.Hash + " from repository");
				} else {
					Console.WriteLine ("Got commit " + gitHash + " from repository");

					if (optionalCommitHash != null && optionalCommitHash != gitHash) {
						Console.Error.WriteLine ("Error: Commit hash specified on command line does not match the one from the git repository.");
						return null;
					}

					commit.Hash = gitHash;
					commit.MergeBaseHash = repo.MergeBase (commit.Hash, "master");
					commit.CommitDate = repo.CommitDate (commit.Hash);

					if (commit.CommitDate == null) {
						Console.Error.WriteLine ("Error: Could not get commit date from the git repository.");
						return null;
					}

					Console.WriteLine ("Commit {0} merge base {1} date {2}", commit.Hash, commit.MergeBaseHash, commit.CommitDate);
				}
			} catch (Exception) {
				Console.WriteLine ("Could not get git repository");
			}

			Octokit.Commit gitHubCommit = null;
			try {
				var gitHubClient = GitHubInterface.GitHubClient;
				gitHubCommit = await ParseInterface.RunWithRetry (() => gitHubClient.GitDatabase.Commit.Get ("mono", "mono", commit.Hash), typeof (Octokit.NotFoundException));
			} catch (Octokit.NotFoundException) {
				Console.WriteLine ("Commit " + commit.Hash + " not found on GitHub");
			}
			if (gitHubCommit == null) {
				Console.WriteLine ("Could not get commit " + commit.Hash + " from GitHub");
			} else {
				if (optionalCommitHash != null && optionalCommitHash != gitHubCommit.Sha) {
					Console.Error.WriteLine ("Error: Commit hash specified on command line does not match the one from GitHub.");
					return null;
				}

				commit.Hash = gitHubCommit.Sha;
				if (commit.CommitDate == null)
					commit.CommitDate = gitHubCommit.Committer.Date.DateTime;
				Console.WriteLine ("Got commit " + commit.Hash + " from GitHub");
			}

			return commit;
		}