Exemplo n.º 1
0
    def delete_home(self, repo_name, revision, f_path):
        commit_id = revision

        repo = c.rhodecode_db_repo
        if repo.enable_locking and repo.locked[0]:
            h.flash(
                _('This repository has been locked by %s on %s') %
                (h.person_by_id(repo.locked[0]),
                 h.format_date(h.time_to_datetime(repo.locked[1]))), 'warning')
            return redirect(
                h.url('files_home', repo_name=repo_name, revision='tip'))

        if not self._is_valid_head(commit_id, repo.scm_instance()):
            h.flash(_('You can only delete files with revision '
                      'being a valid branch '),
                    category='warning')
            return redirect(
                h.url('files_home',
                      repo_name=repo_name,
                      revision='tip',
                      f_path=f_path))

        c.commit = self.__get_commit_or_redirect(commit_id, repo_name)
        c.file = self.__get_filenode_or_redirect(repo_name, c.commit, f_path)

        c.default_message = _('Deleted file %s via RhodeCode Enterprise') % (
            f_path)
        c.f_path = f_path

        return render('files/files_delete.html')
Exemplo n.º 2
0
    def add(self, repo_name, revision, f_path):

        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(_('This repository is has been locked by %s on %s')
                % (h.person_by_id(repo.locked[0]),
                   h.fmt_date(h.time_to_datetime(repo.locked[1]))),
                  'warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip'))

        r_post = request.POST
        c.cs = self.__get_cs_or_redirect(revision, repo_name,
                                         redirect_after=False)
        if c.cs is None:
            c.cs = EmptyChangeset(alias=c.rhodecode_repo.alias)

        c.f_path = f_path

        if r_post:
            unix_mode = 0
            content = convert_line_endings(r_post.get('content'), unix_mode)

            message = r_post.get('message') or (_('Added %s via RhodeCode')
                                                % (f_path))
            location = r_post.get('location')
            filename = r_post.get('filename')
            file_obj = r_post.get('upload_file', None)

            if file_obj is not None and hasattr(file_obj, 'filename'):
                filename = file_obj.filename
                content = file_obj.file

            node_path = os.path.join(location, filename)
            author = self.rhodecode_user.full_contact

            if not content:
                h.flash(_('No content'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            if not filename:
                h.flash(_('No filename'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))

            try:
                self.scm_model.create_node(repo=c.rhodecode_repo,
                                           repo_name=repo_name, cs=c.cs,
                                           user=self.rhodecode_user,
                                           author=author, message=message,
                                           content=content, f_path=node_path)
                h.flash(_('Successfully committed to %s') % node_path,
                        category='success')
            except NodeAlreadyExistsError, e:
                h.flash(_(e), category='error')
            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during commit'), category='error')
Exemplo n.º 3
0
    def edit(self, repo_name, revision, f_path):
        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(_('This repository is has been locked by %s on %s')
                % (h.person_by_id(repo.locked[0]),
                   h.fmt_date(h.time_to_datetime(repo.locked[1]))),
                  'warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip'))

        r_post = request.POST

        c.cs = self.__get_cs_or_redirect(revision, repo_name)
        c.file = self.__get_filenode_or_redirect(repo_name, c.cs, f_path)

        if c.file.is_binary:
            return redirect(url('files_home', repo_name=c.repo_name,
                         revision=c.cs.raw_id, f_path=f_path))

        c.f_path = f_path

        if r_post:

            old_content = c.file.content
            sl = old_content.splitlines(1)
            first_line = sl[0] if sl else ''
            # modes:  0 - Unix, 1 - Mac, 2 - DOS
            mode = detect_mode(first_line, 0)
            content = convert_line_endings(r_post.get('content'), mode)

            message = r_post.get('message') or (_('Edited %s via RhodeCode')
                                                % (f_path))
            author = self.rhodecode_user.full_contact

            if content == old_content:
                h.flash(_('No changes'),
                    category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))

            try:
                self.scm_model.commit_change(repo=c.rhodecode_repo,
                                             repo_name=repo_name, cs=c.cs,
                                             user=self.rhodecode_user,
                                             author=author, message=message,
                                             content=content, f_path=f_path)
                h.flash(_('Successfully committed to %s') % f_path,
                        category='success')

            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during commit'), category='error')
            return redirect(url('changeset_home',
                                repo_name=c.repo_name, revision='tip'))

        return render('files/files_edit.html')
