Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
Is there a C# commandline command one-liner which can help me get the GIT commit hash of an application?

This question is a follow-up of this other one.

In that question, one mentions the usage of [assembly: AssemblyVersion(...)] to the file AssemblyInfo.cs file, and in the meanwhile I've found out that it's forbidden to execute any processing before such a line, the only thing which is allowed is something like:

[assembly: AssemblyVersion("1.0.0.0" + "-" + Namespace.Class.Attribute)], or:
[assembly: AssemblyVersion("1.0.0.0" + "-" + Namespace.Class.Method())]

Original question:
So my question: is there a Namespace.Class.Attribute or Namespace.Class.Method() which contains the commit hash (or sha or shortened sha) of a C# application?

Edit after more investigation
In the meantime I've learnt that the command git describe --always gives me the information I'm looking for, so what I need is something like:

[assembly: AssemblyVersion("1.0.0.0-" + Launch("git describe --always")]

... but how can I execute that Launch()?

I already know that I can launch a commandline command using System.Diagnostics.Process(), like this example:

System.Diagnostics.Process.Start(foldervar + "application.exe", "inputfile.txt");

... but this way does not catch the result of that command.

New question:
So, does anybody know a C# one-liner for launching commandline commands and getting their result?

Thanks in advance

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Not a one liner, but this article https://galdin.dev/blog/show-git-commit-sha-in-net-applications/ shows a simple way to execute the command and access the result. The steps described in this article are as follows,

To do this we create a pre-build event that'll execute a git command and save it in a text file. We then add that generated text file as a string resource. The resource can will now be accessible from C#.

  1. Right click on the web application and click on properties.

  2. Go to the build events tab and type the following in the Pre-build event command line:

    git rev-parse HEAD --short > "$(ProjectDir)\CurrentCommit.txt"
    

    The command can obviously be replaced by anything you see fit.

  3. Save and build the project. (Ctrl+Shift+S, Ctrl+Shift+B)

  4. A new file called CurrentCommit.txt should be created in the root of your project.

  5. Go ahead and exclude it from Source Control

  6. In the projects properties page, go to the Resources tab and Add Existing File CurrentCommit.txt as a resource.

The contents of the generated file can now be accessed as:

Your.NameSpace.Properties.Resources.CurrentCommit;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda