git.cpp

src/mcp/servers/git.cpp

@file git.cpp @brief GitServer implementation — version control operations.

Owners: @tvanfossen

GitServer

entropic::GitServer::GitServer(const std::filesystem::path &repo_dir, const std::string &data_dir)

Construct with repo directory and data dir.

src/mcp/servers/git.cpp:563

Source · 12 lines
GitServer::GitServer(
    const std::filesystem::path& repo_dir,
    const std::string& data_dir)
    : MCPServerBase("git")
    , repo_dir_(repo_dir) {

    create_git_tools(data_dir + "/tools");
    register_git_tools();

    logger->info("GitServer initialized: repo='{}'",
                 repo_dir_.string());
}
Activity · 3 steps
lines 569–569create_git_tools(data_dir + "/tools");lines 570–570register_git_tools();lines 572–573logger->info("GitServer initialized: repo='{}'", repo_dir_.string());

Requirements

REQ-MCP-001 REQ-MCP-023

create_git_tools

void entropic::GitServer::create_git_tools(const std::string &tools_dir)

Construct the eight git tool instances (ctor step 1).

src/mcp/servers/git.cpp:586

Show on the map →

Called by GitServer

Calls GitAddTool, GitBranchTool, GitCheckoutTool, GitCommitTool, GitDiffTool, GitLogTool, GitResetTool, GitStatusTool, load_tool_definition

Source · 18 lines
void GitServer::create_git_tools(const std::string& tools_dir) {
    status_ = std::make_unique<GitStatusTool>(
        load_tool_definition("status", "git", tools_dir), *this);
    diff_ = std::make_unique<GitDiffTool>(
        load_tool_definition("diff", "git", tools_dir), *this);
    log_ = std::make_unique<GitLogTool>(
        load_tool_definition("log", "git", tools_dir), *this);
    commit_ = std::make_unique<GitCommitTool>(
        load_tool_definition("commit", "git", tools_dir), *this);
    branch_ = std::make_unique<GitBranchTool>(
        load_tool_definition("branch", "git", tools_dir), *this);
    checkout_ = std::make_unique<GitCheckoutTool>(
        load_tool_definition("checkout", "git", tools_dir), *this);
    add_ = std::make_unique<GitAddTool>(
        load_tool_definition("add", "git", tools_dir), *this);
    reset_ = std::make_unique<GitResetTool>(
        load_tool_definition("reset", "git", tools_dir), *this);
}
Activity · 8 steps
lines 587–588status_ = std::make_unique<GitStatusTool>( load_tool_definition("status", "git", tools_dir), *this);lines 589–590diff_ = std::make_unique<GitDiffTool>( load_tool_definition("diff", "git", tools_dir), *this);lines 591–592log_ = std::make_unique<GitLogTool>( load_tool_definition("log", "git", tools_dir), *this);lines 593–594commit_ = std::make_unique<GitCommitTool>( load_tool_definition("commit", "git", tools_dir), *this);lines 595–596branch_ = std::make_unique<GitBranchTool>( load_tool_definition("branch", "git", tools_dir), *this);lines 597–598checkout_ = std::make_unique<GitCheckoutTool>( load_tool_definition("checkout", "git", tools_dir), *this);lines 599–600add_ = std::make_unique<GitAddTool>( load_tool_definition("add", "git", tools_dir), *this);lines 601–602reset_ = std::make_unique<GitResetTool>( load_tool_definition("reset", "git", tools_dir), *this);

Requirements

REQ-MCP-023

drain_and_close

static std::string entropic::drain_and_close(int fd)

Read a pipe to EOF and close it.

src/mcp/servers/git.cpp:67

Show on the map →

Called by run_git

Calls none

Source · 9 lines
static std::string drain_and_close(int fd) {
    std::string output;
    std::array<char, 4096> buf{};
    for (ssize_t n; fd >= 0 && (n = ::read(fd, buf.data(), buf.size())) > 0;) {
        output.append(buf.data(), static_cast<size_t>(n));
    }
    if (fd >= 0) { ::close(fd); }
    return output;
}
Activity · 7 steps
lines 68–68std::string output;lines 69–69std::array<char, 4096> buf{};lines 70–70◇ for (ssize_t n; fd >= 0 && (n = ::read(fd, buf.data(), buf.size())) > 0;)lines 71–71output.append(buf.data(), static_cast<size_t>(n));lines 73–73◇ if (fd >= 0)lines 73–73::close(fd);lines 74–74return output;

