import os
import os.path as op
import getpass

from github import Github
import git


def upload(target, repo_name, uname=None, upass=None):
    """
    Upload an assembled AFQ-Browser site to a github pages website

    Parameters
    ----------
    target : str
        Local path to the file-system location where
    repo_name : str
        The website will be at https://<username>.github.io/<repo_name>
    uname : str, optional
        Github user-name
    upass : str, optional
        Github password

    """
    # Get all the files that will be committed/pushed
    file_list = []
    target = op.join(target, 'AFQ-browser', 'client')
    for path, dirs, files in os.walk(target):
        for f in files:
            file_list.append(os.path.abspath(op.join(path, f)))
    # Get credentials from the user
    if uname is None:
        getpass.getpass("Github user-name?")
    if upass is None:
        getpass.gepass("Github password?")

    # Create the remote repo on Github (use PyGithub)
    g = Github(uname, upass)
    u = g.get_user()
    remote = u.create_repo(repo_name)
    # Create the local repo
    r = git.Repo.init(target)
    # Add all of the files to the repo's gh-pages branch
    r.index.add(file_list)
    r.index.commit("Commit everything")
    # Add a .nojekyll file
    f = open(op.join(target, '.nojekyll'))
    f.close()
    r.index.add([os.path.abspath(op.join(path, f))])
    r.index.commit("Add nojekyll file")
    # Push to Github

@@ -589,22 +589,29 @@
  %5B%5D%0A    
-target
+client_folder
  = op.jo
@@ -683,22 +683,29 @@
 os.walk(
-target
+client_folder
 ):%0A     
@@ -844,32 +844,40 @@
 is None:%0A       
+ uname =
  getpass.getpass
@@ -927,16 +927,24 @@
 %0A       
+ upass =
  getpass
@@ -946,16 +946,17 @@
 tpass.ge
+t
 pass(%22Gi
@@ -1365,16 +1365,21 @@
 jekyll')
+, 'w'
 )%0A    f.
@@ -1419,32 +1419,22 @@
 abspath(
-op.join(path, f)
+f.name
 )%5D)%0A    
@@ -1482,16 +1482,175 @@
  Push to Github%0A
+    branch = r.create_head(%22gh-pages%22)%0A    branch.checkout()%0A    r.create_remote(%22origin%22, remote.clone_url)%0A    o = r.remote(%22origin%22)%0A    o.push(%22gh-pages%22)%0A

Complete the process of pushing to Github.