diff options
Diffstat (limited to 'src/edit.c')
| -rw-r--r-- | src/edit.c | 122 |
1 files changed, 2 insertions, 120 deletions
@@ -205,29 +205,6 @@ static int is_rule_for(const char *line, const char *user, const char *repo) return same; } -static int is_set_for(const char *line, const char *key) -{ - char *copy = xstrdup(line); - char *text = authz_trim(copy); - char *hash = strchr(text, '#'); - size_t klen = strlen(key); - int hit = 0; - - if (hash != NULL) - *hash = '\0'; - if (strncmp(text, "set", 3) == 0 && isspace((unsigned char)text[3])) { - text = authz_trim(text + 3); - hit = strncmp(text, key, klen) == 0 && - (text[klen] == '\0' || isspace((unsigned char)text[klen])); - } - free(copy); - return hit; -} - -/* --------------------------------------------------------------- emitting */ - -/* "user repo level ref ref" - one line, no alignment tricks, because the next - * tool to read this is diff. */ static char *rule_text(const char *user, const char *repo, const char *level, int nrefs, char **refs) { @@ -392,7 +369,8 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) lines_push(&lines, xstrdup("# <user> <repo> <write|force|admin> " "[ref pattern ...] levels stack: " "admin > force > write\n")); - lines_push(&lines, xstrdup("set protected-refs refs/heads/main\n")); + lines_push(&lines, xstrdup("# Name the refs a rule covers: with no " + "patterns it covers the whole repository.\n")); } else if (read_lines(path, &lines) != 0) return 3; @@ -493,99 +471,3 @@ int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) return finish(&lines, path, o, what); } } - -int cmd_protect(const char *path, edit_opts o, int argc, char **argv) -{ - lines_t lines; - const char *repo = NULL; - char key[256]; - char *line; - strbuf_t text; - int i; - int replaced = -1; - int created = 0; - for (i = 0; i < argc; i++) { /* --repo anywhere, like --out */ - if (strcmp(argv[i], "--repo") != 0) - continue; - if (i + 1 >= argc) { - fprintf(stderr, "%s: --repo wants a repository name\n", progname); - return 64; - } - repo = argv[i + 1]; - for (; i + 2 < argc; i++) - argv[i] = argv[i + 2]; - argc -= 2; - break; - } - if (argc < 1) { - fprintf(stderr, "%s: protect wants [--repo <name>] <ref pattern> ...\n", - progname); - return 64; - } - if (repo != NULL && !authz_repo_pattern_ok(repo)) { - fprintf(stderr, "%s: invalid repo pattern \"%s\"\n", progname, repo); - return 64; - } - for (i = 0; i < argc; i++) { - if (argv[i][0] == '-' && argv[i][1] == '-') { - fprintf(stderr, "%s: unknown option \"%s\" (the only one here is " - "--repo, which comes first)\n", progname, argv[i]); - return 64; - } - if (argv[i][0] == '!' || argv[i][0] == '-') { - fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n", - progname, argv[i]); - return 64; - } - } - if (refuse_broken(path, &created) != 0) - return 3; - if (created) { - lines_init(&lines); - lines_push(&lines, xstrdup("# git-authz policy\n")); - } else if (read_lines(path, &lines) != 0) - return 3; - - if (repo != NULL) - snprintf(key, sizeof(key), "protected-refs.%s", repo); - else - snprintf(key, sizeof(key), "protected-refs"); - - sb_init(&text); - sb_addf(&text, "set %s", key); - for (i = 0; i < argc; i++) { - char *one = authz_norm_ref(argv[i]); - - sb_addf(&text, " %s", one); - free(one); - } - sb_add(&text, "\n"); - - line = xstrdup(text.s); /* the array owns its own copy; text is - * released by sb_free below */ - for (i = 0; i < (int)lines.n; i++) { - if (!is_set_for(lines.v[i], key)) - continue; - free(lines.v[i]); - lines.v[i] = line; - replaced = i + 1; - break; - } - if (replaced < 0) { - lines_push(&lines, line); - replaced = (int)lines.n; - } - sb_free(&text); - - { - char what[512]; - - if (created) - snprintf(what, sizeof(what), "protected refs for %s (%s, new policy)", - repo != NULL ? repo : "every repository", key); - else - snprintf(what, sizeof(what), "protected refs for %s (%s, line %d)", - repo != NULL ? repo : "every repository", key, replaced); - return finish(&lines, path, o, what); - } -} |
