[BACK]Return to diff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

File: [local] / src / usr.bin / cvs / diff.c (download)

Revision 1.91, Sat May 27 03:30:30 2006 UTC (18 years ago) by joris
Branch: MAIN
Changes since 1.90: +119 -1677 lines

commit the new opencvs code, i have been hacking on
this for the past 2 weeks now and it should go in at
the start of the hackathon so others can help out.

this code is a lot safer, smarter, faster and best of
all it is actually doing what it is suppose to do!

basic checkout, update, status, diff and commit are
working in local mode only.
there is no support for any remote setups now.

/*	$OpenBSD: diff.c,v 1.91 2006/05/27 03:30:30 joris Exp $	*/
/*
 * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "includes.h"

#include "cvs.h"
#include "diff.h"
#include "log.h"
#include "proto.h"

int	cvs_diff(int, char **);
void	cvs_diff_local(struct cvs_file *);

struct cvs_cmd cvs_cmd_diff = {
	CVS_OP_DIFF, CVS_REQ_DIFF, "diff",
	{ "di", "dif" },
	"Show differences between revisions",
	"[-cilNnpu] [[-D date] [-r rev] [-D date2 | -r rev2]] "
	"[-k mode] [file ...]",
	"cD:iklNnpr:Ru",
	NULL,
	cvs_diff
};

int
cvs_diff(int argc, char **argv)
{
	int ch;
	char *arg = ".";
	struct cvs_recursion cr;

	strlcpy(diffargs, argv[0], sizeof(diffargs));

	while ((ch = getopt(argc, argv, cvs_cmd_diff.cmd_opts)) != -1) {
		switch (ch) {
		case 'c':
			strlcat(diffargs, " -c", sizeof(diffargs));
			diff_format = D_CONTEXT;
			break;
		case 'n':
			strlcat(diffargs, " -n", sizeof(diffargs));
			diff_format = D_RCSDIFF;
			break;
		case 'r':
			if (diff_rev1 == NULL) {
				diff_rev1 = rcsnum_parse(optarg);
				if (diff_rev1 == NULL)
					fatal("rcsnum_parse failed");
			} else if (diff_rev2 == NULL) {
				diff_rev2 = rcsnum_parse(optarg);
				if (diff_rev2 == NULL)
					fatal("rcsnum_parse failed");
			} else {
				fatal("no more than 2 revisions/dates can"
				    " be specified");
			}
			break;
		case 'u':
			strlcat(diffargs, " -u", sizeof(diffargs));
			diff_format = D_UNIFIED;
			break;
		default:
			fatal("%s", cvs_cmd_diff.cmd_synopsis);
		}
	}

	argc -= optind;
	argv += optind;

	cr.enterdir = NULL;
	cr.leavedir = NULL;
	cr.local = cvs_diff_local;
	cr.remote = NULL;

	if (argc > 0)
		cvs_file_run(argc, argv, &cr);
	else
		cvs_file_run(1, &arg, &cr);

	return (0);
}

void
cvs_diff_local(struct cvs_file *cf)
{
	size_t len;
	RCSNUM *r1;
	BUF *b1, *b2;
	struct stat st;
	struct timeval tv[2], tv2[2];
	char rbuf[16], p1[MAXPATHLEN], p2[MAXPATHLEN];

	cvs_log(LP_TRACE, "cvs_diff_local(%s)", cf->file_path);

	if (cf->file_type == CVS_DIR) {
		if (verbosity > 1)
			cvs_log(LP_NOTICE, "Diffing inside %s", cf->file_path);
		return;
	}

	cvs_file_classify(cf);

	if (cf->file_status == FILE_LOST) {
		cvs_log(LP_ERR, "cannot find file %s", cf->file_path);
		return;
	} else if (cf->file_status == FILE_UNKNOWN) {
		cvs_log(LP_ERR, "I know nothing about %s", cf->file_path);
		return;
	} else if (cf->file_status == FILE_UPTODATE && diff_rev2 == NULL)
		return;

	diff_file = cf->file_path;
	cvs_printf("Index: %s\n%s\nRCS file: %s\n", cf->file_path,
	    RCS_DIFF_DIV, cf->file_rpath);

	if (diff_rev1 != NULL)
		r1 = diff_rev1;
	else
		r1 = cf->file_ent->ce_rev;

	diff_rev1 = r1;
	rcsnum_tostr(r1, rbuf , sizeof(rbuf));
	cvs_printf("retrieving revision %s\n", rbuf);
	if ((b1 = rcs_getrev(cf->file_rcs, r1)) == NULL)
		fatal("failed to retrieve revision %s", rbuf);

	tv[0].tv_sec = rcs_rev_getdate(cf->file_rcs, r1);
	tv[0].tv_usec = 0;
	tv[1] = tv[0];

	if (diff_rev2 != NULL) {
		rcsnum_tostr(diff_rev2, rbuf, sizeof(rbuf));
		cvs_printf("retrieving revision %s\n", rbuf);
		if ((b2 = rcs_getrev(cf->file_rcs, diff_rev2)) == NULL)
			fatal("failed to retrieve revision %s", rbuf);

		tv2[0].tv_sec = rcs_rev_getdate(cf->file_rcs, diff_rev2);
		tv2[0].tv_usec = 0;
		tv2[1] = tv2[0];
	} else {
		if (fstat(cf->fd, &st) == -1)
			fatal("fstat failed %s", strerror(errno));
		if ((b2 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
			fatal("failed to load %s", cf->file_path);

		st.st_mtime = cvs_hack_time(st.st_mtime, 1);
		if (st.st_mtime == 0)
			fatal("cvs_diff_local: to gmt failed");

		tv2[0].tv_sec = st.st_mtime;
		tv2[0].tv_usec = 0;
		tv2[1] = tv2[0];
	}

	cvs_printf("%s", diffargs);

	rcsnum_tostr(r1, rbuf, sizeof(rbuf));
	cvs_printf(" -r%s", rbuf);

	if (diff_rev2 != NULL) {
		rcsnum_tostr(diff_rev2, rbuf, sizeof(rbuf));
		cvs_printf(" -r%s", rbuf);
	}

	cvs_printf(" %s\n", cf->file_path);

	len = strlcpy(p1, cvs_tmpdir, sizeof(p1));
	if (len >= sizeof(p1))
		fatal("cvs_diff_local: truncation");

	len = strlcat(p1, "/diff1.XXXXXXXXXX", sizeof(p1));
	if (len >= sizeof(p1))
		fatal("cvs_diff_local: truncation");

	cvs_buf_write_stmp(b1, p1, 0600, tv);
	cvs_buf_free(b1);

	len = strlcpy(p2, cvs_tmpdir, sizeof(p2));
	if (len >= sizeof(p2))
		fatal("cvs_diff_local: truncation");

	len = strlcat(p2, "/diff2.XXXXXXXXXX", sizeof(p2));
	if (len >= sizeof(p2))
		fatal("cvs_diff_local: truncation");

	cvs_buf_write_stmp(b2, p2, 0600, tv2);
	cvs_buf_free(b2);

	cvs_diffreg(p1, p2, NULL);
	cvs_worklist_run(&temp_files, cvs_worklist_unlink);
}