setup: refactor upgrade_repository_format()
to have common exit
The `upgrade_repository_format()` function has multiple exit paths, which means that there is no common cleanup of acquired resources. While this isn't much of a problem right now, we're about to fix a memory leak that would require us to free the resource in every one of those exit paths. Refactor the code to have a common exit path so that the subsequent memory leak fix becomes easier to implement. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
568cc818cc
commit
4ce14e1325
31
setup.c
31
setup.c
@ -693,29 +693,38 @@ int upgrade_repository_format(int target_version)
|
|||||||
struct strbuf err = STRBUF_INIT;
|
struct strbuf err = STRBUF_INIT;
|
||||||
struct strbuf repo_version = STRBUF_INIT;
|
struct strbuf repo_version = STRBUF_INIT;
|
||||||
struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
|
struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
|
||||||
|
int ret;
|
||||||
|
|
||||||
strbuf_git_common_path(&sb, the_repository, "config");
|
strbuf_git_common_path(&sb, the_repository, "config");
|
||||||
read_repository_format(&repo_fmt, sb.buf);
|
read_repository_format(&repo_fmt, sb.buf);
|
||||||
strbuf_release(&sb);
|
strbuf_release(&sb);
|
||||||
|
|
||||||
if (repo_fmt.version >= target_version)
|
if (repo_fmt.version >= target_version) {
|
||||||
return 0;
|
ret = 0;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
if (verify_repository_format(&repo_fmt, &err) < 0) {
|
if (verify_repository_format(&repo_fmt, &err) < 0) {
|
||||||
error("cannot upgrade repository format from %d to %d: %s",
|
ret = error("cannot upgrade repository format from %d to %d: %s",
|
||||||
repo_fmt.version, target_version, err.buf);
|
repo_fmt.version, target_version, err.buf);
|
||||||
strbuf_release(&err);
|
goto out;
|
||||||
return -1;
|
}
|
||||||
|
if (!repo_fmt.version && repo_fmt.unknown_extensions.nr) {
|
||||||
|
ret = error("cannot upgrade repository format: "
|
||||||
|
"unknown extension %s",
|
||||||
|
repo_fmt.unknown_extensions.items[0].string);
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
if (!repo_fmt.version && repo_fmt.unknown_extensions.nr)
|
|
||||||
return error("cannot upgrade repository format: "
|
|
||||||
"unknown extension %s",
|
|
||||||
repo_fmt.unknown_extensions.items[0].string);
|
|
||||||
|
|
||||||
strbuf_addf(&repo_version, "%d", target_version);
|
strbuf_addf(&repo_version, "%d", target_version);
|
||||||
git_config_set("core.repositoryformatversion", repo_version.buf);
|
git_config_set("core.repositoryformatversion", repo_version.buf);
|
||||||
|
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
|
out:
|
||||||
strbuf_release(&repo_version);
|
strbuf_release(&repo_version);
|
||||||
return 1;
|
strbuf_release(&err);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_repository_format(struct repository_format *format)
|
static void init_repository_format(struct repository_format *format)
|
||||||
|
Reference in New Issue
Block a user