attr: reformat git_attr_set_direction() function

Move the 'git_attr_set_direction()' up to be closer to the variables that it modifies as well as a small formatting by renaming the variable 'new' to 'new_direction' so that it is more descriptive. Update the comment about how 'direction' is used to read the state of the world. It should be noted that callers of 'git_attr_set_direction()' should ensure that other threads are not making calls into the attribute system until after the call to 'git_attr_set_direction()' completes. This function essentially acts as reset button for the attribute system and should be handled with care. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jan 27, 2017 at 18:02 UTC f0dd042148233ad4681b29f35f3bc3ba3b962474
2 files changed +22 -30
attr.c
+20 -29
@@ -677,26 +677,30 @@ static struct attr_stack *read_attr_from_array(const char **list)
677 }
678
679 /*
680 - * NEEDSWORK: these two are tricky. The callers assume there is a
681 - * single, system-wide global state "where we read attributes from?"
682 - * and when the state is flipped by calling git_attr_set_direction(),
683 - * attr_stack is discarded so that subsequent attr_check will lazily
684 - * read from the right place. And they do not know or care who called
685 - * by them uses the attribute subsystem, hence have no knowledge of
686 - * existing git_attr_check instances or future ones that will be
687 - * created).
688 - *
689 - * Probably we need a thread_local that holds these two variables,
690 - * and a list of git_attr_check instances (which need to be maintained
691 - * by hooking into git_attr_check_alloc(), git_attr_check_initl(), and
692 - * git_attr_check_clear(). Then git_attr_set_direction() updates the
693 - * fields in that thread_local for these two variables, iterate over
694 - * all the active git_attr_check instances and discard the attr_stack
695 - * they hold. Yuck, but it sounds doable.
680 + * Callers into the attribute system assume there is a single, system-wide
681 + * global state where attributes are read from and when the state is flipped by
682 + * calling git_attr_set_direction(), the stack frames that have been
683 + * constructed need to be discarded so so that subsequent calls into the
684 + * attribute system will lazily read from the right place. Since changing
685 + * direction causes a global paradigm shift, it should not ever be called while
686 + * another thread could potentially be calling into the attribute system.
687 */
688 static enum git_attr_direction direction;
689 static struct index_state *use_index;
690
691 +void git_attr_set_direction(enum git_attr_direction new_direction,
692 + struct index_state *istate)
693 +{
694 + if (is_bare_repository() && new_direction != GIT_ATTR_INDEX)
695 + die("BUG: non-INDEX attr direction in a bare repo");
696 +
697 + if (new_direction != direction)
698 + drop_all_attr_stacks();
699 +
700 + direction = new_direction;
701 + use_index = istate;
702 +}
703 +
704 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
705 {
706 FILE *fp = fopen(path, "r");
@@ -1148,19 +1152,6 @@ void git_all_attrs(const char *path, struct attr_check *check)
1152 }
1153 }
1154
1151 -void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
1152 -{
1153 - enum git_attr_direction old = direction;
1154 -
1155 - if (is_bare_repository() && new != GIT_ATTR_INDEX)
1156 - die("BUG: non-INDEX attr direction in a bare repo");
1157 -
1158 - direction = new;
1159 - if (new != old)
1160 - drop_all_attr_stacks();
1161 - use_index = istate;
1162 -}
1163 -
1155 void attr_start(void)
1156 {
1157 #ifndef NO_PTHREADS
attr.h
+2 -1
@@ -72,7 +72,8 @@ enum git_attr_direction {
72 GIT_ATTR_CHECKOUT,
73 GIT_ATTR_INDEX
74 };
75 -void git_attr_set_direction(enum git_attr_direction, struct index_state *);
75 +void git_attr_set_direction(enum git_attr_direction new_direction,
76 + struct index_state *istate);
77
78 extern void attr_start(void);
79