{"thread":{"id":"62824","subject":"Introduction of GpluginLoader Plugin for Git","startedAt":"2025-01-18T07:07:35Z","lastAt":"2025-01-18T07:07:35Z","messageCount":1,"participants":["Tianxiaoxiao1021"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"510864","messageId":"cd89db5.1b48.194783cf396.Coremail.bf_bug_feedback@126.com","threadId":"62824","inReplyTo":null,"subject":"Introduction of GpluginLoader Plugin for Git","fromName":"Tianxiaoxiao1021","fromEmail":"bf_bug_feedback@126.com","sentAt":"2025-01-18T07:07:28Z","receivedAt":"2025-01-18T07:07:35Z","isPatch":false,"sender":{"key":"bf_bug_feedback@126.com","avatar":null},"body":"Dear Git Community,\nTo address the different needs for workflows between community members and developers, I have developed GpluginLoader, a plugin loader for loading plugins based on c/c++ source code, and it is platform independent.\nAdditionally, I have included several source files to provide APIs for plugins, including functions for retrieving the current repository's README file, profile, and holder, among others. These APIs are encapsulated in the git-plugin-api.c and git-plugin-api.h files.\nYou can find the GpluginLoader repository here: https://github.com/Tianxiaoxiao1021/GpluginLoader.\nBest regards,\nTianxiaoxiao developer team: Tianxiaoxiao1021\n\n/*\n* GpluginLoader:git-plugin-api.c\n* \n* 插件可调用的API实现\n*/\n\n\n#include \"git-plugin-api.h\"\n#include \"builtin.h\"\n#include \"repository.h\"\n#include \"strbuf.h\"\n#include \"config.h\"\n\nstatic const char* git_get_repo_name(void) {\n    const char *gitdir = repo_git_path(the_repository, \"\");\n    if (!gitdir)\n        return NULL;\n    const char *last_slash = strrchr(gitdir, '/');\n    if (last_slash && strcmp(last_slash + 1, \".git\") == 0) {\n        const char *parent_dir = last_slash - 1;\n        while (parent_dir > gitdir && *parent_dir != '/')\n            parent_dir--;\n        return parent_dir + 1;\n    }\n    return NULL;\n}\n\nstatic const char* git_get_repo_owner(void) {\n    const char *owner = NULL;\n    if (!git_config_get_string(\"user.name\", &owner))\n        return owner;\n    return NULL;\n}\n\nstatic const char* git_get_repo_description(void) {\n    static struct strbuf buf = STRBUF_INIT;\n    strbuf_reset(&buf);\n    \n    if (strbuf_read_file(&buf, repo_git_path(the_repository, \"description\"), 0) > 0)\n        return buf.buf;\n        \n    return NULL;\n}\n\nstatic char* git_get_repo_readme(void) {\n    static const char *readme_names[] = {\n        \"README.md\",\n        \"README\",\n        \"README.txt\",\n        NULL\n    };\n    \n    static struct strbuf buf = STRBUF_INIT;\n    const char **name;\n    \n    strbuf_reset(&buf);\n    \n    for (name = readme_names; *name; name++) {\n        if (strbuf_read_file(&buf, *name, 0) > 0)\n            return strbuf_detach(&buf, NULL);\n    }\n    \n    return NULL;\n}\n\nstatic int git_add_command(const char *name, int (*fn)(int, const char **, const char *)) {\n    return register_builtin(name, fn);\n}\n\nstatic int git_modify_command(const char *name, int (*fn)(int, const char **, const char *)) {\n    if (unregister_builtin(name) != 0) {\n        return -1;\n    }\n    return register_builtin(name, fn);\n}\n\nstatic struct git_plugin_api plugin_api = {\n    .register_command = register_builtin,\n    .unregister_command = unregister_builtin,\n    .get_repository = the_repository,\n    .get_repo_name = git_get_repo_name,\n    .get_repo_owner = git_get_repo_owner,\n    .get_repo_description = git_get_repo_description,\n    .get_repo_readme = git_get_repo_readme,\n    .add_command = git_add_command,\n    .modify_command = git_modify_command\n};\n\nstruct git_plugin_api* get_git_plugin_api(void) {\n    return &plugin_api;\n}\n\n#ifndef GIT_PLUGIN_API_H\n#define GIT_PLUGIN_API_H\n\n#include \"repository.h\"\n\nstruct git_plugin_api {\n    // 命令注册相关API\n    int (*register_command)(const char *name, int (*fn)(int, const char **, const char *));\n    int (*unregister_command)(const char *name);\n    struct repository* (*get_repository)(void);\n    \n    // 仓库信息API\n    const char* (*get_repo_name)(void);\n    const char* (*get_repo_owner)(void);\n    const char* (*get_repo_description)(void);\n    char* (*get_repo_readme)(void);\n\n    // 新增API：添加和修改命令\n    int (*add_command)(const char *name, int (*fn)(int, const char **, const char *));\n    int (*modify_command)(const char *name, int (*fn)(int, const char **, const char *));\n};\n\n// 获取插件API实例\nstruct git_plugin_api* get_git_plugin_api(void);\n\n#endif "}]}