Flutter: Get latest commit sha
The .git
folder is not bundled by default. If you want to add, you need to add .git
folder under the assets
of your pubspec.yaml
as you do with an image file. Also, if you're trying this on a Flutter Desktop app; you can use the run method from dart:io
. It let you run shell commands (depending on your OS you need to trick a little bit in configuration)
bshelbourne15
Updated on December 30, 2022Comments
-
bshelbourne15 over 1 year
Is there a way in Flutter to get the latest commit sha? This would be useful to displaying a version number in app.
I have tried loading it from
.git/ORIG_HEAD
like so:final commitId = await rootBundle.loadString('.git/ORIG_HEAD');
but receive an error
Could not open '.git/ORIG_HEAD'
I have also investigated whether it's possible to run a command line query like
git rev-parse HEAD
but this doesn't seem possible in Flutter.Is there a package or another way of doing this?
For reference - I have done a
flutter clean
andflutter pub get
and get the same resultEDIT 1:
Here is my
assets
when trying to import the.git/
directory:flutter: assets: - .git/ORIG_HEAD
I have also tried with just
.git/
and get the same result.-
croxx5f almost 3 yearsAre you trying to run this in a flutter app or just investigating. Because I think the .git folder does not get bundled in the app at all.
-
bshelbourne15 almost 3 yearsBit of both. I would like to have it and don't really want to query my vcs to get it as it does exist somewhere locally. I guessed that the folder would not be bundled and that's why it wasn't working and hence the question. This doesn't seem like something that should be difficult, right?
-
croxx5f almost 3 yearsHmm, maybe something like this package solves your needs ?
-
bshelbourne15 almost 3 yearsFunnily enough, I already am using that package to get my current version number, just thought a commit sha might make it more useful for development with multiple people working on it to understand the latest build. Thanks!
-
-
bshelbourne15 almost 3 yearsI have added the folder to the
assets
of mypubspec.yaml
and it still didn't work. -
Kaan almost 3 yearsCan you share the
assets
part of yourpubspec.yaml
? I think maybe/
is missing at the end of the foldername or you can add just the file you want as.git/ORIG_HEAD
-
bshelbourne15 almost 3 yearsI have shared a snippet of my
assets
. I have tried with importing the entire directory with.git/
and with the specific file.git/ORIG_HEAD
and neither work. Have you run this locally and saw it working?