From 2236247b9692e717204c67e076b5a46dc4469ccd Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 20 Sep 2021 23:04:17 -0700 Subject: [PATCH] major refactor refactored into shell script added scripts as tests removed old project in C removed autotools dependency added Makefile --- .gitignore | 26 +- Dockerfile | 77 -- Makefile | 21 + Makefile.am | 76 -- configure.ac | 72 - git-extra | 310 +++++ include/add.h | 22 - include/branch.h | 23 - include/cat.h | 12 - include/commit.h | 12 - include/default.h | 16 - include/edit.h | 17 - include/editor.h | 21 - include/exec.h | 17 - include/git.h | 68 - include/hash.h | 11 - include/log.h | 18 - include/ls.h | 13 - include/main.h | 27 - include/opt.h | 25 - include/prepend.h | 10 - include/templ.h | 19 - src/add.c | 70 - src/branch.c | 115 -- src/cat.c | 14 - src/commit.c | 44 - src/default.c | 78 -- src/edit.c | 38 - src/editor.c | 35 - src/exec.c | 121 -- src/git.c | 24 - src/hash.c | 9 - src/log.c | 14 - src/ls.c | 13 - src/main.c | 104 -- src/prepend.c | 23 - src/templ.c | 73 -- test/add.tests.sh | 74 ++ test/add_to_index.tests.sh | 31 + test/branch_create.tests.sh | 23 + test/branch_exists.tests.sh | 35 + test/branch_hash.tests.sh | 27 + test/branch_reset.tests.sh | 34 + test/cat_file.tests.sh | 28 + test/edit.tests.sh | 41 + test/editor.tests.sh | 47 + test/integration/Makefile.am | 67 - test/integration/index.js | 1 - test/integration/package-lock.json | 1166 ----------------- test/integration/package.json | 14 - .../integration/test/add.integration.tests.js | 79 -- .../test/branch.pattern.integration.test.js | 19 - .../integration/test/cat.integration.tests.js | 46 - .../test/edit.integration.tests.js | 43 - test/integration/test/ls.integration.tests.js | 75 -- test/integration/test/utils.js | 25 - test/ls.tests.sh | 29 + test/parse_arguments.tests.sh | 46 + test/setup.sh | 55 + test/unit/Makefile.am | 100 -- test/unit/add.tests.c | 18 - test/unit/add.tests.h | 12 - test/unit/branch.tests.c | 44 - test/unit/branch.tests.h | 14 - test/unit/cat.tests.c | 19 - test/unit/cat.tests.h | 11 - test/unit/commit.tests.c | 30 - test/unit/commit.tests.h | 11 - test/unit/default.tests.c | 26 - test/unit/default.tests.h | 14 - test/unit/edit.tests.c | 21 - test/unit/edit.tests.h | 12 - test/unit/editor.tests.c | 21 - test/unit/editor.tests.h | 12 - test/unit/hash.tests.c | 20 - test/unit/hash.tests.h | 11 - test/unit/templ.tests.c | 25 - test/unit/templ.tests.h | 11 - test/unit/test_utils.c | 29 - test/unit/test_utils.h | 12 - test/usage.tests.sh | 24 + 81 files changed, 827 insertions(+), 3363 deletions(-) delete mode 100644 Dockerfile create mode 100644 Makefile delete mode 100644 Makefile.am delete mode 100644 configure.ac create mode 100644 git-extra delete mode 100644 include/add.h delete mode 100644 include/branch.h delete mode 100644 include/cat.h delete mode 100644 include/commit.h delete mode 100644 include/default.h delete mode 100644 include/edit.h delete mode 100644 include/editor.h delete mode 100644 include/exec.h delete mode 100644 include/git.h delete mode 100644 include/hash.h delete mode 100644 include/log.h delete mode 100644 include/ls.h delete mode 100644 include/main.h delete mode 100644 include/opt.h delete mode 100644 include/prepend.h delete mode 100644 include/templ.h delete mode 100644 src/add.c delete mode 100644 src/branch.c delete mode 100644 src/cat.c delete mode 100644 src/commit.c delete mode 100644 src/default.c delete mode 100644 src/edit.c delete mode 100644 src/editor.c delete mode 100644 src/exec.c delete mode 100644 src/git.c delete mode 100644 src/hash.c delete mode 100644 src/log.c delete mode 100644 src/ls.c delete mode 100644 src/main.c delete mode 100644 src/prepend.c delete mode 100644 src/templ.c create mode 100755 test/add.tests.sh create mode 100755 test/add_to_index.tests.sh create mode 100755 test/branch_create.tests.sh create mode 100755 test/branch_exists.tests.sh create mode 100755 test/branch_hash.tests.sh create mode 100755 test/branch_reset.tests.sh create mode 100755 test/cat_file.tests.sh create mode 100755 test/edit.tests.sh create mode 100755 test/editor.tests.sh delete mode 100644 test/integration/Makefile.am delete mode 100644 test/integration/index.js delete mode 100644 test/integration/package-lock.json delete mode 100644 test/integration/package.json delete mode 100644 test/integration/test/add.integration.tests.js delete mode 100644 test/integration/test/branch.pattern.integration.test.js delete mode 100644 test/integration/test/cat.integration.tests.js delete mode 100644 test/integration/test/edit.integration.tests.js delete mode 100644 test/integration/test/ls.integration.tests.js delete mode 100644 test/integration/test/utils.js create mode 100755 test/ls.tests.sh create mode 100755 test/parse_arguments.tests.sh create mode 100755 test/setup.sh delete mode 100644 test/unit/Makefile.am delete mode 100644 test/unit/add.tests.c delete mode 100644 test/unit/add.tests.h delete mode 100644 test/unit/branch.tests.c delete mode 100644 test/unit/branch.tests.h delete mode 100644 test/unit/cat.tests.c delete mode 100644 test/unit/cat.tests.h delete mode 100644 test/unit/commit.tests.c delete mode 100644 test/unit/commit.tests.h delete mode 100644 test/unit/default.tests.c delete mode 100644 test/unit/default.tests.h delete mode 100644 test/unit/edit.tests.c delete mode 100644 test/unit/edit.tests.h delete mode 100644 test/unit/editor.tests.c delete mode 100644 test/unit/editor.tests.h delete mode 100644 test/unit/hash.tests.c delete mode 100644 test/unit/hash.tests.h delete mode 100644 test/unit/templ.tests.c delete mode 100644 test/unit/templ.tests.h delete mode 100644 test/unit/test_utils.c delete mode 100644 test/unit/test_utils.h create mode 100755 test/usage.tests.sh diff --git a/.gitignore b/.gitignore index b1eaae0..07d19e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,24 +1,2 @@ -# autoconf/automake -Makefile -*.in -*.in~ -aclocal.m4 -autom4te.cache -autoscan.log -build-aux -config.log -config.status -configure -configure.scan -include/config.h -include/stamp-h1 -src/.deps -src/.dirstamp -test/unit/.deps - -# build objects -*.o -*.log -*.trs -*.tests -git-ex \ No newline at end of file +test/source.sh +test/.testdir diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 559c4cd..0000000 --- a/Dockerfile +++ /dev/null @@ -1,77 +0,0 @@ -FROM debian:latest as base - -RUN apt-get update -RUN apt-get install -y git autoconf automake gcc make vim - -FROM base as unit-tester - -RUN apt-get install -y gdb vim valgrind - -FROM base as core - -WORKDIR /build - -COPY Makefile.am . -COPY configure.ac . - -COPY README.md . -COPY templates templates/ - -COPY test test/ - -COPY include include/ -RUN autoreconf -vif - -COPY src src/ -RUN ./configure - -RUN make -RUN make distcheck -RUN make install - -FROM unit-tester - -WORKDIR /build -COPY --from=core /build /build - -COPY --from=core /usr/local/share/git-extra/templates/ /usr/local/share/git-extra/templates/ - -RUN ./configure -RUN make check || cat test/unit/test-suite.log - -FROM node:latest as integration-tester - -COPY --from=core /usr/local/bin/git-ex /usr/local/bin/git-ex -COPY --from=core /usr/local/share/git-extra/templates/ /usr/local/share/git-extra/templates/ - -USER node -WORKDIR /home/node - -COPY --from=core --chown=node:node /build/test/integration/index.js . -COPY --from=core --chown=node:node /build/test/integration/package.json . -COPY --from=core --chown=node:node /build/test/integration/package-lock.json . - -RUN npm install - -COPY --from=core --chown=node:node /build/test/integration/test/ test/ - -RUN npm test - -FROM core as final - -WORKDIR /root -RUN git init working - -# setup test repository; Docker image meant to be a sandbox -WORKDIR /root/working - -RUN echo "test" > test.txt -RUN git config --global core.editor vim - -RUN git config --local user.name "test" -RUN git config --local user.email "test@email" - -RUN git add -A -RUN git commit -a -m "..." - -ENTRYPOINT bash \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c36ab6c --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin + +all: + @echo "git-extra is a shell script; nothing to make. Try 'make install' instead." + +check: + @head -n `grep -n "main()" git-extra | cut -f1 -d:` git-extra | head -n -1 > test/source.sh + @cd test && run-parts --exit-on-error --regex '.*\.tests\.sh' . + +clean: + @rm -vrf test/.testdir + +install: + @install -v -d "$(BINDIR)/" && install -m 0755 -v git-extra "$(BINDIR)/git-ex" + +uninstall: + @rm -vrf \ + $(BINDIR)/git-ex + +.PHONY: install uninstall check clean diff --git a/Makefile.am b/Makefile.am deleted file mode 100644 index 6334348..0000000 --- a/Makefile.am +++ /dev/null @@ -1,76 +0,0 @@ -templatesdir=$(datadir)/git-extra/templates - -AM_CPPFLAGS = \ - -I$(top_builddir)/include/ \ - -I$(top_srcdir)/include/ \ - -DTEMPLATE_DIR='"$(templatesdir)"' - -bin_PROGRAMS = git-ex -git_ex_SOURCES = \ - src/add.c \ - src/branch.c \ - src/cat.c \ - src/commit.c \ - src/default.c \ - src/edit.c \ - src/editor.c \ - src/exec.c \ - src/git.c \ - src/hash.c \ - src/log.c \ - src/ls.c \ - src/main.c \ - src/prepend.c \ - src/templ.c -git_ex_SOURCES += \ - include/add.h \ - include/branch.h \ - include/cat.h \ - include/commit.h \ - include/default.h \ - include/edit.h \ - include/editor.h \ - include/exec.h \ - include/git.h \ - include/hash.h \ - include/log.h \ - include/ls.h \ - include/main.h \ - include/opt.h \ - include/prepend.h \ - include/templ.h - -dist_doc_DATA = $(top_srcdir)/README.md -dist_templates_DATA = \ - $(top_srcdir)/templates/bug.template - -SUBDIRS = test/integration test/unit - -if HAVE_DOCKER -IMAGE_NAME=git-extra -CONTAINER_NAME=git-extra-test -build: - docker build -t $(IMAGE_NAME) . - -run: build - docker run -d \ - --name $(CONTAINER_NAME) \ - --cap-add=SYS_PTRACE --security-opt seccomp=unconfined \ - $(IMAGE_NAME) - -start: build - -docker run -it --rm \ - --cap-add=SYS_PTRACE --security-opt seccomp=unconfined \ - $(IMAGE_NAME) - -stop: - docker stop $(CONTAINER_NAME) - docker rm $(CONTAINER_NAME); -else -build: missing-local -run: missing-local -start: missing-local -stop: missing-local -missing-local: - @echo "docker not found" -endif \ No newline at end of file diff --git a/configure.ac b/configure.ac deleted file mode 100644 index bc6f3f6..0000000 --- a/configure.ac +++ /dev/null @@ -1,72 +0,0 @@ -AC_PREREQ([2.69]) -AC_INIT([git-extra], [0.0.0]) - -# Store build files not in main directory -AC_CONFIG_AUX_DIR([build-aux]) - -AM_INIT_AUTOMAKE([foreign subdir-objects nostdinc -Wall -Werror]) - -AC_CONFIG_SRCDIR([src/main.c]) -AC_CONFIG_HEADERS([include/config.h]) - -AC_ARG_ENABLE([memcheck], - [AS_HELP_STRING([--disable-memcheck], - [disable valgrind (enabled by default)])], - [enable_memcheck=$enableval], - [enable_memcheck=yes]) - -AC_PATH_PROG([GIT], [git]) -AM_CONDITIONAL([HAVE_GIT], [test -n "$GIT"]) -AM_COND_IF([HAVE_GIT],,[AC_MSG_ERROR([Please install git before trying to build git-extra])]) - -AC_PATH_PROG([DOCKER], [docker]) -AM_CONDITIONAL([HAVE_DOCKER], [test -n "$DOCKER"]) - -AC_PATH_PROG([NODE], [node]) -AM_CONDITIONAL([HAVE_NODE], [test -n "$NODE"]) - -AC_PATH_PROG([VALGRIND], [valgrind]) -AM_CONDITIONAL([HAVE_VALGRIND], [test -n "$VALGRIND"]) - -dnl disable memcheck if valgrind not found -if test "x$enable_memcheck" = "xyes"; then - if test -z "$VALGRIND"; then - enable_memcheck=no - fi -fi - -AC_MSG_CHECKING([if memcheck should be enabled]) -if test x$enable_memcheck != xno; then - AC_MSG_RESULT(yes) -else - AC_MSG_RESULT(no) -fi - -AM_CONDITIONAL([ENABLE_MEMCHECK],[test x$enable_memcheck = xyes]) - -# Checks for programs. -AC_PROG_CC -AC_PROG_INSTALL - -# Checks for libraries. - -# Checks for header files. -AC_CHECK_HEADERS([stddef.h stdlib.h string.h unistd.h]) - -# Checks for typedefs, structures, and compiler characteristics. -AC_TYPE_OFF_T -AC_TYPE_PID_T -AC_TYPE_SIZE_T -AC_TYPE_SSIZE_T - -# Checks for library functions. -AC_FUNC_FORK -AC_FUNC_MALLOC -AC_CHECK_FUNCS([dup2 getcwd memset setenv]) - -AC_CONFIG_FILES([ - Makefile - test/integration/Makefile - test/unit/Makefile -]) -AC_OUTPUT diff --git a/git-extra b/git-extra new file mode 100644 index 0000000..f566e17 --- /dev/null +++ b/git-extra @@ -0,0 +1,310 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# Option variables +LOG_LEVEL=default +BRANCH=extra +EDITOR= +MESSAGE= +TEMPLATE= + +# helper variables +LONG_OUTPUT=false +POSITIONAL_ARGUMENTS= +PREFIX= +PREV_COMMIT= +UTILITY=ls +WORKING_BRANCH= + +detect_git() { + if [[ -z "`git --version`" ]]; then + log_err "git not found in path" + exit 1 + fi +} + +detect_commits() { + set +e + git rev-list --count HEAD > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + log_err "no commits found, create initial commit first" + exit 1 + fi + set -e +} + +log_info() { + if [[ "$LOG_LEVEL" == "info" ]]; then + log "$@" + fi +} + +log_err() { + echo -e "$@" +} + +log() { + if [[ "$LOG_LEVEL" != "error" ]]; then + echo -e "$@" + fi +} + +usage() { + log_err "Usage:" + log_err "" + log_err "\tgit extra [options] [add|cat|edit|ls] [path]" + log_err "" + log_err "Options:" + log_err "\t--branch, -b " + log_err "\t--editor, -e " + log_err "\t--long, -l" + log_err "\t--message, -m " + log_err "\t--quiet, -q" + log_err "\t--template, -t