Skip to content

Commit

Permalink
Merge pull request #67 from nickurak/shallow-clones
Browse files Browse the repository at this point in the history
Implement depth/shallow-clone support
  • Loading branch information
yuvipanda authored Feb 26, 2019
2 parents c464a5d + e7cd350 commit 06e62f6
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 79 deletions.
9 changes: 7 additions & 2 deletions nbgitpuller/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ def get(self):
try:
repo = self.get_argument('repo')
branch = self.get_argument('branch')
depth = self.get_argument('depth', None)
if depth:
depth = int(depth)
repo_dir = repo.split('/')[-1]

# We gonna send out event streams!
self.set_header('content-type', 'text/event-stream')
self.set_header('cache-control', 'no-cache')

gp = GitPuller(repo, branch, repo_dir)
gp = GitPuller(repo, branch, repo_dir, depth=depth)

q = Queue()
def pull():
Expand Down Expand Up @@ -134,6 +137,7 @@ def get(self):

repo = self.get_argument('repo')
branch = self.get_argument('branch', 'master')
depth = self.get_argument('depth', None)
urlPath = self.get_argument('urlpath', None) or \
self.get_argument('urlPath', None)
subPath = self.get_argument('subpath', None) or \
Expand All @@ -155,7 +159,7 @@ def get(self):
self.write(
self.render_template(
'status.html',
repo=repo, branch=branch, path=path, version=__version__
repo=repo, branch=branch, path=path, depth=depth, version=__version__
))
self.flush()

Expand All @@ -180,6 +184,7 @@ def get(self):
query = {
'repo': repo_url,
'branch': self.get_argument('branch', 'gh-pages'),
'depth': self.get_argument('depth'),
'subPath': self.get_argument('path')
}
new_url = '{base}git-pull?{query}'.format(
Expand Down
34 changes: 30 additions & 4 deletions nbgitpuller/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import time
import argparse
import datetime
from traitlets import Integer, default
from traitlets.config import Configurable
from functools import partial

def execute_cmd(cmd, **kwargs):
Expand Down Expand Up @@ -40,13 +42,33 @@ def flush():
if ret != 0:
raise subprocess.CalledProcessError(ret, cmd)

class GitPuller:
def __init__(self, git_url, branch_name, repo_dir):
class GitPuller(Configurable):
depth = Integer(None, allow_none=True, config=True,
help="""
Depth (ie, commit count) to which to perform a
shallow git clone.
If not set, disables shallow clones.
Defaults to reading from the NBGITPULLER_DEPTH
environment variable.
""")

@default('depth')
def _depth_default(self):
depth = os.environ.get('NBGITPULLER_DEPTH')
if depth:
return int(depth)
return None

def __init__(self, git_url, branch_name, repo_dir, **kwargs):
assert git_url and branch_name

self.git_url = git_url
self.branch_name = branch_name
self.repo_dir = repo_dir
newargs = {k: v for k, v in kwargs.items() if v is not None}
super(Configurable, self).__init__(**newargs)

def pull(self):
"""
Expand All @@ -64,8 +86,12 @@ def initialize_repo(self):
"""

logging.info('Repo {} doesn\'t exist. Cloning...'.format(self.repo_dir))
yield from execute_cmd(['git', 'clone', self.git_url, self.repo_dir])
yield from execute_cmd(['git', 'checkout', self.branch_name], cwd=self.repo_dir)
clone_args = ['git', 'clone']
if self.depth and self.depth > 0:
clone_args.extend(['--depth', str(self.depth)])
clone_args.extend(['--branch', self.branch_name])
clone_args.extend([self.git_url, self.repo_dir])
yield from execute_cmd(clone_args)
yield from execute_cmd(['git', 'config', 'user.email', '[email protected]'], cwd=self.repo_dir)
yield from execute_cmd(['git', 'config', 'user.name', 'nbgitpuller'], cwd=self.repo_dir)
logging.info('Repo {} initialized'.format(self.repo_dir))
Expand Down
12 changes: 9 additions & 3 deletions nbgitpuller/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ require([

Terminal.applyAddon(fit);

function GitSync(baseUrl, repo, branch, path) {
function GitSync(baseUrl, repo, branch, depth, path) {
// Class that talks to the API backend & emits events as appropriate
this.baseUrl = baseUrl;
this.repo = repo;
this.branch = branch;
this.depth = depth;
this.redirectUrl = baseUrl + path;

this.callbacks = {};
Expand All @@ -40,10 +41,14 @@ require([

GitSync.prototype.start = function() {
// Start git pulling handled by SyncHandler, declared in handlers.py
var syncUrl = this.baseUrl + 'git-pull/api?' + $.param({
var syncUrlParams = {
repo: this.repo,
branch: this.branch
});
}
if (typeof this.depth !== 'undefined' && this.depth != undefined) {
syncUrlParams['depth'] = this.depth;
}
var syncUrl = this.baseUrl + 'git-pull/api?' + $.param(syncUrlParams);

this.eventSource = new EventSource(syncUrl);
var that = this;
Expand Down Expand Up @@ -123,6 +128,7 @@ require([
utils.get_body_data('baseUrl'),
utils.get_body_data('repo'),
utils.get_body_data('branch'),
utils.get_body_data('depth'),
utils.get_body_data('path')
);

Expand Down
1 change: 1 addition & 0 deletions nbgitpuller/templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
data-repo="{{ repo | urlencode }}"
data-path="{{ path | urlencode }}"
data-branch="{{ branch | urlencode }}"
{% if depth %}data-depth="{{ depth | urlencode }}"{% endif %}
{% endblock %}

{% block site %}
Expand Down
Loading

0 comments on commit 06e62f6

Please sign in to comment.