int git_reference_dwim(git_reference **out, git_repository *repo, const char *refname) { int error = 0, i; bool fallbackmode = true, foundvalid = false; git_reference *ref; git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT; static const char* formatters[] = { "%s", GIT_REFS_DIR "%s", GIT_REFS_TAGS_DIR "%s", GIT_REFS_HEADS_DIR "%s", GIT_REFS_REMOTES_DIR "%s", GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE, NULL }; if (*refname) git_buf_puts(&name, refname); else { git_buf_puts(&name, GIT_HEAD_FILE); fallbackmode = false; } for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) { git_buf_clear(&refnamebuf); if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0) goto cleanup; if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) { error = GIT_EINVALIDSPEC; continue; } foundvalid = true; error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1); if (!error) { *out = ref; error = 0; goto cleanup; } if (error != GIT_ENOTFOUND) goto cleanup; } cleanup: if (error && !foundvalid) { /* never found a valid reference name */ giterr_set(GITERR_REFERENCE, "Could not use '%s' as valid reference name", git_buf_cstr(&name)); } git_buf_free(&name); git_buf_free(&refnamebuf); return error; }
static int filter_ref__cb(git_remote_head *head, void *payload) { struct filter_payload *p = payload; int match = 0; if (!git_reference_is_valid_name(head->name)) return 0; if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0) p->found_head = 1; else if (p->remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { /* * If tagopt is --tags, then we only use the default * tags refspec and ignore the remote's */ if (git_refspec_src_matches(p->tagspec, head->name)) match = 1; else return 0; } else if (git_remote__matching_refspec(p->remote, head->name)) match = 1; if (!match) return 0; /* If we have the object, mark it so we don't ask for it */ if (git_odb_exists(p->odb, &head->oid)) head->local = 1; else p->remote->need_pack = 1; return git_vector_insert(&p->remote->refs, head); }
/** * ggit_ref_is_valid_name: * @name: the name to validate. * * Check if the given @name is a valid name for a reference. Note that @name * should be the full ref name (including prefixes). * * Valid toplevel names can contain only capital letters and underscores * and must start and end with a letter (e.g. HEAD, ORIG_HEAD). * * Valid refs/ names may contain any characters, except '~', '^', ':', '\', '?', * '[', '*', ".." and "@{", because they are interpreted by revparse. * * Returns: %TRUE if @name is valid, %FALSE otherwise. * **/ gboolean ggit_ref_is_valid_name (const gchar *name) { g_return_val_if_fail (name != NULL, FALSE); return git_reference_is_valid_name (name) == 1; }
static int maybe_want(git_remote *remote, git_remote_head *head, git_odb *odb, git_refspec *tagspec) { int match = 0; if (!git_reference_is_valid_name(head->name)) return 0; if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { /* * If tagopt is --tags, always request tags * in addition to the remote's refspecs */ if (git_refspec_src_matches(tagspec, head->name)) match = 1; } if (!match && git_remote__matching_refspec(remote, head->name)) match = 1; if (!match) return 0; /* If we have the object, mark it so we don't ask for it */ if (git_odb_exists(odb, &head->oid)) { head->local = 1; } else remote->need_pack = 1; return git_vector_insert(&remote->refs, head); }
int luagi_reference_create_matching( lua_State *L ) { git_repository **repo = checkrepo( L, 1 ); const char *name = luaL_checkstring( L, 2 ); if( git_reference_is_valid_name( name ) == 0 ) { luaL_error( L, "invalid name" ); return 0; } git_oid oid; if( luagi_check_oid( &oid, L, 3 ) ) { ltk_error_abort( L ); } git_oid curr_oid; if( luagi_check_oid( &curr_oid, L, 4 ) ) { ltk_error_abort( L ); } const char *log_message = luaL_checkstring( L, 6 ); int force = lua_toboolean( L, 7 ); git_reference **out = lua_newuserdata( L, sizeof( git_reference *) ); if( git_reference_create_matching( out, *repo, name, &oid, force, &curr_oid, log_message ) ) { return ltk_push_git_error( L ); } ltk_setmetatable( L, LUAGI_REFERENCE_FUNCS ); return 1; }
static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname) { int error = 0, i; bool fallbackmode = true; git_reference *ref; git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT; static const char* formatters[] = { "%s", GIT_REFS_DIR "%s", GIT_REFS_TAGS_DIR "%s", GIT_REFS_HEADS_DIR "%s", GIT_REFS_REMOTES_DIR "%s", GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE, NULL }; if (*refname) git_buf_puts(&name, refname); else { git_buf_puts(&name, GIT_HEAD_FILE); fallbackmode = false; } for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) { git_buf_clear(&refnamebuf); if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0) goto cleanup; if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) { error = GIT_EINVALIDSPEC; continue; } error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1); if (!error) { *out = ref; error = 0; goto cleanup; } if (error != GIT_ENOTFOUND) goto cleanup; } cleanup: git_buf_free(&name); git_buf_free(&refnamebuf); return error; }
PyObject * reference_is_valid_name(PyObject *self, PyObject *py_refname) { char* refname; int result; refname = py_str_to_c_str(py_refname, NULL); if (refname == NULL) { return NULL; } result = git_reference_is_valid_name(refname); free(refname); return PyBool_FromLong(result); }
int create_deletion_refspecs(git_vector *out, const git_remote_head **heads, size_t heads_len) { git_buf del_spec = GIT_BUF_INIT; size_t i; for (i = 0; i < heads_len; i++) { const git_remote_head *head = heads[i]; /* Ignore malformed ref names (which also saves us from tag^{} */ if (!git_reference_is_valid_name(head->name)) return 0; /* Create a refspec that deletes a branch in the remote */ if (strcmp(head->name, "refs/heads/master")) { cl_git_pass(git_buf_putc(&del_spec, ':')); cl_git_pass(git_buf_puts(&del_spec, head->name)); cl_git_pass(git_vector_insert(out, git_buf_detach(&del_spec))); } } return 0; }
int luagi_reference_symbolic_create( lua_State *L ) { git_repository **repo = checkrepo( L, 1 ); const char *name = luaL_checkstring( L, 2 ); if( git_reference_is_valid_name( name ) == 0 ) { luaL_error( L, "invalid name" ); return 0; } const char *target = luaL_checkstring( L, 3 ); const char *log_message = luaL_checkstring( L, 5 ); int force = lua_toboolean( L, 6 ); git_reference **out = lua_newuserdata( L, sizeof( git_reference *) ); if( git_reference_symbolic_create( out, *repo, name, target, force, log_message ) ) { return ltk_push_git_error( L ); } ltk_setmetatable( L, LUAGI_REFERENCE_FUNCS ); return 1; }
int luagi_reference_is_valid_name( lua_State *L ) { const char *name = luaL_checkstring( L, 1 ); lua_pushboolean( L, git_reference_is_valid_name( name ) ); return 1; }
/* * call-seq: * Reference.valid_name?(ref_name) -> true or false * * Check if a reference name is well-formed. * * Valid reference names must follow one of two patterns: * * 1. Top-level names must contain only capital letters and underscores, * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD"). * 2. Names prefixed with "refs/" can be almost anything. You must avoid * the characters '~', '^', ':', '\\', '?', '[', and '*', and the * sequences ".." and "@{" which have special meaning to revparse. * * Returns true if the reference name is valid, false if not. */ static VALUE rb_git_ref_valid_name(VALUE klass, VALUE rb_name) { Check_Type(rb_name, T_STRING); return git_reference_is_valid_name(StringValueCStr(rb_name)) == 1 ? Qtrue : Qfalse; }