Skip to content
Merged
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
37 changes: 16 additions & 21 deletions src/sdk/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,15 @@ export interface GitInfo {
*/
function isGitRepo(directory: string = process.cwd()): boolean {
try {
// Check if .git directory exists or if git rev-parse succeeds
execSync('git rev-parse --is-inside-work-tree', { cwd: directory, encoding: 'utf-8', stdio: 'ignore' });
return true;
// Use git rev-parse --is-inside-work-tree instead of file I/O
const result = execSync('git rev-parse --is-inside-work-tree', {
cwd: directory,
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore'] // Suppress stderr
}).trim();
return result === 'true';
} catch (error) {
// Return false if git command fails (not a git repo or git not installed)
return false;
}
}
Expand All @@ -127,16 +132,16 @@ export function getGitInfo(directory: string = process.cwd()): GitInfo {
error: "Telemetry disabled"
};
}

// First check if this is a git repository
if (!isGitRepo(directory)) {
return {
uncommittedChanges: false,
error: "Not a git repository"
};
}

try {
// First check if this is a git repository
if (!isGitRepo(directory)) {
return {
uncommittedChanges: false,
error: "Not a git repository"
};
}

const commitHash = execSync('git rev-parse HEAD', { cwd: directory, encoding: 'utf-8' }).trim();
const branch = execSync('git rev-parse --abbrev-ref HEAD', { cwd: directory, encoding: 'utf-8' }).trim();
const repoUrl = execSync('git config --get remote.origin.url', { cwd: directory, encoding: 'utf-8' }).trim().replace(/\.git$/, '');
Expand Down Expand Up @@ -395,16 +400,6 @@ export class HoneyHiveTracer {
sessionId: this.sessionId
}
};

// Gather git information
const gitInfo = getGitInfo();

// Only add git info to metadata if there's no error
if (!gitInfo.error && requestBody.session) {
requestBody.session.metadata = {
git: gitInfo
};
}

if (!HoneyHiveTracer.sdkInstance) {
throw new Error("SDK instance is not initialized");
Expand Down