Skip to content

Instantly share code, notes, and snippets.

@ianoc
Created March 10, 2016 22:53
Show Gist options
  • Save ianoc/5cc6d687390a4a426ab8 to your computer and use it in GitHub Desktop.
Save ianoc/5cc6d687390a4a426ab8 to your computer and use it in GitHub Desktop.
Locate path in repo
import os
import subprocess
from subprocess import PIPE
import sublime, sublime_plugin
class FpathCommand(sublime_plugin.TextCommand):
def run(self, edit):
file = self.view.file_name()
file_dir = "/".join(file.split("/")[:-1])
gitRepo, err = subprocess.Popen('/usr/local/bin/git rev-parse --show-toplevel', cwd=file_dir, shell=True, stdout=PIPE).communicate(timeout=30)
gitRepo = gitRepo[:-1]
resultingPath = file[len(gitRepo)+1:]
if(len(gitRepo) == 0):
resultingPath = self.view.file_name()
endingInBuild = "/BUILD"
if resultingPath.endswith(endingInBuild):
resultingPath = resultingPath[0:len(endingInBuild) * -1]
pbcopy = subprocess.Popen("pbcopy", stdin=PIPE).stdin
pbcopy.write(bytes(resultingPath, 'UTF-8'))
pbcopy.close()
sublime.status_message(resultingPath + " copied to clipboard")
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment