Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env xcrun swift
- import Cocoa
- class Task {
- class func execute(name: String) -> String {
- let task = NSTask()
- task.launchPath = "/bin/sh"
- task.arguments = ["-c", name]
- let pipe = NSPipe()
- task.standardOutput = pipe
- let file = pipe.fileHandleForReading
- task.launch()
- let data = file.readDataToEndOfFile()
- let result = NSString(data: data, encoding: NSUTF8StringEncoding)
- return result as! String
- }
- }
- struct Committer {
- let name : String
- let lines : Int
- }
- struct Commit {
- let filePath : String
- let committers : [Committer]
- func toString() -> String {
- var result = "File: \(self.filePath)\n"
- let sortedCommitters = committers.sort{$0.lines > $1.lines}
- for committer in sortedCommitters {
- result += "\t- \(committer.name) (\(committer.lines))\n"
- }
- return result
- }
- }
- class GitCommand {
- private class func changedFiles() -> [String] {
- let fileChangedCmd = "git --no-pager diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD master)"
- let filesString = Task.execute(fileChangedCmd)
- let files = (filesString as NSString).componentsSeparatedByString("\n");
- return files
- }
- private class func blameFile(file: String) -> Commit? {
- if file.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 {
- return nil
- }
- let blameCmd = "git blame \(file) | awk '{print $2,$3}'"
- let blameResult = Task.execute(blameCmd)
- let lines = (blameResult as NSString).componentsSeparatedByString("\n")
- var committers = [String : Int]()
- for var user in lines {
- if user.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 {
- continue
- }
- if user.hasPrefix("(") {
- user = user.substringFromIndex(user.startIndex.advancedBy(1))
- }
- let count = committers[user]
- committers[user] = (count != nil) ? (count! + 1) : 1
- }
- let realCommitters = committers.map{return Committer(name: $0, lines: $1)}
- let commit = Commit(filePath: file, committers: realCommitters)
- return commit
- }
- class func allUsersInCommit() {
- let files = self.changedFiles()
- for file in files {
- if !file.hasPrefix("Resource") {
- if let commit = self.blameFile(file) {
- print(commit.toString())
- }
- }
- }
- }
- }
- print("\n\n### Current Brach Files ###")
- GitCommand.allUsersInCommit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement