Skip to content

Commit f0dd042

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0787daf commit f0dd042

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

attr.c

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -677,26 +677,30 @@ static struct attr_stack *read_attr_from_array(const char **list)
677677
}
678678

679679
/*
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.
696687
*/
697688
static enum git_attr_direction direction;
698689
static struct index_state *use_index;
699690

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+
700704
static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
701705
{
702706
FILE *fp = fopen(path, "r");
@@ -1148,19 +1152,6 @@ void git_all_attrs(const char *path, struct attr_check *check)
11481152
}
11491153
}
11501154

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-
11641155
void attr_start(void)
11651156
{
11661157
#ifndef NO_PTHREADS

attr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ enum git_attr_direction {
7272
GIT_ATTR_CHECKOUT,
7373
GIT_ATTR_INDEX
7474
};
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);
7677

7778
extern void attr_start(void);
7879

0 commit comments

Comments
 (0)