diff --git a/src/main.cpp b/src/main.cpp index 752a243..4cd34b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,22 +2,25 @@ #include // For version number only #include -#include "git_exception.hpp" +#include "src/utils/git_exception.hpp" #include "version.hpp" #include "subcommand/init_subcommand.hpp" +#include "subcommand/status_subcommand.hpp" int main(int argc, char** argv) { int exitCode = 0; try { + const libgit2_object lg2_obj; CLI::App app{"Git using C++ wrapper of libgit2"}; // Top-level command options. auto version = app.add_flag("-v,--version", "Show version"); // Sub commands - InitSubcommand init(app); + init_subcommand init(lg2_obj, app); + status_subcommand status(lg2_obj, app); app.parse(argc, argv); diff --git a/src/meson.build b/src/meson.build index 52fa838..1e68a2f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,7 +1,7 @@ subdir('subcommand') +subdir('utils') subdir('wrapper') src_files = files([ - 'git_exception.cpp', 'main.cpp' -]) + subcommand_files + wrapper_files +]) + subcommand_files + utils_files + wrapper_files diff --git a/src/subcommand/base_subcommand.hpp b/src/subcommand/base_subcommand.hpp deleted file mode 100644 index 28e82ad..0000000 --- a/src/subcommand/base_subcommand.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -class BaseSubcommand -{ -public: - BaseSubcommand() = default; - - virtual ~BaseSubcommand() = default; - - BaseSubcommand(const BaseSubcommand&) = delete; - BaseSubcommand& operator=(const BaseSubcommand&) = delete; - BaseSubcommand(BaseSubcommand&&) = delete; - BaseSubcommand& operator=(BaseSubcommand&&) = delete; -}; diff --git a/src/subcommand/init_subcommand.cpp b/src/subcommand/init_subcommand.cpp index c288996..2581c3c 100644 --- a/src/subcommand/init_subcommand.cpp +++ b/src/subcommand/init_subcommand.cpp @@ -1,8 +1,8 @@ -#include +// #include #include "init_subcommand.hpp" -#include "../wrapper/repository_wrapper.hpp" +#include "src/wrapper/repository_wrapper.hpp" -InitSubcommand::InitSubcommand(CLI::App& app) +init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("init", "Explanation of init here"); @@ -11,13 +11,12 @@ InitSubcommand::InitSubcommand(CLI::App& app) // If directory not specified, uses cwd. sub->add_option("directory", directory, "info about directory arg") ->check(CLI::ExistingDirectory | CLI::NonexistentPath) - ->default_val(std::filesystem::current_path()); + ->default_val(get_current_git_path()); sub->callback([this]() { this->run(); }); } -void InitSubcommand::run() +void init_subcommand::run() { - RepositoryWrapper repo; - repo.init(directory, bare); + repository_wrapper::init(directory, bare); } diff --git a/src/subcommand/init_subcommand.hpp b/src/subcommand/init_subcommand.hpp index a27e06c..ef39e5a 100644 --- a/src/subcommand/init_subcommand.hpp +++ b/src/subcommand/init_subcommand.hpp @@ -1,12 +1,16 @@ #pragma once #include -#include "base_subcommand.hpp" -class InitSubcommand : public BaseSubcommand +#include + +#include "../utils/common.hpp" + +class init_subcommand { public: - InitSubcommand(CLI::App& app); + + explicit init_subcommand(const libgit2_object&, CLI::App& app); void run(); private: diff --git a/src/subcommand/meson.build b/src/subcommand/meson.build index cc9f8be..66cb1b2 100644 --- a/src/subcommand/meson.build +++ b/src/subcommand/meson.build @@ -1,3 +1,4 @@ subcommand_files = files([ 'init_subcommand.cpp', + 'status_subcommand.cpp', ]) diff --git a/src/subcommand/status_subcommand.cpp b/src/subcommand/status_subcommand.cpp new file mode 100644 index 0000000..5689d13 --- /dev/null +++ b/src/subcommand/status_subcommand.cpp @@ -0,0 +1,202 @@ +#include +#include +#include + +#include + +#include "status_subcommand.hpp" +#include "../wrapper/status_wrapper.hpp" + +status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app) +{ + auto *sub = app.add_subcommand("status", "Show modified files in working directory, staged for your next commit"); + // Displays paths that have differences between the index file and the current HEAD commit, + // paths that have differences between the working tree and the index file, and paths in the + // working tree that are not tracked by Git (and are not ignored by gitignore[5]). + // The first are what you would commit by running git commit; + // the second and third are what you could commit by running git add before running git commit. + + sub->add_flag("-s,--short", short_flag, "Give the output in the short-format."); + sub->add_flag("--long", long_flag, "Give the output in the long-format. This is the default."); + // sub->add_flag("--porcelain[=]", porcelain, "Give the output in an easy-to-parse format for scripts. + // This is similar to the short output, but will remain stable across Git versions and regardless of user configuration. + // See below for details. The version parameter is used to specify the format version. This is optional and defaults + // to the original version v1 format."); + + sub->callback([this]() { this->run(); }); +}; + +const std::string untracked_header = "Untracked files:\n"; +// "Untracked files:\n (use \"git add ...\" to include in what will be committed)"; +const std::string tobecommited_header = "Changes to be committed:\n"; +// "Changes to be committed:\n (use \"git reset HEAD ...\" to unstage)"; +const std::string ignored_header = "Ignored files:\n"; +// "Ignored files:\n (use \"git add -f ...\" to include in what will be committed)" +const std::string notstagged_header = "Changes not staged for commit:\n"; +// "Changes not staged for commit:\n (use \"git add%s ...\" to update what will be committed)\n (use \"git checkout -- ...\" to discard changes in working directory)" +const std::string nothingtocommit_message = "No changes added to commit"; +// "No changes added to commit (use \"git add\" and/or \"git commit -a\")" + +struct status_messages +{ + std::string short_mod; + std::string long_mod; +}; + +const std::map status_msg_map = //TODO : check spaces in short_mod +{ + { GIT_STATUS_CURRENT, {"", ""} }, + { GIT_STATUS_INDEX_NEW, {"A ", "\t new file:"} }, + { GIT_STATUS_INDEX_MODIFIED, {"M ", "\t modified:"} }, + { GIT_STATUS_INDEX_DELETED, {"D ", "\t deleted:"} }, + { GIT_STATUS_INDEX_RENAMED, {"R ", "\t renamed:"} }, + { GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\t typechange:"} }, + { GIT_STATUS_WT_NEW, {"?? ", ""} }, + { GIT_STATUS_WT_MODIFIED, {" M " , "\t modified:"} }, + { GIT_STATUS_WT_DELETED, {" D ", "\t deleted:"} }, + { GIT_STATUS_WT_TYPECHANGE, {" T ", "\t typechange:"} }, + { GIT_STATUS_WT_RENAMED, {" R ", "\t renamed:"} }, + { GIT_STATUS_WT_UNREADABLE, {"", ""} }, + { GIT_STATUS_IGNORED, {"!! ", ""} }, + { GIT_STATUS_CONFLICTED, {"", ""} }, +}; + +enum class output_format +{ + DEFAULT = 0, + LONG = 1, + SHORT = 2 +}; + +void print_entries(git_status_t status, status_list_wrapper& sl, bool head_selector, output_format of) // TODO: add different mods +{ + const auto& entry_list = sl.get_entry_list(status); + if (!entry_list.empty()) + { + for (auto* entry : entry_list) + { + if ((of == output_format::DEFAULT) || (of == output_format::LONG)) + { + std::cout << status_msg_map.at(status).long_mod << "\t"; + } + else if (of == output_format::SHORT) + { + std::cout << status_msg_map.at(status).short_mod; + } + + git_diff_delta* diff_delta; + if (head_selector) + { + diff_delta = entry->head_to_index; + } + else + { + diff_delta = entry->index_to_workdir; + } + const char* old_path = diff_delta->old_file.path; + const char* new_path = diff_delta->new_file.path; + if (old_path && new_path && std::strcmp(old_path, new_path)) + { + std::cout << old_path << " -> " << new_path << std::endl; + } + else + { + if (old_path) + { + std::cout << old_path << std::endl; + } + else + { + std::cout << new_path << std::endl; + } + } + } + } + else + {} +} + +void status_subcommand::run() +{ + auto directory = get_current_git_path(); + auto bare = false; + auto repo = repository_wrapper::init(directory, bare); + auto sl = status_list_wrapper::status_list(repo); + + // TODO: add branch info + + output_format of = output_format::DEFAULT; + if (short_flag) + { + of = output_format::SHORT; + } + if (long_flag) + { + of = output_format::LONG; + } + // else if (porcelain_format) + // { + // output_format = 3; + // } + + bool is_long; + is_long = ((of == output_format::DEFAULT) || (of == output_format::LONG)); + if (sl.has_tobecommited_header()) + { + if (is_long) + { + std::cout << tobecommited_header << std::endl; + } + print_entries(GIT_STATUS_INDEX_NEW, sl, true, of); + print_entries(GIT_STATUS_INDEX_MODIFIED, sl, true, of); + print_entries(GIT_STATUS_INDEX_DELETED, sl, true, of); + print_entries(GIT_STATUS_INDEX_RENAMED, sl, true, of); + print_entries(GIT_STATUS_INDEX_TYPECHANGE, sl, true, of); + if (is_long) + { + std::cout << std::endl; + } + } + + if (sl.has_notstagged_header()) + { + if (is_long) + { + std::cout << notstagged_header << std::endl; + } + print_entries(GIT_STATUS_WT_MODIFIED, sl, false, of); + print_entries(GIT_STATUS_WT_DELETED, sl, false, of); + print_entries(GIT_STATUS_WT_TYPECHANGE, sl, false, of); + print_entries(GIT_STATUS_WT_RENAMED, sl, false, of); + if (is_long) + { + std::cout << std::endl; + } + } + + if (sl.has_untracked_header()) + { + if (is_long) + { + std::cout << untracked_header << std::endl; + } + print_entries(GIT_STATUS_WT_NEW, sl, false, of); + if (is_long) + { + std::cout << std::endl; + } + } + + if (sl.has_ignored_header()) + { + if (is_long) + { + std::cout << ignored_header << std::endl; + } + print_entries(GIT_STATUS_IGNORED, sl, false, of); + if (is_long) + { + std::cout << std::endl; + } + } +} diff --git a/src/subcommand/status_subcommand.hpp b/src/subcommand/status_subcommand.hpp new file mode 100644 index 0000000..0f2b63f --- /dev/null +++ b/src/subcommand/status_subcommand.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "../utils/common.hpp" + +class status_subcommand +{ +public: + + explicit status_subcommand(const libgit2_object&, CLI::App& app); + void run(); + +private: + bool short_flag = false; + bool long_flag = false; +}; diff --git a/src/utils/common.cpp b/src/utils/common.cpp new file mode 100644 index 0000000..908d9b1 --- /dev/null +++ b/src/utils/common.cpp @@ -0,0 +1,25 @@ +#include + +#include + +#include "common.hpp" + +libgit2_object::libgit2_object() +{ + git_libgit2_init(); +} + +libgit2_object::~libgit2_object() +{ + git_libgit2_shutdown(); +} + +std::string get_current_git_path() +{ + return std::filesystem::current_path(); // TODO: make sure that it goes to the root +} + +// // If directory not specified, uses cwd. +// sub->add_option("directory", directory, "info about directory arg") +// ->check(CLI::ExistingDirectory | CLI::NonexistentPath) +// ->default_val(std::filesystem::current_path()); diff --git a/src/utils/common.hpp b/src/utils/common.hpp new file mode 100644 index 0000000..ade2158 --- /dev/null +++ b/src/utils/common.hpp @@ -0,0 +1,59 @@ +#pragma once + +#include +#include + +class noncopyable_nonmovable +{ +public: + noncopyable_nonmovable(const noncopyable_nonmovable&) = delete; + noncopyable_nonmovable& operator=(const noncopyable_nonmovable&) = delete; + noncopyable_nonmovable(noncopyable_nonmovable&&) = delete; + noncopyable_nonmovable& operator=(noncopyable_nonmovable&&) = delete; + +protected: + noncopyable_nonmovable() = default; + ~noncopyable_nonmovable() = default; +}; + +template +class wrapper_base +{ +public: + using resource_type = T; + + wrapper_base(const wrapper_base&) = delete; + wrapper_base& operator=(const wrapper_base&) = delete; + + wrapper_base(wrapper_base&& rhs) + : p_resource(rhs.p_resource) + { + rhs.p_resource = nullptr; + } + wrapper_base& operator=(wrapper_base&& rhs) + { + std::swap(p_resource, rhs.p_resource); + return this; + } + + operator resource_type*() const noexcept + { + return p_resource; + } + +protected: + // Allocation and deletion of p_resource must be handled by inheriting class. + wrapper_base() = default; + ~wrapper_base() = default; + resource_type* p_resource = nullptr; +}; + +class libgit2_object : private noncopyable_nonmovable +{ +public: + + libgit2_object(); + ~libgit2_object(); +}; + +std::string get_current_git_path(); diff --git a/src/git_exception.cpp b/src/utils/git_exception.cpp similarity index 100% rename from src/git_exception.cpp rename to src/utils/git_exception.cpp diff --git a/src/git_exception.hpp b/src/utils/git_exception.hpp similarity index 100% rename from src/git_exception.hpp rename to src/utils/git_exception.hpp diff --git a/src/utils/meson.build b/src/utils/meson.build new file mode 100644 index 0000000..5d728df --- /dev/null +++ b/src/utils/meson.build @@ -0,0 +1,4 @@ +utils_files = files([ + 'common.cpp', + 'git_exception.cpp', +]) diff --git a/src/wrapper/base_wrapper.cpp b/src/wrapper/base_wrapper.cpp deleted file mode 100644 index fe217aa..0000000 --- a/src/wrapper/base_wrapper.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#include "../git_exception.hpp" -#include "base_wrapper.hpp" - -BaseWrapper::BaseWrapper() -{ - // Fine to initialise libgit2 multiple times. - throwIfError(git_libgit2_init()); -} - -BaseWrapper::~BaseWrapper() -{ - git_libgit2_shutdown(); -} diff --git a/src/wrapper/base_wrapper.hpp b/src/wrapper/base_wrapper.hpp deleted file mode 100644 index 3bf809f..0000000 --- a/src/wrapper/base_wrapper.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -class BaseWrapper -{ -public: - BaseWrapper(); - - virtual ~BaseWrapper(); - - BaseWrapper(const BaseWrapper&) = delete; - BaseWrapper& operator=(const BaseWrapper&) = delete; - BaseWrapper(BaseWrapper&&) = delete; - BaseWrapper& operator=(BaseWrapper&&) = delete; -}; diff --git a/src/wrapper/meson.build b/src/wrapper/meson.build index 5af7922..7f5e414 100644 --- a/src/wrapper/meson.build +++ b/src/wrapper/meson.build @@ -1,4 +1,4 @@ wrapper_files = files([ - 'base_wrapper.cpp', 'repository_wrapper.cpp', + 'status_wrapper.cpp', ]) diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index 8788bb3..b27adc4 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -1,22 +1,23 @@ -#include -#include - -#include "../git_exception.hpp" +#include "../utils/git_exception.hpp" #include "repository_wrapper.hpp" -RepositoryWrapper::RepositoryWrapper() - : _repo(nullptr) -{} -RepositoryWrapper::~RepositoryWrapper() +repository_wrapper::~repository_wrapper() +{ + git_repository_free(p_resource); + p_resource=nullptr; +} + +repository_wrapper repository_wrapper::open(const std::string& directory) { - if (_repo != nullptr) { - git_repository_free(_repo); // no return - } + repository_wrapper rw; + throwIfError(git_repository_open(&(rw.p_resource), directory.c_str())); + return rw; } -void RepositoryWrapper::init(const std::string& directory, bool bare) +repository_wrapper repository_wrapper::init(const std::string& directory, bool bare) { - // what if it is already initialised? Throw exception or delete and recreate? - throwIfError(git_repository_init(&_repo, directory.c_str(), bare)); + repository_wrapper rw; + throwIfError(git_repository_init(&(rw.p_resource), directory.c_str(), bare)); + return rw; } diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 3e4cbb8..2836636 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -1,16 +1,24 @@ #pragma once -#include "base_wrapper.hpp" +#include -class RepositoryWrapper : public BaseWrapper +#include + +#include "../utils/common.hpp" + +class repository_wrapper : public wrapper_base { public: - RepositoryWrapper(); - virtual ~RepositoryWrapper(); + ~repository_wrapper(); - void init(const std::string& directory, bool bare); + repository_wrapper(repository_wrapper&&) = default; + repository_wrapper& operator=(repository_wrapper&&) = default; + + static repository_wrapper init(const std::string& directory, bool bare); + static repository_wrapper open(const std::string& directory); private: - git_repository *_repo; + + repository_wrapper() = default; }; diff --git a/src/wrapper/status_wrapper.cpp b/src/wrapper/status_wrapper.cpp new file mode 100644 index 0000000..4f7a981 --- /dev/null +++ b/src/wrapper/status_wrapper.cpp @@ -0,0 +1,91 @@ +#include "../utils/git_exception.hpp" +#include "../wrapper/status_wrapper.hpp" + +status_list_wrapper::~status_list_wrapper() +{ + git_status_list_free(p_resource); + p_resource = nullptr; +} + +status_list_wrapper status_list_wrapper::status_list(const repository_wrapper& rw) +{ + status_list_wrapper res; + throwIfError(git_status_list_new(&(res.p_resource), rw, nullptr)); + + std::size_t status_list_size = git_status_list_entrycount(res.p_resource); + for (std::size_t i = 0; i < status_list_size; ++i) + { + auto entry = git_status_byindex(res.p_resource, i); + res.m_entries[entry->status].push_back(entry); + } + + if (!res.get_entry_list(GIT_STATUS_INDEX_NEW).empty() || !res.get_entry_list(GIT_STATUS_INDEX_MODIFIED).empty() || !res.get_entry_list(GIT_STATUS_INDEX_DELETED).empty() || !res.get_entry_list(GIT_STATUS_INDEX_RENAMED).empty() || !res.get_entry_list(GIT_STATUS_INDEX_TYPECHANGE).empty()) + { + res.m_tobecommited_header_flag = true; + } + if (!res.get_entry_list(GIT_STATUS_WT_NEW).empty()) + { + res.m_untracked_header_flag = true; + } + if (!res.get_entry_list(GIT_STATUS_WT_MODIFIED).empty() || !res.get_entry_list(GIT_STATUS_WT_DELETED).empty() || !res.get_entry_list(GIT_STATUS_WT_TYPECHANGE).empty() || !res.get_entry_list(GIT_STATUS_WT_RENAMED).empty()) + { + res.m_notstagged_header_flag = true; + } + if (!res.get_entry_list(GIT_STATUS_IGNORED).empty()) + { + res.m_ignored_header_flag = true; + } + // if (!res.tobecommited_header_flag) + // { + // res.m_nothingtocommit_message_flag = true; + // } + + return res; +} + +bool status_list_wrapper::has_untracked_header() const +{ + return m_untracked_header_flag; +} +bool status_list_wrapper::has_tobecommited_header() const +{ + return m_tobecommited_header_flag; +} +bool status_list_wrapper::has_ignored_header() const +{ + return m_ignored_header_flag; +} +bool status_list_wrapper::has_notstagged_header() const +{ + return m_notstagged_header_flag; +} +bool status_list_wrapper::has_nothingtocommit_message() const +{ + return m_nothingtocommit_message_flag; +} + +auto status_list_wrapper::get_entry_list(git_status_t status) const -> const status_entry_list& +{ + if (auto search = m_entries.find(status); search != m_entries.end()) + { + return search->second; + } + else + { + return m_empty; + } +} + + + +// std::ostream& operator<<(std::ostream& out, const status_list_wrapper& slw) +// { +// std::size_t status_list_size = git_status_list_entrycount(slw); +// for (std::size_t i = 0; i < status_list_size; ++i) +// { +// std::cout << i << " "; +// auto entry = git_status_byindex(slw, i); + +// } +// return out; +// }; diff --git a/src/wrapper/status_wrapper.hpp b/src/wrapper/status_wrapper.hpp new file mode 100644 index 0000000..4b7ee23 --- /dev/null +++ b/src/wrapper/status_wrapper.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +#include + +#include "../wrapper/repository_wrapper.hpp" + +class status_list_wrapper : public wrapper_base +{ +public: + using status_entry_list = std::vector; + + ~status_list_wrapper(); + + status_list_wrapper(status_list_wrapper&&) = default; + status_list_wrapper& operator=(status_list_wrapper&&) = default; + + static status_list_wrapper status_list(const repository_wrapper& wrapper); + + const status_entry_list& get_entry_list(git_status_t status) const; + + bool has_untracked_header() const; + bool has_tobecommited_header() const; + bool has_ignored_header() const; + bool has_notstagged_header() const; + bool has_nothingtocommit_message() const; + +private: + + status_list_wrapper() = default; + + using status_entry_map = std::map; + status_entry_map m_entries; + status_entry_list m_empty = {}; + bool m_untracked_header_flag = false; + bool m_tobecommited_header_flag = false; + bool m_ignored_header_flag = false; + bool m_notstagged_header_flag = false; + bool m_nothingtocommit_message_flag = false; +}; diff --git a/test/data/status_data/embeded_git/COMMIT_EDITMSG b/test/data/status_data/embeded_git/COMMIT_EDITMSG new file mode 100644 index 0000000..c133ee6 --- /dev/null +++ b/test/data/status_data/embeded_git/COMMIT_EDITMSG @@ -0,0 +1 @@ +Second commit diff --git a/test/data/status_data/embeded_git/HEAD b/test/data/status_data/embeded_git/HEAD new file mode 100644 index 0000000..b870d82 --- /dev/null +++ b/test/data/status_data/embeded_git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/main diff --git a/test/data/status_data/embeded_git/config b/test/data/status_data/embeded_git/config new file mode 100644 index 0000000..515f483 --- /dev/null +++ b/test/data/status_data/embeded_git/config @@ -0,0 +1,5 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true diff --git a/test/data/status_data/embeded_git/description b/test/data/status_data/embeded_git/description new file mode 100644 index 0000000..498b267 --- /dev/null +++ b/test/data/status_data/embeded_git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test/data/status_data/embeded_git/hooks/applypatch-msg.sample b/test/data/status_data/embeded_git/hooks/applypatch-msg.sample new file mode 100755 index 0000000..09b38a8 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/applypatch-msg.sample @@ -0,0 +1,15 @@ +#!/usr/bin/sh +# +# An example hook script to check the commit log message taken by +# applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. The hook is +# allowed to edit the commit message file. +# +# To enable this hook, rename this file to "applypatch-msg". + +. git-sh-setup +commitmsg="$(git rev-parse --git-path hooks/commit-msg)" +test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"} +: diff --git a/test/data/status_data/embeded_git/hooks/commit-msg.sample b/test/data/status_data/embeded_git/hooks/commit-msg.sample new file mode 100755 index 0000000..3ced146 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/commit-msg.sample @@ -0,0 +1,24 @@ +#!/usr/bin/sh +# +# An example hook script to check the commit log message. +# Called by "git commit" with one argument, the name of the file +# that has the commit message. The hook should exit with non-zero +# status after issuing an appropriate message if it wants to stop the +# commit. The hook is allowed to edit the commit message file. +# +# To enable this hook, rename this file to "commit-msg". + +# Uncomment the below to add a Signed-off-by line to the message. +# Doing this in a hook is a bad idea in general, but the prepare-commit-msg +# hook is more suited to it. +# +# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/test/data/status_data/embeded_git/hooks/fsmonitor-watchman.sample b/test/data/status_data/embeded_git/hooks/fsmonitor-watchman.sample new file mode 100755 index 0000000..23e856f --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/fsmonitor-watchman.sample @@ -0,0 +1,174 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + my $last_update_line = ""; + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + $last_update_line = qq[\n"since": $last_update_token,]; + } + my $query = <<" END"; + ["query", "$git_work_tree", {$last_update_line + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; }; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +} diff --git a/test/data/status_data/embeded_git/hooks/post-update.sample b/test/data/status_data/embeded_git/hooks/post-update.sample new file mode 100755 index 0000000..ee496e3 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/usr/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/test/data/status_data/embeded_git/hooks/pre-applypatch.sample b/test/data/status_data/embeded_git/hooks/pre-applypatch.sample new file mode 100755 index 0000000..845130a --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/pre-applypatch.sample @@ -0,0 +1,14 @@ +#!/usr/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +: diff --git a/test/data/status_data/embeded_git/hooks/pre-commit.sample b/test/data/status_data/embeded_git/hooks/pre-commit.sample new file mode 100755 index 0000000..44f7de3 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/pre-commit.sample @@ -0,0 +1,49 @@ +#!/usr/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff-index --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against -- diff --git a/test/data/status_data/embeded_git/hooks/pre-merge-commit.sample b/test/data/status_data/embeded_git/hooks/pre-merge-commit.sample new file mode 100755 index 0000000..09b65a5 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/usr/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: diff --git a/test/data/status_data/embeded_git/hooks/pre-push.sample b/test/data/status_data/embeded_git/hooks/pre-push.sample new file mode 100755 index 0000000..d5e513d --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/pre-push.sample @@ -0,0 +1,53 @@ +#!/usr/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 diff --git a/test/data/status_data/embeded_git/hooks/pre-rebase.sample b/test/data/status_data/embeded_git/hooks/pre-rebase.sample new file mode 100755 index 0000000..36efc64 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/pre-rebase.sample @@ -0,0 +1,169 @@ +#!/usr/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END diff --git a/test/data/status_data/embeded_git/hooks/pre-receive.sample b/test/data/status_data/embeded_git/hooks/pre-receive.sample new file mode 100755 index 0000000..6f473c3 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/pre-receive.sample @@ -0,0 +1,24 @@ +#!/usr/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi diff --git a/test/data/status_data/embeded_git/hooks/prepare-commit-msg.sample b/test/data/status_data/embeded_git/hooks/prepare-commit-msg.sample new file mode 100755 index 0000000..cd8e794 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/prepare-commit-msg.sample @@ -0,0 +1,42 @@ +#!/usr/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi diff --git a/test/data/status_data/embeded_git/hooks/push-to-checkout.sample b/test/data/status_data/embeded_git/hooks/push-to-checkout.sample new file mode 100755 index 0000000..badb4b4 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/push-to-checkout.sample @@ -0,0 +1,78 @@ +#!/usr/bin/sh + +# An example hook script to update a checked-out tree on a git push. +# +# This hook is invoked by git-receive-pack(1) when it reacts to git +# push and updates reference(s) in its repository, and when the push +# tries to update the branch that is currently checked out and the +# receive.denyCurrentBranch configuration variable is set to +# updateInstead. +# +# By default, such a push is refused if the working tree and the index +# of the remote repository has any difference from the currently +# checked out commit; when both the working tree and the index match +# the current commit, they are updated to match the newly pushed tip +# of the branch. This hook is to be used to override the default +# behaviour; however the code below reimplements the default behaviour +# as a starting point for convenient modification. +# +# The hook receives the commit with which the tip of the current +# branch is going to be updated: +commit=$1 + +# It can exit with a non-zero status to refuse the push (when it does +# so, it must not modify the index or the working tree). +die () { + echo >&2 "$*" + exit 1 +} + +# Or it can make any necessary changes to the working tree and to the +# index to bring them to the desired state when the tip of the current +# branch is updated to the new commit, and exit with a zero status. +# +# For example, the hook can simply run git read-tree -u -m HEAD "$1" +# in order to emulate git fetch that is run in the reverse direction +# with git push, as the two-tree form of git read-tree -u -m is +# essentially the same as git switch or git checkout that switches +# branches while keeping the local changes in the working tree that do +# not interfere with the difference between the branches. + +# The below is a more-or-less exact translation to shell of the C code +# for the default behaviour for git's push-to-checkout hook defined in +# the push_to_deploy() function in builtin/receive-pack.c. +# +# Note that the hook will be executed from the repository directory, +# not from the working tree, so if you want to perform operations on +# the working tree, you will have to adapt your code accordingly, e.g. +# by adding "cd .." or using relative paths. + +if ! git update-index -q --ignore-submodules --refresh +then + die "Up-to-date check failed" +fi + +if ! git diff-files --quiet --ignore-submodules -- +then + die "Working directory has unstaged changes" +fi + +# This is a rough translation of: +# +# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX +if git cat-file -e HEAD 2>/dev/null +then + head=HEAD +else + head=$(git hash-object -t tree --stdin &2 + exit 1 +} + +unset GIT_DIR GIT_WORK_TREE +cd "$worktree" && + +if grep -q "^diff --git " "$1" +then + validate_patch "$1" +else + validate_cover_letter "$1" +fi && + +if test "$GIT_SENDEMAIL_FILE_COUNTER" = "$GIT_SENDEMAIL_FILE_TOTAL" +then + git config --unset-all sendemail.validateWorktree && + trap 'git worktree remove -ff "$worktree"' EXIT && + validate_series +fi diff --git a/test/data/status_data/embeded_git/hooks/update.sample b/test/data/status_data/embeded_git/hooks/update.sample new file mode 100755 index 0000000..5503c12 --- /dev/null +++ b/test/data/status_data/embeded_git/hooks/update.sample @@ -0,0 +1,128 @@ +#!/usr/bin/sh +# +# An example hook script to block unannotated tags from entering. +# Called by "git receive-pack" with arguments: refname sha1-old sha1-new +# +# To enable this hook, rename this file to "update". +# +# Config +# ------ +# hooks.allowunannotated +# This boolean sets whether unannotated tags will be allowed into the +# repository. By default they won't be. +# hooks.allowdeletetag +# This boolean sets whether deleting tags will be allowed in the +# repository. By default they won't be. +# hooks.allowmodifytag +# This boolean sets whether a tag may be modified after creation. By default +# it won't be. +# hooks.allowdeletebranch +# This boolean sets whether deleting branches will be allowed in the +# repository. By default they won't be. +# hooks.denycreatebranch +# This boolean sets whether remotely creating branches will be denied +# in the repository. By default this is allowed. +# + +# --- Command line +refname="$1" +oldrev="$2" +newrev="$3" + +# --- Safety check +if [ -z "$GIT_DIR" ]; then + echo "Don't run this script from the command line." >&2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 )" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 " >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero=$(git hash-object --stdin &2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0 diff --git a/test/data/status_data/embeded_git/index b/test/data/status_data/embeded_git/index new file mode 100644 index 0000000..4c1df39 Binary files /dev/null and b/test/data/status_data/embeded_git/index differ diff --git a/test/data/status_data/embeded_git/info/exclude b/test/data/status_data/embeded_git/info/exclude new file mode 100644 index 0000000..a5196d1 --- /dev/null +++ b/test/data/status_data/embeded_git/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/test/data/status_data/embeded_git/logs/HEAD b/test/data/status_data/embeded_git/logs/HEAD new file mode 100644 index 0000000..a996210 --- /dev/null +++ b/test/data/status_data/embeded_git/logs/HEAD @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 75743dcbd85064226c77a0b862af817838ae0b2e Sandrine Pataut 1750769952 +0200 commit (initial): first commit +75743dcbd85064226c77a0b862af817838ae0b2e ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1750771272 +0200 commit: Second commit diff --git a/test/data/status_data/embeded_git/logs/refs/heads/main b/test/data/status_data/embeded_git/logs/refs/heads/main new file mode 100644 index 0000000..a996210 --- /dev/null +++ b/test/data/status_data/embeded_git/logs/refs/heads/main @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 75743dcbd85064226c77a0b862af817838ae0b2e Sandrine Pataut 1750769952 +0200 commit (initial): first commit +75743dcbd85064226c77a0b862af817838ae0b2e ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1750771272 +0200 commit: Second commit diff --git a/test/data/status_data/embeded_git/objects/75/743dcbd85064226c77a0b862af817838ae0b2e b/test/data/status_data/embeded_git/objects/75/743dcbd85064226c77a0b862af817838ae0b2e new file mode 100644 index 0000000..fbfd68e Binary files /dev/null and b/test/data/status_data/embeded_git/objects/75/743dcbd85064226c77a0b862af817838ae0b2e differ diff --git a/test/data/status_data/embeded_git/objects/9c/a9a8716bf7f5ab1ff449be0c97a04a7aae4262 b/test/data/status_data/embeded_git/objects/9c/a9a8716bf7f5ab1ff449be0c97a04a7aae4262 new file mode 100644 index 0000000..ca32467 Binary files /dev/null and b/test/data/status_data/embeded_git/objects/9c/a9a8716bf7f5ab1ff449be0c97a04a7aae4262 differ diff --git a/test/data/status_data/embeded_git/objects/bd/ef436c07ee899d19659581d2944f573ac620e0 b/test/data/status_data/embeded_git/objects/bd/ef436c07ee899d19659581d2944f573ac620e0 new file mode 100644 index 0000000..0d73591 Binary files /dev/null and b/test/data/status_data/embeded_git/objects/bd/ef436c07ee899d19659581d2944f573ac620e0 differ diff --git a/test/data/status_data/embeded_git/objects/dc/430e3c2e6e1193b377040126b95099261e7251 b/test/data/status_data/embeded_git/objects/dc/430e3c2e6e1193b377040126b95099261e7251 new file mode 100644 index 0000000..fd8249d Binary files /dev/null and b/test/data/status_data/embeded_git/objects/dc/430e3c2e6e1193b377040126b95099261e7251 differ diff --git a/test/data/status_data/embeded_git/objects/e3/323b422ccebe9515b4aa335b40615f398b6553 b/test/data/status_data/embeded_git/objects/e3/323b422ccebe9515b4aa335b40615f398b6553 new file mode 100644 index 0000000..ba3fb25 Binary files /dev/null and b/test/data/status_data/embeded_git/objects/e3/323b422ccebe9515b4aa335b40615f398b6553 differ diff --git a/test/data/status_data/embeded_git/objects/ee/8c4cf874c4f1e3ba755f929fe7811018adee3d b/test/data/status_data/embeded_git/objects/ee/8c4cf874c4f1e3ba755f929fe7811018adee3d new file mode 100644 index 0000000..ce96d7b --- /dev/null +++ b/test/data/status_data/embeded_git/objects/ee/8c4cf874c4f1e3ba755f929fe7811018adee3d @@ -0,0 +1,2 @@ +xK +1 @] &M xX/7p-.46wm3#B*V+qUD咘ɭ@X"}/99H W/\uq6hWۤPA؋G]63VoνOu \ No newline at end of file diff --git a/test/data/status_data/embeded_git/objects/fd/511fbb5dd2860baabf28e298fa3373634e8660 b/test/data/status_data/embeded_git/objects/fd/511fbb5dd2860baabf28e298fa3373634e8660 new file mode 100644 index 0000000..476418f Binary files /dev/null and b/test/data/status_data/embeded_git/objects/fd/511fbb5dd2860baabf28e298fa3373634e8660 differ diff --git a/test/data/status_data/embeded_git/refs/heads/main b/test/data/status_data/embeded_git/refs/heads/main new file mode 100644 index 0000000..6d6a647 --- /dev/null +++ b/test/data/status_data/embeded_git/refs/heads/main @@ -0,0 +1 @@ +ee8c4cf874c4f1e3ba755f929fe7811018adee3d diff --git a/test/data/status_data/last_test_file.txt b/test/data/status_data/last_test_file.txt new file mode 100644 index 0000000..975f660 --- /dev/null +++ b/test/data/status_data/last_test_file.txt @@ -0,0 +1 @@ +That's the last test file. diff --git a/test/data/status_data/other_test_file_new.txt b/test/data/status_data/other_test_file_new.txt new file mode 100644 index 0000000..9ca9a87 --- /dev/null +++ b/test/data/status_data/other_test_file_new.txt @@ -0,0 +1 @@ +This is another file. diff --git a/test/data/status_data/random_file.txt b/test/data/status_data/random_file.txt new file mode 100644 index 0000000..c0fc9fb --- /dev/null +++ b/test/data/status_data/random_file.txt @@ -0,0 +1 @@ +That's a random file modified. diff --git a/test/test_status.py b/test/test_status.py new file mode 100644 index 0000000..87abb3d --- /dev/null +++ b/test/test_status.py @@ -0,0 +1,37 @@ +# from pathlib import Path +import os +import subprocess + +import pytest + + +@pytest.fixture +def rename_git(): + os.rename("test/data/status_data/embeded_git/", "test/data/status_data/.git/") + yield + os.rename("test/data/status_data/.git/", "test/data/status_data/embeded_git/") + + +@pytest.mark.parametrize("short_flag", ["", "-s", "--short"]) +@pytest.mark.parametrize("long_flag", ["", "--long"]) +def test_status_format(rename_git, git2cpp_path, short_flag, long_flag): + cmd = [git2cpp_path, 'status'] + if short_flag != "": + cmd.append(short_flag) + if long_flag != "": + cmd.append(long_flag) + p = subprocess.run(cmd, capture_output=True, cwd="test/data/status_data", text=True) + + if (long_flag == "--long") or ((long_flag == "") & (short_flag == "")): + assert "Changes to be committed" in p.stdout + assert "Changes not staged for commit" in p.stdout + assert "Untracked files" in p.stdout + assert "new file" in p.stdout + assert "deleted" in p.stdout + assert "modified" in p.stdout + + elif short_flag in ["-s", "--short"]: + assert "D " in p.stdout + assert " M " in p.stdout + assert " D " in p.stdout + assert "?? " in p.stdout