Skip to content

Instantly share code, notes, and snippets.

@Watson1978
Last active May 21, 2018 02:17
Show Gist options
  • Save Watson1978/4fd5f0eb68b6109f35eef74eba0c41bd to your computer and use it in GitHub Desktop.
Save Watson1978/4fd5f0eb68b6109f35eef74eba0c41bd to your computer and use it in GitHub Desktop.
def detect_project_root(path)
path = File.dirname(path)
if Dir.glob(File.join(path, ".git")).count > 0
return path
end
return nil if path == "/"
detect_project_root(path)
end
full_path = `osascript<<END
tell application "Xcode"
set file_name to name of front window
set file_name to replace(file_name, " — Edited", "") of me
set num to number of source document
repeat with index from 0 to num
set file_path to path of source document index
set pos to offset of file_name in file_path
if pos is greater than 0 then
return file_path
end if
end repeat
end tell
on replace(orgStr, tgtStr, newStr)
local orgDelim, rtn
set orgDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {tgtStr}
set rtn to every text item of orgStr
set AppleScript's text item delimiters to {newStr}
set rtn to rtn as string
set AppleScript's text item delimiters to orgDelim
return rtn
end replace
END`.strip
project_root = detect_project_root(full_path)
file_path = full_path.sub("#{project_root}/", '')
repository = nil
branch = nil
Dir.chdir project_root do
`git remote -v`.strip.lines do |line|
if line =~ %r{[email protected]:(.+)\.git \(fetch\)$} || line =~ %r{https://github.com/(.+) \(fetch\)$}
repository = $1
break
end
end
exit unless repository
`git branch -r`.strip.lines do |line|
if line =~ %r{.+/HEAD -> .+/(.+)$}
branch = $1
break
end
end
exit unless branch
end
line = ARGV[0] || ""
unless line.empty?
line = "#" + line
end
system "open 'https://github.com/#{repository}/blob/#{branch}/#{file_path}#{line}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment