Skip to content

Commit 42dcbb7

Browse files
peffgitster
authored andcommitted
version: add git_user_agent function
This is basically a fancy way of saying "git/$GIT_VERSION", except that it is overridable at build-time and through the environment. Which means that people who don't want to advertise their git version (for privacy or security reasons) can tweak it. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 816fb46 commit 42dcbb7

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ all::
296296
# the diff algorithm. It gives a nice speedup if your processor has
297297
# fast unaligned word loads. Does NOT work on big-endian systems!
298298
# Enabled by default on x86_64.
299+
#
300+
# Define GIT_USER_AGENT if you want to change how git identifies itself during
301+
# network interactions. The default is "git/$(GIT_VERSION)".
299302

300303
GIT-VERSION-FILE: FORCE
301304
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -905,6 +908,8 @@ BUILTIN_OBJS += builtin/write-tree.o
905908
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
906909
EXTLIBS =
907910

911+
GIT_USER_AGENT = git/$(GIT_VERSION)
912+
908913
#
909914
# Platform specific tweaks
910915
#
@@ -1916,6 +1921,11 @@ SHELL_PATH_CQ_SQ = $(subst ','\'',$(SHELL_PATH_CQ))
19161921
BASIC_CFLAGS += -DSHELL_PATH='$(SHELL_PATH_CQ_SQ)'
19171922
endif
19181923

1924+
GIT_USER_AGENT_SQ = $(subst ','\'',$(GIT_USER_AGENT))
1925+
GIT_USER_AGENT_CQ = "$(subst ",\",$(subst \,\\,$(GIT_USER_AGENT)))"
1926+
GIT_USER_AGENT_CQ_SQ = $(subst ','\'',$(GIT_USER_AGENT_CQ))
1927+
BASIC_CFLAGS += -DGIT_USER_AGENT='$(GIT_USER_AGENT_CQ_SQ)'
1928+
19191929
ALL_CFLAGS += $(BASIC_CFLAGS)
19201930
ALL_LDFLAGS += $(BASIC_LDFLAGS)
19211931

@@ -2000,6 +2010,7 @@ sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
20002010
-e 's|@SHELL_PATH@|$(SHELL_PATH_SQ)|' \
20012011
-e 's|@@DIFF@@|$(DIFF_SQ)|' \
20022012
-e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
2013+
-e 's|@@GIT_USER_AGENT@@|$(GIT_USER_AGENT_SQ)|g' \
20032014
-e 's|@@LOCALEDIR@@|$(localedir_SQ)|g' \
20042015
-e 's/@@NO_CURL@@/$(NO_CURL)/g' \
20052016
-e 's/@@USE_GETTEXT_SCHEME@@/$(USE_GETTEXT_SCHEME)/g' \

version.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@
22
#include "version.h"
33

44
const char git_version_string[] = GIT_VERSION;
5+
6+
const char *git_user_agent(void)
7+
{
8+
static const char *agent = NULL;
9+
10+
if (!agent) {
11+
agent = getenv("GIT_USER_AGENT");
12+
if (!agent)
13+
agent = GIT_USER_AGENT;
14+
}
15+
16+
return agent;
17+
}

version.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33

44
extern const char git_version_string[];
55

6+
const char *git_user_agent(void);
7+
68
#endif /* VERSION_H */

0 commit comments

Comments
 (0)