From 8c01048d33de532bf78ab256daf51227c0738ca9 Mon Sep 17 00:00:00 2001 From: unknown <907162427@qq.com> Date: Sat, 21 Oct 2023 10:39:46 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E4=BB=BB=E4=BD=95=E6=8F=90=E4=BA=A4=E6=97=B6=E5=80=99sha1?= =?UTF-8?q?=E8=AE=BE=E4=B8=BANULL=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E6=8C=89git=E7=9A=84=E6=83=85=E5=86=B5=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 ++ checkout/checkout.cpp | 3 +-- commit/commit.cpp | 4 ++-- merge/merge.cpp | 20 ++++++++++++++++++++ object/object.cpp | 7 ++++--- s-git.cpp | 1 + s-git.h | 1 + test/test.py | 1 + 8 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 merge/merge.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f534a31..c7cf707 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ aux_source_directory(./tag TAG_LIST) aux_source_directory(./branch BRANCH_LIST) aux_source_directory(./checkout CHECKOUT_LIST) aux_source_directory(./ls-tree LS_TREE_LIST) +aux_source_directory(./merge MERGE_LIST) set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}) @@ -33,6 +34,7 @@ add_executable(s-git ${MAIN_LIST} ${BRANCH_LIST} ${CHECKOUT_LIST} ${LS_TREE_LIST} + ${MERGE_LIST} ) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_link_libraries(s-git stdc++fs) diff --git a/checkout/checkout.cpp b/checkout/checkout.cpp index 5175c0a..4b119c5 100644 --- a/checkout/checkout.cpp +++ b/checkout/checkout.cpp @@ -55,8 +55,7 @@ bool deleteDir(fs::path path) { } void copyBranches(const fs::path& dir, const std::string& sha1) { - if (sha1 == "NULL") { - + if (sha1 == "") { return; } if (sha1Exist(sha1)) { diff --git a/commit/commit.cpp b/commit/commit.cpp index f1b7d79..c31bf60 100644 --- a/commit/commit.cpp +++ b/commit/commit.cpp @@ -118,7 +118,7 @@ int commitMain(int argc, const char* argv[]) { std::vector index; auto treeOpt = build_tree(ROOT_DIR.value()); // file tree of s-git root directory - std::string newcommitSha1 = writeCommit(treeOpt.has_value() ? treeOpt.value().sha1 : "NULL" , "NULL", message); + std::string newcommitSha1 = writeCommit(treeOpt.has_value() ? treeOpt.value().sha1 : "" , "", message); writeMain(newcommitSha1); std::cout << "first commit \n"; @@ -135,7 +135,7 @@ int commitMain(int argc, const char* argv[]) { if (!readCommit(commitSha1, parentCommit)) { auto treeOpt = build_tree(ROOT_DIR.value()); // file tree of s-git root directory - std::string newcommitSha1 = writeCommit(treeOpt.has_value() ? treeOpt.value().sha1 : "NULL", "NULL", message); + std::string newcommitSha1 = writeCommit(treeOpt.has_value() ? treeOpt.value().sha1 : "", "", message); writeMain(newcommitSha1); std::cout << "first commit \n"; diff --git a/merge/merge.cpp b/merge/merge.cpp new file mode 100644 index 0000000..c3dff6b --- /dev/null +++ b/merge/merge.cpp @@ -0,0 +1,20 @@ +#include "../s-git.h" +#include "../cache/file.h" +#include "../object/object.h" +#include +#include +#include +#include +#include + +int merge(int argc, const char* argv[]); + +Command MergeCommand{ + merge, + "Join two or more development histories together" +}; + +int merge(int argc, const char* argv[]){ + std::cout << "Join two or more development histories together" << std::endl; + return 1; +} diff --git a/object/object.cpp b/object/object.cpp index b35fcd2..42d7f62 100644 --- a/object/object.cpp +++ b/object/object.cpp @@ -34,8 +34,9 @@ bool checkSha1(const std::string &sha1) { } bool readCommit(const std::string &sha1, commit& thisCommit) { - if (sha1 == "NULL") { + if (sha1 == ""){ // not error, but also not commit + std::cerr << red << "Error: " << GIT_NAME << " your current branch 'master' does not have any commits yet" << Reset << std::endl; return false; } @@ -69,7 +70,7 @@ bool readCommit(const std::string &sha1, commit& thisCommit) { } void readTree(const fs::path &dir, const std::string &sha1, std::vector& path) { - if (sha1 == "NULL") { + if (sha1 == "") { return; } if (sha1Exist(sha1)) { @@ -254,7 +255,7 @@ std::optional> readRawFile(const std::string& sha1) { - if (sha1 == "NULL") { + if (sha1 == "") { return {}; } if (sha1Exist(sha1)) { diff --git a/s-git.cpp b/s-git.cpp index aec096f..8e8a17f 100644 --- a/s-git.cpp +++ b/s-git.cpp @@ -27,6 +27,7 @@ static void initFuncTable() { funcTable.emplace("log", LogCommand); funcTable.emplace("branch", BranchCommand); funcTable.emplace("ls-tree", LsTreeCommand); + funcTable.emplace("merge", MergeCommand); } static void usage() { diff --git a/s-git.h b/s-git.h index c84ef3a..09c80d9 100644 --- a/s-git.h +++ b/s-git.h @@ -45,3 +45,4 @@ extern Command CheckoutCommand; extern Command LogCommand; extern Command BranchCommand; extern Command LsTreeCommand; +extern Command MergeCommand; diff --git a/test/test.py b/test/test.py index 7114ebc..dfc39c6 100644 --- a/test/test.py +++ b/test/test.py @@ -252,6 +252,7 @@ def main(): try: TestInit() TestStatusNoCommit() + TestLog(["second", "first"]) TestStatusUntracked() shaFirstShort = TestCommitFirst() TestBranchAdd("master") From 2238117756e0bb0b50b8b04de2c8f8d4edebbc95 Mon Sep 17 00:00:00 2001 From: unknown <907162427@qq.com> Date: Mon, 23 Oct 2023 20:53:33 +0800 Subject: [PATCH 2/3] Fix issue #5 --- logCommand/log.cpp | 2 +- merge/merge.cpp | 17 ++++++++++++++--- object/object.cpp | 6 +++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/logCommand/log.cpp b/logCommand/log.cpp index ab6068c..116bc31 100644 --- a/logCommand/log.cpp +++ b/logCommand/log.cpp @@ -22,7 +22,7 @@ int logMain(int argc, const char* argv[]) { commit currCommit; while (readCommit(sha1, currCommit)) { std::cout << yellow << "[commit " << currCommit.sha1 << ']' << Reset << std::endl; - std::cout << cyan << "mmessage:" << Reset << std::endl; + std::cout << cyan << "message:" << Reset << std::endl; std::cout << std::endl; std::cout << currCommit.message; diff --git a/merge/merge.cpp b/merge/merge.cpp index c3dff6b..81826f9 100644 --- a/merge/merge.cpp +++ b/merge/merge.cpp @@ -7,14 +7,25 @@ #include #include -int merge(int argc, const char* argv[]); +int mergeMain(int argc, const char* argv[]); Command MergeCommand{ - merge, + mergeMain, "Join two or more development histories together" }; -int merge(int argc, const char* argv[]){ +//数据结构定义先 + +int mergeMain(int argc, const char* argv[]){ + cmdline::parser argParser; + argParser.add("message", 'm', "merge message", true, ""); + argParser.parse_check(argc, argv); + // 参数解析 + + // 判断是否需要 merge 是否可以merege先 + // 1. 判断是否有冲突 + // 2. 判断是否有未提交的修改 + std::cout << "Join two or more development histories together" << std::endl; return 1; } diff --git a/object/object.cpp b/object/object.cpp index 42d7f62..96e4358 100644 --- a/object/object.cpp +++ b/object/object.cpp @@ -36,7 +36,11 @@ bool checkSha1(const std::string &sha1) { bool readCommit(const std::string &sha1, commit& thisCommit) { if (sha1 == ""){ // not error, but also not commit - std::cerr << red << "Error: " << GIT_NAME << " your current branch 'master' does not have any commits yet" << Reset << std::endl; + // if this commit is empty ,then means there is no commit + if (thisCommit.sha1 == "") + std::cerr << red << "Error: " << GIT_NAME << " your current branch 'master' does not have any commits yet" << Reset << std::endl; + + return false; } From d60e4ad386370312f0a91507a72a29f4559f7c16 Mon Sep 17 00:00:00 2001 From: unknown <907162427@qq.com> Date: Mon, 23 Oct 2023 21:43:57 +0800 Subject: [PATCH 3/3] for merge --- merge/merge.cpp | 15 +++++++++++++++ test/test.py | 7 ++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/merge/merge.cpp b/merge/merge.cpp index 81826f9..b566069 100644 --- a/merge/merge.cpp +++ b/merge/merge.cpp @@ -9,6 +9,10 @@ int mergeMain(int argc, const char* argv[]); +struct MergeResult{ + std::vector parents; + std::string sha1; +}; Command MergeCommand{ mergeMain, "Join two or more development histories together" @@ -19,7 +23,18 @@ Command MergeCommand{ int mergeMain(int argc, const char* argv[]){ cmdline::parser argParser; argParser.add("message", 'm', "merge message", true, ""); + argParser.footer(" ..."); + argParser.parse_check(argc, argv); + // 首先获得当前分支的, + if (argParser.rest().size() == 0) { + std::cout << argParser.usage(); + return 0; + } + for (auto branch : argParser.rest()) { + std::cout << branch << std::endl; + } + // 参数解析 // 判断是否需要 merge 是否可以merege先 diff --git a/test/test.py b/test/test.py index dfc39c6..01d0881 100644 --- a/test/test.py +++ b/test/test.py @@ -252,7 +252,7 @@ def main(): try: TestInit() TestStatusNoCommit() - TestLog(["second", "first"]) + TestLog([]) TestStatusUntracked() shaFirstShort = TestCommitFirst() TestBranchAdd("master") @@ -293,8 +293,9 @@ def main(): parser.add_argument("exe", metavar = "executable-path") args = parser.parse_args() - app = os.path.abspath(args.exe) - + app = os.path.abspath('D:\learn\programming\s-git\s-git.exe') + + print(app) LoadTmpDir() main() DeleteTmpDir()