예제 #1
0
static void
test_absolute_path_relative() {
  setup();

  char *path;

  will_return( mock_access, 0 );

  path = absolute_path( "/tmp", "file" );
  assert_string_equal( path, "/tmp/file" );
  xfree( path );

  will_return( mock_access, 0 );

  path = absolute_path( "/tmp/", "./file" );
  assert_string_equal( path, "/tmp/./file" );
  xfree( path );

  will_return( mock_access, 0 );

  path = absolute_path( "/tmp", "../tmp/file" );
  assert_string_equal( path, "/tmp/../tmp/file" );
  xfree( path );

  will_return( mock_access, 0 );

  path = absolute_path( "/tmp/", "../../tmp/file" );
  assert_string_equal( path, "/tmp/../../tmp/file" );
  xfree( path );

  teardown();
}
예제 #2
0
파일: clone.c 프로젝트: rafaalves/git
static char *get_repo_path(const char *repo, int *is_bundle)
{
	static char *suffix[] = { "/.git", ".git", "" };
	static char *bundle_suffix[] = { ".bundle", "" };
	struct stat st;
	int i;

	for (i = 0; i < ARRAY_SIZE(suffix); i++) {
		const char *path;
		path = mkpath("%s%s", repo, suffix[i]);
		if (is_directory(path)) {
			*is_bundle = 0;
			return xstrdup(absolute_path(path));
		}
	}

	for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
		const char *path;
		path = mkpath("%s%s", repo, bundle_suffix[i]);
		if (!stat(path, &st) && S_ISREG(st.st_mode)) {
			*is_bundle = 1;
			return xstrdup(absolute_path(path));
		}
	}

	return NULL;
}
예제 #3
0
파일: main.c 프로젝트: lasson-g/polysh
int main(int argc, char *argv[])
{
  FILE *f         = stdin;
  char *rc_file   = absolute_path(RC_FILE);
  char *hist_file = absolute_path(HIST_FILE);

  if (argc > 1) {
    /* On travaille sur un shell script: lui passer les arguments et le lancer */
    add_program_parameters_environ(argc-1, argv+1);
    return execute_script(argv[1]);
  }
  else {
    /* Imprimer une bannière */
    fprintf(stderr, "Bienvenue sur %s version %s\n", SHELL_NAME, VERSION);

    /* lancer éventuellement le script d'initialisation de l'utilisateur*/
    if (access(rc_file, R_OK) == 0)
      execute_script(rc_file);

    /* Passer les paramètres (ici 0 param) et appeler le toplevel de l'interprete */
    add_program_parameters_environ(1, argv);

    /* Gérer l'historique (la sauvegarde se fera automatiquement)  */
    init_history(HISTORY_DEFAULT_SIZE, hist_file);

    /* Lancement du toplevel de l'interprète */
    toplevel(f);
    fprintf(stderr, "Bye\n");
  }

  return EXIT_SUCCESS;
}
예제 #4
0
파일: lockfile.c 프로젝트: 2572/git
void unable_to_lock_message(const char *path, int err, struct strbuf *buf)
{
	if (err == EEXIST) {
		strbuf_addf(buf, "Unable to create '%s.lock': %s.\n\n"
		    "If no other git process is currently running, this probably means a\n"
		    "git process crashed in this repository earlier. Make sure no other git\n"
		    "process is running and remove the file manually to continue.",
			    absolute_path(path), strerror(err));
	} else
		strbuf_addf(buf, "Unable to create '%s.lock': %s",
			    absolute_path(path), strerror(err));
}
예제 #5
0
파일: fs.cpp 프로젝트: cquest/mapnik
    std::string make_relative(std::string const& filepath, std::string const& base)
    {
#ifdef _WINDOWS
        boost::filesystem::path absolute_path(mapnik::utf8_to_utf16(base));
#else
        boost::filesystem::path absolute_path(base);
#endif
        // support symlinks
        if (boost::filesystem::is_symlink(absolute_path))
        {
            absolute_path = boost::filesystem::read_symlink(absolute_path);
        }
        return boost::filesystem::absolute(absolute_path.parent_path() / filepath).string();
    }
