Skip to content

Commit a52beae

Browse files
pks-tgitster
authored andcommitted
environment: move set_git_dir() and related into setup layer
The functions `set_git_dir()` and friends are used to set up repositories. As such, they are quite clearly part of the setup subsystem, but still live in "environment.c". Move them over, which also helps to get rid of dependencies on `the_repository` in the environment subsystem. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c22d183 commit a52beae

File tree

4 files changed

+109
-107
lines changed

4 files changed

+109
-107
lines changed

environment.c

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,9 @@
2222
#include "fmt-merge-msg.h"
2323
#include "commit.h"
2424
#include "strvec.h"
25-
#include "object-file.h"
2625
#include "path.h"
27-
#include "replace-object.h"
28-
#include "tmp-objdir.h"
2926
#include "chdir-notify.h"
3027
#include "setup.h"
31-
#include "shallow.h"
32-
#include "trace.h"
3328
#include "write-or-die.h"
3429

3530
int trust_executable_bit = 1;
@@ -155,41 +150,6 @@ const char *getenv_safe(struct strvec *argv, const char *name)
155150
return argv->v[argv->nr - 1];
156151
}
157152

158-
void setup_git_env(const char *git_dir)
159-
{
160-
char *git_replace_ref_base;
161-
const char *shallow_file;
162-
const char *replace_ref_base;
163-
struct set_gitdir_args args = { NULL };
164-
struct strvec to_free = STRVEC_INIT;
165-
166-
args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
167-
args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
168-
args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
169-
args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
170-
args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
171-
if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
172-
args.disable_ref_updates = 1;
173-
}
174-
175-
repo_set_gitdir(the_repository, git_dir, &args);
176-
strvec_clear(&to_free);
177-
178-
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
179-
disable_replace_refs();
180-
replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
181-
git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
182-
: "refs/replace/");
183-
update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
184-
185-
shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
186-
if (shallow_file)
187-
set_alternate_shallow_file(the_repository, shallow_file, 0);
188-
189-
if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0))
190-
fetch_if_missing = 0;
191-
}
192-
193153
int is_bare_repository(void)
194154
{
195155
/* if core.bare is not 'false', let's see if there is a work tree */
@@ -243,71 +203,6 @@ const char *strip_namespace(const char *namespaced_ref)
243203
return NULL;
244204
}
245205

246-
static int git_work_tree_initialized;
247-
248-
/*
249-
* Note. This works only before you used a work tree. This was added
250-
* primarily to support git-clone to work in a new repository it just
251-
* created, and is not meant to flip between different work trees.
252-
*/
253-
void set_git_work_tree(const char *new_work_tree)
254-
{
255-
if (git_work_tree_initialized) {
256-
struct strbuf realpath = STRBUF_INIT;
257-
258-
strbuf_realpath(&realpath, new_work_tree, 1);
259-
new_work_tree = realpath.buf;
260-
if (strcmp(new_work_tree, the_repository->worktree))
261-
die("internal error: work tree has already been set\n"
262-
"Current worktree: %s\nNew worktree: %s",
263-
the_repository->worktree, new_work_tree);
264-
strbuf_release(&realpath);
265-
return;
266-
}
267-
git_work_tree_initialized = 1;
268-
repo_set_worktree(the_repository, new_work_tree);
269-
}
270-
271-
static void set_git_dir_1(const char *path)
272-
{
273-
xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
274-
setup_git_env(path);
275-
}
276-
277-
static void update_relative_gitdir(const char *name UNUSED,
278-
const char *old_cwd,
279-
const char *new_cwd,
280-
void *data UNUSED)
281-
{
282-
char *path = reparent_relative_path(old_cwd, new_cwd,
283-
repo_get_git_dir(the_repository));
284-
struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
285-
286-
trace_printf_key(&trace_setup_key,
287-
"setup: move $GIT_DIR to '%s'",
288-
path);
289-
set_git_dir_1(path);
290-
if (tmp_objdir)
291-
tmp_objdir_reapply_primary_odb(tmp_objdir, old_cwd, new_cwd);
292-
free(path);
293-
}
294-
295-
void set_git_dir(const char *path, int make_realpath)
296-
{
297-
struct strbuf realpath = STRBUF_INIT;
298-
299-
if (make_realpath) {
300-
strbuf_realpath(&realpath, path, 1);
301-
path = realpath.buf;
302-
}
303-
304-
set_git_dir_1(path);
305-
if (!is_absolute_path(path))
306-
chdir_notify_register(NULL, update_relative_gitdir, NULL);
307-
308-
strbuf_release(&realpath);
309-
}
310-
311206
const char *get_log_output_encoding(void)
312207
{
313208
return git_log_output_encoding ? git_log_output_encoding

environment.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ int have_git_dir(void);
105105
extern int is_bare_repository_cfg;
106106
int is_bare_repository(void);
107107
extern char *git_work_tree_cfg;
108-
void set_git_dir(const char *path, int make_realpath);
109108
const char *get_git_namespace(void);
110109
const char *strip_namespace(const char *namespaced_ref);
111-
void set_git_work_tree(const char *tree);
112110

113111
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
114112

setup.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77
#include "exec-cmd.h"
88
#include "gettext.h"
99
#include "hex.h"
10+
#include "object-file.h"
1011
#include "object-name.h"
1112
#include "refs.h"
13+
#include "replace-object.h"
1214
#include "repository.h"
1315
#include "config.h"
1416
#include "dir.h"
1517
#include "setup.h"
18+
#include "shallow.h"
1619
#include "string-list.h"
20+
#include "strvec.h"
1721
#include "chdir-notify.h"
1822
#include "path.h"
1923
#include "quote.h"
24+
#include "tmp-objdir.h"
25+
#include "trace.h"
2026
#include "trace2.h"
2127
#include "worktree.h"
2228
#include "exec-cmd.h"
@@ -1613,6 +1619,106 @@ enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
16131619
return result;
16141620
}
16151621

