diff options
| author | Vicent Marti <tanoku@gmail.com> | 2011-01-19 17:15:02 -0800 | 
|---|---|---|
| committer | Vicent Marti <tanoku@gmail.com> | 2011-01-19 17:18:01 -0800 | 
| commit | b5e567b9fcaa9bed476feb79b2c934ddab05e2b9 (patch) | |
| tree | beb63a127d36509a56258419804a2457a252aede /src/fileops.c | |
| parent | ec3c7a16c260fa6540cfe8daf37c1324100f51bf (diff) | |
| parent | 170d3f2fbbaf529afc209f78fc7ee88b5680eb5d (diff) | |
| download | libgit2-b5e567b9fcaa9bed476feb79b2c934ddab05e2b9.tar.gz | |
Merge branch 'dir-path-prettifying' of https://github.com/nulltoken/libgit2
Diffstat (limited to 'src/fileops.c')
| -rw-r--r-- | src/fileops.c | 76 | 
1 files changed, 76 insertions, 0 deletions
diff --git a/src/fileops.c b/src/fileops.c index 1d680f354..1c480e727 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -364,5 +364,81 @@ int gitfo_mkdir_recurs(const char *path, int mode)  	free(path_copy);  	return error; +} + +static int retrieve_previous_path_component_start(const char *path) +{ +	int offset, len, start = 0; +	 +	len = strlen(path); +	offset = len - 1; + +	/* Skip leading slash */ +	if (path[start] == '/') +		start++; + +	/* Skip trailing slash */ +	if (path[offset] == '/') +		offset--; + +	if (offset < 0) +		return GIT_ERROR; + +	while (offset > start && path[offset-1] != '/') { +		offset--; +	} + +	return offset; +} + +int git_prettify_dir_path(char *buffer_out, const char *path) +{ +	int len = 0; +	char *current; +	const char *buffer_out_start, *buffer_end; + +	buffer_out_start = buffer_out; +	current = (char *)path; +	buffer_end = path + strlen(path); + +	while (current < buffer_end) { +		/* Prevent multiple slashes from being added to the output */ +		if (*current == '/' && len > 0 && buffer_out_start[len - 1] == '/') { +			current++; +			continue; +		} +		 +		/* Skip current directory */ +		if (*current == '.') { +			current++; + +			/* Handle the double-dot upward directory navigation */ +			if (*current == '.') { +				current++; + +				*buffer_out ='\0'; +				len = retrieve_previous_path_component_start(buffer_out_start); +				if (len < GIT_SUCCESS) +					return GIT_ERROR; +				buffer_out = (char *)buffer_out_start + len; +			} + +			if (*current == '/') +				current++; + +			continue; +		} + +		*buffer_out++ = *current++; +		len++; +	} + +	/* Add a trailing slash if required */ +	if (len > 0 && buffer_out_start[len-1] != '/') +		*buffer_out++ = '/'; + +	*buffer_out = '\0'; + +	return GIT_SUCCESS;  }  | 