Exemplo n.º 4
0
    def delete(self, repo_name, revision, f_path):
        commit_id = revision

        repo = c.rhodecode_db_repo
        if repo.enable_locking and repo.locked[0]:
            h.flash(
                _('This repository has been locked by %s on %s') %
                (h.person_by_id(repo.locked[0]),
                 h.format_date(h.time_to_datetime(repo.locked[1]))), 'warning')
            return redirect(
                h.url('files_home', repo_name=repo_name, revision='tip'))

        if not self._is_valid_head(commit_id, repo.scm_instance()):
            h.flash(_('You can only delete files with revision '
                      'being a valid branch '),
                    category='warning')
            return redirect(
                h.url('files_home',
                      repo_name=repo_name,
                      revision='tip',
                      f_path=f_path))

        c.commit = self.__get_commit_or_redirect(commit_id, repo_name)
        c.file = self.__get_filenode_or_redirect(repo_name, c.commit, f_path)

        c.default_message = _('Deleted file %s via RhodeCode Enterprise') % (
            f_path)
        c.f_path = f_path
        node_path = f_path
        author = c.rhodecode_user.full_contact
        message = request.POST.get('message') or c.default_message
        try:
            nodes = {node_path: {'content': ''}}
            self.scm_model.delete_nodes(
                user=c.rhodecode_user.user_id,
                repo=c.rhodecode_db_repo,
                message=message,
                nodes=nodes,
                parent_commit=c.commit,
                author=author,
            )

            h.flash(_('Successfully deleted file %s') % f_path,
                    category='success')
        except Exception:
            msg = _('Error occurred during commit')
            log.exception(msg)
            h.flash(msg, category='error')
        return redirect(
            url('changeset_home', repo_name=c.repo_name, revision='tip'))
Exemplo n.º 5
0
    def add_home(self, repo_name, revision, f_path):

        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(
                _('This repository has been locked by %s on %s') %
                (h.person_by_id(repo.locked[0]),
                 h.format_date(h.time_to_datetime(repo.locked[1]))), 'warning')
            return redirect(
                h.url('files_home', repo_name=repo_name, revision='tip'))

        c.commit = self.__get_commit_or_redirect(revision,
                                                 repo_name,
                                                 redirect_after=False)
        if c.commit is None:
            c.commit = EmptyCommit(alias=c.rhodecode_repo.alias)
        c.default_message = (_('Added file via RhodeCode Enterprise'))
        c.f_path = f_path

        return render('files/files_add.html')
