Skip to content

Commit 93a8cfa

Browse files
pks-tgitster
authored andcommitted
path: refactor repo_worktree_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 "worktree" family of functions accordingly. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bdfc07b commit 93a8cfa

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

path.c

+22-6
Original file line numberDiff line numberDiff line change
@@ -519,28 +519,44 @@ char *repo_worktree_path(const struct repository *repo, const char *fmt, ...)
519519
struct strbuf path = STRBUF_INIT;
520520
va_list args;
521521

522+
va_start(args, fmt);
523+
do_worktree_path(repo, &path, fmt, args);
524+
va_end(args);
525+
526+
return strbuf_detach(&path, NULL);
527+
}
528+
529+
const char *repo_worktree_path_append(const struct repository *repo,
530+
struct strbuf *sb,
531+
const char *fmt, ...)
532+
{
533+
va_list args;
534+
522535
if (!repo->worktree)
523536
return NULL;
524537

525538
va_start(args, fmt);
526-
do_worktree_path(repo, &path, fmt, args);
539+
do_worktree_path(repo, sb, fmt, args);
527540
va_end(args);
528541

529-
return strbuf_detach(&path, NULL);
542+
return sb->buf;
530543
}
531544

532-
void strbuf_repo_worktree_path(struct strbuf *sb,
533-
const struct repository *repo,
534-
const char *fmt, ...)
545+
const char *repo_worktree_path_replace(const struct repository *repo,
546+
struct strbuf *sb,
547+
const char *fmt, ...)
535548
{
536549
va_list args;
537550

551+
strbuf_reset(sb);
538552
if (!repo->worktree)
539-
return;
553+
return NULL;
540554

541555
va_start(args, fmt);
542556
do_worktree_path(repo, sb, fmt, args);
543557
va_end(args);
558+
559+
return sb->buf;
544560
}
545561

546562
/* Returns 0 on success, negative on failure. */

path.h

+9-11
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,22 @@ const char *worktree_git_path(struct repository *r,
7575
__attribute__((format (printf, 3, 4)));
7676

7777
/*
78-
* Return a path into the worktree of repository `repo`.
78+
* The `repo_worktree_path` family of functions will construct a path into a
79+
* repository's worktree.
7980
*
80-
* If the repository doesn't have a worktree NULL is returned.
81+
* Returns a `NULL` pointer in case the repository has no worktree.
8182
*/
8283
char *repo_worktree_path(const struct repository *repo,
8384
const char *fmt, ...)
8485
__attribute__((format (printf, 2, 3)));
85-
86-
/*
87-
* Construct a path into the worktree of repository `repo` and append it
88-
* to the provided buffer `sb`.
89-
*
90-
* If the repository doesn't have a worktree nothing will be appended to `sb`.
91-
*/
92-
void strbuf_repo_worktree_path(struct strbuf *sb,
93-
const struct repository *repo,
86+
const char *repo_worktree_path_append(const struct repository *repo,
87+
struct strbuf *sb,
9488
const char *fmt, ...)
9589
__attribute__((format (printf, 3, 4)));
90+
const char *repo_worktree_path_replace(const struct repository *repo,
91+
struct strbuf *sb,
92+
const char *fmt, ...)
93+
__attribute__((format (printf, 3, 4)));
9694

9795
/*
9896
* Return a path into a submodule's git directory located at `path`. `path`

repository.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ int repo_submodule_init(struct repository *subrepo,
312312
struct strbuf worktree = STRBUF_INIT;
313313
int ret = 0;
314314

315-
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
316-
strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
315+
repo_worktree_path_append(superproject, &gitdir, "%s/.git", path);
316+
repo_worktree_path_append(superproject, &worktree, "%s", path);
317317

318318
if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
319319
/*

0 commit comments

Comments
 (0)