wt-status: move many global settings to wt_status structure
Turn four global variables (wt_status_use_color, show_tracked_files, wt_status_relative_paths, and wt_status_submodule_summary) into fields of wt_status structure. They can also lose "wt_status_" prefix. Get rid of "untracked" field that was used only to keep track of otherwise available information redundantly. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Aug 9, 2009 at 21:59 UTC
d249b098893809c6de6e5e20063a22ac2c629ce7
3 files changed
+93
-84
builtin-commit.c
+48
-39
@@ -343,27 +343,24 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
343
return false_lock.filename;
344
}
345
346
-static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
346
+static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
347
+ struct wt_status *s)
348
{
348
- struct wt_status s;
349
-
350
- wt_status_prepare(&s);
351
- if (wt_status_relative_paths)
352
- s.prefix = prefix;
349
+ if (s->relative_paths)
350
+ s->prefix = prefix;
351
352
if (amend) {
355
- s.amend = 1;
356
- s.reference = "HEAD^1";
353
+ s->amend = 1;
354
+ s->reference = "HEAD^1";
355
}
358
- s.verbose = verbose;
359
- s.untracked = (show_untracked_files == SHOW_ALL_UNTRACKED_FILES);
360
- s.index_file = index_file;
361
- s.fp = fp;
362
- s.nowarn = nowarn;
356
+ s->verbose = verbose;
357
+ s->index_file = index_file;
358
+ s->fp = fp;
359
+ s->nowarn = nowarn;
360
364
- wt_status_print(&s);
361
+ wt_status_print(s);
362
366
- return s.commitable;
363
+ return s->commitable;
364
}
365
366
static int is_a_merge(const unsigned char *sha1)
@@ -417,7 +414,8 @@ static void determine_author_info(void)
414
author_date = date;
415
}
416
420
-static int prepare_to_commit(const char *index_file, const char *prefix)
417
+static int prepare_to_commit(const char *index_file, const char *prefix,
418
+ struct wt_status *s)
419
{
420
struct stat statbuf;
421
int commitable, saved_color_setting;
@@ -559,10 +557,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
557
if (ident_shown)
558
fprintf(fp, "#\n");
559
562
- saved_color_setting = wt_status_use_color;
563
- wt_status_use_color = 0;
564
- commitable = run_status(fp, index_file, prefix, 1);
565
- wt_status_use_color = saved_color_setting;
560
+ saved_color_setting = s->use_color;
561
+ s->use_color = 0;
562
+ commitable = run_status(fp, index_file, prefix, 1, s);
563
+ s->use_color = saved_color_setting;
564
} else {
565
unsigned char sha1[20];
566
const char *parent = "HEAD";
@@ -583,7 +581,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
581
582
if (!commitable && !in_merge && !allow_empty &&
583
!(amend && is_a_merge(head_sha1))) {
586
- run_status(stdout, index_file, prefix, 0);
584
+ run_status(stdout, index_file, prefix, 0, s);
585
return 0;
586
}
587
@@ -695,7 +693,8 @@ static const char *find_author_by_nickname(const char *name)
693
694
static int parse_and_validate_options(int argc, const char *argv[],
695
const char * const usage[],
698
- const char *prefix)
696
+ const char *prefix,
697
+ struct wt_status *s)
698
{
699
int f = 0;
700
@@ -798,11 +797,11 @@ static int parse_and_validate_options(int argc, const char *argv[],
797
if (!untracked_files_arg)
798
; /* default already initialized */
799
else if (!strcmp(untracked_files_arg, "no"))
801
- show_untracked_files = SHOW_NO_UNTRACKED_FILES;
800
+ s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
801
else if (!strcmp(untracked_files_arg, "normal"))
803
- show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
802
+ s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
803
else if (!strcmp(untracked_files_arg, "all"))
805
- show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
804
+ s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
805
else
806
die("Invalid untracked files mode '%s'", untracked_files_arg);
807
@@ -814,13 +813,14 @@ static int parse_and_validate_options(int argc, const char *argv[],
813
return argc;
814
}
815
817
-static int dry_run_commit(int argc, const char **argv, const char *prefix)
816
+static int dry_run_commit(int argc, const char **argv, const char *prefix,
817
+ struct wt_status *s)
818
{
819
int commitable;
820
const char *index_file;
821
822
index_file = prepare_index(argc, argv, prefix, 1);
823
- commitable = run_status(stdout, index_file, prefix, 0);
823
+ commitable = run_status(stdout, index_file, prefix, 0, s);
824
rollback_index_files();
825
826
return commitable ? 0 : 1;
@@ -828,14 +828,18 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix)
828
829
int cmd_status(int argc, const char **argv, const char *prefix)
830
{
831
- git_config(git_status_config, NULL);
832
- if (wt_status_use_color == -1)
833
- wt_status_use_color = git_use_color_default;
831
+ struct wt_status s;
832
+
833
+ wt_status_prepare(&s);
834
+ git_config(git_status_config, &s);
835
+ if (s.use_color == -1)
836
+ s.use_color = git_use_color_default;
837
if (diff_use_color_default == -1)
838
diff_use_color_default = git_use_color_default;
839
837
- argc = parse_and_validate_options(argc, argv, builtin_status_usage, prefix);
838
- return dry_run_commit(argc, argv, prefix);
840
+ argc = parse_and_validate_options(argc, argv, builtin_status_usage,
841
+ prefix, &s);
842
+ return dry_run_commit(argc, argv, prefix, &s);
843
}
844
845
static void print_summary(const char *prefix, const unsigned char *sha1)
@@ -887,10 +891,12 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
891
892
static int git_commit_config(const char *k, const char *v, void *cb)
893
{
894
+ struct wt_status *s = cb;
895
+
896
if (!strcmp(k, "commit.template"))
897
return git_config_string(&template_file, k, v);
898
893
- return git_status_config(k, v, cb);
899
+ return git_status_config(k, v, s);
900
}
901
902
int cmd_commit(int argc, const char **argv, const char *prefix)
@@ -903,21 +909,24 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
909
struct commit_list *parents = NULL, **pptr = &parents;
910
struct stat statbuf;
911
int allow_fast_forward = 1;
912
+ struct wt_status s;
913
907
- git_config(git_commit_config, NULL);
914
+ wt_status_prepare(&s);
915
+ git_config(git_commit_config, &s);
916
909
- if (wt_status_use_color == -1)
910
- wt_status_use_color = git_use_color_default;
917
+ if (s.use_color == -1)
918
+ s.use_color = git_use_color_default;
919
912
- argc = parse_and_validate_options(argc, argv, builtin_commit_usage, prefix);
920
+ argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
921
+ prefix, &s);
922
if (dry_run)
914
- return dry_run_commit(argc, argv, prefix);
923
+ return dry_run_commit(argc, argv, prefix, &s);
924
925
index_file = prepare_index(argc, argv, prefix, 0);
926
927
/* Set up everything for writing the commit object. This includes
928
running hooks, writing the trees, and interacting with the user. */
920
- if (!prepare_to_commit(index_file, prefix)) {
929
+ if (!prepare_to_commit(index_file, prefix, &s)) {
930
rollback_index_files();
931
return 1;
932
}
wt-status.c
+40
-41
@@ -11,9 +11,6 @@
11
#include "run-command.h"
12
#include "remote.h"
13
14
-int wt_status_relative_paths = 1;
15
-int wt_status_use_color = -1;
16
-static int wt_status_submodule_summary;
14
static char wt_status_colors[][COLOR_MAXLEN] = {
15
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
16
GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
@@ -23,8 +20,6 @@ static char wt_status_colors[][COLOR_MAXLEN] = {
20
GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
21
};
22
26
-enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
27
-
23
static int parse_status_slot(const char *var, int offset)
24
{
25
if (!strcasecmp(var+offset, "header"))
@@ -43,9 +38,9 @@ static int parse_status_slot(const char *var, int offset)
38
die("bad config variable '%s'", var);
39
}
40
46
-static const char *color(int slot)
41
+static const char *color(int slot, struct wt_status *s)
42
{
48
- return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
43
+ return s->use_color > 0 ? wt_status_colors[slot] : "";
44
}
45
46
void wt_status_prepare(struct wt_status *s)
@@ -54,6 +49,9 @@ void wt_status_prepare(struct wt_status *s)
49
const char *head;
50
51
memset(s, 0, sizeof(*s));
52
+ s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
53
+ s->use_color = -1;
54
+ s->relative_paths = 1;
55
head = resolve_ref("HEAD", sha1, 0, NULL);
56
s->branch = head ? xstrdup(head) : NULL;
57
s->reference = "HEAD";
@@ -64,7 +62,7 @@ void wt_status_prepare(struct wt_status *s)
62
63
static void wt_status_print_unmerged_header(struct wt_status *s)
64
{
67
- const char *c = color(WT_STATUS_HEADER);
65
+ const char *c = color(WT_STATUS_HEADER, s);
66
color_fprintf_ln(s->fp, c, "# Unmerged paths:");
67
if (!s->is_initial)
68
color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
@@ -76,7 +74,7 @@ static void wt_status_print_unmerged_header(struct wt_status *s)
74
75
static void wt_status_print_cached_header(struct wt_status *s)
76
{
79
- const char *c = color(WT_STATUS_HEADER);
77
+ const char *c = color(WT_STATUS_HEADER, s);
78
color_fprintf_ln(s->fp, c, "# Changes to be committed:");
79
if (!s->is_initial) {
80
color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
@@ -89,7 +87,7 @@ static void wt_status_print_cached_header(struct wt_status *s)
87
static void wt_status_print_dirty_header(struct wt_status *s,
88
int has_deleted)
89
{
92
- const char *c = color(WT_STATUS_HEADER);
90
+ const char *c = color(WT_STATUS_HEADER, s);
91
color_fprintf_ln(s->fp, c, "# Changed but not updated:");
92
if (!has_deleted)
93
color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
@@ -101,7 +99,7 @@ static void wt_status_print_dirty_header(struct wt_status *s,
99
100
static void wt_status_print_untracked_header(struct wt_status *s)
101
{
104
- const char *c = color(WT_STATUS_HEADER);
102
+ const char *c = color(WT_STATUS_HEADER, s);
103
color_fprintf_ln(s->fp, c, "# Untracked files:");
104
color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
105
color_fprintf_ln(s->fp, c, "#");
@@ -109,7 +107,7 @@ static void wt_status_print_untracked_header(struct wt_status *s)
107
108
static void wt_status_print_trailer(struct wt_status *s)
109
{
112
- color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
110
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
111
}
112
113
#define quote_path quote_path_relative
@@ -117,13 +115,13 @@ static void wt_status_print_trailer(struct wt_status *s)
115
static void wt_status_print_unmerged_data(struct wt_status *s,
116
struct string_list_item *it)
117
{
120
- const char *c = color(WT_STATUS_UNMERGED);
118
+ const char *c = color(WT_STATUS_UNMERGED, s);
119
struct wt_status_change_data *d = it->util;
120
struct strbuf onebuf = STRBUF_INIT;
121
const char *one, *how = "bug";
122
123
one = quote_path(it->string, -1, &onebuf, s->prefix);
126
- color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
124
+ color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
125
switch (d->stagemask) {
126
case 1: how = "both deleted:"; break;
127
case 2: how = "added by us:"; break;
@@ -142,7 +140,7 @@ static void wt_status_print_change_data(struct wt_status *s,
140
struct string_list_item *it)
141
{
142
struct wt_status_change_data *d = it->util;
145
- const char *c = color(change_type);
143
+ const char *c = color(change_type, s);
144
int status = status;
145
char *one_name;
146
char *two_name;
@@ -164,7 +162,7 @@ static void wt_status_print_change_data(struct wt_status *s,
162
one = quote_path(one_name, -1, &onebuf, s->prefix);
163
two = quote_path(two_name, -1, &twobuf, s->prefix);
164
167
- color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
165
+ color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
166
switch (status) {
167
case DIFF_STATUS_ADDED:
168
color_fprintf(s->fp, c, "new file: %s", one);
@@ -450,7 +448,7 @@ static void wt_status_print_submodule_summary(struct wt_status *s)
448
NULL
449
};
450
453
- sprintf(summary_limit, "%d", wt_status_submodule_summary);
451
+ sprintf(summary_limit, "%d", s->submodule_summary);
452
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
453
454
memset(&sm_summary, 0, sizeof(sm_summary));
@@ -471,8 +469,7 @@ static void wt_status_print_untracked(struct wt_status *s)
469
struct strbuf buf = STRBUF_INIT;
470
471
memset(&dir, 0, sizeof(dir));
474
-
475
- if (!s->untracked)
472
+ if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
473
dir.flags |=
474
DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
475
setup_standard_excludes(&dir);
@@ -487,8 +484,8 @@ static void wt_status_print_untracked(struct wt_status *s)
484
wt_status_print_untracked_header(s);
485
shown_header = 1;
486
}
490
- color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
491
- color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
487
+ color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
488
+ color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
489
quote_path(ent->name, ent->len,
490
&buf, s->prefix));
491
}
@@ -532,15 +529,15 @@ static void wt_status_print_tracking(struct wt_status *s)
529
return;
530
531
for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
535
- color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
532
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
533
"# %.*s", (int)(ep - cp), cp);
537
- color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
534
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
535
}
536
537
void wt_status_print(struct wt_status *s)
538
{
539
unsigned char sha1[20];
543
- const char *branch_color = color(WT_STATUS_HEADER);
540
+ const char *branch_color = color(WT_STATUS_HEADER, s);
541
542
s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
543
if (s->branch) {
@@ -550,10 +547,10 @@ void wt_status_print(struct wt_status *s)
547
branch_name += 11;
548
else if (!strcmp(branch_name, "HEAD")) {
549
branch_name = "";
553
- branch_color = color(WT_STATUS_NOBRANCH);
550
+ branch_color = color(WT_STATUS_NOBRANCH, s);
551
on_what = "Not currently on any branch.";
552
}
556
- color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
553
+ color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
554
color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
555
if (!s->is_initial)
556
wt_status_print_tracking(s);
@@ -562,17 +559,17 @@ void wt_status_print(struct wt_status *s)
559
wt_status_collect_changes(s);
560
561
if (s->is_initial) {
565
- color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
566
- color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
567
- color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
562
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
563
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
564
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
565
}
566
567
wt_status_print_unmerged(s);
568
wt_status_print_updated(s);
569
wt_status_print_changed(s);
573
- if (wt_status_submodule_summary)
570
+ if (s->submodule_summary)
571
wt_status_print_submodule_summary(s);
575
- if (show_untracked_files)
572
+ if (s->show_untracked_files)
573
wt_status_print_untracked(s);
574
else if (s->commitable)
575
fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
@@ -590,7 +587,7 @@ void wt_status_print(struct wt_status *s)
587
printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
588
else if (s->is_initial)
589
printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
593
- else if (!show_untracked_files)
590
+ else if (!s->show_untracked_files)
591
printf("nothing to commit (use -u to show untracked files)\n");
592
else
593
printf("nothing to commit (working directory clean)\n");
@@ -599,15 +596,17 @@ void wt_status_print(struct wt_status *s)
596
597
int git_status_config(const char *k, const char *v, void *cb)
598
{
599
+ struct wt_status *s = cb;
600
+
601
if (!strcmp(k, "status.submodulesummary")) {
602
int is_bool;
604
- wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
605
- if (is_bool && wt_status_submodule_summary)
606
- wt_status_submodule_summary = -1;
603
+ s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
604
+ if (is_bool && s->submodule_summary)
605
+ s->submodule_summary = -1;
606
return 0;
607
}
608
if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
610
- wt_status_use_color = git_config_colorbool(k, v, -1);
609
+ s->use_color = git_config_colorbool(k, v, -1);
610
return 0;
611
}
612
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
@@ -618,21 +617,21 @@ int git_status_config(const char *k, const char *v, void *cb)
617
return 0;
618
}
619
if (!strcmp(k, "status.relativepaths")) {
621
- wt_status_relative_paths = git_config_bool(k, v);
620
+ s->relative_paths = git_config_bool(k, v);
621
return 0;
622
}
623
if (!strcmp(k, "status.showuntrackedfiles")) {
624
if (!v)
625
return config_error_nonbool(k);
626
else if (!strcmp(v, "no"))
628
- show_untracked_files = SHOW_NO_UNTRACKED_FILES;
627
+ s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
628
else if (!strcmp(v, "normal"))
630
- show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
629
+ s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
630
else if (!strcmp(v, "all"))
632
- show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
631
+ s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
632
else
633
return error("Invalid untracked files mode '%s'", v);
634
return 0;
635
}
637
- return git_diff_ui_config(k, v, cb);
636
+ return git_diff_ui_config(k, v, NULL);
637
}
wt-status.h
+5
-4
@@ -18,7 +18,6 @@ enum untracked_status_type {
18
SHOW_NORMAL_UNTRACKED_FILES,
19
SHOW_ALL_UNTRACKED_FILES
20
};
21
-extern enum untracked_status_type show_untracked_files;
21
22
struct wt_status_change_data {
23
int worktree_status;
@@ -33,8 +32,12 @@ struct wt_status {
32
const char *reference;
33
int verbose;
34
int amend;
36
- int untracked;
35
int nowarn;
36
+ int use_color;
37
+ int relative_paths;
38
+ int submodule_summary;
39
+ enum untracked_status_type show_untracked_files;
40
+
41
/* These are computed during processing of the individual sections */
42
int commitable;
43
int workdir_dirty;
@@ -46,8 +49,6 @@ struct wt_status {
49
};
50
51
int git_status_config(const char *var, const char *value, void *cb);
49
-extern int wt_status_use_color;
50
-extern int wt_status_relative_paths;
52
void wt_status_prepare(struct wt_status *s);
53
void wt_status_print(struct wt_status *s);
54
void wt_status_collect_changes(struct wt_status *s);