예제 #6
0
파일: main.c 프로젝트: dimatura/sxiv
void check_add_file(char *filename) {
	const char *bn;

	if (filename == NULL || *filename == '\0')
		return;

	if (access(filename, R_OK) < 0) {
		warn("could not open file: %s", filename);
		return;
	}

	if (fileidx == filecnt) {
		filecnt *= 2;
		files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
	}
	if (*filename != '/') {
		files[fileidx].path = absolute_path(filename);
		if (files[fileidx].path == NULL) {
			warn("could not get absolute path of file: %s\n", filename);
			return;
		}
	}
	files[fileidx].loaded = false;
	files[fileidx].name = s_strdup(filename);
	if (*filename == '/')
		files[fileidx].path = files[fileidx].name;
	if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
		files[fileidx].base = ++bn;
	else
		files[fileidx].base = files[fileidx].name;
	fileidx++;
}
예제 #7
0
파일: worktree.c 프로젝트: 9b/git
static void mark_current_worktree(struct worktree **worktrees)
{
	char *git_dir = xstrdup(absolute_path(get_git_dir()));
	int i;

	for (i = 0; worktrees[i]; i++) {
		struct worktree *wt = worktrees[i];
		const char *wt_git_dir = get_worktree_git_dir(wt);

		if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
			wt->is_current = 1;
			break;
		}
	}
	free(git_dir);
}
예제 #8
0
    LIBUPCOREAPI UPALLOC UPWARNRESULT
    char* absolute_path(char const* p, char const* base) noexcept {
        wchar_t* native_p = nullptr, * native_base = nullptr, * native_result = nullptr;
        char* retval = nullptr;

        native_p = transcode(p);
        if (!native_p) {
            goto error;
        }

        if (base) {
            native_base = transcode(base);
            if (!native_base) {
                goto error;
            }
        }

        native_result = absolute_path(native_p, native_base);
        if (native_result) {
            retval = transcode(native_result);
        }

    error:

        free(native_result);
        free(native_base);
        free(native_p);
        return retval;
    }
