Skip to content

Commit e2a57aa

Browse files
carlosmngitster
authored andcommitted
Name make_*_path functions more accurately
Rename the make_*_path functions so it's clearer what they do, in particlar make clear what the differnce between make_absolute_path and make_nonrelative_path is by renaming them real_path and absolute_path respectively. make_relative_path has an understandable name and is renamed to relative_path to maintain the name convention. The function calls have been replaced 1-to-1 in their usage. Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3ca9b0 commit e2a57aa

File tree

14 files changed

+52
-38
lines changed

14 files changed

+52
-38
lines changed

abspath.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ int is_directory(const char *path)
1414
/* We allow "recursive" symbolic links. Only within reason, though. */
1515
#define MAXDEPTH 5
1616

17-
const char *make_absolute_path(const char *path)
17+
/*
18+
* Use this to get the real path, i.e. resolve links. If you want an
19+
* absolute path but don't mind links, use absolute_path.
20+
*
21+
* If path is our buffer, then return path, as it's already what the
22+
* user wants.
23+
*/
24+
const char *real_path(const char *path)
1825
{
1926
static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
2027
char cwd[1024] = "";
@@ -104,7 +111,14 @@ static const char *get_pwd_cwd(void)
104111
return cwd;
105112
}
106113

107-
const char *make_nonrelative_path(const char *path)
114+
/*
115+
* Use this to get an absolute path from a relative one. If you want
116+
* to resolve links, you should use real_path.
117+
*
118+
* If the path is already absolute, then return path. As the user is
119+
* never meant to free the return value, we're safe.
120+
*/
121+
const char *absolute_path(const char *path)
108122
{
109123
static char buf[PATH_MAX + 1];
110124

builtin/clone.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
100100
path = mkpath("%s%s", repo, suffix[i]);
101101
if (is_directory(path)) {
102102
*is_bundle = 0;
103-
return xstrdup(make_nonrelative_path(path));
103+
return xstrdup(absolute_path(path));
104104
}
105105
}
106106

@@ -109,7 +109,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
109109
path = mkpath("%s%s", repo, bundle_suffix[i]);
110110
if (!stat(path, &st) && S_ISREG(st.st_mode)) {
111111
*is_bundle = 1;
112-
return xstrdup(make_nonrelative_path(path));
112+
return xstrdup(absolute_path(path));
113113
}
114114
}
115115

@@ -203,7 +203,7 @@ static void setup_reference(const char *repo)
203203
struct transport *transport;
204204
const struct ref *extra;
205205

206-
ref_git = make_absolute_path(option_reference);
206+
ref_git = real_path(option_reference);
207207

208208
if (is_directory(mkpath("%s/.git/objects", ref_git)))
209209
ref_git = mkpath("%s/.git", ref_git);
@@ -411,7 +411,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
411411

412412
path = get_repo_path(repo_name, &is_bundle);
413413
if (path)
414-
repo = xstrdup(make_nonrelative_path(repo_name));
414+
repo = xstrdup(absolute_path(repo_name));
415415
else if (!strchr(repo_name, ':'))
416416
die("repository '%s' does not exist", repo_name);
417417
else
@@ -466,7 +466,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
466466

467467
if (safe_create_leading_directories_const(git_dir) < 0)
468468
die("could not create leading directories of '%s'", git_dir);
469-
set_git_dir(make_absolute_path(git_dir));
469+
set_git_dir(real_path(git_dir));
470470

471471
if (0 <= option_verbosity)
472472
printf("Cloning into %s%s...\n",

builtin/init-db.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
501501
const char *git_dir_parent = strrchr(git_dir, '/');
502502
if (git_dir_parent) {
503503
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
504-
git_work_tree_cfg = xstrdup(make_absolute_path(rel));
504+
git_work_tree_cfg = xstrdup(real_path(rel));
505505
free(rel);
506506
}
507507
if (!git_work_tree_cfg) {
@@ -510,7 +510,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
510510
die_errno ("Cannot access current working directory");
511511
}
512512
if (work_tree)
513-
set_git_work_tree(make_absolute_path(work_tree));
513+
set_git_work_tree(real_path(work_tree));
514514
else
515515
set_git_work_tree(git_work_tree_cfg);
516516
if (access(get_git_work_tree(), X_OK))
@@ -519,10 +519,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
519519
}
520520
else {
521521
if (work_tree)
522-
set_git_work_tree(make_absolute_path(work_tree));
522+
set_git_work_tree(real_path(work_tree));
523523
}
524524

