Skip to content

Commit 7519443

Browse files
peffgitster
authored andcommitted
push: make non-fast-forward help message configurable
This message is designed to help new users understand what has happened when refs fail to push. However, it does not help experienced users at all, and significantly clutters the output, frequently dwarfing the regular status table and making it harder to see. This patch introduces a general configuration mechanism for optional messages, with this push message as the first example. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6ea71fe commit 7519443

File tree

7 files changed

+51
-1
lines changed

7 files changed

+51
-1
lines changed

Documentation/config.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ For command-specific variables, you will find a more detailed description
113113
in the appropriate manual page. You will find a description of non-core
114114
porcelain configuration variables in the respective porcelain documentation.
115115

116+
advice.*::
117+
When set to 'true', display the given optional help message.
118+
When set to 'false', do not display. The configuration variables
119+
are:
120+
+
121+
--
122+
pushNonFastForward::
123+
Advice shown when linkgit:git-push[1] refuses
124+
non-fast-forward refs. Default: true.
125+
--
126+
116127
core.fileMode::
117128
If false, the executable bit differences between the index and
118129
the working copy are ignored; useful on broken filesystems like FAT.

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ export PERL_PATH
397397
LIB_FILE=libgit.a
398398
XDIFF_LIB=xdiff/lib.a
399399

400+
LIB_H += advice.h
400401
LIB_H += archive.h
401402
LIB_H += attr.h
402403
LIB_H += blob.h
@@ -454,6 +455,7 @@ LIB_H += utf8.h
454455
LIB_H += wt-status.h
455456

456457
LIB_OBJS += abspath.o
458+
LIB_OBJS += advice.o
457459
LIB_OBJS += alias.o
458460
LIB_OBJS += alloc.o
459461
LIB_OBJS += archive.o

advice.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "cache.h"
2+
3+
int advice_push_nonfastforward = 1;
4+
5+
static struct {
6+
const char *name;
7+
int *preference;
8+
} advice_config[] = {
9+
{ "pushnonfastforward", &advice_push_nonfastforward },
10+
};
11+
12+
int git_default_advice_config(const char *var, const char *value)
13+
{
14+
const char *k = skip_prefix(var, "advice.");
15+
int i;
16+
17+
for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
18+
if (strcmp(k, advice_config[i].name))
19+
continue;
20+
*advice_config[i].preference = git_config_bool(var, value);
21+
return 0;
22+
}
23+
24+
return 0;
25+
}

advice.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef ADVICE_H
2+
#define ADVICE_H
3+
4+
extern int advice_push_nonfastforward;
5+
6+
int git_default_advice_config(const char *var, const char *value);
7+
8+
#endif /* ADVICE_H */

builtin-push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static int do_push(const char *repo, int flags)
157157
continue;
158158

159159
error("failed to push some refs to '%s'", url[i]);
160-
if (nonfastforward) {
160+
if (nonfastforward && advice_push_nonfastforward) {
161161
printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
162162
"Merge the remote changes before pushing again. See the 'non-fast forward'\n"
163163
"section of 'git push --help' for details.\n");

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "git-compat-util.h"
55
#include "strbuf.h"
66
#include "hash.h"
7+
#include "advice.h"
78

89
#include SHA1_HEADER
910
#ifndef git_SHA_CTX

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ int git_default_config(const char *var, const char *value, void *dummy)
627627
if (!prefixcmp(var, "mailmap."))
628628
return git_default_mailmap_config(var, value);
629629

630+
if (!prefixcmp(var, "advice."))
631+
return git_default_advice_config(var, value);
632+
630633
if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
631634
pager_use_color = git_config_bool(var,value);
632635
return 0;

0 commit comments

Comments
 (0)