1622+
void setup_git_env(const char *git_dir)
1623+
{
1624+
char *git_replace_ref_base;
1625+
const char *shallow_file;
1626+
const char *replace_ref_base;
1627+
struct set_gitdir_args args = { NULL };
1628+
struct strvec to_free = STRVEC_INIT;
1629+
1630+
args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
1631+
args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
1632+
args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
1633+
args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
1634+
args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
1635+
if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
1636+
args.disable_ref_updates = 1;
1637+
}
1638+
1639+
repo_set_gitdir(the_repository, git_dir, &args);
1640+
strvec_clear(&to_free);
1641+
1642+
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
1643+
disable_replace_refs();
1644+
replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
1645+
git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
1646+
: "refs/replace/");
1647+
update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
1648+
1649+
shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
1650+
if (shallow_file)
1651+
set_alternate_shallow_file(the_repository, shallow_file, 0);
1652+
1653+
if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0))
1654+
fetch_if_missing = 0;
1655+
}
1656+
1657+
static void set_git_dir_1(const char *path)
1658+
{
1659+
xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
1660+
setup_git_env(path);
1661+
}
1662+
1663+
static void update_relative_gitdir(const char *name UNUSED,
1664+
const char *old_cwd,
1665+
const char *new_cwd,
1666+
void *data UNUSED)
1667+
{
1668+
char *path = reparent_relative_path(old_cwd, new_cwd,
1669+
repo_get_git_dir(the_repository));
1670+
struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
1671+
1672+
trace_printf_key(&trace_setup_key,
1673+
"setup: move $GIT_DIR to '%s'",
1674+
path);
1675+
set_git_dir_1(path);
1676+
if (tmp_objdir)
1677+
tmp_objdir_reapply_primary_odb(tmp_objdir, old_cwd, new_cwd);
1678+
free(path);
1679+
}
1680+
1681+
void set_git_dir(const char *path, int make_realpath)
1682+
{
1683+
struct strbuf realpath = STRBUF_INIT;
1684+
1685+
if (make_realpath) {
1686+
strbuf_realpath(&realpath, path, 1);
1687+
path = realpath.buf;
1688+
}
1689+
1690+
set_git_dir_1(path);
1691+
if (!is_absolute_path(path))
1692+
chdir_notify_register(NULL, update_relative_gitdir, NULL);
1693+
1694+
strbuf_release(&realpath);
1695+
}
1696+
1697+
static int git_work_tree_initialized;
1698+
1699+
/*
1700+
* Note. This works only before you used a work tree. This was added
1701+
* primarily to support git-clone to work in a new repository it just
1702+
* created, and is not meant to flip between different work trees.
1703+
*/
1704+
void set_git_work_tree(const char *new_work_tree)
1705+
{
1706+
if (git_work_tree_initialized) {
1707+
struct strbuf realpath = STRBUF_INIT;
1708+
1709+
strbuf_realpath(&realpath, new_work_tree, 1);
1710+
new_work_tree = realpath.buf;
1711+
if (strcmp(new_work_tree, the_repository->worktree))
1712+
die("internal error: work tree has already been set\n"
1713+
"Current worktree: %s\nNew worktree: %s",
1714+
the_repository->worktree, new_work_tree);
1715+
strbuf_release(&realpath);
1716+
return;
1717+
}
1718+
git_work_tree_initialized = 1;
1719+
repo_set_worktree(the_repository, new_work_tree);
1720+
}
1721+
16161722
const char *setup_git_directory_gently(int *nongit_ok)
16171723
{
16181724
static struct strbuf cwd = STRBUF_INIT;

setup.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ static inline int discover_git_directory(struct strbuf *commondir,
9494
return 0;
9595
}
9696

97+
void set_git_dir(const char *path, int make_realpath);
98+
void set_git_work_tree(const char *tree);
99+
97100
const char *setup_git_directory_gently(int *);
98101
const char *setup_git_directory(void);
99102
char *prefix_path(const char *prefix, int len, const char *path);

0 commit comments

Comments
 (0)