Differences between revisions 1 and 2
Revision 1 as of 2008-11-19 15:38:18
Size: 19367
Comment: Translate TutorialMerge to Thai
Revision 2 as of 2008-11-19 15:40:23
Size: 19371
Comment: Changed tutorial index link to Thai index
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
''(หน้านี้เป็นหน้าที่ 7 จาก 9 ของ[:Tutorial:บทเรียน]การใช้งาน Mercurial หน้าก่อนหน้าคือ [:ThaiTutorialExport], หน้าถัดไปคือ [:ThaiTutorialConflict])'' ''(หน้านี้เป็นหน้าที่ 7 จาก 9 ของ[:ThaiTutorial:บทเรียน]การใช้งาน Mercurial หน้าก่อนหน้าคือ [:ThaiTutorialExport], หน้าถัดไปคือ [:ThaiTutorialConflict])''

บทเรียน - รวมการแก้ไขจากหลายๆ repository

(หน้านี้เป็นหน้าที่ 7 จาก 9 ของ[:ThaiTutorial:บทเรียน]การใช้งาน Mercurial หน้าก่อนหน้าคือ [:ThaiTutorialExport], หน้าถัดไปคือ [:ThaiTutorialConflict])

ใน [:ThaiTutorialExport] เราได้เรียนวิธีการแบ่งปันการแก้ไขกับบุคคลอื่น ในบทนี้เราจะเรียนรู้วิธีการ[:Merge:รวมการแก้ไข]โดยการ[:Pull:ดึงการแก้ไข]จาก [:Repository:repository] ที่มีการแก้ไขที่แตกต่างกัน

ก่อนอื่นเราจะต้องสร้าง repository สำหรับรวมการแก้ไข เราต้อง[:Clone:ทำสำเนา] my-hello repository อีกแล้ว:

$ cd ..
$ hg clone my-hello my-hello-desc
updating working directory
2 files updated, 0 files merged, 0 files removed, 0 files unresolved

ทีนี้เราจะแก้ไฟล์ hello.c โดยเพิ่มคำอธิบายในส่วนคอมเม้นต์

$ cd my-hello-desc
$ vi hello.c

เปลี่ยนบรรทัดที่สองจาก:

 * hello.c

เป็น:

 * hello.c - hello, world

จากนั้นเราต้องบันทึกและปิด editor และ[:Commit:คอมมิท]การแก้ไขของเรา ครั้งนี้เราจะประหยัดเวลาลงหน่อยนึงโดยการใช้ตัวเลือก -m กับคำสั่ง commit เพื่อจะได้ไม่ต้องไปพิมพ์คำอธิบาย changeset ใน editor:

$ hg commit -m "Add description of hello.c"

ตอนนี้เรามีการแก้ไขครั้งนึงกับไฟล์ hello.c ใน repository my-hello-new-output และอีกการแก้ไขนึงกับไฟล์ hello.c ใน repository my-hello-desc เราจะรวมการแก้ไขที่แตกต่างกันในสอง repository นี้ยังไงล่ะ? และจะมีปัญหาอะไรหรือเปล่าถ้าเราต้องการดึงการเปลี่ยนแปลงจาก repository นึงไปในอีก repository นึง?

ไม่ต้องเป็นห่วง จะไม่มีปัญหาอะไรหรอก ระหว่างที่เราอยู่ใน my-hello-desc เราสามารถลองดึงการแก้ไขจาก my-hello-new-output และดูกันว่าจะเกิดอะไรขึ้น:

$ hg pull ../my-hello-new-output
pulling from ../my-hello-new-output
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files (+1 heads)
(run 'hg heads' to see heads, 'hg merge' to merge)

Mercurial บอกว่ามันได้เพิ่ม [:Head:head] อีกอันนึงใน repository ของเรา ถ้าเราอยากดู heads เราต้องใช้คำสั่ง heads:

$ hg heads
changeset:   3:86794f718fb1
tag:         tip
parent:      1:82e55d328c8c
user:        mpm@selenic.com
date:        Mon May 05 01:20:46 2008 +0200
summary:     Express great joy at existence of Mercurial

changeset:   2:c3844fde99f0
user:        mpm@selenic.com
date:        Tue May 06 20:10:35 2008 +0200
summary:     Add description of hello.c

