|
| 1 | +// src/app/api/native/fine-tune/git/branches/route.ts |
| 2 | +import { NextResponse } from 'next/server'; |
| 3 | +import * as git from 'isomorphic-git'; |
| 4 | +import fs from 'fs'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +const REMOTE_TAXONOMY_ROOT_DIR = process.env.NEXT_PUBLIC_TAXONOMY_ROOT_DIR || ''; |
| 8 | + |
| 9 | +export async function GET() { |
| 10 | + const REPO_DIR = path.join(REMOTE_TAXONOMY_ROOT_DIR, '/taxonomy'); |
| 11 | + try { |
| 12 | + console.log(`Checking local taxonomy directory for branches: ${REPO_DIR}`); |
| 13 | + |
| 14 | + // Ensure the repository path exists |
| 15 | + if (!fs.existsSync(REPO_DIR)) { |
| 16 | + console.log('Local repository path does not exist:', REPO_DIR); |
| 17 | + return NextResponse.json({ error: 'Local repository path does not exist.' }, { status: 400 }); |
| 18 | + } |
| 19 | + |
| 20 | + console.log('Local taxonomy directory exists. Proceeding with branch listing.'); |
| 21 | + |
| 22 | + // List all branches in the repository |
| 23 | + const branches = await git.listBranches({ fs, dir: REPO_DIR }); |
| 24 | + console.log(`Branches found: ${branches.join(', ')}`); |
| 25 | + |
| 26 | + const branchDetails = []; |
| 27 | + |
| 28 | + for (const branch of branches) { |
| 29 | + const branchCommit = await git.resolveRef({ |
| 30 | + fs, |
| 31 | + dir: REPO_DIR, |
| 32 | + ref: branch |
| 33 | + }); |
| 34 | + const commitDetails = await git.readCommit({ |
| 35 | + fs, |
| 36 | + dir: REPO_DIR, |
| 37 | + oid: branchCommit |
| 38 | + }); |
| 39 | + |
| 40 | + const commitMessage = commitDetails.commit.message; |
| 41 | + |
| 42 | + // Check for Signed-off-by line |
| 43 | + const signoffMatch = commitMessage.match(/^Signed-off-by: (.+)$/m); |
| 44 | + const signoff = signoffMatch ? signoffMatch[1] : null; |
| 45 | + const messageStr = commitMessage.split('Signed-off-by'); |
| 46 | + |
| 47 | + branchDetails.push({ |
| 48 | + name: branch, |
| 49 | + creationDate: commitDetails.commit.committer.timestamp * 1000, |
| 50 | + message: messageStr[0].replace(/\n+$/, ''), |
| 51 | + author: signoff |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + // Sort by creation date, newest first |
| 56 | + branchDetails.sort((a, b) => b.creationDate - a.creationDate); |
| 57 | + |
| 58 | + console.log('Total branches present in native taxonomy (fine-tune):', branchDetails.length); |
| 59 | + |
| 60 | + return NextResponse.json({ branches: branchDetails }, { status: 200 }); |
| 61 | + } catch (error) { |
| 62 | + console.error('Failed to list branches from local taxonomy (fine-tune):', error); |
| 63 | + return NextResponse.json({ error: 'Failed to list branches from local taxonomy (fine-tune)' }, { status: 500 }); |
| 64 | + } |
| 65 | +} |
0 commit comments