|
| 1 | +# mcp-codebase-index - Structural codebase indexer with MCP server |
| 2 | +# Copyright (C) 2026 Michael Doyle |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU Affero General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU Affero General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Affero General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | +# Commercial licensing available. See COMMERCIAL-LICENSE.md for details. |
| 18 | + |
| 19 | +"""Git change detection for incremental re-indexing.""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import subprocess |
| 24 | +from dataclasses import dataclass, field |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class GitChangeSet: |
| 29 | + """Set of files changed since a given git ref.""" |
| 30 | + |
| 31 | + modified: list[str] = field(default_factory=list) |
| 32 | + added: list[str] = field(default_factory=list) |
| 33 | + deleted: list[str] = field(default_factory=list) |
| 34 | + |
| 35 | + @property |
| 36 | + def is_empty(self) -> bool: |
| 37 | + return not self.modified and not self.added and not self.deleted |
| 38 | + |
| 39 | + |
| 40 | +def is_git_repo(root_path: str) -> bool: |
| 41 | + """Check if the given path is inside a git work tree.""" |
| 42 | + try: |
| 43 | + result = subprocess.run( |
| 44 | + ["git", "rev-parse", "--is-inside-work-tree"], |
| 45 | + cwd=root_path, |
| 46 | + capture_output=True, |
| 47 | + text=True, |
| 48 | + timeout=10, |
| 49 | + ) |
| 50 | + return result.returncode == 0 and result.stdout.strip() == "true" |
| 51 | + except (FileNotFoundError, subprocess.TimeoutExpired): |
| 52 | + return False |
| 53 | + |
| 54 | + |
| 55 | +def get_head_commit(root_path: str) -> str | None: |
| 56 | + """Get the current HEAD commit hash.""" |
| 57 | + try: |
| 58 | + result = subprocess.run( |
| 59 | + ["git", "rev-parse", "HEAD"], |
| 60 | + cwd=root_path, |
| 61 | + capture_output=True, |
| 62 | + text=True, |
| 63 | + timeout=10, |
| 64 | + ) |
| 65 | + if result.returncode == 0: |
| 66 | + return result.stdout.strip() |
| 67 | + return None |
| 68 | + except (FileNotFoundError, subprocess.TimeoutExpired): |
| 69 | + return None |
| 70 | + |
| 71 | + |
| 72 | +def get_changed_files(root_path: str, since_ref: str | None) -> GitChangeSet: |
| 73 | + """Get files changed since a given git ref. |
| 74 | +
|
| 75 | + Combines committed changes (since_ref..HEAD), staged changes, |
| 76 | + unstaged changes, and untracked files into a single GitChangeSet. |
| 77 | + """ |
| 78 | + if since_ref is None: |
| 79 | + return GitChangeSet() |
| 80 | + |
| 81 | + modified: set[str] = set() |
| 82 | + added: set[str] = set() |
| 83 | + deleted: set[str] = set() |
| 84 | + |
| 85 | + # 1. Committed changes since the ref |
| 86 | + _parse_diff_output(root_path, ["git", "diff", "--name-status", since_ref, "HEAD"], |
| 87 | + modified, added, deleted) |
| 88 | + |
| 89 | + # 2. Unstaged changes |
| 90 | + _parse_diff_output(root_path, ["git", "diff", "--name-status"], |
| 91 | + modified, added, deleted) |
| 92 | + |
| 93 | + # 3. Staged changes |
| 94 | + _parse_diff_output(root_path, ["git", "diff", "--name-status", "--cached"], |
| 95 | + modified, added, deleted) |
| 96 | + |
| 97 | + # 4. Untracked files |
| 98 | + try: |
| 99 | + result = subprocess.run( |
| 100 | + ["git", "ls-files", "--others", "--exclude-standard"], |
| 101 | + cwd=root_path, |
| 102 | + capture_output=True, |
| 103 | + text=True, |
| 104 | + timeout=10, |
| 105 | + ) |
| 106 | + if result.returncode == 0: |
| 107 | + for line in result.stdout.strip().splitlines(): |
| 108 | + path = line.strip() |
| 109 | + if path: |
| 110 | + added.add(path) |
| 111 | + except (FileNotFoundError, subprocess.TimeoutExpired): |
| 112 | + pass |
| 113 | + |
| 114 | + # Resolve overlaps: file in both added and deleted → modified |
| 115 | + overlap = added & deleted |
| 116 | + modified |= overlap |
| 117 | + added -= overlap |
| 118 | + deleted -= overlap |
| 119 | + |
| 120 | + return GitChangeSet( |
| 121 | + modified=sorted(modified), |
| 122 | + added=sorted(added), |
| 123 | + deleted=sorted(deleted), |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +def _parse_diff_output( |
| 128 | + root_path: str, |
| 129 | + cmd: list[str], |
| 130 | + modified: set[str], |
| 131 | + added: set[str], |
| 132 | + deleted: set[str], |
| 133 | +) -> None: |
| 134 | + """Parse git diff --name-status output into modified/added/deleted sets.""" |
| 135 | + try: |
| 136 | + result = subprocess.run( |
| 137 | + cmd, |
| 138 | + cwd=root_path, |
| 139 | + capture_output=True, |
| 140 | + text=True, |
| 141 | + timeout=10, |
| 142 | + ) |
| 143 | + if result.returncode != 0: |
| 144 | + return |
| 145 | + except (FileNotFoundError, subprocess.TimeoutExpired): |
| 146 | + return |
| 147 | + |
| 148 | + for line in result.stdout.strip().splitlines(): |
| 149 | + parts = line.split("\t") |
| 150 | + if len(parts) < 2: |
| 151 | + continue |
| 152 | + status = parts[0] |
| 153 | + path = parts[1] |
| 154 | + |
| 155 | + if status == "M": |
| 156 | + modified.add(path) |
| 157 | + elif status == "A": |
| 158 | + added.add(path) |
| 159 | + elif status == "D": |
| 160 | + deleted.add(path) |
| 161 | + elif status.startswith("R"): |
| 162 | + # Rename: delete old path, add new path |
| 163 | + deleted.add(path) |
| 164 | + if len(parts) >= 3: |
| 165 | + added.add(parts[2]) |
0 commit comments