Mercurial มาพร้อมกับ [:UsingExtensions:extension] ที่สามารถแสดงกราฟประวัติการแก้ไขของ repository ในแบบ ASCII ได้นั่นก็คือ: [:GraphlogExtension] คุณเพียงแค่ต้องใช้งาน extension นี้ในไฟล์ ~/.hgrc ของคุณ (หรือในไฟล์ Mercurial.ini ในวินโดวส์) โดยเพิ่มบรรทัด hgext.graphlog =  ในส่วน [extensions] แบบนี้:

[extensions]
hgext.graphlog = 

หลังจากคุณเริ่มใช้งาน extension นี้แล้วคุณจะสามารถใช้คำสั่งใหม่คือ glog ได้ทันที (ลองดู hg help glog):

$ hg glog
o  changeset:   3:86794f718fb1
|  tag:         tip
|  parent:      1:82e55d328c8c
|  user:        mpm@selenic.com
|  date:        Mon May 05 01:20:46 2008 +0200
|  summary:     Express great joy at existence of Mercurial
|
| @  changeset:   2:c3844fde99f0
|/   user:        mpm@selenic.com
|    date:        Tue May 06 20:10:35 2008 +0200
|    summary:     Add description of hello.c
|
o  changeset:   1:82e55d328c8c
|  user:        mpm@selenic.com
|  date:        Fri Aug 26 01:21:28 2005 -0700
|  summary:     Create a makefile
|
o  changeset:   0:0a04b987be5a
   user:        mpm@selenic.com
   date:        Fri Aug 26 01:20:50 2005 -0700
   summary:     Create a standard "hello, world" program

ในกราฟนี้ คุณจะเห็นว่าเราได้ดึง changeset 86794f718fb1 เข้ามาใน repository (ซึ่งก็เป็น[:Tip:ปลาย]ของ repository ด้วย) เนื่องจากว่าทั้ง changeset 86794f718fb1 และ c3844fde99f0 ของเราถูกคอมมิทโดยมี changeset ฐานเดียวกันคือ 82e55d328c8c ([:Parent:บรรพบุรุษ]) จึงเกิดกิ่งการแก้ไขที่แยกกัน – หรือที่มีชื่อเรียกสั้นๆว่า [:Branch:branch] ในตัวอย่างนี้ branch จะมีชีวิตที่ค่อนข้างสั้น และการจัดการกับการแตกกิ่งแบบนี้ก็เป็นเรื่องที่ง่ายมากใน Mercurial

ระหว่างที่ดูผลลัพธ์ของคำสั่ง pull คุณอาจจะคิดว่ามันดูคล้ายๆกับผลลัพธ์ในบท [:ThaiTutorialShareChange] นี่นา เพราะอย่างนั้นคุณอาจจะถามว่า: ทำไมไม่ลองใช้คำสั่ง [:Update:update] เลยล่ะ อย่างที่เราทำในบท ThaiTutorialShareChange? ไม่มีปัญหา เรามาลองดูและดูว่าจะเกิดอะไรขึ้น:

$ hg update
abort: update spans branches, use 'hg merge' or 'hg update -C' to lose changes

โอ๊ะโอ มีบางอย่างที่แตกต่างไปแล้ว อย่างที่คุณเห็น Mercurial ระมัดระวังมากและไม่พยายามทำอะไรโง่ๆถ้าเรามองข้ามการแจ้งเตือน "(+1 heads)" หลังจากที่เราใช้คำสั่ง pull เพราะฉะนั้นเราสามารถแน่ใจได้ว่า Mercurial จะเตือนเราดีๆว่าเราน่าจะทำอย่างอื่นมากกว่า ซึ่งในกรณีนี้ก็คือ เราควรจะรวมการแก้ไข

แต่ก่อนที่เราจะรวมการแก้ไขเรามาตรวจสอบก่อนดีกว่าว่าตอนนี้เรากำลังทำงานอยู่กับ changeset ไหน เราสามารถใช้คำสั่ง parents เพื่อการนี้ (ดู [:Parent]):

$ hg parents
changeset:   2:c3844fde99f0
user:        mpm@selenic.com
date:        Tue May 06 20:10:35 2008 +0200
summary:     Add description of hello.c