예제 #9
0
파일: clone.c 프로젝트: IAmAnubhavSaini/git
static char *get_repo_path(const char *repo, int *is_bundle)
{
	static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
	static char *bundle_suffix[] = { ".bundle", "" };
	struct stat st;
	int i;

	for (i = 0; i < ARRAY_SIZE(suffix); i++) {
		const char *path;
		path = mkpath("%s%s", repo, suffix[i]);
		if (stat(path, &st))
			continue;
		if (S_ISDIR(st.st_mode) && is_git_directory(path)) {
			*is_bundle = 0;
			return xstrdup(absolute_path(path));
		} else if (S_ISREG(st.st_mode) && st.st_size > 8) {
			/* Is it a "gitfile"? */
			char signature[8];
			int len, fd = open(path, O_RDONLY);
			if (fd < 0)
				continue;
			len = read_in_full(fd, signature, 8);
			close(fd);
			if (len != 8 || strncmp(signature, "gitdir: ", 8))
				continue;
			path = read_gitfile(path);
			if (path) {
				*is_bundle = 0;
				return xstrdup(absolute_path(path));
			}
		}
	}

	for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
		const char *path;
		path = mkpath("%s%s", repo, bundle_suffix[i]);
		if (!stat(path, &st) && S_ISREG(st.st_mode)) {
			*is_bundle = 1;
			return xstrdup(absolute_path(path));
		}
	}

	return NULL;
}
예제 #10
0
파일: exec_cmd.c 프로젝트: 00027jang27/git
static void add_path(struct strbuf *out, const char *path)
{
	if (path && *path) {
		if (is_absolute_path(path))
			strbuf_addstr(out, path);
		else
			strbuf_addstr(out, absolute_path(path));

		strbuf_addch(out, PATH_SEP);
	}
}
예제 #11
0
파일: receive-pack.c 프로젝트: 1tgr/git
static const char *push_to_checkout(unsigned char *sha1,
				    struct argv_array *env,
				    const char *work_tree)
{
	argv_array_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
	if (run_hook_le(env->argv, push_to_checkout_hook,
			sha1_to_hex(sha1), NULL))
		return "push-to-checkout hook declined";
	else
		return NULL;
}
예제 #12
0
static void changed_files(struct hashmap *result, const char *index_path,
			  const char *workdir)
{
	struct child_process update_index = CHILD_PROCESS_INIT;
	struct child_process diff_files = CHILD_PROCESS_INIT;
	struct strbuf index_env = STRBUF_INIT, buf = STRBUF_INIT;
	const char *git_dir = absolute_path(get_git_dir()), *env[] = {
		NULL, NULL
	};
	FILE *fp;

	strbuf_addf(&index_env, "GIT_INDEX_FILE=%s", index_path);
	env[0] = index_env.buf;

	argv_array_pushl(&update_index.args,
			 "--git-dir", git_dir, "--work-tree", workdir,
			 "update-index", "--really-refresh", "-q",
			 "--unmerged", NULL);
	update_index.no_stdin = 1;
	update_index.no_stdout = 1;
	update_index.no_stderr = 1;
	update_index.git_cmd = 1;
	update_index.use_shell = 0;
	update_index.clean_on_exit = 1;
	update_index.dir = workdir;
	update_index.env = env;
	/* Ignore any errors of update-index */
	run_command(&update_index);

	argv_array_pushl(&diff_files.args,
			 "--git-dir", git_dir, "--work-tree", workdir,
			 "diff-files", "--name-only", "-z", NULL);
	diff_files.no_stdin = 1;
	diff_files.git_cmd = 1;
	diff_files.use_shell = 0;
	diff_files.clean_on_exit = 1;
	diff_files.out = -1;
	diff_files.dir = workdir;
	diff_files.env = env;
	if (start_command(&diff_files))
		die("could not obtain raw diff");
	fp = xfdopen(diff_files.out, "r");
	while (!strbuf_getline_nul(&buf, fp)) {
		struct path_entry *entry;
		FLEX_ALLOC_STR(entry, path, buf.buf);
		hashmap_entry_init(entry, strhash(buf.buf));
		hashmap_add(result, entry);
	}
	fclose(fp);
	if (finish_command(&diff_files))
		die("diff-files did not exit properly");
	strbuf_release(&index_env);
	strbuf_release(&buf);
}
예제 #13
0
파일: clone.c 프로젝트: dindinw/git
static char *get_repo_path(const char *repo, int *is_bundle)
{
	struct strbuf path = STRBUF_INIT;
	const char *raw;
	char *canon;

	strbuf_addstr(&path, repo);
	raw = get_repo_path_1(&path, is_bundle);
	canon = raw ? xstrdup(absolute_path(raw)) : NULL;
	strbuf_release(&path);
	return canon;
}
예제 #14
0
int
main( int argc, char *argv[] ) {
  bool ret;
  const char *switch_daemon = NULL;
  char *startup_dir;

  // get startup directory using absolute_path()
  startup_dir = get_current_dir_name();
  if ( startup_dir == NULL ) {
    die( "Failed to get_current_dir_name." );
  }

  init_trema( &argc, &argv ); // changes the current working directory

  init_listener_info( &listener_info );
  ret = parse_argument( &listener_info, argc, argv );
  if ( !ret ) {
    finalize_listener_info( &listener_info );
    exit( EXIT_FAILURE );
  }

  init_dpid_table();
  start_service_management();
  start_switch_management();

  switch_daemon = listener_info.switch_daemon;
  listener_info.switch_daemon = absolute_path( startup_dir, switch_daemon );
  xfree( ( void * ) ( uintptr_t ) switch_daemon );
  // free returned buffer of get_current_dir_name()
  free( startup_dir );

  catch_sigchild();

  // listener start (listen socket binding and listen)
  ret = secure_channel_listen_start( &listener_info );
  if ( !ret ) {
    finalize_listener_info( &listener_info );
    exit( EXIT_FAILURE );
  }

  set_fd_handler( listener_info.listen_fd, secure_channel_accept, &listener_info, NULL, NULL );
  set_readable( listener_info.listen_fd, true );

  start_trema();

  finalize_listener_info( &listener_info );
  stop_switch_management();
  stop_service_management();
  finalize_dpid_table();

  return 0;
}
예제 #15
0
void
make_absolute_path(char **path)
{
     char *abs;

     assert(path != NULL);
     assert(*path != NULL);

     abs = absolute_path(*path);

     free(*path);
     *path = abs;
}
예제 #16
0
static void
test_absolute_path_access_failed() {
  setup();

  char *path;

  will_return( mock_access, -1 );

  path = absolute_path( "/tmp", "/file" );
  assert_true( path == NULL );

  teardown();
}
예제 #17
0
/*
 * Get several source files from the user directory @srcent,
 * store results in @res.
 */
static struct comp_link_t *get_comp_info(struct dirent *srcent, struct comp_link_t *res)
{
    char *compdirpath=(char*)malloc(sizeof(char)*(strlen(srcent->d_name)+4+2));
    DIR *compdir;
    struct dirent *compent;
    struct srcfile_link_t *srcfile;

