2

BRIEF:

hg clone creates path "default" in /.hg/hgrc, set to where you cloned from.

Q: is it possible to disable this automatically?

DETAIL:

This is already partially answered.

In Can you prevent default-push, but allow pull? we see how to set default-push, in some hgrc file such as /.hg/hgrc, or (my preference), in ~/.hgrc

In Is hg clone equivalent to hg (init→pull) Tim Henigan says that hg clone = init; pull; hg update default; setting up default path in /.hg/hgrc.

Although elsewhere we see that this is not quite true. hg clone may differ, e.g., in that it does hard link sharing. Lacking an official statement of equivalence...

Now, disabling default-push helps a lot.

But... I have fallen into the habit of doing "hg push default". Which somewhat defaets the p;urpose.

By the way: reason I am doing this, wanting to disable the default: workflow that goes master->workspace->staging_area->master. I do many clones of the master. Modifying /.hg/hgrc to edit [path] default each time I do a cline is a pain. Doing "hg push" or "hg push default" in any of the workspaces can be bad. Instead I need to push to the staging area, ad only from the staging area am I allowed to push to the master.

(I have tried master<->staging_area<->workspace, i.e. always cloning from the sdtaging area. But I found this confusing. Plus, the part that I haven't said yet: my project makes me delete or collapse history, which adds an additional level of confusion and error-prone-ness.)

Community
  • 1
  • 1
Krazy Glew
  • 7,210
  • 2
  • 49
  • 62
  • By the way, I am writing a "my-clone" perl script right now - although it is remarkable how hard it is to make it robust abd isomorphic to the existig clone.) – Krazy Glew Jun 23 '12 at 21:09

1 Answers1

0

Here's the post-clone hook I came up with:

 [hooks]
 post-clone = perl ~/bin/delete-default-from-.hgrc "$HG_PATS"

and the perl script is below.

I'd still like to find a one-liner.

#!/usr/local/bin/perl
use strict;

print "editing <repo>/.hg/hgrc to to remove [paths] / default";

# Hand tested
# cd ~/mips/uarch-perf-sim/psim+stuff/f; rm -rf k; hg clone ../m k
# cd ~/mips/uarch-perf-sim/psim+stuff/f; rm -rf k; hg clone ../m

my $debug = 0;


my $hg_pats = $ARGV[0];
die "Usage: delete-default-from-.hgrc HG_PATS (from post-clone hook)\n" if !exists $ARGV[0] || exists $ARGV[2];


# expect HG_PATS from post-clone hook

#['../m']
#['../m', 'target']"
#['../m', '../target']"

my $from;
my $target;

if( $hg_pats =~ m/^\['([^']+)'\]$/ ) {
    $from = $1;
    $target = $from;
    # delete paths if target implicit
    $target =~ s{.*/}{};
    print "from-only: $target\n" if $debug;
} elsif( $hg_pats =~ m/^\['([^']+)',\s+'([^']+)'\]$/ ) {
    $from = $1;
    $target = $2;
    # do NOT delete paths if target explicit
    print "from to: $target\n" if $debug;
} else {
    die "expected HG_PATS, got: $hg_pats\n";
}

die "clone target not found: $target\n" if ! -d $target;

my $hgrc = "$target/.hg/hgrc";
die "clone target/.hg/hgrc not found: $hgrc\n" if ! -r "$hgrc";

open( my $in, "<$hgrc" ) || die "could not open $hgrc to read (edit)\n";
unlink "$hgrc.tmp";
open( my $out, ">$hgrc.tmp" ) || die "could not open $hgrc.tmp to write\n";

my $section = '';
my $paths_found;
my $paths_default_found;
while( my $line = <$in> ) {
    chomp($line);
    print "line = $line\n" if $debug;

    if( $line =~ m/^\[paths\]/ ) {
    $section = "[paths]";
        $paths_found++;
    }
    elsif( $line =~ m/^\[/ ) {
    $section = $line;
    }
    elsif( ($section eq "[paths]") and ($line =~ m/^\s*default\s*=/) ) {
    $line =~ s/default/default-cloned-from/;
        $paths_default_found++;
    }
    print $out $line."\n";
}

die "[paths] section not found in $hgrc" if ! $paths_found;
die "[paths] / default not found in $hgrc" if ! $paths_default_found;

system("echo '<diff edited $hgrc>';diff -U10 $hgrc $hgrc.tmp; echo '</diff>'");


unlink "$hgrc.orig";
die "could not unlink older $hgrc.orig" if -e "$hgrc.orig";

rename "$hgrc","$hgrc.orig" || die "could not rename $hgrc to $hgrc.orig";

rename "$hgrc.tmp","$hgrc" || die "could not rename $hgrc.tmp to $hgrc";

system("echo '$hgrc:';cat $hgrc");

exit(0);
Krazy Glew
  • 7,210
  • 2
  • 49
  • 62
  • I looked everywhere for a solution to my stackoverflow question (44584264): " post-clone hook to copy files into newly cloned repo" but did not find anything. It seems to me that your script could be tuned to achieve what I want. But I can't honestly figure what needs to be done since I am not proficient in Perl. Would appreciate your help. – SFbay007 Jun 20 '17 at 07:04