示例#1
0
    def rename_file(self, old_path, new_path):
        # paths must not be unicode. :(
        old_files = [old_path]
        new_files = [new_path]
        if self.save_script and ".ipynb" in old_path:
            old_files.append(old_files[0].replace('.ipynb', '.py'))
            new_files.append(new_files[0].replace('.ipynb', '.py'))

        renamed = super(GitNotebookManager, self).rename_file(old_path, new_path)

        isFolder = os.path.splitext(old_path)[-1] == ""
        if any(old_path.endswith(ext) for ext in self._tracked_ext) or isFolder:
            if isFolder:
                subprocess.call(["git", "rm", "-r", str(old_path)], shell=False)
                subprocess.call(["git", "add", str(new_path)], shell=False)
                self._repo = None
                self._check_repo()
            else:
                git.rm(self._repo, [str(_) for _ in old_files])
                git.add(self._repo, [str(_) for _ in new_files])

            self.log.debug("Notebook renamed from '%s' to '%s'" % (old_files[0],
                                                                   new_files[0]))
            git.commit(self._repo, "IPython notebook rename\n\n"
                       "Automated commit from IPython via ipylogue",
                       committer=self.committer_fullname)
            # git.push(self._repo, self._repo.get_config()[('remote', 'origin')]["url"], "refs/heads/master")
        return renamed
示例#2
0
    def delete(self, request, user, data, message=None):
        '''Delete file(s) from repository
        '''
        files_to_del = data.get('files')
        if not files_to_del:
            raise DataError('Nothing to delete')
        # convert to list if not already
        if not isinstance(files_to_del, (list, tuple)):
            files_to_del = [files_to_del]

        filenames = []
        path = self.path

        for file in files_to_del:
            filepath = os.path.join(path, self._format_filename(file))
            # remove only files that really exist and not dirs
            if os.path.exists(filepath) and os.path.isfile(filepath):
                # remove from disk
                os.remove(filepath)
                filename = get_rel_dir(filepath, self.repo.path)
                filenames.append(filename)

        if filenames:
            rm(self.repo, filenames)
            if not message:
                message = 'Deleted %s' % ';'.join(filenames)

            return commit(self.repo, _b(message),
                          committer=_b(user.username))
示例#3
0
 def git_rm(args):
     if len(args) > 0:
         repo = _get_repo()
         args = [os.path.join(os.path.relpath('.', repo.path),x) for x in args]
         #repo.rm(args)
         porcelain.rm(repo.repo, args)
     else:
         print command_help['rm']
示例#4
0
 def test_remove_file(self):
     f = open(os.path.join(self.repo.path, 'foo'), 'w')
     try:
         f.write("BAR")
     finally:
         f.close()
     porcelain.add(self.repo.path, paths=["foo"])
     porcelain.rm(self.repo.path, paths=["foo"])
示例#5
0
 def test_remove_file(self):
     f = open(os.path.join(self.repo.path, 'foo'), 'w')
     try:
         f.write("BAR")
     finally:
         f.close()
     porcelain.add(self.repo.path, paths=["foo"])
     porcelain.rm(self.repo.path, paths=["foo"])
示例#6
0
 def git_rm(args):
     if len(args) > 0:
         repo = _get_repo()
         cwd = os.getcwd()
         args = [os.path.join(os.path.relpath(cwd, repo.path), x)
                     if not os.path.samefile(cwd, repo.path) else x for x in args]
         for file in args:
             print 'Removing {0}'.format(file)
         #repo.rm(args)
         porcelain.rm(repo.repo, args)
     else:
         print command_help['rm']
示例#7
0
文件: git.py 项目: zychen/stash
def git_rm(args):
    if len(args) > 0:
        repo = _get_repo()
        cwd = os.getcwd()
        args = [os.path.join(os.path.relpath(cwd, repo.path), x)
                    if not os.path.samefile(cwd, repo.path) else x for x in args]
        for file in args:
            print('Removing {0}'.format(file))
            #repo.rm(args)
            porcelain.rm(repo.repo.path, args)

    else:
        print(command_help['rm'])
示例#8
0
 def rm_and_commit(self, page, abspaths):
     """ remove files and folders (absolute paths) & commit changes """
     # -- page is the folder from which the delete form was submitted.
     relpaths = map(lambda x: os.path.relpath(x, os_git), abspaths)
     page.keep()
     porcelain.rm(os_git, paths=relpaths)
     porcelain.commit(os_git,
                      '--message=user:{}'.format(page.user.username))
     # This porcelain.rm seems to sometimes not delete the file itself.
     for abspath in abspaths:
         try:
             os.remove(abspath)
         except:
             pass