Requirements

REQ-MCP-023

make_git_response

static ServerResponse entropic::make_git_response(const std::string &output, int exit_code)

Format a git result as JSON ServerResponse .

src/mcp/servers/git.cpp:158

Show on the map →

Called by execute

Calls none

Source · 8 lines
static ServerResponse make_git_response(
    const std::string& output, int exit_code) {

    nlohmann::json result;
    result["exit_code"] = exit_code;
    result["output"] = output;
    return {result.dump(), {}};
}
Activity · 4 steps
lines 161–161nlohmann::json result;lines 162–162result["exit_code"] = exit_code;lines 163–163result["output"] = output;lines 164–164return {result.dump(), {}};

Requirements

REQ-MCP-002 REQ-MCP-023

register_git_tools

void entropic::GitServer::register_git_tools()

Register the eight git tools with the base server (step 2).

src/mcp/servers/git.cpp:614

Show on the map →

Called by GitServer

Calls none

Source · 10 lines
void GitServer::register_git_tools() {
    register_tool(status_.get());
    register_tool(diff_.get());
    register_tool(log_.get());
    register_tool(commit_.get());
    register_tool(branch_.get());
    register_tool(checkout_.get());
    register_tool(add_.get());
    register_tool(reset_.get());
}
Activity · 8 steps
lines 615–615register_tool(status_.get());lines 616–616register_tool(diff_.get());lines 617–617register_tool(log_.get());lines 618–618register_tool(commit_.get());lines 619–619register_tool(branch_.get());lines 620–620register_tool(checkout_.get());lines 621–621register_tool(add_.get());lines 622–622register_tool(reset_.get());

Requirements

REQ-MCP-001

repo_dir

const std::filesystem::path & entropic::GitServer::repo_dir() const

Get repo directory.

src/mcp/servers/git.cpp:655

Show on the map →

Called by execute

Calls none

Source · 3 lines
const std::filesystem::path& GitServer::repo_dir() const {
    return repo_dir_;
}
Activity · 1 steps
lines 656–656return repo_dir_;

Requirements

REQ-MCP-023

required_access_level

MCPAccessLevel entropic::GitStatusTool::required_access_level() const override

Read-only tool — requires READ access.

src/mcp/servers/git.cpp:196

Show on the map →

Called by none

Calls none

Source · 3 lines
    MCPAccessLevel required_access_level() const override {
        return MCPAccessLevel::READ;
    }
Activity · 1 steps
lines 197–197return MCPAccessLevel::READ;
MCPAccessLevel entropic::GitDiffTool::required_access_level() const override

Read-only tool — requires READ access.

src/mcp/servers/git.cpp:245

Show on the map →

Called by none

Calls none

Source · 3 lines
    MCPAccessLevel required_access_level() const override {
        return MCPAccessLevel::READ;
    }
Activity · 1 steps
lines 246–246return MCPAccessLevel::READ;
MCPAccessLevel entropic::GitLogTool::required_access_level() const override

Read-only tool — requires READ access.

src/mcp/servers/git.cpp:301

Show on the map →

Called by none

Calls none

Source · 3 lines
    MCPAccessLevel required_access_level() const override {
        return MCPAccessLevel::READ;
    }
Activity · 1 steps
lines 302–302return MCPAccessLevel::READ;

Requirements

REQ-MCP-011

run_git

static std::pair< std::string, int > entropic::run_git(const std::string &repo_dir, const std::vector< std::string > &args)

