Skip to content

Commit bdfc07b

Browse files
pks-tgitster
authored andcommitted
path: refactor repo_git_path() family of functions
As explained in an earlier commit, we're refactoring path-related functions to provide a consistent interface for computing paths into the commondir, gitdir and worktree. Refactor the "gitdir" family of functions accordingly. Note that the `repo_git_pathv()` function is converted into an internal implementation detail. It is only used to implement `the_repository` compatibility shims and will eventually be removed from the public interface. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 70a16ff commit bdfc07b

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

editor.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@ int strbuf_edit_interactively(struct repository *r,
142142
struct strbuf sb = STRBUF_INIT;
143143
int fd, res = 0;
144144

145-
if (!is_absolute_path(path)) {
146-
strbuf_repo_git_path(&sb, r, "%s", path);
147-
path = sb.buf;
148-
}
145+
if (!is_absolute_path(path))
146+
path = repo_git_path_append(r, &sb, "%s", path);
149147

150148
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
151149
if (fd < 0)

hook.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ const char *find_hook(struct repository *r, const char *name)
1616

1717
int found_hook;
1818

19-
strbuf_reset(&path);
20-
strbuf_repo_git_path(&path, r, "hooks/%s", name);
19+
repo_git_path_replace(r, &path, "hooks/%s", name);
2120
found_hook = access(path.buf, X_OK) >= 0;
2221
#ifdef STRIP_EXTENSION
2322
if (!found_hook) {

path.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,27 @@ char *repo_git_path(const struct repository *repo,
443443
return strbuf_detach(&path, NULL);
444444
}
445445

446-
void strbuf_repo_git_path(struct strbuf *sb,
447-
const struct repository *repo,
448-
const char *fmt, ...)
446+
const char *repo_git_path_append(const struct repository *repo,
447+
struct strbuf *sb,
448+
const char *fmt, ...)
449449
{
450450
va_list args;
451451
va_start(args, fmt);
452452
repo_git_pathv(repo, NULL, sb, fmt, args);
453453
va_end(args);
454+
return sb->buf;
455+
}
456+
457+
const char *repo_git_path_replace(const struct repository *repo,
458+
struct strbuf *sb,
459+
const char *fmt, ...)
460+
{
461+
va_list args;
462+
strbuf_reset(sb);
463+
va_start(args, fmt);
464+
repo_git_pathv(repo, NULL, sb, fmt, args);
465+
va_end(args);
466+
return sb->buf;
454467
}
455468

456469
char *mkpathdup(const char *fmt, ...)

path.h

+11-21
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,16 @@ const char *repo_common_path_replace(const struct repository *repo,
5252
* For an exhaustive list of the adjustments made look at `common_list` and
5353
* `adjust_git_path` in path.c.
5454
*/
55-
56-
/*
57-
* Return a path into the git directory of repository `repo`.
58-
*/
5955
char *repo_git_path(const struct repository *repo,
6056
const char *fmt, ...)
6157
__attribute__((format (printf, 2, 3)));
62-
63-
/*
64-
* Print a path into the git directory of repository `repo` into the provided
65-
* buffer.
66-
*/
67-
void repo_git_pathv(const struct repository *repo,
68-
const struct worktree *wt, struct strbuf *buf,
69-
const char *fmt, va_list args);
70-
71-
/*
72-
* Construct a path into the git directory of repository `repo` and append it
73-
* to the provided buffer `sb`.
74-
*/
75-
void strbuf_repo_git_path(struct strbuf *sb,
76-
const struct repository *repo,
77-
const char *fmt, ...)
58+
const char *repo_git_path_append(const struct repository *repo,
59+
struct strbuf *sb,
60+
const char *fmt, ...)
61+
__attribute__((format (printf, 3, 4)));
62+
const char *repo_git_path_replace(const struct repository *repo,
63+
struct strbuf *sb,
64+
const char *fmt, ...)
7865
__attribute__((format (printf, 3, 4)));
7966

8067
/*
@@ -241,11 +228,14 @@ struct strbuf *get_pathname(void);
241228
# include "strbuf.h"
242229
# include "repository.h"
243230

244-
/* Internal implementation detail that should not be used. */
231+
/* Internal implementation details that should not be used. */
245232
void repo_common_pathv(const struct repository *repo,
246233
struct strbuf *buf,
247234
const char *fmt,
248235
va_list args);
236+
void repo_git_pathv(const struct repository *repo,
237+
const struct worktree *wt, struct strbuf *buf,
238+
const char *fmt, va_list args);
249239

250240
/*
251241
* Return a statically allocated path into the main repository's

submodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ static int repo_has_absorbed_submodules(struct repository *r)
13151315
int ret;
13161316
struct strbuf buf = STRBUF_INIT;
13171317

1318-
strbuf_repo_git_path(&buf, r, "modules/");
1318+
repo_git_path_append(r, &buf, "modules/");
13191319
ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
13201320
strbuf_release(&buf);
13211321
return ret;
@@ -2629,6 +2629,6 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
26292629
* administrators can explicitly set. Nothing has been decided,
26302630
* so for now, just append the name at the end of the path.
26312631
*/
2632-
strbuf_repo_git_path(buf, r, "modules/");
2632+
repo_git_path_append(r, buf, "modules/");
26332633
strbuf_addstr(buf, submodule_name);
26342634
}

0 commit comments

Comments
 (0)