0

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
Dmytro
  • 61
  • 2
  • 13
  • What is your use case to need this information available directly in each file? – zigarn Jul 06 '17 at 13:31
  • @zigarn This information will be used for tracking. Who made changes to the file and when? – Dmytro Jul 06 '17 at 14:04
  • Can't you just use the right git commands when those information are needed? – zigarn Jul 06 '17 at 14:07
  • Which git commands do you mean? – Dmytro Jul 06 '17 at 14:12
  • Commands to get date and author of first and last commit of the file. Branch is not a sustainable info, so short commit SHA-1 would be better. – zigarn Jul 06 '17 at 14:49
  • I did not find such commands (SHA-1). And I used just such commands. Date the file was created locally, the date of the last commit and local name author. – Dmytro Jul 06 '17 at 15:08
  • First commit info: `git log -1 --diff-filter=A --format='%an | %aI | %h' -- path/to/file`, last commit info: `git log -1 --format='%an | %aI | %h' -- path/to/file`. SHA-1 is the commit identifier in git, it's not a command – zigarn Jul 06 '17 at 15:24
  • Thank you! I can not add this table to new files. What do you think, what is the problem? – Dmytro Jul 06 '17 at 15:50

1 Answers1

2

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.

William Robertson
  • 15,273
  • 4
  • 38
  • 44
zigarn
  • 10,892
  • 2
  • 31
  • 45
  • Yes, I need these metadata in different project files. I have a problem with the script to create this data. I need to determine the way this script works. – Dmytro Jul 08 '17 at 10:27
  • *Do you really need to have those meta-data available in the file?* We certainly do for database code. Once a PL/SQL package (for example) is compiled into the database, there’s no `git log —1` etc to tell you what release branch it came from and when. That information absolutely has to be included within the source code. – William Robertson Nov 20 '19 at 10:41