Flutter: Get latest commit sha

291

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)

Share:
291
bshelbourne15
Author by

bshelbourne15

Updated on December 30, 2022

Comments

  • bshelbourne15
    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 and flutter pub get and get the same result

    EDIT 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
      croxx5f almost 3 years
      Are 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
      bshelbourne15 almost 3 years
      Bit 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
      croxx5f almost 3 years
      Hmm, maybe something like this package solves your needs ?
    • bshelbourne15
      bshelbourne15 almost 3 years
      Funnily 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
    bshelbourne15 almost 3 years
    I have added the folder to the assets of my pubspec.yaml and it still didn't work.
  • Kaan
    Kaan almost 3 years
    Can you share the assets part of your pubspec.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
    bshelbourne15 almost 3 years
    I 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?