From 86bb1a6764d88db1761d6625d08571d0b96ed0ba Mon Sep 17 00:00:00 2001 From: overengineer <54alpersaid@gmail.com> Date: Fri, 8 Oct 2021 06:58:15 +0300 Subject: [PATCH 1/2] Added git-magic command --- Commands.md | 5 + bin/git-magic | 87 ++++++++++++++++++ etc/git-extras-completion.zsh | 1 + man/git-extras.md | 1 + man/git-magic.1 | 102 +++++++++++++++++++++ man/git-magic.html | 167 ++++++++++++++++++++++++++++++++++ man/git-magic.md | 85 +++++++++++++++++ man/index.txt | 1 + 8 files changed, 449 insertions(+) create mode 100755 bin/git-magic create mode 100644 man/git-magic.1 create mode 100644 man/git-magic.html create mode 100644 man/git-magic.md diff --git a/Commands.md b/Commands.md index 9e01de1..4282e10 100644 --- a/Commands.md +++ b/Commands.md @@ -36,6 +36,7 @@ - [`git local-commits`](#git-local-commits) - [`git lock`](#git-lock) - [`git locked`](#git-locked) + - [`git magic`](#git-magic) - [`git merge-into`](#git-merge-into) - [`git merge-repo`](#git-merge-repo) - [`git missing`](#git-missing) @@ -1544,3 +1545,7 @@ total 308 ## git abort Abort current rebase, merge or cherry-pick, without the need to find exact command in history. + +## git magic + +Commits changes with a generated message. diff --git a/bin/git-magic b/bin/git-magic new file mode 100755 index 0000000..b6b7b6f --- /dev/null +++ b/bin/git-magic @@ -0,0 +1,87 @@ +#!/usr/bin/env bash + + +# Bash unofficial strict mode +set -eo pipefail +IFS=$'\n\t' + +cd "$(git rev-parse --show-toplevel)" + +USAGE='git-magic [-a] [-m msg] [-e] [-p] [-f]' + +ALL=false +PUSH=false +FORCE='' +ARGS=() + +while getopts "m:eapfh" arg; do + case "${arg}" in + m) + ARGS+=("-m") + ARGS+=("${OPTARG}") + ;; + e) + ARGS+=("-e") + ;; + a) + ALL=true + ;; + p) + PUSH=true + ;; + f) + FORCE='-f' + ;; + h) + echo $USAGE + exit 0 + ;; + ?) + echo "${USAGE}" + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) + +if [[ $# != 0 ]]; then + echo "Unknown arguments: $@" + echo "${USAGE}" + exit 1 +fi + +set -- "${ARGS[@]}" # restore positional parameters + +if [[ $ALL == true ]]; then + + # Check if there is no changes to stage + if [[ -z $(git status --porcelain) ]]; then + echo "No changes to commit" + exit 0 + fi + + # Get confirmation from user + git status + echo "Everything will be added" + read -p "Press enter to continue" + + # Restore staging area so that, for example, + # add and modify will not be separate entries in status + git restore --staged . + git add . +fi + +# Commit with generated message +git commit --no-edit "$@" -m "$(git status --porcelain -uno)" + +if [[ $PUSH == true ]]; then + git push $FORCE +fi + +# --no-edit by default. use option -e to override this + +# Arguments are passed with quoted "$@" to avoid misparsing + +# Generated message comes after user provided arguments +# so that user can insert title before it diff --git a/etc/git-extras-completion.zsh b/etc/git-extras-completion.zsh index 2d8bf57..cbc536f 100644 --- a/etc/git-extras-completion.zsh +++ b/etc/git-extras-completion.zsh @@ -379,6 +379,7 @@ zstyle ':completion:*:*:git:*' user-commands $existing_user_commands \ local-commits:'list local commits' \ lock:'lock a file excluded from version control' \ locked:'ls files that have been locked' \ + magic:'commits everything with a generated message' \ merge-into:'merge one branch into another' \ merge-repo:'merge two repo histories' \ missing:'show commits missing from another branch' \ diff --git a/man/git-extras.md b/man/git-extras.md index 90b74e1..1b3cd4f 100644 --- a/man/git-extras.md +++ b/man/git-extras.md @@ -63,6 +63,7 @@ git-extras(1) -- Awesome GIT utilities - **git-local-commits(1)** List local commits - **git-lock(1)** Lock a file excluded from version control - **git-locked(1)** ls files that have been locked + - **git-magic(1)** Automate add/commit/push routines - **git-merge-into(1)** Merge one branch into another - **git-merge-repo(1)** Merge two repo histories - **git-missing(1)** Show commits missing from another branch diff --git a/man/git-magic.1 b/man/git-magic.1 new file mode 100644 index 0000000..5978a30 --- /dev/null +++ b/man/git-magic.1 @@ -0,0 +1,102 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "GIT\-MAGIC" "1" "October 2021" "" "Git Extras" +. +.SH "NAME" +\fBgit\-magic\fR \- Automate add/commit/push routines +. +.SH "SYNOPSIS" +\fBgit\-magic\fR [\-a] [\-m \fImsg\fR] [\-e] [\-p] [\-f] +. +.SH "DESCRIPTION" +Produces summary of changes for commit message from \fBgit status \-\-porcelain\fR output\. Commits staged changes with the generated commit message and opens editor to modify generated commit message optionally\. Also staging and pushing can be automated optinally\. +. +.SH "OPTIONS" +\-a +. +.P +Adds everything including untracked files\. +. +.P +\-m \fImsg\fR +. +.P +Use the given \fImsg\fR as the commit message\. If multiple \-m options are given, their values are concatenated as separate paragraphs\. Passed to git commit command\. The generated is appended to user\-given messages\. +. +.P +\-e +. +.P +This option lets you further edit the generated message\. Passed to git commit command\. +. +.P +\-p +. +.P +Runs \fBgit push\fR after commit\. +. +.P +\-f +. +.P +Adds \fB\-f\fR option to \fBgit push\fR command\. +. +.P +\-h +. +.P +Prints synopsis\. +. +.SH "EXAMPLES" +This example stages all changes then commits with automatic commit message\. +. +.IP "" 4 +. +.nf + +$ git magic \-a +[feature/magic dc2a11e] A man/git\-magic\.md + 1 file changed, 37 insertions(+) + create mode 100644 man/git\-auto\.md +# git log +Author: overengineer <54alpersaid@gmail\.com> +Date: Thu Sep 30 20:14:22 2021 +0300 + + M man/git\-magic\.md +. +.fi +. +.IP "" 0 +. +.P +\fB\-m\fR option PREPENDS generated message\. +. +.IP "" 4 +. +.nf + +$ git magic \-am "Added documentation for git magic" +[feature/magic dc2a11e] Added documentation for git magic + 1 file changed, 42 insertions(+), 0 deletions(\-) + create mode 100644 A man/git\-auto\.md +$ git log +Author: overengineer <54alpersaid@gmail\.com> +Date: Thu Sep 30 20:14:22 2021 +0300 + + Added documentation for git magic + + M man/git\-magic\.md +. +.fi +. +.IP "" 0 +. +.SH "AUTHOR" +Written by Alper S\. Soylu \fI54alpersaid@gmail\.com\fR +. +.SH "REPORTING BUGS" +<\fIhttps://github\.com/tj/git\-extras/issues\fR> +. +.SH "SEE ALSO" +<\fIhttps://github\.com/tj/git\-extras\fR> diff --git a/man/git-magic.html b/man/git-magic.html new file mode 100644 index 0000000..f81b992 --- /dev/null +++ b/man/git-magic.html @@ -0,0 +1,167 @@ + + + + + + git-magic(1) - Automate add/commit/push routines + + + + +
+ + + +
    +
  1. git-magic(1)
  2. +
  3. Git Extras
  4. +
  5. git-magic(1)
  6. +
+ +

NAME

+

+ git-magic - Automate add/commit/push routines +

+ +

SYNOPSIS

+ +

git-magic [-a] [-m msg] [-e] [-p] [-f]

+ +

DESCRIPTION

+ +

Produces summary of changes for commit message from git status --porcelain output. +Commits staged changes with the generated commit message and +opens editor to modify generated commit message optionally. +Also staging and pushing can be automated optinally.

+ +

OPTIONS

+ +

-a

+ +

Adds everything including untracked files.

+ +

-m msg

+ +

Use the given msg as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs. +Passed to git commit command. The generated is appended to user-given messages.

+ +

-e

+ +

This option lets you further edit the generated message. +Passed to git commit command.

+ +

-p

+ +

Runs git push after commit.

+ +

-f

+ +

Adds -f option to git push command.

+ +

-h

+ +

Prints synopsis.

+ +

EXAMPLES

+ +

This example stages all changes then commits with automatic commit message.

+ +
$ git magic -a
+[feature/magic dc2a11e] A  man/git-magic.md
+ 1 file changed, 37 insertions(+)
+ create mode 100644 man/git-auto.md
+# git log
+Author: overengineer <54alpersaid@gmail.com>
+Date:   Thu Sep 30 20:14:22 2021 +0300
+
+    M  man/git-magic.md
+
+ +

-m option PREPENDS generated message.

+ +
$ git magic -am "Added documentation for git magic"
+[feature/magic dc2a11e] Added documentation for git magic
+ 1 file changed, 42 insertions(+), 0 deletions(-)
+ create mode 100644 A man/git-auto.md
+$ git log
+Author: overengineer <54alpersaid@gmail.com>
+Date:   Thu Sep 30 20:14:22 2021 +0300
+
+    Added documentation for git magic
+
+    M  man/git-magic.md
+
+ +

AUTHOR

+ +

Written by Alper S. Soylu 54alpersaid@gmail.com

+ +

REPORTING BUGS

+ +

<https://github.com/tj/git-extras/issues>

+ +

SEE ALSO

+ +

<https://github.com/tj/git-extras>

+ + +
    +
  1. +
  2. October 2021
  3. +
  4. git-magic(1)
  5. +
+ +
+ + diff --git a/man/git-magic.md b/man/git-magic.md new file mode 100644 index 0000000..e915381 --- /dev/null +++ b/man/git-magic.md @@ -0,0 +1,85 @@ +git-magic(1) -- Automate add/commit/push routines +================================ + +## SYNOPSIS + +`git-magic` [-a] [-m ] [-e] [-p] [-f] + +## DESCRIPTION + +Produces summary of changes for commit message from `git status --porcelain` output. +Commits staged changes with the generated commit message and +opens editor to modify generated commit message optionally. +Also staging and pushing can be automated optinally. + +## OPTIONS + +-a + +Adds everything including untracked files. + +-m + +Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs. +Passed to git commit command. The generated is appended to user-given messages. + +-e + +This option lets you further edit the generated message. +Passed to git commit command. + +-p + +Runs `git push` after commit. + +-f + +Adds `-f` option to `git push` command. + +-h + +Prints synopsis. + +## EXAMPLES + +This example stages all changes then commits with automatic commit message. + +``` +$ git magic -a +[feature/magic dc2a11e] A man/git-magic.md + 1 file changed, 37 insertions(+) + create mode 100644 man/git-auto.md +# git log +Author: overengineer <54alpersaid@gmail.com> +Date: Thu Sep 30 20:14:22 2021 +0300 + + M man/git-magic.md +``` + +`-m` option PREPENDS generated message. + +``` +$ git magic -am "Added documentation for git magic" +[feature/magic dc2a11e] Added documentation for git magic + 1 file changed, 42 insertions(+), 0 deletions(-) + create mode 100644 A man/git-auto.md +$ git log +Author: overengineer <54alpersaid@gmail.com> +Date: Thu Sep 30 20:14:22 2021 +0300 + + Added documentation for git magic + + M man/git-magic.md +``` + +## AUTHOR + +Written by Alper S. Soylu <54alpersaid@gmail.com> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<> diff --git a/man/index.txt b/man/index.txt index b76a90a..d57a9c1 100644 --- a/man/index.txt +++ b/man/index.txt @@ -36,6 +36,7 @@ git-info(1) git-info git-local-commits(1) git-local-commits git-lock(1) git-lock git-locked(1) git-locked +git-magic(1) git-magic git-merge-into(1) git-merge-into git-merge-repo(1) git-merge-repo git-missing(1) git-missing From 3f2ad3fa8f8d572f5d53f6dc60dd8cd19e77ec14 Mon Sep 17 00:00:00 2001 From: overengineer <54alpersaid@gmail.com> Date: Fri, 8 Oct 2021 07:36:29 +0300 Subject: [PATCH 2/2] Ignored error when git restore --staged fails when there is no commit --- bin/git-magic | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-magic b/bin/git-magic index b6b7b6f..f020c74 100755 --- a/bin/git-magic +++ b/bin/git-magic @@ -68,7 +68,7 @@ if [[ $ALL == true ]]; then # Restore staging area so that, for example, # add and modify will not be separate entries in status - git restore --staged . + git restore --staged . || true git add . fi