From 45eeecceb7fa5f0af683328f726e878025c323ba Mon Sep 17 00:00:00 2001 From: "lemon.higgins" Date: Wed, 12 Feb 2025 10:05:02 +0800 Subject: [PATCH] Add libgit2 basic function about operate repo --- suite2cases/libgit2.json | 3 + .../oe_test_libgit2_operate_repo.sh | 67 +++++++++++++++ .../operate_repo.c | 85 +++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/oe_test_libgit2_operate_repo.sh create mode 100644 testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/operate_repo.c diff --git a/suite2cases/libgit2.json b/suite2cases/libgit2.json index e2344fc13..86a938e37 100644 --- a/suite2cases/libgit2.json +++ b/suite2cases/libgit2.json @@ -3,6 +3,9 @@ "cases": [ { "name": "oe_test_libgit2" + }, + { + "name": "oe_test_libgit2_operate_repo" } ] } \ No newline at end of file diff --git a/testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/oe_test_libgit2_operate_repo.sh b/testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/oe_test_libgit2_operate_repo.sh new file mode 100644 index 000000000..4fa96be92 --- /dev/null +++ b/testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/oe_test_libgit2_operate_repo.sh @@ -0,0 +1,67 @@ +#!/usr/bin/bash + +# Copyright (c) 2024. Huawei Technologies Co.,Ltd.ALL rights reserved. +# This program is licensed under Mulan PSL v2. +# You can use it according to the terms and conditions of the Mulan PSL v2. +# http://license.coscl.org.cn/MulanPSL2 +# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +# See the Mulan PSL v2 for more details. + +# ############################################# +# @Author : lemon.higgins +# @Contact : lemon.higgins@aliyun.com +# @Date : 2025-02-12 +# @License : Mulan PSL v2 +# @Desc : libgit2 basic function. +# ############################################ + +source "$OET_PATH/libs/locallibs/common_lib.sh" + +currentDir=$( + cd "$(dirname $0)" || exit 1 + pwd +) + +function pre_test() { + LOG_INFO "Start to prepare the test environment." + DNF_INSTALL "libgit2 libgit2-devel libssh2-devel gcc git" + mkdir my_git_repo && cd my_git_repo || exit + git init + echo "hello" >myfile + git add . + git commit -m "Initial commit" + cd $currentDir || exit + LOG_INFO "Finish preparing the test environment." +} + +function run_test() { + LOG_INFO "Start to run test." + gcc -o operate_repo operate_repo.c -lgit2 -lpthread -lm -lssh2 -lz -lcrypto -lssl -lrt -ldl -lrt + CHECK_RESULT $? 0 0 "Failed to compile." + test -e operate_repo + CHECK_RESULT $? 0 0 "Failed to generate executable file." + ./operate_repo | grep "Commit created successfully." + CHECK_RESULT $? 0 0 "Failed to clone." + cd my_git_repo || exit + git status | grep "nothing to commit, working tree clean" + CHECK_RESULT $? 0 0 "Failed to commit." + git log | grep "Add example.txt" + CHECK_RESULT $? 0 0 "Failed to update git log of message." + git log | grep "Author: Lisa " + CHECK_RESULT $? 0 0 "Failed to update git log of Author." + test -f example.txt + CHECK_RESULT $? 0 0 "Failed to commit example.txt file into my_git_repo." + LOG_INFO "End of the test." +} + +function post_test() { + LOG_INFO "Start to restore the test environment." + cd $currentDir || exit + rm -rf ./my_git_repo ./operate_repo + DNF_REMOVE + LOG_INFO "Finish restoring the test environment." +} + +main "$@" diff --git a/testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/operate_repo.c b/testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/operate_repo.c new file mode 100644 index 000000000..295672d34 --- /dev/null +++ b/testcases/cli-test/libgit2/oe_test_libgit2_operate_repo/operate_repo.c @@ -0,0 +1,85 @@ +#include +#include + +int main() { + git_libgit2_init(); + + // 打开/创建 Git 仓库 + git_repository *repo = NULL; + int error = git_repository_open(&repo, "./my_git_repo"); + if (error != 0) { + fprintf(stderr, "Error opening repository: %s\n", giterr_last()->message); + return 1; + } + + // 创建一个新的文件example.txt + FILE *file = fopen("./my_git_repo/example.txt", "w"); + fputs("Hello, libgit2!", file); + fclose(file); + + // 添加文件 example.txt 到暂存区 + git_index *index = NULL; + error = git_repository_index(&index, repo); + if (error != 0) { + fprintf(stderr, "Error getting repository index: %s\n", giterr_last()->message); + return 1; + } + + error = git_index_add_bypath(index, "example.txt"); + if (error != 0) { + fprintf(stderr, "Error adding file to index: %s\n", giterr_last()->message); + return 1; + } + git_index_write(index); + + // 写入暂存区更改到 Git 对象数据库 + git_oid tree_id, commit_id; + git_tree *tree = NULL; + error = git_index_write_tree(&tree_id, index); + if (error != 0) { + fprintf(stderr, "Error writing tree from index: %s\n", giterr_last()->message); + return 1; + } + error = git_tree_lookup(&tree, repo, &tree_id); + if (error != 0) { + fprintf(stderr, "Error looking up tree: %s\n", giterr_last()->message); + return 1; + } + + // 创建提交对象 + git_signature *author, *committer; + git_signature_now(&author, "Lisa", "lisa@163.com"); + committer = author; + + git_commit *parent = NULL; + error = git_reference_name_to_id(&commit_id, repo, "HEAD"); + if (error != 0) { + fprintf(stderr, "Error getting HEAD reference: %s\n", giterr_last()->message); + return 1; + } + error = git_commit_lookup(&parent, repo, &commit_id); + if (error != 0 && error != GIT_ENOTFOUND) { + fprintf(stderr, "Error getting parent commit: %s\n", giterr_last()->message); + return 1; + } + + const char *message = "Add example.txt"; + error = git_commit_create_v(&commit_id, repo, "HEAD", author, committer, NULL, message, tree, error == GIT_ENOTFOUND ? 0 : 1, parent); + if (error != 0) { + fprintf(stderr, "Error creating commit: %s\n", giterr_last()->message); + return 1; + } + + printf("Commit created successfully.\n"); + + // 释放资源 + git_tree_free(tree); + git_commit_free(parent); + git_signature_free(author); + git_index_free(index); + git_repository_free(repo); + + git_libgit2_shutdown(); + + return 0; +} -- Gitee