Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #!/usr/bin/env xcrun swift
  2.  
  3.  
  4. import Cocoa
  5.  
  6.  
  7. class Task {
  8. class func execute(name: String) -> String {
  9. let task = NSTask()
  10. task.launchPath = "/bin/sh"
  11. task.arguments = ["-c", name]
  12.  
  13. let pipe = NSPipe()
  14. task.standardOutput = pipe
  15.  
  16. let file = pipe.fileHandleForReading
  17.  
  18. task.launch()
  19.  
  20. let data = file.readDataToEndOfFile()
  21. let result = NSString(data: data, encoding: NSUTF8StringEncoding)
  22.  
  23.  
  24. return result as! String
  25. }
  26. }
  27.  
  28. struct Committer {
  29. let name : String
  30. let lines : Int
  31. }
  32.  
  33. struct Commit {
  34. let filePath : String
  35. let committers : [Committer]
  36.  
  37. func toString() -> String {
  38. var result = "File: \(self.filePath)\n"
  39. let sortedCommitters = committers.sort{$0.lines > $1.lines}
  40. for committer in sortedCommitters {
  41. result += "\t- \(committer.name) (\(committer.lines))\n"
  42. }
  43.  
  44. return result
  45. }
  46. }
  47.  
  48. class GitCommand {
  49. private class func changedFiles() -> [String] {
  50. let fileChangedCmd = "git --no-pager diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD master)"
  51. let filesString = Task.execute(fileChangedCmd)
  52. let files = (filesString as NSString).componentsSeparatedByString("\n");
  53.  
  54. return files
  55. }
  56.  
  57. private class func blameFile(file: String) -> Commit? {
  58.  
  59. if file.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 {
  60. return nil
  61. }
  62.  
  63. let blameCmd = "git blame \(file) | awk '{print $2,$3}'"
  64. let blameResult = Task.execute(blameCmd)
  65. let lines = (blameResult as NSString).componentsSeparatedByString("\n")
  66. var committers = [String : Int]()
  67. for var user in lines {
  68. if user.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 {
  69. continue
  70. }
  71. if user.hasPrefix("(") {
  72. user = user.substringFromIndex(user.startIndex.advancedBy(1))
  73. }
  74.  
  75. let count = committers[user]
  76. committers[user] = (count != nil) ? (count! + 1) : 1
  77. }
  78.  
  79. let realCommitters = committers.map{return Committer(name: $0, lines: $1)}
  80.  
  81. let commit = Commit(filePath: file, committers: realCommitters)
  82. return commit
  83. }
  84.  
  85.  
  86. class func allUsersInCommit() {
  87. let files = self.changedFiles()
  88. for file in files {
  89. if !file.hasPrefix("Resource") {
  90. if let commit = self.blameFile(file) {
  91. print(commit.toString())
  92. }
  93. }
  94. }
  95. }
  96. }
  97.  
  98.  
  99. print("\n\n### Current Brach Files ###")
  100. GitCommand.allUsersInCommit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement