Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/api/tasks/[taskId]/create-file/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from '@/lib/db/client'
import { tasks } from '@/lib/db/schema'
import { eq, and, isNull } from 'drizzle-orm'
import { getServerSession } from '@/lib/session/get-server-session'
import { PROJECT_DIR } from '@/lib/sandbox/commands'

export async function POST(request: NextRequest, { params }: { params: Promise<{ taskId: string }> }) {
try {
Expand Down Expand Up @@ -65,7 +66,11 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
const pathParts = filename.split('/')
if (pathParts.length > 1) {
const dirPath = pathParts.slice(0, -1).join('/')
const mkdirResult = await sandbox.runCommand('mkdir', ['-p', dirPath])
const mkdirResult = await sandbox.runCommand({
cmd: 'mkdir',
args: ['-p', dirPath],
cwd: PROJECT_DIR,
})

if (mkdirResult.exitCode !== 0) {
const stderr = await mkdirResult.stderr()
Expand All @@ -75,7 +80,11 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
}

// Create the file using touch
const touchResult = await sandbox.runCommand('touch', [filename])
const touchResult = await sandbox.runCommand({
cmd: 'touch',
args: [filename],
cwd: PROJECT_DIR,
})

if (touchResult.exitCode !== 0) {
const stderr = await touchResult.stderr()
Expand Down
7 changes: 6 additions & 1 deletion app/api/tasks/[taskId]/create-folder/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from '@/lib/db/client'
import { tasks } from '@/lib/db/schema'
import { eq, and, isNull } from 'drizzle-orm'
import { getServerSession } from '@/lib/session/get-server-session'
import { PROJECT_DIR } from '@/lib/sandbox/commands'

export async function POST(request: NextRequest, { params }: { params: Promise<{ taskId: string }> }) {
try {
Expand Down Expand Up @@ -61,7 +62,11 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
}

// Create the folder using mkdir -p
const mkdirResult = await sandbox.runCommand('mkdir', ['-p', foldername])
const mkdirResult = await sandbox.runCommand({
cmd: 'mkdir',
args: ['-p', foldername],
cwd: PROJECT_DIR,
})

if (mkdirResult.exitCode !== 0) {
const stderr = await mkdirResult.stderr()
Expand Down
7 changes: 6 additions & 1 deletion app/api/tasks/[taskId]/delete-file/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from '@/lib/db/client'
import { tasks } from '@/lib/db/schema'
import { eq, and, isNull } from 'drizzle-orm'
import { getServerSession } from '@/lib/session/get-server-session'
import { PROJECT_DIR } from '@/lib/sandbox/commands'

export async function DELETE(request: NextRequest, { params }: { params: Promise<{ taskId: string }> }) {
try {
Expand Down Expand Up @@ -61,7 +62,11 @@ export async function DELETE(request: NextRequest, { params }: { params: Promise
}

// Delete the file using rm
const rmResult = await sandbox.runCommand('rm', [filename])
const rmResult = await sandbox.runCommand({
cmd: 'rm',
args: [filename],
cwd: PROJECT_DIR,
})

if (rmResult.exitCode !== 0) {
const stderr = await rmResult.stderr()
Expand Down
43 changes: 36 additions & 7 deletions app/api/tasks/[taskId]/diff/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { tasks } from '@/lib/db/schema'
import { eq, and, isNull } from 'drizzle-orm'
import { getOctokit } from '@/lib/github/client'
import { getServerSession } from '@/lib/session/get-server-session'
import { PROJECT_DIR } from '@/lib/sandbox/commands'
import type { Octokit } from '@octokit/rest'

function getLanguageFromFilename(filename: string): string {
Expand Down Expand Up @@ -204,26 +205,42 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
}

// Fetch latest from remote to ensure we have up-to-date remote refs
const fetchResult = await sandbox.runCommand('git', ['fetch', 'origin', task.branchName])
const fetchResult = await sandbox.runCommand({
cmd: 'git',
args: ['fetch', 'origin', task.branchName],
cwd: PROJECT_DIR,
})

// Check if remote branch actually exists (even if fetch succeeds, the branch might not exist)
const remoteBranchRef = `origin/${task.branchName}`
const checkRemoteResult = await sandbox.runCommand('git', ['rev-parse', '--verify', remoteBranchRef])
const checkRemoteResult = await sandbox.runCommand({
cmd: 'git',
args: ['rev-parse', '--verify', remoteBranchRef],
cwd: PROJECT_DIR,
})
const remoteBranchExists = checkRemoteResult.exitCode === 0

if (!remoteBranchExists) {
// Remote branch doesn't exist yet, compare against HEAD (local changes only)

// Get old content (HEAD version)
const oldContentResult = await sandbox.runCommand('git', ['show', `HEAD:${filename}`])
const oldContentResult = await sandbox.runCommand({
cmd: 'git',
args: ['show', `HEAD:${filename}`],
cwd: PROJECT_DIR,
})
let oldContent = ''
if (oldContentResult.exitCode === 0) {
oldContent = await oldContentResult.stdout()
}
// File might not exist in HEAD (new file)

// Get new content (working directory version)
const newContentResult = await sandbox.runCommand('cat', [filename])
const newContentResult = await sandbox.runCommand({
cmd: 'cat',
args: [filename],
cwd: PROJECT_DIR,
})
const newContent = newContentResult.exitCode === 0 ? await newContentResult.stdout() : ''

return NextResponse.json({
Expand All @@ -241,7 +258,11 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{

// Compare working directory against remote branch
// This shows all uncommitted AND unpushed changes
const diffResult = await sandbox.runCommand('git', ['diff', remoteBranchRef, filename])
const diffResult = await sandbox.runCommand({
cmd: 'git',
args: ['diff', remoteBranchRef, filename],
cwd: PROJECT_DIR,
})

if (diffResult.exitCode !== 0) {
const diffError = await diffResult.stderr()
Expand All @@ -252,15 +273,23 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
const diffOutput = await diffResult.stdout()

// Get old content (remote branch version)
const oldContentResult = await sandbox.runCommand('git', ['show', `${remoteBranchRef}:${filename}`])
const oldContentResult = await sandbox.runCommand({
cmd: 'git',
args: ['show', `${remoteBranchRef}:${filename}`],
cwd: PROJECT_DIR,
})
let oldContent = ''
if (oldContentResult.exitCode === 0) {
oldContent = await oldContentResult.stdout()
}
// File might not exist on remote (new file)

// Get new content (working directory version)
const newContentResult = await sandbox.runCommand('cat', [filename])
const newContentResult = await sandbox.runCommand({
cmd: 'cat',
args: [filename],
cwd: PROJECT_DIR,
})
const newContent = newContentResult.exitCode === 0 ? await newContentResult.stdout() : ''

return NextResponse.json({
Expand Down
19 changes: 16 additions & 3 deletions app/api/tasks/[taskId]/discard-file-changes/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from '@/lib/db/client'
import { tasks } from '@/lib/db/schema'
import { eq, and, isNull } from 'drizzle-orm'
import { getServerSession } from '@/lib/session/get-server-session'
import { PROJECT_DIR } from '@/lib/sandbox/commands'

export async function POST(request: NextRequest, { params }: { params: Promise<{ taskId: string }> }) {
try {
Expand Down Expand Up @@ -61,12 +62,20 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
}

// Check if file is tracked in git
const lsFilesResult = await sandbox.runCommand('git', ['ls-files', filename])
const lsFilesResult = await sandbox.runCommand({
cmd: 'git',
args: ['ls-files', filename],
cwd: PROJECT_DIR,
})
const isTracked = (await lsFilesResult.stdout()).trim().length > 0

if (isTracked) {
// File is tracked, use git checkout to revert changes
const checkoutResult = await sandbox.runCommand('git', ['checkout', 'HEAD', '--', filename])
const checkoutResult = await sandbox.runCommand({
cmd: 'git',
args: ['checkout', 'HEAD', '--', filename],
cwd: PROJECT_DIR,
})

if (checkoutResult.exitCode !== 0) {
const stderr = await checkoutResult.stderr()
Expand All @@ -75,7 +84,11 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
}
} else {
// File is untracked (new file), delete it
const rmResult = await sandbox.runCommand('rm', [filename])
const rmResult = await sandbox.runCommand({
cmd: 'rm',
args: [filename],
cwd: PROJECT_DIR,
})

if (rmResult.exitCode !== 0) {
const stderr = await rmResult.stderr()
Expand Down
7 changes: 6 additions & 1 deletion app/api/tasks/[taskId]/file-content/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { tasks } from '@/lib/db/schema'
import { eq, and, isNull } from 'drizzle-orm'
import { getOctokit } from '@/lib/github/client'
import { getServerSession } from '@/lib/session/get-server-session'
import { PROJECT_DIR } from '@/lib/sandbox/commands'
import type { Octokit } from '@octokit/rest'

function getLanguageFromFilename(filename: string): string {
Expand Down Expand Up @@ -255,7 +256,11 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
if (sandbox) {
// Read file from sandbox
const normalizedPath = filename.startsWith('/') ? filename.substring(1) : filename
const catResult = await sandbox.runCommand('cat', [normalizedPath])
const catResult = await sandbox.runCommand({
cmd: 'cat',
args: [normalizedPath],
cwd: PROJECT_DIR,
})

if (catResult.exitCode === 0) {
newContent = await catResult.stdout()
Expand Down
13 changes: 11 additions & 2 deletions app/api/tasks/[taskId]/file-operation/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from '@/lib/db/client'
import { tasks } from '@/lib/db/schema'
import { eq, and, isNull } from 'drizzle-orm'
import { getServerSession } from '@/lib/session/get-server-session'
import { PROJECT_DIR } from '@/lib/sandbox/commands'

export async function POST(request: NextRequest, { params }: { params: Promise<{ taskId: string }> }) {
try {
Expand Down Expand Up @@ -67,7 +68,11 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{

if (operation === 'copy') {
// Copy file
const copyResult = await sandbox.runCommand('cp', ['-r', sourceFile, targetFile])
const copyResult = await sandbox.runCommand({
cmd: 'cp',
args: ['-r', sourceFile, targetFile],
cwd: PROJECT_DIR,
})

if (copyResult.exitCode !== 0) {
const stderr = await copyResult.stderr()
Expand All @@ -78,7 +83,11 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
return NextResponse.json({ success: true, message: 'File copied successfully' })
} else if (operation === 'cut') {
// Move file
const mvResult = await sandbox.runCommand('mv', [sourceFile, targetFile])
const mvResult = await sandbox.runCommand({
cmd: 'mv',
args: [sourceFile, targetFile],
cwd: PROJECT_DIR,
})

if (mvResult.exitCode !== 0) {
const stderr = await mvResult.stderr()
Expand Down
Loading
Loading