Dmytro
Dmytro

Reputation: 71

GIT: Add author, date create/modified to a file

I need to create a universal script that will add this information to each file when it is saved, as in such text editors like Atom orWebStorm.

Information for add:

  1. File creation date;
  2. Date of last file update;
  3. Project branch;
  4. Author.

Example of this table enter image description here

Here is an example of the code I created with the shell.

#! /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

In the commands for adding a table, there is a drawback, after the first addition to the file, this table no longer works on files that were created later or the table in them was deleted.

And the second problem is the dynamic update of information in files.

# 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

Upvotes: 0

Views: 3465

Answers (1)

zigarn
zigarn

Reputation: 11625

Do you really need to have those meta-data available in the file?

Having meta-data in file is usually an old habit from old centralized SCMs.
But in git, as you need to clone the whole repo locally, you have direct access to all the meta-data.

So you can simply access them when you need them by doing:

# Data of first commit of file
git log -1 --diff-filter=A --format='%an | %aI | %h' -- path/to/file

# Data of last commit of file
git log -1 --format='%an | %aI | %h' -- path/to/file

If you really want to get them in your file, the recommended way is to use keyword-expansion (just like CVS or SVN used to do).

But, it won't solve the necessity to add the placeholders in each files.
If you want to enforce the header creation with placeholders, you will need to use another clean filter to modify files on-the-fly when staging them (see Can you change a file content during git commit?).

But, you will have to deploy and force use of this filter on all git client interacting with your repository.
You can reject publishing commits modifying a file without header by using server-side hooks.

Upvotes: 2

Related Questions