Exemplo n.º 6
0
    def add(self, repo_name, revision, f_path):

        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(_('This repository is has been locked by %s on %s')
                % (h.person_by_id(repo.locked[0]),
                   h.fmt_date(h.time_to_datetime(repo.locked[1]))),
                  'warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip'))

        r_post = request.POST
        c.cs = self.__get_cs_or_redirect(revision, repo_name,
                                         redirect_after=False)
        if c.cs is None:
            c.cs = EmptyChangeset(alias=c.rhodecode_repo.alias)
        c.default_message = (_('Added file via RhodeCode'))
        c.f_path = f_path

        if r_post:
            unix_mode = 0
            content = convert_line_endings(r_post.get('content'), unix_mode)

            message = r_post.get('message') or c.default_message
            filename = r_post.get('filename')
            location = r_post.get('location')
            file_obj = r_post.get('upload_file', None)

            if file_obj is not None and hasattr(file_obj, 'filename'):
                filename = file_obj.filename
                content = file_obj.file

            if not content:
                h.flash(_('No content'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            if not filename:
                h.flash(_('No filename'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            if location.startswith('/') or location.startswith('.') or '../' in location:
                h.flash(_('Location must be relative path and must not '
                          'contain .. in path'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            if location:
                location = os.path.normpath(location)
            filename = os.path.basename(filename)
            node_path = os.path.join(location, filename)
            author = self.rhodecode_user.full_contact

            try:
                self.scm_model.create_node(repo=c.rhodecode_repo,
                                           repo_name=repo_name, cs=c.cs,
                                           user=self.rhodecode_user.user_id,
                                           author=author, message=message,
                                           content=content, f_path=node_path)
                h.flash(_('Successfully committed to %s') % node_path,
                        category='success')
            except (NodeError, NodeAlreadyExistsError), e:
                h.flash(_(e), category='error')
            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during commit'), category='error')
Exemplo n.º 7
0
    def edit(self, repo_name, revision, f_path):
        repo = c.rhodecode_db_repo
        if repo.enable_locking and repo.locked[0]:
            h.flash(_('This repository is has been locked by %s on %s')
                % (h.person_by_id(repo.locked[0]),
                   h.fmt_date(h.time_to_datetime(repo.locked[1]))),
                  'warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip'))

        # check if revision is a branch identifier- basically we cannot
        # create multiple heads via file editing
        _branches = repo.scm_instance.branches
        # check if revision is a branch name or branch hash
        if revision not in _branches.keys() + _branches.values():
            h.flash(_('You can only edit files with revision '
                      'being a valid branch '), category='warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip',
                                  f_path=f_path))

        r_post = request.POST

        c.cs = self.__get_cs_or_redirect(revision, repo_name)
        c.file = self.__get_filenode_or_redirect(repo_name, c.cs, f_path)

        if c.file.is_binary:
            return redirect(url('files_home', repo_name=c.repo_name,
                         revision=c.cs.raw_id, f_path=f_path))
        c.default_message = _('Edited file %s via RhodeCode') % (f_path)
        c.f_path = f_path

        if r_post:

            old_content = c.file.content
            sl = old_content.splitlines(1)
            first_line = sl[0] if sl else ''
            # modes:  0 - Unix, 1 - Mac, 2 - DOS
            mode = detect_mode(first_line, 0)
            content = convert_line_endings(r_post.get('content'), mode)

            message = r_post.get('message') or c.default_message
            author = self.rhodecode_user.full_contact

            if content == old_content:
                h.flash(_('No changes'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            try:
                self.scm_model.commit_change(repo=c.rhodecode_repo,
                                             repo_name=repo_name, cs=c.cs,
                                             user=self.rhodecode_user.user_id,
                                             author=author, message=message,
                                             content=content, f_path=f_path)
                h.flash(_('Successfully committed to %s') % f_path,
                        category='success')

            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during commit'), category='error')
            return redirect(url('changeset_home',
                                repo_name=c.repo_name, revision='tip'))

        return render('files/files_edit.html')
Exemplo n.º 8
0
    def add(self, repo_name, revision, f_path):

        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(
                _('This repository is has been locked by %s on %s') %
                (h.person_by_id(repo.locked[0]),
                 h.fmt_date(h.time_to_datetime(repo.locked[1]))), 'warning')
            return redirect(
                h.url('files_home', repo_name=repo_name, revision='tip'))

        r_post = request.POST
        c.cs = self.__get_cs_or_redirect(revision,
                                         repo_name,
                                         redirect_after=False)
        if c.cs is None:
            c.cs = EmptyChangeset(alias=c.rhodecode_repo.alias)
        c.default_message = (_('Added file via RhodeCode'))
        c.f_path = f_path

        if r_post:
            unix_mode = 0
            content = convert_line_endings(r_post.get('content', ''),
                                           unix_mode)

            message = r_post.get('message') or c.default_message
            filename = r_post.get('filename')
            location = r_post.get('location', '')
            file_obj = r_post.get('upload_file', None)

            if file_obj is not None and hasattr(file_obj, 'filename'):
                filename = file_obj.filename
                content = file_obj.file

                if hasattr(content, 'file'):
                    # non posix systems store real file under file attr
                    content = content.file

            if not content:
                h.flash(_('No content'), category='warning')
                return redirect(
                    url('changeset_home',
                        repo_name=c.repo_name,
                        revision='tip'))
            if not filename:
                h.flash(_('No filename'), category='warning')
                return redirect(
                    url('changeset_home',
                        repo_name=c.repo_name,
                        revision='tip'))
            #strip all crap out of file, just leave the basename
            filename = os.path.basename(filename)
            node_path = os.path.join(location, filename)
            author = self.rhodecode_user.full_contact

            try:
                nodes = {node_path: {'content': content}}
                self.scm_model.create_nodes(
                    user=c.rhodecode_user.user_id,
                    repo=c.rhodecode_db_repo,
                    message=message,
                    nodes=nodes,
                    parent_cs=c.cs,
                    author=author,
                )

                h.flash(_('Successfully committed to %s') % node_path,
                        category='success')
            except NonRelativePathError, e:
                h.flash(_('Location must be relative path and must not '
                          'contain .. in path'),
                        category='warning')
                return redirect(
                    url('changeset_home',
                        repo_name=c.repo_name,
                        revision='tip'))
            except (NodeError, NodeAlreadyExistsError), e:
                h.flash(_(e), category='error')
Exemplo n.º 9
0
    def add(self, repo_name, revision, f_path):

        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(_('This repository is has been locked by %s on %s')
                % (h.person_by_id(repo.locked[0]),
                   h.fmt_date(h.time_to_datetime(repo.locked[1]))),
                  'warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip'))

        r_post = request.POST
        c.cs = self.__get_cs_or_redirect(revision, repo_name,
                                         redirect_after=False)
        if c.cs is None:
            c.cs = EmptyChangeset(alias=c.rhodecode_repo.alias)
        c.default_message = (_('Added file via RhodeCode'))
        c.f_path = f_path

        if r_post:
            unix_mode = 0
            content = convert_line_endings(r_post.get('content', ''), unix_mode)

            message = r_post.get('message') or c.default_message
            filename = r_post.get('filename')
            location = r_post.get('location', '')
            file_obj = r_post.get('upload_file', None)

            if file_obj is not None and hasattr(file_obj, 'filename'):
                filename = file_obj.filename
                content = file_obj.file

                if hasattr(content, 'file'):
                    # non posix systems store real file under file attr
                    content = content.file

            if not content:
                h.flash(_('No content'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            if not filename:
                h.flash(_('No filename'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            #strip all crap out of file, just leave the basename
            filename = os.path.basename(filename)
            node_path = os.path.join(location, filename)
            author = self.rhodecode_user.full_contact

            try:
                nodes = {
                    node_path: {
                        'content': content
                    }
                }
                self.scm_model.create_nodes(
                    user=c.rhodecode_user.user_id, repo=c.rhodecode_db_repo,
                    message=message,
                    nodes=nodes,
                    parent_cs=c.cs,
                    author=author,
                )

                h.flash(_('Successfully committed to %s') % node_path,
                        category='success')
            except NonRelativePathError, e:
                h.flash(_('Location must be relative path and must not '
                          'contain .. in path'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            except (NodeError, NodeAlreadyExistsError), e:
                h.flash(_(e), category='error')
Exemplo n.º 10
0
    def edit(self, repo_name, revision, f_path):
        repo = c.rhodecode_db_repo
        if repo.enable_locking and repo.locked[0]:
            h.flash(_('This repository is has been locked by %s on %s')
                % (h.person_by_id(repo.locked[0]),
                   h.fmt_date(h.time_to_datetime(repo.locked[1]))),
                'warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip'))

        # check if revision is a branch identifier- basically we cannot
        # create multiple heads via file editing
        _branches = repo.scm_instance.branches
        # check if revision is a branch name or branch hash
        if revision not in _branches.keys() + _branches.values():
            h.flash(_('You can only edit files with revision '
                      'being a valid branch '), category='warning')
            return redirect(h.url('files_home',
                                  repo_name=repo_name, revision='tip',
                                  f_path=f_path))

        r_post = request.POST

        c.cs = self.__get_cs_or_redirect(revision, repo_name)
        c.file = self.__get_filenode_or_redirect(repo_name, c.cs, f_path)

        if c.file.is_binary:
            return redirect(url('files_home', repo_name=c.repo_name,
                            revision=c.cs.raw_id, f_path=f_path))
        c.default_message = _('Edited file %s via RhodeCode') % (f_path)
        c.f_path = f_path

        if r_post:

            old_content = c.file.content
            sl = old_content.splitlines(1)
            first_line = sl[0] if sl else ''
            # modes:  0 - Unix, 1 - Mac, 2 - DOS
            mode = detect_mode(first_line, 0)
            content = convert_line_endings(r_post.get('content', ''), mode)

            message = r_post.get('message') or c.default_message
            author = self.rhodecode_user.full_contact

            if content == old_content:
                h.flash(_('No changes'), category='warning')
                return redirect(url('changeset_home', repo_name=c.repo_name,
                                    revision='tip'))
            try:
                self.scm_model.commit_change(repo=c.rhodecode_repo,
                                             repo_name=repo_name, cs=c.cs,
                                             user=self.rhodecode_user.user_id,
                                             author=author, message=message,
                                             content=content, f_path=f_path)
                h.flash(_('Successfully committed to %s') % f_path,
                        category='success')
            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during commit'), category='error')
            return redirect(url('changeset_home',
                                repo_name=c.repo_name, revision='tip'))

        return render('files/files_edit.html')
Exemplo n.º 11
0
    def add(self, repo_name, revision, f_path):

        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(
                _("This repository is has been locked by %s on %s")
                % (h.person_by_id(repo.locked[0]), h.fmt_date(h.time_to_datetime(repo.locked[1]))),
                "warning",
            )
            return redirect(h.url("files_home", repo_name=repo_name, revision="tip"))

        r_post = request.POST
        c.cs = self.__get_cs_or_redirect(revision, repo_name, redirect_after=False)
        if c.cs is None:
            c.cs = EmptyChangeset(alias=c.rhodecode_repo.alias)
        c.default_message = _("Added file via RhodeCode")
        c.f_path = f_path

        if r_post:
            unix_mode = 0
            content = convert_line_endings(r_post.get("content"), unix_mode)

            message = r_post.get("message") or c.default_message
            filename = r_post.get("filename")
            location = r_post.get("location")
            file_obj = r_post.get("upload_file", None)

            if file_obj is not None and hasattr(file_obj, "filename"):
                filename = file_obj.filename
                content = file_obj.file

            if not content:
                h.flash(_("No content"), category="warning")
                return redirect(url("changeset_home", repo_name=c.repo_name, revision="tip"))
            if not filename:
                h.flash(_("No filename"), category="warning")
                return redirect(url("changeset_home", repo_name=c.repo_name, revision="tip"))
            if location.startswith("/") or location.startswith(".") or "../" in location:
                h.flash(_("Location must be relative path and must not " "contain .. in path"), category="warning")
                return redirect(url("changeset_home", repo_name=c.repo_name, revision="tip"))
            if location:
                location = os.path.normpath(location)
            filename = os.path.basename(filename)
            node_path = os.path.join(location, filename)
            author = self.rhodecode_user.full_contact

            try:
                self.scm_model.create_node(
                    repo=c.rhodecode_repo,
                    repo_name=repo_name,
                    cs=c.cs,
                    user=self.rhodecode_user.user_id,
                    author=author,
                    message=message,
                    content=content,
                    f_path=node_path,
                )
                h.flash(_("Successfully committed to %s") % node_path, category="success")
            except (NodeError, NodeAlreadyExistsError), e:
                h.flash(_(e), category="error")
            except Exception:
                log.error(traceback.format_exc())
                h.flash(_("Error occurred during commit"), category="error")
Exemplo n.º 12
0
    def add(self, repo_name, revision, f_path):
        repo = Repository.get_by_repo_name(repo_name)
        if repo.enable_locking and repo.locked[0]:
            h.flash(
                _('This repository has been locked by %s on %s') %
                (h.person_by_id(repo.locked[0]),
                 h.format_date(h.time_to_datetime(repo.locked[1]))), 'warning')
            return redirect(
                h.url('files_home', repo_name=repo_name, revision='tip'))

        r_post = request.POST

        c.commit = self.__get_commit_or_redirect(revision,
                                                 repo_name,
                                                 redirect_after=False)
        if c.commit is None:
            c.commit = EmptyCommit(alias=c.rhodecode_repo.alias)
        c.default_message = (_('Added file via RhodeCode Enterprise'))
        c.f_path = f_path
        unix_mode = 0
        content = convert_line_endings(r_post.get('content', ''), unix_mode)

        message = r_post.get('message') or c.default_message
        filename = r_post.get('filename')
        location = r_post.get('location', '')  # dir location
        file_obj = r_post.get('upload_file', None)

        if file_obj is not None and hasattr(file_obj, 'filename'):
            filename = file_obj.filename
            content = file_obj.file

            if hasattr(content, 'file'):
                # non posix systems store real file under file attr
                content = content.file

        # If there's no commit, redirect to repo summary
        if type(c.commit) is EmptyCommit:
            redirect_url = "summary_home"
        else:
            redirect_url = "changeset_home"

        if not filename:
            h.flash(_('No filename'), category='warning')
            return redirect(
                url(redirect_url, repo_name=c.repo_name, revision='tip'))

        # extract the location from filename,
        # allows using foo/bar.txt syntax to create subdirectories
        subdir_loc = filename.rsplit('/', 1)
        if len(subdir_loc) == 2:
            location = os.path.join(location, subdir_loc[0])

        # strip all crap out of file, just leave the basename
        filename = os.path.basename(filename)
        node_path = os.path.join(location, filename)
        author = c.rhodecode_user.full_contact

        try:
            nodes = {node_path: {'content': content}}
            self.scm_model.create_nodes(
                user=c.rhodecode_user.user_id,
                repo=c.rhodecode_db_repo,
                message=message,
                nodes=nodes,
                parent_commit=c.commit,
                author=author,
            )

            h.flash(_('Successfully committed to %s') % node_path,
                    category='success')
        except NonRelativePathError as e:
            h.flash(_(
                'The location specified must be a relative path and must not '
                'contain .. in the path'),
                    category='warning')
            return redirect(
                url('changeset_home', repo_name=c.repo_name, revision='tip'))
        except (NodeError, NodeAlreadyExistsError) as e:
            h.flash(_(e), category='error')
        except Exception:
            msg = _('Error occurred during commit')
            log.exception(msg)
            h.flash(msg, category='error')
        return redirect(
            url('changeset_home', repo_name=c.repo_name, revision='tip'))
Exemplo n.º 13
0
    def edit(self, repo_name, revision, f_path):
        commit_id = revision

        repo = c.rhodecode_db_repo
        if repo.enable_locking and repo.locked[0]:
            h.flash(
                _('This repository has been locked by %s on %s') %
                (h.person_by_id(repo.locked[0]),
                 h.format_date(h.time_to_datetime(repo.locked[1]))), 'warning')
            return redirect(
                h.url('files_home', repo_name=repo_name, revision='tip'))

        if not self._is_valid_head(commit_id, repo.scm_instance()):
            h.flash(_('You can only edit files with revision '
                      'being a valid branch '),
                    category='warning')
            return redirect(
                h.url('files_home',
                      repo_name=repo_name,
                      revision='tip',
                      f_path=f_path))

        c.commit = self.__get_commit_or_redirect(commit_id, repo_name)
        c.file = self.__get_filenode_or_redirect(repo_name, c.commit, f_path)

        if c.file.is_binary:
            return redirect(
                url('files_home',
                    repo_name=c.repo_name,
                    revision=c.commit.raw_id,
                    f_path=f_path))
        c.default_message = _('Edited file %s via RhodeCode Enterprise') % (
            f_path)
        c.f_path = f_path
        old_content = c.file.content
        sl = old_content.splitlines(1)
        first_line = sl[0] if sl else ''

        # modes:  0 - Unix, 1 - Mac, 2 - DOS
        mode = detect_mode(first_line, 0)
        content = convert_line_endings(request.POST.get('content', ''), mode)

        message = request.POST.get('message') or c.default_message
        org_f_path = c.file.unicode_path
        filename = request.POST['filename']
        org_filename = c.file.name

        if content == old_content and filename == org_filename:
            h.flash(_('No changes'), category='warning')
            return redirect(
                url('changeset_home', repo_name=c.repo_name, revision='tip'))
        try:
            mapping = {
                org_f_path: {
                    'org_filename': org_f_path,
                    'filename': os.path.join(c.file.dir_path, filename),
                    'content': content,
                    'lexer': '',
                    'op': 'mod',
                }
            }

            ScmModel().update_nodes(
                user=c.rhodecode_user.user_id,
                repo=c.rhodecode_db_repo,
                message=message,
                nodes=mapping,
                parent_commit=c.commit,
            )

            h.flash(_('Successfully committed to %s') % f_path,
                    category='success')
        except Exception:
            msg = _('Error occurred during commit')
            log.exception(msg)
            h.flash(msg, category='error')
        return redirect(
            url('changeset_home', repo_name=c.repo_name, revision='tip'))