def commit():
    app = request.args[0]
    path = apath(app, r=request)

    uio = ui.ui()
    uio.quiet = True
    if not os.environ.get('HGUSER') and not uio.config("ui", "username"):
        os.environ['HGUSER'] = '******'
    try:
        r = hg.repository(ui=uio, path=path)
    except:
        r = hg.repository(ui=uio, path=path, create=True)
    hgignore = os.path.join(path, '.hgignore')
    if not os.path.exists(hgignore):
        open(hgignore, 'w').write(_hgignore_content)
    form = FORM('Comment:',INPUT(_name='comment',requires=IS_NOT_EMPTY()),
                INPUT(_type='submit',_value='Commit'))
    if form.accepts(request.vars,session):
        oldid = r[r.lookup('.')]
        cmdutil.addremove(r)
        r.commit(text=form.vars.comment)
        if r[r.lookup('.')] == oldid:
            response.flash = 'no changes' 
    files = r[r.lookup('.')].files()
    return dict(form=form,files=TABLE(*[TR(file) for file in files]),repo=r)
def create_repo_for_project(db, project_id, project_identifier):
    """Creates Repository for the given project
    """
    log.debug("Creating Repo for %s" % project_identifier)

    repo_path = os.path.join(repo_root, project_identifier)

    if not os.path.exists(repo_path):
        #Create the directory
        os.makedirs(repo_path)

        #Create the repository
        r = hg.repository(ui=ui.ui(), path=repo_path, create=True)

        #Create .hgignore
        hgignore = os.path.join(repo_path, '.hgignore')
        open(hgignore, 'w').write(_hgignore_content)

        #Create hgrc
        hgrc = os.path.join(repo_path, '.hg', 'hgrc')
        open(hgrc, 'w').write(_hgrc_content)

        #Add .hgignore to repo and commit
        cmdutil.addremove(r)
        r.commit(text="Created Repository")


    #Insert a record into repositories table
    c = db.cursor()
    c.execute("insert into repositories(project_id, url, root_url, type) "
              "values ('%s', '%s', '%s', 'Mercurial')"
              % (project_id, repo_path, repo_path)
             )
    db.commit()
    log.debug("Inserted a record into repositories table with "
              "project_id = %s and url = %s" % (project_id, repo_path)
             )