Login
Documentation
Login
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 
/* vim: set ts=2 et sw=2 tw=80: */
/*
** Copyright (c) 2013 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** 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.
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*******************************************************************************
*/

#include "fossil-scm/fossil-cli.h" /* Fossil App mini-framework */
#include "fossil-scm/fossil-internal.h"
#include <time.h>

#ifndef _WIN32
#include <unistd.h> /*isatty()*/
#endif

void fcli_local_help(){
  printf("Usage:\n\t%s [options] [filename]\n\n", fcli.appName);

  puts("Creates a new, empty repository db. Options:\n");

  puts("\t--config|-c=repo_db_file Copies parts of the "
       "configuration from the given repo.\n");
  
  puts("\t--message|-m=text Sets the commit message for the initial commit.\n");
  
  puts("\t--file|-f=repo_file_to_create. The filename may optionally "
       "be provided as the first non-flag parameter.\n");
  
}


static struct FNewApp_{
  char * comment;
  char * filename;
  char * configRepo;
} FNewApp = {
NULL/*comment*/,
NULL/*filename*/,
NULL/*configRepo*/
};

static int f_create_repo(){
  int rc;
  fsl_cx * f = fcli.f;
  fsl_repo_create_opt opt = fsl_repo_create_opt_empty;
  char const * mfile = FNewApp.filename;

  if(FNewApp.configRepo){
    f_out("Copying configuration from: %s\n", FNewApp.configRepo);
  }
  opt.filename = mfile;
  if(FNewApp.comment){
    opt.commitMessage = FNewApp.comment;
  }
  opt.configRepo = FNewApp.configRepo;
  opt.username = fcli.userName
    /* This one is actually implicit because of fcli-specific
       setup, but we'll show it for demonstration purposes */;
  rc = fsl_repo_create(f, &opt);
  if(!rc){
    fsl_db * db = fsl_cx_db_repo(f);
    fsl_stmt st = fsl_stmt_empty;
    char * s;
    f_out("Created repository: %s\n", mfile);
    assert(db);
#define CONF(KEY) s = fsl_repo_g_text( f, KEY, NULL); \
    f_out("%-15s= %s\n", KEY, s); \
    fsl_free(s)
    CONF("server-code");
    CONF("project-code");
#undef CONF
    rc = fsl_db_prepare(db, &st,
                        "SELECT login,pw FROM user WHERE uid=1");
    assert(!rc);
    rc = fsl_stmt_step(&st);
    assert(FSL_RC_STEP_ROW==rc);
    if(FSL_RC_STEP_ROW==rc){
      rc = 0;
      f_out("%-15s= %s (password=%s)\n",
             "admin-user",
             fsl_stmt_g_text(&st, 0, NULL),
             fsl_stmt_g_text(&st, 1, NULL)
             );
    }
    fsl_stmt_finalize(&st);
    if(db->error.code){
      fsl_cx_uplift_db_error(f, db);
    }
  }
  fcli_err_report(0);
  return rc;

}


int main(int argc, char * const * argv ){
  int rc = 0;
  fsl_cx * f;
  fcli.checkoutDir = NULL /* Same effect as -C/--no-checkout */;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */ return 0;
  else if(rc) goto end;
  f = fcli.f;
  assert(!fsl_cx_db_repo(f));
  fcli_flag2("c", "config", &FNewApp.configRepo);
  fcli_flag2("m", "message", &FNewApp.comment);
  fcli_flag2("f", "file", &FNewApp.filename);
  if(!FNewApp.filename){
    FNewApp.filename = fcli_next_arg(1);
    if(!FNewApp.filename){
      rc = fcli_err_set(FSL_RC_MISUSE,
                             "Missing filename argument. "
                             "Try --help.");
      goto end;
    }
  }

  if(fcli_has_unused_flags(0)) goto end;

  rc = f_create_repo();
  end:
  fsl_free(FNewApp.comment);
  fsl_free(FNewApp.filename);
  fsl_free(FNewApp.configRepo);
  return fcli_err_report(1) ? EXIT_FAILURE : EXIT_SUCCESS;
}