    strcpy(compdirpath, strconn("src/", srcent->d_name));
    compdir = opendir(compdirpath);

    if(test_error_noexit(!compdir, "Error opening src/%s, ignoring...", srcent->d_name)) return res;

    res->next = malloc_with_type(struct comp_link_t);
    res=res->next;
    srcfile=res->v.srcfile = malloc_with_type(struct srcfile_link_t);
    strcpy(res->v.name, srcent->d_name); /* competitor's name */
    res->next=NULL;
    srcfile->v.path[0]=0; /*header node.*/
    while((compent=readdir(compdir)))   /* srcfile is res->v.srcfile, the target. */
    {
        struct prob_link_t *tmp;
        if(!strcmp(compent->d_name, ".")) continue;
        if(!strcmp(compent->d_name, "..")) continue;

        for(tmp=prob->next; tmp; tmp=tmp->next)
        {
            if(src_match(compent->d_name, tmp->v.name)) /* compent->d_name is source file name */
            {
                srcfile->next=malloc_with_type(struct srcfile_link_t);
                srcfile=srcfile->next;
                memcpy(srcfile->v.path, absolute_path(compent->d_name, compdirpath), sizeof(srcfile->v.path));
                memcpy(srcfile->v.path, absolute_path(srcfile->v.path, base_dir), sizeof(srcfile->v.path));
                /*XXX: v.path can be updated to use pointer.*/
                srcfile->v.prob=tmp;
                srcfile->next=NULL;
            }
        }
    }
예제 #18
0
파일: core.c 프로젝트: kazufusa/kanabo
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);
    }
}
예제 #19
0
파일: main.c 프로젝트: nebososo/sxiv
void check_add_file(char *filename, bool given)
{
	const char *bn;

	if (filename == NULL || *filename == '\0')
		return;

	if (access(filename, R_OK) < 0) {
		if (given)
			warn("could not open file: %s", filename);
		return;
	}

	if (fileidx == filecnt) {
		filecnt *= 2;
		files = s_realloc(files, filecnt * sizeof(*files));
		memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
	}

#if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
    ((_XOPEN_SOURCE - 0) >= 500 || defined _XOPEN_SOURCE_EXTENDED)

	if ((files[fileidx].path = realpath(filename, NULL)) == NULL) {
		warn("could not get real path of file: %s\n", filename);
		return;
	}
#else
	if (*filename != '/') {
		if ((files[fileidx].path = absolute_path(filename)) == NULL) {
			warn("could not get absolute path of file: %s\n", filename);
			return;
		}
	} else {
		files[fileidx].path = NULL;
	}
#endif

	files[fileidx].name = s_strdup(filename);
	if (files[fileidx].path == NULL)
		files[fileidx].path = files[fileidx].name;
	if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
		files[fileidx].base = ++bn;
	else
		files[fileidx].base = files[fileidx].name;
	if (given)
		files[fileidx].flags |= FF_WARN;
	files[fileidx].dir = s_strndup(files[fileidx].path, strlen(files[fileidx].path)-strlen(files[fileidx].base));
	fileidx++;
}
예제 #20
0
파일: gosub.cpp 프로젝트: Aahung/Gorque
int main(int argc, char *argv[]) {
        std::string default_directory(GODIR);
        if ( argc != 2 ) {
        std::cout << "usage: <script file>";
        exit(0);
        }
        char * absolute_path_char = realpath(argv[1], 0);
        std::string absolute_path(absolute_path_char);
        char * user_char = getlogin();
        std::string user(user_char);
        std::string command = default_directory + "/gorque.py -u " + user + " -s " + absolute_path;
        // std::cout << command;
        setuid(0);
        system(command.c_str());
        return 0;
}
예제 #21
0
파일: main.c 프로젝트: kkb7401/sxiv
void check_add_file(char *filename)
{
	const char *bn;

	if (filename == NULL || *filename == '\0')
		return;

	if (access(filename, R_OK) < 0) {
		warn("could not open file: %s", filename);
		return;
	}

	if (fileidx == filecnt) {
		filecnt *= 2;
		files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
	}

#if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
    ((_XOPEN_SOURCE - 0) >= 500 || defined _XOPEN_SOURCE_EXTENDED)

	if ((files[fileidx].path = realpath(filename, NULL)) == NULL) {
		warn("could not get real path of file: %s\n", filename);
		return;
	}
#else
	if (*filename != '/') {
		if ((files[fileidx].path = absolute_path(filename)) == NULL) {
			warn("could not get absolute path of file: %s\n", filename);
			return;
		}
	} else {
		files[fileidx].path = NULL;
	}
#endif

	files[fileidx].loaded = false;
	files[fileidx].name = s_strdup(filename);
	if (files[fileidx].path == NULL)
		files[fileidx].path = files[fileidx].name;
	if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
		files[fileidx].base = ++bn;
	else
		files[fileidx].base = files[fileidx].name;
	fileidx++;
}
예제 #22
0
파일: mkdir.c 프로젝트: arylwen/terebi
int cmd(string str) {
    if( !str )
    {
        return help();
    }
    str = absolute_path(this_player()->query_cwd(), str);
    if( file_size(str) != -1 )
    {
        notify_fail("mkdir: "+str+": file already exists.\n");
        return 0;
    }
    if( (int)master()->valid_write(str, previous_object(), "rmdir") == 0 )
    {
        notify_fail(str+": Permission denied.\n");
        return 0;
    }
    write(mkdir(str) ? "Ok.\n" : str+": couldn't make directory.\n");
    return 1;
}
예제 #23
0
파일: receive-pack.c 프로젝트: 1tgr/git
static const char *update_worktree(unsigned char *sha1)
{
	const char *retval;
	const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
	struct argv_array env = ARGV_ARRAY_INIT;

	if (is_bare_repository())
		return "denyCurrentBranch = updateInstead needs a worktree";

	argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));

	if (!find_hook(push_to_checkout_hook))
		retval = push_to_deploy(sha1, &env, work_tree);
	else
		retval = push_to_checkout(sha1, &env, work_tree);

	argv_array_clear(&env);
	return retval;
}
예제 #24
0
파일: main.cpp 프로젝트: avakar/gh
static bool open_wd(gitdb & db, git_wd & wd, string_view path)
{
	std::string apath = absolute_path(clean_path(path));
	path = apath;

	while (!path.empty())
	{
		std::string nonbare_path = join_paths(path, ".git");
		if (file::is_directory(nonbare_path))
		{
			db.open(nonbare_path);
			wd.open(db, path);
			return true;
		}

		path = path_head(path);
	}
	return false;
}
예제 #25
0
파일: homeroom.c 프로젝트: Elohim/FGmud
mixed cmd(string str){
    object room;
    string path;
    if(!sizeof(str) || str == "here"){
        room = room_environment(this_player());
        path = base_name(room);
    }
    else {
        str = absolute_path(this_player()->query_cwd(), str);
        if(!str) return "File not found.";
        room = find_object(str);
        if(room) path = base_name(room);
    }
    if(!path){
        write("Unable to set "+str+" as your home room.");
        return 1;
    } 
    this_player()->SetParanoia("homeroom", path);
    write("Your current home room is: "+path);
    return 1;
}
int main (int argc, char **argv)
#endif /* def QCAD_NO_CONSOLE */
  {
  char *psz = NULL ;
#ifdef QCAD_NO_CONSOLE
  char **argv = NULL ;
  int argc = 0 ;

  gtk_preamble (&argc, &argv, "graph_dialog", pszCmdLine) ;
#else
  gtk_preamble (&argc, &argv, "graph_dialog") ;
#endif /* def QCAD_NO_CONSOLE */
  SIMULATION_OUTPUT *sim_output = NULL ;

  if (1 == argc)
    {
    if (NULL == (psz = get_file_name_from_user (NULL, _("Open Simulation Results"), NULL, FALSE)))
      return 1 ;
    }
  else
    psz = absolute_path (argv[1]) ;

  if (NULL == (sim_output = open_simulation_output_file (psz)))
    {
    GtkWidget *msg = NULL ;
    gtk_dialog_run (GTK_DIALOG (msg = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
      _("Failed to open simulation data file %s !\n"), psz))) ;
    gtk_widget_hide (msg) ;
    gtk_widget_destroy (msg) ;
    return 2 ;
    }

  set_file_selection_file_name (psz) ;
  g_free (psz) ;

  show_graph_dialog (NULL, sim_output, TRUE, TRUE) ;

  return 0 ;
  }
