Login
f-adiff.c at [8d1ab6166d]
Login

File f-apps/f-adiff.c artifact 153fa5f803 part of check-in 8d1ab6166d


/* -*- 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/
  
  *****************************************************************************
   This file implements a basic 'ls' for in-repo content
   (not yet filesystem-level).
*/

#include "fossil-scm/fossil-cli.h" /* Fossil App mini-framework */
#include <stdlib.h> /* atoi() */


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

  puts("Generates diff of individual blobs (not checkin versions!). Options:");
  puts("\n\t--context-lines|-c=### Specifies the number of context lines around differences.\n");
  puts("\n\t--html|-h Specifies HTML-format output, marked for styling via CSS.\n");
  puts("\n\t--invert|-v Inverts the to/from arguments for diff purposes.\n");
  puts("\n\t--sbs-width|-w=### specifies a side-by-side diff with the given column width.\n");
  puts("\nResults may be weird if specifying conflicting options (-i and -w).\n");
}

struct ADiffOpt {
  int diffFlags;
  short sbsWidth;
  short contextLines;
};
typedef struct ADiffOpt ADiffOpt;

/**
   
 */
static int f_adiff(fsl_id_t v1, fsl_id_t v2, const ADiffOpt * opt){
  int rc = 0;
  fsl_cx * f = fcli_cx();
  fsl_buffer lhs = fsl_buffer_empty;
  fsl_buffer rhs = fsl_buffer_empty;

  rc = fsl_content_get(f, v1, &lhs);
  if(rc) return rc;

  rc = fsl_content_get(f, v2, &rhs);
  if(rc) return rc;

  rc = fsl_diff_text(&lhs, &rhs, fsl_output_f_FILE, stdout,
                     opt->contextLines, opt->sbsWidth,
                     opt->diffFlags);
  fsl_buffer_clear(&lhs);
  fsl_buffer_clear(&rhs);
  return rc;
}

int main(int argc, char * const * argv ){
  int rc = 0;
  char * vFrom = NULL;
  char * vTo = NULL;
  char * tmpVal = NULL;
  fsl_cx * f;
  fsl_id_t idFrom = 0, idTo = 0;
  ADiffOpt diffOpt = {
  0/*diffFlags*/,
  0/*sbsWidth*/,
  0/*contextLines*/
  };

  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */ return 0;
  else if(rc) goto end;

  /* Set up/validate args... */
  f = fcli.f;
  if(!fsl_cx_db_repo(f)){
    rc = fcli_err_set(FSL_RC_NOT_A_REPO,
                      "Requires a repository db. See --help.");
    goto end;
  }

  if(fcli_flag2("v", "invert", NULL)){
    diffOpt.diffFlags |= FSL_DIFF_INVERT;
  }
  if(fcli_flag2("l", "line-numbers", NULL)){
    diffOpt.diffFlags |= FSL_DIFF_LINENO;
  }
  if(fcli_flag2("h", "html", NULL)){
    diffOpt.diffFlags |= FSL_DIFF_HTML;
  }
  if(fcli_flag2("w", "sbs-width", &tmpVal)){
    diffOpt.diffFlags |= FSL_DIFF_SIDEBYSIDE;
    diffOpt.sbsWidth = atoi(tmpVal);
    fsl_free(tmpVal);
    tmpVal = NULL;
  }
  if(fcli_flag2("c", "context-lines", &tmpVal)){
    diffOpt.contextLines = atoi(tmpVal);
    fsl_free(tmpVal);
    tmpVal = NULL;
  }

  if( fcli_flag_or_arg( "v1", "from", &vFrom) ){
    fcli_flag_or_arg( "v2", "to", &vTo);
  }

  if(fcli_has_unused_flags(0)) goto end;

  if(!vFrom || !vTo){
    rc = fcli_err_set(FSL_RC_MISUSE, "Both of -v1 UUID and -v2 UUID are required.");
    goto end;
  }

  rc = fsl_sym_to_rid(f, vFrom, FSL_CATYPE_ANY, &idFrom);
  if(!rc){
    rc = fsl_sym_to_rid(f, vTo, FSL_CATYPE_ANY, &idTo);
  }
  if(rc) goto end;

  rc = f_adiff( idFrom, idTo, &diffOpt );

  end:
  fsl_free(vFrom);
  fsl_free(vTo);
  return fcli_end_of_main(rc);
}