Skip to content

Commit

Permalink
Merge branch 'ps/config-error'
Browse files Browse the repository at this point in the history
Many codepaths forget to check return value from git_config_set();
the function is made to die() to make sure we do not proceed when
setting a configuration variable failed.

* ps/config-error:
  config: rename git_config_set_or_die to git_config_set
  config: rename git_config_set to git_config_set_gently
  compat: die when unable to set core.precomposeunicode
  sequencer: die on config error when saving replay opts
  init-db: die on config errors when initializing empty repo
  clone: die on config error in cmd_clone
  remote: die on config error when manipulating remotes
  remote: die on config error when setting/adding branches
  remote: die on config error when setting URL
  submodule--helper: die on config error when cloning module
  submodule: die on config error when linking modules
  branch: die on config error when editing branch description
  branch: die on config error when unsetting upstream
  branch: report errors in tracking branch setup
  config: introduce set_or_die wrappers
  • Loading branch information
Junio C Hamano committed Feb 26, 2016
2 parents 56d4e7e + 3d18064 commit 225caa7
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 105 deletions.
50 changes: 36 additions & 14 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ static int should_setup_rebase(const char *origin)
return 0;
}

void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
static const char tracking_advice[] =
N_("\n"
"After fixing the error cause you may try to fix up\n"
"the remote tracking information by invoking\n"
"\"git branch --set-upstream-to=%s%s%s\".");

int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
{
const char *shortname = NULL;
struct strbuf key = STRBUF_INIT;
Expand All @@ -60,20 +66,23 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
&& !origin) {
warning(_("Not setting branch %s as its own upstream."),
local);
return;
return 0;
}

strbuf_addf(&key, "branch.%s.remote", local);
git_config_set(key.buf, origin ? origin : ".");
if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
goto out_err;

strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.merge", local);
git_config_set(key.buf, remote);
if (git_config_set_gently(key.buf, remote) < 0)
goto out_err;

if (rebasing) {
strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.rebase", local);
git_config_set(key.buf, "true");
if (git_config_set_gently(key.buf, "true") < 0)
goto out_err;
}
strbuf_release(&key);

Expand Down Expand Up @@ -102,23 +111,36 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
local, remote);
}
}

return 0;

out_err:
strbuf_release(&key);
error(_("Unable to write upstream branch configuration"));

advise(_(tracking_advice),
origin ? origin : "",
origin ? "/" : "",
shortname ? shortname : remote);

return -1;
}

/*
* This is called when new_ref is branched off of orig_ref, and tries
* to infer the settings for branch.<new_ref>.{remote,merge} from the
* config.
*/
static int setup_tracking(const char *new_ref, const char *orig_ref,
enum branch_track track, int quiet)
static void setup_tracking(const char *new_ref, const char *orig_ref,
enum branch_track track, int quiet)
{
struct tracking tracking;
int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;

memset(&tracking, 0, sizeof(tracking));
tracking.spec.dst = (char *)orig_ref;
if (for_each_remote(find_tracked_branch, &tracking))
return 1;
return;

if (!tracking.matches)
switch (track) {
Expand All @@ -127,18 +149,18 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
case BRANCH_TRACK_OVERRIDE:
break;
default:
return 1;
return;
}

if (tracking.matches > 1)
return error(_("Not tracking: ambiguous information for ref %s"),
orig_ref);
die(_("Not tracking: ambiguous information for ref %s"),
orig_ref);

install_branch_config(config_flags, new_ref, tracking.remote,
tracking.src ? tracking.src : orig_ref);
if (install_branch_config(config_flags, new_ref, tracking.remote,
tracking.src ? tracking.src : orig_ref) < 0)
exit(-1);

free(tracking.src);
return 0;
}

int read_branch_desc(struct strbuf *buf, const char *branch_name)
Expand Down
3 changes: 2 additions & 1 deletion branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ void remove_branch_state(void);
/*
* Configure local branch "local" as downstream to branch "remote"
* from remote "origin". Used by git branch --set-upstream.
* Returns 0 on success.
*/
#define BRANCH_CONFIG_VERBOSE 01
extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);
extern int install_branch_config(int flag, const char *local, const char *origin, const char *remote);