예제 #27
0
파일: destfile.c 프로젝트: Elohim/FGmud
mixed cmd(string args) {
    string *lines;
    string buff, file, clone, base;
    int scr, ret;
    object ob;
    if( !args ) return "You must specify a file to destruct.";
    sscanf(args, "%s#%s", base, clone);
    if(clone){
        ob = find_object(args);
        if(!ob){
            write(args + " was not found.");
            return 1;
        }
        ret = ob->eventDestruct();
        if(!ret || ob) destruct(ob);
        if(ob){
            write("Failed to destruct "+args);
            return 1;
        }
        write(args + " destructed.");
        return 1;
    }

    file = absolute_path(this_player()->query_cwd(), args);
    if(!(ob = find_object(file)) && !grepp(args, "/")){
        file = DEFINES_D->GetDefine(args);
    }
    if(!(ob = find_object(file))) return "Object " + args + " not found.";
    ret = ob->eventDestruct();
    if(!ret || ob) destruct(ob);
    if(ob){
        write(args + " could not be destructed.");
    }
    else {
        write(args + " destructed.");
    }
    return 1;
}
예제 #28
0
    LIBUPCOREAPI
    ssize_t absolute_path(char* d, size_t dsz, char const* p, char const* base) noexcept {
        wchar_t* native_d = nullptr, * native_p = nullptr, * native_base = nullptr;
        ssize_t retval = -1;
        size_t length;

        if (dsz) {
            native_d = static_cast<wchar_t *>(malloca(dsz * sizeof(wchar_t)));
            if (!native_d) {
                return -1;
            }
        }

        native_p = transcode(p);
        if (!native_p) {
            goto error;
        }

        if (base) {
            native_base = transcode(base);
            if (!native_base) {
                goto error;
            }
        }

        retval = absolute_path(native_d, dsz, native_p, native_base);
        if (!retval) {
            length = static_cast<size_t>(retval);
            retval = transcode(d, dsz, native_d, length);
        }

    error:

        free(native_base);
        free(native_p);
        freea(native_d);
        return retval;
    }
