Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set destination folder #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Add an 'Open in Sublime' button to GitHub

![Screenshot](https://raw.github.com/mechio/subhub/master/screenshot.png)

Clicking the button clones the repository into `~/.subhub` before opening it in Sublime Text 3
Clicking the button clones the repository into specific folder before opening it in Sublime Text 3

# Requirements

Expand All @@ -13,11 +13,12 @@ Clicking the button clones the repository into `~/.subhub` before opening it in
* Chrome

# Settings
* **if_exists** - what to do the repo already exists on the local computer possible options:
* pull - update your local repository to the newest commit
* **if_exists** - what to do the repo already exists on the local computer possible options:
* pull - update your local repository to the newest commit
* nothing - it will just open the local copy
* **cache_dir** - local directory where you want to clone the repository



# Installation

1. Install the [Chrome Extension](https://chrome.google.com/webstore/detail/subhub/dndgngopahigljbjkfihmkaaaeceagbg)
Expand Down
35 changes: 20 additions & 15 deletions subhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import BaseHTTPServer
except ImportError:
import http.server as BaseHTTPServer

CACHE_DIR = expanduser("~") + '/.subhub'

PORT = 48666
def open(directory):
if sublime.platform() == 'windows':
Expand All @@ -25,19 +24,25 @@ def clone(url):
m = re.search('.*github.com[\/|:]([^\/]+)\/([^\/|\.]+)', url)
user = m.group(1)
repo = m.group(2)

local_repo = CACHE_DIR + '/' + user + '/' + repo

settings = sublime.load_settings("subhub.sublime-settings")

if settings.get('cache_dir') is not "":
cache_dir = settings.get('cache_dir')
else:
cache_dir = expanduser("~") + '/.subhub'

local_repo = cache_dir + '/' + user + '/' + repo

if not os.path.isdir(local_repo):
subprocess.call(['mkdir', '-p', CACHE_DIR + '/' + user])
subprocess.call(['mkdir', '-p', cache_dir + '/' + user])

if sublime.platform() == 'windows':
subprocess.call(['git', 'clone', "https://github.com/"+ user +"/" + repo + ".git", local_repo, '--depth', '1'])
else:
subprocess.call(['git', 'clone', url, local_repo, '--depth', '1'])

else:
settings = sublime.load_settings("subhub.sublime-settings")

else:
if settings.get('if_exists') == 'pull':
process = subprocess.Popen(['git', 'pull'], cwd=local_repo, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = [x.decode('unicode_escape').rstrip() for x in process.stdout.readlines()]
Expand All @@ -52,17 +57,17 @@ def do_GET(self):
print('Received: heartbeat1')
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()


return

def do_POST(self):
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.send_response(201)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_response(201)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
data = json.loads(self.data_string.decode("utf-8"))
open(clone(data['url']))
Expand All @@ -78,5 +83,5 @@ def plugin_unloaded():
print("Closing subhub server")
server.shutdown()
server.server_close()

#curl -X POST -H "Content-Type: application/json" -d '{"url":"https://github.com/nodejitsu/nssocket"}' http://localhost:48666
6 changes: 5 additions & 1 deletion subhub.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
// what to do the repo already exists on the local computer
// possible options: pull - update your local repository to the newest commit, nothing - it will just open the local copy
"if_exists" : "pull"
"if_exists" : "pull",

// local directory where you want to clone the repository
// if not present, the repository will be cloned into ~/.subhub
"cache_dir" : ""
}