我需要創建一個通用腳本,它將在保存時將這些信息添加到每個文件中,如Atom或WebStorm等文本編輯器。GIT:添加作者,創建/修改日期到一個文件
爲附加信息:
實施例是我與外殼創建的代碼的一個例子。
#! /bin/bash
# Abort if any of the commands fail
set -e
# Trace what gets executed. Useful for debugging.
set -x
# If set, the return value of a pipeline is the value of the last (rightmost) command to
#exit with a non-zero status, or zero if all commands in the pipeline exit successfully.
set -o pipefail
# Treat unset variables as an error when performing parameter expansion.
# If expansion is attempted on an unset variable, the shell prints an error message,
# and, if not interactive, exits with a non-zero status.
set -u
# Created:
DATE=$(date +%Y-%m-%d:%H:%M:%S);
BRANCH=$(git symbolic-ref --short -q HEAD);
GIT_USER=$(git config user.name);
CREATED=$(echo 'created');
# Updated:
DATE_U=$(git log -1 --format=%cd --date=format:%Y-%m-%d:%H:%M:%S);
BRANCH_U=$(git symbolic-ref --short -q HEAD);
GIT_USER_U=$(git config user.name);
UPDATED=$(echo 'updated');
# Find file (js) and add table to file.
echo "Create table in js-file..."
if test -a $(grep created: ./development/*.js); then
sudo find ./development -name "*.js" -type f -exec sed -i 1i\ "/*flow*/\n/*---------------------------------------------------------\n $CREATED: $DATE | $BRANCH | $GIT_USER \n $UPDATED: $DATE_U | $BRANCH_U | $GIT_USER_U \n---------------------------------------------------------*/" {} \;
echo "Create table..."
else
echo "Table already is exist..."
fi
# Find file (scss) and add to file.
echo "Create table in scss-file..."
if test -a $(grep created: ./development/*.scss); then
sudo find ./development -name "*.scss" -type f -exec sed -i 1i\ "/*---------------------------------------------------------\n $CREATED: $DATE | $BRANCH | $GIT_USER \n $UPDATED: $DATE_U | $BRANCH_U | $GIT_USER_U \n---------------------------------------------------------*/" {} \;
echo "Create table..."
else
echo "Table already is exist..."
fi
在添加一個表的命令,有一個缺點,首先除了文件後,此表不再適用於後來被創建或其中的表已被刪除的文件。
而第二個問題是動態更新文件中的信息。
# Updated
echo "Update date area..."
if test ! -a $DATE_U;
then
echo "Date is exist..."
else
echo "Update date..."
fi
echo "Update branch area..."
if test ! -a $BRANCH_U;
then
echo "Branch is exist..."
else
echo "Update branch..."
fi
echo "Update user area..."
if test ! -a $GIT_USER;
then
echo "User is exist..."
else
echo "Update user..."
fi
您的使用案例需要在每個文件中直接提供這些信息嗎? – zigarn
@zigarn此信息將用於跟蹤。誰對文件進行了更改以及何時? – Dmytro
當需要這些信息時,你不能只使用正確的git命令嗎? – zigarn