예제 #29
0
파일: vis.c 프로젝트: 5paceToast/vis
static File *file_new(Vis *vis, const char *name) {
	char *name_absolute = NULL;
	if (name) {
		if (!(name_absolute = absolute_path(name)))
			return NULL;
		File *existing = NULL;
		/* try to detect whether the same file is already open in another window
		 * TODO: do this based on inodes */
		for (File *file = vis->files; file; file = file->next) {
			if (file->name && strcmp(file->name, name_absolute) == 0) {
				existing = file;
				break;
			}
		}
		if (existing) {
			free(name_absolute);
			return existing;
		}
	}

	File *file = NULL;
	Text *text = text_load(name);
	if (!text && name && errno == ENOENT)
		text = text_load(NULL);
	if (!text)
		goto err;
	if (!(file = file_new_text(vis, text)))
		goto err;
	file->name = name_absolute;
	if (!file->internal && vis->event && vis->event->file_open)
		vis->event->file_open(vis, file);
	return file;
err:
	free(name_absolute);
	text_free(text);
	file_free(vis, file);
	return NULL;
}
예제 #30
0
파일: receive-pack.c 프로젝트: Lisda/git
static void merge_worktree(unsigned char *sha1)
{
	const char *update_refresh[] = {
		"update-index", "--ignore-submodules", "--refresh", NULL
	};
	const char *read_tree[] = {
		"read-tree", "-u", "-m", sha1_to_hex(sha1), NULL
	};
	struct child_process child;
	struct strbuf git_env = STRBUF_INIT;
	const char *env[2];

	if (is_bare_repository())
		die ("denyCurrentBranch = updateInstead needs a worktree");

	strbuf_addf(&git_env, "GIT_DIR=%s", absolute_path(get_git_dir()));
	env[0] = git_env.buf;
	env[1] = NULL;

	memset(&child, 0, sizeof(child));
	child.argv = update_refresh;
	child.env = env;
	child.dir = git_work_tree_cfg ? git_work_tree_cfg : "..";
	child.stdout_to_stderr = 1;
	child.git_cmd = 1;
	if (run_command(&child))
		die ("Could not refresh the index");

	child.argv = read_tree;
	child.no_stdin = 1;
	child.no_stdout = 1;
	child.stdout_to_stderr = 0;
	if (run_command(&child))
		die ("Could not merge working tree with new HEAD.  Good luck.");

	strbuf_release(&git_env);
}