示例#9
0
 def delete(self, path):
     super(GitNotebookManager, self).delete(path)
     isFolder = os.path.splitext(path)[-1] == ""
     if any(path.endswith(ext) for ext in self._tracked_ext) or isFolder:
         if isFolder:
             subprocess.call(["git", "rm", "-r", str(path)[1:]], shell=False)
             self._repo = None
             self._check_repo()
         else:
             git.rm(self._repo, [str(path)[1:]])
         self.log.debug("Notebook {0} deleted".format(path))
         git.commit(self._repo, "IPython notebook delete\n\n"
                    "Automated commit from IPython via ipylogue",
                    committer=self.committer_fullname)
示例#10
0
    def test_get_tree_changes_delete(self):
        """Unit test for get_tree_changes delete."""

        # Make a dummy file, stage, commit, remove
        filename = "foo"
        with open(os.path.join(self.repo.path, filename), "w") as f:
            f.write("stuff")
        porcelain.add(repo=self.repo.path, paths=filename)
        porcelain.commit(repo=self.repo.path, message=b"test status", author=b"", committer=b"")
        porcelain.rm(repo=self.repo.path, paths=[filename])
        changes = porcelain.get_tree_changes(self.repo.path)

        self.assertEqual(changes["delete"][0], filename.encode("ascii"))
        self.assertEqual(len(changes["add"]), 0)
        self.assertEqual(len(changes["modify"]), 0)
        self.assertEqual(len(changes["delete"]), 1)
示例#11
0
    def test_get_tree_changes_delete(self):
        """Unit test for get_tree_changes delete."""

        # Make a dummy file, stage, commit, remove
        filename = 'foo'
        with open(os.path.join(self.repo.path, filename), 'w') as f:
            f.write('stuff')
        porcelain.add(repo=self.repo.path, paths=filename)
        porcelain.commit(repo=self.repo.path, message=b'test status',
            author=b'', committer=b'')
        porcelain.rm(repo=self.repo.path, paths=[filename])
        changes = porcelain.get_tree_changes(self.repo.path)

        self.assertEqual(changes['delete'][0], filename.encode('ascii'))
        self.assertEqual(len(changes['add']), 0)
        self.assertEqual(len(changes['modify']), 0)
        self.assertEqual(len(changes['delete']), 1)
示例#12
0
    def rename_file(self, old_path, new_path):
        # paths must not be unicode. :(
        old_files = [old_path]
        new_files = [new_path]
        if self.save_script:
            old_files.append(old_files[0].replace('.ipynb', '.py'))
            new_files.append(new_files[0].replace('.ipynb', '.py'))

        git.rm(self._repo, [str(_) for _ in old_files])

        renamed = super(GitNotebookManager, self).rename_file(old_path, new_path)

        git.add(self._repo, [str(_) for _ in new_files])

        self.log.debug("Notebook renamed from '%s' to '%s'" % (old_files[0],
                                                               new_files[0]))
        git.commit(self._repo, "IPython notebook rename\n\n"
                   "Automated commit from IPython via ipylogue",
                   committer=self.committer_fullname)
        return renamed
示例#13
0
    def rename_file(self, old_path, new_path):
        # paths must not be unicode. :(
        old_files = [old_path]
        new_files = [new_path]
        if self.save_script:
            old_files.append(old_files[0].replace('.ipynb', '.py'))
            new_files.append(new_files[0].replace('.ipynb', '.py'))

        git.rm(self._repo, [str(_) for _ in old_files])

        renamed = super(GitNotebookManager,
                        self).rename_file(old_path, new_path)

        git.add(self._repo, [str(_) for _ in new_files])

        self.log.debug("Notebook renamed from '%s' to '%s'" %
                       (old_files[0], new_files[0]))
        git.commit(self._repo, "IPython notebook rename\n\n"
                   "Automated commit from IPython via ipylogue",
                   committer=self.committer_fullname)
        return renamed
示例#14
0
def push(name, api, domain):
    repo = git.Repo(os.getcwd())
    branch = "temp-{}".format(str(uuid.uuid4())[:8])
    set_deploy_branch(name, branch, api, domain)

    remote = git_url(name, api, domain)

    if is_dirty():
        print("Nuking changes.")
        git.reset(repo, "hard")

    with TempBranch(branch, repo, delete=True):
        for fname, file_info in openshift_files.items():
            with open(fname, 'w') as f:
                f.write(file_info.get("contents", ""))
            repo.stage(fname)
        repo.do_commit("Commit openshift files")
        push_out = StringIO()
        push_err = StringIO()
        print("Pushing to openshift (may take a few minutes)")
        git.push(repo,
                 remote,
                 "refs/heads/{}".format(branch),
                 outstream=push_out,
                 errstream=push_err)

        push_out.seek(0)
        out = push_out.read()
        if not re.match(r'^Push to .* successful.', out):
            print("There was a failure while pushing")
            print("---BEGIN STDERR---")
            push_err.seek(0)
            print(push_err.read())
            print("---BEGIN STDOUT---")
            print(out)
            print("There was a failure while pushing")
        git.rm(repo, openshift_files.keys())
        map(os.remove, openshift_files.keys())

    return get_app(name, api, domain)['app_url']
