git-diff-ai-reviewer 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/review.js CHANGED
@@ -3,7 +3,7 @@
3
3
  require('dotenv').config();
4
4
  const path = require('path');
5
5
  const fs = require('fs');
6
- const { getDiff, getChangedFiles, getBranchName, isGitRepo } = require('../src/git');
6
+ const { getDiff, getChangedFiles, getBranchName, isGitRepo, resolveBaseRef } = require('../src/git');
7
7
  const { reviewCode, parseSeverityCounts, detectProvider, PROVIDERS, DEFAULT_MODELS } = require('../src/provider');
8
8
  const { buildFixPrompt } = require('../src/prompts');
9
9
  const { loadConfig } = require('../src/config');
@@ -149,8 +149,12 @@ async function commandReview(config, flags) {
149
149
 
150
150
  // Get branch and diff info
151
151
  const branchName = getBranchName();
152
+ const { ref: effectiveBase, isRemote } = resolveBaseRef(baseBranch);
152
153
  console.log(` Branch: ${branchName}`);
153
- console.log(` Base: ${baseBranch}`);
154
+ console.log(` Base: ${effectiveBase}`);
155
+ if (!isRemote) {
156
+ console.log(` ⚠ Remote origin/${baseBranch} not found — using local ref`);
157
+ }
154
158
 
155
159
  const changedFiles = getChangedFiles(baseBranch);
156
160
  if (changedFiles.length === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-diff-ai-reviewer",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "AI-powered code review using Claude or Gemini API. Reviews git branch diffs and generates actionable fix prompts.",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/git.js CHANGED
@@ -1,22 +1,48 @@
1
1
  const { execSync } = require('child_process');
2
2
 
3
3
  /**
4
- * Get the diff between the current branch and a base branch.
4
+ * Resolve the effective base ref for diff comparison.
5
+ * Always prefers the remote version of the base branch (e.g. origin/main)
6
+ * so the diff reflects what will actually change on the remote after push.
7
+ * Falls back to the local branch name when the remote ref doesn't exist
8
+ * (e.g. first push of a new repo).
9
+ * @param {string} baseBranch - The configured base branch name
10
+ * @param {string} cwd - Working directory
11
+ * @returns {{ ref: string, isRemote: boolean }} The ref to diff against
12
+ */
13
+ function resolveBaseRef(baseBranch, cwd = process.cwd()) {
14
+ // Try origin/<baseBranch> first
15
+ try {
16
+ execSync(`git rev-parse --verify origin/${baseBranch}`, {
17
+ cwd,
18
+ encoding: 'utf-8',
19
+ stdio: ['pipe', 'pipe', 'pipe'],
20
+ });
21
+ return { ref: `origin/${baseBranch}`, isRemote: true };
22
+ } catch {
23
+ // Remote ref doesn't exist — fall back to local branch
24
+ return { ref: baseBranch, isRemote: false };
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Get the diff between the current branch and the remote base branch.
5
30
  * @param {string} baseBranch - The base branch to compare against (default: 'main')
6
31
  * @param {string} cwd - Working directory (default: process.cwd())
7
32
  * @returns {string} The git diff output
8
33
  */
9
34
  function getDiff(baseBranch = 'main', cwd = process.cwd()) {
10
35
  try {
11
- // First try three-dot diff (for branch comparison)
12
- const diff = execSync(`git diff ${baseBranch}...HEAD`, {
36
+ const { ref } = resolveBaseRef(baseBranch, cwd);
37
+
38
+ const diff = execSync(`git diff ${ref}...HEAD`, {
13
39
  cwd,
14
40
  encoding: 'utf-8',
15
- maxBuffer: 10 * 1024 * 1024, // 10MB buffer for large diffs
41
+ maxBuffer: 10 * 1024 * 1024,
16
42
  });
17
43
 
18
44
  if (!diff.trim()) {
19
- // Fallback: try two-dot diff (for uncommitted changes)
45
+ // Fallback: uncommitted changes
20
46
  const uncommitted = execSync('git diff HEAD', {
21
47
  cwd,
22
48
  encoding: 'utf-8',
@@ -24,7 +50,6 @@ function getDiff(baseBranch = 'main', cwd = process.cwd()) {
24
50
  });
25
51
 
26
52
  if (!uncommitted.trim()) {
27
- // Also check staged changes
28
53
  const staged = execSync('git diff --cached', {
29
54
  cwd,
30
55
  encoding: 'utf-8',
@@ -42,14 +67,16 @@ function getDiff(baseBranch = 'main', cwd = process.cwd()) {
42
67
  }
43
68
 
44
69
  /**
45
- * Get list of changed files between current branch and base branch.
70
+ * Get list of changed files between current branch and the remote base branch.
46
71
  * @param {string} baseBranch - The base branch to compare against
47
72
  * @param {string} cwd - Working directory
48
73
  * @returns {string[]} Array of changed file paths
49
74
  */
50
75
  function getChangedFiles(baseBranch = 'main', cwd = process.cwd()) {
51
76
  try {
52
- const output = execSync(`git diff --name-only ${baseBranch}...HEAD`, {
77
+ const { ref } = resolveBaseRef(baseBranch, cwd);
78
+
79
+ const output = execSync(`git diff --name-only ${ref}...HEAD`, {
53
80
  cwd,
54
81
  encoding: 'utf-8',
55
82
  });
@@ -110,4 +137,5 @@ module.exports = {
110
137
  getChangedFiles,
111
138
  getBranchName,
112
139
  isGitRepo,
140
+ resolveBaseRef,
113
141
  };