/* file-diffs.c: 
 *
 ****************************************************************
 * Copyright (C) 2003 Tom Lord
 * 
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */


#include "hackerlab/fs/file-names.h"
#include "hackerlab/char/str.h"
#include "hackerlab/vu/safe.h"
#include "file-utils/libfsutils/diffs.h"
#include "file-utils/libarch/cached-inventory.h"
#include "file-utils/libarch/local-cache.h"
#include "file-utils/libarch/inv-tags.h"
#include "file-utils/libarch/file-diffs.h"



int
arch_file_diffs (int out_fd, t_uchar * tree_root, t_uchar * mod_loc, t_uchar * archive, t_uchar * revision)
{
  t_uchar * mod_path = 0;
  t_uchar * tag = 0;
  t_uchar * cached_tree = 0;
  rel_table index = 0;
  int x;
  t_uchar * orig_loc = 0;
  t_uchar * orig_path = 0;
  int status = 2;

  mod_path = file_name_in_vicinity (0, tree_root, mod_loc);
  tag = arch_inventory_tag (arch_unspecified_tagging, mod_path);
  
  cached_tree = arch_find_local_copy (tree_root, archive, revision);
  if (!cached_tree)
    {
      safe_printfmt (2, "arch_file_diffs: tree not found in cache for comparison -- %s/%s\n", archive, revision);
    }

  index = arch_cached_index (cached_tree);
  
  for (x = 0; x < rel_n_records (index); ++x)
    {
      if (!str_cmp (tag, index[x][1]))
        {
          orig_loc = str_save (0, index[x][0]);
          orig_path = file_name_in_vicinity (0, cached_tree, index[x][0]);
          break;
        }
    }

  if (!orig_path)
    {
      safe_printfmt (out_fd, "new file (not present in %s/%s)\n", archive, revision);
      status = 1;
    }
  else
    {
      status = invoke_diff (out_fd, orig_path, orig_loc, mod_path, mod_loc);
    }


  lim_free (0, mod_path);
  lim_free (0, tag);
  lim_free (0, cached_tree);
  rel_free_table (index);
  lim_free (0, orig_loc);
  lim_free (0, orig_path);

  return status;
}



/* tag: Tom Lord Fri May 30 20:19:00 2003 (file-diffs.c)
 */
