Git Product home page Git Product logo

Comments (8)

ansemjo avatar ansemjo commented on August 15, 2024 1

Without having looked at the code yet, I may add some input on this:

I encountered the problem of including a git version in scripts or binaries a few times now and wrote a blog entry about using .gitattributes and $Format:%h$ strings.

In short, you create a special variable or constant which is assigned the value of $Format:%h$ in the source code and add the respective file to your .gitattributes. During archive creation with git archive ... or e.g. downloading from GitHub this will then be replaced with the commit hash. When using this variable you check for the substring Format: and if it does not exist you know you have a commit hash in your variable.

During build you should then either always build from git archives or add a step in the makefile which checks if the .git dir is present and try to export the commit hash with git rev-parse ..., then replace the constant during compilation. I know Go can do that with a compiler flag, I am not sure about C but there ought to be some way to override #DEFINE's or something?

Any other process I could think of either requires that the user has git installed and builds from git clones, uses client-side hooks which git does not allow to be checked in and out for good reason (think arbitrary code-execution) or requires a VERSION file to be manually updated every time.

from slirp4netns.

AkihiroSuda avatar AkihiroSuda commented on August 15, 2024

Thanks for the info!

@giuseppe Any thought?

from slirp4netns.

giuseppe avatar giuseppe commented on August 15, 2024

would something like:

$ git diff
diff --git a/Makefile.am b/Makefile.am
index 318f448..f180ea4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -66,6 +66,7 @@ libqemu_slirp_a_SOURCES = \
 
 libslirp_a_SOURCES = rd235_libslirp/src/qemu2libslirp.c
 
+slirp4netns_CFLAGS = $(AM_CFLAGS) -DVERSION=\""$(shell git rev-parse HEAD)"\"
 slirp4netns_SOURCES = main.c slirp4netns.c
 slirp4netns_LDADD = libslirp.a libqemu_slirp.a
 man1_MANS = slirp4netns.1
diff --git a/main.c b/main.c
index 5ec4693..224a3b1 100644
--- a/main.c
+++ b/main.c
@@ -242,6 +242,8 @@ static void usage(const char *argv0)
        printf("-r, --ready-fd=FD    specify the FD to write to when the network is configured\n");
        printf("-m, --mtu=MTU        specify MTU (default=1500, max=65521)\n");
        printf("-6, --enable-ipv6    enable IPv6 (experimental)\n");
+       printf("\n");
+       printf("Version: %s\n", VERSION);
 }
 
 struct options {

be fine?

I will polish and open a PR if it is fine for you

from slirp4netns.

AkihiroSuda avatar AkihiroSuda commented on August 15, 2024

Thanks, SGTM

from slirp4netns.

ansemjo avatar ansemjo commented on August 15, 2024

As I said above, I think that will break if you build from a downloaded tarball where git rev-parse cannot find any commit information.

from slirp4netns.

giuseppe avatar giuseppe commented on August 15, 2024

we could create a .tarball-distribution file if creating the tarball with "make dist" but I am not sure creating a tarball from github can simulate that. How would we know the commit?

from slirp4netns.

giuseppe avatar giuseppe commented on August 15, 2024

I've opened a PR here: #34

If git fails, then the version specified in the configure.ac file is used

from slirp4netns.

ansemjo avatar ansemjo commented on August 15, 2024

It has been a while since I touched C code but to iterate on the above I would do something like this:

diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..cba1251
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+main.c export-subst
\ No newline at end of file
diff --git a/Makefile.am b/Makefile.am
index 318f448..6b1879c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -66,6 +66,9 @@ libqemu_slirp_a_SOURCES = \
 
 libslirp_a_SOURCES = rd235_libslirp/src/qemu2libslirp.c
 
+VERSION := $(shell [ -d .git ] && echo "-DVERSION=\"$$(git describe --always --abbrev)\"")
+
+slirp4netns_CFLAGS = $(AM_CFLAGS) $(VERSION)
 slirp4netns_SOURCES = main.c slirp4netns.c
 slirp4netns_LDADD = libslirp.a libqemu_slirp.a
 man1_MANS = slirp4netns.1
diff --git a/main.c b/main.c
index 5ec4693..38ce8d3 100644
--- a/main.c
+++ b/main.c
@@ -233,6 +233,10 @@ static int parent(int sock, int exit_fd, unsigned int mtu, bool enable_ipv6)
 	return 0;
 }
 
+#ifndef VERSION
+#define VERSION "$Format:%h$"
+#endif
+
 static void usage(const char *argv0)
 {
 	printf("Usage: %s [OPTION]... PID TAPNAME\n", argv0);
@@ -242,6 +246,8 @@ static void usage(const char *argv0)
 	printf("-r, --ready-fd=FD    specify the FD to write to when the network is configured\n");
 	printf("-m, --mtu=MTU        specify MTU (default=1500, max=65521)\n");
 	printf("-6, --enable-ipv6    enable IPv6 (experimental)\n");
+	printf("\n");
+	printf("Version: %s\n", VERSION);
 }
 
 struct options {

That fails to compile though with <command-line>: error: ‘e09a981’ undeclared (first use in this function) .. probably screwed up quoting somewhere.

Edit: fixed that, see 740b346

from slirp4netns.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.