Run git -C with NO shell (gh#211, v2.14.4).

src/mcp/servers/git.cpp:96

Source · 15 lines
static std::pair<std::string, int> run_git(
    const std::string& repo_dir,
    const std::vector<std::string>& args) {
    int fd = -1;
    const pid_t pid = spawn_git(repo_dir, args, fd);
    const std::string output = drain_and_close(fd);
    int status = 0;
    const bool reaped = pid > 0 && ::waitpid(pid, &status, 0) == pid;
    const int exit_code = (reaped && WIFEXITED(status)) ? WEXITSTATUS(status) : -1;
    logger->info("Git: {} arg(s) [{}] exit={}, {} chars", args.size(),
                 args.empty() ? std::string{} : args.front(), exit_code,
                 output.size());
    return reaped ? std::make_pair(output, exit_code)
                  : std::make_pair(std::string("Failed to open git process"), -1);
}
Activity · 8 steps
lines 99–99int fd = -1;lines 100–100const pid_t pid = spawn_git(repo_dir, args, fd);lines 101–101const std::string output = drain_and_close(fd);lines 102–102int status = 0;lines 103–103const bool reaped = pid > 0 && ::waitpid(pid, &status, 0) == pid;lines 104–104const int exit_code = (reaped && WIFEXITED(status)) ? WEXITSTATUS(status) : -1;lines 105–107logger->info("Git: {} arg(s) [{}] exit={}, {} chars", args.size(), args.empty() ? std::string{} : args.front(), exit_code, output.size());lines 108–109return reaped ? std::make_pair(output, exit_code) : std::make_pair(std::string("Failed to open git process"), -1);

Requirements

REQ-MCP-023

set_working_dir

bool entropic::GitServer::set_working_dir(const std::string &path) override

Set working directory (repo root).

src/mcp/servers/git.cpp:643

Show on the map →

Called by none

Calls none

Source · 5 lines
bool GitServer::set_working_dir(const std::string& path) {
    repo_dir_ = path;
    logger->info("Repo directory set to: {}", path);
    return true;
}
Activity · 3 steps
lines 644–644repo_dir_ = path;lines 645–645logger->info("Repo directory set to: {}", path);lines 646–646return true;

Requirements

REQ-MCP-023

spawn_git

static pid_t entropic::spawn_git(const std::string &repo_dir, const std::vector< std::string > &args, int &read_fd)

Start git -C with stdout+stderr on a pipe.

src/mcp/servers/git.cpp:38

Show on the map →

Called by run_git

Calls none

Source · 21 lines
static pid_t spawn_git(const std::string& repo_dir,
                       const std::vector<std::string>& args, int& read_fd) {
    std::vector<std::string> owned = {"git", "-C", repo_dir};
    owned.insert(owned.end(), args.begin(), args.end());
    std::vector<char*> argv;
    for (auto& a : owned) { argv.push_back(a.data()); }
    argv.push_back(nullptr);
    int fds[2] = {-1, -1};
    const pid_t pid = (::pipe(fds) == 0) ? ::fork() : -1;
    if (pid == 0) {
        ::dup2(fds[1], STDOUT_FILENO);
        ::dup2(fds[1], STDERR_FILENO);
        ::close(fds[0]);
        ::close(fds[1]);
        ::execvp("git", argv.data());
        ::_exit(127);
    }
    if (fds[1] >= 0) { ::close(fds[1]); }
    read_fd = fds[0];
    return pid;
}
Activity · 19 steps
lines 40–40std::vector<std::string> owned = {"git", "-C", repo_dir};lines 41–41owned.insert(owned.end(), args.begin(), args.end());lines 42–42std::vector<char*> argv;lines 43–43◇ for (auto& a : owned)lines 43–43argv.push_back(a.data());lines 44–44argv.push_back(nullptr);lines 45–45int fds[2] = {-1, -1};lines 46–46const pid_t pid = (::pipe(fds) == 0) ? ::fork() : -1;lines 47–47◇ if (pid == 0)lines 48–48::dup2(fds[1], STDOUT_FILENO);lines 49–49::dup2(fds[1], STDERR_FILENO);lines 50–50::close(fds[0]);lines 51–51::close(fds[1]);lines 52–52::execvp("git", argv.data());lines 53–53::_exit(127);lines 55–55◇ if (fds[1] >= 0)lines 55–55::close(fds[1]);lines 56–56read_fd = fds[0];lines 57–57return pid;

Requirements

REQ-MCP-023