示例#15
0
def push(name, api, domain):
    repo = git.Repo(os.getcwd())
    branch = "temp-{}".format(str(uuid.uuid4())[:8])
    set_deploy_branch(name, branch, api, domain)

    remote = git_url(name, api, domain)

    if is_dirty():
        print("Nuking changes.")
        git.reset(repo, "hard")

    with TempBranch(branch, repo, delete=True):
        for fname, file_info in openshift_files.items():
            with open(fname, 'w') as f:
                f.write(file_info.get("contents", ""))
            repo.stage(fname)
        repo.do_commit("Commit openshift files")
        push_out = StringIO()
        push_err = StringIO()
        print("Pushing to openshift (may take a few minutes)")
        git.push(repo, remote, "refs/heads/{}".format(branch),
                 outstream=push_out, errstream=push_err)

        push_out.seek(0)
        out = push_out.read()
        if not re.match(r'^Push to .* successful.', out):
            print("There was a failure while pushing")
            print("---BEGIN STDERR---")
            push_err.seek(0)
            print(push_err.read())
            print("---BEGIN STDOUT---")
            print(out)
            print("There was a failure while pushing")
        git.rm(repo, openshift_files.keys())
        map(os.remove, openshift_files.keys())

    return get_app(name, api, domain)['app_url']
示例#16
0
 def test_remove_file(self):
     with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
         f.write("BAR")
     porcelain.add(self.repo.path, paths=["foo"])
     porcelain.rm(self.repo.path, paths=["foo"])
