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 5 monthsIs 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_HEADlike 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 HEADbut this doesn't seem possible in Flutter.Is there a package or another way of doing this?
For reference - I have done a
flutter cleanandflutter pub getand get the same resultEDIT 1:
Here is my
assetswhen trying to import the.git/directory:flutter: assets: - .git/ORIG_HEADI have also tried with just
.git/and get the same result.-
croxx5f almost 2 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 2 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 2 yearsHmm, maybe something like this package solves your needs ? -
bshelbourne15 almost 2 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 2 yearsI have added the folder to theassetsof mypubspec.yamland it still didn't work. -
Kaan almost 2 yearsCan you share theassetspart 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 2 yearsI have shared a snippet of myassets. I have tried with importing the entire directory with.git/and with the specific file.git/ORIG_HEADand neither work. Have you run this locally and saw it working?