2018-03-24 15:44:56 +08:00
|
|
|
#include "test-tool.h"
|
2011-12-07 01:43:35 +08:00
|
|
|
#include "cache.h"
|
2014-10-01 18:28:42 +08:00
|
|
|
#include "lockfile.h"
|
2011-12-07 01:43:35 +08:00
|
|
|
#include "tree.h"
|
|
|
|
#include "cache-tree.h"
|
|
|
|
|
2018-03-24 15:44:56 +08:00
|
|
|
int cmd__scrap_cache_tree(int ac, const char **av)
|
2011-12-07 01:43:35 +08:00
|
|
|
{
|
lock_file: move static locks into functions
Placing `struct lock_file`s on the stack used to be a bad idea, because
the temp- and lockfile-machinery would keep a pointer into the struct.
But after 076aa2cbd (tempfile: auto-allocate tempfiles on heap,
2017-09-05), we can safely have lockfiles on the stack. (This applies
even if a user returns early, leaving a locked lock behind.)
Each of these `struct lock_file`s is used from within a single function.
Move them into the respective functions to make the scope clearer and
drop the staticness.
For good measure, I have inspected these sites and come to believe that
they always release the lock, with the possible exception of bailing out
using `die()` or `exit()` or by returning from a `cmd_foo()`.
As pointed out by Jeff King, it would be bad if someone held on to a
`struct lock_file *` for some reason. After some grepping, I agree with
his findings: no-one appears to be doing that.
After this commit, the remaining occurrences of "static struct
lock_file" are locks that are used from within different functions. That
is, they need to remain static. (Short of more intrusive changes like
passing around pointers to non-static locks.)
Signed-off-by: Martin Ågren <martin.agren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-05-10 04:55:39 +08:00
|
|
|
struct lock_file index_lock = LOCK_INIT;
|
|
|
|
|
2016-10-20 14:16:59 +08:00
|
|
|
setup_git_directory();
|
2016-12-08 02:33:54 +08:00
|
|
|
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
|
2011-12-07 01:43:35 +08:00
|
|
|
if (read_cache() < 0)
|
|
|
|
die("unable to read index file");
|
|
|
|
active_cache_tree = NULL;
|
2014-06-13 20:19:23 +08:00
|
|
|
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
|
2011-12-07 01:43:35 +08:00
|
|
|
die("unable to write index file");
|
|
|
|
return 0;
|
|
|
|
}
|