示例#17
0
def merge(args):
    helptext='''git merge' [--msg <msg>] [<commit>]
    git merge --abort\n
    
    merges <commit> into HEAD, or remote tracking branch if commit not specified.
    <commit> can be a local or remote ref, or an existing commit sha.

    merge will handle unambiguous conflicts between head and other 
    merge head, and will insert conflict markers if conflicts cannot be resolved.  
    note that the strategy used will prefer changes in the local head.  
    for instance, if HEAD deleted a section, while MERGE_HEAD modified the same 
    action, the section will be deleted from the final without indicating a conflict.
      
    be sure to commit any local changes before running merge, as files in working tree (i.e on disk) are changed, and checked in, which will probably overwrite any local uncomitted changes.
    
    note merge will not actually commit anything.  run git commit to commit a successful merge.

    
    --abort will remove the MERGE_HEAD and MERGE_MSG files, and will reset staging area, but wont affect files on disk.  use git reset --hard or git checkout if this is desired.
    '''
    repo=_get_repo()
    print '_'*30

    parser=argparse.ArgumentParser(prog='merge', usage=helptext)
    parser.add_argument('commit',action='store',nargs='?', help='commit sha, local branch, or remote branch name to merge from')
    parser.add_argument('--msg',nargs=1,action='store',help='commit message to store')
    parser.add_argument('--abort',action='store_true',help='abort in progress merge attempt')
    result=parser.parse_args(args)
    
    if result.abort:
        print 'attempting to undo merge.  beware, files in working tree are not touched.  \nused git reset --hard to revert particular files'
        git_reset([])
        os.remove(os.path.join(repo.repo.controldir(),'MERGE_HEAD'))
        os.remove(os.path.join(repo.repo.controldir(),'MERGE_MSG'))

    #todo: check for uncommitted changes and confirm
    
    # first, determine merge head
    merge_head = find_revision_sha(repo,result.commit or get_remote_tracking_branch(repo,repo.active_branch))
    if not merge_head:
        raise GitError('must specify a commit sha, branch, remote tracking branch to merge from.  or, need to set-upstream branch using git branch --set-upstream <remote>[/<branch>]')

    head=find_revision_sha(repo,repo.active_branch)

    base_sha=merge_base(repo,head,merge_head)[0]  #fixme, what if multiple bases

    if base_sha==head:
        print 'Fast forwarding {} to {}'.format(repo.active_branch,merge_head)
        repo.refs['HEAD']=merge_head
        return 
    if base_sha == merge_head:
        print 'head is already up to date'
        return  
    
    print 'merging <{}> into <{}>\n{} commits ahead of merge base <{}> respectively'.format(merge_head[0:7],head[0:7],count_commits_between(repo,merge_head,head),base_sha[0:7])
    base_tree=repo[base_sha].tree
    merge_head_tree=repo[merge_head].tree
    head_tree=repo[head].tree

    num_conflicts,added,removed=merge_trees(repo.repo.object_store, base_tree,head_tree,merge_head_tree)
    # update index
    if added: 
        porcelain.add(repo.path, added)
    if removed: 
        porcelain.rm(repo.path, removed)

    repo.repo._put_named_file('MERGE_HEAD',merge_head)
    repo.repo._put_named_file('MERGE_MSG','Merged from {}({})'.format(merge_head, result.commit))
    print 'Merge complete with {} conflicted files'.format(num_conflicts)
    print '''Merged files were added to the staging area, but have not yet been comitted.   
示例#18
0
def performCommit(localRepo, bookid, bookinfo, bookroot, bookfiles):
    has_error = False
    staged = []
    added = []
    ignored = []
    # convert url paths to os specific paths
    repo_home = pathof(localRepo)
    repo_home = repo_home.replace("/", os.sep)
    repo_path = os.path.join(repo_home, "epub_" + bookid)
    book_home = pathof(bookroot)
    book_home = book_home.replace("/", os.sep)
    # convert from bookpaths to os relative file paths
    filepaths = []
    for bkpath in bookfiles:
        afile = pathof(bkpath)
        afile = afile.replace("/", os.sep)
        filepaths.append(afile)

    cdir = os.getcwd()
    if os.path.exists(repo_path):
        # handle updating the staged files and commiting and tagging
        # first collect info to determine files to delete form repo
        # current tag, etc
        os.chdir(repo_path)
        # determine the new tag
        tags = porcelain.list_tags(repo='.')
        tagname = "V%04d" % (len(tags) + 1)
        tagmessage = "Tag: " + tagname
        message = "updating to " + tagname
        # extra parameters must be passed as bytes if annotated is true
        tagname = utf8_str(tagname)
        message = utf8_str(message)
        tagmessage = utf8_str(tagmessage)
        # delete files that are no longer needed from staging area
        tracked = []
        tracked = porcelain.ls_files(repo='.')
        files_to_delete = []
        for afile in tracked:
            afile = pathof(afile)
            if afile not in filepaths:
                if afile not in ["mimetype", ".gitignore", ".bookinfo"]:
                    files_to_delete.append(afile)
        if len(files_to_delete) > 0:
            porcelain.rm(repo='.', paths=files_to_delete)
        # copy over current files
        copy_book_contents_to_destination(book_home, filepaths, repo_path)
        (staged, unstaged, untracked) = porcelain.status(repo='.')
        files_to_update = []
        for afile in unstaged:
            afile = pathof(afile)
            files_to_update.append(afile)
        for afile in untracked:
            afile = pathof(afile)
            files_to_update.append(afile)
        (added, ignored) = porcelain.add(repo='.', paths=files_to_update)
        commit_sha1 = porcelain.commit(repo='.',
                                       message=message,
                                       author=_SIGIL,
                                       committer=_SIGIL)
        # create annotated tags so we can get a date history
        tag = porcelain.tag_create(repo='.',
                                   tag=tagname,
                                   message=tagmessage,
                                   annotated=True,
                                   author=_SIGIL)
        os.chdir(cdir)
        add_bookinfo(repo_path, bookinfo, bookid, unicode_str(tagname))
    else:
        # this will be an initial commit to this repo
        tagname = b"V0001"
        tagmessage = b'First Tag'
        message = b"Initial Commit"
        os.makedirs(repo_path)
        add_gitignore(repo_path)
        add_gitattributes(repo_path)
        cdir = os.getcwd()
        os.chdir(repo_path)
        r = porcelain.init(path='.', bare=False)
        staged = copy_book_contents_to_destination(book_home, filepaths,
                                                   repo_path)
        (added, ignored) = porcelain.add(repo='.', paths=staged)
        # it seems author, committer, messages, and tagname only work with bytes if annotated=True
        commit_sha1 = porcelain.commit(repo='.',
                                       message=message,
                                       author=_SIGIL,
                                       committer=_SIGIL)
        tag = porcelain.tag_create(repo='.',
                                   tag=tagname,
                                   message=tagmessage,
                                   annotated=True,
                                   author=_SIGIL)
        os.chdir(cdir)
        add_bookinfo(repo_path, bookinfo, bookid, unicode_str(tagname))
    result = "\n".join(added)
    result = result + "***********" + "\n".join(ignored)
    if not has_error:
        return result
    return ''
示例#19
0
    def run(self, args):
        opts, args = getopt(args, "", [])

        porcelain.rm(".", paths=args)
示例#20
0
 def delact(sender):
     porcelain.rm(self._repo(),[str(self.list[section][row])])
     self.refresh()
     console.hud_alert('del {} {}'.format(section,row))
示例#21
0
    def run(self, argv):
        parser = argparse.ArgumentParser()
        args = parser.parse_args(argv)

        porcelain.rm(".", paths=args)
示例#22
0
 def delact(sender):
     porcelain.rm(self._repo(), [str(self.list[section][row])])
     self.refresh()
     console.hud_alert('del {} {}'.format(section, row))