/*
* Read branch description
Expand Down
5 changes: 2 additions & 3 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ static const char edit_description[] = "BRANCH_DESCRIPTION";

static int edit_branch_description(const char *branch_name)
{
int status;
struct strbuf buf = STRBUF_INIT;
struct strbuf name = STRBUF_INIT;

Expand All @@ -595,11 +594,11 @@ static int edit_branch_description(const char *branch_name)
strbuf_stripspace(&buf, 1);

strbuf_addf(&name, "branch.%s.description", branch_name);
status = git_config_set(name.buf, buf.len ? buf.buf : NULL);
git_config_set(name.buf, buf.len ? buf.buf : NULL);
strbuf_release(&name);
strbuf_release(&buf);

return status;
return 0;
}

int cmd_branch(int argc, const char **argv, const char *prefix)
Expand Down
2 changes: 1 addition & 1 deletion builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ static int checkout(void)

static int write_one_config(const char *key, const char *value, void *data)
{
return git_config_set_multivar(key, value ? value : "true", "^$", 0);
return git_config_set_multivar_gently(key, value ? value : "true", "^$", 0);
}

static void write_config(struct string_list *config)
Expand Down
28 changes: 14 additions & 14 deletions builtin/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
check_write();
check_argc(argc, 2, 2);
value = normalize_value(argv[0], argv[1]);
ret = git_config_set_in_file(given_config_source.file, argv[0], value);
ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
if (ret == CONFIG_NOTHING_SET)
error("cannot overwrite multiple values with a single value\n"
" Use a regexp, --add or --replace-all to change %s.", argv[0]);
Expand All @@ -625,23 +625,23 @@ int cmd_config(int argc, const char **argv, const char *prefix)
check_write();
check_argc(argc, 2, 3);
value = normalize_value(argv[0], argv[1]);
return git_config_set_multivar_in_file(given_config_source.file,
argv[0], value, argv[2], 0);
return git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], value, argv[2], 0);
}
else if (actions == ACTION_ADD) {
check_write();
check_argc(argc, 2, 2);
value = normalize_value(argv[0], argv[1]);
return git_config_set_multivar_in_file(given_config_source.file,
argv[0], value,
CONFIG_REGEX_NONE, 0);
return git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], value,
CONFIG_REGEX_NONE, 0);
}
else if (actions == ACTION_REPLACE_ALL) {
check_write();
check_argc(argc, 2, 3);
value = normalize_value(argv[0], argv[1]);
return git_config_set_multivar_in_file(given_config_source.file,
argv[0], value, argv[2], 1);
return git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], value, argv[2], 1);
}
else if (actions == ACTION_GET) {
check_argc(argc, 1, 2);
Expand All @@ -667,17 +667,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)
check_write();
check_argc(argc, 1, 2);
if (argc == 2)
return git_config_set_multivar_in_file(given_config_source.file,
argv[0], NULL, argv[1], 0);
return git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], NULL, argv[1], 0);
else
return git_config_set_in_file(given_config_source.file,
argv[0], NULL);
return git_config_set_in_file_gently(given_config_source.file,
argv[0], NULL);
}
else if (actions == ACTION_UNSET_ALL) {
check_write();
check_argc(argc, 1, 2);
return git_config_set_multivar_in_file(given_config_source.file,
argv[0], NULL, argv[1], 1);
return git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], NULL, argv[1], 1);
}
else if (actions == ACTION_RENAME_SECTION) {
int ret;
Expand Down
2 changes: 1 addition & 1 deletion builtin/init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ static int create_default_files(const char *template_path)
git_config_set("core.bare", "false");
/* allow template config file to override the default */
if (log_all_ref_updates == -1)
git_config_set("core.logallrefupdates", "true");
git_config_set("core.logallrefupdates", "true");
if (needs_work_tree_config(get_git_dir(), work_tree))
git_config_set("core.worktree", work_tree);
}
Expand Down
Loading

0 comments on commit 225caa7

Please sign in to comment.