Home > Enterprise >  Get GIT trailer value and save as variable?
Get GIT trailer value and save as variable?

Time:10-12

I'm having some trouble figuring out GIT trailers. I'm trying to find a way to get the latest commit message, parse the trailers and and save the values from the trailers to different variables for a CI script.

I.E. the commit message would be:

Updated level 2 geometry

Level: Assets\LevelTwo.fbx

Platform: Android

And the CI system would run something like:

level = git interpret-trailers --parse(Level(value))
platform = git interpret-trailers --parse(Platform(value))
ReImport $level $platform

I'm not finding the interpret-trailers documentation very helpful in this regard as all the examples are about setting up rules to update or add trailers, and less how to get useful information out of them.

CodePudding user response:

Check if pretty format options, acquired in Git 2.22 (Q2 2019), could help in your case.

Example:

git show -s --pretty='%s%n%(trailers:key=Signed-off-by,valueonly)' aaaa881

CodePudding user response:

For a random commit message,

1st block
blank
2nd block
blank
last block

The 1st block is its subject, which could have one or more non-blank lines. The blank block could also have one or more blank lines. The content below the 1st blank is the body of the message. The last block whose lines are in the format key:value is the trailers.

In your example,

Updated level 2 geometry

Level: Assets\LevelTwo.fbx

Platform: Android

Updated level 2 geometry is the subject, and the rest is the body. Platform: Android belongs to the trailers, while Level: Assets\LevelTwo.fbx does not because it's not included in the last non-blank block.

In the command git interpret-trailers --parse <file>, the file could be a file existing on the disk or the standard output of the previous command. The file content is supposed to be a commit message or a patch, like the output of git log -1 --pretty='%B' or git format-patch -1 --stdout.

For example we can retrieve the trailers of a commit message by

git log -1 $commit --pretty='%B' | git interpret-trailers --parse

# or
git format-patch -1 $commit --stdout | git interpret-trailers --parse

We can also dump the output to a file and then parse the file.

git log -1 $commit --pretty='%B' > message.txt
git interpret-trailers --parse message.txt

# or
git format-patch -1 $commit
git interpret-trailers --parse 0001-*.patch

I can't tell what language your CI system uses. The statements seem a bit different from git commands.

If a commit object and its sha1 value are available, we could also use placeholders %(trailers) to retrieve the trailers.

git log -1 $commit --pretty='%(trailers:key=Platform,valueonly)'

It returns the value of the key Platform, which is Android. %(trailers) prints all keys and values. key=Platform returns Platform's key and value only. valueonly or valueonly=true returns Platform's value only.

Note that Level: Assets\LevelTwo.fbx does not belong to the trailers. For all I know, git interpret-trailers or the placeholder %(trailers) can't directly retrieve it from the commit message. Maybe there are some options I don't know yet.

  • Related