คำสั่งนี้บอกเราว่าตอนนี้[:WorkingDirectory:ไดเร็คทอรี่ทำงาน]ของเราอิงกับ changeset c3844fde99f0 – ซึ่งก็แน่นอนเพราะว่านั่นเป็น changeset ที่เราเพิ่งคอมมิทไป สิ่งสำคัญที่เราจะต้องเข้าใจก็คือ การดึงการแก้ไขเข้ามาเป็นการดึงมาเก็บไว้ที่ store ของ repository ของเราเท่านั้นแต่ไม่ได้แก้ไขข้อมูลใดๆในไดเร็คทอรี่ทำงานของเราเลยแม้แต่น้อย ลองดูผลลัพธ์ของคำสั่ง glog อีกทีสิ: มี changeset นึงที่ถูกให้สัญลักษณ์ด้วยเครื่องหมาย "@" นั่นหมายความว่า changeset นั้นเป็นบรรพบุรุษของไดเร็คทอรี่ที่เรากำลังทำงานอยู่ หรืออีกนัยนึงก็คือ เรากำลัง "อยู่ที่" c3844fde99f0

โอเค เรามาลองรวมการแก้ไขกันเลยดีกว่า การรวมการแก้ไขฟังดูค่อนข้างยากเนอะ? แต่จริงๆแล้วมันเป็นอะไรที่ง่ายมาก เรามาลองทำตามขั้นตอนที่อยู่ในบรรทัดสุดท้ายของผลลัพธ์จากคำสั่ง pull กันดู:

$ hg merge
merging hello.c
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

แค่นั้นแหละ! Mercurial ได้ทำการรวมการเปลี่ยนแปลงให้คุณโดยอัตโนมัติเพราะว่าการเปลี่ยนแปลงทั้งสองนั้นไม่มีอะไรที่ขัดแย้งกันเลย (เราจะพูดถึงการเปลี่ยนแปลงที่มีความขัดแย้งในบท [:ThaiTutorialConflict]) ทีนี้ถ้าเราลองดูเนื้อหาของไฟล์ hello.c ในไดเร็คทอรี่ทำงานเราจะเห็นว่าไฟล์มีเนื้อหาจากการเปลี่ยนแปลงทั้งที่มาจาก my-hello-new-output และ my-hello-desc ตามคาด

เวลาที่คุณทำงานกับการแก้ไขจากคนอื่น กรณีตัวอย่างนี้เป็นกรณีที่จะเกิดขึ้นบ่อยเป็นที่สอง กรณีที่เกิดบ่อยที่คุณนั้นยิ่งง่ายกว่านี้อีกนั่นก็คือการแก้ไขทั้งสองเป็นการแก้ไขที่เกิดขึ้นกับคนละไฟล์ ซึ่งก็หมายความว่าเวลาคุณใช้คำสั่ง merge จะไม่มีไฟล์อะไรที่จะต้องรวมการแก้ไขด้วยซ้ำ

โอเค ตอนนี้เรายังไม่ได้คอมมิทผลของการรวม ก่อนที่เราจะทำลองมาดูกันว่าผลลัพธ์ของคำสั่ง parents จะเป็นยังไง (เราใช้คำสั่งย่อ par):

$ hg par
changeset:   2:c3844fde99f0
user:        mpm@selenic.com
date:        Tue May 06 20:10:35 2008 +0200
summary:     Add description of hello.c

changeset:   3:86794f718fb1
tag:         tip
parent:      1:82e55d328c8c
user:        mpm@selenic.com
date:        Mon May 05 01:20:46 2008 +0200
summary:     Express great joy at existence of Mercurial

คำสั่งนี้บอกเราว่าไดเร็คทอรี่ทำงานของเรานั้นอิงกับ changesets สองอัน: หรือจะเรียกว่ามีบรรพบุรุษสองอันก็ได้ สังเกตุว่าบรรพบุรุษทั้งสองได้ถูกเรียงลำดับ: บรรพบุรุษแรก (parent 1) คือ changeset ก่อนหน้าของเรานั่นก็คือ c3844fde99f0 ที่เราเพิ่มคำอธิบายใน hello.c changeset นี้เป็นบรรพบุรุษของไดเร็คทอรี่ทำงานของเราก่อนที่เราจะใช้คำสั่ง merge ส่วนบรรพบุรุษที่สอง (parent 2) คือ changeset 86794f718fb1 ซึ่งเราได้ดึงมาจากอีก repository นึงและเป็น changeset ที่เราเพิ่งรวมเข้าไป

ลองใช้คำสั่ง glog อีกทีแล้วเราจะเห็นว่าสัญลักษณ์ @ อยู่กับบรรพบุรุษทั้งสองแล้ว:

