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 that points to the last commit id (local repo).
Default Gradle configuration excludes .git
folder from builds, therefore this rule must be removed.
To do this, add the following to your settings.graddle
:
import org.apache.tools.ant.DirectoryScanner
DirectoryScanner.removeDefaultExclude('**/.git')
DirectoryScanner.removeDefaultExclude('**/.git/**')
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:
Note that hot reload works also for assets, so it should show changes.