public IDictionary <string, string> GetResourceInfo(string projectDir) { IDictionary <string, string> dict = new Dictionary <string, string>(); string identifier = SoftwareCoUtil.RunCommand("git config remote.origin.url", projectDir); if (identifier != null && !identifier.Equals("")) { dict.Add("identifier", identifier); // only get these since identifier is available string email = SoftwareCoUtil.RunCommand("git config user.email", projectDir); if (email != null && !email.Equals("")) { dict.Add("email", email); } string branch = SoftwareCoUtil.RunCommand("git symbolic-ref --short HEAD", projectDir); if (branch != null && !branch.Equals("")) { dict.Add("branch", branch); } string tag = SoftwareCoUtil.RunCommand("git describe --all", projectDir); if (tag != null && !tag.Equals("")) { dict.Add("tag", tag); } } return(dict); }
public static string GetUsersEmail(string projectDir) { if (!SoftwareCoUtil.IsGitProject(projectDir)) { return(""); } return(SoftwareCoUtil.RunCommand("git config user.email", projectDir)); }
public static List <string> GetCommandResultList(string cmd, string dir) { List <string> resultList = new List <string>(); string commandResult = SoftwareCoUtil.RunCommand(cmd, dir); if (commandResult != null && !commandResult.Equals("")) { string[] lines = commandResult.Split( new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); resultList = new List <string>(lines); resultList = new List <string>(lines); } return(resultList); }
public static string GetRepoUrlLink(string projectDir) { if (!SoftwareCoUtil.IsGitProject(projectDir)) { return(""); } string repoUrl = SoftwareCoUtil.RunCommand("git config --get remote.origin.url", projectDir); if (repoUrl != null) { repoUrl = repoUrl.Substring(0, repoUrl.LastIndexOf(".git")); } return(repoUrl); }
public static string getHostname() { string hostname = SoftwareCoUtil.RunCommand("hostname", null); return(hostname); }
public async void GetHistoricalCommitsAsync(string projectDir) { try { if (!SoftwareCoUtil.IsGitProject(projectDir)) { return; } RepoResourceInfo info = GitUtilManager.GetResourceInfo(projectDir, false); if (info != null && info.identifier != null) { string identifier = info.identifier; if (identifier != null && !identifier.Equals("")) { string tag = info.tag; string branch = info.branch; string email = info.email; RepoCommit latestCommit = null; latestCommit = await this.GetLatestCommitAsync(projectDir); string sinceOption = ""; if (latestCommit != null) { sinceOption = " --since=" + latestCommit.timestamp; } else { sinceOption = " --max-count=100"; } string cmd = "git log --stat --pretty=COMMIT:%H,%ct,%cI,%s --author=" + email + "" + sinceOption; string gitCommitData = SoftwareCoUtil.RunCommand(cmd, projectDir); if (gitCommitData != null && !gitCommitData.Equals("")) { string[] lines = gitCommitData.Split( new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); RepoCommit currentRepoCommit = null; List <RepoCommit> repoCommits = new List <RepoCommit>(); if (lines != null && lines.Length > 0) { for (int i = 0; i < lines.Length; i++) { string line = lines[i].Trim(); if (line.Length > 0) { bool hasPipe = line.IndexOf("|") != -1 ? true : false; bool isBin = line.ToLower().IndexOf("bin") != -1 ? true : false; if (line.IndexOf("COMMIT:") == 0) { line = line.Substring("COMMIT:".Length); if (currentRepoCommit != null) { repoCommits.Add(currentRepoCommit); } string[] commitInfos = line.Split(','); if (commitInfos != null && commitInfos.Length > 0) { string commitId = commitInfos[0].Trim(); // go to the next line if we've already processed this commitId if (latestCommit != null && commitId.Equals(latestCommit.commitId)) { currentRepoCommit = null; continue; } // get the other attributes now long timestamp = Convert.ToInt64(commitInfos[1].Trim()); string date = commitInfos[2].Trim(); string message = commitInfos[3].Trim(); currentRepoCommit = new RepoCommit(commitId, message, timestamp); currentRepoCommit.date = date; RepoCommitChanges changesObj = new RepoCommitChanges(0, 0); currentRepoCommit.changes.Add("__sftwTotal__", changesObj); } } else if (currentRepoCommit != null && hasPipe && !isBin) { // get the file and changes // i.e. somefile.cs | 20 +++++++++--------- line = string.Join(" ", line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); string[] lineInfos = line.Split('|'); if (lineInfos != null && lineInfos.Length > 1) { string file = lineInfos[0].Trim(); string[] metricInfos = lineInfos[1].Trim().Split(' '); if (metricInfos != null && metricInfos.Length > 1) { string addAndDeletes = metricInfos[1].Trim(); int len = addAndDeletes.Length; int lastPlusIdx = addAndDeletes.LastIndexOf('+'); int insertions = 0; int deletions = 0; if (lastPlusIdx != -1) { insertions = lastPlusIdx + 1; deletions = len - insertions; } else if (len > 0) { // all deletions deletions = len; } if (!currentRepoCommit.changes.ContainsKey(file)) { RepoCommitChanges changesObj = new RepoCommitChanges(insertions, deletions); currentRepoCommit.changes.Add(file, changesObj); } else { RepoCommitChanges fileCommitChanges; currentRepoCommit.changes.TryGetValue(file, out fileCommitChanges); if (fileCommitChanges != null) { fileCommitChanges.deletions += deletions; fileCommitChanges.insertions += insertions; } } RepoCommitChanges totalRepoCommit; currentRepoCommit.changes.TryGetValue("__sftwTotal__", out totalRepoCommit); if (totalRepoCommit != null) { totalRepoCommit.deletions += deletions; totalRepoCommit.insertions += insertions; } } } } } } } if (currentRepoCommit != null) { repoCommits.Add(currentRepoCommit); } if (repoCommits != null && repoCommits.Count > 0) { // batch 10 at a time int batch_size = 10; List <RepoCommit> batch = new List <RepoCommit>(); for (int i = 0; i < repoCommits.Count; i++) { batch.Add(repoCommits[i]); if (i > 0 && i % batch_size == 0) { // send this batch. RepoCommitData commitData = new RepoCommitData(identifier, tag, branch, batch); string jsonContent = commitData.GetAsJson(); // SimpleJson.SerializeObject(commitData); // send the members HttpResponseMessage response = await SoftwareHttpManager.SendRequestAsync( HttpMethod.Post, "/commits", jsonContent); if (SoftwareHttpManager.IsOk(response)) { Logger.Info(response.ToString()); } else { Logger.Error(response.ToString()); } } } if (batch.Count > 0) { RepoCommitData commitData = new RepoCommitData(identifier, tag, branch, batch); string jsonContent = commitData.GetAsJson(); // SimpleJson.SerializeObject(commitData); // send the members HttpResponseMessage response = await SoftwareHttpManager.SendRequestAsync( HttpMethod.Post, "/commits", jsonContent); if (SoftwareHttpManager.IsOk(response)) { Logger.Info(response.ToString()); } else if (response != null) { Logger.Error("Unable to complete commit request, status: " + response.StatusCode); } } } } } } } catch (Exception ex) { Logger.Error("GetHistoricalCommitsAsync ,error: " + ex.Message, ex); } }
public static RepoResourceInfo GetResourceInfo(string projectDir, bool includeMembers) { if (!SoftwareCoUtil.IsGitProject(projectDir)) { return(new RepoResourceInfo()); } RepoResourceInfo info = new RepoResourceInfo(); try { string identifier = SoftwareCoUtil.RunCommand("git config remote.origin.url", projectDir); if (identifier != null && !identifier.Equals("")) { info.identifier = identifier; // only get these since identifier is available string email = SoftwareCoUtil.RunCommand("git config user.email", projectDir); if (email != null && !email.Equals("")) { info.email = email; } string branch = SoftwareCoUtil.RunCommand("git symbolic-ref --short HEAD", projectDir); if (branch != null && !branch.Equals("")) { info.branch = branch; } string tag = SoftwareCoUtil.RunCommand("git describe --all", projectDir); if (tag != null && !tag.Equals("")) { info.tag = tag; } if (includeMembers) { List <RepoMember> repoMembers = new List <RepoMember>(); string gitLogData = SoftwareCoUtil.RunCommand("git log --pretty=%an,%ae | sort", projectDir); IDictionary <string, string> memberMap = new Dictionary <string, string>(); if (gitLogData != null && !gitLogData.Equals("")) { string[] lines = gitLogData.Split( new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); if (lines != null && lines.Length > 0) { for (int i = 0; i < lines.Length; i++) { string line = lines[i]; string[] memberInfos = line.Split(','); if (memberInfos != null && memberInfos.Length > 1) { string name = memberInfos[0].Trim(); string memberEmail = memberInfos[1].Trim(); if (!memberMap.ContainsKey(email)) { memberMap.Add(email, name); repoMembers.Add(new RepoMember(name, email)); } } } } } info.Members = repoMembers; } } } catch (Exception ex) { Logger.Error("GetResourceInfo , error :" + ex.Message, ex); } return(info); }
public async void GetRepoUsers(string projectDir) { if (projectDir == null || projectDir.Equals("")) { return; } IDictionary <string, string> resourceInfo = this.GetResourceInfo(projectDir); if (resourceInfo != null && resourceInfo.ContainsKey("identifier")) { string identifier = ""; resourceInfo.TryGetValue("identifier", out identifier); if (identifier != null && !identifier.Equals("")) { string tag = ""; resourceInfo.TryGetValue("tag", out tag); string branch = ""; resourceInfo.TryGetValue("branch", out branch); string gitLogData = SoftwareCoUtil.RunCommand("git log --pretty=%an,%ae | sort", projectDir); IDictionary <string, string> memberMap = new Dictionary <string, string>(); List <RepoMember> repoMembers = new List <RepoMember>(); if (gitLogData != null && !gitLogData.Equals("")) { string[] lines = gitLogData.Split( new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); if (lines != null && lines.Length > 0) { for (int i = 0; i < lines.Length; i++) { string line = lines[i]; string[] memberInfos = line.Split(','); if (memberInfos != null && memberInfos.Length > 1) { string name = memberInfos[0].Trim(); string email = memberInfos[1].Trim(); if (!memberMap.ContainsKey(email)) { memberMap.Add(email, name); repoMembers.Add(new RepoMember(name, email)); } } } } } if (memberMap.Count > 0) { RepoData repoData = new RepoData(identifier, tag, branch, repoMembers); string jsonContent = SimpleJson.SerializeObject(repoData); // send the members HttpResponseMessage response = await SoftwareHttpManager.SendRequestAsync( HttpMethod.Post, "/repo/members", jsonContent); if (!SoftwareHttpManager.IsOk(response)) { Logger.Error(response.ToString()); } } } } }