$ hg glog
@  changeset:   3:86794f718fb1
|  tag:         tip
|  parent:      1:82e55d328c8c
|  user:        mpm@selenic.com
|  date:        Mon May 05 01:20:46 2008 +0200
|  summary:     Express great joy at existence of Mercurial
|
| @  changeset:   2:c3844fde99f0
|/   user:        mpm@selenic.com
|    date:        Tue May 06 20:10:35 2008 +0200
|    summary:     Add description of hello.c
|
o  changeset:   1:82e55d328c8c
|  user:        mpm@selenic.com
|  date:        Fri Aug 26 01:21:28 2005 -0700
|  summary:     Create a makefile
|
o  changeset:   0:0a04b987be5a
   user:        mpm@selenic.com
   date:        Fri Aug 26 01:20:50 2005 -0700
   summary:     Create a standard "hello, world" program

/!\ หมายเหตุ: ถ้าคุณเกิดเปลี่ยนใจและต้องการยกเลิกการรวมก่อนที่จะคอมมิทอะไร คุณจะต้องใช้คำสั่ง "hg update -C -r." เพื่อย้อนกลับไปที่ revision 2 การใช้คำสั่ง hg revert -r2 --all จะแค่ยกเลิกการรวมเนื้อหาของไฟล์ในไดเร็คทอรี่ทำงานของคุณ แต่จะ ไม่แก้ไขบรรพบุรุษของไดเร็คทอรี่ทำงานกลับเป็นบรรพบุรุษเดียว (ดู [:Revert])

ขั้นตอนสุดท้ายก็อย่าลืมคอมมิท[:LocalModifications:การแก้ไขในเครื่องของเรา]ตามที่คำสั่ง merge ก่อนหน้าได้แนะนำให้เราทำ:

$ hg commit -m "Merged changes from my-hello-new-output"

คำสั่งนี้จะไม่แสดงผลลัพธ์ใดๆ ตอนนี้การรวมไฟล์ได้ถูกบันทึกใน store ของ repository เราเป็น [:MergeChangeset:merge changeset] ใหม่แล้วและเราสามารถดูประวัติได้โดยใช้คำสั่ง glog:

$ hg glog
@    changeset:   4:d2ecac0134d8
|\   tag:         tip
| |  parent:      2:c3844fde99f0
| |  parent:      3:86794f718fb1
| |  user:        mpm@selenic.com
| |  date:        Tue May 06 23:44:19 2008 +0200
| |  summary:     Merged changes from my-hello-new-output
| |
| o  changeset:   3:86794f718fb1
| |  parent:      1:82e55d328c8c
| |  user:        mpm@selenic.com
| |  date:        Mon May 05 01:20:46 2008 +0200
| |  summary:     Express great joy at existence of Mercurial
| |
o |  changeset:   2:c3844fde99f0
|/   user:        mpm@selenic.com
|    date:        Tue May 06 20:10:35 2008 +0200
|    summary:     Add description of hello.c
|
o  changeset:   1:82e55d328c8c
|  user:        mpm@selenic.com
|  date:        Fri Aug 26 01:21:28 2005 -0700
|  summary:     Create a makefile
|
o  changeset:   0:0a04b987be5a
   user:        mpm@selenic.com
   date:        Fri Aug 26 01:20:50 2005 -0700
   summary:     Create a standard "hello, world" program

เรายังสามารถแสดงเนื้อหาที่เปลี่ยนแปลงในการรวมไฟล์โดยใช้คำสั่ง annotate เพื่อแสดงข้อมูลของ changeset ในแต่ละบรรทัดของไฟล์ได้ หมายเหตุ: revision 2 คือการแก้ไขที่เราทำกับ repository my-hello-desc และ revision 3 คือการแก้ไขที่เราดึงมาจาก my-hello-new-output แะรวมเข้าไปกับ repository my-hello-desc

$ hg annotate hello.c
0: /*
2:  * hello.c - hello, world
0:  *
0:  * Placed in the public domain by Bryan O'Sullivan
0:  *
0:  * This program is not covered by patents in the United States or other
0:  * countries.
0:  */
0:
0: #include <stdio.h>
0:
0: int main(int argc, char **argv)
0: {
0:      printf("hello, world!\n");
3:      printf("sure am glad I'm using Mercurial!\n");
0:      return 0;
0: }

เรามาลองดูกันว่าถ้าเกิดการเปลี่ยนแปลงทั้งสองมีการแก้ไขที่[:Conflict:ขัดแย้ง]เราจะทำอย่างไรในบท [:ThaiTutorialConflict]


CategoryTutorial

ThaiTutorialMerge (last edited 2009-05-19 19:30:55 by localhost)