Show git last commit hash and current branch/tag in flutter app

Viewed 1697

How can I access the last git commit hash, current branch and last tag from the source directory, when building a flutter app? I want to display this in a "About version" dialog.

There is https://pub.dev/packages/package_info but it has no information about git.

Are there any other packages that may provide this information?

3 Answers

It may not be the best solution, but here's how I did it.

1. Add the necessary files to assets

flutter:
  assets:
    - .git/HEAD         # This file points out the current branch of the project.
    - .git/ORIG_HEAD    # This file points to the commit id at origin (last commit id of the remote repository).
    - .git/refs/heads/  # This directory includes files for each branch which points to the last commit id (local repo).

2. Consume it from your .dart code

Future<String> getGitInfo() async {
  final _head = await rootBundle.loadString('.git/HEAD');
  final commitId = await rootBundle.loadString('.git/ORIG_HEAD');

  final branch = _head.split('/').last;

  print("Branch: $branch");
  print("Commit ID: $commitId");

  return "Branch: $branch,  Commit ID: $commitId";
}

3. Display it in your flutter app if needed

FutureBuilder<String>(
  future: getGitInfo(),
  builder: (context, snapshot) {
    return Text(snapshot.data ?? "");
  },
)

Expected output:

enter image description here

Do note that, hot reload works also for assets, and hence it should show changes.

Rithvik Nishad's answer does not work for me, because my flutter project is a child project and cannot access .git/ directory. For example:

root/
|__.git/
|__client/
|_____flutter project/  <== can not access to .git/ from here

So my solution is using a small script to get git info and save into the .env file

#!/bin/bash
set -e

branch=`git rev-parse --abbrev-ref HEAD`
escaped_branch=$(printf '%s\n' "$branch" | sed -e 's/[\/&]/\\&/g')
commit=`git rev-parse --short HEAD`

# branch
if grep -q 'GIT_BRANCH=' .env; then
    sed -i '' "s/^GIT_BRANCH.*/GIT_BRANCH=$escaped_branch/g" .env
else
    echo "GIT_BRANCH=$branch" >> .env
fi

# commit hash
if grep -q 'GIT_COMMIT=' .env; then
    sed -i '' "s/^GIT_COMMIT.*/GIT_COMMIT=$commit/g" .env
else
    echo "GIT_COMMIT=$commit" >> .env
fi

then read the .env file to get git branch/hash, ... (i'm using dotenv to read the file)

final gitBranch = dotenv.env['GIT_BRANCH'] ?? ''; // => features/change-logo
final gitCommit = dotenv.env['GIT_COMMIT'] ?? ''; // => 45a97d3

Here is my result:

Git info

My method that doesn't need other packages, env vars, or adding and reading assets. And supports flutter project being in any child directory. Just directly write a .dart file with the post-commit git hook:

# post-commit
FILE="app/lib/such/a/deep/tree/settings/git.dart"
echo "// AUTO GENERATED FILE BY GIT HOOK post-commit" > $FILE
echo "const String GIT_COMMIT = '$(git rev-parse HEAD)';" >> $FILE
echo "const String GIT_BRANCH = '$(git rev-parse --abbrev-ref HEAD)';" >> $FILE

Then:

import 'package:fluffy-squirrel/settings/git.dart';

print(GIT_BRANCH);

This is how I ensure my Sentry instance always has the right release.

Related