//
// aegis - project change supervisor
// Copyright (C) 1991-2009, 2011, 2012, 2014 Peter Miller
// Copyright (C) 2008-2010 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
#include
#include
#include
#include
#include
#include
#include
#include
#include
static void
new_test_usage(void)
{
const char *progname;
progname = progname_get();
fprintf(stderr, "usage: %s -New_Test [ ... ]\n", progname);
fprintf(stderr, " %s -New_Test -List [ ... ]\n", progname);
fprintf(stderr, " %s -New_Test -Help\n", progname);
quit(1);
}
static void
new_test_help(void)
{
help("aent", new_test_usage);
}
static void
new_test_list(void)
{
trace(("new_test_list()\n{\n"));
arglex();
change_identifier cid;
cid.command_line_parse_rest(new_test_usage);
list_project_files(cid, 0);
trace(("}\n"));
}
static void
new_test_main(void)
{
trace(("\n{\n", __PRETTY_FUNCTION__));
change_identifier cid;
cstate_ty *cstate_data;
string_ty *s1;
string_ty *s2;
fstate_src_ty *src_data;
int manual_flag;
int automatic_flag;
long n;
size_t j;
size_t k;
int nerrs;
log_style_ty log_style;
const char *output_filename;
int use_template;
string_ty *uuid;
trace(("new_test_main()\n{\n"));
arglex();
manual_flag = 0;
automatic_flag = 0;
log_style = log_style_append_default;
string_list_ty wl;
output_filename = 0;
use_template = -1;
uuid = 0;
edit_ty edit = edit_not_set;
bool use_uuid = true;
while (arglex_token != arglex_token_eoln)
{
switch (arglex_token)
{
default:
generic_argument(new_test_usage);
continue;
case arglex_token_manual:
if (manual_flag)
duplicate_option(new_test_usage);
manual_flag = 1;
break;
case arglex_token_automatic:
if (automatic_flag)
duplicate_option(new_test_usage);
automatic_flag = 1;
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_usage);
continue;
case arglex_token_file:
if (arglex() != arglex_token_string)
new_test_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_nolog:
if (log_style == log_style_none)
duplicate_option(new_test_usage);
log_style = log_style_none;
break;
case arglex_token_wait:
case arglex_token_wait_not:
user_ty::lock_wait_argument(new_test_usage);
break;
case arglex_token_base_relative:
case arglex_token_current_relative:
user_ty::relative_filename_preference_argument(new_test_usage);
break;
case arglex_token_output:
if (output_filename)
duplicate_option(new_test_usage);
switch (arglex())
{
default:
option_needs_file(arglex_token_output, new_test_usage);
// NOTREACHED
case arglex_token_string:
output_filename = arglex_value.alv_string;
break;
case arglex_token_stdio:
output_filename = "";
break;
}
break;
case arglex_token_template:
case arglex_token_template_not:
if (use_template >= 0)
duplicate_option(new_test_usage);
use_template = (arglex_token == arglex_token_template);
break;
case arglex_token_uuid:
if (uuid)
duplicate_option(new_test_usage);
if (arglex () != arglex_token_string)
option_needs_string(arglex_token_uuid, new_test_usage);
s2 = str_from_c(arglex_value.alv_string);
if (!universal_unique_identifier_valid(s2))
option_needs_string(arglex_token_uuid, new_test_usage);
uuid = str_downcase(s2);
str_free(s2);
s2 = 0;
break;
case arglex_token_uuid_not:
use_uuid = false;
break;
case arglex_token_edit:
if (edit == edit_foreground)
duplicate_option(new_test_usage);
if (edit != edit_not_set)
{
too_many_edits:
mutually_exclusive_options
(
arglex_token_edit,
arglex_token_edit_bg,
new_test_usage
);
}
edit = edit_foreground;
break;
case arglex_token_edit_bg:
if (edit == edit_background)
duplicate_option(new_test_usage);
if (edit != edit_not_set)
goto too_many_edits;
edit = edit_background;
break;
}
arglex();
}
cid.command_line_check(new_test_usage);
if (cid.is_set() && output_filename)
{
mutually_exclusive_options
(
arglex_token_change,
arglex_token_output,
new_test_usage
);
}
if (automatic_flag && manual_flag)
{
mutually_exclusive_options
(
arglex_token_manual,
arglex_token_automatic,
new_test_usage
);
}
if (!manual_flag && !automatic_flag)
automatic_flag = 1;
//
// no-uuid conflicts with uuid
//
if (uuid && !use_uuid)
{
mutually_exclusive_options
(
arglex_token_uuid,
arglex_token_uuid_not,
new_test_usage
);
}
//
// It is an error if the -uuid switch is used and more that one
// file is nominated on the command line.
//
if (uuid && wl.size() > 1)
{
error_intl(0, i18n("too many files"));
new_test_usage();
}
//
// lock everything to avoid race conditions while searching the
// whole project for UUID already assigned to files with the same
// name. This include locking the project state file for test
// number.
//
cid.get_pp()->trunk_get()->lock_prepare_everything();
lock_take();
cstate_data = cid.get_cp()->cstate_get();
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 (cstate_data->state != cstate_state_being_developed)
change_fatal(cid.get_cp(), 0, i18n("bad nt state"));
if (cid.get_cp()->is_a_branch())
change_fatal(cid.get_cp(), 0, i18n("bad nf branch"));
if (nstring(cid.get_cp()->developer_name()) != cid.get_up()->name())
change_fatal(cid.get_cp(), 0, i18n("not developer"));
//
// It is an error if the UUID is already is use.
//
if (uuid)
{
fstate_src_ty *src;
src = cid.get_cp()->file_find_uuid(uuid, view_path_simple);
if (src)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "Other", src->file_name);
sub_var_optional(scp, "Other");
change_fatal(cid.get_cp(), scp, i18n("bad ca, uuid duplicate"));
// NOTREACHED
}
}
nerrs = 0;
if (wl.size())
{
nstring_list search_path;
//
// Search path for resolving filenames.
//
cid.get_cp()->search_path_get(search_path, true);
//
// Find the base for relative filenames.
//
nstring base = search_path_base_get(search_path, cid.get_up());
//
// make sure each file named is unique
//
string_list_ty wl2;
for (j = 0; j < wl.size(); ++j)
{
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].get_ref(), 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_fatal(cid.get_cp(), scp, i18n("$filename unrelated"));
// NOTREACHED
sub_context_delete(scp);
}
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);
++nerrs;
}
else
wl2.push_back(s2);
str_free(s2);
}
wl = wl2;
//
// ensure that each file
// 1. is not already part of the change
// - except removed files
// 2. is not already part of the baseline
//
for (j = 0; j < wl.size(); ++j)
{
s1 = wl[j];
src_data = cid.get_cp()->file_find(nstring(s1), view_path_first);
if (src_data)
{
sub_context_ty *scp;
switch (src_data->action)
{
case file_action_remove:
break;
case file_action_create:
case file_action_modify:
case file_action_insulate:
case file_action_transparent:
#ifndef DEBUG
default:
#endif
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", s1);
change_error(cid.get_cp(), scp, i18n("file $filename dup"));
sub_context_delete(scp);
++nerrs;
break;
}
}
else
{
src_data = cid.get_pp()->file_find(s1, view_path_extreme);
if (src_data)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", s1);
project_error
(
cid.get_pp(),
scp,
i18n("$filename in baseline")
);
sub_context_delete(scp);
++nerrs;
}
}
}
}
else
{
//
// Invent a new test file name.
//
// Try 1000 times, as users could, conceivably, use
// files of the same name.
//
s1 = 0;
for (j = 0; j < 1000; ++j)
{
n = project_next_test_number_get(cid.get_pp());
s1 = change_new_test_filename_get(cid.get_cp(), n, !manual_flag);
if
(
!cid.get_cp()->file_find(nstring(s1), view_path_first)
&&
!cid.get_pp()->file_find(s1, view_path_extreme)
)
break;
s1 = 0;
}
if (!s1)
fatal_intl(0, i18n("all test numbers used"));
wl.push_back(s1);
str_free(s1);
}
//
// check that each filename is OK
//
for (j = 0; j < wl.size(); ++j)
{
string_ty *e;
string_ty *file_name;
string_ty *other;
file_name = wl[j];
if (cid.get_cp()->file_is_config(file_name))
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", file_name);
change_error(cid.get_cp(), scp, i18n("may not test $filename"));
sub_context_delete(scp);
++nerrs;
}
other = change_file_directory_conflict(cid.get_cp(), file_name);
if (other)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", file_name);
sub_var_set_string(scp, "File_Name2", other);
sub_var_optional(scp, "File_Name2");
change_error
(
cid.get_cp(),
scp,
i18n("file $filename directory name conflict")
);
sub_context_delete(scp);
++nerrs;
continue;
}
other = project_file_directory_conflict(cid.get_pp(), file_name);
if (other)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "File_Name", file_name);
sub_var_set_string(scp, "File_Name2", other);
sub_var_optional(scp, "File_Name2");
project_error
(
cid.get_pp(),
scp,
i18n("file $filename directory name conflict")
);
sub_context_delete(scp);
++nerrs;
}
e = change_filename_check(cid.get_cp(), file_name);
if (e)
{
sub_context_ty *scp;
//
// no internationalization if the error string
// required, this is done inside the
// change_filename_check function.
//
scp = sub_context_new();
sub_var_set_string(scp, "MeSsaGe", e);
change_error(cid.get_cp(), scp, i18n("$message"));
sub_context_delete(scp);
++nerrs;
str_free(e);
}
}
if (nerrs)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_long(scp, "Number", nerrs);
sub_var_optional(scp, "Number");
change_fatal(cid.get_cp(), scp, i18n("new test failed"));
// NOTREACHED
sub_context_delete(scp);
}
//
// create the files
//
for (j = 0; j < wl.size(); ++j)
{
change_file_template(cid.get_cp(), wl[j], cid.get_up(), use_template);
}
//
// Add the files to the change.
//
nstring_list files(wl);
change_functor_file_find file_to_uuid(files);
project_inventory_walk(cid.get_pp()->trunk_get(), file_to_uuid);
for (j = 0; j < wl.size(); ++j)
{
s1 = wl[j];
//
// If the file is already in the change (we checked for this
// earlier) then it must be being removed, and we are replacing
// it, so we can change its type.
//
src_data = cid.get_cp()->file_find(nstring(s1), view_path_first);
if (src_data)
{
assert(src_data->action == file_action_remove);
if (src_data->uuid)
{
//
// If the file is removed we reuse the UUID raising an
// error if the user has specified the UUID on the
// command line.
//
// We cannot reuse the UUID if the file has been
// renamed because this lead to UUIDs duplications.
//
if (!src_data->move)
{
if (uuid)
duplicate_option_by_name
(
arglex_token_uuid,
new_test_usage
);
uuid = src_data->uuid;
src_data->uuid = 0;
}
}
cid.get_cp()->file_remove(s1);
}
src_data = cid.get_cp()->file_new(s1);
src_data->action = file_action_create;
if (manual_flag)
src_data->usage = file_usage_manual_test;
else
src_data->usage = file_usage_test;
if (uuid || use_uuid)
{
nstring *candidate_uuid = file_to_uuid.query(src_data->file_name);
if (uuid)
{
if
(
candidate_uuid
&&
!str_equal(candidate_uuid->get_ref(), uuid)
)
{
sub_context_ty *scp;
scp = sub_context_new();
sub_var_set_string(scp, "Other", src_data->file_name);
sub_var_optional(scp, "Other");
change_fatal
(
cid.get_cp(),
scp,
i18n("bad ca, uuid duplicate")
);
// NOTREACHED
}
src_data->uuid = uuid;
uuid = 0;
}
else
{
if (candidate_uuid)
{
trace_nstring(*candidate_uuid);
src_data->uuid = candidate_uuid->get_ref_copy();
}
else
src_data->uuid = universal_unique_identifier();
}
assert(universal_unique_identifier_valid(src_data->uuid));
}
else
{
//
// If the user asked to not have the UUID assigned to this
// file, make sure it is skipped also at aeipass time.
//
src_data->attribute = (attributes_list_ty *)
attributes_list_type.alloc();
attributes_list_append
(
src_data->attribute,
AEIPASS_ASSIGN_FILE_UUID,
"false"
);
}
}
//
// the number of files changed, or the version did,
// so stomp on the validation fields.
//
change_build_times_clear(cid.get_cp());
//
// The change now has at least one test, so cancel any testing
// exemption. (Cancel the baseline test exemption if this is
// not the first change on this branch.)
//
cstate_data->test_exempt = false;
if
(
project_change_nth(cid.get_pp(), 0L, &n)
&&
n != cid.get_change_number()
)
cstate_data->test_baseline_exempt = false;
//
// update the copyright years
//
change_copyright_years_now(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());
//
// If there is an output option,
// write the change number to the file.
//
if (output_filename)
{
string_ty *content = wl.unsplit("\n");
if (*output_filename)
{
string_ty *fn;
fn = str_from_c(output_filename);
user_ty::become scoped(cid.get_up());
file_from_string(fn, content, 0644);
str_free(fn);
}
else
cat_string_to_stdout(content);
str_free(content);
}
// 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();
//
// update the state files
// release the locks
//
project_pstate_write_top(cid.get_pp());
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_command(&wl, cid.get_up());
if (recent_integration)
cid.get_cp()->run_project_file_command(cid.get_up());
//
// verbose success message
//
for (j = 0; j < wl.size(); ++j)
{
sub_context_ty *scp;
scp = sub_context_new();
s1 = wl[j];
sub_var_set_string(scp, "File_Name", s1);
change_verbose(cid.get_cp(), scp, i18n("new test $filename complete"));
sub_context_delete(scp);
}
//
// Edit the files if asked.
//
if (edit != edit_not_set)
{
nstring dd(cid.get_cp()->development_directory_get(0));
nstring_list nfl(wl);
os_edit(nfl, edit, dd);
}
trace(("}\n"));
}
void
new_test(void)
{
static arglex_dispatch_ty dispatch[] =
{
{ arglex_token_help, new_test_help, 0 },
{ arglex_token_list, new_test_list, 0 },
};
trace(("new_test()\n{\n"));
arglex_dispatch(dispatch, SIZEOF(dispatch), new_test_main);
trace(("}\n"));
}
// vim: set ts=8 sw=4 et :