Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,20 @@ static int config_alias_cb(const char *var, const char *value,
if (subsection && !subsection_len)
subsection = NULL;

if (subsection && strcmp(key, "command"))
return 0;
if (subsection && strcmp(key, "command")) {
/*
* We have historically supported the "alias.name" form when
* "name" happens to contain dots (e.g., alias.foo.bar to allow
* "git foo.bar". But our parsing above would split that into
* subsection "foo".
*
* If we do not understand the final key in a subsection-style
* variable, fall back to treating it as a two-level alias.
*/
key = var + strlen("alias.");
subsection = NULL;
subsection_len = 0;
}

if (data->alias) {
int match;
Expand Down
9 changes: 8 additions & 1 deletion help.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,21 @@ static int git_unknown_cmd_config(const char *var, const char *value,
/* Also use aliases for command lookup */
if (!parse_config_key(var, "alias", &subsection, &subsection_len,
&key)) {
size_t key_len = strlen(key);

if (subsection) {
/* [alias "name"] command = value */
if (!strcmp(key, "command"))
add_cmdname(&cfg->aliases, subsection,
subsection_len);
else {
key = var + strlen("alias.");
key_len = strlen(key);
add_cmdname(&cfg->aliases, key, key_len);
}
} else {
/* alias.name = value */
add_cmdname(&cfg->aliases, key, strlen(key));
add_cmdname(&cfg->aliases, key, key_len);
}
}

Expand Down
10 changes: 7 additions & 3 deletions run-command.c
Original file line number Diff line number Diff line change
Expand Up @@ -1945,10 +1945,14 @@ void run_processes_parallel(const struct run_process_parallel_opts *opts)
int prepare_auto_maintenance(struct repository *r, int quiet,
struct child_process *maint)
{
int enabled, auto_detach;
int enabled = 1, auto_detach;

if (!repo_config_get_bool(r, "maintenance.auto", &enabled) &&
!enabled)
if (repo_config_get_bool(r, "maintenance.auto", &enabled)) {
int gc_threshold;
if (!repo_config_get_int(r, "gc.auto", &gc_threshold))
enabled = gc_threshold > 0;
}
if (!enabled)
return 0;

/*
Expand Down
16 changes: 15 additions & 1 deletion setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2171,12 +2171,26 @@ int daemonize(void)
errno = ENOSYS;
return -1;
#else
switch (fork()) {
pid_t parent_pid = getpid();
pid_t child_pid = fork();

switch (child_pid) {
case 0:
/*
* We're in the child process, so we take ownership of
* all tempfiles.
*/
reassign_tempfile_ownership(parent_pid, getpid());
break;
case -1:
die_errno(_("fork failed"));
default:
/*
* We're in the parent process, so we drop ownership of
* all tempfiles to prevent us from removing them upon
* exit.
*/
reassign_tempfile_ownership(parent_pid, child_pid);
exit(0);
}
if (setsid() == -1)
Expand Down
15 changes: 15 additions & 0 deletions setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ void verify_non_filename(const char *prefix, const char *name);
int path_inside_repo(const char *prefix, const char *path);

void sanitize_stdfds(void);

/*
* Daemonize the current process by forking and then exiting the parent
* process. Returns 0 when successful, in which case the parent process will
* have exited and it's the child process that continues to run the code.
* Otherwise, a negative error code is returned and the parent process will
* continue execution.
*
* Note that this function will also perform the following changes:
*
* - Standard file descriptors in the child process are closed.
* - The child process is made a session leader via setsid(3p).
* - All tempfiles owned by the parent process are reassigned to the
* daemonized child process.
*/
int daemonize(void);

/*
Expand Down
6 changes: 5 additions & 1 deletion shallow.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ struct commit_list *get_shallow_commits(struct object_array *heads,
int depth, int shallow_flag, int not_shallow_flag)
{
if (shallows && deepen_relative) {
depth += get_shallows_depth(heads, shallows);
int cur_shallow_depth = get_shallows_depth(heads, shallows);
if (cur_shallow_depth)
depth += cur_shallow_depth;
else
return NULL;
}
return get_shallows_or_depth(heads, NULL, NULL,
depth, shallow_flag, not_shallow_flag);
Expand Down
12 changes: 12 additions & 0 deletions t/t0014-alias.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ test_expect_success 'subsection syntax works' '
test_grep "ran-subsection" output
'

test_expect_success 'simple dotted alias syntax still works' '
test_config alias.simple.dotted "!echo ran-simple-dotted" &&
git simple.dotted >output &&
test_grep "ran-simple-dotted" output
'

test_expect_success 'subsection syntax only accepts command key' '
test_config alias.invalid.notcommand value &&
test_must_fail git invalid 2>error &&
Expand Down Expand Up @@ -183,6 +189,12 @@ test_expect_success 'subsection aliases listed in help -a' '
test_grep "förgrena" output
'

test_expect_success 'simple dotted aliases listed in help -a' '
test_config alias.simple.listed "!echo test" &&
git help -a >output &&
test_grep "simple.listed" output
'

test_expect_success 'empty subsection treated as no subsection' '
test_config "alias..something" "!echo foobar" &&
git something >actual &&
Expand Down
10 changes: 10 additions & 0 deletions t/t5537-fetch-shallow.sh
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ test_expect_success '.git/shallow is edited by repack' '
origin "+refs/heads/*:refs/remotes/origin/*"
'

test_expect_success 'fetch --deepen does not truncate' '
git clone --no-local .git full-clone &&
git -C full-clone rev-parse --is-shallow-repository >expect &&
git -C full-clone log --oneline >>expect &&
git -C full-clone fetch --deepen=1 &&
git -C full-clone rev-parse --is-shallow-repository >actual &&
git -C full-clone log --oneline >>actual &&
test_cmp expect actual
'

. "$TEST_DIRECTORY"/lib-httpd.sh
start_httpd

Expand Down
83 changes: 83 additions & 0 deletions t/t7900-maintenance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ test_expect_success 'maintenance.auto config option' '
test_subcommand ! git maintenance run --auto --quiet --detach <false
'

test_expect_success 'gc.auto config option' '
GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
test_subcommand git maintenance run --auto --quiet --detach <default &&
GIT_TRACE2_EVENT="$(pwd)/true" \
git -c gc.auto=1 commit --quiet --allow-empty -m 2 &&
test_subcommand git maintenance run --auto --quiet --detach <true &&
GIT_TRACE2_EVENT="$(pwd)/false" \
git -c gc.auto=0 commit --quiet --allow-empty -m 3 &&
test_subcommand ! git maintenance run --auto --quiet --detach <false
'

test_expect_success 'maintenance.auto overrides gc.auto' '
test_when_finished "rm -f trace" &&

test_config maintenance.auto false &&
test_config gc.auto 1 &&
GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
test_subcommand ! git maintenance run --auto --quiet --detach <trace &&

test_config maintenance.auto true &&
test_config gc.auto 0 &&
GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
test_subcommand git maintenance run --auto --quiet --detach <trace
'

for cfg in maintenance.autoDetach gc.autoDetach
do
test_expect_success "$cfg=true config option" '
Expand Down Expand Up @@ -1509,6 +1534,64 @@ test_expect_success '--no-detach causes maintenance to not run in background' '
)
'

test_expect_success PIPE '--detach holds maintenance lock until daemonized child exits' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&

git config maintenance.auto false &&
git config core.lockfilepid true &&

git remote add origin /does/not/exist &&
git config set remote.origin.uploadpack "cat fifo-uploadpack" &&

mkfifo fifo-uploadpack fifo-maint &&

# Open the maintenance FIFO, as otherwise spawning
# git-maintenance(1) would block. Note that we need to open it
# as read-write, as otherwise we would block here already.
exec 9<>fifo-maint &&

{ git maintenance run --task=prefetch --detach 7>&9 & } &&
parent="$!" &&

# Reap the parent process so that the exec call below will not
# get SIGCHLD.
wait "$parent" &&

# Open the git-upload-pack(1) FIFO for writing, which will
# block until the upload-pack script opens it for reading. Once
# exec returns, we know that the daemonized child is alive and
# pinned.
exec 8>fifo-uploadpack &&

test_path_is_file .git/objects/maintenance.lock &&
test_path_is_file .git/objects/"maintenance~pid.lock" &&

# Verify that the maintenance.lock still exists, and
# that it was created by the parent process, not the
# child.
echo "pid $parent" >expect &&
test_cmp expect .git/objects/"maintenance~pid.lock" &&

# Reopen the maintenance FIFO as read-only so that
# git-maintenance(1) is the only writer. This will cause it to
# close the FIFO once the process exits.
exec 9<&- &&
exec 9<fifo-maint &&

# Close the FIFO used by git-upload-pack(1) to unblock it and
# then wait until the maintenance FIFO is closed by
# git-maintenance(1), indicating that it has exited.
exec 8>&- &&
cat <&9 &&

test_path_is_missing .git/objects/maintenance.lock &&
test_path_is_missing .git/objects/"maintenance~pid.lock"
)
'

test_expect_success '--detach causes maintenance to run in background' '
test_when_finished "rm -rf repo" &&
git init repo &&
Expand Down
12 changes: 12 additions & 0 deletions tempfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,15 @@ int delete_tempfile(struct tempfile **tempfile_p)

return err ? -1 : 0;
}

void reassign_tempfile_ownership(pid_t from, pid_t to)
{
volatile struct volatile_list_head *pos;

list_for_each(pos, &tempfile_list) {
struct tempfile *p = list_entry(pos, struct tempfile, list);

if (is_tempfile_active(p) && p->owner == from)
p->owner = to;
}
}
11 changes: 11 additions & 0 deletions tempfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,15 @@ int delete_tempfile(struct tempfile **tempfile_p);
*/
int rename_tempfile(struct tempfile **tempfile_p, const char *path);

/*
* Reassign ownership of all active tempfiles whose `owner` field matches
* `from` to `to`.
*
* This is intended for use by `daemonize()`; after `fork(2)`-ing, the parent
* transfers ownership to the daemonized child so that its atexit handler does
* not unlink tempfiles that should outlive it, and the child claims the
* inherited tempfiles so that they are cleaned up when the daemon exits.
*/
void reassign_tempfile_ownership(pid_t from, pid_t to);

#endif /* TEMPFILE_H */
Loading