2022-11-19 13:07:37 +00:00
|
|
|
#define USE_THE_INDEX_VARIABLE
|
2018-03-24 07:44:56 +00:00
|
|
|
#include "test-tool.h"
|
2014-10-01 10:28:42 +00:00
|
|
|
#include "lockfile.h"
|
2023-05-16 06:33:56 +00:00
|
|
|
#include "read-cache-ll.h"
|
2023-04-22 20:17:20 +00:00
|
|
|
#include "repository.h"
|
2023-03-21 06:26:05 +00:00
|
|
|
#include "setup.h"
|
2011-12-06 17:43:35 +00:00
|
|
|
#include "tree.h"
|
|
|
|
#include "cache-tree.h"
|
|
|
|
|
2023-03-28 20:57:25 +00:00
|
|
|
int cmd__scrap_cache_tree(int ac UNUSED, const char **av UNUSED)
|
2011-12-06 17:43:35 +00: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-09 20:55:39 +00:00
|
|
|
struct lock_file index_lock = LOCK_INIT;
|
|
|
|
|
2016-10-20 06:16:59 +00:00
|
|
|
setup_git_directory();
|
2022-11-19 13:07:35 +00:00
|
|
|
repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
|
|
|
|
if (repo_read_index(the_repository) < 0)
|
2011-12-06 17:43:35 +00:00
|
|
|
die("unable to read index file");
|
2022-11-19 13:07:34 +00:00
|
|
|
cache_tree_free(&the_index.cache_tree);
|
|
|
|
the_index.cache_tree = NULL;
|
2014-06-13 12:19:23 +00:00
|
|
|
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
|
2011-12-06 17:43:35 +00:00
|
|
|
die("unable to write index file");
|
|
|
|
return 0;
|
|
|
|
}
|