525-
set_git_dir(make_absolute_path(git_dir));
525+
set_git_dir(real_path(git_dir));
526526

527527
return init_db(template_dir, flags);
528528
}

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ static int add_refs_from_alternate(struct alternate_object_database *e, void *un
740740
const struct ref *extra;
741741

742742
e->name[-1] = '\0';
743-
other = xstrdup(make_absolute_path(e->base));
743+
other = xstrdup(real_path(e->base));
744744
e->name[-1] = '/';
745745
len = strlen(other);
746746

cache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,9 @@ static inline int is_absolute_path(const char *path)
731731
return path[0] == '/' || has_dos_drive_prefix(path);
732732
}
733733
int is_directory(const char *);
734-
const char *make_absolute_path(const char *path);
735-
const char *make_nonrelative_path(const char *path);
736-
const char *make_relative_path(const char *abs, const char *base);
734+
const char *real_path(const char *path);
735+
const char *absolute_path(const char *path);
736+
const char *relative_path(const char *abs, const char *base);
737737
int normalize_path_copy(char *dst, const char *src);
738738
int longest_ancestor_length(const char *path, const char *prefix_list);
739739
char *strip_path_suffix(const char *path, const char *suffix);

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)
11281128
die_errno("can't find the current directory");
11291129

11301130
if (!is_absolute_path(dir))
1131-
dir = make_absolute_path(dir);
1131+
dir = real_path(dir);
11321132

11331133
while (*dir && *dir == *cwd) {
11341134
dir++;

environment.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ static int git_work_tree_initialized;
139139
void set_git_work_tree(const char *new_work_tree)
140140
{
141141
if (git_work_tree_initialized) {
142-
new_work_tree = make_absolute_path(new_work_tree);
142+
new_work_tree = real_path(new_work_tree);
143143
if (strcmp(new_work_tree, work_tree))
144144
die("internal error: work tree has already been set\n"
145145
"Current worktree: %s\nNew worktree: %s",
146146
work_tree, new_work_tree);
147147
return;
148148
}
149149
git_work_tree_initialized = 1;
150-
work_tree = xstrdup(make_absolute_path(new_work_tree));
150+
work_tree = xstrdup(real_path(new_work_tree));
151151
}
152152

153153
const char *get_git_work_tree(void)

exec_cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void add_path(struct strbuf *out, const char *path)
8989
if (is_absolute_path(path))
9090
strbuf_addstr(out, path);
9191
else
92-
strbuf_addstr(out, make_nonrelative_path(path));
92+
strbuf_addstr(out, absolute_path(path));
9393

9494
strbuf_addch(out, PATH_SEP);
9595
}

lockfile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ static char *unable_to_lock_message(const char *path, int err)
164164
"If no other git process is currently running, this probably means a\n"
165165
"git process crashed in this repository earlier. Make sure no other git\n"
166166
"process is running and remove the file manually to continue.",
167-
make_nonrelative_path(path), strerror(err));
167+
absolute_path(path), strerror(err));
168168
} else
169169
strbuf_addf(&buf, "Unable to create '%s.lock': %s",
170-
make_nonrelative_path(path), strerror(err));
170+
absolute_path(path), strerror(err));
171171
return strbuf_detach(&buf, NULL);
172172
}
173173

path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ int set_shared_perm(const char *path, int mode)
397397
return 0;
398398
}
399399

400-
const char *make_relative_path(const char *abs, const char *base)
400+
const char *relative_path(const char *abs, const char *base)
401401
{
402402
static char buf[PATH_MAX + 1];
403403
int i = 0, j = 0;

0 commit comments

Comments
 (0)