Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/commands.py @ 3670:d2d8d23944a9
commands.docopy: pay attention on whether paths use "/" or os.sep
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Wed, 15 Nov 2006 19:18:57 -0200 |
parents | b984dcb1df71 |
children | e8730b5b8a32 |
comparison
equal
deleted
inserted
replaced
3669:48768b1ab23c | 3670:d2d8d23944a9 |
---|---|
827 except ValueError, inst: | 827 except ValueError, inst: |
828 raise util.Abort(str(inst)) | 828 raise util.Abort(str(inst)) |
829 | 829 |
830 def docopy(ui, repo, pats, opts, wlock): | 830 def docopy(ui, repo, pats, opts, wlock): |
831 # called with the repo lock held | 831 # called with the repo lock held |
832 # | |
833 # hgsep => pathname that uses "/" to separate directories | |
834 # ossep => pathname that uses os.sep to separate directories | |
832 cwd = repo.getcwd() | 835 cwd = repo.getcwd() |
833 errors = 0 | 836 errors = 0 |
834 copied = [] | 837 copied = [] |
835 targets = {} | 838 targets = {} |
836 | 839 |
840 # abs: hgsep | |
841 # rel: ossep | |
842 # return: hgsep | |
837 def okaytocopy(abs, rel, exact): | 843 def okaytocopy(abs, rel, exact): |
838 reasons = {'?': _('is not managed'), | 844 reasons = {'?': _('is not managed'), |
839 'a': _('has been marked for add'), | 845 'a': _('has been marked for add'), |
840 'r': _('has been marked for remove')} | 846 'r': _('has been marked for remove')} |
841 state = repo.dirstate.state(abs) | 847 state = repo.dirstate.state(abs) |
848 if exact: | 854 if exact: |
849 ui.warn(_('%s: not copying - file %s\n') % (rel, reason)) | 855 ui.warn(_('%s: not copying - file %s\n') % (rel, reason)) |
850 else: | 856 else: |
851 return abs | 857 return abs |
852 | 858 |
859 # origsrc: hgsep | |
860 # abssrc: hgsep | |
861 # relsrc: ossep | |
862 # target: ossep | |
853 def copy(origsrc, abssrc, relsrc, target, exact): | 863 def copy(origsrc, abssrc, relsrc, target, exact): |
854 abstarget = util.canonpath(repo.root, cwd, target) | 864 abstarget = util.canonpath(repo.root, cwd, target) |
855 reltarget = util.pathto(cwd, abstarget) | 865 reltarget = util.pathto(cwd, abstarget) |
856 prevsrc = targets.get(abstarget) | 866 prevsrc = targets.get(abstarget) |
857 if prevsrc is not None: | 867 if prevsrc is not None: |
858 ui.warn(_('%s: not overwriting - %s collides with %s\n') % | 868 ui.warn(_('%s: not overwriting - %s collides with %s\n') % |
859 (reltarget, abssrc, prevsrc)) | 869 (reltarget, util.localpath(abssrc), |
870 util.localpath(prevsrc))) | |
860 return | 871 return |
861 if (not opts['after'] and os.path.exists(reltarget) or | 872 if (not opts['after'] and os.path.exists(reltarget) or |
862 opts['after'] and repo.dirstate.state(abstarget) not in '?r'): | 873 opts['after'] and repo.dirstate.state(abstarget) not in '?r'): |
863 if not opts['force']: | 874 if not opts['force']: |
864 ui.warn(_('%s: not overwriting - file exists\n') % | 875 ui.warn(_('%s: not overwriting - file exists\n') % |
897 targets[abstarget] = abssrc | 908 targets[abstarget] = abssrc |
898 if abstarget != origsrc and not opts.get('dry_run'): | 909 if abstarget != origsrc and not opts.get('dry_run'): |
899 repo.copy(origsrc, abstarget, wlock) | 910 repo.copy(origsrc, abstarget, wlock) |
900 copied.append((abssrc, relsrc, exact)) | 911 copied.append((abssrc, relsrc, exact)) |
901 | 912 |
913 # pat: ossep | |
914 # dest ossep | |
915 # srcs: list of (hgsep, hgsep, ossep, bool) | |
916 # return: function that takes hgsep and returns ossep | |
902 def targetpathfn(pat, dest, srcs): | 917 def targetpathfn(pat, dest, srcs): |
903 if os.path.isdir(pat): | 918 if os.path.isdir(pat): |
904 abspfx = util.canonpath(repo.root, cwd, pat) | 919 abspfx = util.canonpath(repo.root, cwd, pat) |
920 abspfx = util.localpath(abspfx) | |
905 if destdirexists: | 921 if destdirexists: |
906 striplen = len(os.path.split(abspfx)[0]) | 922 striplen = len(os.path.split(abspfx)[0]) |
907 else: | 923 else: |
908 striplen = len(abspfx) | 924 striplen = len(abspfx) |
909 if striplen: | 925 if striplen: |
910 striplen += len(os.sep) | 926 striplen += len(os.sep) |
911 res = lambda p: os.path.join(dest, p[striplen:]) | 927 res = lambda p: os.path.join(dest, util.localpath(p)[striplen:]) |
912 elif destdirexists: | 928 elif destdirexists: |
913 res = lambda p: os.path.join(dest, os.path.basename(p)) | 929 res = lambda p: os.path.join(dest, |
930 os.path.basename(util.localpath(p))) | |
914 else: | 931 else: |
915 res = lambda p: dest | 932 res = lambda p: dest |
916 return res | 933 return res |
917 | 934 |
935 # pat: ossep | |
936 # dest ossep | |
937 # srcs: list of (hgsep, hgsep, ossep, bool) | |
938 # return: function that takes hgsep and returns ossep | |
918 def targetpathafterfn(pat, dest, srcs): | 939 def targetpathafterfn(pat, dest, srcs): |
919 if util.patkind(pat, None)[0]: | 940 if util.patkind(pat, None)[0]: |
920 # a mercurial pattern | 941 # a mercurial pattern |
921 res = lambda p: os.path.join(dest, os.path.basename(p)) | 942 res = lambda p: os.path.join(dest, |
943 os.path.basename(util.localpath(p))) | |
922 else: | 944 else: |
923 abspfx = util.canonpath(repo.root, cwd, pat) | 945 abspfx = util.canonpath(repo.root, cwd, pat) |
924 if len(abspfx) < len(srcs[0][0]): | 946 if len(abspfx) < len(srcs[0][0]): |
925 # A directory. Either the target path contains the last | 947 # A directory. Either the target path contains the last |
926 # component of the source path or it does not. | 948 # component of the source path or it does not. |
927 def evalpath(striplen): | 949 def evalpath(striplen): |
928 score = 0 | 950 score = 0 |
929 for s in srcs: | 951 for s in srcs: |
930 t = os.path.join(dest, s[0][striplen:]) | 952 t = os.path.join(dest, util.localpath(s[0])[striplen:]) |
931 if os.path.exists(t): | 953 if os.path.exists(t): |
932 score += 1 | 954 score += 1 |
933 return score | 955 return score |
934 | 956 |
957 abspfx = util.localpath(abspfx) | |
935 striplen = len(abspfx) | 958 striplen = len(abspfx) |
936 if striplen: | 959 if striplen: |
937 striplen += len(os.sep) | 960 striplen += len(os.sep) |
938 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])): | 961 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])): |
939 score = evalpath(striplen) | 962 score = evalpath(striplen) |
940 striplen1 = len(os.path.split(abspfx)[0]) | 963 striplen1 = len(os.path.split(abspfx)[0]) |
941 if striplen1: | 964 if striplen1: |
942 striplen1 += len(os.sep) | 965 striplen1 += len(os.sep) |
943 if evalpath(striplen1) > score: | 966 if evalpath(striplen1) > score: |
944 striplen = striplen1 | 967 striplen = striplen1 |
945 res = lambda p: os.path.join(dest, p[striplen:]) | 968 res = lambda p: os.path.join(dest, |
969 util.localpath(p)[striplen:]) | |
946 else: | 970 else: |
947 # a file | 971 # a file |
948 if destdirexists: | 972 if destdirexists: |
949 res = lambda p: os.path.join(dest, os.path.basename(p)) | 973 res = lambda p: os.path.join(dest, |
974 os.path.basename(util.localpath(p))) | |
950 else: | 975 else: |
951 res = lambda p: dest | 976 res = lambda p: dest |
952 return res | 977 return res |
953 | 978 |
954 | 979 |