//
// aegis - project change supervisor
// Copyright (C) 1991-1999, 2001-2009, 2011, 2012, 2014 Peter Miller
// Copyright (C) 2008, 2009 Walter Franzini
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static void
new_test_undo_usage(void)
{
const char *progname;
progname = progname_get();
fprintf
(
stderr,
"usage: %s -New_Test_Undo ... [ ... ]\n",
progname
);
fprintf
(
stderr,
" %s -New_Test_Undo -List [ ... ]\n",
progname
);
fprintf(stderr, " %s -New_Test_Undo -Help\n", progname);
quit(1);
}
static void
new_test_undo_help(void)
{
help("aentu", new_test_undo_usage);
}
static void
new_test_undo_list(void)
{
trace(("new_test_undo_list()\n{\n"));
arglex();
change_identifier cid;
cid.command_line_parse_rest(new_test_undo_usage);
list_change_files(cid, 0);
trace(("}\n"));
}
static int
aentu_candidate(fstate_src_ty *src)
{
if (src->move)
return 0;
switch (src->action)
{
case file_action_create:
switch (src->usage)
{
case file_usage_test:
case file_usage_manual_test:
return 1;
case file_usage_source:
case file_usage_config:
case file_usage_build:
break;
}
break;
case file_action_modify:
case file_action_remove:
case file_action_insulate:
case file_action_transparent:
break;
}
return 0;
}
static void
new_test_undo_main(void)
{
trace(("%s\n{\n", __PRETTY_FUNCTION__));
change_identifier cid;
size_t k;
string_ty *s1;
string_ty *s2;
int number_of_errors;
string_list_ty search_path;
log_style_ty log_style;
arglex();
log_style = log_style_append_default;
string_list_ty wl;
while (arglex_token != arglex_token_eoln)
{
switch (arglex_token)
{
default:
generic_argument(new_test_undo_usage);
continue;
case arglex_token_file:
case arglex_token_directory:
if (arglex() != arglex_token_string)
new_test_undo_usage();
// fall through...
case arglex_token_string:
s2 = str_from_c(arglex_value.alv_string);
wl.push_back(s2);
str_free(s2);
break;
case arglex_token_keep:
case arglex_token_interactive:
case arglex_token_keep_not:
user_ty::delete_file_argument(new_test_undo_usage);
break;
case arglex_token_baseline:
case arglex_token_branch:
case arglex_token_change:
case arglex_token_delta:
case arglex_token_delta_date:
case arglex_token_delta_from_change:
case arglex_token_development_directory:
case arglex_token_grandparent:
case arglex_token_number:
case arglex_token_project:
case arglex_token_trunk:
cid.command_line_parse(new_test_undo_usage);
continue;
case arglex_token_nolog:
if (log_style == log_style_none)
duplicate_option(new_test_undo_usage);
log_style = log_style_none;
break;
case arglex_token_wait:
case arglex_token_wait_not:
user_ty::lock_wait_argument(new_test_undo_usage);
break;
case arglex_token_base_relative:
case arglex_token_current_relative:
user_ty::relative_filename_preference_argument(new_test_undo_usage);
break;
case arglex_token_symbolic_links:
case arglex_token_symbolic_links_not:
user_ty::symlink_pref_argument(new_test_undo_usage);
break;
}
arglex();
}
cid.command_line_check(new_test_undo_usage);
if (!wl.size())
{
error_intl(0, i18n("no file names"));
new_test_undo_usage();
}
//
// lock the change file
//
change_cstate_lock_prepare(cid.get_cp());
lock_take();
log_open(cid.get_cp()->logfile_get(), cid.get_up(), log_style);
//
// It is an error if the change is not in the in_development state.
// It is an error if the change is not assigned to the current user.
//
if (!cid.get_cp()->is_being_developed())
change_fatal(cid.get_cp(), 0, i18n("bad ntu state"));
if (cid.get_cp()->is_a_branch())
change_fatal(cid.get_cp(), 0, i18n("bad nfu branch"));
if (nstring(cid.get_cp()->developer_name()) != cid.get_up()->name())
change_fatal(cid.get_cp(), 0, i18n("not developer"));
//
// search path for resolving filenames
//
cid.get_cp()->search_path_get(&search_path, true);
//
// Find the base for relative filenames.
//
nstring_list n_search_path(search_path);
nstring base = search_path_base_get(n_search_path, cid.get_up());
//
// resolve the path of each file
// 1. the absolute path of the file name is obtained
// 2. if the file is inside the development directory, ok
// 3. if the file is inside the baseline, ok
// 4. if neither, error
//
number_of_errors = 0;
string_list_ty wl2;
for (size_t j = 0; j < wl.size(); ++j)
{
string_list_ty wl_in;
s1 = wl[j];
if (s1->str_text[0] == '/')
s2 = str_copy(s1);
else
s2 = os_path_join(base.get_ref(), s1);
cid.get_up()->become_begin();
s1 = os_pathname(s2, 1);
cid.get_up()->become_end();
str_free(s2);
s2 = 0;
for (k = 0; k < search_path.size(); ++k)
{
s2 = os_below_dir(search_path[k], s1);
if (s2)
break;
}
str_free(s1);
if (!s2)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", wl[j]);
change_error(cid.get_cp(), scp, i18n("$filename unrelated"));
sub_context_delete(scp);
++number_of_errors;
continue;
}
change_file_directory_query(cid.get_cp(), s2, &wl_in, 0);
if (wl_in.size())
{
int used;
//
// If the user named a directory, add all of the
// source files in that directory, provided they
// are not already in the change.
//
used = 0;
for (k = 0; k < wl_in.size(); ++k)
{
string_ty *s3;
fstate_src_ty *src_data;
s3 = wl_in[k];
src_data =
cid.get_cp()->file_find(nstring(s3), view_path_first);
assert(src_data);
if (!src_data)
continue;
if (aentu_candidate(src_data))
{
if (wl2.member(s3))
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", s3);
change_error
(
cid.get_cp(),
scp,
i18n("too many $filename")
);
sub_context_delete(scp);
++number_of_errors;
}
else
wl2.push_back(s3);
++used;
}
}
if (!used)
{
sub_context_ty *scp;
scp = sub_context_new();
if (s2->str_length)
sub_var_set_string(scp, "File_Name", s2);
else
sub_var_set_charstar(scp, "File_Name", ".");
sub_var_set_long(scp, "Number", (long)wl_in.size());
sub_var_optional(scp, "Number");
change_error
(
cid.get_cp(),
scp,
i18n("directory $filename contains no relevant files")
);
sub_context_delete(scp);
++number_of_errors;
}
}
else
{
if (wl2.member(s2))
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", s2);
change_error(cid.get_cp(), scp, i18n("too many $filename"));
sub_context_delete(scp);
++number_of_errors;
}
else
wl2.push_back(s2);
}
str_free(s2);
}
wl = wl2;
//
// ensure that each file
// 1. is already part of the change
// 2. is being created by this change
//
for (size_t j = 0; j < wl.size(); ++j)
{
fstate_src_ty *src_data;
s1 = wl[j];
src_data = cid.get_cp()->file_find(nstring(s1), view_path_first);
if (!src_data)
{
src_data =
cid.get_cp()->file_find_fuzzy(nstring(s1), view_path_first);
if (src_data)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", s1);
sub_var_set_string(scp, "Guess", src_data->file_name);
change_error
(
cid.get_cp(),
scp,
i18n("no $filename, closest is $guess")
);
sub_context_delete(scp);
}
else
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", s1);
change_error(cid.get_cp(), scp, i18n("no $filename"));
sub_context_delete(scp);
}
++number_of_errors;
continue;
}
if (!aentu_candidate(src_data))
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", s1);
change_error(cid.get_cp(), scp, i18n("bad nt undo $filename"));
sub_context_delete(scp);
++number_of_errors;
}
}
if (number_of_errors)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_long(scp, "Number", number_of_errors);
sub_var_optional(scp, "Number");
change_fatal(cid.get_cp(), scp, i18n("new test undo fail"));
}
//
// Remove each file from the development directory,
// if it still exists.
//
for (size_t j = 0; j < wl.size(); ++j)
{
int exists;
s1 = wl[j];
s2 = cid.get_cp()->file_path(s1);
assert(s2);
cid.get_up()->become_begin();
exists = os_exists(s2);
cid.get_up()->become_end();
if (exists && cid.get_up()->delete_file_query(nstring(s1), false, -1))
{
//
// This is not as robust in the face of errors as using
// commit. Its merit is its simplicity.
//
// It plays nice with the change_maintain_symlinks_to_basline
// call, below.
//
// Also, the rename-and-delete shenanigans take a long time
// over NFS, and users expect this to be fast.
//
user_ty::become scoped(cid.get_up());
os_unlink_errok(s2);
}
//
// Always unlink the difference file.
//
s1 = str_format("%s,D", s2->str_text);
str_free(s2);
s2 = s1;
user_ty::become scoped(cid.get_up());
if (os_exists(s2))
commit_unlink_errok(s2);
str_free(s2);
}
//
// Remove each file to the change file,
// and write it back out.
//
for (size_t j = 0; j < wl.size(); ++j)
cid.get_cp()->file_remove(wl[j]);
//
// the number of files changed, or the version did,
// so stomp on the validation fields.
//
change_build_times_clear(cid.get_cp());
//
// If the file manifest of the change is altered (e.g. by aenf, aenfu,
// aecp, aecpu, etc), or the contents of any file is changed, the
// UUID is cleared. This is because it is no longer the same change
// as was received by aedist or aepatch, and the UUID is invalidated.
//
change_uuid_clear(cid.get_cp());
//
// Maintain the symlinks (etc) to the baseline.
//
change_maintain_symlinks_to_baseline(cid.get_cp(), cid.get_up());
// remember we are about to
bool recent_integration = cid.get_cp()->run_project_file_command_needed();
if (recent_integration)
cid.get_cp()->run_project_file_command_done();
//
// release the locks
//
cid.get_cp()->cstate_write();
commit();
lock_release();
//
// run the change file command
// and the project file command if necessary
//
cid.get_cp()->run_new_test_undo_command(&wl, cid.get_up());
if (recent_integration)
cid.get_cp()->run_project_file_command(cid.get_up());
//
// verbose success message
//
for (size_t j = 0; j < wl.size(); ++j)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", wl[j]);
change_verbose
(
cid.get_cp(),
scp,
i18n("new test undo $filename complete")
);
sub_context_delete(scp);
}
trace(("}\n"));
}
void
new_test_undo(void)
{
static arglex_dispatch_ty dispatch[] =
{
{ arglex_token_help, new_test_undo_help, 0 },
{ arglex_token_list, new_test_undo_list, 0 },
};
trace(("new_test_undo()\n{\n"));
arglex_dispatch(dispatch, SIZEOF(dispatch), new_test_undo_main);
trace(("}\n"));
}
// vim: set ts=8 sw=4 et :