# All files / src git.ts

5.49% Statements 5/91
2.32% Branches 1/43
11.76% Functions 2/17
6.25% Lines 5/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215

    3x

    163x
    163x

    163x
    163x

     import { simpleGit, SimpleGit } from 'simple-git';
    import { addCommit } from './db';

    // Session-scoped git instances and commit tracking
    interface GitSession {
      git: SimpleGit;
      lastCommitHash: string | null;
      interval?: NodeJS.Timeout;
    }

    const sessions = new Map<number, GitSession>();

    export function initGit(sessionId: number, cwd: string): void {
      sessions.set(sessionId, {
        git: simpleGit(cwd),
        lastCommitHash: null,
      });
    }

    export async function checkForNewCommits(sessionId: number): Promise<void> {
      const session = sessions.get(sessionId);
      if (!session) return;

      try {
        const log = await session.git.log({ maxCount: 1 });
        if (log.latest && log.latest.hash !== session.lastCommitHash) {
          session.lastCommitHash = log.latest.hash;

          addCommit({
            sessionId,
            hash: log.latest.hash.substring(0, 7),
            message: log.latest.message,
            timestamp: new Date().toISOString(),
          });
        }
      } catch (error) {
        // Not a git repo or no commits yet
      }
    }

    export async function getGitInfo(sessionId: number): Promise<{ branch: string; hasChanges: boolean } | null> {
      const session = sessions.get(sessionId);
      if (!session) return null;

      try {
        const branch = await session.git.revparse(['--abbrev-ref', 'HEAD']);
        const status = await session.git.status();
        return {
          branch: branch.trim(),
          hasChanges: !status.isClean(),
        };
      } catch (error) {
        return null;
      }
    }

    export function startGitPolling(sessionId: number, intervalMs: number = 10000): void {
      const session = sessions.get(sessionId);
      if (!session) return;

      // Clear existing interval if any
      if (session.interval) {
        clearInterval(session.interval);
      }

      // Start new polling interval
      session.interval = setInterval(async () => {
        await checkForNewCommits(sessionId);
      }, intervalMs);
    }

    export function stopGitPolling(sessionId: number): void {
      const session = sessions.get(sessionId);
      Iif (session?.interval) {
        clearInterval(session.interval);
        session.interval = undefined;
      }
    }

    export function cleanupGit(sessionId: number): void {
      stopGitPolling(sessionId);
      sessions.delete(sessionId);
    }

    /**
     * Get the root directory of the current git repository.
     */
    export async function getGitRoot(cwd: string): Promise<string | null> {
      try {
        const g = simpleGit(cwd);
        const root = await g.revparse(['--show-toplevel']);
        return root.trim();
      } catch (_) {
        return null;
      }
    }

    /**
     * Get the current HEAD SHA (full hash).
     */
    export async function getGitHead(cwd: string): Promise<string | null> {
      try {
        const g = simpleGit(cwd);
        const head = await g.revparse(['HEAD']);
        return head.trim();
      } catch (_) {
        return null;
      }
    }

    /**
     * Get files changed between a start SHA and HEAD via `git diff --name-status`.
     * Returns an array of { filePath, changeType }.
     */
    export async function getGitDiffFiles(cwd: string, fromHead: string): Promise<{ filePath: string; changeType: 'created' | 'modified' | 'deleted' }[]> {
      try {
        const g = simpleGit(cwd);
        const diff = await g.diff(['--name-status', `${fromHead}..HEAD`]);
        if (!diff.trim()) return [];
        return diff.trim().split('\n').map((line) => {
          const [status, ...pathParts] = line.split('\t');
          const filePath = pathParts.join('\t');
          let changeType: 'created' | 'modified' | 'deleted' = 'modified';
          if (status.startsWith('A')) changeType = 'created';
          else if (status.startsWith('D')) changeType = 'deleted';
          else if (status.startsWith('R')) changeType = 'modified'; // rename
          return { filePath, changeType };
        });
      } catch (_) {
        return [];
      }
    }

    /**
     * Get commits between a start SHA and HEAD via `git log`.
     * Returns array of { hash, message, timestamp }.
     */
    export async function getGitLogCommits(cwd: string, fromHead: string): Promise<{ hash: string; message: string; timestamp: string }[]> {
      try {
        const g = simpleGit(cwd);
        const log = await g.log({ from: fromHead, to: 'HEAD' });
        return log.all.map((entry) => ({
          hash: entry.hash.substring(0, 7),
          message: entry.message,
          timestamp: entry.date || new Date().toISOString(),
        }));
      } catch (_) {
        return [];
      }
    }

    /**
     * Get unified diff between two SHAs (or SHA..HEAD).
     * If filePath is provided, returns diff for that file only.
     */
    export async function getGitDiff(
      cwd: string,
      fromSha: string,
      toSha: string | null,
      filePath?: string,
    ): Promise<string> {
      try {
        const g = simpleGit(cwd);
        const range = toSha ? `${fromSha}..${toSha}` : `${fromSha}..HEAD`;
        const args = ['diff', '--unified=5', range];
        if (filePath) args.push('--', filePath);
        const result = await g.raw(args);
        return result;
      } catch (_) {
        return '';
      }
    }

    /**
     * Get diff for a single commit.
     */
    export async function getCommitDiff(cwd: string, hash: string, filePath?: string): Promise<string> {
      try {
        const g = simpleGit(cwd);
        const args = ['diff', '--unified=5', `${hash}~1`, hash];
        if (filePath) args.push('--', filePath);
        const result = await g.raw(args);
        return result;
      } catch (_) {
        return '';
      }
    }

    /**
     * Get per-file diff stats (additions/deletions) between two SHAs.
     * Uses `git diff --numstat`.
     */
    export async function getGitDiffStats(
      cwd: string,
      fromSha: string,
      toSha: string | null,
    ): Promise<{ filePath: string; additions: number; deletions: number }[]> {
      try {
        const g = simpleGit(cwd);
        const range = toSha ? `${fromSha}..${toSha}` : `${fromSha}..HEAD`;
        const result = await g.raw(['diff', '--numstat', range]);
        if (!result.trim()) return [];
        return result.trim().split('\n').map((line) => {
          const [add, del, ...pathParts] = line.split('\t');
          return {
            filePath: pathParts.join('\t'),
            additions: add === '-' ? 0 : parseInt(add) || 0,
            deletions: del === '-' ? 0 : parseInt(del) || 0,
          };
        });
      } catch (_) {
        return [];
      }
    }