/* Update gitfile and core.worktree setting to connect work tree and git dir */ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir) { struct strbuf file_name = STRBUF_INIT; struct strbuf rel_path = STRBUF_INIT; const char *real_work_tree = xstrdup(real_path(work_tree)); FILE *fp; /* Update gitfile */ strbuf_addf(&file_name, "%s/.git", work_tree); fp = fopen(file_name.buf, "w"); if (!fp) die(_("Could not create git link %s"), file_name.buf); fprintf(fp, "gitdir: %s\n", relative_path(git_dir, real_work_tree, &rel_path)); fclose(fp); /* Update core.worktree setting */ strbuf_reset(&file_name); strbuf_addf(&file_name, "%s/config", git_dir); if (git_config_set_in_file(file_name.buf, "core.worktree", relative_path(real_work_tree, git_dir, &rel_path))) die(_("Could not set core.worktree in %s"), file_name.buf); strbuf_release(&file_name); strbuf_release(&rel_path); free((void *)real_work_tree); }
static char *pick_symlink_target(const char *symlink_path) { struct dir_info *dir; struct dir_entry_info *entry; size_t r; char *path, *rel_path; dir = pick_dir(); if (tests_random_no(100) < 10) return dir_path(dir, make_name(dir)); r = tests_random_no(dir->number_of_entries); entry = dir->first; while (entry && r) { entry = entry->next; --r; } if (!entry) entry = dir->first; if (!entry) return dir_path(dir, make_name(dir)); path = dir_path(dir, entry->name); if (tests_random_no(20) < 10) return path; rel_path = relative_path(symlink_path, path); free(path); return rel_path; }
static int is_usable_file( filelist_t *filelist, const char *node, const char *filename ) { if(!node) return 0; struct stat l; veejay_memset(&l,0,sizeof(struct stat)); if( lstat( node, &l) < 0 ) return 0; if( S_ISLNK( l.st_mode )) { veejay_memset(&l,0,sizeof(struct stat)); stat(node, &l); return 1; } if( S_ISDIR( l.st_mode ) ) { return 1; } if( S_ISREG( l.st_mode ) ) { if( is_it_usable(node)) { if( filelist->num_files < filelist->max_files ) { filelist->files[ filelist->num_files ] = relative_path(filelist,node); filelist->num_files ++; } } } return 0; }
static int resolve_relative_path(int argc, const char **argv, const char *prefix) { struct strbuf sb = STRBUF_INIT; if (argc != 3) die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc); printf("%s", relative_path(argv[1], argv[2], &sb)); strbuf_release(&sb); return 0; }
void write_name_quoted_relative(const char *name, const char *prefix, FILE *fp, int terminator) { struct strbuf sb = STRBUF_INIT; name = relative_path(name, prefix, &sb); write_name_quoted(name, fp, terminator); strbuf_release(&sb); }
/* quote path as relative to the given prefix */ char *quote_path_relative(const char *in, const char *prefix, struct strbuf *out) { struct strbuf sb = STRBUF_INIT; const char *rel = relative_path(in, prefix, &sb); strbuf_reset(out); quote_c_style_counted(rel, strlen(rel), out, NULL, 0); strbuf_release(&sb); return out->buf; }
WrapperStr UmlPackage::source_path(const WrapperStr & f, WrapperStr relto) { if (!dir.read) { dir.src = cppSrcDir(); dir.h = cppHDir(); dir.src_absolute = dir.h_absolute = FALSE; QDir d_root(rootDir()); if (dir.src.isEmpty()) // considered given relative dir.src = RootDir; else if (QDir::isRelativePath(dir.src)) dir.src = d_root.filePath(dir.src); else dir.src_absolute = TRUE; if (dir.h.isEmpty()) // considered given relative dir.h = RootDir; else if (QDir::isRelativePath(dir.h)) dir.h = d_root.filePath(dir.h); else dir.h_absolute = TRUE; if (dir.src.isEmpty()) { UmlCom::trace(WrapperStr("<font color=\"red\"><b><b> The generation directory " "must be specified for the package<i> ") + name() + "</i>, edit the <i> generation settings</i> (tab 'directory') " "or edit the package (tab 'C++')</b></font><br>"); UmlCom::bye(n_errors() + 1); UmlCom::fatal_error("UmlPackage::source_path"); } dir.read = TRUE; } if (f.isEmpty()) return dir.src; QDir d(dir.src); if (! d.exists()) create_directory(dir.src); // don't return on error QByteArray temp = d.filePath(f).toLatin1(); WrapperStr df = (dir.src_absolute || relto.isEmpty()) ? WrapperStr(temp.constData()) : relative_path(d, relto) + f; return df + WrapperStr(".") + CppSettings::sourceExtension(); }
/* Update gitfile and core.worktree setting to connect work tree and git dir */ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir) { struct strbuf file_name = STRBUF_INIT; struct strbuf rel_path = STRBUF_INIT; const char *real_work_tree = xstrdup(real_path(work_tree)); /* Update gitfile */ strbuf_addf(&file_name, "%s/.git", work_tree); write_file(file_name.buf, "gitdir: %s", relative_path(git_dir, real_work_tree, &rel_path)); /* Update core.worktree setting */ strbuf_reset(&file_name); strbuf_addf(&file_name, "%s/config", git_dir); git_config_set_in_file(file_name.buf, "core.worktree", relative_path(real_work_tree, git_dir, &rel_path)); strbuf_release(&file_name); strbuf_release(&rel_path); free((void *)real_work_tree); }
static void path(parser_context *context) { enter_state(context, ST_START); skip_ws(context); absolute_path(context); if(ERR_UNEXPECTED_VALUE == context->result.code && '$' == context->result.expected_char) { enter_state(context, ST_START); context->current_step_kind = SINGLE; relative_path(context); } }
void cOverworld_Manager :: Load_Dir( const std::string &dir, bool user_dir /* = 0 */ ) { // set world directory vector<std::string> subdirs = Get_Directory_Files( dir, "", 1, 0 ); for( vector<std::string>::iterator curdir = subdirs.begin(); curdir != subdirs.end(); ++curdir ) { try { std::string current_dir = *curdir; // only directories with an existing description if( File_Exists( current_dir + "/description.xml" ) ) { cOverworld *overworld = Get_from_Path( current_dir ); // already available if( overworld ) { overworld->m_description->m_user = 2; continue; } overworld = new cOverworld(); // get relative path std::string relative_path( current_dir, current_dir.rfind( '/' ) ); // set relative path overworld->m_description->m_path = relative_path; // default name is the path overworld->m_description->m_name = relative_path; // set user overworld->m_description->m_user = user_dir; objects.push_back( overworld ); overworld->Load(); } } catch( const std::exception &ex ) { printf( "%s %s\n", curdir->c_str(), ex.what() ); } } }
void window_server_t::push_back(const char* name, size_enum_t dialog_size) { boost::filesystem::path relative_path(window_list_m.back()->path_m.branch_path() / name); iterator window(window_list_m.insert(window_list_m.end(), NULL)); boost::filesystem::path file_name; try { file_name = find_resource(name); } catch (...) { file_name = relative_path; } boost::filesystem::ifstream stream(file_name); /* Update before attaching the window so that we can correctly capture contributing for reset. */ sheet_m.update(); window_list_m.back() = make_view( name_t(file_name.string().c_str()), line_position_t::getline_proc_t(), stream, sheet_m, behavior_m, boost::bind(&window_server_t::dispatch_window_action, boost::ref(*this), window, _1, _2), dialog_size, default_widget_factory_proc_with_factory(widget_factory_m)).release(); sheet_m.update(); // Force values to their correct states. window_list_m.back()->path_m = file_name; window_list_m.back()->eve_m.evaluate(eve_t::evaluate_nested); window_list_m.back()->show_window_m(); }
static void qualified_path(parser_context *context) { enter_state(context, ST_QUALIFIED_PATH); skip_ws(context); abbreviated_relative_path(context); skip_ws(context); if(JSONPATH_SUCCESS == context->result.code || !has_more_input(context)) { return; } else if('.' == get_char(context)) { consume_char(context); context->current_step_kind = SINGLE; relative_path(context); } else { unexpected_value(context, '.'); } }
WrapperStr UmlPackage::header_path(const WrapperStr & f, WrapperStr relto) { if (!dir.read) { source_path(f); if (dir.h.isEmpty()) { UmlCom::trace(WrapperStr("<font color=\"red\"><b><b> The generation directory " "must be specified for the package<i> ") + name() + "</i>, edit the <i> generation settings</i> (tab 'directory') " "or edit the package (tab 'C++')</b></font><br>"); UmlCom::bye(n_errors() + 1); UmlCom::fatal_error("UmlPackage::source_path"); } if (QDir::isRelativePath(dir.h)) { UmlCom::trace(WrapperStr("<font color=\"red\"><b><i>") + name() + "</i>'s header path <i>(" + dir.h + "</i>) is not absolute, edit the <i> generation settings</i> " "(tab 'directory'), or edit the package (tab 'C++')</b></font><br>"); UmlCom::bye(n_errors() + 1); UmlCom::fatal_error("UmlPackage::source_path"); } } if (f.isEmpty()) return dir.h; QDir d(dir.h); if (! d.exists()) create_directory(dir.h); // don't return on error QByteArray temp = d.filePath(f).toLatin1(); WrapperStr df = (dir.h_absolute || relto.isEmpty()) ? WrapperStr(temp.constData()) : relative_path(d, relto) + f; return df + WrapperStr(".") + CppSettings::headerExtension(); }
/* Test creating paths */ void createPaths() { ::core::Path empty_path; equal(empty_path.toString(), "/"); isTrue(empty_path.absolute()); ::core::Path ordinary_path("/an/ordinary/path"); equal(ordinary_path.toString(), "/an/ordinary/path"); isTrue(ordinary_path.absolute()); ::core::Path extra_separators("/path//with///extra/separators//"); equal(extra_separators.toString(), "/path/with/extra/separators"); isTrue(extra_separators.absolute()); ::core::Path with_dot("/a/path/with/./dot"); equal(with_dot.toString(), "/a/path/with/dot"); isTrue(with_dot.absolute()); ::core::Path with_dots("/a/path/with/../dots"); equal(with_dots.toString(), "/a/path/dots"); isTrue(with_dots.absolute()); ::core::Path relative_path("relative/path"); equal(relative_path.toString(), "relative/path"); isFalse(relative_path.absolute()); relative_path.set("./new/path/"); equal(relative_path.toString(), "new/path"); isFalse(relative_path.absolute()); ::core::Path path_start_dots("../starts/with/relative"); equal(path_start_dots.toString(), "../starts/with/relative"); isFalse(path_start_dots.absolute()); ::core::Path multiple_dots("./../starts/./with/../two/dots"); equal(multiple_dots.toString(), "../starts/two/dots"); isFalse(multiple_dots.absolute()); }
void setup_work_tree(void) { const char *work_tree, *git_dir; static int initialized = 0; if (initialized) return; work_tree = get_git_work_tree(); git_dir = get_git_dir(); if (!is_absolute_path(git_dir)) git_dir = real_path(get_git_dir()); if (!work_tree || chdir(work_tree)) die("This operation must be run in a work tree"); /* * Make sure subsequent git processes find correct worktree * if $GIT_WORK_TREE is set relative */ if (getenv(GIT_WORK_TREE_ENVIRONMENT)) setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); set_git_dir(relative_path(git_dir, work_tree)); initialized = 1; }
static void abbreviated_relative_path(parser_context *context) { enter_state(context, ST_ABBREVIATED_RELATIVE_PATH); size_t mark = context->cursor; skip_ws(context); if('.' != get_char(context)) { unexpected_value(context, '.'); return; } consume_char(context); skip_ws(context); if('.' != get_char(context)) { unexpected_value(context, '.'); reset(context, mark); return; } consume_char(context); context->current_step_kind = RECURSIVE; relative_path(context); }
static void init_submodule(const char *path, const char *prefix, int quiet) { const struct submodule *sub; struct strbuf sb = STRBUF_INIT; char *upd = NULL, *url = NULL, *displaypath; /* Only loads from .gitmodules, no overlay with .git/config */ gitmodules_config(); if (prefix && get_super_prefix()) die("BUG: cannot have prefix and superprefix"); else if (prefix) displaypath = xstrdup(relative_path(path, prefix, &sb)); else if (get_super_prefix()) { strbuf_addf(&sb, "%s%s", get_super_prefix(), path); displaypath = strbuf_detach(&sb, NULL); } else displaypath = xstrdup(path); sub = submodule_from_path(null_sha1, path); if (!sub) die(_("No url found for submodule path '%s' in .gitmodules"), displaypath); /* * NEEDSWORK: In a multi-working-tree world, this needs to be * set in the per-worktree config. * * Set active flag for the submodule being initialized */ if (!is_submodule_initialized(path)) { strbuf_reset(&sb); strbuf_addf(&sb, "submodule.%s.active", sub->name); git_config_set_gently(sb.buf, "true"); } /* * Copy url setting when it is not set yet. * To look up the url in .git/config, we must not fall back to * .gitmodules, so look it up directly. */ strbuf_reset(&sb); strbuf_addf(&sb, "submodule.%s.url", sub->name); if (git_config_get_string(sb.buf, &url)) { url = xstrdup(sub->url); if (!url) die(_("No url found for submodule path '%s' in .gitmodules"), displaypath); /* Possibly a url relative to parent */ if (starts_with_dot_dot_slash(url) || starts_with_dot_slash(url)) { char *remoteurl, *relurl; char *remote = get_default_remote(); struct strbuf remotesb = STRBUF_INIT; strbuf_addf(&remotesb, "remote.%s.url", remote); free(remote); if (git_config_get_string(remotesb.buf, &remoteurl)) { warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf); remoteurl = xgetcwd(); } relurl = relative_url(remoteurl, url, NULL); strbuf_release(&remotesb); free(remoteurl); free(url); url = relurl; } if (git_config_set_gently(sb.buf, url)) die(_("Failed to register url for submodule path '%s'"), displaypath); if (!quiet) fprintf(stderr, _("Submodule '%s' (%s) registered for path '%s'\n"), sub->name, url, displaypath); } /* Copy "update" setting when it is not set yet */ strbuf_reset(&sb); strbuf_addf(&sb, "submodule.%s.update", sub->name); if (git_config_get_string(sb.buf, &upd) && sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { if (sub->update_strategy.type == SM_UPDATE_COMMAND) { fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"), sub->name); upd = xstrdup("none"); } else upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy)); if (git_config_set_gently(sb.buf, upd)) die(_("Failed to register update mode for submodule path '%s'"), displaypath); } strbuf_release(&sb); free(displaypath); free(url); free(upd); }
int main(int argc, char **argv) { if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) { char *buf = xmalloc(PATH_MAX + 1); int rv = normalize_path_copy(buf, argv[2]); if (rv) buf = "++failed++"; puts(buf); return 0; } if (argc >= 2 && !strcmp(argv[1], "real_path")) { while (argc > 2) { puts(real_path(argv[2])); argc--; argv++; } return 0; } if (argc >= 2 && !strcmp(argv[1], "absolute_path")) { while (argc > 2) { puts(absolute_path(argv[2])); argc--; argv++; } return 0; } if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) { int len; struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; char *path = xstrdup(argv[2]); /* * We have to normalize the arguments because under * Windows, bash mangles arguments that look like * absolute POSIX paths or colon-separate lists of * absolute POSIX paths into DOS paths (e.g., * "/foo:/foo/bar" might be converted to * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"), * whereas longest_ancestor_length() requires paths * that use forward slashes. */ if (normalize_path_copy(path, path)) die("Path \"%s\" could not be normalized", argv[2]); string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1); filter_string_list(&ceiling_dirs, 0, normalize_ceiling_entry, NULL); len = longest_ancestor_length(path, &ceiling_dirs); string_list_clear(&ceiling_dirs, 0); free(path); printf("%d\n", len); return 0; } if (argc >= 4 && !strcmp(argv[1], "prefix_path")) { char *prefix = argv[2]; int prefix_len = strlen(prefix); int nongit_ok; setup_git_directory_gently(&nongit_ok); while (argc > 3) { puts(prefix_path(prefix, prefix_len, argv[3])); argc--; argv++; } return 0; } if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) { char *prefix = strip_path_suffix(argv[2], argv[3]); printf("%s\n", prefix ? prefix : "(null)"); return 0; } if (argc == 3 && !strcmp(argv[1], "mingw_path")) { puts(argv[2]); return 0; } if (argc == 4 && !strcmp(argv[1], "relative_path")) { struct strbuf sb = STRBUF_INIT; const char *in, *prefix, *rel; normalize_argv_string(&in, argv[2]); normalize_argv_string(&prefix, argv[3]); rel = relative_path(in, prefix, &sb); if (!rel) puts("(null)"); else puts(strlen(rel) > 0 ? rel : "(empty)"); strbuf_release(&sb); return 0; } fprintf(stderr, "%s: unknown function name: %s\n", argv[0], argv[1] ? argv[1] : "(there was none)"); return 1; }
Q3CString UmlPackage::file_path(const Q3CString & f, Q3CString relto) { if (!dir.read) { dir.file = phpDir(); dir.file_absolute = FALSE; QDir d_root(rootDir()); if (dir.file.isEmpty()) dir.file = RootDir; else if (QDir::isRelativePath(dir.file)) dir.file = d_root.filePath(dir.file); else dir.file_absolute = TRUE; if (dir.file.isEmpty()) { UmlCom::trace(Q3CString("<font color=\"red\"><b><b> The generation directory " "must be specified for the package<i> ") + name() + "</i>, edit the <i> generation settings</i> (tab 'directory') " "or edit the package (tab 'Php')</b></font><br>"); UmlCom::bye(n_errors() + 1); UmlCom::fatal_error("UmlPackage::file_path"); } dir.read = TRUE; } if (f.isEmpty()) return dir.file; QDir d(dir.file); if (! d.exists()) { // create directory including the intermediates Q3CString s = dir.file; int index = 0; QChar sep = QDir::separator(); if (sep != '/') { while ((index = s.find(sep, index)) != -1) s.replace(index++, 1, "/"); } s = QDir::cleanDirPath(s) + "/"; index = s.find("/"); int index2; while ((index2 = s.find("/", index + 1)) != -1) { Q3CString s2 = s.left(index2); QDir sd(s2); if (!sd.exists()) { if (!sd.mkdir(s2)) { UmlCom::trace(Q3CString("<font color=\"red\"><b> cannot create directory <i>") + s2 + "</i></b></font><br>"); UmlCom::bye(n_errors() + 1); UmlCom::fatal_error("UmlPackage::file_path"); } } index = index2; } } Q3CString df = (dir.file_absolute || relto.isEmpty()) ? Q3CString(d.filePath(f).toAscii().constData()) : relative_path(d, relto) + f; if (PhpSettings::isRelativePath() && (df[0] != '/') && (df[0] != '.')) df = "./" + df; return df + Q3CString(".") + PhpSettings::sourceExtension(); }
/** * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise. */ static int prepare_to_clone_next_submodule(const struct cache_entry *ce, struct child_process *child, struct submodule_update_clone *suc, struct strbuf *out) { const struct submodule *sub = NULL; struct strbuf displaypath_sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT; const char *displaypath = NULL; char *url = NULL; int needs_cloning = 0; if (ce_stage(ce)) { if (suc->recursive_prefix) strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name); else strbuf_addf(&sb, "%s", ce->name); strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf); strbuf_addch(out, '\n'); goto cleanup; } sub = submodule_from_path(null_sha1, ce->name); if (suc->recursive_prefix) displaypath = relative_path(suc->recursive_prefix, ce->name, &displaypath_sb); else displaypath = ce->name; if (!sub) { next_submodule_warn_missing(suc, out, displaypath); goto cleanup; } if (suc->update.type == SM_UPDATE_NONE || (suc->update.type == SM_UPDATE_UNSPECIFIED && sub->update_strategy.type == SM_UPDATE_NONE)) { strbuf_addf(out, _("Skipping submodule '%s'"), displaypath); strbuf_addch(out, '\n'); goto cleanup; } /* * Looking up the url in .git/config. * We must not fall back to .gitmodules as we only want * to process configured submodules. */ strbuf_reset(&sb); strbuf_addf(&sb, "submodule.%s.url", sub->name); git_config_get_string(sb.buf, &url); if (!url) { next_submodule_warn_missing(suc, out, displaypath); goto cleanup; } strbuf_reset(&sb); strbuf_addf(&sb, "%s/.git", ce->name); needs_cloning = !file_exists(sb.buf); strbuf_reset(&sb); strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce), needs_cloning, ce->name); string_list_append(&suc->projectlines, sb.buf); if (!needs_cloning) goto cleanup; child->git_cmd = 1; child->no_stdin = 1; child->stdout_to_stderr = 1; child->err = -1; argv_array_push(&child->args, "submodule--helper"); argv_array_push(&child->args, "clone"); if (suc->quiet) argv_array_push(&child->args, "--quiet"); if (suc->prefix) argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL); if (suc->recommend_shallow && sub->recommend_shallow == 1) argv_array_push(&child->args, "--depth=1"); argv_array_pushl(&child->args, "--path", sub->path, NULL); argv_array_pushl(&child->args, "--name", sub->name, NULL); argv_array_pushl(&child->args, "--url", url, NULL); if (suc->reference) argv_array_push(&child->args, suc->reference); if (suc->depth) argv_array_push(&child->args, suc->depth); cleanup: free(url); strbuf_reset(&displaypath_sb); strbuf_reset(&sb); return needs_cloning; }
int cmd_rev_parse(int argc, const char **argv, const char *prefix) { int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; int did_repo_setup = 0; int has_dashdash = 0; int output_prefix = 0; struct object_id oid; unsigned int flags = 0; const char *name = NULL; struct object_context unused; struct strbuf buf = STRBUF_INIT; if (argc > 1 && !strcmp("--parseopt", argv[1])) return cmd_parseopt(argc - 1, argv + 1, prefix); if (argc > 1 && !strcmp("--sq-quote", argv[1])) return cmd_sq_quote(argc - 2, argv + 2); if (argc > 1 && !strcmp("-h", argv[1])) usage(builtin_rev_parse_usage); for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "--")) { has_dashdash = 1; break; } } /* No options; just report on whether we're in a git repo or not. */ if (argc == 1) { setup_git_directory(); git_config(git_default_config, NULL); return 0; } for (i = 1; i < argc; i++) { const char *arg = argv[i]; if (!strcmp(arg, "--local-env-vars")) { int i; for (i = 0; local_repo_env[i]; i++) printf("%s\n", local_repo_env[i]); continue; } if (!strcmp(arg, "--resolve-git-dir")) { const char *gitdir = argv[++i]; if (!gitdir) die("--resolve-git-dir requires an argument"); gitdir = resolve_gitdir(gitdir); if (!gitdir) die("not a gitdir '%s'", argv[i]); puts(gitdir); continue; } /* The rest of the options require a git repository. */ if (!did_repo_setup) { prefix = setup_git_directory(); git_config(git_default_config, NULL); did_repo_setup = 1; } if (!strcmp(arg, "--git-path")) { if (!argv[i + 1]) die("--git-path requires an argument"); strbuf_reset(&buf); puts(relative_path(git_path("%s", argv[i + 1]), prefix, &buf)); i++; continue; } if (as_is) { if (show_file(arg, output_prefix) && as_is < 2) verify_filename(prefix, arg, 0); continue; } if (!strcmp(arg,"-n")) { if (++i >= argc) die("-n requires an argument"); if ((filter & DO_FLAGS) && (filter & DO_REVS)) { show(arg); show(argv[i]); } continue; } if (starts_with(arg, "-n")) { if ((filter & DO_FLAGS) && (filter & DO_REVS)) show(arg); continue; } if (*arg == '-') { if (!strcmp(arg, "--")) { as_is = 2; /* Pass on the "--" if we show anything but files.. */ if (filter & (DO_FLAGS | DO_REVS)) show_file(arg, 0); continue; } if (!strcmp(arg, "--default")) { def = argv[++i]; if (!def) die("--default requires an argument"); continue; } if (!strcmp(arg, "--prefix")) { prefix = argv[++i]; if (!prefix) die("--prefix requires an argument"); startup_info->prefix = prefix; output_prefix = 1; continue; } if (!strcmp(arg, "--revs-only")) { filter &= ~DO_NOREV; continue; } if (!strcmp(arg, "--no-revs")) { filter &= ~DO_REVS; continue; } if (!strcmp(arg, "--flags")) { filter &= ~DO_NONFLAGS; continue; } if (!strcmp(arg, "--no-flags")) { filter &= ~DO_FLAGS; continue; } if (!strcmp(arg, "--verify")) { filter &= ~(DO_FLAGS|DO_NOREV); verify = 1; continue; } if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { quiet = 1; flags |= GET_OID_QUIETLY; continue; } if (opt_with_value(arg, "--short", &arg)) { filter &= ~(DO_FLAGS|DO_NOREV); verify = 1; abbrev = DEFAULT_ABBREV; if (!arg) continue; abbrev = strtoul(arg, NULL, 10); if (abbrev < MINIMUM_ABBREV) abbrev = MINIMUM_ABBREV; else if (40 <= abbrev) abbrev = 40; continue; } if (!strcmp(arg, "--sq")) { output_sq = 1; continue; } if (!strcmp(arg, "--not")) { show_type ^= REVERSED; continue; } if (!strcmp(arg, "--symbolic")) { symbolic = SHOW_SYMBOLIC_ASIS; continue; } if (!strcmp(arg, "--symbolic-full-name")) { symbolic = SHOW_SYMBOLIC_FULL; continue; } if (opt_with_value(arg, "--abbrev-ref", &arg)) { abbrev_ref = 1; abbrev_ref_strict = warn_ambiguous_refs; if (arg) { if (!strcmp(arg, "strict")) abbrev_ref_strict = 1; else if (!strcmp(arg, "loose")) abbrev_ref_strict = 0; else die("unknown mode for --abbrev-ref: %s", arg); } continue; } if (!strcmp(arg, "--all")) { for_each_ref(show_reference, NULL); continue; } if (skip_prefix(arg, "--disambiguate=", &arg)) { for_each_abbrev(arg, show_abbrev, NULL); continue; } if (!strcmp(arg, "--bisect")) { for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0); for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0); continue; } if (opt_with_value(arg, "--branches", &arg)) { handle_ref_opt(arg, "refs/heads/"); continue; } if (opt_with_value(arg, "--tags", &arg)) { handle_ref_opt(arg, "refs/tags/"); continue; } if (skip_prefix(arg, "--glob=", &arg)) { handle_ref_opt(arg, NULL); continue; } if (opt_with_value(arg, "--remotes", &arg)) { handle_ref_opt(arg, "refs/remotes/"); continue; } if (skip_prefix(arg, "--exclude=", &arg)) { add_ref_exclusion(&ref_excludes, arg); continue; } if (!strcmp(arg, "--show-toplevel")) { const char *work_tree = get_git_work_tree(); if (work_tree) puts(work_tree); continue; } if (!strcmp(arg, "--show-superproject-working-tree")) { const char *superproject = get_superproject_working_tree(); if (superproject) puts(superproject); continue; } if (!strcmp(arg, "--show-prefix")) { if (prefix) puts(prefix); else putchar('\n'); continue; } if (!strcmp(arg, "--show-cdup")) { const char *pfx = prefix; if (!is_inside_work_tree()) { const char *work_tree = get_git_work_tree(); if (work_tree) printf("%s\n", work_tree); continue; } while (pfx) { pfx = strchr(pfx, '/'); if (pfx) { pfx++; printf("../"); } } putchar('\n'); continue; } if (!strcmp(arg, "--git-dir") || !strcmp(arg, "--absolute-git-dir")) { const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); char *cwd; int len; if (arg[2] == 'g') { /* --git-dir */ if (gitdir) { puts(gitdir); continue; } if (!prefix) { puts(".git"); continue; } } else { /* --absolute-git-dir */ if (!gitdir && !prefix) gitdir = ".git"; if (gitdir) { puts(real_path(gitdir)); continue; } } cwd = xgetcwd(); len = strlen(cwd); printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : ""); free(cwd); continue; } if (!strcmp(arg, "--git-common-dir")) { strbuf_reset(&buf); puts(relative_path(get_git_common_dir(), prefix, &buf)); continue; } if (!strcmp(arg, "--is-inside-git-dir")) { printf("%s\n", is_inside_git_dir() ? "true" : "false"); continue; } if (!strcmp(arg, "--is-inside-work-tree")) { printf("%s\n", is_inside_work_tree() ? "true" : "false"); continue; } if (!strcmp(arg, "--is-bare-repository")) { printf("%s\n", is_bare_repository() ? "true" : "false"); continue; } if (!strcmp(arg, "--is-shallow-repository")) { printf("%s\n", is_repository_shallow() ? "true" : "false"); continue; } if (!strcmp(arg, "--shared-index-path")) { if (read_cache() < 0) die(_("Could not read the index")); if (the_index.split_index) { const unsigned char *sha1 = the_index.split_index->base_sha1; const char *path = git_path("sharedindex.%s", sha1_to_hex(sha1)); strbuf_reset(&buf); puts(relative_path(path, prefix, &buf)); } continue; } if (skip_prefix(arg, "--since=", &arg)) { show_datestring("--max-age=", arg); continue; } if (skip_prefix(arg, "--after=", &arg)) { show_datestring("--max-age=", arg); continue; } if (skip_prefix(arg, "--before=", &arg)) { show_datestring("--min-age=", arg); continue; } if (skip_prefix(arg, "--until=", &arg)) { show_datestring("--min-age=", arg); continue; } if (show_flag(arg) && verify) die_no_single_rev(quiet); continue; } /* Not a flag argument */ if (try_difference(arg)) continue; if (try_parent_shorthands(arg)) continue; name = arg; type = NORMAL; if (*arg == '^') { name++; type = REVERSED; } if (!get_oid_with_context(name, flags, &oid, &unused)) { if (verify) revs_count++; else show_rev(type, &oid, name); continue; } if (verify) die_no_single_rev(quiet); if (has_dashdash) die("bad revision '%s'", arg); as_is = 1; if (!show_file(arg, output_prefix)) continue; verify_filename(prefix, arg, 1); } strbuf_release(&buf); if (verify) { if (revs_count == 1) { show_rev(type, &oid, name); return 0; } else if (revs_count == 0 && show_default()) return 0; die_no_single_rev(quiet); } else show_default(); return 0; }
bool has_relative_path() const { return !relative_path().empty(); }
/** * \brief write tile data to disk * * writes the content of mapcache_tile::data to disk. * \returns MAPCACHE_FAILURE if there is no data to write, or if the tile isn't locked * \returns MAPCACHE_SUCCESS if the tile has been successfully written to disk * \private \memberof mapcache_cache_disk * \sa mapcache_cache::tile_set() */ static void _mapcache_cache_disk_set(mapcache_context *ctx, mapcache_cache *pcache, mapcache_tile *tile) { apr_size_t bytes; apr_file_t *f; apr_status_t ret; char errmsg[120]; char *filename, *hackptr1, *hackptr2=NULL; mapcache_cache_disk *cache = (mapcache_cache_disk*)pcache; const int creation_retry = cache->creation_retry; int retry_count_create_file = 0; #ifdef DEBUG /* all this should be checked at a higher level */ if(!tile->encoded_data && !tile->raw_image) { ctx->set_error(ctx,500,"attempting to write empty tile to disk"); return; } if(!tile->encoded_data && !tile->tileset->format) { ctx->set_error(ctx,500,"received a raw tile image for a tileset with no format"); return; } #endif cache->tile_key(ctx, cache, tile, &filename); GC_CHECK_ERROR(ctx); /* find the location of the last '/' in the string */ hackptr1 = filename; while(*hackptr1) { if(*hackptr1 == '/') hackptr2 = hackptr1; hackptr1++; } *hackptr2 = '\0'; if(APR_SUCCESS != (ret = apr_dir_make_recursive(filename,APR_OS_DEFAULT,ctx->pool))) { /* * apr_dir_make_recursive sometimes sends back this error, although it should not. * ignore this one */ if(!APR_STATUS_IS_EEXIST(ret)) { ctx->set_error(ctx, 500, "failed to create directory %s: %s",filename, apr_strerror(ret,errmsg,120)); return; } } *hackptr2 = '/'; ret = apr_file_remove(filename,ctx->pool); if(ret != APR_SUCCESS && !APR_STATUS_IS_ENOENT(ret)) { ctx->set_error(ctx, 500, "failed to remove file %s: %s",filename, apr_strerror(ret,errmsg,120)); } #ifdef HAVE_SYMLINK if(cache->symlink_blank) { if(!tile->raw_image) { tile->raw_image = mapcache_imageio_decode(ctx, tile->encoded_data); GC_CHECK_ERROR(ctx); } if(mapcache_image_blank_color(tile->raw_image) != MAPCACHE_FALSE) { char *blankname; int retry_count_create_symlink = 0; char *blankname_rel = NULL; _mapcache_cache_disk_blank_tile_key(ctx,cache,tile,tile->raw_image->data,&blankname); if(apr_file_open(&f, blankname, APR_FOPEN_READ, APR_OS_DEFAULT, ctx->pool) != APR_SUCCESS) { int isLocked; void *lock; char *blankdirname; if(!tile->encoded_data) { tile->encoded_data = tile->tileset->format->write(ctx, tile->raw_image, tile->tileset->format); GC_CHECK_ERROR(ctx); } /* create the blank file */ blankdirname = apr_psprintf(ctx->pool, "%s/%s/%s/blanks", cache->base_directory, tile->tileset->name, tile->grid_link->grid->name); if(APR_SUCCESS != (ret = apr_dir_make_recursive( blankdirname, APR_OS_DEFAULT,ctx->pool))) { if(!APR_STATUS_IS_EEXIST(ret)) { ctx->set_error(ctx, 500, "failed to create directory %s for blank tiles",blankdirname, apr_strerror(ret,errmsg,120)); return; } } /* aquire a lock on the blank file */ isLocked = mapcache_lock_or_wait_for_resource(ctx,ctx->config->locker,blankname, &lock); if(isLocked == MAPCACHE_TRUE) { if((ret = apr_file_open(&f, blankname, APR_FOPEN_CREATE|APR_FOPEN_WRITE|APR_FOPEN_BUFFERED|APR_FOPEN_BINARY, APR_OS_DEFAULT, ctx->pool)) != APR_SUCCESS) { ctx->set_error(ctx, 500, "failed to create file %s: %s",blankname, apr_strerror(ret,errmsg,120)); mapcache_unlock_resource(ctx,ctx->config->locker,blankname, lock); return; /* we could not create the file */ } bytes = (apr_size_t)tile->encoded_data->size; ret = apr_file_write(f,(void*)tile->encoded_data->buf,&bytes); if(ret != APR_SUCCESS) { ctx->set_error(ctx, 500, "failed to write data to file %s (wrote %d of %d bytes): %s",blankname, (int)bytes, (int)tile->encoded_data->size, apr_strerror(ret,errmsg,120)); mapcache_unlock_resource(ctx,ctx->config->locker,blankname, lock); return; /* we could not create the file */ } if(bytes != tile->encoded_data->size) { ctx->set_error(ctx, 500, "failed to write image data to %s, wrote %d of %d bytes", blankname, (int)bytes, (int)tile->encoded_data->size); mapcache_unlock_resource(ctx,ctx->config->locker,blankname, lock); return; } apr_file_close(f); mapcache_unlock_resource(ctx,ctx->config->locker,blankname, lock); #ifdef DEBUG ctx->log(ctx,MAPCACHE_DEBUG,"created blank tile %s",blankname); #endif } } else { apr_file_close(f); } /* * compute the relative path between tile and blank tile */ blankname_rel = relative_path(ctx,filename, blankname); GC_CHECK_ERROR(ctx); /* * depending on configuration symlink creation will retry if it fails. * this can happen on nfs mounted network storage. * the solution is to create the containing directory again and retry the symlink creation. */ while(symlink(blankname_rel, filename) != 0) { retry_count_create_symlink++; if(retry_count_create_symlink > creation_retry) { char *error = strerror(errno); ctx->set_error(ctx, 500, "failed to link tile %s to %s: %s",filename, blankname_rel, error); return; /* we could not create the file */ } *hackptr2 = '\0'; if(APR_SUCCESS != (ret = apr_dir_make_recursive(filename,APR_OS_DEFAULT,ctx->pool))) { if(!APR_STATUS_IS_EEXIST(ret)) { ctx->set_error(ctx, 500, "failed to create symlink, can not create directory %s: %s",filename, apr_strerror(ret,errmsg,120)); return; /* we could not create the file */ } } *hackptr2 = '/'; } #ifdef DEBUG ctx->log(ctx, MAPCACHE_DEBUG, "linked blank tile %s to %s",filename,blankname); #endif return; } } #endif /*HAVE_SYMLINK*/ /* go the normal way: either we haven't configured blank tile detection, or the tile was not blank */ if(!tile->encoded_data) { tile->encoded_data = tile->tileset->format->write(ctx, tile->raw_image, tile->tileset->format); GC_CHECK_ERROR(ctx); } /* * depending on configuration file creation will retry if it fails. * this can happen on nfs mounted network storage. * the solution is to create the containing directory again and retry the file creation. */ while((ret = apr_file_open(&f, filename, APR_FOPEN_CREATE|APR_FOPEN_WRITE|APR_FOPEN_BUFFERED|APR_FOPEN_BINARY, APR_OS_DEFAULT, ctx->pool)) != APR_SUCCESS) { retry_count_create_file++; if(retry_count_create_file > creation_retry) { ctx->set_error(ctx, 500, "failed to create file %s: %s",filename, apr_strerror(ret,errmsg,120)); return; /* we could not create the file */ } *hackptr2 = '\0'; if(APR_SUCCESS != (ret = apr_dir_make_recursive(filename,APR_OS_DEFAULT,ctx->pool))) { if(!APR_STATUS_IS_EEXIST(ret)) { ctx->set_error(ctx, 500, "failed to create file, can not create directory %s: %s",filename, apr_strerror(ret,errmsg,120)); return; /* we could not create the file */ } } *hackptr2 = '/'; } bytes = (apr_size_t)tile->encoded_data->size; ret = apr_file_write(f,(void*)tile->encoded_data->buf,&bytes); if(ret != APR_SUCCESS) { ctx->set_error(ctx, 500, "failed to write data to file %s (wrote %d of %d bytes): %s",filename, (int)bytes, (int)tile->encoded_data->size, apr_strerror(ret,errmsg,120)); return; /* we could not create the file */ } if(bytes != tile->encoded_data->size) { ctx->set_error(ctx, 500, "failed to write image data to %s, wrote %d of %d bytes", filename, (int)bytes, (int)tile->encoded_data->size); } ret = apr_file_close(f); if(ret != APR_SUCCESS) { ctx->set_error(ctx, 500, "failed to close file %s:%s",filename, apr_strerror(ret,errmsg,120)); return; /* we could not create the file */ } }
static int module_clone(int argc, const char **argv, const char *prefix) { const char *path = NULL, *name = NULL, *url = NULL; const char *reference = NULL, *depth = NULL; int quiet = 0; FILE *submodule_dot_git; char *sm_gitdir, *cwd, *p; struct strbuf rel_path = STRBUF_INIT; struct strbuf sb = STRBUF_INIT; struct option module_clone_options[] = { OPT_STRING(0, "prefix", &prefix, N_("path"), N_("alternative anchor for relative paths")), OPT_STRING(0, "path", &path, N_("path"), N_("where the new submodule will be cloned to")), OPT_STRING(0, "name", &name, N_("string"), N_("name of the new submodule")), OPT_STRING(0, "url", &url, N_("string"), N_("url where to clone the submodule from")), OPT_STRING(0, "reference", &reference, N_("string"), N_("reference repository")), OPT_STRING(0, "depth", &depth, N_("string"), N_("depth for shallow clones")), OPT__QUIET(&quiet, "Suppress output for cloning a submodule"), OPT_END() }; const char *const git_submodule_helper_usage[] = { N_("git submodule--helper clone [--prefix=<path>] [--quiet] " "[--reference <repository>] [--name <name>] [--url <url>]" "[--depth <depth>] [--] [<path>...]"), NULL }; argc = parse_options(argc, argv, prefix, module_clone_options, git_submodule_helper_usage, 0); strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name); sm_gitdir = strbuf_detach(&sb, NULL); if (!file_exists(sm_gitdir)) { if (safe_create_leading_directories_const(sm_gitdir) < 0) die(_("could not create directory '%s'"), sm_gitdir); if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet)) die(_("clone of '%s' into submodule path '%s' failed"), url, path); } else { if (safe_create_leading_directories_const(path) < 0) die(_("could not create directory '%s'"), path); strbuf_addf(&sb, "%s/index", sm_gitdir); unlink_or_warn(sb.buf); strbuf_reset(&sb); } /* Write a .git file in the submodule to redirect to the superproject. */ if (safe_create_leading_directories_const(path) < 0) die(_("could not create directory '%s'"), path); if (path && *path) strbuf_addf(&sb, "%s/.git", path); else strbuf_addstr(&sb, ".git"); if (safe_create_leading_directories_const(sb.buf) < 0) die(_("could not create leading directories of '%s'"), sb.buf); submodule_dot_git = fopen(sb.buf, "w"); if (!submodule_dot_git) die_errno(_("cannot open file '%s'"), sb.buf); fprintf(submodule_dot_git, "gitdir: %s\n", relative_path(sm_gitdir, path, &rel_path)); if (fclose(submodule_dot_git)) die(_("could not close file %s"), sb.buf); strbuf_reset(&sb); strbuf_reset(&rel_path); cwd = xgetcwd(); /* Redirect the worktree of the submodule in the superproject's config */ if (!is_absolute_path(sm_gitdir)) { strbuf_addf(&sb, "%s/%s", cwd, sm_gitdir); free(sm_gitdir); sm_gitdir = strbuf_detach(&sb, NULL); } strbuf_addf(&sb, "%s/%s", cwd, path); p = git_pathdup_submodule(path, "config"); if (!p) die(_("could not get submodule directory for '%s'"), path); git_config_set_in_file(p, "core.worktree", relative_path(sb.buf, sm_gitdir, &rel_path)); strbuf_release(&sb); strbuf_release(&rel_path); free(sm_gitdir); free(cwd); free(p); return 0; }
/** * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise. */ static int prepare_to_clone_next_submodule(const struct cache_entry *ce, struct child_process *child, struct submodule_update_clone *suc, struct strbuf *out) { const struct submodule *sub = NULL; struct strbuf displaypath_sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT; const char *displaypath = NULL; int needs_cloning = 0; if (ce_stage(ce)) { if (suc->recursive_prefix) strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name); else strbuf_addstr(&sb, ce->name); strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf); strbuf_addch(out, '\n'); goto cleanup; } sub = submodule_from_path(null_sha1, ce->name); if (suc->recursive_prefix) displaypath = relative_path(suc->recursive_prefix, ce->name, &displaypath_sb); else displaypath = ce->name; if (!sub) { next_submodule_warn_missing(suc, out, displaypath); goto cleanup; } if (suc->update.type == SM_UPDATE_NONE || (suc->update.type == SM_UPDATE_UNSPECIFIED && sub->update_strategy.type == SM_UPDATE_NONE)) { strbuf_addf(out, _("Skipping submodule '%s'"), displaypath); strbuf_addch(out, '\n'); goto cleanup; } /* Check if the submodule has been initialized. */ if (!is_submodule_initialized(ce->name)) { next_submodule_warn_missing(suc, out, displaypath); goto cleanup; } strbuf_reset(&sb); strbuf_addf(&sb, "%s/.git", ce->name); needs_cloning = !file_exists(sb.buf); strbuf_reset(&sb); strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode, oid_to_hex(&ce->oid), ce_stage(ce), needs_cloning, ce->name); string_list_append(&suc->projectlines, sb.buf); if (!needs_cloning) goto cleanup; child->git_cmd = 1; child->no_stdin = 1; child->stdout_to_stderr = 1; child->err = -1; argv_array_push(&child->args, "submodule--helper"); argv_array_push(&child->args, "clone"); if (suc->progress) argv_array_push(&child->args, "--progress"); if (suc->quiet) argv_array_push(&child->args, "--quiet"); if (suc->prefix) argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL); if (suc->recommend_shallow && sub->recommend_shallow == 1) argv_array_push(&child->args, "--depth=1"); argv_array_pushl(&child->args, "--path", sub->path, NULL); argv_array_pushl(&child->args, "--name", sub->name, NULL); argv_array_pushl(&child->args, "--url", sub->url, NULL); if (suc->references.nr) { struct string_list_item *item; for_each_string_list_item(item, &suc->references) argv_array_pushl(&child->args, "--reference", item->string, NULL); } if (suc->depth) argv_array_push(&child->args, suc->depth); cleanup: strbuf_reset(&displaypath_sb); strbuf_reset(&sb); return needs_cloning; }
std::string folder_to_relative_path(const std::string& root, const std::string& folder) { return relative_path(root, folder); }