Skip to content

Commit

Permalink
builtin alias: add --delete
Browse files Browse the repository at this point in the history
What if there is more than one definition in the file or in multiple files?
  • Loading branch information
phillipwood committed May 15, 2023
1 parent 4b53468 commit 43f3746
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions builtin/alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
enum cmd {
CMD_GET,
CMD_SET,
CMD_DELETE,
CMD_EDIT,
};

static const char * const alias_usage[] = {
N_("git alias <name> [<command> [args ...]]"),
N_("git alias --edit <name>"),
N_("git alias ( --edit | --delete ) <name>"),
NULL
};

Expand Down Expand Up @@ -169,6 +170,35 @@ static int find_alias_definition(const char *alias,
return res;
}

static int delete_alias(const char* alias)
{
int res;
char *file;
const char *origin;
struct strbuf key = STRBUF_INIT;

res = find_alias_definition(alias, NULL, &file, &origin);
if (res < 0) {
goto out;
} else if (res) {
res = error(_("alias '%s' does not exist"), alias);
goto out;
} else if (strcmp(origin, "file")) {
res = error(_("cannot delete alias set in %s"), origin);
goto out;
}
strbuf_addf(&key, "alias.%s", alias);
res = git_config_set_in_file_gently(file, key.buf, NULL);
strbuf_release(&key);
if (res)
error(_("could not delete alias '%s'"), alias);
else
printf(_("deleted alias '%s'\n"), alias);
out:
free(file);
return res;
}

static int edit_definition(const char* alias, const char *old_definition,
char **new_definition)
{
Expand Down Expand Up @@ -343,14 +373,19 @@ int cmd_alias(int argc, const char **argv, const char *prefix)
enum cmd cmd = CMD_GET;

struct option options[] = {
OPT_CMDMODE(0, "delete", &cmd, _("delete alias"), CMD_DELETE),
OPT_CMDMODE('e', "edit", &cmd, _("edit alias definition"),
CMD_EDIT),
OPT_END()
};

argc = parse_options(argc, argv, prefix, options, alias_usage,
PARSE_OPT_STOP_AT_NON_OPTION);
if (cmd == CMD_EDIT) {
if (cmd == CMD_DELETE) {
if (argc != 1)
usage_with_options(alias_usage, options);
return !!delete_alias(argv[0]);
} else if (cmd == CMD_EDIT) {
if (argc != 1)
usage_with_options(alias_usage, options);
return !!update_alias(argc, argv, cmd);
Expand Down

0 comments on commit 43f3746

Please sign in to comment.